/[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 95701 - (hide annotations)
Sat Aug 23 01:19:55 2014 UTC (5 years, 4 months ago) by schmisam
File size: 15691 byte(s)
Added Fixes for rules: #48, #49, #15, #38, #16, #19 (included in #3), #68, #37, #47
Added code analysis rule #19 and #48
Added tests for rules #19 and #48
Cleaned up a lot of the older rules
Added Mac OSX 64bit specific files to svn ignore property on several folders

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23