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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23