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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 96586 - (hide annotations)
Thu Feb 5 11:41:26 2015 UTC (4 years, 11 months ago) by schmisam
File size: 16953 byte(s)
Added exact copy of class FIX_UNUSED_LOCAL_APPLICATION to code_analysis library in order to make it available for use.

Added rule #87.
Added testcase for rule #87.

Fixed nested case in rule #50.
Adjusted testcase for rule #50.
Added fix for rule #50.

Changed implementation of fixes to use the new "execute" method for the general "move instruction within loop"-fix and the fixes for rules #47, #22, #37, #45, #16, #38, #15, #49 and #48.

Some code formatting of rules.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23