OSDN Git Service

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