OSDN Git Service

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