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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23