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