1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Debug; use Debug;
28 with Osint; use Osint;
30 with Prepcomp; use Prepcomp;
31 with Validsw; use Validsw;
32 with Sem_Warn; use Sem_Warn;
33 with Stylesw; use Stylesw;
36 with System.WCh_Con; use System.WCh_Con;
38 package body Switch.C is
40 RTS_Specified : String_Access := null;
41 -- Used to detect multiple use of --RTS= flag
43 function Switch_Subsequently_Cancelled
46 Arg_Rank : Positive) return Boolean;
47 -- This function is called from Scan_Front_End_Switches. It determines if
48 -- the switch currently being scanned is followed by a switch of the form
49 -- "-gnat-" & C, where C is the argument. If so, then True is returned,
50 -- and Scan_Front_End_Switches will cancel the effect of the switch. If
51 -- no such switch is found, False is returned.
53 -----------------------------
54 -- Scan_Front_End_Switches --
55 -----------------------------
57 procedure Scan_Front_End_Switches
58 (Switch_Chars : String;
62 First_Switch : Boolean := True;
63 -- False for all but first switch
65 Max : constant Natural := Switch_Chars'Last;
70 Store_Switch : Boolean;
71 -- For -gnatxx switches, the normal processing, signalled by this flag
72 -- being set to True, is to store the switch on exit from the case
73 -- statement, the switch stored is -gnat followed by the characters
74 -- from First_Char to Ptr-1. For cases like -gnaty, where the switch
75 -- is stored in separate pieces, this flag is set to False, and the
76 -- appropriate calls to Store_Compilation_Switch are made from within
79 First_Char : Positive;
80 -- Marks start of switch to be stored
83 Ptr := Switch_Chars'First;
85 -- Skip past the initial character (must be the switch character)
93 -- Handle switches that do not start with -gnat
96 or else Switch_Chars (Ptr .. Ptr + 3) /= "gnat"
98 -- There are two front-end switches that do not start with -gnat:
101 if Switch_Chars (Ptr) = 'I' then
103 -- Set flag Search_Directory_Present if switch is "-I" only:
104 -- the directory will be the next argument.
107 Search_Directory_Present := True;
113 -- Find out whether this is a -I- or regular -Ixxx switch
115 -- Note: -I switches are not recorded in the ALI file, since the
116 -- meaning of the program depends on the source files compiled,
117 -- not where they came from.
119 if Ptr = Max and then Switch_Chars (Ptr) = '-' then
120 Look_In_Primary_Dir := False;
122 Add_Src_Search_Dir (Switch_Chars (Ptr .. Max));
125 -- Processing of the --RTS switch. --RTS may have been modified by
126 -- gcc into -fRTS (for GCC targets).
129 and then (Switch_Chars (Ptr .. Ptr + 3) = "fRTS"
131 Switch_Chars (Ptr .. Ptr + 3) = "-RTS")
136 or else Switch_Chars (Ptr + 3) /= '='
138 Osint.Fail ("missing path for --RTS");
140 -- Check that this is the first time --RTS is specified or if
141 -- it is not the first time, the same path has been specified.
143 if RTS_Specified = null then
144 RTS_Specified := new String'(Switch_Chars (Ptr + 4 .. Max));
147 RTS_Specified.all /= Switch_Chars (Ptr + 4 .. Max)
149 Osint.Fail ("--RTS cannot be specified multiple times");
152 -- Valid --RTS switch
154 Opt.No_Stdinc := True;
155 Opt.RTS_Switch := True;
159 (Switch_Chars (Ptr + 4 .. Max), Include);
163 (Switch_Chars (Ptr + 4 .. Max), Objects);
165 if RTS_Src_Path_Name /= null
166 and then RTS_Lib_Path_Name /= null
168 -- Store the -fRTS switch (Note: Store_Compilation_Switch
169 -- changes -fRTS back into --RTS for the actual output).
171 Store_Compilation_Switch (Switch_Chars);
173 elsif RTS_Src_Path_Name = null
174 and then RTS_Lib_Path_Name = null
176 Osint.Fail ("RTS path not valid: missing " &
177 "adainclude and adalib directories");
179 elsif RTS_Src_Path_Name = null then
180 Osint.Fail ("RTS path not valid: missing " &
181 "adainclude directory");
183 elsif RTS_Lib_Path_Name = null then
184 Osint.Fail ("RTS path not valid: missing " &
189 -- There are no other switches not starting with -gnat
192 Bad_Switch (Switch_Chars);
195 -- Case of switch starting with -gnat
200 -- Loop to scan through switches given in switch string
202 while Ptr <= Max loop
204 Store_Switch := True;
206 C := Switch_Chars (Ptr);
212 Assertions_Enabled := True;
213 Debug_Pragmas_Enabled := True;
215 -- Processing for A switch
219 Config_File := False;
221 -- Processing for b switch
225 Brief_Output := True;
227 -- Processing for B switch
231 Assume_No_Invalid_Values := True;
233 -- Processing for c switch
236 if not First_Switch then
238 ("-gnatc must be first if combined with other switches");
242 Operating_Mode := Check_Semantics;
244 -- Processing for C switch
248 CodePeer_Mode := True;
250 -- Processing for d switch
253 Store_Switch := False;
256 -- Note: for the debug switch, the remaining characters in this
257 -- switch field must all be debug flags, since all valid switch
258 -- characters are also valid debug characters.
260 -- Loop to scan out debug flags
264 C := Switch_Chars (Ptr);
265 exit when C = ASCII.NUL or else C = '/' or else C = '-';
267 if C in '1' .. '9' or else
268 C in 'a' .. 'z' or else
272 Set_Dotted_Debug_Flag (C);
273 Store_Compilation_Switch ("-gnatd." & C);
276 Store_Compilation_Switch ("-gnatd" & C);
283 Bad_Switch ("-gnatd." & Switch_Chars (Ptr .. Max));
285 Bad_Switch ("-gnatd" & Switch_Chars (Ptr .. Max));
291 -- Processing for D switch
296 -- Scan optional integer line limit value
298 if Nat_Present (Switch_Chars, Max, Ptr) then
299 Scan_Nat (Switch_Chars, Max, Ptr, Sprint_Line_Limit, 'D');
300 Sprint_Line_Limit := Nat'Max (Sprint_Line_Limit, 40);
303 -- Note: -gnatD also sets -gnatx (to turn off cross-reference
304 -- generation in the ali file) since otherwise this generation
305 -- gets confused by the "wrong" Sloc values put in the tree.
307 Debug_Generated_Code := True;
308 Xref_Active := False;
309 Set_Debug_Flag ('g');
311 -- -gnate? (extended switches)
316 -- The -gnate? switches are all double character switches
317 -- so we must always have a character after the e.
320 Bad_Switch ("-gnate");
323 case Switch_Chars (Ptr) is
325 -- -gnatea (initial delimiter of explicit switches)
327 -- All switches that come before -gnatea have been added by
328 -- the GCC driver and are not stored in the ALI file.
329 -- See also -gnatez below.
332 Store_Switch := False;
333 Enable_Switch_Storing;
336 -- -gnatec (configuration pragmas)
339 Store_Switch := False;
342 -- There may be an equal sign between -gnatec and
343 -- the path name of the config file.
345 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
350 Bad_Switch ("-gnatec");
354 Config_File_Name : constant String_Access :=
356 (Switch_Chars (Ptr .. Max));
359 if Config_File_Names = null then
361 new String_List'(1 => Config_File_Name);
365 New_Names : constant String_List_Access :=
368 Config_File_Names'Length + 1);
371 for Index in Config_File_Names'Range loop
373 Config_File_Names (Index);
374 Config_File_Names (Index) := null;
377 New_Names (New_Names'Last) := Config_File_Name;
378 Free (Config_File_Names);
379 Config_File_Names := New_Names;
386 -- -gnateC switch (CodePeer SCIL generation)
388 -- Not enabled for now, keep it for later???
389 -- use -gnatd.I only for now
393 -- Generate_SCIL := True;
395 -- -gnateD switch (preprocessing symbol definition)
398 Store_Switch := False;
402 Bad_Switch ("-gnateD");
405 Add_Symbol_Definition (Switch_Chars (Ptr .. Max));
409 Store_Compilation_Switch
410 ("-gnateD" & Switch_Chars (Ptr .. Max));
413 -- -gnatef (full source path for brief error messages)
416 Store_Switch := False;
418 Full_Path_Name_For_Brief_Errors := True;
420 -- -gnateG (save preprocessor output)
423 Generate_Processed_File := True;
426 -- -gnateI (index of unit in multi-unit source)
430 Scan_Pos (Switch_Chars, Max, Ptr, Multiple_Unit_Index, C);
432 -- -gnatem (mapping file)
435 Store_Switch := False;
438 -- There may be an equal sign between -gnatem and
439 -- the path name of the mapping file.
441 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
446 Bad_Switch ("-gnatem");
450 new String'(Switch_Chars (Ptr .. Max));
453 -- -gnatep (preprocessing data file)
456 Store_Switch := False;
459 -- There may be an equal sign between -gnatep and
460 -- the path name of the mapping file.
462 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
467 Bad_Switch ("-gnatep");
470 Preprocessing_Data_File :=
471 new String'(Switch_Chars (Ptr .. Max));
473 -- Store the switch, normalizing to -gnatep=
475 Store_Compilation_Switch
476 ("-gnatep=" & Preprocessing_Data_File.all);
480 -- -gnatez (final delimiter of explicit switches)
482 -- All switches that come after -gnatez have been added by
483 -- the GCC driver and are not stored in the ALI file. See
484 -- also -gnatea above.
487 Store_Switch := False;
488 Disable_Switch_Storing;
491 -- -gnateS (generate SCO information)
493 -- Include Source Coverage Obligation information in ALI
494 -- files for the benefit of source coverage analysis tools
498 Generate_SCO := True;
501 -- All other -gnate? switches are unassigned
504 Bad_Switch ("-gnate" & Switch_Chars (Ptr .. Max));
507 -- -gnatE (dynamic elaboration checks)
511 Dynamic_Elaboration_Checks := True;
513 -- -gnatf (full error messages)
517 All_Errors_Mode := True;
519 -- Processing for F switch
523 External_Name_Exp_Casing := Uppercase;
524 External_Name_Imp_Casing := Uppercase;
526 -- Processing for g switch
531 Identifier_Character_Set := 'n';
532 System_Extend_Unit := Empty;
533 Warning_Mode := Treat_As_Error;
535 -- Set Ada 2005 mode explicitly. We don't want to rely on the
536 -- implicit setting here, since for example, we want
537 -- Preelaborate_05 treated as Preelaborate
539 Ada_Version := Ada_05;
540 Ada_Version_Explicit := Ada_Version;
542 -- Set default warnings and style checks for -gnatg
544 Set_GNAT_Mode_Warnings;
545 Set_GNAT_Style_Check_Options;
547 -- Processing for G switch
551 Print_Generated_Code := True;
553 -- Scan optional integer line limit value
555 if Nat_Present (Switch_Chars, Max, Ptr) then
556 Scan_Nat (Switch_Chars, Max, Ptr, Sprint_Line_Limit, 'G');
557 Sprint_Line_Limit := Nat'Max (Sprint_Line_Limit, 40);
560 -- Processing for h switch
564 Usage_Requested := True;
566 -- Processing for H switch
572 -- Processing for i switch
576 Bad_Switch ("-gnati");
580 C := Switch_Chars (Ptr);
590 Identifier_Character_Set := C;
594 Bad_Switch ("-gnati" & Switch_Chars (Ptr .. Max));
597 -- Processing for I switch
601 Ignore_Rep_Clauses := True;
603 -- Processing for j switch
607 Scan_Nat (Switch_Chars, Max, Ptr, Error_Msg_Line_Length, C);
609 -- Processing for k switch
614 (Switch_Chars, Max, Ptr, Maximum_File_Name_Length, C);
616 -- Processing for l switch
622 -- There may be an equal sign between -gnatl and a file name
624 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
626 Osint.Fail ("file name for -gnatl= is null");
628 Opt.Full_List_File_Name :=
629 new String'(Switch_Chars (Ptr + 1 .. Max));
634 -- Processing for L switch
638 Dump_Source_Text := True;
640 -- Processing for m switch
644 Scan_Nat (Switch_Chars, Max, Ptr, Maximum_Messages, C);
646 -- Processing for n switch
650 Inline_Active := True;
652 -- Processing for N switch
656 Inline_Active := True;
657 Front_End_Inlining := True;
659 -- Processing for o switch
663 Suppress_Options (Overflow_Check) := False;
664 Opt.Enable_Overflow_Checks := True;
666 -- Processing for O switch
669 Store_Switch := False;
671 Output_File_Name_Present := True;
673 -- Processing for p switch
678 -- Skip processing if cancelled by subsequent -gnat-p
680 if Switch_Subsequently_Cancelled ("p", Args, Arg_Rank) then
681 Store_Switch := False;
684 -- Set all specific options as well as All_Checks in the
685 -- Suppress_Options array, excluding Elaboration_Check,
686 -- since this is treated specially because we do not want
687 -- -gnatp to disable static elaboration processing.
689 for J in Suppress_Options'Range loop
690 if J /= Elaboration_Check then
691 Suppress_Options (J) := True;
695 Validity_Checks_On := False;
696 Opt.Suppress_Checks := True;
697 Opt.Enable_Overflow_Checks := False;
700 -- Processing for P switch
704 Polling_Required := True;
706 -- Processing for q switch
710 Try_Semantics := True;
712 -- Processing for Q switch
716 Force_ALI_Tree_File := True;
717 Try_Semantics := True;
719 -- Processing for r switch
723 Treat_Restrictions_As_Warnings := True;
725 -- Processing for R switch
728 Back_Annotate_Rep_Info := True;
729 List_Representation_Info := 1;
732 while Ptr <= Max loop
733 C := Switch_Chars (Ptr);
735 if C in '1' .. '3' then
736 List_Representation_Info :=
737 Character'Pos (C) - Character'Pos ('0');
739 elsif Switch_Chars (Ptr) = 's' then
740 List_Representation_Info_To_File := True;
742 elsif Switch_Chars (Ptr) = 'm' then
743 List_Representation_Info_Mechanisms := True;
746 Bad_Switch ("-gnatR" & Switch_Chars (Ptr .. Max));
752 -- Processing for s switch
755 if not First_Switch then
757 ("-gnats must be first if combined with other switches");
761 Operating_Mode := Check_Syntax;
763 -- Processing for S switch
766 Print_Standard := True;
769 -- Processing for t switch
774 Back_Annotate_Rep_Info := True;
776 -- Processing for T switch
780 Scan_Pos (Switch_Chars, Max, Ptr, Table_Factor, C);
782 -- Processing for u switch
788 -- Processing for U switch
792 Unique_Error_Tag := True;
794 -- Processing for v switch
798 Verbose_Mode := True;
800 -- Processing for V switch
803 Store_Switch := False;
807 Bad_Switch ("-gnatV");
814 Set_Validity_Check_Options
815 (Switch_Chars (Ptr .. Max), OK, Ptr);
818 Bad_Switch ("-gnatV" & Switch_Chars (Ptr .. Max));
821 for Index in First_Char + 1 .. Max loop
822 Store_Compilation_Switch
823 ("-gnatV" & Switch_Chars (Index));
830 -- Processing for w switch
833 Store_Switch := False;
837 Bad_Switch ("-gnatw");
840 while Ptr <= Max loop
841 C := Switch_Chars (Ptr);
843 -- Case of dot switch
845 if C = '.' and then Ptr < Max then
847 C := Switch_Chars (Ptr);
849 if Set_Dot_Warning_Switch (C) then
850 Store_Compilation_Switch ("-gnatw." & C);
852 Bad_Switch ("-gnatw." & Switch_Chars (Ptr .. Max));
855 -- Normal case, no dot
858 if Set_Warning_Switch (C) then
859 Store_Compilation_Switch ("-gnatw" & C);
861 Bad_Switch ("-gnatw" & Switch_Chars (Ptr .. Max));
870 -- Processing for W switch
876 Bad_Switch ("-gnatW");
880 Wide_Character_Encoding_Method :=
881 Get_WC_Encoding_Method (Switch_Chars (Ptr));
883 when Constraint_Error =>
884 Bad_Switch ("-gnatW" & Switch_Chars (Ptr .. Max));
887 Wide_Character_Encoding_Method_Specified := True;
889 Upper_Half_Encoding :=
890 Wide_Character_Encoding_Method in
891 WC_Upper_Half_Encoding_Method;
895 -- Processing for x switch
899 Xref_Active := False;
901 -- Processing for X switch
905 Extensions_Allowed := True;
907 -- Processing for y switch
913 Set_Default_Style_Check_Options;
916 Store_Switch := False;
922 Set_Style_Check_Options
923 (Switch_Chars (Ptr .. Max), OK, Ptr);
927 ("bad -gnaty switch (" &
928 Style_Msg_Buf (1 .. Style_Msg_Len) & ')');
931 Ptr := First_Char + 1;
932 while Ptr <= Max loop
933 if Switch_Chars (Ptr) = 'M' then
938 or else Switch_Chars (Ptr) not in '0' .. '9';
941 Store_Compilation_Switch
942 ("-gnaty" & Switch_Chars (First_Char .. Ptr - 1));
945 Store_Compilation_Switch
946 ("-gnaty" & Switch_Chars (Ptr));
953 -- Processing for z switch
957 -- -gnatz must be the first and only switch in Switch_Chars,
958 -- and is a two-letter switch.
960 if Ptr /= Switch_Chars'First + 5
961 or else (Max - Ptr + 1) > 2
964 ("-gnatz* may not be combined with other switches");
968 Bad_Switch ("-gnatz");
973 -- Only one occurrence of -gnat* is permitted
975 if Distribution_Stub_Mode = No_Stubs then
976 case Switch_Chars (Ptr) is
978 Distribution_Stub_Mode := Generate_Receiver_Stub_Body;
981 Distribution_Stub_Mode := Generate_Caller_Stub_Body;
984 Bad_Switch ("-gnatz" & Switch_Chars (Ptr .. Max));
990 Osint.Fail ("only one -gnatz* switch allowed");
993 -- Processing for Z switch
998 ("-gnatZ is no longer supported: consider using --RTS=zcx");
1000 -- Processing for 83 switch
1004 Bad_Switch ("-gnat8");
1009 if Switch_Chars (Ptr) /= '3' then
1010 Bad_Switch ("-gnat8" & Switch_Chars (Ptr .. Max));
1013 Ada_Version := Ada_83;
1014 Ada_Version_Explicit := Ada_Version;
1017 -- Processing for 95 switch
1021 Bad_Switch ("-gnat9");
1026 if Switch_Chars (Ptr) /= '5' then
1027 Bad_Switch ("-gnat9" & Switch_Chars (Ptr .. Max));
1030 Ada_Version := Ada_95;
1031 Ada_Version_Explicit := Ada_Version;
1034 -- Processing for 05 switch
1038 Bad_Switch ("-gnat0");
1043 if Switch_Chars (Ptr) /= '5' then
1044 Bad_Switch ("-gnat0" & Switch_Chars (Ptr .. Max));
1047 Ada_Version := Ada_05;
1048 Ada_Version_Explicit := Ada_Version;
1051 -- Switch cancellation, currently only -gnat-p is allowed.
1052 -- All we do here is the error checking, since the actual
1053 -- processing for switch cancellation is done by calls to
1054 -- Switch_Subsequently_Cancelled at the appropriate point.
1058 -- Simple ignore -gnat-p
1060 if Switch_Chars = "-gnat-p" then
1063 -- Any other occurrence of minus is ignored. This is for
1064 -- maximum compatibility with previous version which ignored
1065 -- all occurrences of minus.
1068 Store_Switch := False;
1072 -- We ignore '/' in switches, this is historical, still needed???
1075 Store_Switch := False;
1077 -- Anything else is an error (illegal switch character)
1080 Bad_Switch ("-gnat" & Switch_Chars (Ptr .. Max));
1083 if Store_Switch then
1084 Store_Compilation_Switch
1085 ("-gnat" & Switch_Chars (First_Char .. Ptr - 1));
1088 First_Switch := False;
1091 end Scan_Front_End_Switches;
1093 -----------------------------------
1094 -- Switch_Subsequently_Cancelled --
1095 -----------------------------------
1097 function Switch_Subsequently_Cancelled
1099 Args : Argument_List;
1100 Arg_Rank : Positive) return Boolean
1102 use type System.Strings.String_Access;
1104 -- Loop through arguments following the current one
1106 for Arg in Arg_Rank + 1 .. Args'Last loop
1107 if Args (Arg).all = "-gnat-" & C then
1112 -- No match found, not cancelled
1115 end Switch_Subsequently_Cancelled;