2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / gnatfind.adb
blob5592d528a2cf808e04713a6dbc8a74011a72bbcd
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-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 ------------------------------------------------------------------------------
26 with Opt;
27 with Osint; use Osint;
28 with Switch; use Switch;
29 with Types; use Types;
30 with Xr_Tabls; use Xr_Tabls;
31 with Xref_Lib; use Xref_Lib;
33 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
34 with Ada.Text_IO; use Ada.Text_IO;
36 with GNAT.Command_Line; use GNAT.Command_Line;
38 with System.Strings; use System.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 Usage;
71 -- Display the usage
73 procedure Write_Usage;
74 -- Print a small help page for program usage and exit program
76 --------------------
77 -- Parse_Cmd_Line --
78 --------------------
80 procedure Parse_Cmd_Line is
82 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
84 -- Start of processing for Parse_Cmd_Line
86 begin
87 -- First check for --version or --help
89 Check_Version_And_Help ("GNATFIND", "1998");
91 -- Now scan the other switches
93 GNAT.Command_Line.Initialize_Option_Scan;
95 loop
96 case
97 GNAT.Command_Line.Getopt
98 ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS=")
100 when ASCII.NUL =>
101 exit;
103 when 'a' =>
104 if GNAT.Command_Line.Full_Switch = "a" then
105 Read_Only := True;
106 elsif GNAT.Command_Line.Full_Switch = "aI" then
107 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
108 else
109 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
110 end if;
112 when 'd' =>
113 Der_Info := True;
115 when 'e' =>
116 Glob_Mode := False;
118 when 'f' =>
119 Full_Path_Name := True;
121 when 'g' =>
122 Local_Symbols := False;
124 when 'h' =>
125 Write_Usage;
127 when 'I' =>
128 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
129 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
131 when 'n' =>
132 if GNAT.Command_Line.Full_Switch = "nostdinc" then
133 Opt.No_Stdinc := True;
134 elsif GNAT.Command_Line.Full_Switch = "nostdlib" then
135 Opt.No_Stdlib := True;
136 end if;
138 when 'p' =>
139 declare
140 S : constant String := GNAT.Command_Line.Parameter;
141 begin
142 Prj_File_Length := S'Length;
143 Prj_File (1 .. Prj_File_Length) := S;
144 end;
146 when 'r' =>
147 Output_Ref := True;
149 when 's' =>
150 Source_Lines := True;
152 when 't' =>
153 Type_Tree := True;
155 -- Only switch starting with -- recognized is --RTS
157 when '-' =>
158 -- Check that it is the first time we see this switch
160 if RTS_Specified = null then
161 RTS_Specified := new String'(GNAT.Command_Line.Parameter);
163 elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
164 Osint.Fail ("--RTS cannot be specified multiple times");
165 end if;
167 Opt.No_Stdinc := True;
168 Opt.RTS_Switch := True;
170 declare
171 Src_Path_Name : constant String_Ptr :=
172 Get_RTS_Search_Dir
173 (GNAT.Command_Line.Parameter, Include);
174 Lib_Path_Name : constant String_Ptr :=
175 Get_RTS_Search_Dir
176 (GNAT.Command_Line.Parameter, Objects);
178 begin
179 if Src_Path_Name /= null and then Lib_Path_Name /= null then
180 Add_Search_Dirs (Src_Path_Name, Include);
181 Add_Search_Dirs (Lib_Path_Name, Objects);
183 elsif Src_Path_Name = null and then Lib_Path_Name = null then
184 Osint.Fail ("RTS path not valid: missing " &
185 "adainclude and adalib directories");
187 elsif Src_Path_Name = null then
188 Osint.Fail ("RTS path not valid: missing " &
189 "adainclude directory");
191 elsif Lib_Path_Name = null then
192 Osint.Fail ("RTS path not valid: missing " &
193 "adalib directory");
194 end if;
195 end;
197 when others =>
198 Write_Usage;
199 end case;
200 end loop;
202 -- Get the other arguments
204 loop
205 declare
206 S : constant String := GNAT.Command_Line.Get_Argument;
208 begin
209 exit when S'Length = 0;
211 -- First argument is the pattern
213 if not Have_Entity then
214 Add_Entity (Pattern, S, Glob_Mode);
215 Have_Entity := True;
217 if not Has_File_In_Entity
218 and then Index (S, ":") /= 0
219 then
220 Has_File_In_Entity := True;
221 end if;
223 -- Next arguments are the files to search
225 else
226 Add_Xref_File (S);
227 Wide_Search := False;
228 Nb_File := Nb_File + 1;
229 end if;
230 end;
231 end loop;
233 exception
234 when GNAT.Command_Line.Invalid_Switch =>
235 Ada.Text_IO.Put_Line ("Invalid switch : "
236 & GNAT.Command_Line.Full_Switch);
237 Write_Usage;
239 when GNAT.Command_Line.Invalid_Parameter =>
240 Ada.Text_IO.Put_Line ("Parameter missing for : "
241 & GNAT.Command_Line.Full_Switch);
242 Write_Usage;
244 when Xref_Lib.Invalid_Argument =>
245 Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
246 Write_Usage;
247 end Parse_Cmd_Line;
249 -----------
250 -- Usage --
251 -----------
253 procedure Usage is
254 begin
255 Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
256 & "[file1 file2 ...]");
257 New_Line;
258 Put_Line (" pattern Name of the entity to look for (can have "
259 & "wildcards)");
260 Put_Line (" sourcefile Only find entities referenced from this "
261 & "file");
262 Put_Line (" line Only find entities referenced from this line "
263 & "of file");
264 Put_Line (" column Only find entities referenced from this columns"
265 & " of file");
266 Put_Line (" file ... Set of Ada source files to search for "
267 & "references. This parameters are optional");
268 New_Line;
269 Put_Line ("gnatfind switches:");
270 Put_Line (" -a Consider all files, even when the ali file is "
271 & "readonly");
272 Put_Line (" -aIdir Specify source files search path");
273 Put_Line (" -aOdir Specify library/object files search path");
274 Put_Line (" -d Output derived type information");
275 Put_Line (" -e Use the full regular expression set for "
276 & "pattern");
277 Put_Line (" -f Output full path name");
278 Put_Line (" -g Output information only for global symbols");
279 Put_Line (" -Idir Like -aIdir -aOdir");
280 Put_Line (" -nostdinc Don't look for sources in the system default"
281 & " directory");
282 Put_Line (" -nostdlib Don't look for library files in the system"
283 & " default directory");
284 Put_Line (" --RTS=dir specify the default source and object search"
285 & " path");
286 Put_Line (" -p file Use file as the default project file");
287 Put_Line (" -r Find all references (default to find declaration"
288 & " only)");
289 Put_Line (" -s Print source line");
290 Put_Line (" -t Print type hierarchy");
291 end Usage;
293 -----------------
294 -- Write_Usage --
295 -----------------
297 procedure Write_Usage is
298 begin
299 Display_Version ("GNATFIND", "1998");
300 New_Line;
302 Usage;
304 raise Usage_Error;
305 end Write_Usage;
307 -- Start of processing for Gnatfind
309 begin
310 Parse_Cmd_Line;
312 if not Have_Entity then
313 Write_Usage;
314 end if;
316 -- Special case to speed things up: if the user has a command line of the
317 -- form 'gnatfind entity:file', i.e. has specified a file and only wants
318 -- the bodies and specs, then we can restrict the search to the .ali file
319 -- associated with 'file'.
321 if Has_File_In_Entity
322 and then not Output_Ref
323 then
324 Wide_Search := False;
325 end if;
327 -- Find the project file
329 if Prj_File_Length = 0 then
330 Xr_Tabls.Create_Project_File (Default_Project_File ("."));
331 else
332 Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
333 end if;
335 -- Fill up the table
337 if Type_Tree and then Nb_File > 1 then
338 Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
339 & "specify only one file.");
340 Ada.Text_IO.New_Line;
341 Write_Usage;
342 end if;
344 Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
345 Der_Info, Type_Tree);
347 if Source_Lines then
348 Xr_Tabls.Grep_Source_Files;
349 end if;
351 Print_Gnatfind (Output_Ref, Full_Path_Name);
353 exception
354 when Usage_Error =>
355 null;
356 end Gnatfind;