/[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 94149 - (hide annotations)
Fri Jan 31 17:54:09 2014 UTC (5 years, 11 months ago) by zurfluhs
File size: 12472 byte(s)
Code Analysis: refactorings and improvements

most importantly:
- status progress during analysis in GUI status bar
- 'short circuit if' bugfix
1 zurfluhs 93168 note
2 zurfluhs 93963 description: "THE Code Analyzer."
3 zurfluhs 93168 author: "Stefan Zurfluh"
4     date: "$Date$"
5     revision: "$Revision$"
6    
7     class
8     CA_CODE_ANALYZER
9    
10 zurfluhs 94138 inherit
11 zurfluhs 93168 SHARED_EIFFEL_PROJECT
12 zurfluhs 94138
13 zurfluhs 93179 CA_SHARED_NAMES
14 zurfluhs 93168
15     create
16     make
17    
18     feature {NONE} -- Initialization
19    
20     make
21     -- Initialization for `Current'.
22     do
23 zurfluhs 93475 create settings.make
24 zurfluhs 93168 create rules.make
25 zurfluhs 93963 -- Adding the rules.
26 zurfluhs 93168 rules.extend (create {CA_SELF_ASSIGNMENT_RULE}.make)
27     rules.extend (create {CA_UNUSED_ARGUMENT_RULE}.make)
28 zurfluhs 93526 rules.extend (create {CA_NPATH_RULE}.make (settings.preference_manager))
29 zurfluhs 93216 rules.extend (create {CA_EMPTY_IF_RULE}.make)
30 zurfluhs 93299 rules.extend (create {CA_FEATURE_NEVER_CALLED_RULE}.make)
31 zurfluhs 93300 rules.extend (create {CA_CQ_SEPARATION_RULE}.make)
32 zurfluhs 93428 rules.extend (create {CA_UNNEEDED_OT_LOCAL_RULE}.make)
33 zurfluhs 94118 rules.extend (create {CA_UNNEEDED_OBJECT_TEST_RULE}.make) -- Needs type info.
34 zurfluhs 93526 rules.extend (create {CA_NESTED_COMPLEXITY_RULE}.make (settings.preference_manager))
35 zurfluhs 93544 rules.extend (create {CA_MANY_ARGUMENTS_RULE}.make (settings.preference_manager))
36 zurfluhs 93546 rules.extend (create {CA_CREATION_PROC_EXPORTED_RULE}.make)
37 zurfluhs 93588 rules.extend (create {CA_VARIABLE_NOT_READ_RULE}.make)
38 zurfluhs 93638 rules.extend (create {CA_SEMICOLON_ARGUMENTS_RULE}.make)
39 zurfluhs 93659 rules.extend (create {CA_VERY_LONG_ROUTINE_RULE}.make (settings.preference_manager))
40 zurfluhs 93660 rules.extend (create {CA_VERY_BIG_CLASS_RULE}.make (settings.preference_manager))
41 zurfluhs 93662 rules.extend (create {CA_FEATURE_SECTION_COMMENT_RULE}.make)
42 zurfluhs 93663 rules.extend (create {CA_FEATURE_NOT_COMMENTED_RULE}.make)
43 zurfluhs 93670 rules.extend (create {CA_BOOLEAN_RESULT_RULE}.make)
44 zurfluhs 93671 rules.extend (create {CA_BOOLEAN_COMPARISON_RULE}.make)
45 zurfluhs 93681 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 zurfluhs 93683 rules.extend (create {CA_MISSING_IS_EQUAL_RULE}.make)
48 zurfluhs 93712 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 zurfluhs 93714 rules.extend (create {CA_INSPECT_INSTRUCTIONS_RULE}.make (settings.preference_manager))
53 zurfluhs 93732 rules.extend (create {CA_ATTRIBUTE_TO_LOCAL_RULE}.make)
54 zurfluhs 93734 rules.extend (create {CA_EMPTY_EFFECTIVE_ROUTINE_RULE}.make)
55 zurfluhs 93736 rules.extend (create {CA_IF_ELSE_NOT_EQUAL_RULE}.make)
56 zurfluhs 93737 rules.extend (create {CA_SHORT_CIRCUIT_IF_RULE}.make)
57 zurfluhs 94118 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 zurfluhs 93745 rules.extend (create {CA_DEEPLY_NESTED_IF_RULE}.make (settings.preference_manager))
60 zurfluhs 93899 rules.extend (create {CA_UNNEEDED_HELPER_VARIABLE_RULE}.make (settings.preference_manager))
61 zurfluhs 93896 rules.extend (create {CA_UNNEEDED_PARENTHESES_RULE}.make)
62 zurfluhs 93173
63 zurfluhs 93526 settings.initialize_rule_settings (rules)
64    
65 zurfluhs 93173 create classes_to_analyze.make
66     create rule_violations.make (100)
67 zurfluhs 93333 create completed_actions
68 zurfluhs 94149 create output_actions
69 zurfluhs 93426
70     create ignoredby.make (25)
71     create library_class.make (25)
72     create nonlibrary_class.make (25)
73 zurfluhs 93168 end
74    
75 zurfluhs 93173 feature -- Analysis interface
76 zurfluhs 93168
77 zurfluhs 93712 add_completed_action (a_action: PROCEDURE [ANY, TUPLE [BOOLEAN]])
78 zurfluhs 93963 -- Adds `a_action' to the list of procedures that will be
79     -- called when analysis has completed.
80 zurfluhs 93333 do
81     completed_actions.extend (a_action)
82     end
83    
84 zurfluhs 94149 add_output_action (a_action: PROCEDURE [ANY, TUPLE [READABLE_STRING_GENERAL]])
85     do
86     output_actions.extend (a_action)
87     end
88    
89 zurfluhs 93173 analyze
90 zurfluhs 93963 -- Analyze all the classes that have been added.
91 zurfluhs 93333 require
92     not is_running
93 zurfluhs 93168 local
94 zurfluhs 93173 l_rules_checker: CA_ALL_RULES_CHECKER
95 zurfluhs 93389 l_task: CA_RULE_CHECKING_TASK
96 zurfluhs 93966 l_rules_to_check: LINKED_LIST [CA_RULE]
97 zurfluhs 93168 do
98 zurfluhs 93333 is_running := True
99    
100 zurfluhs 93173 create l_rules_checker.make
101 zurfluhs 93966 create l_rules_to_check.make
102 zurfluhs 93168 across rules as l_rules loop
103 zurfluhs 93377 l_rules.item.clear_violations
104 zurfluhs 93966 if is_rule_checkable (l_rules.item) then
105     l_rules_to_check.extend (l_rules.item)
106     -- Here we only prepare standard rules. The rule checking task will iterate again
107     -- through the rules and run the analysis on the enabled rules.
108     if attached {CA_STANDARD_RULE} l_rules.item as l_std_rule then
109     l_std_rule.prepare_checking (l_rules_checker)
110 zurfluhs 93174 end
111 zurfluhs 93168 end
112     end
113    
114 zurfluhs 93966 create l_task.make (l_rules_checker, l_rules_to_check, classes_to_analyze, agent analysis_completed)
115 zurfluhs 94149 l_task.set_output_actions (output_actions)
116 zurfluhs 93389 rota.run_task (l_task)
117 zurfluhs 93173 end
118    
119 zurfluhs 93966 is_rule_checkable (a_rule: CA_RULE): BOOLEAN
120     do
121     Result := a_rule.is_enabled.value
122     and then (system_wide_check or else (not a_rule.is_system_wide))
123     and then is_severity_enabled (a_rule.severity)
124     end
125    
126 zurfluhs 93173 clear_classes_to_analyze
127 zurfluhs 93963 -- Remove all classes that have been added to the list of classes
128     -- to analyze.
129 zurfluhs 93173 do
130     classes_to_analyze.wipe_out
131     end
132    
133     add_whole_system
134 zurfluhs 93963 -- Add all the classes that are part of the current system. Classes of references libraries
135     -- will not be added.
136 zurfluhs 93173 local
137     l_groups: LIST [CONF_GROUP]
138     l_cluster: CLUSTER_I
139     do
140 zurfluhs 93168 from
141     l_groups := eiffel_universe.groups
142     l_groups.start
143     until
144     l_groups.after
145     loop
146     l_cluster ?= l_groups.item_for_iteration
147 zurfluhs 93897 -- Only load top-level clusters, as the others will be loaded recursively afterwards.
148 zurfluhs 93168 if l_cluster /= Void and then l_cluster.parent_cluster = Void then
149 zurfluhs 93173 add_cluster (l_cluster)
150 zurfluhs 93168 end
151     l_groups.forth
152     end
153 zurfluhs 93174
154     system_wide_check := True
155 zurfluhs 93168 end
156    
157 zurfluhs 93173 add_cluster (a_cluster: CLUSTER_I)
158 zurfluhs 93963 -- Add all classes of cluster `a_cluster'.
159 zurfluhs 93168 local
160     l_conf_class: CONF_CLASS
161     l_class_i: CLASS_I
162     do
163 zurfluhs 93174 system_wide_check := False
164    
165 zurfluhs 93168 from
166     a_cluster.classes.start
167     until
168     a_cluster.classes.after
169     loop
170     l_conf_class := a_cluster.classes.item_for_iteration
171     l_class_i := eiffel_universe.class_named (l_conf_class.name, a_cluster)
172 zurfluhs 93173 add_class (l_class_i)
173 zurfluhs 93168 a_cluster.classes.forth
174     end
175     if a_cluster.sub_clusters /= Void then
176     from
177     a_cluster.sub_clusters.start
178     until
179     a_cluster.sub_clusters.after
180     loop
181 zurfluhs 93173 add_cluster (a_cluster.sub_clusters.item_for_iteration)
182 zurfluhs 93168 a_cluster.sub_clusters.forth
183     end
184     end
185     end
186    
187 zurfluhs 93333 add_group (a_group: CONF_GROUP)
188 zurfluhs 93963 -- Add all classes of the configuration group `a_group'.
189 zurfluhs 93333 require
190     a_group_not_void: a_group /= Void
191     local
192     l_conf_class: CONF_CLASS
193     l_class_i: CLASS_I
194     do
195     from
196     a_group.classes.start
197     until
198     a_group.classes.after
199     loop
200     l_conf_class := a_group.classes.item_for_iteration
201     l_class_i := eiffel_universe.class_named (l_conf_class.name, a_group)
202     add_class (l_class_i)
203     a_group.classes.forth
204     end
205     end
206    
207 zurfluhs 93897 add_classes (a_classes: ITERABLE [CLASS_I])
208 zurfluhs 93963 -- Add the classes `a_classes'.
209 zurfluhs 93168 do
210 zurfluhs 93174 system_wide_check := False
211    
212 zurfluhs 93173 across a_classes as l_classes loop
213     add_class (l_classes.item)
214     end
215 zurfluhs 93168 end
216    
217 zurfluhs 93173 add_class (a_class: CLASS_I)
218 zurfluhs 93963 -- Adds class `a_class'.
219 zurfluhs 93173 local
220     l_class_c: CLASS_C
221 zurfluhs 93168 do
222 zurfluhs 93174 system_wide_check := False
223    
224 zurfluhs 93173 if a_class.is_compiled then
225     l_class_c := a_class.compiled_class
226     check l_class_c /= Void end
227     classes_to_analyze.extend (l_class_c)
228 zurfluhs 93426
229     extract_indexes (l_class_c)
230 zurfluhs 93173 else
231 zurfluhs 94149 output_actions.call ([ca_messages.class_skipped (a_class.name)])
232 zurfluhs 93168 end
233     end
234    
235 zurfluhs 93173 feature -- Properties
236 zurfluhs 93168
237 zurfluhs 93333 is_running: BOOLEAN
238 zurfluhs 93963 -- Is code analysis running?
239 zurfluhs 93333
240 zurfluhs 93897 rules: LINKED_LIST [CA_RULE]
241 zurfluhs 93963 -- List of rules that will be used for analysis.
242 zurfluhs 93168
243 zurfluhs 93897 rule_violations: detachable HASH_TABLE [SORTED_TWO_WAY_LIST [CA_RULE_VIOLATION], CLASS_C]
244 zurfluhs 93963 -- All found violations from the last analysis.
245 zurfluhs 93168
246 zurfluhs 93475 preferences: PREFERENCES
247 zurfluhs 93963 -- Code Analysis preferences.
248 zurfluhs 93475 do Result := settings.preferences end
249    
250 zurfluhs 93914 class_list: ITERABLE [CLASS_C]
251 zurfluhs 93963 -- List of classes that have been added.
252 zurfluhs 93914 do Result := classes_to_analyze end
253    
254 zurfluhs 93168 feature {NONE} -- Implementation
255    
256 zurfluhs 93389 analysis_completed
257 zurfluhs 93963 -- Will be called when the analysis task has finished.
258 zurfluhs 94106 local
259     l_logger: CA_LOGGER
260 zurfluhs 93389 do
261 zurfluhs 94106 create l_logger.make ("last_analysis_result.csv")
262     -- CSV title column.
263     l_logger.log ("Severity;Class;Location;Title;Description;Rule ID;Severity Score")
264    
265 zurfluhs 93389 across rules as l_rules loop
266     across l_rules.item.violations as l_v loop
267 zurfluhs 93897 -- Check the ignore list.
268 zurfluhs 93426 if is_violation_valid (l_v.item) then
269 zurfluhs 93897 -- Make sure a list for this class exists in the hash table:
270     rule_violations.put (create {SORTED_TWO_WAY_LIST [CA_RULE_VIOLATION]}.make, l_v.item.affected_class)
271     -- Add the violation.
272 zurfluhs 93426 rule_violations.at (l_v.item.affected_class).extend (l_v.item)
273 zurfluhs 94106 -- Log it.
274     l_logger.log (l_v.item.out)
275 zurfluhs 93426 end
276 zurfluhs 93389 end
277     end
278    
279 zurfluhs 94106 l_logger.close_log
280    
281 zurfluhs 93389 clear_classes_to_analyze
282    
283     is_running := False
284     completed_actions.call ([True])
285     completed_actions.wipe_out
286     end
287    
288 zurfluhs 93426 is_violation_valid (a_viol: CA_RULE_VIOLATION): BOOLEAN
289 zurfluhs 93963 -- Is the violation `a_viol' valid under the current settings
290     -- such as the rule ignore list of a class, or the library or
291     -- non-library status of a class?
292 zurfluhs 93426 local
293     l_affected_class: CLASS_C
294     l_rule: CA_RULE
295     do
296     l_affected_class := a_viol.affected_class
297     l_rule := a_viol.rule
298    
299     Result := True
300    
301 zurfluhs 93967 if ignoredby.has (l_affected_class)
302 zurfluhs 93426 and then (ignoredby.at (l_affected_class)).has (l_rule.id) then
303     Result := False
304     end
305    
306 zurfluhs 93967 if (not l_rule.checks_library_classes) and then library_class.at (l_affected_class) then
307 zurfluhs 93426 Result := False
308     end
309    
310 zurfluhs 93967 if (not l_rule.checks_nonlibrary_classes) and then nonlibrary_class.at (l_affected_class) then
311 zurfluhs 93426 Result := False
312     end
313     end
314    
315 zurfluhs 93168 settings: CA_SETTINGS
316 zurfluhs 93963 -- The settings manager for Code Analysis.
317 zurfluhs 93168
318 zurfluhs 93389 classes_to_analyze: LINKED_SET [CLASS_C]
319 zurfluhs 93963 -- List of classes that shall be analyzed.
320 zurfluhs 93168
321 zurfluhs 93174 system_wide_check: BOOLEAN
322 zurfluhs 93963 -- Shall the whole system be analyzed?
323 zurfluhs 93174
324 zurfluhs 93389 completed_actions: ACTION_SEQUENCE [TUPLE [BOOLEAN]]
325 zurfluhs 94149 -- List of procedures to call when analysis has completed.
326 zurfluhs 93298
327 zurfluhs 93389 frozen rota: detachable ROTA_S
328 zurfluhs 93897 -- Accesses the rota service.
329 zurfluhs 93389 local
330     l_service_consumer: SERVICE_CONSUMER [ROTA_S]
331     do
332     create l_service_consumer
333     if l_service_consumer.is_service_available and then l_service_consumer.service.is_interface_usable then
334     Result := l_service_consumer.service
335     end
336     end
337    
338 zurfluhs 93966 is_severity_enabled (a_severity: CA_RULE_SEVERITY): BOOLEAN
339     do
340     Result := (attached {CA_HINT} a_severity and settings.are_hints_enabled.value)
341     or else (attached {CA_SUGGESTION} a_severity and settings.are_suggestions_enabled.value)
342     or else (attached {CA_WARNING} a_severity and settings.are_warnings_enabled.value)
343     or else (attached {CA_ERROR} a_severity and settings.are_errors_enabled.value)
344     end
345 zurfluhs 93426
346 zurfluhs 94149 output_actions: ACTION_SEQUENCE [TUPLE [READABLE_STRING_GENERAL]]
347     -- Will be called whenever there is a message to output.
348 zurfluhs 93966
349 zurfluhs 93426 feature {NONE} -- Class-wide Options (From Indexing Clauses)
350    
351     extract_indexes (a_class: CLASS_C)
352 zurfluhs 93963 -- Extracts options from the indexing clause of class `a_class'.
353 zurfluhs 93426 local
354     l_ast: CLASS_AS
355 zurfluhs 93431 l_item: STRING_32
356     l_ignoredby: LINKED_LIST [STRING_32]
357 zurfluhs 93426 do
358     create l_ignoredby.make
359 zurfluhs 93963 l_ignoredby.compare_objects -- We want to compare the actual strings.
360     -- Reset the class flags.
361 zurfluhs 93426 library_class.force (False, a_class)
362     nonlibrary_class.force (False, a_class)
363     l_ast := a_class.ast
364    
365 zurfluhs 93894 if attached l_ast.internal_top_indexes then
366     across l_ast.internal_top_indexes as l_indexes loop
367 zurfluhs 93426
368 zurfluhs 93894 if l_indexes.item.tag.name_32.is_equal ("ca_ignoredby") then
369 zurfluhs 93963 -- Class wants to ignore certain rules.
370 zurfluhs 93894 across l_indexes.item.index_list as l_list loop
371     l_item := l_list.item.string_value_32
372     l_item.prune_all ('%"')
373     l_ignoredby.extend (l_item)
374 zurfluhs 93426 end
375 zurfluhs 93894 elseif l_indexes.item.tag.name_32.is_equal ("ca_library") then
376 zurfluhs 93963 -- Class has information on whether it is a library class.
377 zurfluhs 93894 if not l_indexes.item.index_list.is_empty then
378     l_item := l_indexes.item.index_list.first.string_value_32
379     l_item.to_lower
380     l_item.prune_all ('%"')
381     if l_item.is_equal ("true") then
382     library_class.force (True, a_class)
383     elseif l_item.is_equal ("false") then
384     nonlibrary_class.force (True, a_class)
385     end
386     end
387 zurfluhs 93426 end
388     end
389     end
390    
391     ignoredby.force (l_ignoredby, a_class)
392     end
393    
394 zurfluhs 93431 ignoredby: HASH_TABLE [LINKED_LIST [STRING_32], CLASS_C]
395 zurfluhs 93963 -- Maps classes to lists of rules (rule IDs) the class wants to be ignored by.
396 zurfluhs 93426
397     library_class, nonlibrary_class: HASH_TABLE [BOOLEAN, CLASS_C]
398 zurfluhs 93963 -- Stores classes that are marked as library or non-library classes.
399 zurfluhs 93426
400     invariant
401     -- law_of_non_contradiction: one class must not be both a library_class and a nonlibrary_class
402    
403 zurfluhs 93168 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23