/[eiffelstudio]/branches/eth/eve/Src/framework/code_analysis/ca_code_analyzer.e
ViewVC logotype

Contents of /branches/eth/eve/Src/framework/code_analysis/ca_code_analyzer.e

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23