Merge from mainline
[official-gcc.git] / gcc / ada / gprep.adb
blob3a3e2825962c413dd95cfddae8363535ba4e6ed8
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G P R E P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2002-2006, Free Software Foundation, 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 Csets;
28 with Err_Vars; use Err_Vars;
29 with Errutil;
30 with Gnatvsn; use Gnatvsn;
31 with Namet; use Namet;
32 with Opt;
33 with Osint; use Osint;
34 with Output; use Output;
35 with Prep; use Prep;
36 with Scng;
37 with Sinput.C;
38 with Snames;
39 with Stringt; use Stringt;
40 with Types; use Types;
42 with Ada.Text_IO; use Ada.Text_IO;
43 with GNAT.Case_Util; use GNAT.Case_Util;
44 with GNAT.Command_Line;
45 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
46 with GNAT.OS_Lib; use GNAT.OS_Lib;
48 package body GPrep is
50 Copyright_Displayed : Boolean := False;
51 -- Used to prevent multiple displays of the copyright notice
53 ------------------------
54 -- Argument Line Data --
55 ------------------------
57 Infile_Name : Name_Id := No_Name;
58 Outfile_Name : Name_Id := No_Name;
59 Deffile_Name : Name_Id := No_Name;
61 Output_Directory : Name_Id := No_Name;
62 -- Used when the specified output is an existing directory
64 Input_Directory : Name_Id := No_Name;
65 -- Used when the specified input and output are existing directories
67 Source_Ref_Pragma : Boolean := False;
68 -- Record command line options (set if -r switch set)
70 Text_Outfile : aliased Ada.Text_IO.File_Type;
71 Outfile : constant File_Access := Text_Outfile'Access;
73 File_Name_Buffer_Initial_Size : constant := 50;
74 File_Name_Buffer : String_Access :=
75 new String (1 .. File_Name_Buffer_Initial_Size);
76 -- A buffer to build output file names from input file names
78 -----------------
79 -- Subprograms --
80 -----------------
82 procedure Display_Copyright;
83 -- Display the copyright notice
85 procedure Obsolescent_Check (S : Source_Ptr);
86 -- Null procedure, needed by instantiation of Scng below
88 procedure Post_Scan;
89 -- Null procedure, needed by instantiation of Scng below
91 package Scanner is new Scng
92 (Post_Scan,
93 Errutil.Error_Msg,
94 Errutil.Error_Msg_S,
95 Errutil.Error_Msg_SC,
96 Errutil.Error_Msg_SP,
97 Obsolescent_Check,
98 Errutil.Style);
99 -- The scanner for the preprocessor
101 function Is_ASCII_Letter (C : Character) return Boolean;
102 -- True if C is in 'a' .. 'z' or in 'A' .. 'Z'
104 procedure Double_File_Name_Buffer;
105 -- Double the size of the file name buffer
107 procedure Preprocess_Infile_Name;
108 -- When the specified output is a directory, preprocess the infile name
109 -- for symbol substitution, to get the output file name.
111 procedure Process_Files;
112 -- Process the single input file or all the files in the directory tree
113 -- rooted at the input directory.
115 procedure Process_Command_Line_Symbol_Definition (S : String);
116 -- Process a -D switch on the command line
118 procedure Put_Char_To_Outfile (C : Character);
119 -- Output one character to the output file. Used to initialize the
120 -- preprocessor.
122 procedure New_EOL_To_Outfile;
123 -- Output a new line to the output file. Used to initialize the
124 -- preprocessor.
126 procedure Scan_Command_Line;
127 -- Scan the switches and the file names
129 procedure Usage;
130 -- Display the usage
132 -----------------------
133 -- Display_Copyright --
134 -----------------------
136 procedure Display_Copyright is
137 begin
138 if not Copyright_Displayed then
139 Write_Line ("GNAT Preprocessor " & Gnatvsn.Gnat_Version_String);
140 Write_Line ("Copyright 1996-" &
141 Current_Year &
142 ", Free Software Foundation, Inc.");
143 Copyright_Displayed := True;
144 end if;
145 end Display_Copyright;
147 -----------------------------
148 -- Double_File_Name_Buffer --
149 -----------------------------
151 procedure Double_File_Name_Buffer is
152 New_Buffer : constant String_Access :=
153 new String (1 .. 2 * File_Name_Buffer'Length);
154 begin
155 New_Buffer (File_Name_Buffer'Range) := File_Name_Buffer.all;
156 Free (File_Name_Buffer);
157 File_Name_Buffer := New_Buffer;
158 end Double_File_Name_Buffer;
160 --------------
161 -- Gnatprep --
162 --------------
164 procedure Gnatprep is
165 begin
166 -- Do some initializations (order is important here!)
168 Csets.Initialize;
169 Namet.Initialize;
170 Snames.Initialize;
171 Stringt.Initialize;
173 -- Initialize the preprocessor
175 Prep.Initialize
176 (Error_Msg => Errutil.Error_Msg'Access,
177 Scan => Scanner.Scan'Access,
178 Set_Ignore_Errors => Errutil.Set_Ignore_Errors'Access,
179 Put_Char => Put_Char_To_Outfile'Access,
180 New_EOL => New_EOL_To_Outfile'Access);
182 -- Set the scanner characteristics for the preprocessor
184 Scanner.Set_Special_Character ('#');
185 Scanner.Set_Special_Character ('$');
186 Scanner.Set_End_Of_Line_As_Token (True);
188 -- Initialize the mapping table of symbols to values
190 Prep.Symbol_Table.Init (Prep.Mapping);
192 -- Parse the switches and arguments
194 Scan_Command_Line;
196 if Opt.Verbose_Mode then
197 Display_Copyright;
198 end if;
200 -- Test we had all the arguments needed
202 if Infile_Name = No_Name then
204 -- No input file specified, just output the usage and exit
206 Usage;
207 return;
209 elsif Outfile_Name = No_Name then
211 -- No output file specified, just output the usage and exit
213 Usage;
214 return;
215 end if;
217 -- If a pragma Source_File_Name, we need to keep line numbers. So, if
218 -- the deleted lines are not put as comment, we must output them as
219 -- blank lines.
221 if Source_Ref_Pragma and (not Opt.Comment_Deleted_Lines) then
222 Opt.Blank_Deleted_Lines := True;
223 end if;
225 -- If we have a definition file, parse it
227 if Deffile_Name /= No_Name then
228 declare
229 Deffile : Source_File_Index;
231 begin
232 Errutil.Initialize;
233 Deffile := Sinput.C.Load_File (Get_Name_String (Deffile_Name));
235 -- Set Main_Source_File to the definition file for the benefit of
236 -- Errutil.Finalize.
238 Sinput.Main_Source_File := Deffile;
240 if Deffile = No_Source_File then
241 Fail ("unable to find definition file """,
242 Get_Name_String (Deffile_Name),
243 """");
244 end if;
246 Scanner.Initialize_Scanner (Deffile);
248 Prep.Parse_Def_File;
249 end;
250 end if;
252 -- If there are errors in the definition file, output them and exit
254 if Total_Errors_Detected > 0 then
255 Errutil.Finalize (Source_Type => "definition");
256 Fail ("errors in definition file """,
257 Get_Name_String (Deffile_Name), """");
258 end if;
260 -- If -s switch was specified, print a sorted list of symbol names and
261 -- values, if any.
263 if Opt.List_Preprocessing_Symbols then
264 Prep.List_Symbols (Foreword => "");
265 end if;
267 Output_Directory := No_Name;
268 Input_Directory := No_Name;
270 -- Check if the specified output is an existing directory
272 if Is_Directory (Get_Name_String (Outfile_Name)) then
273 Output_Directory := Outfile_Name;
275 -- As the output is an existing directory, check if the input too
276 -- is a directory.
278 if Is_Directory (Get_Name_String (Infile_Name)) then
279 Input_Directory := Infile_Name;
280 end if;
281 end if;
283 -- And process the single input or the files in the directory tree
284 -- rooted at the input directory.
286 Process_Files;
287 end Gnatprep;
289 ---------------------
290 -- Is_ASCII_Letter --
291 ---------------------
293 function Is_ASCII_Letter (C : Character) return Boolean is
294 begin
295 return C in 'A' .. 'Z' or else C in 'a' .. 'z';
296 end Is_ASCII_Letter;
298 ------------------------
299 -- New_EOL_To_Outfile --
300 ------------------------
302 procedure New_EOL_To_Outfile is
303 begin
304 New_Line (Outfile.all);
305 end New_EOL_To_Outfile;
307 -----------------------
308 -- Obsolescent_Check --
309 -----------------------
311 procedure Obsolescent_Check (S : Source_Ptr) is
312 pragma Warnings (Off, S);
313 begin
314 null;
315 end Obsolescent_Check;
317 ---------------
318 -- Post_Scan --
319 ---------------
321 procedure Post_Scan is
322 begin
323 null;
324 end Post_Scan;
326 ----------------------------
327 -- Preprocess_Infile_Name --
328 ----------------------------
330 procedure Preprocess_Infile_Name is
331 Len : Natural;
332 First : Positive;
333 Last : Natural;
334 Symbol : Name_Id;
335 Data : Symbol_Data;
337 begin
338 -- Initialize the buffer with the name of the input file
340 Get_Name_String (Infile_Name);
341 Len := Name_Len;
343 while File_Name_Buffer'Length < Len loop
344 Double_File_Name_Buffer;
345 end loop;
347 File_Name_Buffer (1 .. Len) := Name_Buffer (1 .. Len);
349 -- Look for possible symbols in the file name
351 First := 1;
352 while First < Len loop
354 -- A symbol starts with a dollar sign followed by a letter
356 if File_Name_Buffer (First) = '$' and then
357 Is_ASCII_Letter (File_Name_Buffer (First + 1))
358 then
359 Last := First + 1;
361 -- Find the last letter of the symbol
363 while Last < Len and then
364 Is_ASCII_Letter (File_Name_Buffer (Last + 1))
365 loop
366 Last := Last + 1;
367 end loop;
369 -- Get the symbol name id
371 Name_Len := Last - First;
372 Name_Buffer (1 .. Name_Len) :=
373 File_Name_Buffer (First + 1 .. Last);
374 To_Lower (Name_Buffer (1 .. Name_Len));
375 Symbol := Name_Find;
377 -- And look for this symbol name in the symbol table
379 for Index in 1 .. Symbol_Table.Last (Mapping) loop
380 Data := Mapping.Table (Index);
382 if Data.Symbol = Symbol then
384 -- We found the symbol. If its value is not a string,
385 -- replace the symbol in the file name with the value of
386 -- the symbol.
388 if not Data.Is_A_String then
389 String_To_Name_Buffer (Data.Value);
391 declare
392 Sym_Len : constant Positive := Last - First + 1;
393 Offset : constant Integer := Name_Len - Sym_Len;
394 New_Len : constant Natural := Len + Offset;
396 begin
397 while New_Len > File_Name_Buffer'Length loop
398 Double_File_Name_Buffer;
399 end loop;
401 File_Name_Buffer (Last + 1 + Offset .. New_Len) :=
402 File_Name_Buffer (Last + 1 .. Len);
403 Len := New_Len;
404 Last := Last + Offset;
405 File_Name_Buffer (First .. Last) :=
406 Name_Buffer (1 .. Name_Len);
407 end;
408 end if;
410 exit;
411 end if;
412 end loop;
414 -- Skip over the symbol name or its value: we are not checking
415 -- for another symbol name in the value.
417 First := Last + 1;
419 else
420 First := First + 1;
421 end if;
422 end loop;
424 -- We now have the output file name in the buffer. Get the output
425 -- path and put it in Outfile_Name.
427 Get_Name_String (Output_Directory);
428 Add_Char_To_Name_Buffer (Directory_Separator);
429 Add_Str_To_Name_Buffer (File_Name_Buffer (1 .. Len));
430 Outfile_Name := Name_Find;
431 end Preprocess_Infile_Name;
433 --------------------------------------------
434 -- Process_Command_Line_Symbol_Definition --
435 --------------------------------------------
437 procedure Process_Command_Line_Symbol_Definition (S : String) is
438 Data : Symbol_Data;
439 Symbol : Symbol_Id;
441 begin
442 -- Check the symbol definition and get the symbol and its value.
443 -- Fail if symbol definition is illegal.
445 Check_Command_Line_Symbol_Definition (S, Data);
447 Symbol := Index_Of (Data.Symbol);
449 -- If symbol does not alrady exist, create a new entry in the mapping
450 -- table.
452 if Symbol = No_Symbol then
453 Symbol_Table.Increment_Last (Mapping);
454 Symbol := Symbol_Table.Last (Mapping);
455 end if;
457 Mapping.Table (Symbol) := Data;
458 end Process_Command_Line_Symbol_Definition;
460 -------------------
461 -- Process_Files --
462 -------------------
464 procedure Process_Files is
466 procedure Process_One_File;
467 -- Process input file Infile_Name and put the result in file
468 -- Outfile_Name.
470 procedure Recursive_Process (In_Dir : String; Out_Dir : String);
471 -- Process recursively files in In_Dir. Results go to Out_Dir
473 ----------------------
474 -- Process_One_File --
475 ----------------------
477 procedure Process_One_File is
478 Infile : Source_File_Index;
480 begin
481 -- Create the output file (fails if this does not work)
483 begin
484 Create (Text_Outfile, Out_File, Get_Name_String (Outfile_Name));
486 exception
487 when others =>
488 Fail
489 ("unable to create output file """,
490 Get_Name_String (Outfile_Name), """");
491 end;
493 -- Load the input file
495 Infile := Sinput.C.Load_File (Get_Name_String (Infile_Name));
497 if Infile = No_Source_File then
498 Fail ("unable to find input file """,
499 Get_Name_String (Infile_Name), """");
500 end if;
502 -- Set Main_Source_File to the input file for the benefit of
503 -- Errutil.Finalize.
505 Sinput.Main_Source_File := Infile;
507 Scanner.Initialize_Scanner (Infile);
509 -- Output the SFN pragma if asked to
511 if Source_Ref_Pragma then
512 Put_Line (Outfile.all, "pragma Source_Reference (1, """ &
513 Get_Name_String (Sinput.File_Name (Infile)) &
514 """);");
515 end if;
517 -- Preprocess the input file
519 Prep.Preprocess;
521 -- In verbose mode, if there is no error, report it
523 if Opt.Verbose_Mode and then Err_Vars.Total_Errors_Detected = 0 then
524 Errutil.Finalize (Source_Type => "input");
525 end if;
527 -- If we had some errors, delete the output file, and report them
529 if Err_Vars.Total_Errors_Detected > 0 then
530 if Outfile /= Standard_Output then
531 Delete (Text_Outfile);
532 end if;
534 Errutil.Finalize (Source_Type => "input");
536 OS_Exit (0);
538 -- Otherwise, close the output file, and we are done
540 elsif Outfile /= Standard_Output then
541 Close (Text_Outfile);
542 end if;
543 end Process_One_File;
545 -----------------------
546 -- Recursive_Process --
547 -----------------------
549 procedure Recursive_Process (In_Dir : String; Out_Dir : String) is
550 Dir_In : Dir_Type;
551 Name : String (1 .. 255);
552 Last : Natural;
553 In_Dir_Name : Name_Id;
554 Out_Dir_Name : Name_Id;
556 procedure Set_Directory_Names;
557 -- Establish or reestablish the current input and output directories
559 -------------------------
560 -- Set_Directory_Names --
561 -------------------------
563 procedure Set_Directory_Names is
564 begin
565 Input_Directory := In_Dir_Name;
566 Output_Directory := Out_Dir_Name;
567 end Set_Directory_Names;
569 -- Start of processing for Recursive_Process
571 begin
572 -- Open the current input directory
574 begin
575 Open (Dir_In, In_Dir);
577 exception
578 when Directory_Error =>
579 Fail ("could not read directory " & In_Dir);
580 end;
582 -- Set the new input and output directory names
584 Name_Len := In_Dir'Length;
585 Name_Buffer (1 .. Name_Len) := In_Dir;
586 In_Dir_Name := Name_Find;
587 Name_Len := Out_Dir'Length;
588 Name_Buffer (1 .. Name_Len) := Out_Dir;
589 Out_Dir_Name := Name_Find;
591 Set_Directory_Names;
593 -- Traverse the input directory
594 loop
595 Read (Dir_In, Name, Last);
596 exit when Last = 0;
598 if Name (1 .. Last) /= "." and then Name (1 .. Last) /= ".." then
599 declare
600 Input : constant String :=
601 In_Dir & Directory_Separator & Name (1 .. Last);
602 Output : constant String :=
603 Out_Dir & Directory_Separator & Name (1 .. Last);
605 begin
606 -- If input is an ordinary file, process it
608 if Is_Regular_File (Input) then
609 -- First get the output file name
611 Name_Len := Last;
612 Name_Buffer (1 .. Name_Len) := Name (1 .. Last);
613 Infile_Name := Name_Find;
614 Preprocess_Infile_Name;
616 -- Set the input file name and process the file
618 Name_Len := Input'Length;
619 Name_Buffer (1 .. Name_Len) := Input;
620 Infile_Name := Name_Find;
621 Process_One_File;
623 elsif Is_Directory (Input) then
624 -- Input is a directory. If the corresponding output
625 -- directory does not already exist, create it.
627 if not Is_Directory (Output) then
628 begin
629 Make_Dir (Dir_Name => Output);
631 exception
632 when Directory_Error =>
633 Fail ("could not create directory """,
634 Output, """");
635 end;
636 end if;
638 -- And process this new input directory
640 Recursive_Process (Input, Output);
642 -- Reestablish the input and output directory names
643 -- that have been modified by the recursive call.
645 Set_Directory_Names;
646 end if;
647 end;
648 end if;
649 end loop;
650 end Recursive_Process;
652 -- Start of processing for Process_Files
654 begin
655 if Output_Directory = No_Name then
657 -- If the output is not a directory, fail if the input is
658 -- an existing directory, to avoid possible problems.
660 if Is_Directory (Get_Name_String (Infile_Name)) then
661 Fail ("input file """ & Get_Name_String (Infile_Name) &
662 """ is a directory");
663 end if;
665 -- Just process the single input file
667 Process_One_File;
669 elsif Input_Directory = No_Name then
671 -- Get the output file name from the input file name, and process
672 -- the single input file.
674 Preprocess_Infile_Name;
675 Process_One_File;
677 else
678 -- Recursively process files in the directory tree rooted at the
679 -- input directory.
681 Recursive_Process
682 (In_Dir => Get_Name_String (Input_Directory),
683 Out_Dir => Get_Name_String (Output_Directory));
684 end if;
685 end Process_Files;
687 -------------------------
688 -- Put_Char_To_Outfile --
689 -------------------------
691 procedure Put_Char_To_Outfile (C : Character) is
692 begin
693 Put (Outfile.all, C);
694 end Put_Char_To_Outfile;
696 -----------------------
697 -- Scan_Command_Line --
698 -----------------------
700 procedure Scan_Command_Line is
701 Switch : Character;
703 begin
704 -- Parse the switches
706 loop
707 begin
708 Switch := GNAT.Command_Line.Getopt ("D: b c C r s u v");
710 case Switch is
712 when ASCII.NUL =>
713 exit;
715 when 'D' =>
716 Process_Command_Line_Symbol_Definition
717 (S => GNAT.Command_Line.Parameter);
719 when 'b' =>
720 Opt.Blank_Deleted_Lines := True;
722 when 'c' =>
723 Opt.Comment_Deleted_Lines := True;
725 when 'C' =>
726 Opt.Replace_In_Comments := True;
728 when 'r' =>
729 Source_Ref_Pragma := True;
731 when 's' =>
732 Opt.List_Preprocessing_Symbols := True;
734 when 'u' =>
735 Opt.Undefined_Symbols_Are_False := True;
737 when 'v' =>
738 Opt.Verbose_Mode := True;
740 when others =>
741 Fail ("Invalid Switch: -" & Switch);
742 end case;
744 exception
745 when GNAT.Command_Line.Invalid_Switch =>
746 Write_Str ("Invalid Switch: -");
747 Write_Line (GNAT.Command_Line.Full_Switch);
748 Usage;
749 OS_Exit (1);
750 end;
751 end loop;
753 -- Get the file names
755 loop
756 declare
757 S : constant String := GNAT.Command_Line.Get_Argument;
759 begin
760 exit when S'Length = 0;
762 Name_Len := S'Length;
763 Name_Buffer (1 .. Name_Len) := S;
765 if Infile_Name = No_Name then
766 Infile_Name := Name_Find;
767 elsif Outfile_Name = No_Name then
768 Outfile_Name := Name_Find;
769 elsif Deffile_Name = No_Name then
770 Deffile_Name := Name_Find;
771 else
772 Fail ("too many arguments specifed");
773 end if;
774 end;
775 end loop;
776 end Scan_Command_Line;
778 -----------
779 -- Usage --
780 -----------
782 procedure Usage is
783 begin
784 Display_Copyright;
785 Write_Line ("Usage: gnatprep [-bcrsuv] [-Dsymbol=value] " &
786 "infile outfile [deffile]");
787 Write_Eol;
788 Write_Line (" infile Name of the input file");
789 Write_Line (" outfile Name of the output file");
790 Write_Line (" deffile Name of the definition file");
791 Write_Eol;
792 Write_Line ("gnatprep switches:");
793 Write_Line (" -b Replace preprocessor lines by blank lines");
794 Write_Line (" -c Keep preprocessor lines as comments");
795 Write_Line (" -C Do symbol replacements within comments");
796 Write_Line (" -D Associate symbol with value");
797 Write_Line (" -r Generate Source_Reference pragma");
798 Write_Line (" -s Print a sorted list of symbol names and values");
799 Write_Line (" -u Treat undefined symbols as FALSE");
800 Write_Line (" -v Verbose mode");
801 Write_Eol;
802 end Usage;
804 end GPrep;