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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23