2007-06-06 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / gcc / ada / switch-m.adb
blob7c7259d6b4b1b401a0105ba7a723a795296dd566
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S W I T C H - M --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2001-2007, 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 Debug; use Debug;
28 with Osint; use Osint;
29 with Opt; use Opt;
30 with Prj.Ext; use Prj.Ext;
31 with Table;
33 package body Switch.M is
35 package Normalized_Switches is new Table.Table
36 (Table_Component_Type => String_Access,
37 Table_Index_Type => Integer,
38 Table_Low_Bound => 1,
39 Table_Initial => 20,
40 Table_Increment => 100,
41 Table_Name => "Switch.M.Normalized_Switches");
42 -- This table is used to keep the normalized switches, so that they may be
43 -- reused for subsequent invocations of Normalize_Compiler_Switches with
44 -- similar switches.
46 Initial_Number_Of_Switches : constant := 10;
48 Global_Switches : Argument_List_Access := null;
49 -- Used by function Normalize_Compiler_Switches
51 ---------------------------------
52 -- Normalize_Compiler_Switches --
53 ---------------------------------
55 procedure Normalize_Compiler_Switches
56 (Switch_Chars : String;
57 Switches : in out Argument_List_Access;
58 Last : out Natural)
60 Switch_Starts_With_Gnat : Boolean;
62 Ptr : Integer := Switch_Chars'First;
63 Max : constant Integer := Switch_Chars'Last;
64 C : Character := ' ';
66 Storing : String := Switch_Chars;
67 First_Stored : Positive := Ptr + 1;
68 Last_Stored : Positive := First_Stored;
70 procedure Add_Switch_Component (S : String);
71 -- Add a new String_Access component in Switches. If a string equal
72 -- to S is already stored in the table Normalized_Switches, use it.
73 -- Other wise add a new component to the table.
75 --------------------------
76 -- Add_Switch_Component --
77 --------------------------
79 procedure Add_Switch_Component (S : String) is
80 begin
81 -- If Switches is null, allocate a new array
83 if Switches = null then
84 Switches := new Argument_List (1 .. Initial_Number_Of_Switches);
86 -- otherwise, if Switches is full, extend it
88 elsif Last = Switches'Last then
89 declare
90 New_Switches : constant Argument_List_Access :=
91 new Argument_List
92 (1 .. Switches'Length + Switches'Length);
93 begin
94 New_Switches (1 .. Switches'Length) := Switches.all;
95 Last := Switches'Length;
96 Switches := New_Switches;
97 end;
98 end if;
100 -- If this is the first switch, Last designates the first component
102 if Last = 0 then
103 Last := Switches'First;
104 else
105 Last := Last + 1;
106 end if;
108 -- Look into the table Normalized_Switches for a similar string.
109 -- If one is found, put it at the added component, and return.
111 for Index in 1 .. Normalized_Switches.Last loop
112 if S = Normalized_Switches.Table (Index).all then
113 Switches (Last) := Normalized_Switches.Table (Index);
114 return;
115 end if;
116 end loop;
118 -- No string equal to S was found in the table Normalized_Switches.
119 -- Add a new component in the table.
121 Switches (Last) := new String'(S);
122 Normalized_Switches.Increment_Last;
123 Normalized_Switches.Table (Normalized_Switches.Last) :=
124 Switches (Last);
125 end Add_Switch_Component;
127 -- Start of processing for Normalize_Compiler_Switches
129 begin
130 Last := 0;
132 if Ptr = Max or else Switch_Chars (Ptr) /= '-' then
133 return;
134 end if;
136 Ptr := Ptr + 1;
138 Switch_Starts_With_Gnat :=
139 Ptr + 3 <= Max and then Switch_Chars (Ptr .. Ptr + 3) = "gnat";
141 if Switch_Starts_With_Gnat then
142 Ptr := Ptr + 4;
143 First_Stored := Ptr;
144 end if;
146 while Ptr <= Max loop
147 C := Switch_Chars (Ptr);
149 -- Processing for a switch
151 case Switch_Starts_With_Gnat is
153 when False =>
155 -- All switches that don't start with -gnat stay as is,
156 -- except -v, -E and -pg
158 if Switch_Chars = "-pg" then
160 -- The gcc driver converts -pg to -p, so that is what
161 -- is stored in the ALI file.
163 Add_Switch_Component ("-p");
165 -- Do not take into account switches that are not transmitted
166 -- to gnat1 by the gcc driver.
168 elsif C /= 'v' and then C /= 'E' then
169 Add_Switch_Component (Switch_Chars);
170 end if;
172 return;
174 when True =>
176 case C is
178 -- One-letter switches
180 when 'a' | 'A' | 'b' | 'c' | 'D' | 'E' | 'f' |
181 'F' | 'g' | 'h' | 'H' | 'k' | 'l' | 'L' | 'n' | 'N' |
182 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 's' | 't' |
183 'u' | 'U' | 'v' | 'x' | 'X' | 'Z' =>
184 Storing (First_Stored) := C;
185 Add_Switch_Component
186 (Storing (Storing'First .. First_Stored));
187 Ptr := Ptr + 1;
189 -- One-letter switches followed by a positive number
191 when 'm' | 'T' =>
192 Storing (First_Stored) := C;
193 Last_Stored := First_Stored;
195 loop
196 Ptr := Ptr + 1;
197 exit when Ptr > Max
198 or else Switch_Chars (Ptr) not in '0' .. '9';
199 Last_Stored := Last_Stored + 1;
200 Storing (Last_Stored) := Switch_Chars (Ptr);
201 end loop;
203 Add_Switch_Component
204 (Storing (Storing'First .. Last_Stored));
206 when 'd' =>
207 Storing (First_Stored) := 'd';
209 while Ptr < Max loop
210 Ptr := Ptr + 1;
211 C := Switch_Chars (Ptr);
212 exit when C = ASCII.NUL or else C = '/'
213 or else C = '-';
215 if C in '1' .. '9' or else
216 C in 'a' .. 'z' or else
217 C in 'A' .. 'Z'
218 then
219 Storing (First_Stored + 1) := C;
220 Add_Switch_Component
221 (Storing (Storing'First .. First_Stored + 1));
223 else
224 Last := 0;
225 return;
226 end if;
227 end loop;
229 return;
231 when 'e' =>
233 -- Only -gnateD and -gnatep= need storing in ALI file
235 Storing (First_Stored) := 'e';
236 Ptr := Ptr + 1;
238 if Ptr > Max
239 or else (Switch_Chars (Ptr) /= 'D'
240 and then Switch_Chars (Ptr) /= 'p')
241 then
242 Last := 0;
243 return;
244 end if;
246 -- Processing for -gnateD
248 if Switch_Chars (Ptr) = 'D' then
249 Storing (First_Stored + 1 ..
250 First_Stored + Max - Ptr + 1) :=
251 Switch_Chars (Ptr .. Max);
252 Add_Switch_Component
253 (Storing (Storing'First ..
254 First_Stored + Max - Ptr + 1));
256 -- Processing for -gnatep=
258 else
259 Ptr := Ptr + 1;
261 if Ptr = Max then
262 Last := 0;
263 return;
264 end if;
266 if Switch_Chars (Ptr) = '=' then
267 Ptr := Ptr + 1;
268 end if;
270 -- To normalize, always put a '=' after -gnatep.
271 -- Because that could lengthen the switch string,
272 -- declare a local variable.
274 declare
275 To_Store : String (1 .. Max - Ptr + 9);
276 begin
277 To_Store (1 .. 8) := "-gnatep=";
278 To_Store (9 .. Max - Ptr + 9) :=
279 Switch_Chars (Ptr .. Max);
280 Add_Switch_Component (To_Store);
281 end;
282 end if;
284 return;
286 when 'i' =>
287 Storing (First_Stored) := 'i';
289 Ptr := Ptr + 1;
291 if Ptr > Max then
292 Last := 0;
293 return;
294 end if;
296 C := Switch_Chars (Ptr);
298 if C in '1' .. '5'
299 or else C = '8'
300 or else C = 'p'
301 or else C = 'f'
302 or else C = 'n'
303 or else C = 'w'
304 then
305 Storing (First_Stored + 1) := C;
306 Add_Switch_Component
307 (Storing (Storing'First .. First_Stored + 1));
308 Ptr := Ptr + 1;
310 else
311 Last := 0;
312 return;
313 end if;
315 -- -gnatR may be followed by '0', '1', '2' or '3',
316 -- then by 's'
318 when 'R' =>
319 Last_Stored := First_Stored;
320 Storing (Last_Stored) := 'R';
321 Ptr := Ptr + 1;
323 if Ptr <= Max
324 and then Switch_Chars (Ptr) in '0' .. '9'
325 then
326 C := Switch_Chars (Ptr);
328 if C in '4' .. '9' then
329 Last := 0;
330 return;
332 else
333 Last_Stored := Last_Stored + 1;
334 Storing (Last_Stored) := C;
335 Ptr := Ptr + 1;
337 if Ptr <= Max
338 and then Switch_Chars (Ptr) = 's' then
339 Last_Stored := Last_Stored + 1;
340 Storing (Last_Stored) := 's';
341 Ptr := Ptr + 1;
342 end if;
343 end if;
344 end if;
346 Add_Switch_Component
347 (Storing (Storing'First .. Last_Stored));
349 -- Multiple switches
351 when 'V' | 'w' | 'y' =>
352 Storing (First_Stored) := C;
353 Ptr := Ptr + 1;
355 if Ptr > Max then
356 if C = 'y' then
357 Add_Switch_Component
358 (Storing (Storing'First .. First_Stored));
360 else
361 Last := 0;
362 return;
363 end if;
364 end if;
366 while Ptr <= Max loop
367 C := Switch_Chars (Ptr);
368 Ptr := Ptr + 1;
370 -- -gnatyMxxx
372 if C = 'M' and then
373 Storing (First_Stored) = 'y'
374 then
375 Last_Stored := First_Stored + 1;
376 Storing (Last_Stored) := 'M';
378 while Ptr <= Max loop
379 C := Switch_Chars (Ptr);
380 exit when C not in '0' .. '9';
381 Last_Stored := Last_Stored + 1;
382 Storing (Last_Stored) := C;
383 Ptr := Ptr + 1;
384 end loop;
386 -- If there is no digit after -gnatyM,
387 -- the switch is invalid.
389 if Last_Stored = First_Stored + 1 then
390 Last := 0;
391 return;
393 else
394 Add_Switch_Component
395 (Storing (Storing'First .. Last_Stored));
396 end if;
398 -- All other switches are -gnatxx
400 else
401 Storing (First_Stored + 1) := C;
402 Add_Switch_Component
403 (Storing (Storing'First .. First_Stored + 1));
404 end if;
405 end loop;
407 -- -gnat95 -gnat05
409 when '0' | '9' =>
410 Last_Stored := First_Stored;
411 Storing (Last_Stored) := C;
412 Ptr := Ptr + 1;
414 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
416 -- Invalid switch
418 Last := 0;
419 return;
421 else
422 Last_Stored := Last_Stored + 1;
423 Storing (Last_Stored) := '5';
424 Add_Switch_Component
425 (Storing (Storing'First .. Last_Stored));
426 Ptr := Ptr + 1;
427 end if;
429 -- -gnat83
431 when '8' =>
432 Last_Stored := First_Stored;
433 Storing (Last_Stored) := '8';
434 Ptr := Ptr + 1;
436 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
438 -- Invalid switch
440 Last := 0;
441 return;
443 else
444 Last_Stored := Last_Stored + 1;
445 Storing (Last_Stored) := '3';
446 Add_Switch_Component
447 (Storing (Storing'First .. Last_Stored));
448 Ptr := Ptr + 1;
449 end if;
451 -- Not a valid switch
453 when others =>
454 Last := 0;
455 return;
457 end case;
459 end case;
460 end loop;
461 end Normalize_Compiler_Switches;
463 function Normalize_Compiler_Switches
464 (Switch_Chars : String)
465 return Argument_List
467 Last : Natural;
469 begin
470 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
472 if Last = 0 then
473 return (1 .. 0 => null);
474 else
475 return Global_Switches (Global_Switches'First .. Last);
476 end if;
478 end Normalize_Compiler_Switches;
480 ------------------------
481 -- Scan_Make_Switches --
482 ------------------------
484 procedure Scan_Make_Switches
485 (Switch_Chars : String;
486 Success : out Boolean)
488 Ptr : Integer := Switch_Chars'First;
489 Max : constant Integer := Switch_Chars'Last;
490 C : Character := ' ';
492 begin
493 -- Assume a good switch
495 Success := True;
497 -- Skip past the initial character (must be the switch character)
499 if Ptr = Max then
500 Bad_Switch (Switch_Chars);
502 else
503 Ptr := Ptr + 1;
504 end if;
506 -- A little check, "gnat" at the start of a switch is for the compiler
508 if Switch_Chars'Length >= Ptr + 3
509 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
510 then
511 Success := False;
512 return;
513 end if;
515 C := Switch_Chars (Ptr);
517 -- Multiple character switches
519 if Switch_Chars'Length > 2 then
520 if Switch_Chars = "--create-missing-dirs" then
521 Setup_Projects := True;
523 elsif Switch_Chars'Length > 3 and then
524 Switch_Chars (Ptr .. Ptr + 1) = "aP"
525 then
526 Add_Search_Project_Directory
527 (Switch_Chars (Ptr + 2 .. Switch_Chars'Last));
529 elsif C = 'v' and then Switch_Chars'Length = 3 then
530 Ptr := Ptr + 1;
531 Verbose_Mode := True;
533 case Switch_Chars (Ptr) is
534 when 'l' =>
535 Verbosity_Level := Opt.Low;
537 when 'm' =>
538 Verbosity_Level := Opt.Medium;
540 when 'h' =>
541 Verbosity_Level := Opt.High;
543 when others =>
544 Success := False;
545 end case;
547 elsif C = 'd' then
549 -- Note: for the debug switch, the remaining characters in this
550 -- switch field must all be debug flags, since all valid switch
551 -- characters are also valid debug characters. This switch is not
552 -- documented on purpose because it is only used by the
553 -- implementors.
555 -- Loop to scan out debug flags
557 while Ptr < Max loop
558 Ptr := Ptr + 1;
559 C := Switch_Chars (Ptr);
561 if C in 'a' .. 'z' or else C in 'A' .. 'Z' then
562 Set_Debug_Flag (C);
563 else
564 Bad_Switch (Switch_Chars);
565 end if;
566 end loop;
568 elsif C = 'e' then
569 Ptr := Ptr + 1;
571 case Switch_Chars (Ptr) is
573 -- Processing for eI switch
575 when 'I' =>
576 Ptr := Ptr + 1;
577 Scan_Pos (Switch_Chars, Max, Ptr, Main_Index, C);
579 if Ptr <= Max then
580 Bad_Switch (Switch_Chars);
581 end if;
583 -- Processing for eL switch
585 when 'L' =>
586 if Ptr /= Max then
587 Bad_Switch (Switch_Chars);
589 else
590 Follow_Links := True;
591 end if;
593 -- Processing for eS switch
595 when 'S' =>
596 if Ptr /= Max then
597 Bad_Switch (Switch_Chars);
599 else
600 Commands_To_Stdout := True;
601 end if;
603 when others =>
604 Bad_Switch (Switch_Chars);
605 end case;
607 elsif C = 'j' then
608 Ptr := Ptr + 1;
610 declare
611 Max_Proc : Pos;
612 begin
613 Scan_Pos (Switch_Chars, Max, Ptr, Max_Proc, C);
615 if Ptr <= Max then
616 Bad_Switch (Switch_Chars);
618 else
619 Maximum_Processes := Positive (Max_Proc);
620 end if;
621 end;
623 elsif C = 'w' and then Switch_Chars'Length = 3 then
624 Ptr := Ptr + 1;
626 if Switch_Chars = "-we" then
627 Warning_Mode := Treat_As_Error;
629 elsif Switch_Chars = "-wn" then
630 Warning_Mode := Normal;
632 elsif Switch_Chars = "-ws" then
633 Warning_Mode := Suppress;
635 else
636 Success := False;
637 end if;
639 else
640 Success := False;
641 end if;
643 -- Single-character switches
645 else
646 Check_Switch : begin
648 case C is
650 when 'a' =>
651 Check_Readonly_Files := True;
653 -- Processing for b switch
655 when 'b' =>
656 Bind_Only := True;
657 Make_Steps := True;
659 -- Processing for B switch
661 when 'B' =>
662 Build_Bind_And_Link_Full_Project := True;
664 -- Processing for c switch
666 when 'c' =>
667 Compile_Only := True;
668 Make_Steps := True;
670 -- Processing for C switch
672 when 'C' =>
673 Create_Mapping_File := True;
675 -- Processing for D switch
677 when 'D' =>
678 if Object_Directory_Present then
679 Osint.Fail ("duplicate -D switch");
681 else
682 Object_Directory_Present := True;
683 end if;
685 -- Processing for f switch
687 when 'f' =>
688 Force_Compilations := True;
690 -- Processing for F switch
692 when 'F' =>
693 Full_Path_Name_For_Brief_Errors := True;
695 -- Processing for h switch
697 when 'h' =>
698 Usage_Requested := True;
700 -- Processing for i switch
702 when 'i' =>
703 In_Place_Mode := True;
705 -- Processing for j switch
707 when 'j' =>
708 -- -j not followed by a number is an error
710 Bad_Switch (Switch_Chars);
712 -- Processing for k switch
714 when 'k' =>
715 Keep_Going := True;
717 -- Processing for l switch
719 when 'l' =>
720 Link_Only := True;
721 Make_Steps := True;
723 -- Processing for M switch
725 when 'M' =>
726 List_Dependencies := True;
728 -- Processing for n switch
730 when 'n' =>
731 Do_Not_Execute := True;
733 -- Processing for o switch
735 when 'o' =>
736 if Output_File_Name_Present then
737 Osint.Fail ("duplicate -o switch");
738 else
739 Output_File_Name_Present := True;
740 end if;
742 -- Processing for p switch
744 when 'p' =>
745 Setup_Projects := True;
747 -- Processing for q switch
749 when 'q' =>
750 Quiet_Output := True;
752 -- Processing for R switch
754 when 'R' =>
755 Run_Path_Option := False;
757 -- Processing for s switch
759 when 's' =>
760 Ptr := Ptr + 1;
761 Check_Switches := True;
763 -- Processing for v switch
765 when 'v' =>
766 Verbose_Mode := True;
767 Verbosity_Level := Opt.High;
769 -- Processing for x switch
771 when 'x' =>
772 External_Unit_Compilation_Allowed := True;
774 -- Processing for z switch
776 when 'z' =>
777 No_Main_Subprogram := True;
779 -- Any other small letter is an illegal switch
781 when others =>
782 if C in 'a' .. 'z' then
783 Bad_Switch (Switch_Chars);
785 else
786 Success := False;
787 end if;
789 end case;
790 end Check_Switch;
791 end if;
792 end Scan_Make_Switches;
794 end Switch.M;