OSDN Git Service

2007-08-30 Vincent Celier <celier@adacore.com>
[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-2007, 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 Opt;
28 with Osint;    use Osint;
29 with Switch;   use Switch;
30 with Types;    use Types;
31 with Xr_Tabls; use Xr_Tabls;
32 with Xref_Lib; use Xref_Lib;
33
34 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
35 with Ada.Text_IO;       use Ada.Text_IO;
36
37 with GNAT.Command_Line; use GNAT.Command_Line;
38
39 with System.Strings;    use System.Strings;
40
41 --------------
42 -- Gnatfind --
43 --------------
44
45 procedure Gnatfind is
46    Output_Ref      : Boolean := False;
47    Pattern         : Xref_Lib.Search_Pattern;
48    Local_Symbols   : Boolean := True;
49    Prj_File        : File_Name_String;
50    Prj_File_Length : Natural := 0;
51    Nb_File         : Natural := 0;
52    Usage_Error     : exception;
53    Full_Path_Name  : Boolean := False;
54    Have_Entity     : Boolean := False;
55    Wide_Search     : Boolean := True;
56    Glob_Mode       : Boolean := True;
57    Der_Info        : Boolean := False;
58    Type_Tree       : Boolean := False;
59    Read_Only       : Boolean := False;
60    Source_Lines    : Boolean := False;
61
62    Has_File_In_Entity : Boolean := False;
63    --  Will be true if a file name was specified in the entity
64
65    RTS_Specified : String_Access := null;
66    --  Used to detect multiple use of --RTS= switch
67
68    procedure Parse_Cmd_Line;
69    --  Parse every switch on the command line
70
71    procedure Usage;
72    --  Display the usage
73
74    procedure Write_Usage;
75    --  Print a small help page for program usage and exit program
76
77    --------------------
78    -- Parse_Cmd_Line --
79    --------------------
80
81    procedure Parse_Cmd_Line is
82    begin
83       --  First check for --version or --help
84
85       Check_Version_And_Help ("GNATFIND", "1998", Usage'Unrestricted_Access);
86
87       --  Now scan the other switches
88
89       GNAT.Command_Line.Initialize_Option_Scan;
90
91       loop
92          case
93            GNAT.Command_Line.Getopt
94              ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS=")
95          is
96             when ASCII.NUL =>
97                exit;
98
99             when 'a'    =>
100                if GNAT.Command_Line.Full_Switch = "a" then
101                   Read_Only := True;
102                elsif GNAT.Command_Line.Full_Switch = "aI" then
103                   Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
104                else
105                   Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
106                end if;
107
108             when 'd'    =>
109                Der_Info := True;
110
111             when 'e'    =>
112                Glob_Mode := False;
113
114             when 'f'    =>
115                Full_Path_Name := True;
116
117             when 'g'    =>
118                Local_Symbols := False;
119
120             when 'h'    =>
121                Write_Usage;
122
123             when 'I'    =>
124                Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
125                Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
126
127             when 'n'    =>
128                if GNAT.Command_Line.Full_Switch = "nostdinc" then
129                   Opt.No_Stdinc := True;
130                elsif GNAT.Command_Line.Full_Switch = "nostlib" then
131                   Opt.No_Stdlib := True;
132                end if;
133
134             when 'p'    =>
135                declare
136                   S : constant String := GNAT.Command_Line.Parameter;
137                begin
138                   Prj_File_Length := S'Length;
139                   Prj_File (1 .. Prj_File_Length) := S;
140                end;
141
142             when 'r'    =>
143                Output_Ref := True;
144
145             when 's' =>
146                Source_Lines := True;
147
148             when 't' =>
149                Type_Tree := True;
150
151             --  Only switch starting with -- recognized is --RTS
152
153             when '-'    =>
154                --  Check that it is the first time we see this switch
155
156                if RTS_Specified = null then
157                   RTS_Specified := new String'(GNAT.Command_Line.Parameter);
158
159                elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
160                   Osint.Fail ("--RTS cannot be specified multiple times");
161                end if;
162
163                Opt.No_Stdinc := True;
164                Opt.RTS_Switch := True;
165
166                declare
167                   Src_Path_Name : constant String_Ptr :=
168                                     Get_RTS_Search_Dir
169                                       (GNAT.Command_Line.Parameter, Include);
170                   Lib_Path_Name : constant String_Ptr :=
171                                     Get_RTS_Search_Dir
172                                       (GNAT.Command_Line.Parameter, Objects);
173
174                begin
175                   if Src_Path_Name /= null and then Lib_Path_Name /= null then
176                      Add_Search_Dirs (Src_Path_Name, Include);
177                      Add_Search_Dirs (Lib_Path_Name, Objects);
178
179                   elsif Src_Path_Name = null and then Lib_Path_Name = null then
180                      Osint.Fail ("RTS path not valid: missing " &
181                                  "adainclude and adalib directories");
182
183                   elsif Src_Path_Name = null then
184                      Osint.Fail ("RTS path not valid: missing " &
185                                  "adainclude directory");
186
187                   elsif Lib_Path_Name = null then
188                      Osint.Fail ("RTS path not valid: missing " &
189                                  "adalib directory");
190                   end if;
191                end;
192
193             when others =>
194                Write_Usage;
195          end case;
196       end loop;
197
198       --  Get the other arguments
199
200       loop
201          declare
202             S : constant String := GNAT.Command_Line.Get_Argument;
203
204          begin
205             exit when S'Length = 0;
206
207             --  First argument is the pattern
208
209             if not Have_Entity then
210                Add_Entity (Pattern, S, Glob_Mode);
211                Have_Entity := True;
212
213                if not Has_File_In_Entity
214                  and then Index (S, ":") /= 0
215                then
216                   Has_File_In_Entity := True;
217                end if;
218
219             --  Next arguments are the files to search
220
221             else
222                Add_Xref_File (S);
223                Wide_Search := False;
224                Nb_File := Nb_File + 1;
225             end if;
226          end;
227       end loop;
228
229    exception
230       when GNAT.Command_Line.Invalid_Switch =>
231          Ada.Text_IO.Put_Line ("Invalid switch : "
232                                & GNAT.Command_Line.Full_Switch);
233          Write_Usage;
234
235       when GNAT.Command_Line.Invalid_Parameter =>
236          Ada.Text_IO.Put_Line ("Parameter missing for : "
237                                & GNAT.Command_Line.Full_Switch);
238          Write_Usage;
239
240       when Xref_Lib.Invalid_Argument =>
241          Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
242          Write_Usage;
243    end Parse_Cmd_Line;
244
245    -----------
246    -- Usage --
247    -----------
248
249    procedure Usage is
250    begin
251       Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
252                 & "[file1 file2 ...]");
253       New_Line;
254       Put_Line ("  pattern     Name of the entity to look for (can have "
255                 & "wildcards)");
256       Put_Line ("  sourcefile  Only find entities referenced from this "
257                 & "file");
258       Put_Line ("  line        Only find entities referenced from this line "
259                 & "of file");
260       Put_Line ("  column      Only find entities referenced from this columns"
261                 & " of file");
262       Put_Line ("  file ...    Set of Ada source files to search for "
263                 & "references. This parameters are optional");
264       New_Line;
265       Put_Line ("gnatfind switches:");
266       Put_Line ("   -a        Consider all files, even when the ali file is "
267                 & "readonly");
268       Put_Line ("   -aIdir    Specify source files search path");
269       Put_Line ("   -aOdir    Specify library/object files search path");
270       Put_Line ("   -d        Output derived type information");
271       Put_Line ("   -e        Use the full regular expression set for "
272                 & "pattern");
273       Put_Line ("   -f        Output full path name");
274       Put_Line ("   -g        Output information only for global symbols");
275       Put_Line ("   -Idir     Like -aIdir -aOdir");
276       Put_Line ("   -nostdinc Don't look for sources in the system default"
277                 & " directory");
278       Put_Line ("   -nostdlib Don't look for library files in the system"
279                 & " default directory");
280       Put_Line ("   --RTS=dir specify the default source and object search"
281                 & " path");
282       Put_Line ("   -p file   Use file as the default project file");
283       Put_Line ("   -r        Find all references (default to find declaration"
284                 & " only)");
285       Put_Line ("   -s        Print source line");
286       Put_Line ("   -t        Print type hierarchy");
287    end Usage;
288
289    -----------------
290    -- Write_Usage --
291    -----------------
292
293    procedure Write_Usage is
294    begin
295       Display_Version ("GNATFIND", "1998");
296       New_Line;
297
298       Usage;
299
300       raise Usage_Error;
301    end Write_Usage;
302
303 --  Start of processing for Gnatfind
304
305 begin
306    Parse_Cmd_Line;
307
308    if not Have_Entity then
309       Write_Usage;
310    end if;
311
312    --  Special case to speed things up: if the user has a command line of the
313    --  form 'gnatfind entity:file', ie has specified a file and only wants the
314    --  bodies and specs, then we can restrict the search to the .ali file
315    --  associated with 'file'.
316
317    if Has_File_In_Entity
318      and then not Output_Ref
319    then
320       Wide_Search := False;
321    end if;
322
323    --  Find the project file
324
325    if Prj_File_Length = 0 then
326       Xr_Tabls.Create_Project_File (Default_Project_File ("."));
327    else
328       Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
329    end if;
330
331    --  Fill up the table
332
333    if Type_Tree and then Nb_File > 1 then
334       Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
335                             & "specify only one file.");
336       Ada.Text_IO.New_Line;
337       Write_Usage;
338    end if;
339
340    Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
341            Der_Info, Type_Tree);
342
343    if Source_Lines then
344       Xr_Tabls.Grep_Source_Files;
345    end if;
346
347    Print_Gnatfind (Output_Ref, Full_Path_Name);
348
349 exception
350    when Usage_Error =>
351       null;
352 end Gnatfind;