* rtl.h (struct rtx_def): Update comments.
[official-gcc.git] / gcc / ada / gnatfind.adb
blob20c0ccc1fec10a8fdd699fa8bd70e1c970b51957
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T F I N D --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1998-2002 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
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;
39 ---------------
40 -- Gnatfind --
41 ---------------
43 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 procedure Parse_Cmd_Line;
65 -- Parse every switch on the command line
67 procedure Write_Usage;
68 -- Print a small help page for program usage
70 --------------------
71 -- Parse_Cmd_Line --
72 --------------------
74 procedure Parse_Cmd_Line is
75 begin
76 loop
77 case
78 GNAT.Command_Line.Getopt
79 ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS=")
81 when ASCII.NUL =>
82 exit;
84 when 'a' =>
85 if GNAT.Command_Line.Full_Switch = "a" then
86 Read_Only := True;
88 elsif GNAT.Command_Line.Full_Switch = "aI" then
89 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
91 else
92 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
93 end if;
95 when 'd' =>
96 Der_Info := True;
98 when 'e' =>
99 Glob_Mode := False;
101 when 'f' =>
102 Full_Path_Name := True;
104 when 'g' =>
105 Local_Symbols := False;
107 when 'h' =>
108 Write_Usage;
110 when 'I' =>
111 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
112 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
114 when 'n' =>
115 if GNAT.Command_Line.Full_Switch = "nostdinc" then
116 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;
126 begin
127 Prj_File_Length := S'Length;
128 Prj_File (1 .. Prj_File_Length) := S;
129 end;
131 when 'r' =>
132 Output_Ref := True;
134 when 's' =>
135 Source_Lines := True;
137 when 't' =>
138 Type_Tree := True;
140 -- Only switch starting with -- recognized is --RTS
142 when '-' =>
143 Opt.No_Stdinc := True;
144 Opt.RTS_Switch := True;
146 declare
147 Src_Path_Name : String_Ptr :=
148 Get_RTS_Search_Dir
149 (GNAT.Command_Line.Parameter, Include);
150 Lib_Path_Name : String_Ptr :=
151 Get_RTS_Search_Dir
152 (GNAT.Command_Line.Parameter, Objects);
154 begin
155 if Src_Path_Name /= null and then Lib_Path_Name /= null then
156 Add_Search_Dirs (Src_Path_Name, Include);
157 Add_Search_Dirs (Lib_Path_Name, Objects);
159 elsif Src_Path_Name = null and then Lib_Path_Name = null then
160 Osint.Fail ("RTS path not valid: missing " &
161 "adainclude and adalib directories");
163 elsif Src_Path_Name = null then
164 Osint.Fail ("RTS path not valid: missing " &
165 "adainclude directory");
167 elsif Lib_Path_Name = null then
168 Osint.Fail ("RTS path not valid: missing " &
169 "adalib directory");
170 end if;
171 end;
173 when others =>
174 Write_Usage;
175 end case;
176 end loop;
178 -- Get the other arguments
180 loop
181 declare
182 S : constant String := GNAT.Command_Line.Get_Argument;
184 begin
185 exit when S'Length = 0;
187 -- First argument is the pattern
189 if not Have_Entity then
190 Add_Entity (Pattern, S, Glob_Mode);
191 Have_Entity := True;
193 if not Has_File_In_Entity
194 and then Index (S, ":") /= 0
195 then
196 Has_File_In_Entity := True;
197 end if;
199 -- Next arguments are the files to search
200 else
201 Add_Xref_File (S);
202 Wide_Search := False;
203 Nb_File := Nb_File + 1;
204 end if;
205 end;
206 end loop;
208 exception
209 when GNAT.Command_Line.Invalid_Switch =>
210 Ada.Text_IO.Put_Line ("Invalid switch : "
211 & GNAT.Command_Line.Full_Switch);
212 Write_Usage;
214 when GNAT.Command_Line.Invalid_Parameter =>
215 Ada.Text_IO.Put_Line ("Parameter missing for : "
216 & GNAT.Command_Line.Full_Switch);
217 Write_Usage;
219 when Xref_Lib.Invalid_Argument =>
220 Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
221 Write_Usage;
222 end Parse_Cmd_Line;
224 -----------------
225 -- Write_Usage --
226 -----------------
228 procedure Write_Usage is
229 begin
230 Put_Line ("GNATFIND " & Gnatvsn.Gnat_Version_String
231 & " Copyright 1998-2002, Ada Core Technologies Inc.");
232 Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
233 & "[file1 file2 ...]");
234 New_Line;
235 Put_Line (" pattern Name of the entity to look for (can have "
236 & "wildcards)");
237 Put_Line (" sourcefile Only find entities referenced from this "
238 & "file");
239 Put_Line (" line Only find entities referenced from this line "
240 & "of file");
241 Put_Line (" column Only find entities referenced from this columns"
242 & " of file");
243 Put_Line (" file ... Set of Ada source files to search for "
244 & "references. This parameters are optional");
245 New_Line;
246 Put_Line ("gnatfind switches:");
247 Put_Line (" -a Consider all files, even when the ali file is "
248 & "readonly");
249 Put_Line (" -aIdir Specify source files search path");
250 Put_Line (" -aOdir Specify library/object files search path");
251 Put_Line (" -d Output derived type information");
252 Put_Line (" -e Use the full regular expression set for "
253 & "pattern");
254 Put_Line (" -f Output full path name");
255 Put_Line (" -g Output information only for global symbols");
256 Put_Line (" -Idir Like -aIdir -aOdir");
257 Put_Line (" -nostdinc Don't look for sources in the system default"
258 & " directory");
259 Put_Line (" -nostdlib Don't look for library files in the system"
260 & " default directory");
261 Put_Line (" --RTS=dir specify the default source and object search"
262 & " path");
263 Put_Line (" -p file Use file as the default project file");
264 Put_Line (" -r Find all references (default to find declaration"
265 & " only)");
266 Put_Line (" -s Print source line");
267 Put_Line (" -t Print type hierarchy");
268 New_Line;
270 raise Usage_Error;
271 end Write_Usage;
273 -- Start of processing for Gnatfind
275 begin
276 Parse_Cmd_Line;
278 if not Have_Entity then
279 Write_Usage;
280 end if;
282 -- Special case to speed things up: if the user has a command line of the
283 -- form 'gnatfind entity:file', ie has specified a file and only wants the
284 -- bodies and specs, then we can restrict the search to the .ali file
285 -- associated with 'file'.
287 if Has_File_In_Entity
288 and then not Output_Ref
289 then
290 Wide_Search := False;
291 end if;
293 -- Find the project file
295 if Prj_File_Length = 0 then
296 Xr_Tabls.Create_Project_File (Default_Project_File ("."));
297 else
298 Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
299 end if;
301 -- Fill up the table
303 if Type_Tree and then Nb_File > 1 then
304 Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
305 & "specify only one file.");
306 Ada.Text_IO.New_Line;
307 Write_Usage;
308 end if;
310 Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
311 Der_Info, Type_Tree);
313 if Source_Lines then
314 Xr_Tabls.Grep_Source_Files;
315 end if;
317 Print_Gnatfind (Output_Ref, Full_Path_Name);
319 exception
320 when Usage_Error =>
321 null;
322 end Gnatfind;