File: gnatcheck-rules-metrics.adb

package info (click to toggle)
asis 2015-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,640 kB
  • sloc: ada: 140,372; makefile: 260; sh: 50; xml: 48; csh: 10
file content (201 lines) | stat: -rw-r--r-- 7,846 bytes parent folder | download | duplicates (2)
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
------------------------------------------------------------------------------
--                                                                          --
--                          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-2013, 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_2005;

with ASIS_UL.Metrics.Compute;        use ASIS_UL.Metrics.Compute;
with ASIS_UL.Metrics.Definitions;    use ASIS_UL.Metrics.Definitions;
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 --
   -------------------

   -------------------------------------------
   -- 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);
   end Activate_In_Test_Mode;

   ---------------------------------------
   -- 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 Is_Program_Unit (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;