OSDN Git Service

PR bootstrap/11932
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatfind.adb
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-2004 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 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,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Xr_Tabls; use Xr_Tabls;
28 with Xref_Lib; use Xref_Lib;
29 with Osint;    use Osint;
30 with Types;    use Types;
31
32 with Gnatvsn;
33 with Opt;
34
35 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
36 with Ada.Text_IO;       use Ada.Text_IO;
37 with GNAT.Command_Line; use GNAT.Command_Line;
38 with GNAT.Strings;      use GNAT.Strings;
39 ---------------
40 --  Gnatfind --
41 ---------------
42
43 procedure Gnatfind is
44    Output_Ref      : Boolean := False;
45    Pattern         : Xref_Lib.Search_Pattern;
46    Local_Symbols   : Boolean := True;
47    Prj_File        : File_Name_String;
48    Prj_File_Length : Natural := 0;
49    Nb_File         : Natural := 0;
50    Usage_Error     : exception;
51    Full_Path_Name  : Boolean := False;
52    Have_Entity     : Boolean := False;
53    Wide_Search     : Boolean := True;
54    Glob_Mode       : Boolean := True;
55    Der_Info        : Boolean := False;
56    Type_Tree       : Boolean := False;
57    Read_Only       : Boolean := False;
58    Source_Lines    : Boolean := False;
59
60    Has_File_In_Entity : Boolean := False;
61    --  Will be true if a file name was specified in the entity
62
63    RTS_Specified : String_Access := null;
64    --  Used to detect multiple use of --RTS= switch
65
66    procedure Parse_Cmd_Line;
67    --  Parse every switch on the command line
68
69    procedure Write_Usage;
70    --  Print a small help page for program usage
71
72    --------------------
73    -- Parse_Cmd_Line --
74    --------------------
75
76    procedure Parse_Cmd_Line is
77    begin
78       loop
79          case
80            GNAT.Command_Line.Getopt
81              ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS=")
82          is
83             when ASCII.NUL =>
84                exit;
85
86             when 'a'    =>
87                if GNAT.Command_Line.Full_Switch = "a" then
88                   Read_Only := True;
89
90                elsif GNAT.Command_Line.Full_Switch = "aI" then
91                   Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
92
93                else
94                   Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
95                end if;
96
97             when 'd'    =>
98                Der_Info := True;
99
100             when 'e'    =>
101                Glob_Mode := False;
102
103             when 'f'    =>
104                Full_Path_Name := True;
105
106             when 'g'    =>
107                Local_Symbols := False;
108
109             when 'h'    =>
110                Write_Usage;
111
112             when 'I'    =>
113                Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
114                Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
115
116             when 'n'    =>
117                if GNAT.Command_Line.Full_Switch = "nostdinc" then
118                   Opt.No_Stdinc := True;
119
120                elsif GNAT.Command_Line.Full_Switch = "nostlib" then
121                   Opt.No_Stdlib := True;
122                end if;
123
124             when 'p'    =>
125                declare
126                   S : constant String := GNAT.Command_Line.Parameter;
127
128                begin
129                   Prj_File_Length := S'Length;
130                   Prj_File (1 .. Prj_File_Length) := S;
131                end;
132
133             when 'r'    =>
134                Output_Ref := True;
135
136             when 's' =>
137                Source_Lines := True;
138
139             when 't' =>
140                Type_Tree := True;
141
142             --  Only switch starting with -- recognized is --RTS
143
144             when '-'    =>
145                --  Check that it is the first time we see this switch
146
147                if RTS_Specified = null then
148                   RTS_Specified := new String'(GNAT.Command_Line.Parameter);
149
150                elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
151                   Osint.Fail ("--RTS cannot be specified multiple times");
152                end if;
153
154                Opt.No_Stdinc := True;
155                Opt.RTS_Switch := True;
156
157                declare
158                   Src_Path_Name : constant String_Ptr :=
159                                     Get_RTS_Search_Dir
160                                       (GNAT.Command_Line.Parameter, Include);
161                   Lib_Path_Name : constant String_Ptr :=
162                                     Get_RTS_Search_Dir
163                                       (GNAT.Command_Line.Parameter, Objects);
164
165                begin
166                   if Src_Path_Name /= null and then Lib_Path_Name /= null then
167                      Add_Search_Dirs (Src_Path_Name, Include);
168                      Add_Search_Dirs (Lib_Path_Name, Objects);
169
170                   elsif Src_Path_Name = null and then Lib_Path_Name = null then
171                      Osint.Fail ("RTS path not valid: missing " &
172                                  "adainclude and adalib directories");
173
174                   elsif Src_Path_Name = null then
175                      Osint.Fail ("RTS path not valid: missing " &
176                                  "adainclude directory");
177
178                   elsif Lib_Path_Name = null then
179                      Osint.Fail ("RTS path not valid: missing " &
180                                  "adalib directory");
181                   end if;
182                end;
183
184             when others =>
185                Write_Usage;
186          end case;
187       end loop;
188
189       --  Get the other arguments
190
191       loop
192          declare
193             S : constant String := GNAT.Command_Line.Get_Argument;
194
195          begin
196             exit when S'Length = 0;
197
198             --  First argument is the pattern
199
200             if not Have_Entity then
201                Add_Entity (Pattern, S, Glob_Mode);
202                Have_Entity := True;
203
204                if not Has_File_In_Entity
205                  and then Index (S, ":") /= 0
206                then
207                   Has_File_In_Entity := True;
208                end if;
209
210             --  Next arguments are the files to search
211             else
212                Add_Xref_File (S);
213                Wide_Search := False;
214                Nb_File := Nb_File + 1;
215             end if;
216          end;
217       end loop;
218
219    exception
220       when GNAT.Command_Line.Invalid_Switch =>
221          Ada.Text_IO.Put_Line ("Invalid switch : "
222                                & GNAT.Command_Line.Full_Switch);
223          Write_Usage;
224
225       when GNAT.Command_Line.Invalid_Parameter =>
226          Ada.Text_IO.Put_Line ("Parameter missing for : "
227                                & GNAT.Command_Line.Full_Switch);
228          Write_Usage;
229
230       when Xref_Lib.Invalid_Argument =>
231          Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
232          Write_Usage;
233    end Parse_Cmd_Line;
234
235    -----------------
236    -- Write_Usage --
237    -----------------
238
239    procedure Write_Usage is
240    begin
241       Put_Line ("GNATFIND " & Gnatvsn.Gnat_Version_String
242                 & " Copyright 1998-2004, Ada Core Technologies Inc.");
243       Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
244                 & "[file1 file2 ...]");
245       New_Line;
246       Put_Line ("  pattern     Name of the entity to look for (can have "
247                 & "wildcards)");
248       Put_Line ("  sourcefile  Only find entities referenced from this "
249                 & "file");
250       Put_Line ("  line        Only find entities referenced from this line "
251                 & "of file");
252       Put_Line ("  column      Only find entities referenced from this columns"
253                 & " of file");
254       Put_Line ("  file ...    Set of Ada source files to search for "
255                 & "references. This parameters are optional");
256       New_Line;
257       Put_Line ("gnatfind switches:");
258       Put_Line ("   -a        Consider all files, even when the ali file is "
259                 & "readonly");
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 "
264                 & "pattern");
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"
269                 & " directory");
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"
273                 & " path");
274       Put_Line ("   -p file   Use file as the default project file");
275       Put_Line ("   -r        Find all references (default to find declaration"
276                 & " only)");
277       Put_Line ("   -s        Print source line");
278       Put_Line ("   -t        Print type hierarchy");
279       New_Line;
280
281       raise Usage_Error;
282    end Write_Usage;
283
284 --  Start of processing for Gnatfind
285
286 begin
287    Parse_Cmd_Line;
288
289    if not Have_Entity then
290       Write_Usage;
291    end if;
292
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'.
297
298    if Has_File_In_Entity
299      and then not Output_Ref
300    then
301       Wide_Search := False;
302    end if;
303
304    --  Find the project file
305
306    if Prj_File_Length = 0 then
307       Xr_Tabls.Create_Project_File (Default_Project_File ("."));
308    else
309       Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
310    end if;
311
312    --  Fill up the table
313
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;
318       Write_Usage;
319    end if;
320
321    Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
322            Der_Info, Type_Tree);
323
324    if Source_Lines then
325       Xr_Tabls.Grep_Source_Files;
326    end if;
327
328    Print_Gnatfind (Output_Ref, Full_Path_Name);
329
330 exception
331    when Usage_Error =>
332       null;
333 end Gnatfind;