reek 6.0.3 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +31 -26
  3. data/.rubocop.yml +5 -3
  4. data/CHANGELOG.md +22 -1
  5. data/CONTRIBUTING.md +3 -0
  6. data/Dockerfile +1 -1
  7. data/Gemfile +7 -7
  8. data/README.md +1 -1
  9. data/bin/code_climate_reek +2 -3
  10. data/lib/reek/ast/ast_node_class_map.rb +1 -1
  11. data/lib/reek/ast/sexp_extensions/arguments.rb +11 -0
  12. data/lib/reek/ast/sexp_extensions/case.rb +1 -1
  13. data/lib/reek/ast/sexp_extensions/if.rb +1 -1
  14. data/lib/reek/ast/sexp_extensions/send.rb +1 -1
  15. data/lib/reek/cli/command/todo_list_command.rb +1 -1
  16. data/lib/reek/cli/options.rb +1 -1
  17. data/lib/reek/code_comment.rb +22 -17
  18. data/lib/reek/context_builder.rb +1 -1
  19. data/lib/reek/rake/task.rb +1 -1
  20. data/lib/reek/report/code_climate/code_climate_formatter.rb +1 -3
  21. data/lib/reek/smell_detectors/base_detector.rb +1 -1
  22. data/lib/reek/smell_warning.rb +1 -1
  23. data/lib/reek/source/source_locator.rb +1 -3
  24. data/lib/reek/spec/should_reek_of.rb +6 -4
  25. data/lib/reek/version.rb +2 -2
  26. data/reek.gemspec +28 -25
  27. metadata +7 -225
  28. data/docs/API.md +0 -174
  29. data/docs/Attribute.md +0 -39
  30. data/docs/Basic-Smell-Options.md +0 -85
  31. data/docs/Boolean-Parameter.md +0 -54
  32. data/docs/Class-Variable.md +0 -40
  33. data/docs/Code-Smells.md +0 -39
  34. data/docs/Command-Line-Options.md +0 -119
  35. data/docs/Control-Couple.md +0 -26
  36. data/docs/Control-Parameter.md +0 -32
  37. data/docs/Data-Clump.md +0 -46
  38. data/docs/Duplicate-Method-Call.md +0 -264
  39. data/docs/Feature-Envy.md +0 -93
  40. data/docs/How-To-Write-New-Detectors.md +0 -132
  41. data/docs/How-reek-works-internally.md +0 -114
  42. data/docs/Instance-Variable-Assumption.md +0 -163
  43. data/docs/Irresponsible-Module.md +0 -47
  44. data/docs/Large-Class.md +0 -16
  45. data/docs/Long-Parameter-List.md +0 -39
  46. data/docs/Long-Yield-List.md +0 -37
  47. data/docs/Manual-Dispatch.md +0 -30
  48. data/docs/Missing-Safe-Method.md +0 -92
  49. data/docs/Module-Initialize.md +0 -62
  50. data/docs/Nested-Iterators.md +0 -59
  51. data/docs/Nil-Check.md +0 -47
  52. data/docs/RSpec-matchers.md +0 -129
  53. data/docs/Rake-Task.md +0 -66
  54. data/docs/Reek-4-to-Reek-5-migration.md +0 -188
  55. data/docs/Reek-Driven-Development.md +0 -46
  56. data/docs/Repeated-Conditional.md +0 -47
  57. data/docs/Simulated-Polymorphism.md +0 -16
  58. data/docs/Smell-Suppression.md +0 -96
  59. data/docs/Style-Guide.md +0 -19
  60. data/docs/Subclassed-From-Core-Class.md +0 -79
  61. data/docs/Too-Many-Constants.md +0 -37
  62. data/docs/Too-Many-Instance-Variables.md +0 -43
  63. data/docs/Too-Many-Methods.md +0 -56
  64. data/docs/Too-Many-Statements.md +0 -54
  65. data/docs/Uncommunicative-Method-Name.md +0 -94
  66. data/docs/Uncommunicative-Module-Name.md +0 -92
  67. data/docs/Uncommunicative-Name.md +0 -18
  68. data/docs/Uncommunicative-Parameter-Name.md +0 -90
  69. data/docs/Uncommunicative-Variable-Name.md +0 -96
  70. data/docs/Unused-Parameters.md +0 -28
  71. data/docs/Unused-Private-Method.md +0 -101
  72. data/docs/Utility-Function.md +0 -56
  73. data/docs/Versioning-Policy.md +0 -7
  74. data/docs/YAML-Reports.md +0 -93
  75. data/docs/defaults.reek.yml +0 -129
  76. data/docs/templates/default/docstring/html/public_api_marker.erb +0 -3
  77. data/docs/templates/default/docstring/setup.rb +0 -35
  78. data/docs/templates/default/fulldoc/html/css/common.css +0 -1
  79. data/docs/yard_plugin.rb +0 -17
  80. data/features/command_line_interface/basic_usage.feature +0 -15
  81. data/features/command_line_interface/options.feature +0 -123
  82. data/features/command_line_interface/show_progress.feature +0 -33
  83. data/features/command_line_interface/smell_selection.feature +0 -15
  84. data/features/command_line_interface/smells_count.feature +0 -38
  85. data/features/command_line_interface/stdin.feature +0 -65
  86. data/features/configuration_files/accept_setting.feature +0 -87
  87. data/features/configuration_files/directory_specific_directives.feature +0 -274
  88. data/features/configuration_files/exclude_directives.feature +0 -35
  89. data/features/configuration_files/exclude_paths_directives.feature +0 -42
  90. data/features/configuration_files/masking_smells.feature +0 -94
  91. data/features/configuration_files/mix_accept_reject_setting.feature +0 -84
  92. data/features/configuration_files/reject_setting.feature +0 -89
  93. data/features/configuration_files/schema_validation.feature +0 -59
  94. data/features/configuration_files/show_configuration_file.feature +0 -44
  95. data/features/configuration_files/unused_private_method.feature +0 -68
  96. data/features/configuration_loading.feature +0 -91
  97. data/features/configuration_via_source_comments/erroneous_source_comments.feature +0 -68
  98. data/features/configuration_via_source_comments/well_formed_source_comments.feature +0 -116
  99. data/features/locales.feature +0 -32
  100. data/features/programmatic_access.feature +0 -41
  101. data/features/rake_task/rake_task.feature +0 -138
  102. data/features/reports/codeclimate.feature +0 -59
  103. data/features/reports/json.feature +0 -59
  104. data/features/reports/reports.feature +0 -219
  105. data/features/reports/yaml.feature +0 -52
  106. data/features/rspec_matcher.feature +0 -41
  107. data/features/samples.feature +0 -305
  108. data/features/step_definitions/.rubocop.yml +0 -5
  109. data/features/step_definitions/reek_steps.rb +0 -102
  110. data/features/step_definitions/sample_file_steps.rb +0 -63
  111. data/features/support/env.rb +0 -33
  112. data/features/todo_list.feature +0 -108
  113. data/samples/checkstyle.xml +0 -7
  114. data/samples/clean_source/clean.rb +0 -6
  115. data/samples/configuration/accepts_rejects_and_excludes_for_detectors.reek.yml +0 -29
  116. data/samples/configuration/accepts_rejects_and_excludes_for_directory_directives.reek.yml +0 -30
  117. data/samples/configuration/corrupt.reek +0 -1
  118. data/samples/configuration/empty.reek +0 -0
  119. data/samples/configuration/full_configuration.reek +0 -13
  120. data/samples/configuration/full_mask.reek +0 -6
  121. data/samples/configuration/home/home.reek.yml +0 -4
  122. data/samples/configuration/partial_mask.reek +0 -4
  123. data/samples/configuration/regular_configuration/.reek.yml +0 -4
  124. data/samples/configuration/regular_configuration/empty_sub_directory/.gitignore +0 -0
  125. data/samples/configuration/with_excluded_paths.reek +0 -5
  126. data/samples/no_config_file/.keep +0 -0
  127. data/samples/paths.rb +0 -5
  128. data/samples/smelly_source/inline.rb +0 -704
  129. data/samples/smelly_source/optparse.rb +0 -1788
  130. data/samples/smelly_source/redcloth.rb +0 -1130
  131. data/samples/smelly_source/ruby.rb +0 -368
  132. data/samples/smelly_source/smelly.rb +0 -7
  133. data/samples/source_with_exclude_paths/ignore_me/uncommunicative_method_name.rb +0 -5
  134. data/samples/source_with_exclude_paths/nested/ignore_me_as_well/irresponsible_module.rb +0 -2
  135. data/samples/source_with_exclude_paths/nested/uncommunicative_parameter_name.rb +0 -6
  136. data/samples/source_with_exclude_paths/nested/uncommunicative_variable_name.rb +0 -6
  137. data/samples/source_with_hidden_directories/.hidden/hidden.rb +0 -1
  138. data/samples/source_with_hidden_directories/not_hidden.rb +0 -1
  139. data/samples/source_with_non_ruby_files/gibberish +0 -1
  140. data/samples/source_with_non_ruby_files/python_source.py +0 -1
  141. data/samples/source_with_non_ruby_files/ruby.rb +0 -6
  142. data/spec/performance/reek/smell_detectors/runtime_speed_spec.rb +0 -15
  143. data/spec/quality/documentation_spec.rb +0 -41
  144. data/spec/quality/reek_source_spec.rb +0 -11
  145. data/spec/reek/ast/node_spec.rb +0 -211
  146. data/spec/reek/ast/object_refs_spec.rb +0 -83
  147. data/spec/reek/ast/reference_collector_spec.rb +0 -47
  148. data/spec/reek/ast/sexp_extensions_spec.rb +0 -498
  149. data/spec/reek/cli/application_spec.rb +0 -168
  150. data/spec/reek/cli/command/report_command_spec.rb +0 -44
  151. data/spec/reek/cli/command/todo_list_command_spec.rb +0 -86
  152. data/spec/reek/cli/options_spec.rb +0 -51
  153. data/spec/reek/cli/silencer_spec.rb +0 -28
  154. data/spec/reek/code_comment_spec.rb +0 -184
  155. data/spec/reek/configuration/app_configuration_spec.rb +0 -195
  156. data/spec/reek/configuration/configuration_file_finder_spec.rb +0 -230
  157. data/spec/reek/configuration/default_directive_spec.rb +0 -13
  158. data/spec/reek/configuration/directory_directives_spec.rb +0 -122
  159. data/spec/reek/configuration/excluded_paths_spec.rb +0 -25
  160. data/spec/reek/configuration/rake_task_converter_spec.rb +0 -33
  161. data/spec/reek/configuration/schema_validator_spec.rb +0 -165
  162. data/spec/reek/context/code_context_spec.rb +0 -192
  163. data/spec/reek/context/ghost_context_spec.rb +0 -60
  164. data/spec/reek/context/method_context_spec.rb +0 -72
  165. data/spec/reek/context/module_context_spec.rb +0 -55
  166. data/spec/reek/context/root_context_spec.rb +0 -12
  167. data/spec/reek/context/statement_counter_spec.rb +0 -24
  168. data/spec/reek/context_builder_spec.rb +0 -457
  169. data/spec/reek/detector_repository_spec.rb +0 -22
  170. data/spec/reek/documentation_link_spec.rb +0 -20
  171. data/spec/reek/errors/base_error_spec.rb +0 -13
  172. data/spec/reek/examiner_spec.rb +0 -309
  173. data/spec/reek/logging_error_handler_spec.rb +0 -24
  174. data/spec/reek/rake/task_spec.rb +0 -56
  175. data/spec/reek/report/code_climate/code_climate_configuration_spec.rb +0 -22
  176. data/spec/reek/report/code_climate/code_climate_fingerprint_spec.rb +0 -126
  177. data/spec/reek/report/code_climate/code_climate_formatter_spec.rb +0 -51
  178. data/spec/reek/report/code_climate/code_climate_report_spec.rb +0 -56
  179. data/spec/reek/report/html_report_spec.rb +0 -19
  180. data/spec/reek/report/json_report_spec.rb +0 -58
  181. data/spec/reek/report/location_formatter_spec.rb +0 -32
  182. data/spec/reek/report/progress_formatter_spec.rb +0 -68
  183. data/spec/reek/report/text_report_spec.rb +0 -89
  184. data/spec/reek/report/xml_report_spec.rb +0 -24
  185. data/spec/reek/report/yaml_report_spec.rb +0 -55
  186. data/spec/reek/report_spec.rb +0 -28
  187. data/spec/reek/smell_configuration_spec.rb +0 -56
  188. data/spec/reek/smell_detectors/attribute_spec.rb +0 -197
  189. data/spec/reek/smell_detectors/base_detector_spec.rb +0 -50
  190. data/spec/reek/smell_detectors/boolean_parameter_spec.rb +0 -93
  191. data/spec/reek/smell_detectors/class_variable_spec.rb +0 -106
  192. data/spec/reek/smell_detectors/control_parameter_spec.rb +0 -300
  193. data/spec/reek/smell_detectors/data_clump_spec.rb +0 -134
  194. data/spec/reek/smell_detectors/duplicate_method_call_spec.rb +0 -211
  195. data/spec/reek/smell_detectors/feature_envy_spec.rb +0 -295
  196. data/spec/reek/smell_detectors/instance_variable_assumption_spec.rb +0 -96
  197. data/spec/reek/smell_detectors/irresponsible_module_spec.rb +0 -226
  198. data/spec/reek/smell_detectors/long_parameter_list_spec.rb +0 -61
  199. data/spec/reek/smell_detectors/long_yield_list_spec.rb +0 -49
  200. data/spec/reek/smell_detectors/manual_dispatch_spec.rb +0 -75
  201. data/spec/reek/smell_detectors/missing_safe_method_spec.rb +0 -68
  202. data/spec/reek/smell_detectors/module_initialize_spec.rb +0 -77
  203. data/spec/reek/smell_detectors/nested_iterators_spec.rb +0 -333
  204. data/spec/reek/smell_detectors/nil_check_spec.rb +0 -100
  205. data/spec/reek/smell_detectors/repeated_conditional_spec.rb +0 -100
  206. data/spec/reek/smell_detectors/subclassed_from_core_class_spec.rb +0 -77
  207. data/spec/reek/smell_detectors/too_many_constants_spec.rb +0 -144
  208. data/spec/reek/smell_detectors/too_many_instance_variables_spec.rb +0 -132
  209. data/spec/reek/smell_detectors/too_many_methods_spec.rb +0 -54
  210. data/spec/reek/smell_detectors/too_many_statements_spec.rb +0 -90
  211. data/spec/reek/smell_detectors/uncommunicative_method_name_spec.rb +0 -78
  212. data/spec/reek/smell_detectors/uncommunicative_module_name_spec.rb +0 -78
  213. data/spec/reek/smell_detectors/uncommunicative_parameter_name_spec.rb +0 -147
  214. data/spec/reek/smell_detectors/uncommunicative_variable_name_spec.rb +0 -201
  215. data/spec/reek/smell_detectors/unused_parameters_spec.rb +0 -114
  216. data/spec/reek/smell_detectors/unused_private_method_spec.rb +0 -205
  217. data/spec/reek/smell_detectors/utility_function_spec.rb +0 -309
  218. data/spec/reek/smell_warning_spec.rb +0 -137
  219. data/spec/reek/source/source_code_spec.rb +0 -79
  220. data/spec/reek/source/source_locator_spec.rb +0 -166
  221. data/spec/reek/spec/should_reek_of_spec.rb +0 -153
  222. data/spec/reek/spec/should_reek_only_of_spec.rb +0 -91
  223. data/spec/reek/spec/should_reek_spec.rb +0 -52
  224. data/spec/reek/spec/smell_matcher_spec.rb +0 -87
  225. data/spec/reek/tree_dresser_spec.rb +0 -46
  226. data/spec/spec_helper.rb +0 -110
  227. data/tasks/configuration.rake +0 -18
  228. data/tasks/console.rake +0 -5
  229. data/tasks/reek.rake +0 -6
  230. data/tasks/rubocop.rake +0 -11
  231. data/tasks/test.rake +0 -32
