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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23