File: errorhandler-errorbuffer.adb

package info (click to toggle)
spark 2012.0.deb-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 29,260 kB
  • ctags: 3,098
  • sloc: ada: 186,243; cpp: 13,497; makefile: 685; yacc: 440; lex: 176; ansic: 119; sh: 16
file content (465 lines) | stat: -rw-r--r-- 20,136 bytes parent folder | download | duplicates (3)
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
-------------------------------------------------------------------------------
-- (C) Altran Praxis Limited
-------------------------------------------------------------------------------
--
-- The SPARK toolset 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 3, or (at your option) any later
-- version. The SPARK toolset 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 the SPARK toolset; see file
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy of
-- the license.
--
--=============================================================================

separate (ErrorHandler)
package body ErrorBuffer
--# own Buffer is Current_Flow_Error,
--#               The_Buffer;
is

   subtype Buff_Index is Integer range 1 .. ExaminerConstants.ErrorBufferSize;
   subtype Buff_Ptr is Integer range 0 .. ExaminerConstants.ErrorBufferSize;
   type Error_Array is array (Buff_Index) of Error_Types.NumericError;
   type Buffers is record
      ErrorList : Error_Array;
      ErrPtr    : Buff_Ptr;
   end record;

   The_Buffer : Buffers;

   Current_Flow_Error : Error_Types.NumericError;

   --------------------------------------------------------------------------
   --                      Local Procedures
   --------------------------------------------------------------------------

   function Null_Error return  Error_Types.NumericError
   --# global in Dictionary.Dict;
   is
   begin
      return Error_Types.NumericError'
        (ErrorType => Error_Types.NoErr,
         Position  => LexTokenManager.Token_Position'(Start_Line_No => 0,
                                                      Start_Pos     => 0),
         Scope     => Dictionary.GlobalScope,
         ErrorNum  => 0,
         Reference => 0,
         Name1     => Error_Types.NoName,
         Name2     => Error_Types.NoName,
         Name3     => Error_Types.NoName);
   end Null_Error;

   --------------------------------------------------------------------------
   --                      Exported Procedures
   --------------------------------------------------------------------------

   procedure Flush (Err_File : in out Error_IO.File_Type)
   --# global in     Dictionary.Dict;
   --#        in out SPARK_IO.File_Sys;
   --#        in out The_Buffer;
   --# derives Err_File          from *,
   --#                                SPARK_IO.File_Sys,
   --#                                The_Buffer &
   --#         SPARK_IO.File_Sys from *,
   --#                                Dictionary.Dict,
   --#                                Err_File,
   --#                                The_Buffer &
   --#         The_Buffer        from ;
   is

      function Is_Less_Than (One, Two : LexTokenManager.Token_Position) return Boolean is
         Less_Than : Boolean;
      begin
         if One.Start_Line_No = Two.Start_Line_No then
            Less_Than := One.Start_Pos < Two.Start_Pos;
         else
            Less_Than := One.Start_Line_No < Two.Start_Line_No;
         end if;
         return Less_Than;
      end Is_Less_Than;

      --------------------------

      procedure Sort_Buff
      --# global in out The_Buffer;
      --# derives The_Buffer from *;
      is

         procedure Swap (X, Y : in Buff_Ptr)
         --# global in out The_Buffer;
         --# derives The_Buffer from *,
         --#                         X,
         --#                         Y;
         is
            T : Error_Types.NumericError;
         begin
            T                        := The_Buffer.ErrorList (X);
            The_Buffer.ErrorList (X) := The_Buffer.ErrorList (Y);
            The_Buffer.ErrorList (Y) := T;
         end Swap;

      begin --Sort_Buff
         for I in Buff_Ptr range 1 .. The_Buffer.ErrPtr loop
            for J in Buff_Ptr range I .. The_Buffer.ErrPtr loop
               if Is_Less_Than (One => The_Buffer.ErrorList (J).Position,
                                Two => The_Buffer.ErrorList (I).Position) then
                  Swap (X => I,
                        Y => J);
               end if;
            end loop;
         end loop;
      end Sort_Buff;

      ------------------------------------------

      procedure Merge
      --# global in     Dictionary.Dict;
      --#        in     The_Buffer;
      --#        in out Err_File;
      --#        in out SPARK_IO.File_Sys;
      --# derives Err_File          from *,
      --#                                SPARK_IO.File_Sys,
      --#                                The_Buffer &
      --#         SPARK_IO.File_Sys from *,
      --#                                Dictionary.Dict,
      --#                                Err_File,
      --#                                The_Buffer;
      is
         New_File              : Error_IO.File_Type;
         Ptr                   : Buff_Ptr;
         Buf_Empty, File_Empty : Boolean;
         Buf_Ent, File_Ent     : Error_Types.NumericError;

         --------------------------------------------------------------------------

         procedure Create_Temp (F : out Error_IO.File_Type)
         --# global in out SPARK_IO.File_Sys;
         --# derives F,
         --#         SPARK_IO.File_Sys from SPARK_IO.File_Sys;
         is
            OK      : SPARK_IO.File_Status;
            Local_F : Error_IO.File_Type := Error_IO.Null_File;
         begin
            Error_IO.Create (Local_F, OK);
            if OK /= SPARK_IO.Ok then
               SystemErrors.Fatal_Error
                 (Sys_Err => SystemErrors.Error_Handler_Temporary_Files,
                  Msg     => "in ErrorBuffer.Create_Temp");
            end if;
            F := Local_F;
         end Create_Temp;

         --------------------------------------------------------------------------

         procedure Reset_Temp (F : in out Error_IO.File_Type)
         --# global in out SPARK_IO.File_Sys;
         --# derives F,
         --#         SPARK_IO.File_Sys from *,
         --#                                F;
         is
            OK : SPARK_IO.File_Status;
         begin
            Error_IO.Reset (F, SPARK_IO.In_File, OK);
            if OK /= SPARK_IO.Ok then
               SystemErrors.Fatal_Error
                 (Sys_Err => SystemErrors.Error_Handler_Temporary_Files,
                  Msg     => "in ErrorBuffer.Reset_Temp");
            end if;
         end Reset_Temp;

         --------------------------------------------------------------------------

         procedure Close_Temp (F : in out Error_IO.File_Type)
         --# global in out SPARK_IO.File_Sys;
         --# derives F,
         --#         SPARK_IO.File_Sys from *,
         --#                                F;
         is
            OK : SPARK_IO.File_Status;
         begin
            Error_IO.Close (F, OK);
            if OK /= SPARK_IO.Ok then
               SystemErrors.Fatal_Error
                 (Sys_Err => SystemErrors.Error_Handler_Temporary_Files,
                  Msg     => "in ErrorBuffer.Close_Temp");
            end if;
         end Close_Temp;

         --------------------------------------------------------------------------

         procedure Get_Buffer_Entry (Ent   : out Error_Types.NumericError;
                                     Empty : out Boolean)
         --# global in     Dictionary.Dict;
         --#        in     The_Buffer;
         --#        in out Ptr;
         --# derives Empty,
         --#         Ptr   from Ptr,
         --#                    The_Buffer &
         --#         Ent   from Dictionary.Dict,
         --#                    Ptr,
         --#                    The_Buffer;
         is
         begin
            if Ptr = The_Buffer.ErrPtr then
               Ent   := Null_Error;
               Empty := True;
            else
               Ptr   := Ptr + 1;
               Ent   := The_Buffer.ErrorList (Ptr);
               Empty := False;
            end if;
         end Get_Buffer_Entry;

      begin --Merge
         Ptr := 0;
         Get_Buffer_Entry (Ent   => Buf_Ent,
                           Empty => Buf_Empty);
         if not Buf_Empty then --only merge sort if buffer contains some entries
            Create_Temp (F => New_File);
            Reset_Temp (F => Err_File);

            Error_IO.Get_Numeric_Error (Err_File, File_Ent);
            File_Empty := (File_Ent = Error_Types.Empty_NumericError);

            while not (Buf_Empty and File_Empty) loop
               if File_Empty then
                  Error_IO.Put_Numeric_Error (New_File, Buf_Ent);
                  Get_Buffer_Entry (Ent   => Buf_Ent,
                                    Empty => Buf_Empty);
               elsif Buf_Empty then
                  Error_IO.Put_Numeric_Error (New_File, File_Ent);
                  Error_IO.Get_Numeric_Error (Err_File, File_Ent);
                  File_Empty := (File_Ent = Error_Types.Empty_NumericError);

               else --neither empty
                  if Is_Less_Than (One => Buf_Ent.Position,
                                   Two => File_Ent.Position) then
                     Error_IO.Put_Numeric_Error (New_File, Buf_Ent);
                     Get_Buffer_Entry (Ent   => Buf_Ent,
                                       Empty => Buf_Empty);
                  else
                     Error_IO.Put_Numeric_Error (New_File, File_Ent);
                     Error_IO.Get_Numeric_Error (Err_File, File_Ent);
                     File_Empty := (File_Ent = Error_Types.Empty_NumericError);

                  end if;
               end if;
            end loop;
            --# accept Flow, 10, Err_File, "Expected ineffective assignment";
            Close_Temp (F => Err_File);
            --# end accept;
            Err_File := New_File;
         end if;
      end Merge;

      ------------------------------------------

      procedure Init_Buff
      --# global out The_Buffer;
      --# derives The_Buffer from ;
      is
      begin
         The_Buffer.ErrPtr := 0;
         --intentional failure to initialize array will cause flow error here
         --# accept F, 31, The_Buffer.ErrorList, "Intentional incomplete initialization" &
         --#        F, 32, The_Buffer.ErrorList, "Intentional incomplete initialization" &
         --#        F, 602, The_Buffer, The_Buffer.ErrorList, "Intentional incomplete initialization";
      end Init_Buff; -- Init. is partial but effecive.  Expect 2 errs + 1 warning

   begin -- Flush
      Sort_Buff;
      Merge;
      Init_Buff;
   end Flush;

   --------------------------------------------------------------------------

   procedure Add
     (Err_File            : in out Error_IO.File_Type;
      Err_Type            : in     Error_Types.Error_Class;
      Pos                 : in     LexTokenManager.Token_Position;
      Scope               : in     Dictionary.Scopes;
      Error_Number        : in     Natural;
      Reference           : in     Natural;
      Name1, Name2, Name3 : in     Error_Types.Names;
      Echo_Str            :    out Error_Types.StringError)
   --# global in     CommandLineData.Content;
   --#        in     Dictionary.Dict;
   --#        in     LexTokenManager.State;
   --#        in out Conversions.State;
   --#        in out Current_Flow_Error;
   --#        in out SPARK_IO.File_Sys;
   --#        in out The_Buffer;
   --# derives Conversions.State,
   --#         The_Buffer         from *,
   --#                                 CommandLineData.Content,
   --#                                 Current_Flow_Error,
   --#                                 Error_Number,
   --#                                 Err_Type,
   --#                                 Name1,
   --#                                 Name2,
   --#                                 Name3,
   --#                                 Pos,
   --#                                 Reference,
   --#                                 Scope &
   --#         Current_Flow_Error from *,
   --#                                 CommandLineData.Content,
   --#                                 Dictionary.Dict,
   --#                                 Error_Number,
   --#                                 Err_Type,
   --#                                 Name1,
   --#                                 Name2,
   --#                                 Name3,
   --#                                 Pos,
   --#                                 Reference,
   --#                                 Scope &
   --#         Echo_Str           from CommandLineData.Content,
   --#                                 Conversions.State,
   --#                                 Current_Flow_Error,
   --#                                 Dictionary.Dict,
   --#                                 Error_Number,
   --#                                 Err_Type,
   --#                                 LexTokenManager.State,
   --#                                 Name1,
   --#                                 Name2,
   --#                                 Name3,
   --#                                 Pos,
   --#                                 Reference,
   --#                                 Scope &
   --#         Err_File           from *,
   --#                                 CommandLineData.Content,
   --#                                 Current_Flow_Error,
   --#                                 Error_Number,
   --#                                 Err_Type,
   --#                                 Name1,
   --#                                 Name2,
   --#                                 Name3,
   --#                                 Pos,
   --#                                 Reference,
   --#                                 Scope,
   --#                                 SPARK_IO.File_Sys,
   --#                                 The_Buffer &
   --#         SPARK_IO.File_Sys  from *,
   --#                                 CommandLineData.Content,
   --#                                 Current_Flow_Error,
   --#                                 Dictionary.Dict,
   --#                                 Error_Number,
   --#                                 Err_File,
   --#                                 Err_Type,
   --#                                 Name1,
   --#                                 Name2,
   --#                                 Name3,
   --#                                 Pos,
   --#                                 Reference,
   --#                                 Scope,
   --#                                 The_Buffer;
   is
      New_Entry : Error_Types.NumericError;

      procedure Set_To_New_Errors (New_Entry : in out Error_Types.NumericError)
      --# derives New_Entry from *;
      is
      begin
         if (New_Entry.ErrorNum = ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.May_Be_Used) and
               New_Entry.ErrorType = Error_Types.CondlDependencyErr) then
            New_Entry.ErrorNum := ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.May_Be_Used_New);
         elsif (New_Entry.ErrorNum = ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.Not_Used) and
                  New_Entry.ErrorType = Error_Types.UncondDependencyErr) then
            New_Entry.ErrorNum := ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.Not_Used_New);
         end if;

      end Set_To_New_Errors;

      procedure Reset_Error_Num (Err_Num : in out Error_Types.NumericError)
      --# global in     Dictionary.Dict;
      --#        in out Current_Flow_Error;
      --# derives Current_Flow_Error from *,
      --#                                 Dictionary.Dict,
      --#                                 Err_Num &
      --#         Err_Num            from *,
      --#                                 Current_Flow_Error;
      is
      begin
         case Err_Num.ErrorType is
            when Error_Types.UncondDependencyErr =>
               if Err_Num.ErrorNum = ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.Not_Used_New) then
                  if Current_Flow_Error.ErrorNum = ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.Not_Used_New)
                    and then (Err_Num.Name2 = Current_Flow_Error.Name2 and
                                Err_Num.Scope = Current_Flow_Error.Scope and
                                Err_Num.Position = Current_Flow_Error.Position) then
                     -- Continuation
                     Err_Num.ErrorNum := ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.Not_Used_Continue);
                  else
                     -- New Error;
                     Current_Flow_Error := Err_Num;
                  end if;
               else
                  Current_Flow_Error := Null_Error;
               end if;

            when Error_Types.CondlDependencyErr =>
               if Err_Num.ErrorNum = ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.May_Be_Used_New) then
                  if Current_Flow_Error.ErrorNum = ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.May_Be_Used_New)
                    and then (Err_Num.Name2 = Current_Flow_Error.Name2 and
                                Err_Num.Scope = Current_Flow_Error.Scope and
                                Err_Num.Position = Current_Flow_Error.Position) then
                     -- Continuation
                     Err_Num.ErrorNum := ErrorHandler.Dependency_Err_Number (Err_Type => ErrorHandler.May_Be_Used_Continue);
                  else
                     -- New Error;
                     Current_Flow_Error := Err_Num;
                  end if;
               else
                  Current_Flow_Error := Null_Error;
               end if;

            when others =>
               Current_Flow_Error := Null_Error;
         end case;
      end Reset_Error_Num;

   begin
      New_Entry :=
        Error_Types.NumericError'
        (ErrorType => Err_Type,
         Position  => Pos,
         Scope     => Scope,
         ErrorNum  => Error_Number,
         Reference => Reference,
         Name1     => Name1,
         Name2     => Name2,
         Name3     => Name3);
      if not CommandLineData.Content.Legacy_Errors then
         Set_To_New_Errors (New_Entry => New_Entry);
      end if;
      Reset_Error_Num (Err_Num => New_Entry);
      Conversions.ToString (New_Entry, Error_Types.ForScreen, Echo_Str);
      The_Buffer.ErrPtr                        := The_Buffer.ErrPtr + 1;
      The_Buffer.ErrorList (The_Buffer.ErrPtr) := New_Entry;
      if The_Buffer.ErrPtr = ExaminerConstants.ErrorBufferSize then
         Flush (Err_File => Err_File);
      end if;
   end Add;

