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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23