@@ -1,704 +0,0 @@
1
- #!/usr/local/bin/ruby -w
2
-
3
- ##
4
- # Ruby Inline is a framework for writing ruby extensions in foreign
5
- # languages.
6
- #
7
- # == SYNOPSIS
8
- #
9
- # require 'inline'
10
- # class MyClass
11
- # inline do |builder|
12
- # builder.include "<math.h>"
13
- # builder.c %q{
14
- # long factorial(int max) {
15
- # int i=max, result=1;
16
- # while (i >= 2) { result *= i--; }
17
- # return result;
18
- # }
19
- # }
20
- # end
21
- # end
22
- #
23
- # == DESCRIPTION
24
- #
25
- # Inline allows you to write foreign code within your ruby code. It
26
- # automatically determines if the code in question has changed and
27
- # builds it only when necessary. The extensions are then automatically
28
- # loaded into the class/module that defines it.
29
- #
30
- # You can even write extra builders that will allow you to write
31
- # inlined code in any language. Use Inline::C as a template and look
32
- # at Module#inline for the required API.
33
- #
34
- # == PACKAGING
35
- #
36
- # To package your binaries into a gem, use hoe's INLINE and
37
- # FORCE_PLATFORM env vars.
38
- #
39
- # Example:
40
- #
41
- # rake package INLINE=1
42
- #
43
- # or:
44
- #
45
- # rake package INLINE=1 FORCE_PLATFORM=mswin32
46
- #
47
- # See hoe for more details.
48
- #
49
-
50
- require "rbconfig"
51
- require "digest/md5"
52
- require 'fileutils'
53
- require 'rubygems'
54
-
55
- $TESTING = false unless defined? $TESTING
56
-
57
- class CompilationError < RuntimeError; end
58
-
59
- ##
60
- # The Inline module is the top-level module used. It is responsible
61
- # for instantiating the builder for the right language used,
62
- # compilation/linking when needed, and loading the inlined code into
63
- # the current namespace.
64
-
65
- module Inline
66
- VERSION = '3.7.0'
67
-
68
- WINDOZE = /win(32|64)/ =~ RUBY_PLATFORM
69
- RUBINIUS = defined? RUBY_ENGINE
70
- DEV_NULL = (WINDOZE ? 'nul' : '/dev/null')
71
- GEM = (WINDOZE ? 'gem.bat' : 'gem')
72
- RAKE = if WINDOZE then
73
- 'rake.bat'
74
- elsif RUBINIUS then
75
- File.join(Gem.bindir, 'rake')
76
- else
77
- "#{Gem.ruby} -S rake"
78
- end
79
-
80
-
81
- warn "RubyInline v #{VERSION}" if $DEBUG
82
-
83
- protected
84
-
85
- def self.rootdir
86
- env = ENV['INLINEDIR'] || ENV['HOME']
87
-
88
- # in case both INLINEDIR and HOME aren't defined, and under Windows
89
- # default to HOMEDRIVE + HOMEPATH values
90
- env = ENV['HOMEDRIVE'] + ENV['HOMEPATH'] if env.nil? and WINDOZE
91
-
92
- if env.nil? then
93
- abort "Define INLINEDIR or HOME in your environment and try again"
94
- end
95
-
96
- unless defined? @@rootdir and env == @@rootdir and test ?d, @@rootdir then
97
- rootdir = env
98
- Dir.mkdir rootdir, 0700 unless test ?d, rootdir
99
- Dir.assert_secure rootdir
100
- @@rootdir = rootdir
101
- end
102
-
103
- @@rootdir
104
- end
105
-
106
- def self.directory
107
- directory = File.join(rootdir, ".ruby_inline")
108
- unless defined? @@directory and directory == @@directory
109
- @@directory = File.join(self.rootdir, ".ruby_inline")
110
- end
111
- Dir.assert_secure directory
112
- @@directory
113
- end
114
-
115
- # Inline::C is the default builder used and the only one provided by
116
- # Inline. It can be used as a template to write builders for other
117
- # languages. It understands type-conversions for the basic types and
118
- # can be extended as needed.
119
-
120
- class C
121
-
122
- protected unless $TESTING
123
-
124
- MAGIC_ARITY_THRESHOLD = 15
125
- MAGIC_ARITY = -1
126
-
127
- @@type_map = {
128
- 'char' => [ 'NUM2CHR', 'CHR2FIX' ],
129
- 'char *' => [ 'STR2CSTR', 'rb_str_new2' ],
130
- 'double' => [ 'NUM2DBL', 'rb_float_new' ],
131
- 'int' => [ 'F'+'IX2INT', 'INT2FIX' ],
132
- 'long' => [ 'NUM2INT', 'INT2NUM' ],
133
- 'unsigned int' => [ 'NUM2UINT', 'UINT2NUM' ],
134
- 'unsigned long' => [ 'NUM2UINT', 'UINT2NUM' ],
135
- 'unsigned' => [ 'NUM2UINT', 'UINT2NUM' ],
136
- 'VALUE' => [ '', '' ],
137
- # Can't do these converters because they conflict with the above:
138
- # ID2SYM(x), SYM2ID(x), NUM2DBL(x), F\IX2UINT(x)
139
- }
140
-
141
- def ruby2c(type)
142
- raise ArgumentError, "Unknown type #{type.inspect}" unless @@type_map.has_key? type
143
- @@type_map[type].first
144
- end
145
-
146
- def c2ruby(type)
147
- raise ArgumentError, "Unknown type #{type.inspect}" unless @@type_map.has_key? type
148
- @@type_map[type].last
149
- end
150
-
151
- def strip_comments(src)
152
- # strip c-comments
153
- src = src.gsub(%r%\s*/\*.*?\*/%m, '')
154
- # strip cpp-comments
155
- src = src.gsub(%r%^\s*//.*?\n%, '')
156
- src = src.gsub(%r%[ \t]*//[^\n]*%, '')
157
- src
158
- end
159
-
160
- def parse_signature(src, raw=false)
161
-
162
- sig = self.strip_comments(src)
163
- # strip preprocessor directives
164
- sig.gsub!(/^\s*\#.*(\\\n.*)*/, '')
165
- # strip {}s
166
- sig.gsub!(/\{[^\}]*\}/, '{ }')
167
- # clean and collapse whitespace
168
- sig.gsub!(/\s+/, ' ')
169
-
170
- unless defined? @types then
171
- @types = 'void|' + @@type_map.keys.map{|x| Regexp.escape(x)}.join('|')
172
- end
173
-
174
- if /(#{@types})\s*(\w+)\s*\(([^)]*)\)/ =~ sig then
175
- return_type, function_name, arg_string = $1, $2, $3
176
- args = []
177
- arg_string.split(',').each do |arg|
178
-
179
- # helps normalize into 'char * varname' form
180
- arg = arg.gsub(/\s*\*\s*/, ' * ').strip
181
-
182
- if /(((#{@types})\s*\*?)+)\s+(\w+)\s*$/ =~ arg then
183
- args.push([$4, $1])
184
- elsif arg != "void" then
185
- warn "WAR\NING: '#{arg}' not understood"
186
- end
187
- end
188
-
189
- arity = args.size
190
- arity = MAGIC_ARITY if raw
191
-
192
- return {
193
- 'return' => return_type,
194
- 'name' => function_name,
195
- 'args' => args,
196
- 'arity' => arity
197
- }
198
- end
199
-
200
- raise SyntaxError, "Can't parse signature: #{sig}"
201
- end # def parse_signature
202
-
203
- def generate(src, options={})
204
- options = {:expand_types=>options} unless Hash === options
205
-
206
- expand_types = options[:expand_types]
207
- singleton = options[:singleton]
208
- result = self.strip_comments(src)
209
-
210
- signature = parse_signature(src, !expand_types)
211
- function_name = signature['name']
212
- method_name = options[:method_name] || function_name
213
- return_type = signature['return']
214
- arity = signature['arity']
215
-
216
- raise ArgumentError, "too many arguments" if arity > MAGIC_ARITY_THRESHOLD
217
-
218
- if expand_types then
219
- prefix = "static VALUE #{function_name}("
220
- if arity == MAGIC_ARITY then
221
- prefix += "int argc, VALUE *argv, VALUE self"
222
- else
223
- prefix += "VALUE self"
224
- prefix += signature['args'].map { |arg, type| ", VALUE _#{arg}"}.join
225
- end
226
- prefix += ") {\n"
227
- prefix += signature['args'].map { |arg, type|
228
- " #{type} #{arg} = #{ruby2c(type)}(_#{arg});\n"
229
- }.join
230
-
231
- # replace the function signature (hopefully) with new sig (prefix)
232
- result.sub!(/[^;\/\"\>]+#{function_name}\s*\([^\{]+\{/, "\n" + prefix)
233
- result.sub!(/\A\n/, '') # strip off the \n in front in case we added it
234
- unless return_type == "void" then
235
- raise SyntaxError, "Couldn't find return statement for #{function_name}" unless
236
- result =~ /return/
237
- result.gsub!(/return\s+([^\;\}]+)/) do
238
- "return #{c2ruby(return_type)}(#{$1})"
239
- end
240
- else
241
- result.sub!(/\s*\}\s*\Z/, "\nreturn Qnil;\n}")
242
- end
243
- else
244
- prefix = "static #{return_type} #{function_name}("
245
- result.sub!(/[^;\/\"\>]+#{function_name}\s*\(/, prefix)
246
- result.sub!(/\A\n/, '') # strip off the \n in front in case we added it
247
- end
248
-
249
- delta = if result =~ /\A(static.*?\{)/m then
250
- $1.split(/\n/).size
251
- else
252
- warn "WAR\NING: Can't find signature in #{result.inspect}\n" unless $TESTING
253
- 0
254
- end
255
-
256
- file, line = caller[1].split(/:/)
257
- result = "# line #{line.to_i + delta} \"#{file}\"\n" + result unless $DEBUG and not $TESTING
258
-
259
- @src << result
260
- @sig[function_name] = [arity,singleton,method_name]
261
-
262
- return result if $TESTING
263
- end # def generate
264
-
265
- def module_name
266
- unless defined? @module_name then
267
- module_name = @mod.name.gsub('::','__')
268
- md5 = Digest::MD5.new
269
- @sig.keys.sort_by { |x| x.to_s }.each { |m| md5 << m.to_s }
270
- @module_name = "Inline_#{module_name}_#{md5.to_s[0,4]}"
271
- end
272
- @module_name
273
- end
274
-
275
- def so_name
276
- unless defined? @so_name then
277
- @so_name = "#{Inline.directory}/#{module_name}.#{Config::CONFIG["DLEXT"]}"
278
- end
279
- @so_name
280
- end
281
-
282
- attr_reader :rb_file, :mod
283
- if $TESTING then
284
- attr_writer :mod
285
- attr_accessor :src, :sig, :flags, :libs
286
- end
287
-
288
- public
289
-
290
- def initialize(mod)
291
- raise ArgumentError, "Class/Module arg is required" unless Module === mod
292
- # new (but not on some 1.8s) -> inline -> real_caller|eval
293
- stack = caller
294
- meth = stack.shift until meth =~ /in .(inline|test_|setup)/ or stack.empty?
295
- raise "Couldn't discover caller" if stack.empty?
296
- real_caller = stack.first
297
- real_caller = stack[3] if real_caller =~ /\(eval\)/
298
- real_caller = real_caller.split(/:/, 3)[0..1]
299
- @real_caller = real_caller.join ':'
300
- @rb_file = File.expand_path real_caller.first
301
-
302
- @mod = mod
303
- @src = []
304
- @inc = []
305
- @sig = {}
306
- @flags = []
307
- @libs = []
308
- @init_extra = []
309
- @include_ruby_first = true
310
- end
311
-
312
- ##
313
- # Attempts to load pre-generated code returning true if it succeeds.
314
-
315
- def load_cache
316
- begin
317
- file = File.join("inline", File.basename(so_name))
318
- if require file then
319
- dir = Inline.directory
320
- warn "WAR\NING: #{dir} exists but is not being used" if test ?d, dir and $VERBOSE
321
- return true
322
- end
323
- rescue LoadError
324
- end
325
- return false
326
- end
327
-
328
- ##
329
- # Loads the generated code back into ruby
330
-
331
- def load
332
- require "#{so_name}" or raise LoadError, "require on #{so_name} failed"
333
- end
334
-
335
- ##
336
- # Builds the source file, if needed, and attempts to compile it.
337
-
338
- def build
339
- so_name = self.so_name
340
- so_exists = File.file? so_name
341
- unless so_exists and File.mtime(rb_file) < File.mtime(so_name) then
342
-
343
- unless File.directory? Inline.directory then
344
- warn "NOTE: creating #{Inline.directory} for RubyInline" if $DEBUG
345
- Dir.mkdir Inline.directory, 0700
346
- end
347
-
348
- src_name = "#{Inline.directory}/#{module_name}.c"
349
- old_src_name = "#{src_name}.old"
350
- should_compare = File.write_with_backup(src_name) do |io|
351
- if @include_ruby_first
352
- @inc.unshift "#include \"ruby.h\""
353
- else
354
- @inc.push "#include \"ruby.h\""
355
- end
356
-
357
- io.puts
358
- io.puts @inc.join("\n")
359
- io.puts
360
- io.puts @src.join("\n\n")
361
- io.puts
362
- io.puts
363
- io.puts "#ifdef __cplusplus"
364
- io.puts "extern \"C\" {"
365
- io.puts "#endif"
366
- io.puts " __declspec(dllexport)" if WINDOZE
367
- io.puts " void Init_#{module_name}() {"
368
- io.puts " VALUE c = rb_cObject;"
369
-
370
- # TODO: use rb_class2path
371
- # io.puts " VALUE c = rb_path2class(#{@mod.name.inspect});"
372
- io.puts @mod.name.split("::").map { |n|
373
- " c = rb_const_get(c,rb_intern(\"#{n}\"));"
374
- }.join("\n")
375
-
376
- @sig.keys.sort.each do |name|
377
- arity, singleton, method_name = @sig[name]
378
- if singleton then
379
- io.print " rb_define_singleton_method(c, \"#{method_name}\", "
380
- else
381
- io.print " rb_define_method(c, \"#{method_name}\", "
382
- end
383
- io.puts "(VALUE(*)(ANYARGS))#{name}, #{arity});"
384
- end
385
- io.puts @init_extra.join("\n") unless @init_extra.empty?
386
-
387
- io.puts
388
- io.puts " }"
389
- io.puts "#ifdef __cplusplus"
390
- io.puts "}"
391
- io.puts "#endif"
392
- io.puts
393
- end
394
-
395
- # recompile only if the files are different
396
- recompile = true
397
- if so_exists and should_compare and
398
- FileUtils.compare_file(old_src_name, src_name) then
399
- recompile = false
400
-
401
- # Updates the timestamps on all the generated/compiled files.
402
- # Prevents us from entering this conditional unless the source
403
- # file changes again.
404
- t = Time.now
405
- File.utime(t, t, src_name, old_src_name, so_name)
406
- end
407
-
408
- if recompile then
409
-
410
- hdrdir = %w(srcdir archdir rubyhdrdir).map { |name|
411
- Config::CONFIG[name]
412
- }.find { |dir|
413
- dir and File.exist? File.join(dir, "/ruby.h")
414
- } or abort "ERROR: Can't find header dir for ruby. Exiting..."
415
-
416
- flags = @flags.join(' ')
417
- libs = @libs.join(' ')
418
-
419
- config_hdrdir = if RUBY_VERSION > '1.9' then
420
- "-I #{File.join hdrdir, RbConfig::CONFIG['arch']}"
421
- else
422
- nil
423
- end
424
-
425
- cmd = [ Config::CONFIG['LDSHARED'],
426
- flags,
427
- Config::CONFIG['CCDLFLAGS'],
428
- Config::CONFIG['CFLAGS'],
429
- '-I', hdrdir,
430
- config_hdrdir,
431
- '-I', Config::CONFIG['includedir'],
432
- "-L#{Config::CONFIG['libdir']}",
433
- '-o', so_name.inspect,
434
- File.expand_path(src_name).inspect,
435
- libs,
436
- crap_for_windoze ].join(' ')
437
-
438
- # TODO: remove after osx 10.5.2
439
- cmd += ' -flat_namespace -undefined suppress' if
440
- RUBY_PLATFORM =~ /darwin9\.[01]/
441
- cmd += " 2> #{DEV_NULL}" if $TESTING and not $DEBUG
442
-
443
- warn "Building #{so_name} with '#{cmd}'" if $DEBUG
444
- result = `#{cmd}`
445
- warn "Output:\n#{result}" if $DEBUG
446
- if $? != 0 then
447
- bad_src_name = src_name + ".bad"
448
- File.rename src_name, bad_src_name
449
- raise CompilationError, "error executing #{cmd.inspect}: #{$?}\nRenamed #{src_name} to #{bad_src_name}"
450
- end
451
-
452
- # NOTE: manifest embedding is only required when using VC8 ruby
453
- # build or compiler.
454
- # Errors from this point should be ignored if Config::CONFIG['arch']
455
- # (RUBY_PLATFORM) matches 'i386-mswin32_80'
456
- if WINDOZE and RUBY_PLATFORM =~ /_80$/ then
457
- Dir.chdir Inline.directory do
458
- cmd = "mt /manifest lib.so.manifest /outputresource:so.dll;#2"
459
- warn "Embedding manifest with '#{cmd}'" if $DEBUG
460
- result = `#{cmd}`
461
- warn "Output:\n#{result}" if $DEBUG
462
- if $? != 0 then
463
- raise CompilationError, "error executing #{cmd}: #{$?}"
464
- end
465
- end
466
- end
467
-
468
- warn "Built successfully" if $DEBUG
469
- end
470
-
471
- else
472
- warn "#{so_name} is up to date" if $DEBUG
473
- end # unless (file is out of date)
474
- end # def build
475
-
476
- ##
477
- # Returns extra compilation flags for windoze platforms. Ugh.
478
-
479
- def crap_for_windoze
480
- # gawd windoze land sucks
481
- case RUBY_PLATFORM
482
- when /mswin32/ then
483
- " -link /LIBPATH:\"#{Config::CONFIG['libdir']}\" /DEFAULTLIB:\"#{Config::CONFIG['LIBRUBY']}\" /INCREMENTAL:no /EXPORT:Init_#{module_name}"
484
- when /mingw32/ then
485
- " -Wl,--enable-auto-import -L#{Config::CONFIG['libdir']} -lmsvcrt-ruby18"
486
- when /i386-cygwin/ then
487
- ' -L/usr/local/lib -lruby.dll'
488
- else
489
- ''
490
- end
491
- end
492
-
493
- ##
494
- # Adds compiler options to the compiler command line. No
495
- # preprocessing is done, so you must have all your dashes and
496
- # everything.
497
-
498
- def add_compile_flags(*flags)
499
- @flags.push(*flags)
500
- end
501
-
502
- ##
503
- # Adds linker flags to the link command line. No preprocessing is
504
- # done, so you must have all your dashes and everything.
505
-
506
- def add_link_flags(*flags)
507
- @libs.push(*flags)
508
- end
509
-
510
- ##
511
- # Adds custom content to the end of the init function.
512
-
513
- def add_to_init(*src)
514
- @init_extra.push(*src)
515
- end
516
-
517
- ##
518
- # Registers C type-casts +r2c+ and +c2r+ for +type+.
519
-
520
- def add_type_converter(type, r2c, c2r)
521
- warn "WAR\NING: overridding #{type} on #{caller[0]}" if @@type_map.has_key? type
522
- @@type_map[type] = [r2c, c2r]
523
- end
524
-
525
- ##
526
- # Maps a ruby constant to C (with the same name)
527
-
528
- def map_ruby_const(*names)
529
- names.each do |name|
530
- self.prefix "static VALUE #{name};"
531
- self.add_to_init " #{name} = rb_const_get(c, rb_intern(#{name.to_s.inspect}));"
532
- end
533
- end
534
-
535
- ##
536
- # Maps a C constant to ruby (with the same
537
- # name). +names_and_types+ is a hash that maps the name of the
538
- # constant to its C type.
539
-
540
- def map_c_const(names_and_types)
541
- names_and_types.each do |name, typ|
542
- self.add_to_init " rb_define_const(c, #{name.to_s.inspect}, #{c2ruby(typ.to_s)}(#{name}));"
543
- end
544
- end
545
-
546
- ##
547
- # Adds an include to the top of the file. Don't forget to use
548
- # quotes or angle brackets.
549
-
550
- def include(header)
551
- @inc << "#include #{header}"
552
- end
553
-
554
- ##
555
- # Specifies that the the ruby.h header should be included *after* custom
556
- # header(s) instead of before them.
557
-
558
- def include_ruby_last
559
- @include_ruby_first = false
560
- end
561
-
562
- ##
563
- # Adds any amount of text/code to the source
564
-
565
- def prefix(code)
566
- @src << code
567
- end
568
-
569
- ##
570
- # Adds a C function to the source, including performing automatic
571
- # type conversion to arguments and the return value. The Ruby
572
- # method name can be overridden by providing method_name. Unknown
573
- # type conversions can be extended by using +add_type_converter+.
574
-
575
- def c src, options = {}
576
- options = {
577
- :expand_types => true,
578
- }.merge options
579
- self.generate src, options
580
- end
581
-
582
- ##
583
- # Same as +c+, but adds a class function.
584
-
585
- def c_singleton src, options = {}
586
- options = {
587
- :expand_types => true,
588
- :singleton => true,
589
- }.merge options
590
- self.generate src, options
591
- end
592
-
593
- ##
594
- # Adds a raw C function to the source. This version does not
595
- # perform any type conversion and must conform to the ruby/C
596
- # coding conventions. The Ruby method name can be overridden
597
- # by providing method_name.
598
-
599
- def c_raw src, options = {}
600
- self.generate src, options
601
- end
602
-
603
- ##
604
- # Same as +c_raw+, but adds a class function.
605
-
606
- def c_raw_singleton src, options = {}
607
- options = {
608
- :singleton => true,
609
- }.merge options
610
- self.generate src, options
611
- end
612
-
613
- end # class Inline::C
614
- end # module Inline
615
-
616
- class Module
617
-
618
- ##
619
- # options is a hash that allows you to pass extra data to your
620
- # builder. The only key that is guaranteed to exist is :testing.
621
-
622
- attr_reader :options
623
-
624
- ##
625
- # Extends the Module class to have an inline method. The default
626
- # language/builder used is C, but can be specified with the +lang+
627
- # parameter.
628
-
629
- def inline(lang = :C, options={})
630
- case options
631
- when TrueClass, FalseClass then
632
- warn "WAR\NING: 2nd argument to inline is now a hash, changing to {:testing=>#{options}}" unless options
633
- options = { :testing => options }
634
- when Hash
635
- options[:testing] ||= false
636
- else
637
- raise ArgumentError, "BLAH"
638
- end
639
-
640
- builder_class = begin
641
- Inline.const_get(lang)
642
- rescue NameError
643
- require "inline/#{lang}"
644
- Inline.const_get(lang)
645
- end
646
-
647
- @options = options
648
- builder = builder_class.new self
649
-
650
- yield builder
651
-
652
- unless options[:testing] then
653
- unless builder.load_cache then
654
- builder.build
655
- builder.load
656
- end
657
- end
658
- end
659
- end
660
-
661
- class File
662
-
663
- ##
664
- # Equivalent to +File::open+ with an associated block, but moves
665
- # any existing file with the same name to the side first.
666
-
667
- def self.write_with_backup(path) # returns true if file already existed
668
-
669
- # move previous version to the side if it exists
670
- renamed = false
671
- if test ?f, path then
672
- renamed = true
673
- File.rename path, path + ".old"
674
- end
675
-
676
- File.open(path, "w") do |io|
677
- yield(io)
678
- end
679
-
680
- return renamed
681
- end
682
- end # class File
683
-
684
- class Dir
685
-
686
- ##
687
- # +assert_secure+ checks that if a +path+ exists it has minimally
688
- # writable permissions. If not, it prints an error and exits. It
689
- # only works on +POSIX+ systems. Patches for other systems are
690
- # welcome.
691
-
692
- def self.assert_secure(path)
693
- mode = File.stat(path).mode
694
- unless ((mode % 01000) & 0022) == 0 then
695
- if $TESTING then
696
- raise SecurityError, "Directory #{path} is insecure"
697
- else
698
- abort "#{path} is insecure (#{'%o' % mode}). It may not be group or world writable. Exiting."
699
- end
700
- end
701
- rescue Errno::ENOENT
702
- # If it ain't there, it's certainly secure
703
- end
704
- end