* gcc.c-torture/execute/20020307-1.c: New test.
[official-gcc.git] / gcc / ada / gnatfind.adb
blobf7ebf856a0c4ef88624e5b26330af2af2e5865d7
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T F I N D --
6 -- --
7 -- B o d y --
8 -- --
9 -- $Revision: 1.26 $
10 -- --
11 -- Copyright (C) 1998-2001 Free Software Foundation, Inc. --
12 -- --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 -- --
26 ------------------------------------------------------------------------------
28 with Xr_Tabls;
29 with Xref_Lib; use Xref_Lib;
30 with Ada.Text_IO;
31 with GNAT.Command_Line;
32 with Gnatvsn;
33 with Osint;
34 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
36 ---------------
37 -- Gnatfind --
38 ---------------
40 procedure Gnatfind is
42 Output_Ref : Boolean := False;
43 Pattern : Xref_Lib.Search_Pattern;
44 Local_Symbols : Boolean := True;
45 Prj_File : File_Name_String;
46 Prj_File_Length : Natural := 0;
47 Nb_File : Natural := 0;
48 Usage_Error : exception;
49 Full_Path_Name : Boolean := False;
50 Have_Entity : Boolean := False;
51 Wide_Search : Boolean := True;
52 Glob_Mode : Boolean := True;
53 Der_Info : Boolean := False;
54 Type_Tree : Boolean := False;
55 Read_Only : Boolean := False;
56 Source_Lines : Boolean := False;
58 Has_File_In_Entity : Boolean := False;
59 -- Will be true if a file name was specified in the entity
61 procedure Parse_Cmd_Line;
62 -- Parse every switch on the command line
64 procedure Write_Usage;
65 -- Print a small help page for program usage
67 --------------------
68 -- Parse_Cmd_Line --
69 --------------------
71 procedure Parse_Cmd_Line is
72 begin
73 loop
74 case GNAT.Command_Line.Getopt ("a aI: aO: d e f g h I: p: r s t") is
75 when ASCII.NUL =>
76 exit;
78 when 'a' =>
79 if GNAT.Command_Line.Full_Switch = "a" then
80 Read_Only := True;
81 elsif GNAT.Command_Line.Full_Switch = "aI" then
82 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
83 else
84 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
85 end if;
87 when 'd' =>
88 Der_Info := True;
90 when 'e' =>
91 Glob_Mode := False;
93 when 'f' =>
94 Full_Path_Name := True;
96 when 'g' =>
97 Local_Symbols := False;
99 when 'h' =>
100 Write_Usage;
102 when 'I' =>
103 Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
104 Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
106 when 'p' =>
107 declare
108 S : constant String := GNAT.Command_Line.Parameter;
109 begin
110 Prj_File_Length := S'Length;
111 Prj_File (1 .. Prj_File_Length) := S;
112 end;
114 when 'r' =>
115 Output_Ref := True;
117 when 's' =>
118 Source_Lines := True;
120 when 't' =>
121 Type_Tree := True;
123 when others =>
124 Write_Usage;
125 end case;
126 end loop;
128 -- Get the other arguments
130 loop
131 declare
132 S : constant String := GNAT.Command_Line.Get_Argument;
133 begin
134 exit when S'Length = 0;
136 -- First argument is the pattern
138 if not Have_Entity then
139 Add_Entity (Pattern, S, Glob_Mode);
140 Have_Entity := True;
142 if not Has_File_In_Entity
143 and then Index (S, ":") /= 0
144 then
145 Has_File_In_Entity := True;
146 end if;
148 -- Next arguments are the files to search
149 else
150 Add_File (S);
151 Wide_Search := False;
152 Nb_File := Nb_File + 1;
153 end if;
154 end;
155 end loop;
157 exception
158 when GNAT.Command_Line.Invalid_Switch =>
159 Ada.Text_IO.Put_Line ("Invalid switch : "
160 & GNAT.Command_Line.Full_Switch);
161 Write_Usage;
163 when GNAT.Command_Line.Invalid_Parameter =>
164 Ada.Text_IO.Put_Line ("Parameter missing for : "
165 & GNAT.Command_Line.Parameter);
166 Write_Usage;
168 when Xref_Lib.Invalid_Argument =>
169 Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
170 Write_Usage;
171 end Parse_Cmd_Line;
173 -----------------
174 -- Write_Usage --
175 -----------------
177 procedure Write_Usage is
178 use Ada.Text_IO;
180 begin
181 Put_Line ("GNATFIND " & Gnatvsn.Gnat_Version_String
182 & " Copyright 1998-2001, Ada Core Technologies Inc.");
183 Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
184 & "[file1 file2 ...]");
185 New_Line;
186 Put_Line (" pattern Name of the entity to look for (can have "
187 & "wildcards)");
188 Put_Line (" sourcefile Only find entities referenced from this "
189 & "file");
190 Put_Line (" line Only find entities referenced from this line "
191 & "of file");
192 Put_Line (" column Only find entities referenced from this columns"
193 & " of file");
194 Put_Line (" file ... Set of Ada source files to search for "
195 & "references. This parameters are optional");
196 New_Line;
197 Put_Line ("gnatfind switches:");
198 Put_Line (" -a Consider all files, even when the ali file is "
199 & "readonly");
200 Put_Line (" -aIdir Specify source files search path");
201 Put_Line (" -aOdir Specify library/object files search path");
202 Put_Line (" -d Output derived type information");
203 Put_Line (" -e Use the full regular expression set for pattern");
204 Put_Line (" -f Output full path name");
205 Put_Line (" -g Output information only for global symbols");
206 Put_Line (" -Idir Like -aIdir -aOdir");
207 Put_Line (" -p file Use file as the default project file");
208 Put_Line (" -r Find all references (default to find declaration"
209 & " only)");
210 Put_Line (" -s Print source line");
211 Put_Line (" -t Print type hierarchy");
212 New_Line;
214 raise Usage_Error;
215 end Write_Usage;
217 begin
218 Osint.Initialize (Osint.Compiler);
220 Parse_Cmd_Line;
222 if not Have_Entity then
223 Write_Usage;
224 end if;
226 -- Special case to speed things up: if the user has a command line of the
227 -- form 'gnatfind entity:file', ie has specified a file and only wants the
228 -- bodies and specs, then we can restrict the search to the .ali file
229 -- associated with 'file'.
231 if Has_File_In_Entity
232 and then not Output_Ref
233 then
234 Wide_Search := False;
235 end if;
237 -- Find the project file
239 if Prj_File_Length = 0 then
240 Xr_Tabls.Create_Project_File (Default_Project_File ("."));
241 else
242 Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
243 end if;
245 -- Fill up the table
247 if Type_Tree and then Nb_File > 1 then
248 Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
249 & "specify only one file.");
250 Ada.Text_IO.New_Line;
251 Write_Usage;
252 end if;
254 Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
255 Der_Info, Type_Tree);
257 if Source_Lines then
258 Xr_Tabls.Grep_Source_Files;
259 end if;
261 Print_Gnatfind (Output_Ref, Full_Path_Name);
263 exception
264 when Usage_Error =>
265 null;
266 end Gnatfind;