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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23