/[eiffelstudio]/branches/eth/eve/Src/framework/code_analysis/ca_code_analyzer.e
ViewVC logotype

Contents of /branches/eth/eve/Src/framework/code_analysis/ca_code_analyzer.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 95855 - (show annotations)
Tue Oct 7 03:40:41 2014 UTC (5 years, 3 months ago) by schmisam
File size: 16029 byte(s)
Added rules 50 and 87.
Added fix for rule 50.
Changed implementation of execution of fixes to increase performance in certain cases.
Added testcases for new rules.

1 note
2 description: "THE Code Analyzer."
3 author: "Stefan Zurfluh"
4 date: "$Date$"
5 revision: "$Revision$"
6
7 class
8 CA_CODE_ANALYZER
9
10 inherit
11 SHARED_EIFFEL_PROJECT
12
13 CA_SHARED_NAMES
14
15 create
16 make
17
18 feature {NONE} -- Initialization
19
20 make
21 -- Initialization for `Current'.
22 do
23 create settings.make
24 create rules.make_caseless (100) -- Rule IDs should be case insensitive.
25
26 -- Adding the rules.
27 add_rule (create {CA_SELF_ASSIGNMENT_RULE}.make)
28 add_rule (create {CA_UNUSED_ARGUMENT_RULE}.make)
29 add_rule (create {CA_NPATH_RULE}.make (settings.preference_manager))
30 add_rule (create {CA_EMPTY_IF_RULE}.make)
31 add_rule (create {CA_FEATURE_NEVER_CALLED_RULE}.make)
32 add_rule (create {CA_CQ_SEPARATION_RULE}.make)
33 add_rule (create {CA_UNNEEDED_OT_LOCAL_RULE}.make)
34 add_rule (create {CA_UNNEEDED_OBJECT_TEST_RULE}.make)
35 add_rule (create {CA_NESTED_COMPLEXITY_RULE}.make (settings.preference_manager))
36 add_rule (create {CA_MANY_ARGUMENTS_RULE}.make (settings.preference_manager))
37 add_rule (create {CA_CREATION_PROC_EXPORTED_RULE}.make)
38 add_rule (create {CA_VARIABLE_NOT_READ_RULE}.make)
39 add_rule (create {CA_SEMICOLON_ARGUMENTS_RULE}.make)
40 add_rule (create {CA_VERY_LONG_ROUTINE_RULE}.make (settings.preference_manager))
41 add_rule (create {CA_VERY_BIG_CLASS_RULE}.make (settings.preference_manager))
42 add_rule (create {CA_FEATURE_SECTION_COMMENT_RULE}.make)
43 add_rule (create {CA_FEATURE_NOT_COMMENTED_RULE}.make)
44 add_rule (create {CA_BOOLEAN_RESULT_RULE}.make)
45 add_rule (create {CA_BOOLEAN_COMPARISON_RULE}.make)
46 add_rule (create {CA_VERY_SHORT_IDENTIFIER_RULE}.make (settings.preference_manager))
47 add_rule (create {CA_VERY_LONG_IDENTIFIER_RULE}.make (settings.preference_manager))
48 add_rule (create {CA_MISSING_IS_EQUAL_RULE}.make)
49 add_rule (create {CA_SIMPLIFIABLE_BOOLEAN_RULE}.make)
50 add_rule (create {CA_SELF_COMPARISON_RULE}.make)
51 add_rule (create {CA_TODO_RULE}.make)
52 add_rule (create {CA_WRONG_LOOP_ITERATION_RULE}.make)
53 add_rule (create {CA_INSPECT_INSTRUCTIONS_RULE}.make (settings.preference_manager))
54 add_rule (create {CA_ATTRIBUTE_TO_LOCAL_RULE}.make)
55 add_rule (create {CA_EMPTY_EFFECTIVE_ROUTINE_RULE}.make)
56 add_rule (create {CA_IF_ELSE_NOT_EQUAL_RULE}.make)
57 add_rule (create {CA_SHORT_CIRCUIT_IF_RULE}.make)
58 add_rule (create {CA_ITERABLE_LOOP_RULE}.make)
59 add_rule (create {CA_COUNT_EQUALS_ZERO_RULE}.make)
60 add_rule (create {CA_DEEPLY_NESTED_IF_RULE}.make (settings.preference_manager))
61 add_rule (create {CA_UNNEEDED_HELPER_VARIABLE_RULE}.make (settings.preference_manager))
62 add_rule (create {CA_UNNEEDED_PARENTHESES_RULE}.make)
63 add_rule (create {CA_CLASS_NAMING_CONVENTION_RULE}.make)
64 add_rule (create {CA_FEATURE_NAMING_CONVENTION_RULE}.make)
65 add_rule (create {CA_LOCAL_NAMING_CONVENTION_RULE}.make (settings.preference_manager))
66 add_rule (create {CA_ARGUMENT_NAMING_CONVENTION_RULE}.make (settings.preference_manager))
67 add_rule (create {CA_UNNECESSARY_SIGN_OPERATOR_RULE}.make)
68 add_rule (create {CA_EMPTY_UNCOMMENTED_ROUTINE_RULE}.make)
69 add_rule (create {CA_UNNEEDED_ACCESSOR_FUNCTION_RULE}.make)
70 add_rule (create {CA_MERGEABLE_FEATURE_CLAUSES_RULE}.make)
71 add_rule (create {CA_EMPTY_RESCUE_CLAUSE_RULE}.make)
72 add_rule (create {CA_INSPECT_NO_WHEN_RULE}.make)
73 add_rule (create {CA_EXPLICIT_REDUNDANT_INHERITANCE_RULE}.make)
74 add_rule (create {CA_UNDESIRABLE_COMMENT_CONTENT_RULE}.make (settings.preference_manager))
75 add_rule (create {CA_INHERIT_FROM_ANY_RULE}.make)
76 add_rule (create {CA_DOUBLE_NEGATION_RULE}.make)
77 add_rule (create {CA_EMPTY_LOOP_RULE}.make)
78 add_rule (create {CA_MISSING_CREATION_PROC_WITHOUT_ARGS_RULE}.make)
79 add_rule (create {CA_COMMENT_NOT_WELL_PHRASED_RULE}.make)
80 add_rule (create {CA_OBJECT_CREATION_WITHIN_LOOP_RULE}.make)
81 add_rule (create {CA_EMPTY_CREATION_PROC_RULE}.make)
82 add_rule (create {CA_VOID_CHECK_USING_IS_EQUAL_RULE}.make)
83 add_rule (create {CA_COMPARISON_OF_OBJECT_REFS_RULE}.make)
84 add_rule (create {CA_ATTRIBUTE_CAN_BE_CONSTANT_RULE}.make)
85 add_rule (create {CA_LOOP_INVARIANT_COMPUTATION_RULE}.make)
86 add_rule (create {CA_UNREACHABLE_CODE_RULE}.make)
87 add_rule (create {CA_OBJECT_TEST_FAILING_RULE}.make)
88 add_rule (create {CA_USELESS_CONTRACT_RULE}.make)
89 add_rule (create {CA_REAL_NAN_COMPARISON_RULE}.make)
90 add_rule (create {CA_LOCAL_USED_FOR_RESULT_RULE}.make)
91 add_rule (create {CA_MERGEABLE_CONDITIONALS_RULE}.make)
92
93 settings.initialize_rule_settings (rules)
94
95 create classes_to_analyze.make
96 create rule_violations.make (100)
97 create completed_actions
98 create output_actions
99
100 create ignoredby.make (25)
101 create library_class.make (25)
102 create nonlibrary_class.make (25)
103 end
104
105 feature -- Analysis interface
106
107 add_completed_action (a_action: attached PROCEDURE [ANY, TUPLE [ITERABLE [TUPLE [detachable EXCEPTION, CLASS_C]]]])
108 -- Adds `a_action' to the list of procedures that will be
109 -- called when analysis has completed.
110 do
111 completed_actions.extend (a_action)
112 end
113
114 add_output_action (a_action: attached PROCEDURE [ANY, TUPLE [READABLE_STRING_GENERAL]])
115 -- Adds `a_action' to the procedures that are called for outputting status. The final results
116 -- (rule violations) are not given to these procedures.
117 do
118 output_actions.extend (a_action)
119 end
120
121 analyze
122 -- Analyze all the classes that have been added.
123 require
124 not is_running
125 local
126 l_rules_checker: CA_ALL_RULES_CHECKER
127 l_task: CA_RULE_CHECKING_TASK
128 l_rules_to_check: LINKED_LIST [CA_RULE]
129 do
130 is_running := True
131
132 create l_rules_checker.make
133 create l_rules_to_check.make
134 across rules as l_rules loop
135 l_rules.item.clear_violations
136 if is_rule_checkable (l_rules.item) then
137 l_rules_to_check.extend (l_rules.item)
138 -- Here we only prepare standard rules. The rule checking task will iterate again
139 -- through the rules and run the analysis on the enabled rules.
140 if attached {CA_STANDARD_RULE} l_rules.item as l_std_rule then
141 l_std_rule.prepare_checking (l_rules_checker)
142 end
143 end
144 end
145
146 create l_task.make (l_rules_checker, l_rules_to_check, classes_to_analyze, agent analysis_completed)
147 l_task.set_output_actions (output_actions)
148 if attached rota as l_rota then
149 rota.run_task (l_task)
150 else
151 -- No ROTA task is available, we execute the task synchronously.
152 from
153
154 until
155 not l_task.has_next_step
156 loop
157 l_task.step
158 end
159 end
160 end
161
162 is_rule_checkable (a_rule: attached CA_RULE): BOOLEAN
163 -- Will `a_rule' be checked based on the current preferences and based on the current
164 -- checking scope?
165 do
166 Result := a_rule.is_enabled.value
167 and then (system_wide_check or else (not a_rule.is_system_wide))
168 and then is_severity_enabled (a_rule.severity)
169 end
170
171 clear_classes_to_analyze
172 -- Removes all classes that have been added to the list of classes
173 -- to analyze.
174 do
175 classes_to_analyze.wipe_out
176 end
177
178 add_whole_system
179 -- Adds all the classes that are part of the current system. Classes of referenced libraries
180 -- will not be added.
181 do
182 across
183 eiffel_universe.groups as l_groups
184 loop
185 -- Only load top-level clusters, as the others will be loaded recursively afterwards.
186 if attached {CLUSTER_I} l_groups.item as l_cluster and then l_cluster.parent_cluster = Void then
187 add_cluster (l_cluster)
188 end
189 end
190
191 system_wide_check := True
192 end
193
194 add_cluster (a_cluster: attached CLUSTER_I)
195 -- Add all classes of cluster `a_cluster'.
196 do
197 system_wide_check := False
198
199 if a_cluster.classes /= Void then
200 across a_cluster.classes as ic loop
201 add_class (ic.item)
202 end
203 end
204
205 if a_cluster.sub_clusters /= Void then
206 across a_cluster.sub_clusters as ic loop
207 add_cluster (ic.item)
208 end
209 end
210 end
211
212 add_group (a_group: attached CONF_GROUP)
213 -- Add all classes of the configuration group `a_group'.
214 require
215 a_group_not_void: a_group /= Void
216 do
217 if a_group.classes /= Void then
218 across a_group.classes as ic loop
219 add_class (ic.item)
220 end
221 end
222 end
223
224 add_classes (a_classes: attached ITERABLE [attached CONF_CLASS])
225 -- Add the classes `a_classes'.
226 do
227 system_wide_check := False
228
229 across a_classes as l_classes loop
230 add_class (l_classes.item)
231 end
232 end
233
234 add_class (a_class: attached CONF_CLASS)
235 -- Adds class `a_class'.
236 do
237 system_wide_check := False
238
239 if attached {EIFFEL_CLASS_I} a_class as l_eiffel_class
240 and then attached l_eiffel_class.compiled_class as l_compiled
241 then
242 classes_to_analyze.extend (l_compiled)
243
244 extract_indexes (l_compiled)
245 else
246 output_actions.call ([ca_messages.class_skipped (a_class.name)])
247 end
248 end
249
250
251 force_preferences (a_preferences: LIST [TUPLE [rule_id: READABLE_STRING_GENERAL; preference_name: STRING; preference_value: READABLE_STRING_GENERAL]])
252 -- Forcefully set the preferences in `a_preferences' to the specified values,
253 -- overwriting the current ones.
254 local
255 l_full_preference_name: STRING
256 l_preference: PREFERENCE
257 do
258 -- Disable all rules.
259 across rules as ic loop
260 ic.item.is_enabled.set_value (False)
261 end
262
263 across a_preferences as ic loop
264 if rules.has_key (ic.item.rule_id) then
265 l_full_preference_name := rules.found_item.full_preference_name (ic.item.preference_name)
266 l_preference := preferences.get_preference (l_full_preference_name)
267
268 if attached l_preference then
269 l_preference.set_value_from_string (ic.item.preference_value)
270 else
271 output_actions.call ([ca_messages.preference_not_found (l_full_preference_name)])
272 end
273
274 else
275 output_actions.call ([ca_messages.rule_not_found (ic.item.rule_id)])
276 end
277 end
278 end
279
280
281 feature -- Properties
282
283 is_running: BOOLEAN
284 -- Is code analysis running?
285
286 rules: STRING_TABLE [CA_RULE]
287 -- Table containing the rules that will be used for analysis. Rules are indexed by ID.
288
289 add_rule (a_rule: CA_RULE)
290 -- Adds `a_rule' to the rules list.
291 require
292 not_added_yet: not rules.has (a_rule.id)
293 do
294 rules.extend (a_rule, a_rule.id)
295 ensure
296 added: rules.has (a_rule.id)
297 end
298
299 rule_violations: detachable HASH_TABLE [SORTED_TWO_WAY_LIST [CA_RULE_VIOLATION], CLASS_C]
300 -- All found violations from the last analysis.
301
302 settings: CA_SETTINGS
303 -- The settings manager for Code Analysis.
304
305 preferences: PREFERENCES
306 -- Code Analysis preferences.
307 do Result := settings.preferences end
308
309 class_list: ITERABLE [CLASS_C]
310 -- List of classes that have been added.
311 do Result := classes_to_analyze end
312
313 feature {NONE} -- Implementation
314
315 csv_file_name: STRING = "last_analysis_result.csv"
316
317 csv_header: STRING = "Severity;Class;Location;Title;Description;Rule ID;Severity Score"
318
319 analysis_completed (a_exceptions: detachable ITERABLE [TUPLE [detachable EXCEPTION, CLASS_C]])
320 -- Will be called when the analysis task has finished. `a_exceptions'
321 -- contains a list of exception that occurred during analysis.
322 local
323 l_csv_writer: CA_CSV_WRITER
324 do
325 create l_csv_writer.make (eiffel_project.project_location.target_path.extended (csv_file_name), csv_header)
326
327 across rules as l_rules loop
328 across l_rules.item.violations as l_v loop
329 -- Check the ignore list.
330 if is_violation_valid (l_v.item) then
331 -- Make sure a list for this class exists in the hash table:
332 rule_violations.put (create {SORTED_TWO_WAY_LIST [CA_RULE_VIOLATION]}.make, l_v.item.affected_class)
333 -- Add the violation.
334 rule_violations.at (l_v.item.affected_class).extend (l_v.item)
335 -- Log it.
336 l_csv_writer.add_line (l_v.item.csv_line)
337 end
338 end
339 end
340
341 l_csv_writer.close_file
342
343 clear_classes_to_analyze
344
345 is_running := False
346 completed_actions.call ([a_exceptions])
347 completed_actions.wipe_out
348 end
349
350 is_violation_valid (a_viol: attached CA_RULE_VIOLATION): BOOLEAN
351 -- Is the violation `a_viol' valid under the current settings
352 -- such as the rule ignore list of a class, or the library or
353 -- non-library status of a class?
354 local
355 l_affected_class: CLASS_C
356 l_rule: CA_RULE
357 do
358 l_affected_class := a_viol.affected_class
359 l_rule := a_viol.rule
360
361 Result := True
362
363 if ignoredby.has (l_affected_class)
364 and then (ignoredby.at (l_affected_class)).has (l_rule.id) then
365 Result := False
366 end
367
368 if (not l_rule.checks_library_classes) and then library_class.at (l_affected_class) then
369 Result := False
370 end
371
372 if (not l_rule.checks_nonlibrary_classes) and then nonlibrary_class.at (l_affected_class) then
373 Result := False
374 end
375 end
376
377 classes_to_analyze: LINKED_SET [CLASS_C]
378 -- List of classes that shall be analyzed.
379
380 system_wide_check: BOOLEAN
381 -- Shall the whole system be analyzed?
382
383 completed_actions: ACTION_SEQUENCE [TUPLE [ITERABLE [TUPLE [detachable EXCEPTION, CLASS_C]]]]
384 -- List of procedures to call when analysis has completed.
385
386 frozen rota: detachable ROTA_S
387 -- Accesses the rota service.
388 local
389 l_service_consumer: SERVICE_CONSUMER [ROTA_S]
390 do
391 create l_service_consumer
392 if attached l_service_consumer.service as l_service and then l_service.is_interface_usable then
393 Result := l_service
394 end
395 end
396
397 is_severity_enabled (a_severity: attached CA_RULE_SEVERITY): BOOLEAN
398 do
399 Result := (attached {CA_HINT} a_severity and settings.are_hints_enabled.value)
400 or else (attached {CA_SUGGESTION} a_severity and settings.are_suggestions_enabled.value)
401 or else (attached {CA_WARNING} a_severity and settings.are_warnings_enabled.value)
402 or else (attached {CA_ERROR} a_severity and settings.are_errors_enabled.value)
403 end
404
405 output_actions: ACTION_SEQUENCE [TUPLE [READABLE_STRING_GENERAL]]
406 -- Will be called whenever there is a message to output.
407
408 feature {NONE} -- Class-wide Options (From Indexing Clauses)
409
410 extract_indexes (a_class: attached CLASS_C)
411 -- Extracts options from the indexing clause of class `a_class'.
412 local
413 l_ast: CLASS_AS
414 l_ignoredby: LINKED_LIST [STRING_32]
415 do
416 create l_ignoredby.make
417 l_ignoredby.compare_objects -- We want to compare the actual strings.
418 -- Reset the class flags.
419 library_class.force (False, a_class)
420 nonlibrary_class.force (False, a_class)
421 l_ast := a_class.ast
422
423 if attached l_ast.internal_top_indexes as l_top then
424 search_indexing_tags (l_top, a_class, l_ignoredby)
425 end
426 if attached l_ast.internal_bottom_indexes as l_bottom then
427 search_indexing_tags (l_bottom, a_class, l_ignoredby)
428 end
429
430 ignoredby.force (l_ignoredby, a_class)
431 end
432
433 search_indexing_tags (a_clause: attached INDEXING_CLAUSE_AS; a_class: attached CLASS_C; a_ignoredby: attached LINKED_LIST [STRING_32])
434 -- Searches `a_clause' for settings relevant to code analysis.
435 local
436 l_item: STRING_32
437 do
438 across a_clause as ic loop
439 if attached ic.item.tag as l_tag then
440 if l_tag.name_32.same_string_general ("ca_ignoredby") then
441 -- Class wants to ignore certain rules.
442 across ic.item.index_list as l_list loop
443 l_item := l_list.item.string_value_32
444 l_item.prune_all ('%"')
445 a_ignoredby.extend (l_item)
446 end
447 elseif l_tag.name_32.is_equal ("ca_library") then
448 -- Class has information on whether it is a library class.
449 if not ic.item.index_list.is_empty then
450 l_item := ic.item.index_list.first.string_value_32
451 l_item.to_lower
452 l_item.prune_all ('%"')
453 if l_item.is_equal ("true") then
454 library_class.force (True, a_class)
455 elseif l_item.is_equal ("false") then
456 nonlibrary_class.force (True, a_class)
457 end
458 end
459 end
460 end
461 end
462 end
463
464 ignoredby: HASH_TABLE [LINKED_LIST [STRING_32], CLASS_C]
465 -- Maps classes to lists of rules (rule IDs) the class wants to be ignored by.
466
467 library_class, nonlibrary_class: HASH_TABLE [BOOLEAN, CLASS_C]
468 -- Stores classes that are marked as library or non-library classes.
469
470 invariant
471 -- law_of_non_contradiction: one class must not be both a library_class and a nonlibrary_class
472
473 end

Properties

Name Value
svn:eol-style native
svn:keywords Author Date ID Revision

  ViewVC Help
Powered by ViewVC 1.1.23