File: gnatcheck-rules-traversing.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 (627 lines) | stat: -rw-r--r-- 22,256 bytes parent folder | download
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
------------------------------------------------------------------------------
--                                                                          --
--                          GNATCHECK COMPONENTS                            --
--                                                                          --
--           G N A T C H E C K . R U L E S. T R A V E R S I N G             --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                     Copyright (C) 2004-2014, 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).             --
--                                                                          --
------------------------------------------------------------------------------

with Asis.Elements;                    use Asis.Elements;
with Asis.Exceptions;
with Asis.Declarations;                use Asis.Declarations;

with ASIS_UL.Common;                   use ASIS_UL.Common;
with ASIS_UL.Global_State;             use ASIS_UL.Global_State;
with ASIS_UL.Global_State.CG;
with ASIS_UL.Options;                  use ASIS_UL.Options;
with ASIS_UL.Output;                   use ASIS_UL.Output;

with Gnatcheck.Diagnoses;              use Gnatcheck.Diagnoses;
with Gnatcheck.Ids;                    use Gnatcheck.Ids;
with Gnatcheck.Options;                use Gnatcheck.Options;
with Gnatcheck.Rules.Output;           use Gnatcheck.Rules.Output;
with Gnatcheck.Rules.Rule_Table;       use Gnatcheck.Rules.Rule_Table;
with Gnatcheck.Source_Checks;
with Gnatcheck.Traversal_Stack;

with ASIS_UL.Global_State.Utilities;   use ASIS_UL.Global_State.Utilities;

package body Gnatcheck.Rules.Traversing is

   -----------------------
   -- Local subprograms --
   -----------------------

   procedure Check_Node_For_Global_Rules
     (N : ASIS_UL.Global_State.GS_Node_Id);
   --  Incapsulates all the global rule checks that can be done on node by
   --  node basis

--   Definition          : Asis.Element;
--   Is_Global_Reference : Boolean;
--   Reference_Kind      : ASIS_UL.Global_State.Reference_Kinds;
   --  These variables are used for collecting the global information about
   --  data objects/ We define them as global for Pre_Operations because of
   --  performance reasons (to avoid their allocation for each identifier
   --  element being visited during traversal)

   ----------------------
   -- All_Rules_Pre_Op --
   ----------------------

   procedure All_Rules_Pre_Op
     (Element :        Asis.Element;
      Control : in out Traverse_Control;
      State   : in out Rule_Traversal_State)
   is
      Expanded_Code : Asis.Element;
      --  Used to place the expanded generic spec or body into
   begin

      if Analyse_Source_Text then
         Gnatcheck.Source_Checks.Check_Text_Rules
           (Up_To => Element,
            State => State);
      end if;

      if Is_Exemption_Pragma (Element) then
         Process_Exemption_Pragma (Element);
      end if;

      if Is_Non_Executable_Construct (Element) then
         Increase_Nonexec_Level (State);
      end if;

      if ASIS_UL.Options.Buld_Call_Graph
        and then
         In_Executable_Code (State)
      then

         begin

            ASIS_UL.Global_State.CG.Add_CG_Info (Element);

            for J in First_Rule .. All_Rules.Last loop

               if Is_Enable (All_Rules.Table (J).all)
                 and then
                  All_Rules.Table (J).all in Global_Rule_Template'Class
               then
                  Collect_Global_Info_Pre_Op
                    (Rule => Global_Rule_Template'Class
                               (All_Rules.Table (J).all),
                     Element => Element,
                     Control => Control,
                     State   => State);
               end if;

            end loop;

            --  Disabled for now
--            if ASIS_UL.Options.Collect_Data_Info
--              and then
--               Expression_Kind (Element) = An_Identifier
--            then

--               .Global_State.Data.Check_If_Global_Reference
--                 (Element                => Element,
--                  Definition             => Definition,
--                  Is_Global_Reference    => Is_Global_Reference,
--                  Reference_Kind         => Reference_Kind,
--                  Compute_Reference_Kind => True);

--               if Is_Global_Reference then

--                  ASIS_UL.Global_State.Data.Process_Global_Reference
--                    (Element, Definition, Reference_Kind);
--               end if;

