/[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 94866 - (show annotations)
Wed Apr 16 16:19:10 2014 UTC (5 years, 9 months ago) by apaolo
File size: 13751 byte(s)
Added new "Mergeable feature clauses" Inspector Eiffel rule. Added new class.CA_MISC_UTILS class.
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
25 -- Adding the rules.
26 rules.extend (create {CA_SELF_ASSIGNMENT_RULE}.make)
27 rules.extend (create {CA_UNUSED_ARGUMENT_RULE}.make)
28 rules.extend (create {CA_NPATH_RULE}.make (settings.preference_manager))
29 rules.extend (create {CA_EMPTY_IF_RULE}.make)
30 rules.extend (create {CA_FEATURE_NEVER_CALLED_RULE}.make)
31 rules.extend (create {CA_CQ_SEPARATION_RULE}.make)
32 rules.extend (create {CA_UNNEEDED_OT_LOCAL_RULE}.make)
33 rules.extend (create {CA_UNNEEDED_OBJECT_TEST_RULE}.make) -- Needs type info.
34 rules.extend (create {CA_NESTED_COMPLEXITY_RULE}.make (settings.preference_manager))
35 rules.extend (create {CA_MANY_ARGUMENTS_RULE}.make (settings.preference_manager))
36 rules.extend (create {CA_CREATION_PROC_EXPORTED_RULE}.make)
37 rules.extend (create {CA_VARIABLE_NOT_READ_RULE}.make)
38 rules.extend (create {CA_SEMICOLON_ARGUMENTS_RULE}.make)
39 rules.extend (create {CA_VERY_LONG_ROUTINE_RULE}.make (settings.preference_manager))
40 rules.extend (create {CA_VERY_BIG_CLASS_RULE}.make (settings.preference_manager))
41 rules.extend (create {CA_FEATURE_SECTION_COMMENT_RULE}.make)
42 rules.extend (create {CA_FEATURE_NOT_COMMENTED_RULE}.make)
43 rules.extend (create {CA_BOOLEAN_RESULT_RULE}.make)
44 rules.extend (create {CA_BOOLEAN_COMPARISON_RULE}.make)
45 rules.extend (create {CA_VERY_SHORT_IDENTIFIER_RULE}.make (settings.preference_manager))
46 rules.extend (create {CA_VERY_LONG_IDENTIFIER_RULE}.make (settings.preference_manager))
47 rules.extend (create {CA_MISSING_IS_EQUAL_RULE}.make)
48 rules.extend (create {CA_SIMPLIFIABLE_BOOLEAN_RULE}.make)
49 rules.extend (create {CA_SELF_COMPARISON_RULE}.make)
50 rules.extend (create {CA_TODO_RULE}.make)
51 rules.extend (create {CA_WRONG_LOOP_ITERATION_RULE}.make)
52 rules.extend (create {CA_INSPECT_INSTRUCTIONS_RULE}.make (settings.preference_manager))
53 rules.extend (create {CA_ATTRIBUTE_TO_LOCAL_RULE}.make)
54 rules.extend (create {CA_EMPTY_EFFECTIVE_ROUTINE_RULE}.make)
55 rules.extend (create {CA_IF_ELSE_NOT_EQUAL_RULE}.make)
56 rules.extend (create {CA_SHORT_CIRCUIT_IF_RULE}.make)
57 rules.extend (create {CA_ITERABLE_LOOP_RULE}.make) -- Needs type info.
58 rules.extend (create {CA_COUNT_EQUALS_ZERO_RULE}.make) -- Needs type info.
59 rules.extend (create {CA_DEEPLY_NESTED_IF_RULE}.make (settings.preference_manager))
60 rules.extend (create {CA_UNNEEDED_HELPER_VARIABLE_RULE}.make (settings.preference_manager))
61 rules.extend (create {CA_UNNEEDED_PARENTHESES_RULE}.make)
62 rules.extend (create {CA_CLASS_NAMING_CONVENTION_RULE}.make)
63 rules.extend (create {CA_FEATURE_NAMING_CONVENTION_RULE}.make)
64 rules.extend (create {CA_VARIABLE_NAMING_CONVENTION_RULE}.make)
65 rules.extend (create {CA_ARGUMENT_NAMING_CONVENTION_RULE}.make)
66 rules.extend (create {CA_UNNECESSARY_SIGN_OPERATOR}.make)
67 rules.extend (create {CA_EMPTY_UNCOMMENTED_ROUTINE}.make)
68 rules.extend (create {CA_UNNEEDED_ACCESSOR_FUNCTION}.make)
69 rules.extend (create {CA_MERGEABLE_FEATURE_CLAUSES}.make)
70
71 settings.initialize_rule_settings (rules)
72
73 create classes_to_analyze.make
74 create rule_violations.make (100)
75 create completed_actions
76 create output_actions
77
78 create ignoredby.make (25)
79 create library_class.make (25)
80 create nonlibrary_class.make (25)
81 end
82
83 feature -- Analysis interface
84
85 add_completed_action (a_action: attached PROCEDURE [ANY, TUPLE [ITERABLE [TUPLE [detachable EXCEPTION, CLASS_C]]]])
86 -- Adds `a_action' to the list of procedures that will be
87 -- called when analysis has completed.
88 do
89 completed_actions.extend (a_action)
90 end
91
92 add_output_action (a_action: attached PROCEDURE [ANY, TUPLE [READABLE_STRING_GENERAL]])
93 -- Adds `a_action' to the procedures that are called for outputting status. The final results
94 -- (rule violations) are not given to these procedures.
95 do
96 output_actions.extend (a_action)
97 end
98
99 analyze
100 -- Analyze all the classes that have been added.
101 require
102 not is_running
103 local
104 l_rules_checker: CA_ALL_RULES_CHECKER
105 l_task: CA_RULE_CHECKING_TASK
106 l_rules_to_check: LINKED_LIST [CA_RULE]
107 do
108 is_running := True
109
110 create l_rules_checker.make
111 create l_rules_to_check.make
112 across rules as l_rules loop
113 l_rules.item.clear_violations
114 if is_rule_checkable (l_rules.item) then
115 l_rules_to_check.extend (l_rules.item)
116 -- Here we only prepare standard rules. The rule checking task will iterate again
117 -- through the rules and run the analysis on the enabled rules.
118 if attached {CA_STANDARD_RULE} l_rules.item as l_std_rule then
119 l_std_rule.prepare_checking (l_rules_checker)
120 end
121 end
122 end
123
124 create l_task.make (l_rules_checker, l_rules_to_check, classes_to_analyze, agent analysis_completed)
125 l_task.set_output_actions (output_actions)
126 rota.run_task (l_task)
127 end
128
129 is_rule_checkable (a_rule: attached CA_RULE): BOOLEAN
130 -- Will `a_rule' be checked based on the current preferences and based on the current
131 -- checking scope?
132 do
133 Result := a_rule.is_enabled.value
134 and then (system_wide_check or else (not a_rule.is_system_wide))
135 and then is_severity_enabled (a_rule.severity)
136 end
137
138 clear_classes_to_analyze
139 -- Removes all classes that have been added to the list of classes
140 -- to analyze.
141 do
142 classes_to_analyze.wipe_out
143 end
144
145 add_whole_system
146 -- Adds all the classes that are part of the current system. Classes of referenced libraries
147 -- will not be added.
148 local
149 l_cluster: CLUSTER_I
150 do
151 across
152 eiffel_universe.groups as l_groups
153 loop
154 l_cluster ?= l_groups.item
155 -- Only load top-level clusters, as the others will be loaded recursively afterwards.
156 if l_cluster /= Void and then l_cluster.parent_cluster = Void then
157 add_cluster (l_cluster)
158 end
159 end
160
161 system_wide_check := True
162 end
163
164 add_cluster (a_cluster: attached CLUSTER_I)
165 -- Add all classes of cluster `a_cluster'.
166 do
167 system_wide_check := False
168
169 if a_cluster.classes /= Void then
170 across a_cluster.classes as ic loop
171 add_class (ic.item)
172 end
173 end
174
175 if a_cluster.sub_clusters /= Void then
176 across a_cluster.sub_clusters as ic loop
177 add_cluster (ic.item)
178 end
179 end
180 end
181
182 add_group (a_group: attached CONF_GROUP)
183 -- Add all classes of the configuration group `a_group'.
184 require
185 a_group_not_void: a_group /= Void
186 do
187 if a_group.classes /= Void then
188 across a_group.classes as ic loop
189 add_class (ic.item)
190 end
191 end
192 end
193
194 add_classes (a_classes: attached ITERABLE [attached CONF_CLASS])
195 -- Add the classes `a_classes'.
196 do
197 system_wide_check := False
198
199 across a_classes as l_classes loop
200 add_class (l_classes.item)
201 end
202 end
203
204 add_class (a_class: attached CONF_CLASS)
205 -- Adds class `a_class'.
206 local
207 l_class_c: CLASS_C
208 do
209 system_wide_check := False
210
211 if attached {EIFFEL_CLASS_I} a_class as l_eiffel_class
212 and then attached l_eiffel_class.compiled_class as l_compiled
213 then
214 classes_to_analyze.extend (l_compiled)
215
216 extract_indexes (l_compiled)
217 else
218 output_actions.call ([ca_messages.class_skipped (a_class.name)])
219 end
220 end
221
222 feature -- Properties
223
224 is_running: BOOLEAN
225 -- Is code analysis running?
226
227 rules: LINKED_LIST [CA_RULE]
228 -- List of rules that will be used for analysis.
229
230 rule_violations: detachable HASH_TABLE [SORTED_TWO_WAY_LIST [CA_RULE_VIOLATION], CLASS_C]
231 -- All found violations from the last analysis.
232
233 settings: CA_SETTINGS
234 -- The settings manager for Code Analysis.
235
236 preferences: PREFERENCES
237 -- Code Analysis preferences.
238 do Result := settings.preferences end
239
240 class_list: ITERABLE [CLASS_C]
241 -- List of classes that have been added.
242 do Result := classes_to_analyze end
243
244 feature {NONE} -- Implementation
245
246 csv_file_name: STRING = "last_analysis_result.csv"
247
248 csv_header: STRING = "Severity;Class;Location;Title;Description;Rule ID;Severity Score"
249
250 analysis_completed (a_exceptions: detachable ITERABLE [TUPLE [detachable EXCEPTION, CLASS_C]])
251 -- Will be called when the analysis task has finished. `a_exceptions'
252 -- contains a list of exception that occurred during analysis.
253 local
254 l_csv_writer: CA_CSV_WRITER
255 do
256 create l_csv_writer.make (csv_file_name, csv_header)
257
258 across rules as l_rules loop
259 across l_rules.item.violations as l_v loop
260 -- Check the ignore list.
261 if is_violation_valid (l_v.item) then
262 -- Make sure a list for this class exists in the hash table:
263 rule_violations.put (create {SORTED_TWO_WAY_LIST [CA_RULE_VIOLATION]}.make, l_v.item.affected_class)
264 -- Add the violation.
265 rule_violations.at (l_v.item.affected_class).extend (l_v.item)
266 -- Log it.
267 l_csv_writer.add_line (l_v.item.csv_line)
268 end
269 end
270 end
271
272 l_csv_writer.close_file
273
274 clear_classes_to_analyze
275
276 is_running := False
277 completed_actions.call ([a_exceptions])
278 completed_actions.wipe_out
279 end
280
281 is_violation_valid (a_viol: attached CA_RULE_VIOLATION): BOOLEAN
282 -- Is the violation `a_viol' valid under the current settings
283 -- such as the rule ignore list of a class, or the library or
284 -- non-library status of a class?
285 local
286 l_affected_class: CLASS_C
287 l_rule: CA_RULE
288 do
289 l_affected_class := a_viol.affected_class
290 l_rule := a_viol.rule
291
292 Result := True
293
294 if ignoredby.has (l_affected_class)
295 and then (ignoredby.at (l_affected_class)).has (l_rule.id) then
296 Result := False
297 end
298
299 if (not l_rule.checks_library_classes) and then library_class.at (l_affected_class) then
300 Result := False
301 end
302
303 if (not l_rule.checks_nonlibrary_classes) and then nonlibrary_class.at (l_affected_class) then
304 Result := False
305 end
306 end
307
308 classes_to_analyze: LINKED_SET [CLASS_C]
309 -- List of classes that shall be analyzed.
310
311 system_wide_check: BOOLEAN
312 -- Shall the whole system be analyzed?
313
314 completed_actions: ACTION_SEQUENCE [TUPLE [ITERABLE [TUPLE [detachable EXCEPTION, CLASS_C]]]]
315 -- List of procedures to call when analysis has completed.
316
317 frozen rota: detachable ROTA_S
318 -- Accesses the rota service.
319 local
320 l_service_consumer: SERVICE_CONSUMER [ROTA_S]
321 do
322 create l_service_consumer
323 if attached l_service_consumer.service as l_service and then l_service.is_interface_usable then
324 Result := l_service
325 end
326 end
327
328 is_severity_enabled (a_severity: attached CA_RULE_SEVERITY): BOOLEAN
329 do
330 Result := (attached {CA_HINT} a_severity and settings.are_hints_enabled.value)
331 or else (attached {CA_SUGGESTION} a_severity and settings.are_suggestions_enabled.value)
332 or else (attached {CA_WARNING} a_severity and settings.are_warnings_enabled.value)
333 or else (attached {CA_ERROR} a_severity and settings.are_errors_enabled.value)
334 end
335
336 output_actions: ACTION_SEQUENCE [TUPLE [READABLE_STRING_GENERAL]]
337 -- Will be called whenever there is a message to output.
338
339 feature {NONE} -- Class-wide Options (From Indexing Clauses)
340
341 extract_indexes (a_class: attached CLASS_C)
342 -- Extracts options from the indexing clause of class `a_class'.
343 local
344 l_ast: CLASS_AS
345 l_ignoredby: LINKED_LIST [STRING_32]
346 do
347 create l_ignoredby.make
348 l_ignoredby.compare_objects -- We want to compare the actual strings.
349 -- Reset the class flags.
350 library_class.force (False, a_class)
351 nonlibrary_class.force (False, a_class)
352 l_ast := a_class.ast
353
354 if attached l_ast.internal_top_indexes as l_top then
355 search_indexing_tags (l_top, a_class, l_ignoredby)
356 end
357 if attached l_ast.internal_bottom_indexes as l_bottom then
358 search_indexing_tags (l_bottom, a_class, l_ignoredby)
359 end
360
361 ignoredby.force (l_ignoredby, a_class)
362 end
363
364 search_indexing_tags (a_clause: attached INDEXING_CLAUSE_AS; a_class: attached CLASS_C; a_ignoredby: attached LINKED_LIST [STRING_32])
365 -- Searches `a_clause' for settings relevant to code analysis.
366 local
367 l_item: STRING_32
368 do
369 across a_clause as ic loop
370 if ic.item.tag.name_32.is_equal ("ca_ignoredby") then
371 -- Class wants to ignore certain rules.
372 across ic.item.index_list as l_list loop
373 l_item := l_list.item.string_value_32
374 l_item.prune_all ('%"')
375 a_ignoredby.extend (l_item)
376 end
377 elseif ic.item.tag.name_32.is_equal ("ca_library") then
378 -- Class has information on whether it is a library class.
379 if not ic.item.index_list.is_empty then
380 l_item := ic.item.index_list.first.string_value_32
381 l_item.to_lower
382 l_item.prune_all ('%"')
383 if l_item.is_equal ("true") then
384 library_class.force (True, a_class)
385 elseif l_item.is_equal ("false") then
386 nonlibrary_class.force (True, a_class)
387 end
388 end
389 end
390 end
391 end
392
393 ignoredby: HASH_TABLE [LINKED_LIST [STRING_32], CLASS_C]
394 -- Maps classes to lists of rules (rule IDs) the class wants to be ignored by.
395
396 library_class, nonlibrary_class: HASH_TABLE [BOOLEAN, CLASS_C]
397 -- Stores classes that are marked as library or non-library classes.
398
399 invariant
400 -- law_of_non_contradiction: one class must not be both a library_class and a nonlibrary_class
401
402 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23