1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332 | ------------------------------------------------------------------------------
-- --
-- GNATCHECK COMPONENTS --
-- --
-- G N A T C H E C K . R U L E S . M E T R I C S --
-- --
-- B o d y --
-- --
-- Copyright (C) 2008-2017, AdaCore --
-- --
-- GNATCHECK is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 2, or ( at your option) any later --
-- version. GNATCHECK is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General --
-- Public License for more details. You should have received a copy of the --
-- GNU General Public License distributed with GNAT; see file COPYING. If --
-- not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02110-1301, USA. --
-- --
-- GNATCHECK is maintained by AdaCore (http://www.adacore.com). --
-- --
------------------------------------------------------------------------------
pragma Ada_2012;
with Ada.Characters.Handling; use Ada.Characters.Handling;
with Asis.Elements; use Asis.Elements;
with ASIS_UL.Metrics.Compute; use ASIS_UL.Metrics.Compute;
with ASIS_UL.Metrics.Definitions; use ASIS_UL.Metrics.Definitions;
with ASIS_UL.Output; use ASIS_UL.Output;
with ASIS_UL.Utilities; use ASIS_UL.Utilities;
package body Gnatcheck.Rules.Metrics is
Complexity_Metrics : Complexity_Metric_Counter;
-- This variable collects complexity metrics. We make it global to allow to
-- call the routine that computes all the complexity metrics for a given
-- element only once and then to use the results for both cyclomatic
-- complexity and essential complexity checks.
-----------------------------------
-- Metrics_Cyclomatic_Complexity --
-----------------------------------
-------------------------------------------------------
-- Rule_Check_Pre_Op (Metrics_Cyclomatic_Complexity) --
-------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Metrics_Cyclomatic_Complexity_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control);
Cyclomatic_Complexity : Metric_Count;
begin
if Is_Executable_Body (Element) then
Compute_Complexity_Metrics (Element, Complexity_Metrics);
Cyclomatic_Complexity := Complexity_Metrics.Statement_Complexity +
Complexity_Metrics.Expression_Complexity;
if Cyclomatic_Complexity > Metric_Count (Rule.Rule_Limit) then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" & Cyclomatic_Complexity'Img);
end if;
end if;
end Rule_Check_Pre_Op;
-----------------------------------------------
-- Init_Rule (Metrics_Cyclomatic_Complexity) --
-----------------------------------------------
procedure
Init_Rule (Rule : in out Metrics_Cyclomatic_Complexity_Rule_Type)
is
begin
Init_Rule (One_Integer_Parameter_Rule_Template (Rule));
-- Common rule fields:
Rule.Name := new String'("Metrics_Cyclomatic_Complexity");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("(metrics) high cyclomatic complexity");
Rule.Diagnosis := new String'("cyclomatic complexity is too high: %1%");
end Init_Rule;
----------------------------------
-- Metrics_Essential_Complexity --
----------------------------------
------------------------------------------------------
-- Rule_Check_Pre_Op (Metrics_Essential_Complexity) --
------------------------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Metrics_Essential_Complexity_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control);
begin
if Is_Executable_Body (Element) then
if not Is_Enable (Metrics_Cyclomatic_Complexity_Rule) then
Compute_Complexity_Metrics (Element, Complexity_Metrics);
end if;
if Complexity_Metrics.Essential_Complexity >
Metric_Count (Rule.Rule_Limit)
then
State.Detected := True;
State.Diag_Params :=
Enter_String ("%1%" &
Complexity_Metrics.Essential_Complexity'Img);
end if;
end if;
end Rule_Check_Pre_Op;
----------------------------------------------
-- Init_Rule (Metrics_Essential_Complexity) --
----------------------------------------------
procedure Init_Rule
(Rule : in out Metrics_Essential_Complexity_Rule_Type)
is
begin
Init_Rule (One_Integer_Parameter_Rule_Template (Rule));
-- Common rule fields:
Rule.Name := new String'("Metrics_Essential_Complexity");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("(metrics) high essential complexity");
Rule.Diagnosis := new String'("essential complexity is too high: %1%");
end Init_Rule;
-------------------
-- Metrics_LSLOC --
-------------------
function Check_LSLOC_On_This (Element : Asis.Element) return Boolean;
-- Checks if Metrics_LSLOC rule should be checked for this Element
-- according to rule settings.
Exceptions_Enabled : Boolean := False;
-------------------------------------------
-- Activate_In_Test_Mode (Metrics_LSLOC) --
-------------------------------------------
overriding procedure Activate_In_Test_Mode
(Rule : in out Metrics_LSLOC_Rule_Type)
is
begin
Process_Rule_Parameter
(Rule => Rule,
Param => "30",
Enable => True,
Defined_At => "");
end Activate_In_Test_Mode;
-----------------------------------------
-- Check_LSLOC_On_This (Metrics_LSLOC) --
-----------------------------------------
function Check_LSLOC_On_This (Element : Asis.Element) return Boolean is
Result : Boolean := False;
Arg_Kind : constant Declaration_Kinds := Declaration_Kind (Element);
begin
if Exceptions_Enabled then
if Metrics_LSLOC_Rule.Subprograms_Only then
Result := Arg_Kind in
A_Procedure_Body_Declaration |
A_Function_Body_Declaration;
end if;
else
Result := Is_Program_Unit (Element);
end if;
return Result;
end Check_LSLOC_On_This;
--------------------------------
-- Print_Rule (Metrics_LSLOC) --
--------------------------------
overriding procedure Print_Rule
(Rule : Metrics_LSLOC_Rule_Type;
Indent_Level : Natural := 0)
is
begin
Print_Rule (Rule => One_Integer_Parameter_Rule_Template (Rule),
Indent_Level => Indent_Level);
if Rule.Subprograms_Only then
Report_No_EOL (", Subprograms");
end if;
end Print_Rule;
----------------------------------------
-- Print_Rule_To_File (Metrics_LSLOC) --
----------------------------------------
overriding procedure Print_Rule_To_File
(Rule : Metrics_LSLOC_Rule_Type;
Rule_File : File_Type;
Indent_Level : Natural := 0)
is
begin
Print_Rule_To_File
(Rule => One_Integer_Parameter_Rule_Template (Rule),
Rule_File => Rule_File,
Indent_Level => Indent_Level);
if Rule.Subprograms_Only then
Put (Rule_File, ", Subprograms");
end if;
end Print_Rule_To_File;
--------------------------------------------
-- Process_Rule_Parameter (Metrics_LSLOC) --
--------------------------------------------
overriding procedure Process_Rule_Parameter
(Rule : in out Metrics_LSLOC_Rule_Type;
Param : String;
Enable : Boolean;
Defined_At : String)
is
Is_Number : Boolean := True;
Norm_Param : String (Param'Range);
begin
if Param = "" then
if Enable then
Error ("(" & Rule.Name.all & ") parameter is required for +R");
else
Rule.Rule_State := Disabled;
Rule.Subprograms_Only := False;
Exceptions_Enabled := False;
end if;
else
if Enable then
for J in Param'Range loop
if not Is_Digit (Param (J)) then
Is_Number := False;
exit;
end if;
end loop;
if Is_Number then
Process_Rule_Parameter
(Rule => One_Integer_Parameter_Rule_Template (Rule),
Param => Param,
Enable => True,
Defined_At => Defined_At);
else
-- Note that these parameters do not enable the rule!!!
Norm_Param := To_Lower (Param);
if Norm_Param = "subprograms" then
Exceptions_Enabled := True;
Rule.Subprograms_Only := True;
else
Error ("wrong parameter (" & Param & ") for rule " &
Rule.Name.all);
end if;
end if;
else
Error ("(" & Rule.Name.all & ") no parameter allowed for -R");
end if;
end if;
end Process_Rule_Parameter;
---------------------------------------
-- Rule_Check_Pre_Op (Metrics_LSLOC) --
---------------------------------------
procedure Rule_Check_Pre_Op
(Rule : in out Metrics_LSLOC_Rule_Type;
Element : Asis.Element;
Control : in out Traverse_Control;
State : in out Rule_Traversal_State)
is
pragma Unreferenced (Control);
Counter : Syntax_Metric_Counter;
LSLOC : Metric_Count;
begin
if Check_LSLOC_On_This (Element) then
Compute_Syntaxt_Metrics (Element, Counter);
LSLOC := Counter.All_Statements + Counter.All_Declarations;
if LSLOC > Metric_Count (Rule.Rule_Limit) then
State.Detected := True;
State.Diag_Params := Enter_String ("%1%" & LSLOC'Img);
end if;
end if;
end Rule_Check_Pre_Op;
-------------------------------
-- Init_Rule (Metrics_LSLOC) --
-------------------------------
procedure Init_Rule (Rule : in out Metrics_LSLOC_Rule_Type) is
begin
Init_Rule (One_Integer_Parameter_Rule_Template (Rule));
-- Common rule fields:
Rule.Name := new String'("Metrics_LSLOC");
Rule.Rule_Status := Fully_Implemented;
Rule.Help_Info := new String'("(metrics) high LSLOC value");
Rule.Diagnosis := new String'("LSLOC is too high: %1%");
end Init_Rule;
end Gnatcheck.Rules.Metrics;
|