1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2004 Ada Core Technologies, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Ada
.Command_Line
; use Ada
.Command_Line
;
28 with Ada
.Text_IO
; use Ada
.Text_IO
;
30 with GNAT
.Command_Line
; use GNAT
.Command_Line
;
31 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
32 with GNAT
.Heap_Sort_G
;
40 Terminate_Program
: exception;
41 -- Used to terminate execution immediately
43 Config_File_Name
: constant String_Access
:= new String'("gnat.adc");
44 -- The name of the file holding the GNAT configuration pragmas
46 Gcc : String_Access := new String'("gcc");
47 -- May be modified by switch --GCC=
49 Gcc_Set
: Boolean := False;
50 -- True if a switch --GCC= is used
52 Gnat_Cmd
: String_Access
;
53 -- Command to execute the GNAT compiler
55 Gnat_Args
: Argument_List_Access
:=
60 new String'("-gnats"),
61 new String'("-gnatu"));
62 -- Arguments used in Gnat_Cmd call
64 EOF
: constant Character := Character'Val (26);
65 -- Special character to signal end of file. Not required in input
66 -- files, but properly treated if present. Not generated in output
67 -- files except as a result of copying input file.
73 subtype File_Num
is Natural;
74 subtype File_Offset
is Natural;
76 type File_Entry
is record
78 -- Name of chop file or directory
80 SR_Name
: String_Access
;
81 -- Null unless the chop file starts with a source reference pragma
82 -- in which case this field points to the file name from this pragma.
85 package File
is new GNAT
.Table
86 (Table_Component_Type
=> File_Entry
,
87 Table_Index_Type
=> File_Num
,
90 Table_Increment
=> 100);
92 Directory
: String_Access
;
93 -- Record name of directory, or a null string if no directory given
95 Compilation_Mode
: Boolean := False;
96 Overwrite_Files
: Boolean := False;
97 Preserve_Mode
: Boolean := False;
98 Quiet_Mode
: Boolean := False;
99 Source_References
: Boolean := False;
100 Verbose_Mode
: Boolean := False;
101 Exit_On_Error
: Boolean := False;
104 Write_gnat_adc
: Boolean := False;
105 -- Gets set true if we append to gnat.adc or create a new gnat.adc.
106 -- Used to inhibit complaint about no units generated.
112 type Line_Num
is new Natural;
113 -- Line number (for source reference pragmas)
115 type Unit_Count_Type
is new Integer;
116 subtype Unit_Num
is Unit_Count_Type
range 1 .. Unit_Count_Type
'Last;
117 -- Used to refer to unit number in unit table
119 type SUnit_Num
is new Integer;
120 -- Used to refer to entry in sorted units table. Note that entry
121 -- zero is only for use by Heapsort, and is not otherwise referenced.
123 type Unit_Kind
is (Unit_Spec
, Unit_Body
, Config_Pragmas
);
125 -- Structure to contain all necessary information for one unit.
126 -- Entries are also temporarily used to record config pragma sequences.
128 type Unit_Info
is record
129 File_Name
: String_Access
;
130 -- File name from GNAT output line
132 Chop_File
: File_Num
;
133 -- File number in chop file sequence
135 Start_Line
: Line_Num
;
136 -- Line number from GNAT output line
138 Offset
: File_Offset
;
139 -- Offset name from GNAT output line
141 SR_Present
: Boolean;
142 -- Set True if SR parameter present
144 Length
: File_Offset
;
145 -- A length of 0 means that the Unit is the last one in the file
148 -- Indicates kind of unit
150 Sorted_Index
: SUnit_Num
;
151 -- Index of unit in sorted unit list
153 Bufferg
: String_Access
;
154 -- Pointer to buffer containing configuration pragmas to be
155 -- prepended. Null if no pragmas to be prepended.
159 -- The following table stores the unit offset information
161 package Unit
is new GNAT
.Table
162 (Table_Component_Type
=> Unit_Info
,
163 Table_Index_Type
=> Unit_Count_Type
,
164 Table_Low_Bound
=> 1,
165 Table_Initial
=> 500,
166 Table_Increment
=> 100);
168 -- The following table is used as a sorted index to the Unit.Table.
169 -- The entries in Unit.Table are not moved, instead we just shuffle
170 -- the entries in Sorted_Units. Note that the zeroeth entry in this
171 -- table is used by GNAT.Heap_Sort_G.
173 package Sorted_Units
is new GNAT
.Table
174 (Table_Component_Type
=> Unit_Num
,
175 Table_Index_Type
=> SUnit_Num
,
176 Table_Low_Bound
=> 0,
177 Table_Initial
=> 500,
178 Table_Increment
=> 100);
180 function Is_Duplicated
(U
: SUnit_Num
) return Boolean;
181 -- Returns true if U is duplicated by a later unit.
182 -- Note that this function returns false for the last entry.
184 procedure Sort_Units
;
185 -- Sort units and set up sorted unit table.
187 ----------------------
188 -- File_Descriptors --
189 ----------------------
191 function dup
(handle
: File_Descriptor
) return File_Descriptor
;
192 function dup2
(from
, to
: File_Descriptor
) return File_Descriptor
;
193 -- File descriptor based functions needed for redirecting stdin/stdout
195 pragma Import
(C
, dup
, "dup");
196 pragma Import
(C
, dup2
, "dup2");
198 ---------------------
199 -- Local variables --
200 ---------------------
202 Warning_Count
: Natural := 0;
203 -- Count of warnings issued so far
205 -----------------------
206 -- Local subprograms --
207 -----------------------
209 procedure Error_Msg
(Message
: String; Warning
: Boolean := False);
210 -- Produce an error message on standard error output
212 procedure File_Time_Stamp
(Name
: C_File_Name
; Time
: OS_Time
);
213 -- Given the name of a file or directory, Name, set the
214 -- time stamp. This function must be used for an unopened file.
216 function Files_Exist
return Boolean;
217 -- Check Unit.Table for possible file names that already exist
218 -- in the file system. Returns true if files exist, False otherwise
220 function Get_Maximum_File_Name_Length
return Integer;
221 pragma Import
(C
, Get_Maximum_File_Name_Length
,
222 "__gnat_get_maximum_file_name_length");
223 -- Function to get maximum file name length for system
225 Maximum_File_Name_Length
: constant Integer := Get_Maximum_File_Name_Length
;
226 Maximum_File_Name_Length_String
: constant String :=
228 (Maximum_File_Name_Length
);
230 function Locate_Executable
231 (Program_Name
: String;
232 Look_For_Prefix
: Boolean := True)
233 return String_Access
;
234 -- Locate executable for given program name. This takes into account
235 -- the target-prefix of the current command, if Look_For_Prefix is True.
237 subtype EOL_Length
is Natural range 0 .. 2;
238 -- Possible lengths of end of line sequence
240 type EOL_String
(Len
: EOL_Length
:= 0) is record
241 Str
: String (1 .. Len
);
245 (Source
: access String;
248 -- Return the line terminator used in the passed string
250 procedure Parse_EOL
(Source
: access String; Ptr
: in out Positive);
251 -- On return Source (Ptr) is the first character of the next line
252 -- or EOF. Source.all must be terminated by EOF.
254 function Parse_File
(Num
: File_Num
) return Boolean;
255 -- Calls the GNAT compiler to parse the given source file and parses the
256 -- output using Parse_Offset_Info. Returns True if parse operation
257 -- completes, False if some system error (e.g. failure to read the
258 -- offset information) occurs.
260 procedure Parse_Offset_Info
(Chop_File
: File_Num
; Source
: access String);
261 -- Parses the output of the compiler indicating the offsets
262 -- and names of the compilation units in Chop_File.
264 procedure Parse_Token
265 (Source
: access String;
266 Ptr
: in out Positive;
267 Token_Ptr
: out Positive);
268 -- Skips any separators and stores the start of the token in Token_Ptr.
269 -- Then stores the position of the next separator in Ptr.
270 -- On return Source (Token_Ptr .. Ptr - 1) is the token.
273 (FD
: File_Descriptor
;
274 Contents
: out String_Access
;
275 Success
: out Boolean);
276 -- Reads file associated with FS into the newly allocated
278 -- [VMS] Success is true iff the number of bytes read is less than or
279 -- equal to the file size.
280 -- [Other] Success is true iff the number of bytes read is equal to
283 function Report_Duplicate_Units
return Boolean;
284 -- Output messages about duplicate units in the input files in Unit.Table
285 -- Returns True if any duplicates found, Fals if no duplicates found.
287 function Scan_Arguments
return Boolean;
288 -- Scan command line options and set global variables accordingly.
289 -- Also scan out file and directory arguments. Returns True if scan
290 -- was successful, and False if the scan fails for any reason.
293 -- Output message on standard output describing syntax of gnatchop command
295 procedure Warning_Msg
(Message
: String);
296 -- Output a warning message on standard error and update warning count
298 function Write_Chopped_Files
(Input
: File_Num
) return Boolean;
299 -- Write all units that result from chopping the Input file
301 procedure Write_Config_File
(Input
: File_Num
; U
: Unit_Num
);
302 -- Call to write configuration pragmas (append them to gnat.adc)
303 -- Input is the file number for the chop file and U identifies the
304 -- unit entry for the configuration pragmas.
306 function Get_Config_Pragmas
309 return String_Access
;
310 -- Call to read configuration pragmas from given unit entry, and
311 -- return a buffer containing the pragmas to be appended to
312 -- following units. Input is the file number for the chop file and
313 -- U identifies the unit entry for the configuration pragmas.
315 procedure Write_Source_Reference_Pragma
318 FD
: File_Descriptor
;
320 Success
: in out Boolean);
321 -- If Success is True on entry, writes a source reference pragma using
322 -- the chop file from Info, and the given line number. On return Success
323 -- indicates whether the write succeeded. If Success is False on entry,
324 -- or if the global flag Source_References is False, then the call to
325 -- Write_Source_Reference_Pragma has no effect. EOL indicates the end
326 -- of line sequence to be written at the end of the pragma.
329 (Source
: access String;
332 Success
: out Boolean);
333 -- Write one compilation unit of the source to file
339 procedure Error_Msg
(Message
: String; Warning
: Boolean := False) is
341 Put_Line
(Standard_Error
, Message
);
344 Set_Exit_Status
(Failure
);
346 if Exit_On_Error
then
347 raise Terminate_Program
;
352 ---------------------
353 -- File_Time_Stamp --
354 ---------------------
356 procedure File_Time_Stamp
(Name
: C_File_Name
; Time
: OS_Time
) is
357 procedure Set_File_Time
(Name
: C_File_Name
; Time
: OS_Time
);
358 pragma Import
(C
, Set_File_Time
, "__gnat_set_file_time_name");
361 Set_File_Time
(Name
, Time
);
368 function Files_Exist
return Boolean is
369 Exists
: Boolean := False;
372 for SNum
in 1 .. SUnit_Num
(Unit
.Last
) loop
374 -- Only check and report for the last instance of duplicated files
376 if not Is_Duplicated
(SNum
) then
378 Info
: constant Unit_Info
:=
379 Unit
.Table
(Sorted_Units
.Table
(SNum
));
382 if Is_Writable_File
(Info
.File_Name
.all) then
383 if Hostparm
.OpenVMS
then
386 & " already exists, use /OVERWRITE to overwrite");
388 Error_Msg
(Info
.File_Name
.all
389 & " already exists, use -w to overwrite");
401 ------------------------
402 -- Get_Config_Pragmas --
403 ------------------------
405 function Get_Config_Pragmas
410 Info
: Unit_Info
renames Unit
.Table
(U
);
411 FD
: File_Descriptor
;
412 Name
: aliased constant String :=
413 File
.Table
(Input
).Name
.all & ASCII
.Nul
;
414 Length
: File_Offset
;
415 Buffer
: String_Access
;
417 Result
: String_Access
;
420 FD
:= Open_Read
(Name
'Address, Binary
);
422 if FD
= Invalid_FD
then
423 Error_Msg
("cannot open " & File
.Table
(Input
).Name
.all);
427 Read_File
(FD
, Buffer
, Success
);
429 -- A length of 0 indicates that the rest of the file belongs to
430 -- this unit. The actual length must be calculated now. Take into
431 -- account that the last character (EOF) must not be written.
433 if Info
.Length
= 0 then
434 Length
:= Buffer
'Last - (Buffer
'First + Info
.Offset
);
436 Length
:= Info
.Length
;
439 Result
:= new String'(Buffer (1 .. Length));
442 end Get_Config_Pragmas;
449 (Source : access String;
453 Ptr : Positive := Start;
458 -- Skip to end of line
460 while Source (Ptr) /= ASCII.CR and then
461 Source (Ptr) /= ASCII.LF and then
469 if Source (Ptr) /= EOF then
479 -- Recognize CR/LF or LF/CR combination
481 if (Source (Ptr + 1) = ASCII.CR or Source (Ptr + 1) = ASCII.LF)
482 and then Source (Ptr) /= Source (Ptr + 1)
487 return (Len => Last + 1 - First, Str => Source (First .. Last));
494 function Is_Duplicated (U : SUnit_Num) return Boolean is
496 return U < SUnit_Num (Unit.Last)
498 Unit.Table (Sorted_Units.Table (U)).File_Name.all =
499 Unit.Table (Sorted_Units.Table (U + 1)).File_Name.all;
502 -----------------------
503 -- Locate_Executable --
504 -----------------------
506 function Locate_Executable
507 (Program_Name : String;
508 Look_For_Prefix : Boolean := True)
511 Current_Command : constant String := Command_Name;
512 End_Of_Prefix : Natural := Current_Command'First - 1;
513 Start_Of_Prefix : Positive := Current_Command'First;
514 Result : String_Access;
518 if Look_For_Prefix then
519 -- Find Start_Of_Prefix
521 for J in reverse Current_Command'Range loop
522 if Current_Command (J) = '/' or
523 Current_Command (J) = Directory_Separator or
524 Current_Command (J) = ':'
526 Start_Of_Prefix := J + 1;
531 -- Find End_Of_Prefix
533 End_Of_Prefix := Start_Of_Prefix - 1;
535 for J in reverse Start_Of_Prefix .. Current_Command'Last loop
536 if Current_Command (J) = '-' then
544 Command : constant String :=
545 Current_Command (Start_Of_Prefix .. End_Of_Prefix) &
548 Result := Locate_Exec_On_Path (Command);
550 if Result = null then
552 (Command & ": installation problem, executable not found");
557 end Locate_Executable;
563 procedure Parse_EOL (Source : access String; Ptr : in out Positive) is
565 -- Skip to end of line
567 while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
568 and then Source (Ptr) /= EOF
573 if Source (Ptr) /= EOF then
574 Ptr := Ptr + 1; -- skip CR or LF
577 -- Skip past CR/LF or LF/CR combination
579 if (Source (Ptr) = ASCII.CR or Source (Ptr) = ASCII.LF)
580 and then Source (Ptr) /= Source (Ptr - 1)
590 function Parse_File (Num : File_Num) return Boolean is
591 Chop_Name : constant String_Access := File.Table (Num).Name;
592 Save_Stdout : constant File_Descriptor := dup (Standout);
593 Offset_Name : Temp_File_Name;
594 Offset_FD : File_Descriptor;
595 Buffer : String_Access;
600 -- Display copy of GNAT command if verbose mode
605 for J in 1 .. Gnat_Args'Length loop
607 Put (Gnat_Args (J).all);
611 Put_Line (Chop_Name.all);
614 -- Create temporary file
616 Create_Temp_File (Offset_FD, Offset_Name);
618 if Offset_FD = Invalid_FD then
619 Error_Msg ("gnatchop: cannot create temporary file");
624 -- Redirect Stdout to this temporary file in the Unix way
626 if dup2 (Offset_FD, Standout) = Invalid_FD then
627 Error_Msg ("gnatchop: cannot redirect stdout to temporary file");
633 -- Call Gnat on the source filename argument with special options
634 -- to generate offset information. If this special compilation completes
635 -- successfully then we can do the actual gnatchop operation.
637 Spawn (Gnat_Cmd.all, Gnat_Args.all & Chop_Name, Success);
640 Error_Msg (Chop_Name.all & ": parse errors detected");
641 Error_Msg (Chop_Name.all & ": chop may not be successful");
646 if dup2 (Save_Stdout, Standout) = Invalid_FD then
647 Error_Msg ("gnatchop: cannot restore stdout");
650 -- Reopen the file to start reading from the beginning
654 Offset_FD := Open_Read (Offset_Name'Address, Binary);
656 if Offset_FD = Invalid_FD then
657 Error_Msg ("gnatchop: cannot access offset info");
661 Read_File (Offset_FD, Buffer, Success);
664 Error_Msg ("gnatchop: error reading offset info");
668 Parse_Offset_Info (Num, Buffer);
671 -- Close and delete temporary file
674 Delete_File (Offset_Name'Address, Success);
679 when Failure | Terminate_Program =>
681 Delete_File (Offset_Name'Address, Success);
686 -----------------------
687 -- Parse_Offset_Info --
688 -----------------------
690 procedure Parse_Offset_Info
691 (Chop_File : File_Num;
692 Source : access String)
694 First_Unit : constant Unit_Num := Unit.Last + 1;
695 Bufferg : String_Access := null;
696 Parse_Ptr : File_Offset := Source'First;
697 Token_Ptr : File_Offset;
700 function Match (Literal : String) return Boolean;
701 -- Checks if given string appears at the current Token_Ptr location
702 -- and if so, bumps Parse_Ptr past the token and returns True. If
703 -- the string is not present, sets Parse_Ptr to Token_Ptr and
710 function Match (Literal : String) return Boolean is
712 Parse_Token (Source, Parse_Ptr, Token_Ptr);
714 if Source'Last + 1 - Token_Ptr < Literal'Length
716 Source (Token_Ptr .. Token_Ptr + Literal'Length - 1) /= Literal
718 Parse_Ptr := Token_Ptr;
722 Parse_Ptr := Token_Ptr + Literal'Length;
726 -- Start of processing for Parse_Offset_Info
730 -- Set default values, should get changed for all
731 -- units/pragmas except for the last
733 Info.Chop_File := Chop_File;
736 -- Parse the current line of offset information into Info
737 -- and exit the loop if there are any errors or on EOF.
739 -- First case, parse a line in the following format:
741 -- Unit x (spec) line 7, file offset 142, [SR, ]file name x.ads
743 -- Note that the unit name can be an operator name in quotes.
744 -- This is of course illegal, but both GNAT and gnatchop handle
745 -- the case so that this error does not intefere with chopping.
747 -- The SR ir present indicates that a source reference pragma
748 -- was processed as part of this unit (and that therefore no
749 -- Source_Reference pragma should be generated.
751 if Match ("Unit") then
752 Parse_Token (Source, Parse_Ptr, Token_Ptr);
754 if Match ("(body)") then
755 Info.Kind := Unit_Body;
756 elsif Match ("(spec)") then
757 Info.Kind := Unit_Spec;
762 exit when not Match ("line");
763 Parse_Token (Source, Parse_Ptr, Token_Ptr);
764 Info.Start_Line := Line_Num'Value
765 (Source (Token_Ptr .. Parse_Ptr - 1));
767 exit when not Match ("file offset");
768 Parse_Token (Source, Parse_Ptr, Token_Ptr);
769 Info.Offset := File_Offset'Value
770 (Source (Token_Ptr .. Parse_Ptr - 1));
772 Info.SR_Present := Match ("SR, ");
774 exit when not Match ("file name");
775 Parse_Token (Source, Parse_Ptr, Token_Ptr);
776 Info.File_Name := new String'
777 (Directory
.all & Source
(Token_Ptr
.. Parse_Ptr
- 1));
778 Parse_EOL
(Source
, Parse_Ptr
);
780 -- Second case, parse a line of the following form
782 -- Configuration pragmas at line 10, file offset 223
784 elsif Match
("Configuration pragmas at") then
785 Info
.Kind
:= Config_Pragmas
;
786 Info
.File_Name
:= Config_File_Name
;
788 exit when not Match
("line");
789 Parse_Token
(Source
, Parse_Ptr
, Token_Ptr
);
790 Info
.Start_Line
:= Line_Num
'Value
791 (Source
(Token_Ptr
.. Parse_Ptr
- 1));
793 exit when not Match
("file offset");
794 Parse_Token
(Source
, Parse_Ptr
, Token_Ptr
);
795 Info
.Offset
:= File_Offset
'Value
796 (Source
(Token_Ptr
.. Parse_Ptr
- 1));
798 Parse_EOL
(Source
, Parse_Ptr
);
800 -- Third case, parse a line of the following form
802 -- Source_Reference pragma for file "filename"
804 -- This appears at the start of the file only, and indicates
805 -- the name to be used on any generated Source_Reference pragmas.
807 elsif Match
("Source_Reference pragma for file ") then
808 Parse_Token
(Source
, Parse_Ptr
, Token_Ptr
);
809 File
.Table
(Chop_File
).SR_Name
:=
810 new String'(Source (Token_Ptr + 1 .. Parse_Ptr - 2));
811 Parse_EOL (Source, Parse_Ptr);
814 -- Unrecognized keyword or end of file
820 -- Store the data in the Info record in the Unit.Table
823 Unit.Table (Unit.Last) := Info;
825 -- If this is not the first unit from the file, calculate
826 -- the length of the previous unit as difference of the offsets
828 if Unit.Last > First_Unit then
829 Unit.Table (Unit.Last - 1).Length :=
830 Info.Offset - Unit.Table (Unit.Last - 1).Offset;
833 -- If not in compilation mode combine current unit with any
834 -- preceding configuration pragmas.
836 if not Compilation_Mode
837 and then Unit.Last > First_Unit
838 and then Unit.Table (Unit.Last - 1).Kind = Config_Pragmas
840 Info.Start_Line := Unit.Table (Unit.Last - 1).Start_Line;
841 Info.Offset := Unit.Table (Unit.Last - 1).Offset;
843 -- Delete the configuration pragma entry
845 Unit.Table (Unit.Last - 1) := Info;
849 -- If in compilation mode, and previous entry is the initial
850 -- entry for the file and is for configuration pragmas, then
851 -- they are to be appended to every unit in the file.
854 and then Unit.Last = First_Unit + 1
855 and then Unit.Table (First_Unit).Kind = Config_Pragmas
859 (Unit.Table (Unit.Last - 1).Chop_File, First_Unit);
860 Unit.Table (Unit.Last - 1) := Info;
864 Unit.Table (Unit.Last).Bufferg := Bufferg;
866 -- If in compilation mode, and this is not the first item,
867 -- combine configuration pragmas with previous unit, which
868 -- will cause an error message to be generated when the unit
872 and then Unit.Last > First_Unit
873 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
883 -- Find out if the loop was exited prematurely because of
884 -- an error or if the EOF marker was found.
886 if Source (Parse_Ptr) /= EOF then
888 (File.Table (Chop_File).Name.all & ": error parsing offset info");
892 -- Handle case of a chop file consisting only of config pragmas
894 if Unit.Last = First_Unit
895 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
897 -- In compilation mode, we append such a file to gnat.adc
899 if Compilation_Mode then
900 Write_Config_File (Unit.Table (Unit.Last).Chop_File, First_Unit);
903 -- In default (non-compilation) mode, this is invalid
907 (File.Table (Chop_File).Name.all &
908 ": no units found (only pragmas)");
913 -- Handle case of a chop file ending with config pragmas. This can
914 -- happen only in default non-compilation mode, since in compilation
915 -- mode such configuration pragmas are part of the preceding unit.
916 -- We simply concatenate such pragmas to the previous file which
917 -- will cause a compilation error, which is appropriate.
919 if Unit.Last > First_Unit
920 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
924 end Parse_Offset_Info;
930 procedure Parse_Token
931 (Source : access String;
932 Ptr : in out Positive;
933 Token_Ptr : out Positive)
935 In_Quotes : Boolean := False;
940 while Source (Ptr) = ' ' or Source (Ptr) = ',' loop
948 while (In_Quotes or else not (Source (Ptr) = ' ' or Source (Ptr) = ','))
949 and then Source (Ptr) >= ' '
951 if Source (Ptr) = '"' then
952 In_Quotes := not In_Quotes;
964 (FD : File_Descriptor;
965 Contents : out String_Access;
966 Success : out Boolean)
968 Length : constant File_Offset := File_Offset (File_Length (FD));
969 -- Include room for EOF char
970 Buffer : constant String_Access := new String (1 .. Length + 1);
973 Read_Ptr : File_Offset := 1;
978 This_Read := Read (FD,
979 A => Buffer (Read_Ptr)'Address,
980 N => Length + 1 - Read_Ptr);
981 Read_Ptr := Read_Ptr + Integer'Max (This_Read, 0);
982 exit when This_Read <= 0;
985 Buffer (Read_Ptr) := EOF;
986 Contents := new String (1 .. Read_Ptr);
987 Contents.all := Buffer (1 .. Read_Ptr);
989 -- Things aren't simple on VMS due to the plethora of file types
990 -- and organizations. It seems clear that there shouldn't be more
991 -- bytes read than are contained in the file though.
993 if Hostparm.OpenVMS then
994 Success := Read_Ptr <= Length + 1;
996 Success := Read_Ptr = Length + 1;
1000 ----------------------------
1001 -- Report_Duplicate_Units --
1002 ----------------------------
1004 function Report_Duplicate_Units return Boolean is
1008 Duplicates : Boolean := False;
1012 while US < SUnit_Num (Unit.Last) loop
1013 U := Sorted_Units.Table (US);
1015 if Is_Duplicated (US) then
1018 -- Move to last two versions of duplicated file to make it clearer
1019 -- to understand which file is retained in case of overwriting.
1021 while US + 1 < SUnit_Num (Unit.Last) loop
1022 exit when not Is_Duplicated (US + 1);
1026 U := Sorted_Units.Table (US);
1028 if Overwrite_Files then
1029 Warning_Msg (Unit.Table (U).File_Name.all
1030 & " is duplicated
(all but last will be skipped
)");
1032 elsif Unit.Table (U).Chop_File =
1033 Unit.Table (Sorted_Units.Table (US + 1)).Chop_File
1035 Error_Msg (Unit.Table (U).File_Name.all
1036 & " is duplicated
in "
1037 & File.Table (Unit.Table (U).Chop_File).Name.all);
1040 Error_Msg (Unit.Table (U).File_Name.all
1042 & File.Table (Unit.Table (U).Chop_File).Name.all
1043 & " is duplicated
in "
1046 (Sorted_Units.Table (US + 1)).Chop_File).Name.all);
1053 if Duplicates and not Overwrite_Files then
1054 if Hostparm.OpenVMS then
1056 ("use /OVERWRITE to overwrite files
and keep last version
");
1058 Put_Line ("use -w to overwrite files
and keep last version
");
1063 end Report_Duplicate_Units;
1065 --------------------
1066 -- Scan_Arguments --
1067 --------------------
1069 function Scan_Arguments return Boolean is
1070 Kset : Boolean := False;
1071 -- Set true if -k switch found
1074 Initialize_Option_Scan;
1076 -- Scan options first
1079 case Getopt ("c gnat? h k? p q r v w x
-GCC
=!") is
1084 Gcc := new String'(Parameter);
1088 Compilation_Mode := True;
1092 new Argument_List'(Gnat_Args.all &
1093 new String'("-gnat
" & Parameter));
1097 raise Terminate_Program;
1101 Param : String_Access := new String'(Parameter);
1104 if Param.all /= "" then
1105 for J in Param'Range loop
1106 if Param (J) not in '0' .. '9' then
1107 if Hostparm.OpenVMS then
1108 Error_Msg ("/FILE_NAME_MAX_LENGTH
=nnn
" &
1109 " requires numeric parameter
");
1111 Error_Msg ("-k# requires numeric parameter
");
1119 if Hostparm.OpenVMS then
1120 Param := new String'("39");
1122 Param := new String'("8");
1127 new Argument_List'(Gnat_Args.all &
1128 new String'("-gnatk
" & Param.all));
1133 Preserve_Mode := True;
1139 Source_References := True;
1142 Verbose_Mode := True;
1144 -- Why is following written to standard error. Most other
1145 -- tools write to standard output ???
1147 Put (Standard_Error, "GNATCHOP
");
1148 Put_Line (Standard_Error, Gnatvsn.Gnat_Version_String);
1151 "Copyright
1998-2004, Ada Core Technologies Inc
.");
1154 Overwrite_Files := True;
1157 Exit_On_Error := True;
1164 if not Kset and then Maximum_File_Name_Length > 0 then
1166 -- If this system has restricted filename lengths, tell gnat1
1167 -- about them, removing the leading blank from the image string.
1170 new Argument_List'(Gnat_Args.all
1171 & new String'("-gnatk
"
1172 & Maximum_File_Name_Length_String
1173 (Maximum_File_Name_Length_String'First + 1
1174 .. Maximum_File_Name_Length_String'Last)));
1181 S : constant String := Get_Argument (Do_Expansion => True);
1185 File.Increment_Last;
1186 File.Table (File.Last).Name := new String'(S);
1187 File.Table (File.Last).SR_Name := null;
1191 -- Case of more than one file where last file is a directory
1194 and then Is_Directory (File.Table (File.Last).Name.all)
1196 Directory := File.Table (File.Last).Name;
1197 File.Decrement_Last;
1199 -- Make sure Directory is terminated with a directory separator,
1200 -- so we can generate the output by just appending a filename.
1202 if Directory (Directory'Last) /= Directory_Separator
1203 and then Directory (Directory'Last) /= '/'
1205 Directory := new String'(Directory.all & Directory_Separator);
1208 -- At least one filename must be given
1210 elsif File.Last = 0 then
1214 -- No directory given, set directory to null, so that we can just
1215 -- concatenate the directory name to the file name unconditionally.
1218 Directory := new String'("");
1221 -- Finally check all filename arguments
1223 for File_Num in 1 .. File.Last loop
1225 F : constant String := File.Table (File_Num).Name.all;
1229 if Is_Directory (F) then
1230 Error_Msg (F & " is a directory
, cannot be chopped
");
1233 elsif not Is_Regular_File (F) then
1234 Error_Msg (F & " not found
");
1243 when Invalid_Switch =>
1244 Error_Msg ("invalid switch
" & Full_Switch);
1247 when Invalid_Parameter =>
1248 if Hostparm.OpenVMS then
1249 Error_Msg ("/FILE_NAME_MAX_LENGTH
=nnn qualifier
" &
1250 " requires numeric parameter
");
1252 Error_Msg ("-k switch requires numeric parameter
");
1263 procedure Sort_Units is
1265 procedure Move (From : Natural; To : Natural);
1266 -- Procedure used to sort the unit list
1267 -- Unit.Table (To) := Unit_List (From); used by sort
1269 function Lt (Left, Right : Natural) return Boolean;
1270 -- Compares Left and Right units based on file name (first),
1271 -- Chop_File (second) and Offset (third). This ordering is
1272 -- important to keep the last version in case of duplicate files.
1274 package Unit_Sort is new GNAT.Heap_Sort_G (Move, Lt);
1275 -- Used for sorting on filename to detect duplicates
1281 function Lt (Left, Right : Natural) return Boolean is
1282 L : Unit_Info renames
1283 Unit.Table (Sorted_Units.Table (SUnit_Num (Left)));
1285 R : Unit_Info renames
1286 Unit.Table (Sorted_Units.Table (SUnit_Num (Right)));
1289 return L.File_Name.all < R.File_Name.all
1290 or else (L.File_Name.all = R.File_Name.all
1291 and then (L.Chop_File < R.Chop_File
1292 or else (L.Chop_File = R.Chop_File
1293 and then L.Offset < R.Offset)));
1300 procedure Move (From : Natural; To : Natural) is
1302 Sorted_Units.Table (SUnit_Num (To)) :=
1303 Sorted_Units.Table (SUnit_Num (From));
1306 -- Start of processing for Sort_Units
1309 Sorted_Units.Set_Last (SUnit_Num (Unit.Last));
1311 for J in 1 .. Unit.Last loop
1312 Sorted_Units.Table (SUnit_Num (J)) := J;
1315 -- Sort Unit.Table, using Sorted_Units.Table (0) as scratch
1317 Unit_Sort.Sort (Natural (Unit.Last));
1319 -- Set the Sorted_Index fields in the unit tables.
1321 for J in 1 .. SUnit_Num (Unit.Last) loop
1322 Unit.Table (Sorted_Units.Table (J)).Sorted_Index := J;
1333 ("Usage
: gnatchop
[-c
] [-h
] [-k#
] " &
1334 "[-r
] [-p
] [-q
] [-v
] [-w
] [-x
] [--GCC=xx] file [file ...] [dir]");
1338 (" -c compilation mode, configuration pragmas " &
1342 (" -gnatxxx passes the -gnatxxx switch to gnat parser");
1345 (" -h help: output this usage information");
1348 (" -k# krunch file names of generated files to " &
1349 "no more than # characters");
1352 (" -k krunch file names of generated files to " &
1353 "no more than 8 characters");
1356 (" -p preserve time stamp, output files will " &
1357 "have same stamp as input");
1360 (" -q quiet mode, no output of generated file " &
1364 (" -r generate Source_Reference pragmas refer" &
1365 "encing original source file");
1368 (" -v verbose mode, output version and generat" &
1372 (" -w overwrite existing filenames");
1375 (" -x exit on error");
1378 (" --GCC=xx specify the path of the gnat parser to be used");
1382 (" file... list of source files to be chopped");
1385 (" dir directory location for split files (defa" &
1386 "ult = current directory)");
1393 procedure Warning_Msg
(Message
: String) is
1395 Warning_Count
:= Warning_Count
+ 1;
1396 Put_Line
(Standard_Error
, "warning: " & Message
);
1399 -------------------------
1400 -- Write_Chopped_Files --
1401 -------------------------
1403 function Write_Chopped_Files
(Input
: File_Num
) return Boolean is
1404 Name
: aliased constant String :=
1405 File
.Table
(Input
).Name
.all & ASCII
.Nul
;
1406 FD
: File_Descriptor
;
1407 Buffer
: String_Access
;
1412 FD
:= Open_Read
(Name
'Address, Binary
);
1413 TS_Time
:= File_Time_Stamp
(FD
);
1415 if FD
= Invalid_FD
then
1416 Error_Msg
("cannot open " & File
.Table
(Input
).Name
.all);
1420 Read_File
(FD
, Buffer
, Success
);
1423 Error_Msg
("cannot read " & File
.Table
(Input
).Name
.all);
1428 if not Quiet_Mode
then
1429 Put_Line
("splitting " & File
.Table
(Input
).Name
.all & " into:");
1432 -- Only chop those units that come from this file
1434 for Num
in 1 .. Unit
.Last
loop
1435 if Unit
.Table
(Num
).Chop_File
= Input
then
1436 Write_Unit
(Buffer
, Num
, TS_Time
, Success
);
1437 exit when not Success
;
1444 end Write_Chopped_Files
;
1446 -----------------------
1447 -- Write_Config_File --
1448 -----------------------
1450 procedure Write_Config_File
(Input
: File_Num
; U
: Unit_Num
) is
1451 FD
: File_Descriptor
;
1452 Name
: aliased constant String := "gnat.adc" & ASCII
.NUL
;
1453 Buffer
: String_Access
;
1456 Buffera
: String_Access
;
1460 Write_gnat_adc
:= True;
1461 FD
:= Open_Read_Write
(Name
'Address, Binary
);
1463 if FD
= Invalid_FD
then
1464 FD
:= Create_File
(Name
'Address, Binary
);
1467 if not Quiet_Mode
then
1468 Put_Line
("writing configuration pragmas from " &
1469 File
.Table
(Input
).Name
.all & " to gnat.adc");
1475 if not Quiet_Mode
then
1477 ("appending configuration pragmas from " &
1478 File
.Table
(Input
).Name
.all & " to gnat.adc");
1482 Success
:= FD
/= Invalid_FD
;
1485 Error_Msg
("cannot create gnat.adc");
1489 -- In append mode, acquire existing gnat.adc file
1492 Read_File
(FD
, Buffera
, Success
);
1495 Error_Msg
("cannot read gnat.adc");
1499 -- Find location of EOF byte if any to exclude from append
1502 while Bufferl
<= Buffera
'Last
1503 and then Buffera
(Bufferl
) /= EOF
1505 Bufferl
:= Bufferl
+ 1;
1508 Bufferl
:= Bufferl
- 1;
1511 -- Write existing gnat.adc to new gnat.adc file
1513 FD
:= Create_File
(Name
'Address, Binary
);
1514 Success
:= Write
(FD
, Buffera
(1)'Address, Bufferl
) = Bufferl
;
1517 Error_Msg
("error writing gnat.adc");
1522 Buffer
:= Get_Config_Pragmas
(Input
, U
);
1524 if Buffer
/= null then
1525 Success
:= Write
(FD
, Buffer
.all'Address, Buffer
'Length) =
1529 Error_Msg
("disk full writing gnat.adc");
1535 end Write_Config_File
;
1537 -----------------------------------
1538 -- Write_Source_Reference_Pragma --
1539 -----------------------------------
1541 procedure Write_Source_Reference_Pragma
1544 FD
: File_Descriptor
;
1546 Success
: in out Boolean)
1548 FTE
: File_Entry
renames File
.Table
(Info
.Chop_File
);
1549 Nam
: String_Access
;
1552 if Success
and Source_References
and not Info
.SR_Present
then
1553 if FTE
.SR_Name
/= null then
1560 Reference
: aliased String :=
1561 "pragma Source_Reference (000000, """
1562 & Nam
.all & """);" & EOL
.Str
;
1564 Pos
: Positive := Reference
'First;
1565 Lin
: Line_Num
:= Line
;
1568 while Reference
(Pos
+ 1) /= ',' loop
1572 while Reference
(Pos
) = '0' loop
1573 Reference
(Pos
) := Character'Val
1574 (Character'Pos ('0') + Lin
mod 10);
1579 -- Assume there are enough zeroes for any program length
1581 pragma Assert
(Lin
= 0);
1584 Write
(FD
, Reference
'Address, Reference
'Length)
1588 end Write_Source_Reference_Pragma
;
1594 procedure Write_Unit
1595 (Source
: access String;
1598 Success
: out Boolean)
1600 Info
: Unit_Info
renames Unit
.Table
(Num
);
1601 FD
: File_Descriptor
;
1602 Name
: aliased constant String := Info
.File_Name
.all & ASCII
.NUL
;
1603 Length
: File_Offset
;
1604 EOL
: constant EOL_String
:=
1605 Get_EOL
(Source
, Source
'First + Info
.Offset
);
1608 -- Skip duplicated files
1610 if Is_Duplicated
(Info
.Sorted_Index
) then
1611 Put_Line
(" " & Info
.File_Name
.all & " skipped");
1612 Success
:= Overwrite_Files
;
1616 if Overwrite_Files
then
1617 FD
:= Create_File
(Name
'Address, Binary
);
1619 FD
:= Create_New_File
(Name
'Address, Binary
);
1622 Success
:= FD
/= Invalid_FD
;
1625 Error_Msg
("cannot create " & Info
.File_Name
.all);
1629 -- A length of 0 indicates that the rest of the file belongs to
1630 -- this unit. The actual length must be calculated now. Take into
1631 -- account that the last character (EOF) must not be written.
1633 if Info
.Length
= 0 then
1634 Length
:= Source
'Last - (Source
'First + Info
.Offset
);
1636 Length
:= Info
.Length
;
1639 -- Prepend configuration pragmas if necessary
1641 if Success
and then Info
.Bufferg
/= null then
1642 Write_Source_Reference_Pragma
(Info
, 1, FD
, EOL
, Success
);
1644 Write
(FD
, Info
.Bufferg
.all'Address, Info
.Bufferg
'Length) =
1645 Info
.Bufferg
'Length;
1648 Write_Source_Reference_Pragma
(Info
, Info
.Start_Line
, FD
, EOL
, Success
);
1651 Success
:= Write
(FD
, Source
(Source
'First + Info
.Offset
)'Address,
1656 Error_Msg
("disk full writing " & Info
.File_Name
.all);
1660 if not Quiet_Mode
then
1661 Put_Line
(" " & Info
.File_Name
.all);
1666 if Preserve_Mode
then
1667 File_Time_Stamp
(Name
'Address, TS_Time
);
1672 -- Start of processing for gnatchop
1675 -- Add the directory where gnatchop is invoked in front of the
1676 -- path, if gnatchop is invoked with directory information.
1677 -- Only do this if the platform is not VMS, where the notion of path
1678 -- does not really exist.
1680 if not Hostparm
.OpenVMS
then
1682 Command
: constant String := Command_Name
;
1685 for Index
in reverse Command
'Range loop
1686 if Command
(Index
) = Directory_Separator
then
1688 Absolute_Dir
: constant String :=
1690 (Command
(Command
'First .. Index
));
1692 PATH
: constant String :=
1695 Getenv
("PATH").all;
1698 Setenv
("PATH", PATH
);
1707 -- Process command line options and initialize global variables
1709 if not Scan_Arguments
then
1710 Set_Exit_Status
(Failure
);
1714 -- Check presence of required executables
1716 Gnat_Cmd
:= Locate_Executable
(Gcc
.all, not Gcc_Set
);
1718 if Gnat_Cmd
= null then
1719 goto No_Files_Written
;
1722 -- First parse all files and read offset information
1724 for Num
in 1 .. File
.Last
loop
1725 if not Parse_File
(Num
) then
1726 goto No_Files_Written
;
1730 -- Check if any units have been found (assumes non-empty Unit.Table)
1732 if Unit
.Last
= 0 then
1733 if not Write_gnat_adc
then
1734 Error_Msg
("no compilation units found", Warning
=> True);
1737 goto No_Files_Written
;
1742 -- Check if any duplicate files would be created. If so, emit
1743 -- a warning if Overwrite_Files is true, otherwise generate an error.
1745 if Report_Duplicate_Units
and then not Overwrite_Files
then
1746 goto No_Files_Written
;
1749 -- Check if any files exist, if so do not write anything
1750 -- Because all files have been parsed and checked already,
1751 -- there won't be any duplicates
1753 if not Overwrite_Files
and then Files_Exist
then
1754 goto No_Files_Written
;
1757 -- After this point, all source files are read in succession
1758 -- and chopped into their destination files.
1760 -- As the Source_File_Name pragmas are handled as logical file 0,
1763 for F
in 1 .. File
.Last
loop
1764 if not Write_Chopped_Files
(F
) then
1765 Set_Exit_Status
(Failure
);
1770 if Warning_Count
> 0 then
1772 Warnings_Msg
: constant String := Warning_Count
'Img & " warning(s)";
1774 Error_Msg
(Warnings_Msg
(2 .. Warnings_Msg
'Last), Warning
=> True);
1780 <<No_Files_Written
>>
1782 -- Special error exit for all situations where no files have
1785 if not Write_gnat_adc
then
1786 Error_Msg
("no source files written", Warning
=> True);
1792 when Terminate_Program
=>