1 ------------------------------------------------------------------------------
3 -- GNAT SYSTEM UTILITIES --
5 -- S P A R K _ X R E F S _ T E S T --
9 -- Copyright (C) 2011-2013, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- This utility program is used to test proper operation of the
27 -- Get_SPARK_Xrefs and Put_SPARK_Xrefs units. To run it, compile any source
28 -- file with switch -gnatd.E or -gnatd.F to get an ALI file file.ALI
29 -- containing SPARK information. Then run this utility using:
31 -- spark_xrefs_test file.ali
33 -- This test will read the SPARK cross-reference information from the ALI
34 -- file, and use Get_SPARK_Xrefs to store this in binary form in the internal
35 -- tables in SPARK_Xrefs. Then Put_SPARK_Xrefs is used to write the
36 -- information from these tables back into text form. This output is compared
37 -- with the original SPARK cross-reference information in the ALI file and the
38 -- two should be identical. If not an error message is output.
43 with SPARK_Xrefs
; use SPARK_Xrefs
;
44 with Types
; use Types
;
46 with Ada
.Command_Line
; use Ada
.Command_Line
;
47 with Ada
.Streams
; use Ada
.Streams
;
48 with Ada
.Streams
.Stream_IO
; use Ada
.Streams
.Stream_IO
;
51 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
53 procedure SPARK_Xrefs_Test
is
55 Name1
: String_Access
;
56 Outfile_1
: File_Type
;
57 Name2
: String_Access
;
58 Outfile_2
: File_Type
;
62 -- Terminate execution
64 Diff_Exec
: constant String_Access
:= Locate_Exec_On_Path
("diff");
65 Diff_Result
: Integer;
70 if Argument_Count
/= 1 then
71 Ada
.Text_IO
.Put_Line
("Usage: spark_xrefs_test FILE.ali");
75 Name1
:= new String'(Argument (1) & ".1");
76 Name2 := new String'(Argument
(1) & ".2");
78 Open
(Infile
, In_File
, Argument
(1));
79 Create
(Outfile_1
, Out_File
, Name1
.all);
80 Create
(Outfile_2
, Out_File
, Name2
.all);
82 -- Read input file till we get to first 'F' line
85 Output_Col
: Positive := 1;
87 function Get_Char
(F
: File_Type
) return Character;
88 -- Read one character from specified file
90 procedure Put_Char
(F
: File_Type
; C
: Character);
91 -- Write one character to specified file
93 function Get_Output_Col
return Positive;
94 -- Return current column in output file, where each line starts at
95 -- column 1 and terminate with LF, and HT is at columns 1, 9, etc.
96 -- All output is supposed to be carried through Put_Char.
102 function Get_Char
(F
: File_Type
) return Character is
103 Item
: Stream_Element_Array
(1 .. 1);
104 Last
: Stream_Element_Offset
;
107 Read
(F
, Item
, Last
);
112 return Character'Val (Item
(1));
120 function Get_Output_Col
return Positive is
129 procedure Put_Char
(F
: File_Type
; C
: Character) is
130 Item
: Stream_Element_Array
(1 .. 1);
133 if C
/= CR
and then C
/= EOF
then
137 Output_Col
:= ((Output_Col
+ 6) / 8) * 8 + 1;
139 Output_Col
:= Output_Col
+ 1;
142 Item
(1) := Character'Pos (C
);
147 -- Subprograms used by Get_SPARK_Xrefs (these also copy the output to
148 -- Outfile_1 for later comparison with the output generated by
151 function Getc
return Character;
152 function Nextc
return Character;
159 function Getc
return Character is
162 C
:= Get_Char
(Infile
);
163 Put_Char
(Outfile_1
, C
);
171 function Nextc
return Character is
175 C
:= Get_Char
(Infile
);
178 Set_Index
(Infile
, Index
(Infile
) - 1);
190 pragma Unreferenced
(C
);
195 -- Subprograms used by Put_SPARK_Xrefs, which write information to
198 function Write_Info_Col
return Positive;
199 procedure Write_Info_Char
(C
: Character);
200 procedure Write_Info_Initiate
(Key
: Character);
201 procedure Write_Info_Nat
(N
: Nat
);
202 procedure Write_Info_Terminate
;
208 function Write_Info_Col
return Positive is
210 return Get_Output_Col
;
213 ---------------------
214 -- Write_Info_Char --
215 ---------------------
217 procedure Write_Info_Char
(C
: Character) is
219 Put_Char
(Outfile_2
, C
);
222 -------------------------
223 -- Write_Info_Initiate --
224 -------------------------
226 procedure Write_Info_Initiate
(Key
: Character) is
228 Write_Info_Char
(Key
);
229 end Write_Info_Initiate
;
235 procedure Write_Info_Nat
(N
: Nat
) is
238 Write_Info_Nat
(N
/ 10);
241 Write_Info_Char
(Character'Val (48 + N
mod 10));
244 --------------------------
245 -- Write_Info_Terminate --
246 --------------------------
248 procedure Write_Info_Terminate
is
250 Write_Info_Char
(LF
);
251 end Write_Info_Terminate
;
253 -- Local instantiations of Put_SPARK_Xrefs and Get_SPARK_Xrefs
255 procedure Get_SPARK_Xrefs_Info
is new Get_SPARK_Xrefs
;
256 procedure Put_SPARK_Xrefs_Info
is new Put_SPARK_Xrefs
;
258 -- Start of processing for Process
261 -- Loop to skip till first 'F' line
264 C
:= Get_Char
(Infile
);
269 elsif C
= LF
or else C
= CR
then
271 C
:= Get_Char
(Infile
);
272 exit when C
/= LF
and then C
/= CR
;
279 -- Position back to initial 'F' of first 'F' line
281 Set_Index
(Infile
, Index
(Infile
) - 1);
283 -- Read SPARK cross-reference information to internal SPARK tables, also
284 -- copying SPARK xrefs info to Outfile_1.
286 Initialize_SPARK_Tables
;
287 Get_SPARK_Xrefs_Info
;
289 -- Write SPARK cross-reference information from internal SPARK tables to
292 Put_SPARK_Xrefs_Info
;
294 -- Junk blank line (see comment at end of Lib.Writ)
296 Write_Info_Terminate
;
303 -- Now Outfile_1 and Outfile_2 should be identical
306 Spawn
(Diff_Exec
.all,
307 Argument_String_To_List
308 ("-u " & Name1
.all & " " & Name2
.all).all);
310 if Diff_Result
/= 0 then
311 Ada
.Text_IO
.Put_Line
("diff(1) exit status" & Diff_Result
'Img);
314 OS_Exit
(Diff_Result
);
321 end SPARK_Xrefs_Test
;