/[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 95466 - (show annotations)
Sat Jul 19 21:50:46 2014 UTC (5 years, 6 months ago) by schmisam
File size: 15505 byte(s)
Added code analysis rule for empty creation procedures without arguments.
Added test for this rule.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23