OSDN Git Service

PR 33870
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatxref.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             G N A T X R E F                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1998-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 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 ------------------------------------------------------------------------------
25
26 with Opt;
27 with Osint;    use Osint;
28 with Types;    use Types;
29 with Switch;   use Switch;
30 with Xr_Tabls; use Xr_Tabls;
31 with Xref_Lib; use Xref_Lib;
32
33 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
34 with Ada.Text_IO;       use Ada.Text_IO;
35
36 with GNAT.Command_Line; use GNAT.Command_Line;
37
38 with System.Strings;    use System.Strings;
39
40 procedure Gnatxref is
41    Search_Unused   : Boolean := False;
42    Local_Symbols   : Boolean := True;
43    Prj_File        : File_Name_String;
44    Prj_File_Length : Natural := 0;
45    Usage_Error     : exception;
46    Full_Path_Name  : Boolean := False;
47    Vi_Mode         : Boolean := False;
48    Read_Only       : Boolean := False;
49    Have_File       : Boolean := False;
50    Der_Info        : Boolean := False;
51
52    RTS_Specified : String_Access := null;
53    --  Used to detect multiple use of --RTS= switch
54
55    procedure Parse_Cmd_Line;
56    --  Parse every switch on the command line
57
58    procedure Usage;
59    --  Display the usage
60
61    procedure Write_Usage;
62    --  Print a small help page for program usage
63
64    --------------------
65    -- Parse_Cmd_Line --
66    --------------------
67
68    procedure Parse_Cmd_Line is
69    begin
70       --  First check for --version or --help
71
72       Check_Version_And_Help ("GNATXREF", "1998", Usage'Unrestricted_Access);
73
74       loop
75          case
76            GNAT.Command_Line.Getopt
77              ("a aI: aO: d f g h I: nostdinc nostdlib p: u v -RTS=")
78          is
79             when ASCII.NUL =>
80                exit;
81
82             when 'a' =>
83                if GNAT.Command_Line.Full_Switch = "a" then
84                   Read_Only := True;
85
86                elsif GNAT.Command_Line.Full_Switch = "aI" then
87                   Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
88
89                else
90                   Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
91                end if;
92
93             when 'd' =>
94                Der_Info := True;
95
96             when 'f' =>
97                Full_Path_Name := True;
98
99             when 'g' =>
100                Local_Symbols := False;
101
102             when 'h' =>
103                Write_Usage;
104
105             when 'I' =>
106                Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
107                Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
108
109             when 'n' =>
110                if GNAT.Command_Line.Full_Switch = "nostdinc" then
111                   Opt.No_Stdinc := True;
112                elsif GNAT.Command_Line.Full_Switch = "nostlib" then
113                   Opt.No_Stdlib := True;
114                end if;
115
116             when 'p' =>
117                declare
118                   S : constant String := GNAT.Command_Line.Parameter;
119                begin
120                   Prj_File_Length := S'Length;
121                   Prj_File (1 .. Prj_File_Length) := S;
122                end;
123
124             when 'u' =>
125                Search_Unused := True;
126                Vi_Mode := False;
127
128             when 'v' =>
129                Vi_Mode := True;
130                Search_Unused := False;
131
132             --  The only switch starting with -- recognized is --RTS
133
134             when '-' =>
135
136                --  Check that it is the first time we see this switch
137
138                if RTS_Specified = null then
139                   RTS_Specified := new String'(GNAT.Command_Line.Parameter);
140
141                elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
142                   Osint.Fail ("--RTS cannot be specified multiple times");
143                end if;
144
145                Opt.No_Stdinc  := True;
146                Opt.RTS_Switch := True;
147
148                declare
149                   Src_Path_Name : constant String_Ptr :=
150                                     Get_RTS_Search_Dir
151                                       (GNAT.Command_Line.Parameter, Include);
152
153                   Lib_Path_Name : constant String_Ptr :=
154                                     Get_RTS_Search_Dir
155                                       (GNAT.Command_Line.Parameter, Objects);
156
157                begin
158                   if Src_Path_Name /= null and then Lib_Path_Name /= null then
159                      Add_Search_Dirs (Src_Path_Name, Include);
160                      Add_Search_Dirs (Lib_Path_Name, Objects);
161
162                   elsif Src_Path_Name = null and then Lib_Path_Name = null then
163                      Osint.Fail ("RTS path not valid: missing " &
164                                  "adainclude and adalib directories");
165
166                   elsif Src_Path_Name = null then
167                      Osint.Fail ("RTS path not valid: missing " &
168                                  "adainclude directory");
169
170                   elsif  Lib_Path_Name = null then
171                      Osint.Fail ("RTS path not valid: missing " &
172                                  "adalib directory");
173                   end if;
174                end;
175
176             when others =>
177                Write_Usage;
178          end case;
179       end loop;
180
181       --  Get the other arguments
182
183       loop
184          declare
185             S : constant String := GNAT.Command_Line.Get_Argument;
186
187          begin
188             exit when S'Length = 0;
189
190             if Ada.Strings.Fixed.Index (S, ":") /= 0 then
191                Ada.Text_IO.Put_Line
192                  ("Only file names are allowed on the command line");
193                Write_Usage;
194             end if;
195
196             Add_Xref_File (S);
197             Have_File := True;
198          end;
199       end loop;
200
201    exception
202       when GNAT.Command_Line.Invalid_Switch =>
203          Ada.Text_IO.Put_Line ("Invalid switch : "
204                                & GNAT.Command_Line.Full_Switch);
205          Write_Usage;
206
207       when GNAT.Command_Line.Invalid_Parameter =>
208          Ada.Text_IO.Put_Line ("Parameter missing for : "
209                                & GNAT.Command_Line.Full_Switch);
210          Write_Usage;
211    end Parse_Cmd_Line;
212
213    -----------
214    -- Usage --
215    -----------
216
217    procedure Usage is
218    begin
219       Put_Line ("Usage: gnatxref [switches] file1 file2 ...");
220       New_Line;
221       Put_Line ("  file ... list of source files to xref, " &
222                 "including with'ed units");
223       New_Line;
224       Put_Line ("gnatxref switches:");
225       Put_Line ("   -a        Consider all files, even when the ali file is"
226                 & " readonly");
227       Put_Line ("   -aIdir    Specify source files search path");
228       Put_Line ("   -aOdir    Specify library/object files search path");
229       Put_Line ("   -d        Output derived type information");
230       Put_Line ("   -f        Output full path name");
231       Put_Line ("   -g        Output information only for global symbols");
232       Put_Line ("   -Idir     Like -aIdir -aOdir");
233       Put_Line ("   -nostdinc Don't look for sources in the system default"
234                 & " directory");
235       Put_Line ("   -nostdlib Don't look for library files in the system"
236                 & " default directory");
237       Put_Line ("   --RTS=dir specify the default source and object search"
238                 & " path");
239       Put_Line ("   -p file   Use file as the default project file");
240       Put_Line ("   -u        List unused entities");
241       Put_Line ("   -v        Print a 'tags' file for vi");
242       New_Line;
243
244    end Usage;
245
246    -----------------
247    -- Write_Usage --
248    -----------------
249
250    procedure Write_Usage is
251    begin
252       Display_Version ("GNATXREF", "1998");
253       New_Line;
254       Usage;
255       raise Usage_Error;
256    end Write_Usage;
257
258 begin
259    Parse_Cmd_Line;
260
261    if not Have_File then
262       Write_Usage;
263    end if;
264
265    Xr_Tabls.Set_Default_Match (True);
266
267    --  Find the project file
268
269    if Prj_File_Length = 0 then
270       Xr_Tabls.Create_Project_File
271         (Default_Project_File (Osint.To_Host_Dir_Spec (".", False).all));
272    else
273       Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
274    end if;
275
276    --  Fill up the table
277
278    Search_Xref (Local_Symbols, Read_Only, Der_Info);
279
280    if Search_Unused then
281       Print_Unused (Full_Path_Name);
282    elsif Vi_Mode then
283       Print_Vi (Full_Path_Name);
284    else
285       Print_Xref (Full_Path_Name);
286    end if;
287
288 exception
289    when Usage_Error =>
290       null;
291 end Gnatxref;