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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23