/[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 96078 - (show annotations)
Wed Nov 12 04:16:34 2014 UTC (5 years, 3 months ago) by schmisam
File size: 16772 byte(s)
Fixed bug in rule #48.
Changed tag "ca_ignoredby" to simply "ca_ignore".
Added tag "ca_only" to only check the given rule IDs (This will override any rules in tag "ca_ignore").

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23