OSDN Git Service

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