OSDN Git Service

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