1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2007, Free Software Foundation, Inc. --
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. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
35 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
36 with Ada.Text_IO; use Ada.Text_IO;
38 with GNAT.Command_Line; use GNAT.Command_Line;
40 with System.Strings; use System.Strings;
47 Output_Ref : Boolean := False;
48 Pattern : Xref_Lib.Search_Pattern;
49 Local_Symbols : Boolean := True;
50 Prj_File : File_Name_String;
51 Prj_File_Length : Natural := 0;
52 Nb_File : Natural := 0;
53 Usage_Error : exception;
54 Full_Path_Name : Boolean := False;
55 Have_Entity : Boolean := False;
56 Wide_Search : Boolean := True;
57 Glob_Mode : Boolean := True;
58 Der_Info : Boolean := False;
59 Type_Tree : Boolean := False;
60 Read_Only : Boolean := False;
61 Source_Lines : Boolean := False;
63 Has_File_In_Entity : Boolean := False;
64 -- Will be true if a file name was specified in the entity
66 RTS_Specified : String_Access := null;
67 -- Used to detect multiple use of --RTS= switch
69 procedure Parse_Cmd_Line;
70 -- Parse every switch on the command line
72 procedure Write_Usage;
73 -- Print a small help page for program usage
79 procedure Parse_Cmd_Line is
83 GNAT.Command_Line.Getopt
84 ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS=")
90 if GNAT.Command_Line.Full_Switch = "a" then
92 elsif GNAT.Command_Line.Full_Switch = "aI" then
93 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
95 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
105 Full_Path_Name := True;
108 Local_Symbols := False;
114 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
115 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
118 if GNAT.Command_Line.Full_Switch = "nostdinc" then
119 Opt.No_Stdinc := True;
120 elsif GNAT.Command_Line.Full_Switch = "nostlib" then
121 Opt.No_Stdlib := True;
126 S : constant String := GNAT.Command_Line.Parameter;
128 Prj_File_Length := S'Length;
129 Prj_File (1 .. Prj_File_Length) := S;
136 Source_Lines := True;
141 -- Only switch starting with -- recognized is --RTS
144 -- Check that it is the first time we see this switch
146 if RTS_Specified = null then
147 RTS_Specified := new String'(GNAT.Command_Line.Parameter);
149 elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
150 Osint.Fail ("--RTS cannot be specified multiple times");
153 Opt.No_Stdinc := True;
154 Opt.RTS_Switch := True;
157 Src_Path_Name : constant String_Ptr :=
159 (GNAT.Command_Line.Parameter, Include);
160 Lib_Path_Name : constant String_Ptr :=
162 (GNAT.Command_Line.Parameter, Objects);
165 if Src_Path_Name /= null and then Lib_Path_Name /= null then
166 Add_Search_Dirs (Src_Path_Name, Include);
167 Add_Search_Dirs (Lib_Path_Name, Objects);
169 elsif Src_Path_Name = null and then Lib_Path_Name = null then
170 Osint.Fail ("RTS path not valid: missing " &
171 "adainclude and adalib directories");
173 elsif Src_Path_Name = null then
174 Osint.Fail ("RTS path not valid: missing " &
175 "adainclude directory");
177 elsif Lib_Path_Name = null then
178 Osint.Fail ("RTS path not valid: missing " &
188 -- Get the other arguments
192 S : constant String := GNAT.Command_Line.Get_Argument;
195 exit when S'Length = 0;
197 -- First argument is the pattern
199 if not Have_Entity then
200 Add_Entity (Pattern, S, Glob_Mode);
203 if not Has_File_In_Entity
204 and then Index (S, ":") /= 0
206 Has_File_In_Entity := True;
209 -- Next arguments are the files to search
213 Wide_Search := False;
214 Nb_File := Nb_File + 1;
220 when GNAT.Command_Line.Invalid_Switch =>
221 Ada.Text_IO.Put_Line ("Invalid switch : "
222 & GNAT.Command_Line.Full_Switch);
225 when GNAT.Command_Line.Invalid_Parameter =>
226 Ada.Text_IO.Put_Line ("Parameter missing for : "
227 & GNAT.Command_Line.Full_Switch);
230 when Xref_Lib.Invalid_Argument =>
231 Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
239 procedure Write_Usage is
241 Put_Line ("GNATFIND " & Gnatvsn.Gnat_Version_String);
242 Put_Line ("Copyright 1998-2005, AdaCore");
243 Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
244 & "[file1 file2 ...]");
246 Put_Line (" pattern Name of the entity to look for (can have "
248 Put_Line (" sourcefile Only find entities referenced from this "
250 Put_Line (" line Only find entities referenced from this line "
252 Put_Line (" column Only find entities referenced from this columns"
254 Put_Line (" file ... Set of Ada source files to search for "
255 & "references. This parameters are optional");
257 Put_Line ("gnatfind switches:");
258 Put_Line (" -a Consider all files, even when the ali file is "
260 Put_Line (" -aIdir Specify source files search path");
261 Put_Line (" -aOdir Specify library/object files search path");
262 Put_Line (" -d Output derived type information");
263 Put_Line (" -e Use the full regular expression set for "
265 Put_Line (" -f Output full path name");
266 Put_Line (" -g Output information only for global symbols");
267 Put_Line (" -Idir Like -aIdir -aOdir");
268 Put_Line (" -nostdinc Don't look for sources in the system default"
270 Put_Line (" -nostdlib Don't look for library files in the system"
271 & " default directory");
272 Put_Line (" --RTS=dir specify the default source and object search"
274 Put_Line (" -p file Use file as the default project file");
275 Put_Line (" -r Find all references (default to find declaration"
277 Put_Line (" -s Print source line");
278 Put_Line (" -t Print type hierarchy");
284 -- Start of processing for Gnatfind
289 if not Have_Entity then
293 -- Special case to speed things up: if the user has a command line of the
294 -- form 'gnatfind entity:file', ie has specified a file and only wants the
295 -- bodies and specs, then we can restrict the search to the .ali file
296 -- associated with 'file'.
298 if Has_File_In_Entity
299 and then not Output_Ref
301 Wide_Search := False;
304 -- Find the project file
306 if Prj_File_Length = 0 then
307 Xr_Tabls.Create_Project_File (Default_Project_File ("."));
309 Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
314 if Type_Tree and then Nb_File > 1 then
315 Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
316 & "specify only one file.");
317 Ada.Text_IO.New_Line;
321 Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
322 Der_Info, Type_Tree);
325 Xr_Tabls.Grep_Source_Files;
328 Print_Gnatfind (Output_Ref, Full_Path_Name);