/[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 95830 - (show annotations)
Wed Oct 1 18:42:26 2014 UTC (5 years, 3 months ago) by schmisam
File size: 15956 byte(s)
Added rules 7 and 9.
Added tests for rules.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23