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