* gimplify.c (find_single_pointer_decl_1): New static function.
[official-gcc.git] / gcc / ada / gnatchop.adb
blob2a68776a5f89e61193a347ad4828a46de8b9a026
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T C H O P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2005 Ada Core Technologies, Inc. --
10 -- --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
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;
33 with GNAT.Table;
35 with Gnatvsn;
36 with Hostparm;
38 with System.CRTL; use System.CRTL;
40 procedure Gnatchop is
42 Terminate_Program : exception;
43 -- Used to terminate execution immediately
45 Config_File_Name : constant String_Access := new String'("gnat.adc");
46 -- The name of the file holding the GNAT configuration pragmas
48 Gcc : String_Access := new String'("gcc");
49 -- May be modified by switch --GCC=
51 Gcc_Set : Boolean := False;
52 -- True if a switch --GCC= is used
54 Gnat_Cmd : String_Access;
55 -- Command to execute the GNAT compiler
57 Gnat_Args : Argument_List_Access :=
58 new Argument_List'
59 (new String'("-c"),
60 new String'("-x"),
61 new String'("ada"),
62 new String'("-gnats"),
63 new String'("-gnatu"));
64 -- Arguments used in Gnat_Cmd call
66 EOF : constant Character := Character'Val (26);
67 -- Special character to signal end of file. Not required in input
68 -- files, but properly treated if present. Not generated in output
69 -- files except as a result of copying input file.
71 --------------------
72 -- File arguments --
73 --------------------
75 subtype File_Num is Natural;
76 subtype File_Offset is Natural;
78 type File_Entry is record
79 Name : String_Access;
80 -- Name of chop file or directory
82 SR_Name : String_Access;
83 -- Null unless the chop file starts with a source reference pragma
84 -- in which case this field points to the file name from this pragma.
85 end record;
87 package File is new GNAT.Table
88 (Table_Component_Type => File_Entry,
89 Table_Index_Type => File_Num,
90 Table_Low_Bound => 1,
91 Table_Initial => 100,
92 Table_Increment => 100);
94 Directory : String_Access;
95 -- Record name of directory, or a null string if no directory given
97 Compilation_Mode : Boolean := False;
98 Overwrite_Files : Boolean := False;
99 Preserve_Mode : Boolean := False;
100 Quiet_Mode : Boolean := False;
101 Source_References : Boolean := False;
102 Verbose_Mode : Boolean := False;
103 Exit_On_Error : Boolean := False;
104 -- Global options
106 Write_gnat_adc : Boolean := False;
107 -- Gets set true if we append to gnat.adc or create a new gnat.adc.
108 -- Used to inhibit complaint about no units generated.
110 ---------------
111 -- Unit list --
112 ---------------
114 type Line_Num is new Natural;
115 -- Line number (for source reference pragmas)
117 type Unit_Count_Type is new Integer;
118 subtype Unit_Num is Unit_Count_Type range 1 .. Unit_Count_Type'Last;
119 -- Used to refer to unit number in unit table
121 type SUnit_Num is new Integer;
122 -- Used to refer to entry in sorted units table. Note that entry
123 -- zero is only for use by Heapsort, and is not otherwise referenced.
125 type Unit_Kind is (Unit_Spec, Unit_Body, Config_Pragmas);
127 -- Structure to contain all necessary information for one unit.
128 -- Entries are also temporarily used to record config pragma sequences.
130 type Unit_Info is record
131 File_Name : String_Access;
132 -- File name from GNAT output line
134 Chop_File : File_Num;
135 -- File number in chop file sequence
137 Start_Line : Line_Num;
138 -- Line number from GNAT output line
140 Offset : File_Offset;
141 -- Offset name from GNAT output line
143 SR_Present : Boolean;
144 -- Set True if SR parameter present
146 Length : File_Offset;
147 -- A length of 0 means that the Unit is the last one in the file
149 Kind : Unit_Kind;
150 -- Indicates kind of unit
152 Sorted_Index : SUnit_Num;
153 -- Index of unit in sorted unit list
155 Bufferg : String_Access;
156 -- Pointer to buffer containing configuration pragmas to be
157 -- prepended. Null if no pragmas to be prepended.
159 end record;
161 -- The following table stores the unit offset information
163 package Unit is new GNAT.Table
164 (Table_Component_Type => Unit_Info,
165 Table_Index_Type => Unit_Count_Type,
166 Table_Low_Bound => 1,
167 Table_Initial => 500,
168 Table_Increment => 100);
170 -- The following table is used as a sorted index to the Unit.Table.
171 -- The entries in Unit.Table are not moved, instead we just shuffle
172 -- the entries in Sorted_Units. Note that the zeroeth entry in this
173 -- table is used by GNAT.Heap_Sort_G.
175 package Sorted_Units is new GNAT.Table
176 (Table_Component_Type => Unit_Num,
177 Table_Index_Type => SUnit_Num,
178 Table_Low_Bound => 0,
179 Table_Initial => 500,
180 Table_Increment => 100);
182 function Is_Duplicated (U : SUnit_Num) return Boolean;
183 -- Returns true if U is duplicated by a later unit.
184 -- Note that this function returns false for the last entry.
186 procedure Sort_Units;
187 -- Sort units and set up sorted unit table
189 ----------------------
190 -- File_Descriptors --
191 ----------------------
193 function dup (handle : File_Descriptor) return File_Descriptor;
194 function dup2 (from, to : File_Descriptor) return File_Descriptor;
196 ---------------------
197 -- Local variables --
198 ---------------------
200 Warning_Count : Natural := 0;
201 -- Count of warnings issued so far
203 -----------------------
204 -- Local subprograms --
205 -----------------------
207 procedure Error_Msg (Message : String; Warning : Boolean := False);
208 -- Produce an error message on standard error output
210 procedure File_Time_Stamp (Name : C_File_Name; Time : OS_Time);
211 -- Given the name of a file or directory, Name, set the
212 -- time stamp. This function must be used for an unopened file.
214 function Files_Exist return Boolean;
215 -- Check Unit.Table for possible file names that already exist
216 -- in the file system. Returns true if files exist, False otherwise
218 function Get_Maximum_File_Name_Length return Integer;
219 pragma Import (C, Get_Maximum_File_Name_Length,
220 "__gnat_get_maximum_file_name_length");
221 -- Function to get maximum file name length for system
223 Maximum_File_Name_Length : constant Integer := Get_Maximum_File_Name_Length;
224 Maximum_File_Name_Length_String : constant String :=
225 Integer'Image
226 (Maximum_File_Name_Length);
228 function Locate_Executable
229 (Program_Name : String;
230 Look_For_Prefix : Boolean := True)
231 return String_Access;
232 -- Locate executable for given program name. This takes into account
233 -- the target-prefix of the current command, if Look_For_Prefix is True.
235 subtype EOL_Length is Natural range 0 .. 2;
236 -- Possible lengths of end of line sequence
238 type EOL_String (Len : EOL_Length := 0) is record
239 Str : String (1 .. Len);
240 end record;
242 function Get_EOL
243 (Source : access String;
244 Start : Positive)
245 return EOL_String;
246 -- Return the line terminator used in the passed string
248 procedure Parse_EOL (Source : access String; Ptr : in out Positive);
249 -- On return Source (Ptr) is the first character of the next line
250 -- or EOF. Source.all must be terminated by EOF.
252 function Parse_File (Num : File_Num) return Boolean;
253 -- Calls the GNAT compiler to parse the given source file and parses the
254 -- output using Parse_Offset_Info. Returns True if parse operation
255 -- completes, False if some system error (e.g. failure to read the
256 -- offset information) occurs.
258 procedure Parse_Offset_Info (Chop_File : File_Num; Source : access String);
259 -- Parses the output of the compiler indicating the offsets
260 -- and names of the compilation units in Chop_File.
262 procedure Parse_Token
263 (Source : access String;
264 Ptr : in out Positive;
265 Token_Ptr : out Positive);
266 -- Skips any separators and stores the start of the token in Token_Ptr.
267 -- Then stores the position of the next separator in Ptr.
268 -- On return Source (Token_Ptr .. Ptr - 1) is the token.
270 procedure Read_File
271 (FD : File_Descriptor;
272 Contents : out String_Access;
273 Success : out Boolean);
274 -- Reads file associated with FS into the newly allocated
275 -- string Contents.
276 -- [VMS] Success is true iff the number of bytes read is less than or
277 -- equal to the file size.
278 -- [Other] Success is true iff the number of bytes read is equal to
279 -- the file size.
281 function Report_Duplicate_Units return Boolean;
282 -- Output messages about duplicate units in the input files in Unit.Table
283 -- Returns True if any duplicates found, Fals if no duplicates found.
285 function Scan_Arguments return Boolean;
286 -- Scan command line options and set global variables accordingly.
287 -- Also scan out file and directory arguments. Returns True if scan
288 -- was successful, and False if the scan fails for any reason.
290 procedure Usage;
291 -- Output message on standard output describing syntax of gnatchop command
293 procedure Warning_Msg (Message : String);
294 -- Output a warning message on standard error and update warning count
296 function Write_Chopped_Files (Input : File_Num) return Boolean;
297 -- Write all units that result from chopping the Input file
299 procedure Write_Config_File (Input : File_Num; U : Unit_Num);
300 -- Call to write configuration pragmas (append them to gnat.adc)
301 -- Input is the file number for the chop file and U identifies the
302 -- unit entry for the configuration pragmas.
304 function Get_Config_Pragmas
305 (Input : File_Num;
306 U : Unit_Num)
307 return String_Access;
308 -- Call to read configuration pragmas from given unit entry, and
309 -- return a buffer containing the pragmas to be appended to
310 -- following units. Input is the file number for the chop file and
311 -- U identifies the unit entry for the configuration pragmas.
313 procedure Write_Source_Reference_Pragma
314 (Info : Unit_Info;
315 Line : Line_Num;
316 FD : File_Descriptor;
317 EOL : EOL_String;
318 Success : in out Boolean);
319 -- If Success is True on entry, writes a source reference pragma using
320 -- the chop file from Info, and the given line number. On return Success
321 -- indicates whether the write succeeded. If Success is False on entry,
322 -- or if the global flag Source_References is False, then the call to
323 -- Write_Source_Reference_Pragma has no effect. EOL indicates the end
324 -- of line sequence to be written at the end of the pragma.
326 procedure Write_Unit
327 (Source : access String;
328 Num : Unit_Num;
329 TS_Time : OS_Time;
330 Success : out Boolean);
331 -- Write one compilation unit of the source to file
333 ---------
334 -- dup --
335 ---------
337 function dup (handle : File_Descriptor) return File_Descriptor is
338 begin
339 return File_Descriptor (System.CRTL.dup (int (handle)));
340 end dup;
342 ----------
343 -- dup2 --
344 ----------
346 function dup2 (from, to : File_Descriptor) return File_Descriptor is
347 begin
348 return File_Descriptor (System.CRTL.dup2 (int (from), int (to)));
349 end dup2;
351 ---------------
352 -- Error_Msg --
353 ---------------
355 procedure Error_Msg (Message : String; Warning : Boolean := False) is
356 begin
357 Put_Line (Standard_Error, Message);
359 if not Warning then
360 Set_Exit_Status (Failure);
362 if Exit_On_Error then
363 raise Terminate_Program;
364 end if;
365 end if;
366 end Error_Msg;
368 ---------------------
369 -- File_Time_Stamp --
370 ---------------------
372 procedure File_Time_Stamp (Name : C_File_Name; Time : OS_Time) is
373 procedure Set_File_Time (Name : C_File_Name; Time : OS_Time);
374 pragma Import (C, Set_File_Time, "__gnat_set_file_time_name");
376 begin
377 Set_File_Time (Name, Time);
378 end File_Time_Stamp;
380 -----------------
381 -- Files_Exist --
382 -----------------
384 function Files_Exist return Boolean is
385 Exists : Boolean := False;
387 begin
388 for SNum in 1 .. SUnit_Num (Unit.Last) loop
390 -- Only check and report for the last instance of duplicated files
392 if not Is_Duplicated (SNum) then
393 declare
394 Info : constant Unit_Info :=
395 Unit.Table (Sorted_Units.Table (SNum));
397 begin
398 if Is_Writable_File (Info.File_Name.all) then
399 if Hostparm.OpenVMS then
400 Error_Msg
401 (Info.File_Name.all
402 & " already exists, use /OVERWRITE to overwrite");
403 else
404 Error_Msg (Info.File_Name.all
405 & " already exists, use -w to overwrite");
406 end if;
408 Exists := True;
409 end if;
410 end;
411 end if;
412 end loop;
414 return Exists;
415 end Files_Exist;
417 ------------------------
418 -- Get_Config_Pragmas --
419 ------------------------
421 function Get_Config_Pragmas
422 (Input : File_Num;
423 U : Unit_Num)
424 return String_Access
426 Info : Unit_Info renames Unit.Table (U);
427 FD : File_Descriptor;
428 Name : aliased constant String :=
429 File.Table (Input).Name.all & ASCII.Nul;
430 Length : File_Offset;
431 Buffer : String_Access;
432 Success : Boolean;
433 Result : String_Access;
435 begin
436 FD := Open_Read (Name'Address, Binary);
438 if FD = Invalid_FD then
439 Error_Msg ("cannot open " & File.Table (Input).Name.all);
440 return null;
441 end if;
443 Read_File (FD, Buffer, Success);
445 -- A length of 0 indicates that the rest of the file belongs to
446 -- this unit. The actual length must be calculated now. Take into
447 -- account that the last character (EOF) must not be written.
449 if Info.Length = 0 then
450 Length := Buffer'Last - (Buffer'First + Info.Offset);
451 else
452 Length := Info.Length;
453 end if;
455 Result := new String'(Buffer (1 .. Length));
456 Close (FD);
457 return Result;
458 end Get_Config_Pragmas;
460 -------------
461 -- Get_EOL --
462 -------------
464 function Get_EOL
465 (Source : access String;
466 Start : Positive)
467 return EOL_String
469 Ptr : Positive := Start;
470 First : Positive;
471 Last : Natural;
473 begin
474 -- Skip to end of line
476 while Source (Ptr) /= ASCII.CR and then
477 Source (Ptr) /= ASCII.LF and then
478 Source (Ptr) /= EOF
479 loop
480 Ptr := Ptr + 1;
481 end loop;
483 Last := Ptr;
485 if Source (Ptr) /= EOF then
487 -- Found CR or LF
489 First := Ptr;
491 else
492 First := Ptr + 1;
493 end if;
495 -- Recognize CR/LF or LF/CR combination
497 if (Source (Ptr + 1) = ASCII.CR or Source (Ptr + 1) = ASCII.LF)
498 and then Source (Ptr) /= Source (Ptr + 1)
499 then
500 Last := First + 1;
501 end if;
503 return (Len => Last + 1 - First, Str => Source (First .. Last));
504 end Get_EOL;
506 -------------------
507 -- Is_Duplicated --
508 -------------------
510 function Is_Duplicated (U : SUnit_Num) return Boolean is
511 begin
512 return U < SUnit_Num (Unit.Last)
513 and then
514 Unit.Table (Sorted_Units.Table (U)).File_Name.all =
515 Unit.Table (Sorted_Units.Table (U + 1)).File_Name.all;
516 end Is_Duplicated;
518 -----------------------
519 -- Locate_Executable --
520 -----------------------
522 function Locate_Executable
523 (Program_Name : String;
524 Look_For_Prefix : Boolean := True) return String_Access
526 Current_Command : constant String := Normalize_Pathname (Command_Name);
527 End_Of_Prefix : Natural;
528 Start_Of_Prefix : Positive;
529 Result : String_Access;
531 begin
532 Start_Of_Prefix := Current_Command'First;
533 End_Of_Prefix := Start_Of_Prefix - 1;
535 if Look_For_Prefix then
537 -- Find Start_Of_Prefix
539 for J in reverse Current_Command'Range loop
540 if Current_Command (J) = '/' or
541 Current_Command (J) = Directory_Separator or
542 Current_Command (J) = ':'
543 then
544 Start_Of_Prefix := J + 1;
545 exit;
546 end if;
547 end loop;
549 -- Find End_Of_Prefix
551 for J in reverse Start_Of_Prefix .. Current_Command'Last loop
552 if Current_Command (J) = '-' then
553 End_Of_Prefix := J;
554 exit;
555 end if;
556 end loop;
557 end if;
559 declare
560 Command : constant String :=
561 Current_Command (Start_Of_Prefix .. End_Of_Prefix) &
562 Program_Name;
563 begin
564 Result := Locate_Exec_On_Path (Command);
566 if Result = null then
567 Error_Msg
568 (Command & ": installation problem, executable not found");
569 end if;
570 end;
572 return Result;
573 end Locate_Executable;
575 ---------------
576 -- Parse_EOL --
577 ---------------
579 procedure Parse_EOL (Source : access String; Ptr : in out Positive) is
580 begin
581 -- Skip to end of line
583 while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
584 and then Source (Ptr) /= EOF
585 loop
586 Ptr := Ptr + 1;
587 end loop;
589 if Source (Ptr) /= EOF then
590 Ptr := Ptr + 1; -- skip CR or LF
591 end if;
593 -- Skip past CR/LF or LF/CR combination
595 if (Source (Ptr) = ASCII.CR or Source (Ptr) = ASCII.LF)
596 and then Source (Ptr) /= Source (Ptr - 1)
597 then
598 Ptr := Ptr + 1;
599 end if;
600 end Parse_EOL;
602 ----------------
603 -- Parse_File --
604 ----------------
606 function Parse_File (Num : File_Num) return Boolean is
607 Chop_Name : constant String_Access := File.Table (Num).Name;
608 Save_Stdout : constant File_Descriptor := dup (Standout);
609 Offset_Name : Temp_File_Name;
610 Offset_FD : File_Descriptor;
611 Buffer : String_Access;
612 Success : Boolean;
613 Failure : exception;
615 begin
616 -- Display copy of GNAT command if verbose mode
618 if Verbose_Mode then
619 Put (Gnat_Cmd.all);
621 for J in 1 .. Gnat_Args'Length loop
622 Put (' ');
623 Put (Gnat_Args (J).all);
624 end loop;
626 Put (' ');
627 Put_Line (Chop_Name.all);
628 end if;
630 -- Create temporary file
632 Create_Temp_File (Offset_FD, Offset_Name);
634 if Offset_FD = Invalid_FD then
635 Error_Msg ("gnatchop: cannot create temporary file");
636 Close (Save_Stdout);
637 return False;
638 end if;
640 -- Redirect Stdout to this temporary file in the Unix way
642 if dup2 (Offset_FD, Standout) = Invalid_FD then
643 Error_Msg ("gnatchop: cannot redirect stdout to temporary file");
644 Close (Save_Stdout);
645 Close (Offset_FD);
646 return False;
647 end if;
649 -- Call Gnat on the source filename argument with special options
650 -- to generate offset information. If this special compilation completes
651 -- successfully then we can do the actual gnatchop operation.
653 Spawn (Gnat_Cmd.all, Gnat_Args.all & Chop_Name, Success);
655 if not Success then
656 Error_Msg (Chop_Name.all & ": parse errors detected");
657 Error_Msg (Chop_Name.all & ": chop may not be successful");
658 end if;
660 -- Restore stdout
662 if dup2 (Save_Stdout, Standout) = Invalid_FD then
663 Error_Msg ("gnatchop: cannot restore stdout");
664 end if;
666 -- Reopen the file to start reading from the beginning
668 Close (Offset_FD);
669 Close (Save_Stdout);
670 Offset_FD := Open_Read (Offset_Name'Address, Binary);
672 if Offset_FD = Invalid_FD then
673 Error_Msg ("gnatchop: cannot access offset info");
674 raise Failure;
675 end if;
677 Read_File (Offset_FD, Buffer, Success);
679 if not Success then
680 Error_Msg ("gnatchop: error reading offset info");
681 Close (Offset_FD);
682 raise Failure;
683 else
684 Parse_Offset_Info (Num, Buffer);
685 end if;
687 -- Close and delete temporary file
689 Close (Offset_FD);
690 Delete_File (Offset_Name'Address, Success);
692 return Success;
694 exception
695 when Failure | Terminate_Program =>
696 Close (Offset_FD);
697 Delete_File (Offset_Name'Address, Success);
698 return False;
700 end Parse_File;
702 -----------------------
703 -- Parse_Offset_Info --
704 -----------------------
706 procedure Parse_Offset_Info
707 (Chop_File : File_Num;
708 Source : access String)
710 First_Unit : constant Unit_Num := Unit.Last + 1;
711 Bufferg : String_Access := null;
712 Parse_Ptr : File_Offset := Source'First;
713 Token_Ptr : File_Offset;
714 Info : Unit_Info;
716 function Match (Literal : String) return Boolean;
717 -- Checks if given string appears at the current Token_Ptr location
718 -- and if so, bumps Parse_Ptr past the token and returns True. If
719 -- the string is not present, sets Parse_Ptr to Token_Ptr and
720 -- returns False.
722 -----------
723 -- Match --
724 -----------
726 function Match (Literal : String) return Boolean is
727 begin
728 Parse_Token (Source, Parse_Ptr, Token_Ptr);
730 if Source'Last + 1 - Token_Ptr < Literal'Length
731 or else
732 Source (Token_Ptr .. Token_Ptr + Literal'Length - 1) /= Literal
733 then
734 Parse_Ptr := Token_Ptr;
735 return False;
736 end if;
738 Parse_Ptr := Token_Ptr + Literal'Length;
739 return True;
740 end Match;
742 -- Start of processing for Parse_Offset_Info
744 begin
745 loop
746 -- Set default values, should get changed for all
747 -- units/pragmas except for the last
749 Info.Chop_File := Chop_File;
750 Info.Length := 0;
752 -- Parse the current line of offset information into Info
753 -- and exit the loop if there are any errors or on EOF.
755 -- First case, parse a line in the following format:
757 -- Unit x (spec) line 7, file offset 142, [SR, ]file name x.ads
759 -- Note that the unit name can be an operator name in quotes.
760 -- This is of course illegal, but both GNAT and gnatchop handle
761 -- the case so that this error does not intefere with chopping.
763 -- The SR ir present indicates that a source reference pragma
764 -- was processed as part of this unit (and that therefore no
765 -- Source_Reference pragma should be generated.
767 if Match ("Unit") then
768 Parse_Token (Source, Parse_Ptr, Token_Ptr);
770 if Match ("(body)") then
771 Info.Kind := Unit_Body;
772 elsif Match ("(spec)") then
773 Info.Kind := Unit_Spec;
774 else
775 exit;
776 end if;
778 exit when not Match ("line");
779 Parse_Token (Source, Parse_Ptr, Token_Ptr);
780 Info.Start_Line := Line_Num'Value
781 (Source (Token_Ptr .. Parse_Ptr - 1));
783 exit when not Match ("file offset");
784 Parse_Token (Source, Parse_Ptr, Token_Ptr);
785 Info.Offset := File_Offset'Value
786 (Source (Token_Ptr .. Parse_Ptr - 1));
788 Info.SR_Present := Match ("SR, ");
790 exit when not Match ("file name");
791 Parse_Token (Source, Parse_Ptr, Token_Ptr);
792 Info.File_Name := new String'
793 (Directory.all & Source (Token_Ptr .. Parse_Ptr - 1));
794 Parse_EOL (Source, Parse_Ptr);
796 -- Second case, parse a line of the following form
798 -- Configuration pragmas at line 10, file offset 223
800 elsif Match ("Configuration pragmas at") then
801 Info.Kind := Config_Pragmas;
802 Info.File_Name := Config_File_Name;
804 exit when not Match ("line");
805 Parse_Token (Source, Parse_Ptr, Token_Ptr);
806 Info.Start_Line := Line_Num'Value
807 (Source (Token_Ptr .. Parse_Ptr - 1));
809 exit when not Match ("file offset");
810 Parse_Token (Source, Parse_Ptr, Token_Ptr);
811 Info.Offset := File_Offset'Value
812 (Source (Token_Ptr .. Parse_Ptr - 1));
814 Parse_EOL (Source, Parse_Ptr);
816 -- Third case, parse a line of the following form
818 -- Source_Reference pragma for file "filename"
820 -- This appears at the start of the file only, and indicates
821 -- the name to be used on any generated Source_Reference pragmas.
823 elsif Match ("Source_Reference pragma for file ") then
824 Parse_Token (Source, Parse_Ptr, Token_Ptr);
825 File.Table (Chop_File).SR_Name :=
826 new String'(Source (Token_Ptr + 1 .. Parse_Ptr - 2));
827 Parse_EOL (Source, Parse_Ptr);
828 goto Continue;
830 -- Unrecognized keyword or end of file
832 else
833 exit;
834 end if;
836 -- Store the data in the Info record in the Unit.Table
838 Unit.Increment_Last;
839 Unit.Table (Unit.Last) := Info;
841 -- If this is not the first unit from the file, calculate
842 -- the length of the previous unit as difference of the offsets
844 if Unit.Last > First_Unit then
845 Unit.Table (Unit.Last - 1).Length :=
846 Info.Offset - Unit.Table (Unit.Last - 1).Offset;
847 end if;
849 -- If not in compilation mode combine current unit with any
850 -- preceding configuration pragmas.
852 if not Compilation_Mode
853 and then Unit.Last > First_Unit
854 and then Unit.Table (Unit.Last - 1).Kind = Config_Pragmas
855 then
856 Info.Start_Line := Unit.Table (Unit.Last - 1).Start_Line;
857 Info.Offset := Unit.Table (Unit.Last - 1).Offset;
859 -- Delete the configuration pragma entry
861 Unit.Table (Unit.Last - 1) := Info;
862 Unit.Decrement_Last;
863 end if;
865 -- If in compilation mode, and previous entry is the initial
866 -- entry for the file and is for configuration pragmas, then
867 -- they are to be appended to every unit in the file.
869 if Compilation_Mode
870 and then Unit.Last = First_Unit + 1
871 and then Unit.Table (First_Unit).Kind = Config_Pragmas
872 then
873 Bufferg :=
874 Get_Config_Pragmas
875 (Unit.Table (Unit.Last - 1).Chop_File, First_Unit);
876 Unit.Table (Unit.Last - 1) := Info;
877 Unit.Decrement_Last;
878 end if;
880 Unit.Table (Unit.Last).Bufferg := Bufferg;
882 -- If in compilation mode, and this is not the first item,
883 -- combine configuration pragmas with previous unit, which
884 -- will cause an error message to be generated when the unit
885 -- is compiled.
887 if Compilation_Mode
888 and then Unit.Last > First_Unit
889 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
890 then
891 Unit.Decrement_Last;
892 end if;
894 <<Continue>>
895 null;
897 end loop;
899 -- Find out if the loop was exited prematurely because of
900 -- an error or if the EOF marker was found.
902 if Source (Parse_Ptr) /= EOF then
903 Error_Msg
904 (File.Table (Chop_File).Name.all & ": error parsing offset info");
905 return;
906 end if;
908 -- Handle case of a chop file consisting only of config pragmas
910 if Unit.Last = First_Unit
911 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
912 then
913 -- In compilation mode, we append such a file to gnat.adc
915 if Compilation_Mode then
916 Write_Config_File (Unit.Table (Unit.Last).Chop_File, First_Unit);
917 Unit.Decrement_Last;
919 -- In default (non-compilation) mode, this is invalid
921 else
922 Error_Msg
923 (File.Table (Chop_File).Name.all &
924 ": no units found (only pragmas)");
925 Unit.Decrement_Last;
926 end if;
927 end if;
929 -- Handle case of a chop file ending with config pragmas. This can
930 -- happen only in default non-compilation mode, since in compilation
931 -- mode such configuration pragmas are part of the preceding unit.
932 -- We simply concatenate such pragmas to the previous file which
933 -- will cause a compilation error, which is appropriate.
935 if Unit.Last > First_Unit
936 and then Unit.Table (Unit.Last).Kind = Config_Pragmas
937 then
938 Unit.Decrement_Last;
939 end if;
940 end Parse_Offset_Info;
942 -----------------
943 -- Parse_Token --
944 -----------------
946 procedure Parse_Token
947 (Source : access String;
948 Ptr : in out Positive;
949 Token_Ptr : out Positive)
951 In_Quotes : Boolean := False;
953 begin
954 -- Skip separators
956 while Source (Ptr) = ' ' or Source (Ptr) = ',' loop
957 Ptr := Ptr + 1;
958 end loop;
960 Token_Ptr := Ptr;
962 -- Find end-of-token
964 while (In_Quotes or else not (Source (Ptr) = ' ' or Source (Ptr) = ','))
965 and then Source (Ptr) >= ' '
966 loop
967 if Source (Ptr) = '"' then
968 In_Quotes := not In_Quotes;
969 end if;
971 Ptr := Ptr + 1;
972 end loop;
973 end Parse_Token;
975 ---------------
976 -- Read_File --
977 ---------------
979 procedure Read_File
980 (FD : File_Descriptor;
981 Contents : out String_Access;
982 Success : out Boolean)
984 Length : constant File_Offset := File_Offset (File_Length (FD));
985 -- Include room for EOF char
986 Buffer : constant String_Access := new String (1 .. Length + 1);
988 This_Read : Integer;
989 Read_Ptr : File_Offset := 1;
991 begin
993 loop
994 This_Read := Read (FD,
995 A => Buffer (Read_Ptr)'Address,
996 N => Length + 1 - Read_Ptr);
997 Read_Ptr := Read_Ptr + Integer'Max (This_Read, 0);
998 exit when This_Read <= 0;
999 end loop;
1001 Buffer (Read_Ptr) := EOF;
1002 Contents := new String (1 .. Read_Ptr);
1003 Contents.all := Buffer (1 .. Read_Ptr);
1005 -- Things aren't simple on VMS due to the plethora of file types
1006 -- and organizations. It seems clear that there shouldn't be more
1007 -- bytes read than are contained in the file though.
1009 if Hostparm.OpenVMS then
1010 Success := Read_Ptr <= Length + 1;
1011 else
1012 Success := Read_Ptr = Length + 1;
1013 end if;
1014 end Read_File;
1016 ----------------------------
1017 -- Report_Duplicate_Units --
1018 ----------------------------
1020 function Report_Duplicate_Units return Boolean is
1021 US : SUnit_Num;
1022 U : Unit_Num;
1024 Duplicates : Boolean := False;
1026 begin
1027 US := 1;
1028 while US < SUnit_Num (Unit.Last) loop
1029 U := Sorted_Units.Table (US);
1031 if Is_Duplicated (US) then
1032 Duplicates := True;
1034 -- Move to last two versions of duplicated file to make it clearer
1035 -- to understand which file is retained in case of overwriting.
1037 while US + 1 < SUnit_Num (Unit.Last) loop
1038 exit when not Is_Duplicated (US + 1);
1039 US := US + 1;
1040 end loop;
1042 U := Sorted_Units.Table (US);
1044 if Overwrite_Files then
1045 Warning_Msg (Unit.Table (U).File_Name.all
1046 & " is duplicated (all but last will be skipped)");
1048 elsif Unit.Table (U).Chop_File =
1049 Unit.Table (Sorted_Units.Table (US + 1)).Chop_File
1050 then
1051 Error_Msg (Unit.Table (U).File_Name.all
1052 & " is duplicated in "
1053 & File.Table (Unit.Table (U).Chop_File).Name.all);
1055 else
1056 Error_Msg (Unit.Table (U).File_Name.all
1057 & " in "
1058 & File.Table (Unit.Table (U).Chop_File).Name.all
1059 & " is duplicated in "
1060 & File.Table
1061 (Unit.Table
1062 (Sorted_Units.Table (US + 1)).Chop_File).Name.all);
1063 end if;
1064 end if;
1066 US := US + 1;
1067 end loop;
1069 if Duplicates and not Overwrite_Files then
1070 if Hostparm.OpenVMS then
1071 Put_Line
1072 ("use /OVERWRITE to overwrite files and keep last version");
1073 else
1074 Put_Line ("use -w to overwrite files and keep last version");
1075 end if;
1076 end if;
1078 return Duplicates;
1079 end Report_Duplicate_Units;
1081 --------------------
1082 -- Scan_Arguments --
1083 --------------------
1085 function Scan_Arguments return Boolean is
1086 Kset : Boolean := False;
1087 -- Set true if -k switch found
1089 begin
1090 Initialize_Option_Scan;
1092 -- Scan options first
1094 loop
1095 case Getopt ("c gnat? h k? p q r v w x -GCC=!") is
1096 when ASCII.NUL =>
1097 exit;
1099 when '-' =>
1100 Gcc := new String'(Parameter);
1101 Gcc_Set := True;
1103 when 'c' =>
1104 Compilation_Mode := True;
1106 when 'g' =>
1107 Gnat_Args :=
1108 new Argument_List'(Gnat_Args.all &
1109 new String'("-gnat" & Parameter));
1111 when 'h' =>
1112 Usage;
1113 raise Terminate_Program;
1115 when 'k' =>
1116 declare
1117 Param : String_Access := new String'(Parameter);
1119 begin
1120 if Param.all /= "" then
1121 for J in Param'Range loop
1122 if Param (J) not in '0' .. '9' then
1123 if Hostparm.OpenVMS then
1124 Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn" &
1125 " requires numeric parameter");
1126 else
1127 Error_Msg ("-k# requires numeric parameter");
1128 end if;
1130 return False;
1131 end if;
1132 end loop;
1134 else
1135 if Hostparm.OpenVMS then
1136 Param := new String'("39");
1137 else
1138 Param := new String'("8");
1139 end if;
1140 end if;
1142 Gnat_Args :=
1143 new Argument_List'(Gnat_Args.all &
1144 new String'("-gnatk" & Param.all));
1145 Kset := True;
1146 end;
1148 when 'p' =>
1149 Preserve_Mode := True;
1151 when 'q' =>
1152 Quiet_Mode := True;
1154 when 'r' =>
1155 Source_References := True;
1157 when 'v' =>
1158 Verbose_Mode := True;
1160 -- Why is following written to standard error. Most other
1161 -- tools write to standard output ???
1163 Put (Standard_Error, "GNATCHOP ");
1164 Put_Line (Standard_Error, Gnatvsn.Gnat_Version_String);
1165 Put_Line
1166 (Standard_Error,
1167 "Copyright 1998-2005, Ada Core Technologies Inc.");
1169 when 'w' =>
1170 Overwrite_Files := True;
1172 when 'x' =>
1173 Exit_On_Error := True;
1175 when others =>
1176 null;
1177 end case;
1178 end loop;
1180 if not Kset and then Maximum_File_Name_Length > 0 then
1182 -- If this system has restricted filename lengths, tell gnat1
1183 -- about them, removing the leading blank from the image string.
1185 Gnat_Args :=
1186 new Argument_List'(Gnat_Args.all
1187 & new String'("-gnatk"
1188 & Maximum_File_Name_Length_String
1189 (Maximum_File_Name_Length_String'First + 1
1190 .. Maximum_File_Name_Length_String'Last)));
1191 end if;
1193 -- Scan file names
1195 loop
1196 declare
1197 S : constant String := Get_Argument (Do_Expansion => True);
1199 begin
1200 exit when S = "";
1201 File.Increment_Last;
1202 File.Table (File.Last).Name := new String'(S);
1203 File.Table (File.Last).SR_Name := null;
1204 end;
1205 end loop;
1207 -- Case of more than one file where last file is a directory
1209 if File.Last > 1
1210 and then Is_Directory (File.Table (File.Last).Name.all)
1211 then
1212 Directory := File.Table (File.Last).Name;
1213 File.Decrement_Last;
1215 -- Make sure Directory is terminated with a directory separator,
1216 -- so we can generate the output by just appending a filename.
1218 if Directory (Directory'Last) /= Directory_Separator
1219 and then Directory (Directory'Last) /= '/'
1220 then
1221 Directory := new String'(Directory.all & Directory_Separator);
1222 end if;
1224 -- At least one filename must be given
1226 elsif File.Last = 0 then
1227 Usage;
1228 return False;
1230 -- No directory given, set directory to null, so that we can just
1231 -- concatenate the directory name to the file name unconditionally.
1233 else
1234 Directory := new String'("");
1235 end if;
1237 -- Finally check all filename arguments
1239 for File_Num in 1 .. File.Last loop
1240 declare
1241 F : constant String := File.Table (File_Num).Name.all;
1243 begin
1245 if Is_Directory (F) then
1246 Error_Msg (F & " is a directory, cannot be chopped");
1247 return False;
1249 elsif not Is_Regular_File (F) then
1250 Error_Msg (F & " not found");
1251 return False;
1252 end if;
1253 end;
1254 end loop;
1256 return True;
1258 exception
1259 when Invalid_Switch =>
1260 Error_Msg ("invalid switch " & Full_Switch);
1261 return False;
1263 when Invalid_Parameter =>
1264 if Hostparm.OpenVMS then
1265 Error_Msg ("/FILE_NAME_MAX_LENGTH=nnn qualifier" &
1266 " requires numeric parameter");
1267 else
1268 Error_Msg ("-k switch requires numeric parameter");
1269 end if;
1271 return False;
1273 end Scan_Arguments;
1275 ----------------
1276 -- Sort_Units --
1277 ----------------
1279 procedure Sort_Units is
1281 procedure Move (From : Natural; To : Natural);
1282 -- Procedure used to sort the unit list
1283 -- Unit.Table (To) := Unit_List (From); used by sort
1285 function Lt (Left, Right : Natural) return Boolean;
1286 -- Compares Left and Right units based on file name (first),
1287 -- Chop_File (second) and Offset (third). This ordering is
1288 -- important to keep the last version in case of duplicate files.
1290 package Unit_Sort is new GNAT.Heap_Sort_G (Move, Lt);
1291 -- Used for sorting on filename to detect duplicates
1293 --------
1294 -- Lt --
1295 --------
1297 function Lt (Left, Right : Natural) return Boolean is
1298 L : Unit_Info renames
1299 Unit.Table (Sorted_Units.Table (SUnit_Num (Left)));
1301 R : Unit_Info renames
1302 Unit.Table (Sorted_Units.Table (SUnit_Num (Right)));
1304 begin
1305 return L.File_Name.all < R.File_Name.all
1306 or else (L.File_Name.all = R.File_Name.all
1307 and then (L.Chop_File < R.Chop_File
1308 or else (L.Chop_File = R.Chop_File
1309 and then L.Offset < R.Offset)));
1310 end Lt;
1312 ----------
1313 -- Move --
1314 ----------
1316 procedure Move (From : Natural; To : Natural) is
1317 begin
1318 Sorted_Units.Table (SUnit_Num (To)) :=
1319 Sorted_Units.Table (SUnit_Num (From));
1320 end Move;
1322 -- Start of processing for Sort_Units
1324 begin
1325 Sorted_Units.Set_Last (SUnit_Num (Unit.Last));
1327 for J in 1 .. Unit.Last loop
1328 Sorted_Units.Table (SUnit_Num (J)) := J;
1329 end loop;
1331 -- Sort Unit.Table, using Sorted_Units.Table (0) as scratch
1333 Unit_Sort.Sort (Natural (Unit.Last));
1335 -- Set the Sorted_Index fields in the unit tables
1337 for J in 1 .. SUnit_Num (Unit.Last) loop
1338 Unit.Table (Sorted_Units.Table (J)).Sorted_Index := J;
1339 end loop;
1340 end Sort_Units;
1342 -----------
1343 -- Usage --
1344 -----------
1346 procedure Usage is
1347 begin
1348 Put_Line
1349 ("Usage: gnatchop [-c] [-h] [-k#] " &
1350 "[-r] [-p] [-q] [-v] [-w] [-x] [--GCC=xx] file [file ...] [dir]");
1352 New_Line;
1353 Put_Line
1354 (" -c compilation mode, configuration pragmas " &
1355 "follow RM rules");
1357 Put_Line
1358 (" -gnatxxx passes the -gnatxxx switch to gnat parser");
1360 Put_Line
1361 (" -h help: output this usage information");
1363 Put_Line
1364 (" -k# krunch file names of generated files to " &
1365 "no more than # characters");
1367 Put_Line
1368 (" -k krunch file names of generated files to " &
1369 "no more than 8 characters");
1371 Put_Line
1372 (" -p preserve time stamp, output files will " &
1373 "have same stamp as input");
1375 Put_Line
1376 (" -q quiet mode, no output of generated file " &
1377 "names");
1379 Put_Line
1380 (" -r generate Source_Reference pragmas refer" &
1381 "encing original source file");
1383 Put_Line
1384 (" -v verbose mode, output version and generat" &
1385 "ed commands");
1387 Put_Line
1388 (" -w overwrite existing filenames");
1390 Put_Line
1391 (" -x exit on error");
1393 Put_Line
1394 (" --GCC=xx specify the path of the gnat parser to be used");
1396 New_Line;
1397 Put_Line
1398 (" file... list of source files to be chopped");
1400 Put_Line
1401 (" dir directory location for split files (defa" &
1402 "ult = current directory)");
1403 end Usage;
1405 -----------------
1406 -- Warning_Msg --
1407 -----------------
1409 procedure Warning_Msg (Message : String) is
1410 begin
1411 Warning_Count := Warning_Count + 1;
1412 Put_Line (Standard_Error, "warning: " & Message);
1413 end Warning_Msg;
1415 -------------------------
1416 -- Write_Chopped_Files --
1417 -------------------------
1419 function Write_Chopped_Files (Input : File_Num) return Boolean is
1420 Name : aliased constant String :=
1421 File.Table (Input).Name.all & ASCII.Nul;
1422 FD : File_Descriptor;
1423 Buffer : String_Access;
1424 Success : Boolean;
1425 TS_Time : OS_Time;
1427 begin
1428 FD := Open_Read (Name'Address, Binary);
1429 TS_Time := File_Time_Stamp (FD);
1431 if FD = Invalid_FD then
1432 Error_Msg ("cannot open " & File.Table (Input).Name.all);
1433 return False;
1434 end if;
1436 Read_File (FD, Buffer, Success);
1438 if not Success then
1439 Error_Msg ("cannot read " & File.Table (Input).Name.all);
1440 Close (FD);
1441 return False;
1442 end if;
1444 if not Quiet_Mode then
1445 Put_Line ("splitting " & File.Table (Input).Name.all & " into:");
1446 end if;
1448 -- Only chop those units that come from this file
1450 for Num in 1 .. Unit.Last loop
1451 if Unit.Table (Num).Chop_File = Input then
1452 Write_Unit (Buffer, Num, TS_Time, Success);
1453 exit when not Success;
1454 end if;
1455 end loop;
1457 Close (FD);
1458 return Success;
1460 end Write_Chopped_Files;
1462 -----------------------
1463 -- Write_Config_File --
1464 -----------------------
1466 procedure Write_Config_File (Input : File_Num; U : Unit_Num) is
1467 FD : File_Descriptor;
1468 Name : aliased constant String := "gnat.adc" & ASCII.NUL;
1469 Buffer : String_Access;
1470 Success : Boolean;
1471 Append : Boolean;
1472 Buffera : String_Access;
1473 Bufferl : Natural;
1475 begin
1476 Write_gnat_adc := True;
1477 FD := Open_Read_Write (Name'Address, Binary);
1479 if FD = Invalid_FD then
1480 FD := Create_File (Name'Address, Binary);
1481 Append := False;
1483 if not Quiet_Mode then
1484 Put_Line ("writing configuration pragmas from " &
1485 File.Table (Input).Name.all & " to gnat.adc");
1486 end if;
1488 else
1489 Append := True;
1491 if not Quiet_Mode then
1492 Put_Line
1493 ("appending configuration pragmas from " &
1494 File.Table (Input).Name.all & " to gnat.adc");
1495 end if;
1496 end if;
1498 Success := FD /= Invalid_FD;
1500 if not Success then
1501 Error_Msg ("cannot create gnat.adc");
1502 return;
1503 end if;
1505 -- In append mode, acquire existing gnat.adc file
1507 if Append then
1508 Read_File (FD, Buffera, Success);
1510 if not Success then
1511 Error_Msg ("cannot read gnat.adc");
1512 return;
1513 end if;
1515 -- Find location of EOF byte if any to exclude from append
1517 Bufferl := 1;
1518 while Bufferl <= Buffera'Last
1519 and then Buffera (Bufferl) /= EOF
1520 loop
1521 Bufferl := Bufferl + 1;
1522 end loop;
1524 Bufferl := Bufferl - 1;
1525 Close (FD);
1527 -- Write existing gnat.adc to new gnat.adc file
1529 FD := Create_File (Name'Address, Binary);
1530 Success := Write (FD, Buffera (1)'Address, Bufferl) = Bufferl;
1532 if not Success then
1533 Error_Msg ("error writing gnat.adc");
1534 return;
1535 end if;
1536 end if;
1538 Buffer := Get_Config_Pragmas (Input, U);
1540 if Buffer /= null then
1541 Success := Write (FD, Buffer.all'Address, Buffer'Length) =
1542 Buffer'Length;
1544 if not Success then
1545 Error_Msg ("disk full writing gnat.adc");
1546 return;
1547 end if;
1548 end if;
1550 Close (FD);
1551 end Write_Config_File;
1553 -----------------------------------
1554 -- Write_Source_Reference_Pragma --
1555 -----------------------------------
1557 procedure Write_Source_Reference_Pragma
1558 (Info : Unit_Info;
1559 Line : Line_Num;
1560 FD : File_Descriptor;
1561 EOL : EOL_String;
1562 Success : in out Boolean)
1564 FTE : File_Entry renames File.Table (Info.Chop_File);
1565 Nam : String_Access;
1567 begin
1568 if Success and Source_References and not Info.SR_Present then
1569 if FTE.SR_Name /= null then
1570 Nam := FTE.SR_Name;
1571 else
1572 Nam := FTE.Name;
1573 end if;
1575 declare
1576 Reference : aliased String :=
1577 "pragma Source_Reference (000000, """
1578 & Nam.all & """);" & EOL.Str;
1580 Pos : Positive := Reference'First;
1581 Lin : Line_Num := Line;
1583 begin
1584 while Reference (Pos + 1) /= ',' loop
1585 Pos := Pos + 1;
1586 end loop;
1588 while Reference (Pos) = '0' loop
1589 Reference (Pos) := Character'Val
1590 (Character'Pos ('0') + Lin mod 10);
1591 Lin := Lin / 10;
1592 Pos := Pos - 1;
1593 end loop;
1595 -- Assume there are enough zeroes for any program length
1597 pragma Assert (Lin = 0);
1599 Success :=
1600 Write (FD, Reference'Address, Reference'Length)
1601 = Reference'Length;
1602 end;
1603 end if;
1604 end Write_Source_Reference_Pragma;
1606 ----------------
1607 -- Write_Unit --
1608 ----------------
1610 procedure Write_Unit
1611 (Source : access String;
1612 Num : Unit_Num;
1613 TS_Time : OS_Time;
1614 Success : out Boolean)
1616 Info : Unit_Info renames Unit.Table (Num);
1617 FD : File_Descriptor;
1618 Name : aliased constant String := Info.File_Name.all & ASCII.NUL;
1619 Length : File_Offset;
1620 EOL : constant EOL_String :=
1621 Get_EOL (Source, Source'First + Info.Offset);
1623 begin
1624 -- Skip duplicated files
1626 if Is_Duplicated (Info.Sorted_Index) then
1627 Put_Line (" " & Info.File_Name.all & " skipped");
1628 Success := Overwrite_Files;
1629 return;
1630 end if;
1632 if Overwrite_Files then
1633 FD := Create_File (Name'Address, Binary);
1634 else
1635 FD := Create_New_File (Name'Address, Binary);
1636 end if;
1638 Success := FD /= Invalid_FD;
1640 if not Success then
1641 Error_Msg ("cannot create " & Info.File_Name.all);
1642 return;
1643 end if;
1645 -- A length of 0 indicates that the rest of the file belongs to
1646 -- this unit. The actual length must be calculated now. Take into
1647 -- account that the last character (EOF) must not be written.
1649 if Info.Length = 0 then
1650 Length := Source'Last - (Source'First + Info.Offset);
1651 else
1652 Length := Info.Length;
1653 end if;
1655 -- Prepend configuration pragmas if necessary
1657 if Success and then Info.Bufferg /= null then
1658 Write_Source_Reference_Pragma (Info, 1, FD, EOL, Success);
1659 Success :=
1660 Write (FD, Info.Bufferg.all'Address, Info.Bufferg'Length) =
1661 Info.Bufferg'Length;
1662 end if;
1664 Write_Source_Reference_Pragma (Info, Info.Start_Line, FD, EOL, Success);
1666 if Success then
1667 Success := Write (FD, Source (Source'First + Info.Offset)'Address,
1668 Length) = Length;
1669 end if;
1671 if not Success then
1672 Error_Msg ("disk full writing " & Info.File_Name.all);
1673 return;
1674 end if;
1676 if not Quiet_Mode then
1677 Put_Line (" " & Info.File_Name.all);
1678 end if;
1680 Close (FD);
1682 if Preserve_Mode then
1683 File_Time_Stamp (Name'Address, TS_Time);
1684 end if;
1686 end Write_Unit;
1688 -- Start of processing for gnatchop
1690 begin
1691 -- Add the directory where gnatchop is invoked in front of the
1692 -- path, if gnatchop is invoked with directory information.
1693 -- Only do this if the platform is not VMS, where the notion of path
1694 -- does not really exist.
1696 if not Hostparm.OpenVMS then
1697 declare
1698 Command : constant String := Command_Name;
1700 begin
1701 for Index in reverse Command'Range loop
1702 if Command (Index) = Directory_Separator then
1703 declare
1704 Absolute_Dir : constant String :=
1705 Normalize_Pathname
1706 (Command (Command'First .. Index));
1708 PATH : constant String :=
1709 Absolute_Dir &
1710 Path_Separator &
1711 Getenv ("PATH").all;
1713 begin
1714 Setenv ("PATH", PATH);
1715 end;
1717 exit;
1718 end if;
1719 end loop;
1720 end;
1721 end if;
1723 -- Process command line options and initialize global variables
1725 if not Scan_Arguments then
1726 Set_Exit_Status (Failure);
1727 return;
1728 end if;
1730 -- Check presence of required executables
1732 Gnat_Cmd := Locate_Executable (Gcc.all, not Gcc_Set);
1734 if Gnat_Cmd = null then
1735 goto No_Files_Written;
1736 end if;
1738 -- First parse all files and read offset information
1740 for Num in 1 .. File.Last loop
1741 if not Parse_File (Num) then
1742 goto No_Files_Written;
1743 end if;
1744 end loop;
1746 -- Check if any units have been found (assumes non-empty Unit.Table)
1748 if Unit.Last = 0 then
1749 if not Write_gnat_adc then
1750 Error_Msg ("no compilation units found", Warning => True);
1751 end if;
1753 goto No_Files_Written;
1754 end if;
1756 Sort_Units;
1758 -- Check if any duplicate files would be created. If so, emit
1759 -- a warning if Overwrite_Files is true, otherwise generate an error.
1761 if Report_Duplicate_Units and then not Overwrite_Files then
1762 goto No_Files_Written;
1763 end if;
1765 -- Check if any files exist, if so do not write anything
1766 -- Because all files have been parsed and checked already,
1767 -- there won't be any duplicates
1769 if not Overwrite_Files and then Files_Exist then
1770 goto No_Files_Written;
1771 end if;
1773 -- After this point, all source files are read in succession
1774 -- and chopped into their destination files.
1776 -- As the Source_File_Name pragmas are handled as logical file 0,
1777 -- write it first.
1779 for F in 1 .. File.Last loop
1780 if not Write_Chopped_Files (F) then
1781 Set_Exit_Status (Failure);
1782 return;
1783 end if;
1784 end loop;
1786 if Warning_Count > 0 then
1787 declare
1788 Warnings_Msg : constant String := Warning_Count'Img & " warning(s)";
1789 begin
1790 Error_Msg (Warnings_Msg (2 .. Warnings_Msg'Last), Warning => True);
1791 end;
1792 end if;
1794 return;
1796 <<No_Files_Written>>
1798 -- Special error exit for all situations where no files have
1799 -- been written.
1801 if not Write_gnat_adc then
1802 Error_Msg ("no source files written", Warning => True);
1803 end if;
1805 return;
1807 exception
1808 when Terminate_Program =>
1809 null;
1811 end Gnatchop;