1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2010, 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
249 if not CodePeer_Mode then
250 CodePeer_Mode := True;
252 -- Suppress compiler warnings by default, since what we are
253 -- interested in here is what CodePeer can find out. Note
254 -- that if -gnatwxxx is specified after -gnatC on the
255 -- command line, we do not want to override this setting in
256 -- Adjust_Global_Switches, and assume that the user wants to
257 -- get both warnings from GNAT and CodePeer messages.
259 Warning_Mode := Suppress;
262 -- Processing for d switch
265 Store_Switch := False;
268 -- Note: for the debug switch, the remaining characters in this
269 -- switch field must all be debug flags, since all valid switch
270 -- characters are also valid debug characters.
272 -- Loop to scan out debug flags
276 C := Switch_Chars (Ptr);
277 exit when C = ASCII.NUL or else C = '/' or else C = '-';
279 if C in '1' .. '9' or else
280 C in 'a' .. 'z' or else
284 Set_Dotted_Debug_Flag (C);
285 Store_Compilation_Switch ("-gnatd." & C);
288 Store_Compilation_Switch ("-gnatd" & C);
295 Bad_Switch ("-gnatd." & Switch_Chars (Ptr .. Max));
297 Bad_Switch ("-gnatd" & Switch_Chars (Ptr .. Max));
303 -- Processing for D switch
308 -- Scan optional integer line limit value
310 if Nat_Present (Switch_Chars, Max, Ptr) then
311 Scan_Nat (Switch_Chars, Max, Ptr, Sprint_Line_Limit, 'D');
312 Sprint_Line_Limit := Nat'Max (Sprint_Line_Limit, 40);
315 -- Note: -gnatD also sets -gnatx (to turn off cross-reference
316 -- generation in the ali file) since otherwise this generation
317 -- gets confused by the "wrong" Sloc values put in the tree.
319 Debug_Generated_Code := True;
320 Xref_Active := False;
321 Set_Debug_Flag ('g');
323 -- -gnate? (extended switches)
328 -- The -gnate? switches are all double character switches
329 -- so we must always have a character after the e.
332 Bad_Switch ("-gnate");
335 case Switch_Chars (Ptr) is
337 -- -gnatea (initial delimiter of explicit switches)
339 -- All switches that come before -gnatea have been added by
340 -- the GCC driver and are not stored in the ALI file.
341 -- See also -gnatez below.
344 Store_Switch := False;
345 Enable_Switch_Storing;
348 -- -gnatec (configuration pragmas)
351 Store_Switch := False;
354 -- There may be an equal sign between -gnatec and
355 -- the path name of the config file.
357 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
362 Bad_Switch ("-gnatec");
366 Config_File_Name : constant String_Access :=
368 (Switch_Chars (Ptr .. Max));
371 if Config_File_Names = null then
373 new String_List'(1 => Config_File_Name);
377 New_Names : constant String_List_Access :=
380 Config_File_Names'Length + 1);
383 for Index in Config_File_Names'Range loop
385 Config_File_Names (Index);
386 Config_File_Names (Index) := null;
389 New_Names (New_Names'Last) := Config_File_Name;
390 Free (Config_File_Names);
391 Config_File_Names := New_Names;
398 -- -gnateC switch (CodePeer SCIL generation)
400 -- Not enabled for now, keep it for later???
401 -- use -gnatd.I only for now
405 -- Generate_SCIL := True;
407 -- -gnateD switch (preprocessing symbol definition)
410 Store_Switch := False;
414 Bad_Switch ("-gnateD");
417 Add_Symbol_Definition (Switch_Chars (Ptr .. Max));
421 Store_Compilation_Switch
422 ("-gnateD" & Switch_Chars (Ptr .. Max));
425 -- -gnateE (extra exception information)
428 Exception_Extra_Info := True;
431 -- -gnatef (full source path for brief error messages)
434 Store_Switch := False;
436 Full_Path_Name_For_Brief_Errors := True;
438 -- -gnateG (save preprocessor output)
441 Generate_Processed_File := True;
444 -- -gnateI (index of unit in multi-unit source)
448 Scan_Pos (Switch_Chars, Max, Ptr, Multiple_Unit_Index, C);
450 -- -gnatem (mapping file)
453 Store_Switch := False;
456 -- There may be an equal sign between -gnatem and
457 -- the path name of the mapping file.
459 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
464 Bad_Switch ("-gnatem");
468 new String'(Switch_Chars (Ptr .. Max));
471 -- -gnatep (preprocessing data file)
474 Store_Switch := False;
477 -- There may be an equal sign between -gnatep and
478 -- the path name of the mapping file.
480 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
485 Bad_Switch ("-gnatep");
488 Preprocessing_Data_File :=
489 new String'(Switch_Chars (Ptr .. Max));
491 -- Store the switch, normalizing to -gnatep=
493 Store_Compilation_Switch
494 ("-gnatep=" & Preprocessing_Data_File.all);
498 -- -gnateP (Treat pragma Pure/Preelaborate errs as warnings)
501 Treat_Categorization_Errors_As_Warnings := True;
503 -- -gnatez (final delimiter of explicit switches)
505 -- All switches that come after -gnatez have been added by
506 -- the GCC driver and are not stored in the ALI file. See
507 -- also -gnatea above.
510 Store_Switch := False;
511 Disable_Switch_Storing;
514 -- -gnateS (generate SCO information)
516 -- Include Source Coverage Obligation information in ALI
517 -- files for the benefit of source coverage analysis tools
521 Generate_SCO := True;
524 -- All other -gnate? switches are unassigned
527 Bad_Switch ("-gnate" & Switch_Chars (Ptr .. Max));
530 -- -gnatE (dynamic elaboration checks)
534 Dynamic_Elaboration_Checks := True;
536 -- -gnatf (full error messages)
540 All_Errors_Mode := True;
542 -- Processing for F switch
546 External_Name_Exp_Casing := Uppercase;
547 External_Name_Imp_Casing := Uppercase;
549 -- Processing for g switch
554 Identifier_Character_Set := 'n';
555 System_Extend_Unit := Empty;
556 Warning_Mode := Treat_As_Error;
558 -- Set Ada 2012 mode explicitly. We don't want to rely on the
559 -- implicit setting here, since for example, we want
560 -- Preelaborate_05 treated as Preelaborate
562 Ada_Version := Ada_2012;
563 Ada_Version_Explicit := Ada_Version;
565 -- Set default warnings and style checks for -gnatg
567 Set_GNAT_Mode_Warnings;
568 Set_GNAT_Style_Check_Options;
570 -- Other special modes set by -gnatg
572 Treat_Categorization_Errors_As_Warnings := True;
574 -- Processing for G switch
578 Print_Generated_Code := True;
580 -- Scan optional integer line limit value
582 if Nat_Present (Switch_Chars, Max, Ptr) then
583 Scan_Nat (Switch_Chars, Max, Ptr, Sprint_Line_Limit, 'G');
584 Sprint_Line_Limit := Nat'Max (Sprint_Line_Limit, 40);
587 -- Processing for h switch
591 Usage_Requested := True;
593 -- Processing for H switch
599 -- Processing for i switch
603 Bad_Switch ("-gnati");
607 C := Switch_Chars (Ptr);
617 Identifier_Character_Set := C;
621 Bad_Switch ("-gnati" & Switch_Chars (Ptr .. Max));
624 -- Processing for I switch
628 Ignore_Rep_Clauses := True;
630 -- Processing for j switch
634 Scan_Nat (Switch_Chars, Max, Ptr, Error_Msg_Line_Length, C);
636 -- Processing for k switch
641 (Switch_Chars, Max, Ptr, Maximum_File_Name_Length, C);
643 -- Processing for l switch
649 -- There may be an equal sign between -gnatl and a file name
651 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
653 Osint.Fail ("file name for -gnatl= is null");
655 Opt.Full_List_File_Name :=
656 new String'(Switch_Chars (Ptr + 1 .. Max));
661 -- Processing for L switch
665 Dump_Source_Text := True;
667 -- Processing for m switch
671 Scan_Nat (Switch_Chars, Max, Ptr, Maximum_Messages, C);
673 -- Processing for n switch
677 Inline_Active := True;
679 -- Processing for N switch
683 Inline_Active := True;
684 Front_End_Inlining := True;
686 -- Processing for o switch
690 Suppress_Options (Overflow_Check) := False;
691 Opt.Enable_Overflow_Checks := True;
693 -- Processing for O switch
696 Store_Switch := False;
698 Output_File_Name_Present := True;
700 -- Processing for p switch
705 -- Skip processing if cancelled by subsequent -gnat-p
707 if Switch_Subsequently_Cancelled ("p", Args, Arg_Rank) then
708 Store_Switch := False;
711 -- Set all specific options as well as All_Checks in the
712 -- Suppress_Options array, excluding Elaboration_Check,
713 -- since this is treated specially because we do not want
714 -- -gnatp to disable static elaboration processing.
716 for J in Suppress_Options'Range loop
717 if J /= Elaboration_Check then
718 Suppress_Options (J) := True;
722 Validity_Checks_On := False;
723 Opt.Suppress_Checks := True;
724 Opt.Enable_Overflow_Checks := False;
727 -- Processing for P switch
731 Polling_Required := True;
733 -- Processing for q switch
737 Try_Semantics := True;
739 -- Processing for Q switch
743 Force_ALI_Tree_File := True;
744 Try_Semantics := True;
746 -- Processing for r switch
750 Treat_Restrictions_As_Warnings := True;
752 -- Processing for R switch
755 Back_Annotate_Rep_Info := True;
756 List_Representation_Info := 1;
759 while Ptr <= Max loop
760 C := Switch_Chars (Ptr);
762 if C in '1' .. '3' then
763 List_Representation_Info :=
764 Character'Pos (C) - Character'Pos ('0');
766 elsif Switch_Chars (Ptr) = 's' then
767 List_Representation_Info_To_File := True;
769 elsif Switch_Chars (Ptr) = 'm' then
770 List_Representation_Info_Mechanisms := True;
773 Bad_Switch ("-gnatR" & Switch_Chars (Ptr .. Max));
779 -- Processing for s switch
782 if not First_Switch then
784 ("-gnats must be first if combined with other switches");
788 Operating_Mode := Check_Syntax;
790 -- Processing for S switch
793 Print_Standard := True;
796 -- Processing for t switch
801 Back_Annotate_Rep_Info := True;
803 -- Processing for T switch
807 Scan_Pos (Switch_Chars, Max, Ptr, Table_Factor, C);
809 -- Processing for u switch
815 -- Processing for U switch
819 Unique_Error_Tag := True;
821 -- Processing for v switch
825 Verbose_Mode := True;
827 -- Processing for V switch
830 Store_Switch := False;
834 Bad_Switch ("-gnatV");
841 Set_Validity_Check_Options
842 (Switch_Chars (Ptr .. Max), OK, Ptr);
845 Bad_Switch ("-gnatV" & Switch_Chars (Ptr .. Max));
848 for Index in First_Char + 1 .. Max loop
849 Store_Compilation_Switch
850 ("-gnatV" & Switch_Chars (Index));
857 -- Processing for w switch
860 Store_Switch := False;
864 Bad_Switch ("-gnatw");
867 while Ptr <= Max loop
868 C := Switch_Chars (Ptr);
870 -- Case of dot switch
872 if C = '.' and then Ptr < Max then
874 C := Switch_Chars (Ptr);
876 if Set_Dot_Warning_Switch (C) then
877 Store_Compilation_Switch ("-gnatw." & C);
879 Bad_Switch ("-gnatw." & Switch_Chars (Ptr .. Max));
882 -- Normal case, no dot
885 if Set_Warning_Switch (C) then
886 Store_Compilation_Switch ("-gnatw" & C);
888 Bad_Switch ("-gnatw" & Switch_Chars (Ptr .. Max));
897 -- Processing for W switch
903 Bad_Switch ("-gnatW");
907 Wide_Character_Encoding_Method :=
908 Get_WC_Encoding_Method (Switch_Chars (Ptr));
910 when Constraint_Error =>
911 Bad_Switch ("-gnatW" & Switch_Chars (Ptr .. Max));
914 Wide_Character_Encoding_Method_Specified := True;
916 Upper_Half_Encoding :=
917 Wide_Character_Encoding_Method in
918 WC_Upper_Half_Encoding_Method;
922 -- Processing for x switch
926 Xref_Active := False;
928 -- Processing for X switch
932 Extensions_Allowed := True;
933 Ada_Version := Ada_Version_Type'Last;
934 Ada_Version_Explicit := Ada_Version_Type'Last;
936 -- Processing for y switch
942 Set_Default_Style_Check_Options;
945 Store_Switch := False;
951 Set_Style_Check_Options
952 (Switch_Chars (Ptr .. Max), OK, Ptr);
956 ("bad -gnaty switch (" &
957 Style_Msg_Buf (1 .. Style_Msg_Len) & ')');
960 Ptr := First_Char + 1;
961 while Ptr <= Max loop
962 if Switch_Chars (Ptr) = 'M' then
967 or else Switch_Chars (Ptr) not in '0' .. '9';
970 Store_Compilation_Switch
971 ("-gnaty" & Switch_Chars (First_Char .. Ptr - 1));
974 Store_Compilation_Switch
975 ("-gnaty" & Switch_Chars (Ptr));
982 -- Processing for z switch
986 -- -gnatz must be the first and only switch in Switch_Chars,
987 -- and is a two-letter switch.
989 if Ptr /= Switch_Chars'First + 5
990 or else (Max - Ptr + 1) > 2
993 ("-gnatz* may not be combined with other switches");
997 Bad_Switch ("-gnatz");
1002 -- Only one occurrence of -gnat* is permitted
1004 if Distribution_Stub_Mode = No_Stubs then
1005 case Switch_Chars (Ptr) is
1007 Distribution_Stub_Mode := Generate_Receiver_Stub_Body;
1010 Distribution_Stub_Mode := Generate_Caller_Stub_Body;
1013 Bad_Switch ("-gnatz" & Switch_Chars (Ptr .. Max));
1019 Osint.Fail ("only one -gnatz* switch allowed");
1022 -- Processing for Z switch
1027 ("-gnatZ is no longer supported: consider using --RTS=zcx");
1029 -- Processing for 83 switch
1033 Bad_Switch ("-gnat8");
1038 if Switch_Chars (Ptr) /= '3' then
1039 Bad_Switch ("-gnat8" & Switch_Chars (Ptr .. Max));
1042 Ada_Version := Ada_83;
1043 Ada_Version_Explicit := Ada_Version;
1046 -- Processing for 95 switch
1050 Bad_Switch ("-gnat9");
1055 if Switch_Chars (Ptr) /= '5' then
1056 Bad_Switch ("-gnat9" & Switch_Chars (Ptr .. Max));
1059 Ada_Version := Ada_95;
1060 Ada_Version_Explicit := Ada_Version;
1063 -- Processing for 05 switch
1067 Bad_Switch ("-gnat0");
1072 if Switch_Chars (Ptr) /= '5' then
1073 Bad_Switch ("-gnat0" & Switch_Chars (Ptr .. Max));
1076 Ada_Version := Ada_2005;
1077 Ada_Version_Explicit := Ada_Version;
1080 -- Processing for 12 switch
1084 Bad_Switch ("-gnat1");
1089 if Switch_Chars (Ptr) /= '2' then
1090 Bad_Switch ("-gnat1" & Switch_Chars (Ptr .. Max));
1093 Ada_Version := Ada_2012;
1094 Ada_Version_Explicit := Ada_Version;
1097 -- Processing for 2005 and 2012 switches
1100 if Ptr > Max - 3 then
1101 Bad_Switch ("-gnat" & Switch_Chars (Ptr .. Max));
1103 elsif Switch_Chars (Ptr .. Ptr + 3) = "2005" then
1104 Ada_Version := Ada_2005;
1106 elsif Switch_Chars (Ptr .. Ptr + 3) = "2012" then
1107 Ada_Version := Ada_2012;
1110 Bad_Switch ("-gnat" & Switch_Chars (Ptr .. Ptr + 3));
1113 Ada_Version_Explicit := Ada_Version;
1116 -- Switch cancellation, currently only -gnat-p is allowed.
1117 -- All we do here is the error checking, since the actual
1118 -- processing for switch cancellation is done by calls to
1119 -- Switch_Subsequently_Cancelled at the appropriate point.
1123 -- Simple ignore -gnat-p
1125 if Switch_Chars = "-gnat-p" then
1128 -- Any other occurrence of minus is ignored. This is for
1129 -- maximum compatibility with previous version which ignored
1130 -- all occurrences of minus.
1133 Store_Switch := False;
1137 -- We ignore '/' in switches, this is historical, still needed???
1140 Store_Switch := False;
1142 -- Anything else is an error (illegal switch character)
1145 Bad_Switch ("-gnat" & Switch_Chars (Ptr .. Max));
1148 if Store_Switch then
1149 Store_Compilation_Switch
1150 ("-gnat" & Switch_Chars (First_Char .. Ptr - 1));
1153 First_Switch := False;
1156 end Scan_Front_End_Switches;
1158 -----------------------------------
1159 -- Switch_Subsequently_Cancelled --
1160 -----------------------------------
1162 function Switch_Subsequently_Cancelled
1164 Args : Argument_List;
1165 Arg_Rank : Positive) return Boolean
1167 use type System.Strings.String_Access;
1170 -- Loop through arguments following the current one
1172 for Arg in Arg_Rank + 1 .. Args'Last loop
1173 if Args (Arg).all = "-gnat-" & C then
1178 -- No match found, not cancelled
1181 end Switch_Subsequently_Cancelled;