OSDN Git Service

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