/[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 94138 - (show annotations)
Fri Jan 31 13:04:58 2014 UTC (5 years, 11 months ago) by zurfluhs
File size: 12143 byte(s)
Code Analysis: refactoring

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23