* opts.c (finish_options): Remove duplicate sorry.
[official-gcc.git] / gcc / ada / prepcomp.adb
blob2da21df3c42a253bf83503b25b444bf7e8591565
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P R E P C O M P --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2003-2010, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Errout; use Errout;
27 with Lib.Writ; use Lib.Writ;
28 with Opt; use Opt;
29 with Osint; use Osint;
30 with Prep; use Prep;
31 with Scans; use Scans;
32 with Scn; use Scn;
33 with Sinput.L; use Sinput.L;
34 with Stringt; use Stringt;
35 with Table;
36 with Types; use Types;
38 package body Prepcomp is
40 No_Preprocessing : Boolean := True;
41 -- Set to False if there is at least one source that needs to be
42 -- preprocessed.
44 Source_Index_Of_Preproc_Data_File : Source_File_Index := No_Source_File;
46 -- The following variable should be a constant, but this is not possible
47 -- because its type GNAT.Dynamic_Tables.Instance has a component P of
48 -- uninitialized private type GNAT.Dynamic_Tables.Table_Private and there
49 -- are no exported values for this private type. Warnings are Off because
50 -- it is never assigned a value.
52 pragma Warnings (Off);
53 No_Mapping : Prep.Symbol_Table.Instance;
54 pragma Warnings (On);
56 type Preproc_Data is record
57 Mapping : Symbol_Table.Instance;
58 File_Name : File_Name_Type := No_File;
59 Deffile : String_Id := No_String;
60 Undef_False : Boolean := False;
61 Always_Blank : Boolean := False;
62 Comments : Boolean := False;
63 List_Symbols : Boolean := False;
64 Processed : Boolean := False;
65 end record;
66 -- Structure to keep the preprocessing data for a file name or for the
67 -- default (when Name_Id = No_Name).
69 No_Preproc_Data : constant Preproc_Data :=
70 (Mapping => No_Mapping,
71 File_Name => No_File,
72 Deffile => No_String,
73 Undef_False => False,
74 Always_Blank => False,
75 Comments => False,
76 List_Symbols => False,
77 Processed => False);
79 Default_Data : Preproc_Data := No_Preproc_Data;
80 -- The preprocessing data to be used when no specific preprocessing data
81 -- is specified for a source.
83 Default_Data_Defined : Boolean := False;
84 -- True if source for which no specific preprocessing is specified need to
85 -- be preprocess with the Default_Data.
87 Current_Data : Preproc_Data := No_Preproc_Data;
89 package Preproc_Data_Table is new Table.Table
90 (Table_Component_Type => Preproc_Data,
91 Table_Index_Type => Int,
92 Table_Low_Bound => 1,
93 Table_Initial => 5,
94 Table_Increment => 100,
95 Table_Name => "Prepcomp.Preproc_Data_Table");
96 -- Table to store the specific preprocessing data
98 Command_Line_Symbols : Symbol_Table.Instance;
99 -- A table to store symbol definitions specified on the command line with
100 -- -gnateD switches.
102 package Dependencies is new Table.Table
103 (Table_Component_Type => Source_File_Index,
104 Table_Index_Type => Int,
105 Table_Low_Bound => 1,
106 Table_Initial => 10,
107 Table_Increment => 100,
108 Table_Name => "Prepcomp.Dependencies");
109 -- Table to store the dependencies on preprocessing files
111 procedure Add_Command_Line_Symbols;
112 -- Add the command line symbol definitions, if any, to Prep.Mapping table
114 procedure Skip_To_End_Of_Line;
115 -- Ignore errors and scan up to the next end of line or the end of file
117 ------------------------------
118 -- Add_Command_Line_Symbols --
119 ------------------------------
121 procedure Add_Command_Line_Symbols is
122 Symbol_Id : Prep.Symbol_Id;
124 begin
125 for J in 1 .. Symbol_Table.Last (Command_Line_Symbols) loop
126 Symbol_Id := Prep.Index_Of (Command_Line_Symbols.Table (J).Symbol);
128 if Symbol_Id = No_Symbol then
129 Symbol_Table.Increment_Last (Prep.Mapping);
130 Symbol_Id := Symbol_Table.Last (Prep.Mapping);
131 end if;
133 Prep.Mapping.Table (Symbol_Id) := Command_Line_Symbols.Table (J);
134 end loop;
135 end Add_Command_Line_Symbols;
137 ----------------------
138 -- Add_Dependencies --
139 ----------------------
141 procedure Add_Dependencies is
142 begin
143 for Index in 1 .. Dependencies.Last loop
144 Add_Preprocessing_Dependency (Dependencies.Table (Index));
145 end loop;
146 end Add_Dependencies;
148 -------------------
149 -- Check_Symbols --
150 -------------------
152 procedure Check_Symbols is
153 begin
154 -- If there is at least one switch -gnateD specified
156 if Symbol_Table.Last (Command_Line_Symbols) >= 1 then
157 Current_Data := No_Preproc_Data;
158 No_Preprocessing := False;
159 Current_Data.Processed := True;
161 -- Start with an empty, initialized mapping table; use Prep.Mapping,
162 -- because Prep.Index_Of uses Prep.Mapping.
164 Prep.Mapping := No_Mapping;
165 Symbol_Table.Init (Prep.Mapping);
167 -- Add the command line symbols
169 Add_Command_Line_Symbols;
171 -- Put the resulting Prep.Mapping in Current_Data, and immediately
172 -- set Prep.Mapping to nil.
174 Current_Data.Mapping := Prep.Mapping;
175 Prep.Mapping := No_Mapping;
177 -- Set the default data
179 Default_Data := Current_Data;
180 Default_Data_Defined := True;
181 end if;
182 end Check_Symbols;
184 ------------------------------
185 -- Parse_Preprocessing_Data --
186 ------------------------------
188 procedure Parse_Preprocessing_Data_File (N : File_Name_Type) is
189 OK : Boolean := False;
190 Dash_Location : Source_Ptr;
191 Symbol_Data : Prep.Symbol_Data;
192 Symbol_Id : Prep.Symbol_Id;
193 T : constant Nat := Total_Errors_Detected;
195 begin
196 -- Load the preprocessing data file
198 Source_Index_Of_Preproc_Data_File := Load_Preprocessing_Data_File (N);
200 -- Fail if preprocessing data file cannot be found
202 if Source_Index_Of_Preproc_Data_File = No_Source_File then
203 Get_Name_String (N);
204 Fail ("preprocessing data file """
205 & Name_Buffer (1 .. Name_Len)
206 & """ not found");
207 end if;
209 -- Initialize scanner and set its behavior for processing a data file
211 Scn.Scanner.Initialize_Scanner (Source_Index_Of_Preproc_Data_File);
212 Scn.Scanner.Set_End_Of_Line_As_Token (True);
213 Scn.Scanner.Reset_Special_Characters;
215 For_Each_Line : loop
216 <<Scan_Line>>
217 Scan;
219 exit For_Each_Line when Token = Tok_EOF;
221 if Token = Tok_End_Of_Line then
222 goto Scan_Line;
223 end if;
225 -- Line is not empty
227 OK := False;
228 No_Preprocessing := False;
229 Current_Data := No_Preproc_Data;
231 case Token is
232 when Tok_Asterisk =>
234 -- Default data
236 if Default_Data_Defined then
237 Error_Msg
238 ("multiple default preprocessing data", Token_Ptr);
240 else
241 OK := True;
242 Default_Data_Defined := True;
243 end if;
245 when Tok_String_Literal =>
247 -- Specific data
249 String_To_Name_Buffer (String_Literal_Id);
250 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
251 Current_Data.File_Name := Name_Find;
252 OK := True;
254 for Index in 1 .. Preproc_Data_Table.Last loop
255 if Current_Data.File_Name =
256 Preproc_Data_Table.Table (Index).File_Name
257 then
258 Error_Msg_File_1 := Current_Data.File_Name;
259 Error_Msg
260 ("multiple preprocessing data for{", Token_Ptr);
261 OK := False;
262 exit;
263 end if;
264 end loop;
266 when others =>
267 Error_Msg ("`'*` or literal string expected", Token_Ptr);
268 end case;
270 -- If there is a problem, skip the line
272 if not OK then
273 Skip_To_End_Of_Line;
274 goto Scan_Line;
275 end if;
277 -- Scan past the * or the literal string
279 Scan;
281 -- A literal string in second position is a definition file
283 if Token = Tok_String_Literal then
284 Current_Data.Deffile := String_Literal_Id;
285 Current_Data.Processed := False;
286 Scan;
288 else
289 -- If there is no definition file, set Processed to True now
291 Current_Data.Processed := True;
292 end if;
294 -- Start with an empty, initialized mapping table; use Prep.Mapping,
295 -- because Prep.Index_Of uses Prep.Mapping.
297 Prep.Mapping := No_Mapping;
298 Symbol_Table.Init (Prep.Mapping);
300 -- Check the switches that may follow
302 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
303 if Token /= Tok_Minus then
304 Error_Msg -- CODEFIX
305 ("`'-` expected", Token_Ptr);
306 Skip_To_End_Of_Line;
307 goto Scan_Line;
308 end if;
310 -- Keep the location of the '-' for possible error reporting
312 Dash_Location := Token_Ptr;
314 -- Scan past the '-'
316 Scan;
317 OK := False;
318 Change_Reserved_Keyword_To_Symbol;
320 -- An identifier (or a reserved word converted to an
321 -- identifier) is expected and there must be no blank space
322 -- between the '-' and the identifier.
324 if Token = Tok_Identifier
325 and then Token_Ptr = Dash_Location + 1
326 then
327 Get_Name_String (Token_Name);
329 -- Check the character in the source, because the case is
330 -- significant.
332 case Sinput.Source (Token_Ptr) is
333 when 'u' =>
335 -- Undefined symbol are False
337 if Name_Len = 1 then
338 Current_Data.Undef_False := True;
339 OK := True;
340 end if;
342 when 'b' =>
344 -- Blank lines
346 if Name_Len = 1 then
347 Current_Data.Always_Blank := True;
348 OK := True;
349 end if;
351 when 'c' =>
353 -- Comment removed lines
355 if Name_Len = 1 then
356 Current_Data.Comments := True;
357 OK := True;
358 end if;
360 when 's' =>
362 -- List symbols
364 if Name_Len = 1 then
365 Current_Data.List_Symbols := True;
366 OK := True;
367 end if;
369 when 'D' =>
371 -- Symbol definition
373 OK := Name_Len > 1;
375 if OK then
377 -- A symbol must be an Ada identifier; it cannot start
378 -- with an underline or a digit.
380 if Name_Buffer (2) = '_'
381 or else Name_Buffer (2) in '0' .. '9'
382 then
383 Error_Msg ("symbol expected", Token_Ptr + 1);
384 Skip_To_End_Of_Line;
385 goto Scan_Line;
386 end if;
388 -- Get the name id of the symbol
390 Symbol_Data.On_The_Command_Line := True;
391 Name_Buffer (1 .. Name_Len - 1) :=
392 Name_Buffer (2 .. Name_Len);
393 Name_Len := Name_Len - 1;
394 Symbol_Data.Symbol := Name_Find;
396 if Name_Buffer (1 .. Name_Len) = "if"
397 or else Name_Buffer (1 .. Name_Len) = "else"
398 or else Name_Buffer (1 .. Name_Len) = "elsif"
399 or else Name_Buffer (1 .. Name_Len) = "end"
400 or else Name_Buffer (1 .. Name_Len) = "not"
401 or else Name_Buffer (1 .. Name_Len) = "and"
402 or else Name_Buffer (1 .. Name_Len) = "then"
403 then
404 Error_Msg ("symbol expected", Token_Ptr + 1);
405 Skip_To_End_Of_Line;
406 goto Scan_Line;
407 end if;
409 -- Get the name id of the original symbol, with
410 -- possibly capital letters.
412 Name_Len := Integer (Scan_Ptr - Token_Ptr - 1);
414 for J in 1 .. Name_Len loop
415 Name_Buffer (J) :=
416 Sinput.Source (Token_Ptr + Text_Ptr (J));
417 end loop;
419 Symbol_Data.Original := Name_Find;
421 -- Scan past D<symbol>
423 Scan;
425 if Token /= Tok_Equal then
426 Error_Msg -- CODEFIX
427 ("`=` expected", Token_Ptr);
428 Skip_To_End_Of_Line;
429 goto Scan_Line;
430 end if;
432 -- Scan past '='
434 Scan;
436 -- Here any reserved word is OK
438 Change_Reserved_Keyword_To_Symbol
439 (All_Keywords => True);
441 -- Value can be an identifier (or a reserved word)
442 -- or a literal string.
444 case Token is
445 when Tok_String_Literal =>
446 Symbol_Data.Is_A_String := True;
447 Symbol_Data.Value := String_Literal_Id;
449 when Tok_Identifier =>
450 Symbol_Data.Is_A_String := False;
451 Start_String;
453 for J in Token_Ptr .. Scan_Ptr - 1 loop
454 Store_String_Char (Sinput.Source (J));
455 end loop;
457 Symbol_Data.Value := End_String;
459 when others =>
460 Error_Msg
461 ("literal string or identifier expected",
462 Token_Ptr);
463 Skip_To_End_Of_Line;
464 goto Scan_Line;
465 end case;
467 -- If symbol already exists, replace old definition
468 -- by new one.
470 Symbol_Id := Prep.Index_Of (Symbol_Data.Symbol);
472 -- Otherwise, add a new entry in the table
474 if Symbol_Id = No_Symbol then
475 Symbol_Table.Increment_Last (Prep.Mapping);
476 Symbol_Id := Symbol_Table.Last (Mapping);
477 end if;
479 Prep.Mapping.Table (Symbol_Id) := Symbol_Data;
480 end if;
482 when others =>
483 null;
484 end case;
486 Scan;
487 end if;
489 if not OK then
490 Error_Msg ("invalid switch", Dash_Location);
491 Skip_To_End_Of_Line;
492 goto Scan_Line;
493 end if;
494 end loop;
496 -- Add the command line symbols, if any, possibly replacing symbols
497 -- just defined.
499 Add_Command_Line_Symbols;
501 -- Put the resulting Prep.Mapping in Current_Data, and immediately
502 -- set Prep.Mapping to nil.
504 Current_Data.Mapping := Prep.Mapping;
505 Prep.Mapping := No_Mapping;
507 -- Record Current_Data
509 if Current_Data.File_Name = No_File then
510 Default_Data := Current_Data;
512 else
513 Preproc_Data_Table.Increment_Last;
514 Preproc_Data_Table.Table (Preproc_Data_Table.Last) := Current_Data;
515 end if;
517 Current_Data := No_Preproc_Data;
518 end loop For_Each_Line;
520 Scn.Scanner.Set_End_Of_Line_As_Token (False);
522 -- Fail if there were errors in the preprocessing data file
524 if Total_Errors_Detected > T then
525 Errout.Finalize (Last_Call => True);
526 Errout.Output_Messages;
527 Fail ("errors found in preprocessing data file """
528 & Get_Name_String (N) & """");
529 end if;
531 -- Record the dependency on the preprocessor data file
533 Dependencies.Increment_Last;
534 Dependencies.Table (Dependencies.Last) :=
535 Source_Index_Of_Preproc_Data_File;
536 end Parse_Preprocessing_Data_File;
538 ---------------------------
539 -- Prepare_To_Preprocess --
540 ---------------------------
542 procedure Prepare_To_Preprocess
543 (Source : File_Name_Type;
544 Preprocessing_Needed : out Boolean)
546 Default : Boolean := False;
547 Index : Int := 0;
549 begin
550 -- By default, preprocessing is not needed
552 Preprocessing_Needed := False;
554 if No_Preprocessing then
555 return;
556 end if;
558 -- First, look for preprocessing data specific to the current source
560 for J in 1 .. Preproc_Data_Table.Last loop
561 if Preproc_Data_Table.Table (J).File_Name = Source then
562 Index := J;
563 Current_Data := Preproc_Data_Table.Table (J);
564 exit;
565 end if;
566 end loop;
568 -- If no specific preprocessing data, then take the default
570 if Index = 0 then
571 if Default_Data_Defined then
572 Current_Data := Default_Data;
573 Default := True;
575 else
576 -- If no default, then nothing to do
578 return;
579 end if;
580 end if;
582 -- Set the preprocessing flags according to the preprocessing data
584 if Current_Data.Comments and then not Current_Data.Always_Blank then
585 Comment_Deleted_Lines := True;
586 Blank_Deleted_Lines := False;
588 else
589 Comment_Deleted_Lines := False;
590 Blank_Deleted_Lines := True;
591 end if;
593 Undefined_Symbols_Are_False := Current_Data.Undef_False;
594 List_Preprocessing_Symbols := Current_Data.List_Symbols;
596 -- If not already done it, process the definition file
598 if Current_Data.Processed then
600 -- Set Prep.Mapping
602 Prep.Mapping := Current_Data.Mapping;
604 else
605 -- First put the mapping in Prep.Mapping, because Prep.Parse_Def_File
606 -- works on Prep.Mapping.
608 Prep.Mapping := Current_Data.Mapping;
610 String_To_Name_Buffer (Current_Data.Deffile);
612 declare
613 N : constant File_Name_Type := Name_Find;
614 Deffile : constant Source_File_Index :=
615 Load_Definition_File (N);
616 Add_Deffile : Boolean := True;
617 T : constant Nat := Total_Errors_Detected;
619 begin
620 if Deffile = No_Source_File then
621 Fail ("definition file """
622 & Get_Name_String (N)
623 & """ not found");
624 end if;
626 -- Initialize the preprocessor and set the characteristics of the
627 -- scanner for a definition file.
629 Prep.Setup_Hooks
630 (Error_Msg => Errout.Error_Msg'Access,
631 Scan => Scn.Scanner.Scan'Access,
632 Set_Ignore_Errors => Errout.Set_Ignore_Errors'Access,
633 Put_Char => null,
634 New_EOL => null);
636 Scn.Scanner.Set_End_Of_Line_As_Token (True);
637 Scn.Scanner.Reset_Special_Characters;
639 -- Initialize the scanner and process the definition file
641 Scn.Scanner.Initialize_Scanner (Deffile);
642 Prep.Parse_Def_File;
644 -- Reset the behaviour of the scanner to the default
646 Scn.Scanner.Set_End_Of_Line_As_Token (False);
648 -- Fail if errors were found while processing the definition file
650 if T /= Total_Errors_Detected then
651 Errout.Finalize (Last_Call => True);
652 Errout.Output_Messages;
653 Fail ("errors found in definition file """
654 & Get_Name_String (N)
655 & """");
656 end if;
658 for Index in 1 .. Dependencies.Last loop
659 if Dependencies.Table (Index) = Deffile then
660 Add_Deffile := False;
661 exit;
662 end if;
663 end loop;
665 if Add_Deffile then
666 Dependencies.Increment_Last;
667 Dependencies.Table (Dependencies.Last) := Deffile;
668 end if;
669 end;
671 -- Get back the mapping, indicate that the definition file is
672 -- processed and store back the preprocessing data.
674 Current_Data.Mapping := Prep.Mapping;
675 Current_Data.Processed := True;
677 if Default then
678 Default_Data := Current_Data;
680 else
681 Preproc_Data_Table.Table (Index) := Current_Data;
682 end if;
683 end if;
685 Preprocessing_Needed := True;
686 end Prepare_To_Preprocess;
688 ---------------------------------------------
689 -- Process_Command_Line_Symbol_Definitions --
690 ---------------------------------------------
692 procedure Process_Command_Line_Symbol_Definitions is
693 Symbol_Data : Prep.Symbol_Data;
694 Found : Boolean := False;
696 begin
697 Symbol_Table.Init (Command_Line_Symbols);
699 -- The command line definitions have been stored temporarily in
700 -- array Symbol_Definitions.
702 for Index in 1 .. Preprocessing_Symbol_Last loop
703 -- Check each symbol definition, fail immediately if syntax is not
704 -- correct.
706 Check_Command_Line_Symbol_Definition
707 (Definition => Preprocessing_Symbol_Defs (Index).all,
708 Data => Symbol_Data);
709 Found := False;
711 -- If there is already a definition for this symbol, replace the old
712 -- definition by this one.
714 for J in 1 .. Symbol_Table.Last (Command_Line_Symbols) loop
715 if Command_Line_Symbols.Table (J).Symbol = Symbol_Data.Symbol then
716 Command_Line_Symbols.Table (J) := Symbol_Data;
717 Found := True;
718 exit;
719 end if;
720 end loop;
722 -- Otherwise, create a new entry in the table
724 if not Found then
725 Symbol_Table.Increment_Last (Command_Line_Symbols);
726 Command_Line_Symbols.Table
727 (Symbol_Table.Last (Command_Line_Symbols)) := Symbol_Data;
728 end if;
729 end loop;
730 end Process_Command_Line_Symbol_Definitions;
732 -------------------------
733 -- Skip_To_End_Of_Line --
734 -------------------------
736 procedure Skip_To_End_Of_Line is
737 begin
738 Set_Ignore_Errors (To => True);
740 while Token /= Tok_End_Of_Line and then Token /= Tok_EOF loop
741 Scan;
742 end loop;
744 Set_Ignore_Errors (To => False);
745 end Skip_To_End_Of_Line;
747 end Prepcomp;