begin --init
   The_Buffer.ErrPtr            := 0;
   Current_Flow_Error.ErrorType := Error_Types.NoErr;
   Current_Flow_Error.Position  := LexTokenManager.Token_Position'(Start_Line_No => 0,
                                                                   Start_Pos     => 0);
   Current_Flow_Error.ErrorNum  := 0;
   Current_Flow_Error.Reference := 0;
   Current_Flow_Error.Name1     := Error_Types.NoName;
   Current_Flow_Error.Name2     := Error_Types.NoName;
   Current_Flow_Error.Name3     := Error_Types.NoName;
   --intentional non-initialization of array will cause flow error here
   --# accept F, 31, The_Buffer.ErrorList, "Intentional incomplete initialization" &
   --#        F, 32, The_Buffer.ErrorList, "Intentional incomplete initialization" &
   --#        F, 602, The_Buffer, The_Buffer.ErrorList, "Intentional incomplete initialization" &
   --#        F, 31, Current_Flow_Error.Scope, "Intentional incomplete initialization" &
   --#        F, 32, Current_Flow_Error.Scope, "Intentional incomplete initialization" &
   --#        F, 602, Current_Flow_Error, Current_Flow_Error.Scope, "Intentional incomplete initialization";
end ErrorBuffer; -- Init. is partial but effective.  Expect 4 errs + 2 warnings.