1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2009, 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 EXT_Specified : String_Access := null;
68 -- Used to detect multiple use of --ext= switch
70 procedure Parse_Cmd_Line;
71 -- Parse every switch on the command line
76 procedure Write_Usage;
77 -- Print a small help page for program usage and exit program
83 procedure Parse_Cmd_Line is
85 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
87 -- Start of processing for Parse_Cmd_Line
90 -- First check for --version or --help
92 Check_Version_And_Help ("GNATFIND", "1998");
94 -- Now scan the other switches
96 GNAT.Command_Line.Initialize_Option_Scan;
100 GNAT.Command_Line.Getopt
101 ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS= -ext=")
107 if GNAT.Command_Line.Full_Switch = "a" then
109 elsif GNAT.Command_Line.Full_Switch = "aI" then
110 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
112 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
122 Full_Path_Name := True;
125 Local_Symbols := False;
131 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
132 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
135 if GNAT.Command_Line.Full_Switch = "nostdinc" then
136 Opt.No_Stdinc := True;
137 elsif GNAT.Command_Line.Full_Switch = "nostdlib" then
138 Opt.No_Stdlib := True;
143 S : constant String := GNAT.Command_Line.Parameter;
145 Prj_File_Length := S'Length;
146 Prj_File (1 .. Prj_File_Length) := S;
153 Source_Lines := True;
158 -- Only switch starting with -- recognized is --RTS
161 if GNAT.Command_Line.Full_Switch = "-RTS" then
163 -- Check that it is the first time we see this switch
165 if RTS_Specified = null then
166 RTS_Specified := new String'(GNAT.Command_Line.Parameter);
167 elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
168 Osint.Fail ("--RTS cannot be specified multiple times");
171 Opt.No_Stdinc := True;
172 Opt.RTS_Switch := True;
175 Src_Path_Name : constant String_Ptr :=
177 (GNAT.Command_Line.Parameter,
179 Lib_Path_Name : constant String_Ptr :=
181 (GNAT.Command_Line.Parameter,
185 if Src_Path_Name /= null
186 and then Lib_Path_Name /= null
188 Add_Search_Dirs (Src_Path_Name, Include);
189 Add_Search_Dirs (Lib_Path_Name, Objects);
191 elsif Src_Path_Name = null
192 and then Lib_Path_Name = null
194 Osint.Fail ("RTS path not valid: missing " &
195 "adainclude and adalib directories");
197 elsif Src_Path_Name = null then
198 Osint.Fail ("RTS path not valid: missing " &
199 "adainclude directory");
201 elsif Lib_Path_Name = null then
202 Osint.Fail ("RTS path not valid: missing " &
207 -- Process -ext switch
209 elsif GNAT.Command_Line.Full_Switch = "-ext" then
211 -- Check that it is the first time we see this switch
213 if EXT_Specified = null then
214 EXT_Specified := new String'(GNAT.Command_Line.Parameter);
215 elsif EXT_Specified.all /= GNAT.Command_Line.Parameter then
216 Osint.Fail ("--ext cannot be specified multiple times");
220 EXT_Specified'Length = Osint.ALI_Default_Suffix'Length
222 Osint.ALI_Suffix := EXT_Specified.all'Access;
224 Osint.Fail ("--ext argument must have 3 characters");
234 -- Get the other arguments
238 S : constant String := GNAT.Command_Line.Get_Argument;
241 exit when S'Length = 0;
243 -- First argument is the pattern
245 if not Have_Entity then
246 Add_Entity (Pattern, S, Glob_Mode);
249 if not Has_File_In_Entity
250 and then Index (S, ":") /= 0
252 Has_File_In_Entity := True;
255 -- Next arguments are the files to search
259 Wide_Search := False;
260 Nb_File := Nb_File + 1;
266 when GNAT.Command_Line.Invalid_Switch =>
267 Ada.Text_IO.Put_Line ("Invalid switch : "
268 & GNAT.Command_Line.Full_Switch);
271 when GNAT.Command_Line.Invalid_Parameter =>
272 Ada.Text_IO.Put_Line ("Parameter missing for : "
273 & GNAT.Command_Line.Full_Switch);
276 when Xref_Lib.Invalid_Argument =>
277 Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
287 Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
288 & "[file1 file2 ...]");
290 Put_Line (" pattern Name of the entity to look for (can have "
292 Put_Line (" sourcefile Only find entities referenced from this "
294 Put_Line (" line Only find entities referenced from this line "
296 Put_Line (" column Only find entities referenced from this columns"
298 Put_Line (" file ... Set of Ada source files to search for "
299 & "references. This parameters are optional");
301 Put_Line ("gnatfind switches:");
302 Put_Line (" -a Consider all files, even when the ali file is "
304 Put_Line (" -aIdir Specify source files search path");
305 Put_Line (" -aOdir Specify library/object files search path");
306 Put_Line (" -d Output derived type information");
307 Put_Line (" -e Use the full regular expression set for "
309 Put_Line (" -f Output full path name");
310 Put_Line (" -g Output information only for global symbols");
311 Put_Line (" -Idir Like -aIdir -aOdir");
312 Put_Line (" -nostdinc Don't look for sources in the system default"
314 Put_Line (" -nostdlib Don't look for library files in the system"
315 & " default directory");
316 Put_Line (" --ext=xxx Specify alternate ali file extension");
317 Put_Line (" --RTS=dir specify the default source and object search"
319 Put_Line (" -p file Use file as the default project file");
320 Put_Line (" -r Find all references (default to find declaration"
322 Put_Line (" -s Print source line");
323 Put_Line (" -t Print type hierarchy");
330 procedure Write_Usage is
332 Display_Version ("GNATFIND", "1998");
340 -- Start of processing for Gnatfind
345 if not Have_Entity then
349 -- Special case to speed things up: if the user has a command line of the
350 -- form 'gnatfind entity:file', i.e. has specified a file and only wants
351 -- the bodies and specs, then we can restrict the search to the .ali file
352 -- associated with 'file'.
354 if Has_File_In_Entity
355 and then not Output_Ref
357 Wide_Search := False;
360 -- Find the project file
362 if Prj_File_Length = 0 then
363 Xr_Tabls.Create_Project_File (Default_Project_File ("."));
365 Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
370 if Type_Tree and then Nb_File > 1 then
371 Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
372 & "specify only one file.");
373 Ada.Text_IO.New_Line;
377 Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
378 Der_Info, Type_Tree);
381 Xr_Tabls.Grep_Source_Files;
384 Print_Gnatfind (Output_Ref, Full_Path_Name);