/[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 95429 - (show annotations)
Wed Jul 16 12:44:38 2014 UTC (5 years, 6 months ago) by schmisam
File size: 15206 byte(s)
Added code analysis rule for double negation.
Added tests for double negation.
Renamed test folder for inherit from any rule to represent rule ID correctly.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23