--            end if;

         exception
            when ASIS_UL.Common.Non_Implemented_Error =>
               Set_Source_Status (State.SF, Error_Detected);

               Store_Diagnosis
                 (Text           => Build_GNAT_Location (Element) & ": " &
                                    Tool_Name.all & " internal error",
                  Diagnosis_Kind => Compiler_Error,
                  SF             => State.SF);

               Reset_State (State);

               Error ("global state construction fails " &
                      "because of non-implemented feature");
               Error (Build_GNAT_Location (Element));

            when Ex : Asis.Exceptions.ASIS_Inappropriate_Context          |
                      Asis.Exceptions.ASIS_Inappropriate_Container        |
                      Asis.Exceptions.ASIS_Inappropriate_Compilation_Unit |
                      Asis.Exceptions.ASIS_Inappropriate_Element          |
                      Asis.Exceptions.ASIS_Inappropriate_Line             |
                      Asis.Exceptions.ASIS_Inappropriate_Line_Number      |
                      Asis.Exceptions.ASIS_Failed                         =>

               Set_Source_Status (State.SF, Error_Detected);

               Store_Diagnosis
                 (Text           => Build_GNAT_Location (Element) & ": " &
                                    Tool_Name.all & " internal error",
                  Diagnosis_Kind => Compiler_Error,
                  SF             => State.SF);

               Reset_State (State);

               Error ("ASIS failure in pre-op when constructing global " &
                      "structure, report to report@adacore.com");
               Error (Build_GNAT_Location (Element));

               ASIS_UL.Output.Report_Unhandled_ASIS_Exception (Ex);

            when Ex : others =>
               Set_Source_Status (State.SF, Error_Detected);

               Store_Diagnosis
                 (Text           => Build_GNAT_Location (Element) & ": " &
                                    Tool_Name.all & " internal error",
                  Diagnosis_Kind => Compiler_Error,
                  SF             => State.SF);

               Reset_State (State);

               Error ("pre-operation failed when constructing global " &
                      "structure, report to report@adacore.com");
               Error (Build_GNAT_Location (Element));

               ASIS_UL.Output.Report_Unhandled_Exception (Ex);
               raise Fatal_Error;
         end;

      end if;

      for J in First_Rule .. All_Rules.Last loop

         begin

            if Is_Enable (All_Rules.Table (J).all) then

               if not Is_Part_Of_Instance (Element)
                 or else
                  Checked_On_Expanded_Code (All_Rules.Table (J).all)
               then
                  Reset_State (State);

                  Rule_Check_Pre_Op
                    (Rule    => All_Rules.Table (J).all,
                     Element => Element,
                     Control => Control,
                     State   => State);

                  if State.Detected then
                     Report_Detection
                       (For_Rule      => J,
                        On            => Element,
                        In_SF         => State.SF,
                        Justification => Exemption_Justification (J),
                        Diagnosis_Num => State.Diagnosis,
                        Diag_Actuals  => State.Diag_Params,
                        Diag_Line     => State.Line,
                        Diag_Column   => State.Column);
                  end if;

               end if;

            end if;

         exception
            when ASIS_UL.Common.Non_Implemented_Error =>
               Set_Source_Status (State.SF, Error_Detected);

               Store_Diagnosis
                 (Text           => Build_GNAT_Location (Element) & ": " &
                                    Tool_Name.all & " internal error",
                  Diagnosis_Kind => Compiler_Error,
                  SF             => State.SF);

               Reset_State (State);

               Error
                 ("(" & All_Rules.Table (J).Name. all & ") check fails " &
                  "because of non-implemented feature");
               Error (Build_GNAT_Location (Element));

            when Ex : Asis.Exceptions.ASIS_Inappropriate_Context          |
                      Asis.Exceptions.ASIS_Inappropriate_Container        |
                      Asis.Exceptions.ASIS_Inappropriate_Compilation_Unit |
                      Asis.Exceptions.ASIS_Inappropriate_Element          |
                      Asis.Exceptions.ASIS_Inappropriate_Line             |
                      Asis.Exceptions.ASIS_Inappropriate_Line_Number      |
                      Asis.Exceptions.ASIS_Failed                         =>

               Set_Source_Status (State.SF, Error_Detected);

               Store_Diagnosis
                 (Text           => Build_GNAT_Location (Element) & ": " &
                                    Tool_Name.all & " internal error",
                  Diagnosis_Kind => Compiler_Error,
                  SF             => State.SF);

               Reset_State (State);

               Error
                 ("(" & All_Rules.Table (J).Name. all & ") ASIS failure " &
                  "in pre-operation, report to report@adacore.com");
               Error (Build_GNAT_Location (Element));

               ASIS_UL.Output.Report_Unhandled_ASIS_Exception (Ex);

            when Ex : others =>
               Set_Source_Status (State.SF, Error_Detected);

               Store_Diagnosis
                 (Text           => Build_GNAT_Location (Element) & ": " &
                                    Tool_Name.all & " internal error",
                  Diagnosis_Kind => Compiler_Error,
                  SF             => State.SF);

               Reset_State (State);

               Error
                 ("(" & All_Rules.Table (J).Name. all & ") " &
                  "pre-operation failed, report to report@adacore.com");
               Error (Build_GNAT_Location (Element));

               ASIS_UL.Output.Report_Unhandled_Exception (Ex);
         end;

      end loop;

      Gnatcheck.Traversal_Stack.Push (Element);

      if Declaration_Kind (Element) in
           A_Package_Instantiation .. A_Function_Instantiation
         and then
           Analyse_Expanded_Code
      then
         Expanded_Code := Corresponding_Declaration (Element);

         Check_Rules
           (Element => Expanded_Code,
            Control => Control,
            State   => State);

         Expanded_Code := Corresponding_Body (Element);

         if not Is_Nil (Expanded_Code) then
            Check_Rules
              (Element => Expanded_Code,
               Control => Control,
               State   => State);
         end if;

      end if;

   exception
      when Ex : others =>
         Set_Source_Status (State.SF, Error_Detected);

         Store_Diagnosis
           (Text           => Build_GNAT_Location (Element) & ": " &
                              Tool_Name.all & " internal error",
            Diagnosis_Kind => Compiler_Error,
            SF             => State.SF);

         Error
           ("pre-operation failed (collecting global info?), " &
            "report to report@adacore.com)");
         Error (Build_GNAT_Location (Element));

         ASIS_UL.Output.Report_Unhandled_Exception (Ex);

   end All_Rules_Pre_Op;

   -----------------------
   -- All_Rules_Post_Op --
   -----------------------

   procedure All_Rules_Post_Op
     (Element :        Asis.Element;
      Control : in out Traverse_Control;
      State   : in out Rule_Traversal_State)
   is
   begin

