2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / ada / gnatfind.adb
blob49b1e41419ea94e051bcfe3261e772eb0f354dbd
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T F I N D --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2005 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 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 --------------
41 -- Gnatfind --
42 --------------
44 procedure Gnatfind is
45 Output_Ref : Boolean := False;
46 Pattern : Xref_Lib.Search_Pattern;
47 Local_Symbols : Boolean := True;
48 Prj_File : File_Name_String;
49 Prj_File_Length : Natural := 0;
50 Nb_File : Natural := 0;
51 Usage_Error : exception;
52 Full_Path_Name : Boolean := False;
53 Have_Entity : Boolean := False;
54 Wide_Search : Boolean := True;
55 Glob_Mode : Boolean := True;
56 Der_Info : Boolean := False;
57 Type_Tree : Boolean := False;
58 Read_Only : Boolean := False;
59 Source_Lines : Boolean := False;
61 Has_File_In_Entity : Boolean := False;
62 -- Will be true if a file name was specified in the entity
64 RTS_Specified : String_Access := null;
65 -- Used to detect multiple use of --RTS= switch
67 procedure Parse_Cmd_Line;
68 -- Parse every switch on the command line
70 procedure Write_Usage;
71 -- Print a small help page for program usage
73 --------------------
74 -- Parse_Cmd_Line --
75 --------------------
77 procedure Parse_Cmd_Line is
78 begin
79 loop
80 case
81 GNAT.Command_Line.Getopt
82 ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS=")
84 when ASCII.NUL =>
85 exit;
87 when 'a' =>
88 if GNAT.Command_Line.Full_Switch = "a" then
89 Read_Only := True;
90 elsif GNAT.Command_Line.Full_Switch = "aI" then
91 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
92 else
93 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
94 end if;
96 when 'd' =>
97 Der_Info := True;
99 when 'e' =>
100 Glob_Mode := False;
102 when 'f' =>
103 Full_Path_Name := True;
105 when 'g' =>
106 Local_Symbols := False;
108 when 'h' =>
109 Write_Usage;
111 when 'I' =>
112 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
113 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
115 when 'n' =>
116 if GNAT.Command_Line.Full_Switch = "nostdinc" then
117 Opt.No_Stdinc := True;
118 elsif GNAT.Command_Line.Full_Switch = "nostlib" then
119 Opt.No_Stdlib := True;
120 end if;
122 when 'p' =>
123 declare
124 S : constant String := GNAT.Command_Line.Parameter;
125 begin
126 Prj_File_Length := S'Length;
127 Prj_File (1 .. Prj_File_Length) := S;
128 end;
130 when 'r' =>
131 Output_Ref := True;
133 when 's' =>
134 Source_Lines := True;
136 when 't' =>
137 Type_Tree := True;
139 -- Only switch starting with -- recognized is --RTS
141 when '-' =>
142 -- Check that it is the first time we see this switch
144 if RTS_Specified = null then
145 RTS_Specified := new String'(GNAT.Command_Line.Parameter);
147 elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
148 Osint.Fail ("--RTS cannot be specified multiple times");
149 end if;
151 Opt.No_Stdinc := True;
152 Opt.RTS_Switch := True;
154 declare
155 Src_Path_Name : constant String_Ptr :=
156 Get_RTS_Search_Dir
157 (GNAT.Command_Line.Parameter, Include);
158 Lib_Path_Name : constant String_Ptr :=
159 Get_RTS_Search_Dir
160 (GNAT.Command_Line.Parameter, Objects);
162 begin
163 if Src_Path_Name /= null and then Lib_Path_Name /= null then
164 Add_Search_Dirs (Src_Path_Name, Include);
165 Add_Search_Dirs (Lib_Path_Name, Objects);
167 elsif Src_Path_Name = null and then Lib_Path_Name = null then
168 Osint.Fail ("RTS path not valid: missing " &
169 "adainclude and adalib directories");
171 elsif Src_Path_Name = null then
172 Osint.Fail ("RTS path not valid: missing " &
173 "adainclude directory");
175 elsif Lib_Path_Name = null then
176 Osint.Fail ("RTS path not valid: missing " &
177 "adalib directory");
178 end if;
179 end;
181 when others =>
182 Write_Usage;
183 end case;
184 end loop;
186 -- Get the other arguments
188 loop
189 declare
190 S : constant String := GNAT.Command_Line.Get_Argument;
192 begin
193 exit when S'Length = 0;
195 -- First argument is the pattern
197 if not Have_Entity then
198 Add_Entity (Pattern, S, Glob_Mode);
199 Have_Entity := True;
201 if not Has_File_In_Entity
202 and then Index (S, ":") /= 0
203 then
204 Has_File_In_Entity := True;
205 end if;
207 -- Next arguments are the files to search
209 else
210 Add_Xref_File (S);
211 Wide_Search := False;
212 Nb_File := Nb_File + 1;
213 end if;
214 end;
215 end loop;
217 exception
218 when GNAT.Command_Line.Invalid_Switch =>
219 Ada.Text_IO.Put_Line ("Invalid switch : "
220 & GNAT.Command_Line.Full_Switch);
221 Write_Usage;
223 when GNAT.Command_Line.Invalid_Parameter =>
224 Ada.Text_IO.Put_Line ("Parameter missing for : "
225 & GNAT.Command_Line.Full_Switch);
226 Write_Usage;
228 when Xref_Lib.Invalid_Argument =>
229 Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
230 Write_Usage;
231 end Parse_Cmd_Line;
233 -----------------
234 -- Write_Usage --
235 -----------------
237 procedure Write_Usage is
238 begin
239 Put_Line ("GNATFIND " & Gnatvsn.Gnat_Version_String);
240 Put_Line ("Copyright 1998-2005, AdaCore");
241 Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
242 & "[file1 file2 ...]");
243 New_Line;
244 Put_Line (" pattern Name of the entity to look for (can have "
245 & "wildcards)");
246 Put_Line (" sourcefile Only find entities referenced from this "
247 & "file");
248 Put_Line (" line Only find entities referenced from this line "
249 & "of file");
250 Put_Line (" column Only find entities referenced from this columns"
251 & " of file");
252 Put_Line (" file ... Set of Ada source files to search for "
253 & "references. This parameters are optional");
254 New_Line;
255 Put_Line ("gnatfind switches:");
256 Put_Line (" -a Consider all files, even when the ali file is "
257 & "readonly");
258 Put_Line (" -aIdir Specify source files search path");
259 Put_Line (" -aOdir Specify library/object files search path");
260 Put_Line (" -d Output derived type information");
261 Put_Line (" -e Use the full regular expression set for "
262 & "pattern");
263 Put_Line (" -f Output full path name");
264 Put_Line (" -g Output information only for global symbols");
265 Put_Line (" -Idir Like -aIdir -aOdir");
266 Put_Line (" -nostdinc Don't look for sources in the system default"
267 & " directory");
268 Put_Line (" -nostdlib Don't look for library files in the system"
269 & " default directory");
270 Put_Line (" --RTS=dir specify the default source and object search"
271 & " path");
272 Put_Line (" -p file Use file as the default project file");
273 Put_Line (" -r Find all references (default to find declaration"
274 & " only)");
275 Put_Line (" -s Print source line");
276 Put_Line (" -t Print type hierarchy");
277 New_Line;
279 raise Usage_Error;
280 end Write_Usage;
282 -- Start of processing for Gnatfind
284 begin
285 Parse_Cmd_Line;
287 if not Have_Entity then
288 Write_Usage;
289 end if;
291 -- Special case to speed things up: if the user has a command line of the
292 -- form 'gnatfind entity:file', ie has specified a file and only wants the
293 -- bodies and specs, then we can restrict the search to the .ali file
294 -- associated with 'file'.
296 if Has_File_In_Entity
297 and then not Output_Ref
298 then
299 Wide_Search := False;
300 end if;
302 -- Find the project file
304 if Prj_File_Length = 0 then
305 Xr_Tabls.Create_Project_File (Default_Project_File ("."));
306 else
307 Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
308 end if;
310 -- Fill up the table
312 if Type_Tree and then Nb_File > 1 then
313 Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
314 & "specify only one file.");
315 Ada.Text_IO.New_Line;
316 Write_Usage;
317 end if;
319 Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
320 Der_Info, Type_Tree);
322 if Source_Lines then
323 Xr_Tabls.Grep_Source_Files;
324 end if;
326 Print_Gnatfind (Output_Ref, Full_Path_Name);
328 exception
329 when Usage_Error =>
330 null;
331 end Gnatfind;