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 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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
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
;
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
73 procedure Write_Usage
;
74 -- Print a small help page for program usage and exit program
80 procedure Parse_Cmd_Line
is
82 -- First check for --version or --help
84 Check_Version_And_Help
("GNATFIND", "1998", Usage
'Unrestricted_Access);
86 -- Now scan the other switches
88 GNAT
.Command_Line
.Initialize_Option_Scan
;
92 GNAT
.Command_Line
.Getopt
93 ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS=")
99 if GNAT
.Command_Line
.Full_Switch
= "a" then
101 elsif GNAT
.Command_Line
.Full_Switch
= "aI" then
102 Osint
.Add_Src_Search_Dir
(GNAT
.Command_Line
.Parameter
);
104 Osint
.Add_Lib_Search_Dir
(GNAT
.Command_Line
.Parameter
);
114 Full_Path_Name
:= True;
117 Local_Symbols
:= False;
123 Osint
.Add_Src_Search_Dir
(GNAT
.Command_Line
.Parameter
);
124 Osint
.Add_Lib_Search_Dir
(GNAT
.Command_Line
.Parameter
);
127 if GNAT
.Command_Line
.Full_Switch
= "nostdinc" then
128 Opt
.No_Stdinc
:= True;
129 elsif GNAT
.Command_Line
.Full_Switch
= "nostlib" then
130 Opt
.No_Stdlib
:= True;
135 S
: constant String := GNAT
.Command_Line
.Parameter
;
137 Prj_File_Length
:= S
'Length;
138 Prj_File
(1 .. Prj_File_Length
) := S
;
145 Source_Lines
:= True;
150 -- Only switch starting with -- recognized is --RTS
153 -- Check that it is the first time we see this switch
155 if RTS_Specified
= null then
156 RTS_Specified
:= new String'(GNAT.Command_Line.Parameter);
158 elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
159 Osint.Fail ("--RTS cannot be specified multiple times");
162 Opt.No_Stdinc := True;
163 Opt.RTS_Switch := True;
166 Src_Path_Name : constant String_Ptr :=
168 (GNAT.Command_Line.Parameter, Include);
169 Lib_Path_Name : constant String_Ptr :=
171 (GNAT.Command_Line.Parameter, Objects);
174 if Src_Path_Name /= null and then Lib_Path_Name /= null then
175 Add_Search_Dirs (Src_Path_Name, Include);
176 Add_Search_Dirs (Lib_Path_Name, Objects);
178 elsif Src_Path_Name = null and then Lib_Path_Name = null then
179 Osint.Fail ("RTS path not valid: missing " &
180 "adainclude and adalib directories");
182 elsif Src_Path_Name = null then
183 Osint.Fail ("RTS path not valid: missing " &
184 "adainclude directory");
186 elsif Lib_Path_Name = null then
187 Osint.Fail ("RTS path not valid: missing " &
197 -- Get the other arguments
201 S : constant String := GNAT.Command_Line.Get_Argument;
204 exit when S'Length = 0;
206 -- First argument is the pattern
208 if not Have_Entity then
209 Add_Entity (Pattern, S, Glob_Mode);
212 if not Has_File_In_Entity
213 and then Index (S, ":") /= 0
215 Has_File_In_Entity := True;
218 -- Next arguments are the files to search
222 Wide_Search := False;
223 Nb_File := Nb_File + 1;
229 when GNAT.Command_Line.Invalid_Switch =>
230 Ada.Text_IO.Put_Line ("Invalid switch : "
231 & GNAT.Command_Line.Full_Switch);
234 when GNAT.Command_Line.Invalid_Parameter =>
235 Ada.Text_IO.Put_Line ("Parameter missing for : "
236 & GNAT.Command_Line.Full_Switch);
239 when Xref_Lib.Invalid_Argument =>
240 Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
250 Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
251 & "[file1 file2 ...]");
253 Put_Line (" pattern Name of the entity to look for (can have "
255 Put_Line (" sourcefile Only find entities referenced from this "
257 Put_Line (" line Only find entities referenced from this line "
259 Put_Line (" column Only find entities referenced from this columns"
261 Put_Line (" file ... Set of Ada source files to search for "
262 & "references. This parameters are optional");
264 Put_Line ("gnatfind switches:");
265 Put_Line (" -a Consider all files, even when the ali file is "
267 Put_Line (" -aIdir Specify source files search path");
268 Put_Line (" -aOdir Specify library/object files search path");
269 Put_Line (" -d Output derived type information");
270 Put_Line (" -e Use the full regular expression set for "
272 Put_Line (" -f Output full path name");
273 Put_Line (" -g Output information only for global symbols");
274 Put_Line (" -Idir Like -aIdir -aOdir");
275 Put_Line (" -nostdinc Don't look for sources in the system default"
277 Put_Line (" -nostdlib Don't look for library files in the system"
278 & " default directory");
279 Put_Line (" --RTS=dir specify the default source and object search"
281 Put_Line (" -p file Use file as the default project file");
282 Put_Line (" -r Find all references (default to find declaration"
284 Put_Line (" -s Print source line");
285 Put_Line (" -t Print type hierarchy");
292 procedure Write_Usage is
294 Display_Version ("GNATFIND", "1998");
302 -- Start of processing for Gnatfind
307 if not Have_Entity then
311 -- Special case to speed things up: if the user has a command line of the
312 -- form 'gnatfind entity
:file
', ie has specified a file and only wants the
313 -- bodies and specs, then we can restrict the search to the .ali file
314 -- associated with 'file
'.
316 if Has_File_In_Entity
317 and then not Output_Ref
319 Wide_Search := False;
322 -- Find the project file
324 if Prj_File_Length = 0 then
325 Xr_Tabls.Create_Project_File (Default_Project_File ("."));
327 Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
332 if Type_Tree and then Nb_File > 1 then
333 Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
334 & "specify only one file.");
335 Ada.Text_IO.New_Line;
339 Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
340 Der_Info, Type_Tree);
343 Xr_Tabls.Grep_Source_Files;
346 Print_Gnatfind (Output_Ref, Full_Path_Name);