--      if Gnatcheck.Global_State.Buld_Call_Graph
--       and then
--         Declaration_Kind (Element) in
--           A_Package_Instantiation .. A_Function_Instantiation
--      then
--         Expanded_Code := Corresponding_Declaration (Element);

--         Extract_Global_Information
--           (Element => Expanded_Code,
--            Control => Control,
--            State   => State);

--         Expanded_Code := Corresponding_Body (Element);

--         if not Is_Nil (Expanded_Code) then
--            Extract_Global_Information
--              (Element => Expanded_Code,
--               Control => Control,
--               State   => State);
--         end if;

--      end if;

      Gnatcheck.Traversal_Stack.Pop;

      for J in First_Rule .. All_Rules.Last loop

         begin

            if Is_Enable (All_Rules.Table (J).all) then

               if not Is_Part_Of_Instance (Element)
                 or else
                  Checked_On_Expanded_Code (All_Rules.Table (J).all)
               then

                  Reset_State (State);

                  Rule_Check_Post_Op
                    (Rule    => All_Rules.Table (J).all,
                     Element => Element,
                     Control => Control,
                     State   => State);

                  if State.Detected then
                     Report_Detection
                       (For_Rule      => J,
                        On            => Element,
                        In_SF         => State.SF,
                        Justification => Exemption_Justification (J),
                        Diagnosis_Num => State.Diagnosis,
                        Diag_Actuals  => State.Diag_Params,
                        Diag_Line     => State.Line,
                        Diag_Column   => State.Column);
                  end if;

               end if;

            end if;

         exception
            when ASIS_UL.Common.Non_Implemented_Error =>
               Set_Source_Status (State.SF, Error_Detected);

               Store_Diagnosis
                 (Text           => Build_GNAT_Location (Element) & ": " &
                                    Tool_Name.all & " internal error",
                  Diagnosis_Kind => Compiler_Error,
                  SF             => State.SF);

               Reset_State (State);

               Error
                 ("(" & All_Rules.Table (J).Name. all & ") check fails " &
                  "because of non-implemented feature");
               Error (Build_GNAT_Location (Element));

            when Ex : Asis.Exceptions.ASIS_Inappropriate_Context          |
                      Asis.Exceptions.ASIS_Inappropriate_Container        |
                      Asis.Exceptions.ASIS_Inappropriate_Compilation_Unit |
                      Asis.Exceptions.ASIS_Inappropriate_Element          |
                      Asis.Exceptions.ASIS_Inappropriate_Line             |
                      Asis.Exceptions.ASIS_Inappropriate_Line_Number      |
                      Asis.Exceptions.ASIS_Failed                         =>

               Set_Source_Status (State.SF, Error_Detected);

               Store_Diagnosis
                 (Text           => Build_GNAT_Location (Element) & ": " &
                                    Tool_Name.all & " internal error",
                  Diagnosis_Kind => Compiler_Error,
                  SF             => State.SF);

               Reset_State (State);

               Error
                 ("(" & All_Rules.Table (J).Name. all & ") ASIS failure " &
                  "in post-operation, report to report@adacore.com");
               Error (Build_GNAT_Location (Element));

               ASIS_UL.Output.Report_Unhandled_ASIS_Exception (Ex);

            when Ex : others =>
               Set_Source_Status (State.SF, Error_Detected);

               Store_Diagnosis
                 (Text           => Build_GNAT_Location (Element) & ": " &
                                    Tool_Name.all & " internal error",
                  Diagnosis_Kind => Compiler_Error,
                  SF             => State.SF);

               Reset_State (State);

               Error
                 ("(" & All_Rules.Table (J).Name. all & ") " &
                  "post-operation failed, report to report@adacore.com");
               Error (Build_GNAT_Location (Element));

               ASIS_UL.Output.Report_Unhandled_Exception (Ex);
         end;

      end loop;

      if ASIS_UL.Options.Buld_Call_Graph
        and then
         In_Executable_Code (State)
      then
         ASIS_UL.Global_State.CG.Complete_CG_Info (Element);

         for J in First_Rule .. All_Rules.Last loop

            if Is_Enable (All_Rules.Table (J).all)
              and then
               All_Rules.Table (J).all in Global_Rule_Template'Class
            then
               Collect_Global_Info_Post_Op
                 (Rule => Global_Rule_Template'Class
                            (All_Rules.Table (J).all),
                  Element => Element,
                  Control => Control,
                  State   => State);
            end if;

         end loop;

      end if;

      if Is_Non_Executable_Construct (Element) then
         Decrease_Nonexec_Level (State);
      end if;

   exception
      when Ex : others =>
         Set_Source_Status (State.SF, Error_Detected);

         Store_Diagnosis
           (Text           => Build_GNAT_Location (Element) & ": " &
                              Tool_Name.all & " internal error",
            Diagnosis_Kind => Compiler_Error,
            SF             => State.SF);

         Error
           ("post-operation failed (collecting global info?), " &
            "report to report@adacore.com)");
         Error (Build_GNAT_Location (Element));

         ASIS_UL.Output.Report_Unhandled_Exception (Ex);

   end All_Rules_Post_Op;

   ------------------------
   -- Check_Global_Rules --
   ------------------------

   procedure Check_Global_Rules is
   begin

      --  If the following condition is false, we just have nothing to do!

      if ASIS_UL.Options.Buld_Call_Graph then

         --  First, preparing the global structure (the transitive closure of
         --  the call graph has been already done)

         if ASIS_UL.Options.Debug_Mode then
            Info ("Check global rules - preparing the global structure ... ");
         end if;

         for J in First_Rule .. All_Rules.Last loop

            if All_Rules.Table (J).Rule_State = Enabled
              and then
               All_Rules.Table (J).all in Global_Rule_Template'Class
            then
               Analyze_Global_Structure
                 (Global_Rule_Template'Class (All_Rules.Table (J).all));
            end if;

         end loop;

         if ASIS_UL.Options.Debug_Mode then
            Info ("...Done");
         end if;

         --  Do the checks that can be done on node by node  basis

         if ASIS_UL.Options.Debug_Mode then
            Info ("Check global rules - node by node checks ... ");
         end if;

         for J in 1 .. ASIS_UL.Global_State.Last_Node loop

            if not (not Process_RTL_Units
                  and then
                    ASIS_UL.Global_State.Is_RTL_Node (J))
            then
               Check_Node_For_Global_Rules (J);
            end if;

         end loop;

         if ASIS_UL.Options.Debug_Mode then
            Info ("...Done");
         end if;

         --  ???

      end if;

   end Check_Global_Rules;

   ---------------------------------
   -- Check_Node_For_Global_Rules --
   ---------------------------------

   procedure Check_Node_For_Global_Rules
     (N : ASIS_UL.Global_State.GS_Node_Id)
   is
      Detected : Boolean := False;
   begin
      for J in First_Rule .. All_Rules.Last loop

         if All_Rules.Table (J).all in Global_Rule_Template'Class
           and then
            All_Rules.Table (J).Rule_State = Enabled
         then
            Check_Global_Structure_Node
              (Rule          =>
                Global_Rule_Template'Class (All_Rules.Table (J).all),
               N             => N,
               Detected      => Detected);

            if Detected then
               Report_Global_Rule_Detection
                 (For_Rule      => J,
                  On            => N);
            end if;

         end if;

      end loop;
   end Check_Node_For_Global_Rules;

   -----------------
   -- Reset_State --
   -----------------

   procedure Reset_State (State : in out Rule_Traversal_State) is
   begin
      State.Detected    := False;
      State.Diagnosis   := 0;
      State.Diag_Params := Nil_String_Loc;
      State.Line        := 0;
      State.Column      := 0;
   end Reset_State;

end Gnatcheck.Rules.Traversing;