summaryrefslogtreecommitdiff
path: root/gcc/ada/gnatfind.adb
blob: 49b1e41419ea94e051bcfe3261e772eb0f354dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                             G N A T F I N D                              --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--         Copyright (C) 1998-2005 Free Software Foundation, Inc.           --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 2,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNAT;  see file COPYING.  If not, write --
-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
-- Boston, MA 02110-1301, USA.                                              --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

with Xr_Tabls; use Xr_Tabls;
with Xref_Lib; use Xref_Lib;
with Osint;    use Osint;
with Types;    use Types;

with Gnatvsn;
with Opt;

with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Text_IO;       use Ada.Text_IO;
with GNAT.Command_Line; use GNAT.Command_Line;
with GNAT.Strings;      use GNAT.Strings;

--------------
-- Gnatfind --
--------------

procedure Gnatfind is
   Output_Ref      : Boolean := False;
   Pattern         : Xref_Lib.Search_Pattern;
   Local_Symbols   : Boolean := True;
   Prj_File        : File_Name_String;
   Prj_File_Length : Natural := 0;
   Nb_File         : Natural := 0;
   Usage_Error     : exception;
   Full_Path_Name  : Boolean := False;
   Have_Entity     : Boolean := False;
   Wide_Search     : Boolean := True;
   Glob_Mode       : Boolean := True;
   Der_Info        : Boolean := False;
   Type_Tree       : Boolean := False;
   Read_Only       : Boolean := False;
   Source_Lines    : Boolean := False;

   Has_File_In_Entity : Boolean := False;
   --  Will be true if a file name was specified in the entity

   RTS_Specified : String_Access := null;
   --  Used to detect multiple use of --RTS= switch

   procedure Parse_Cmd_Line;
   --  Parse every switch on the command line

   procedure Write_Usage;
   --  Print a small help page for program usage

   --------------------
   -- Parse_Cmd_Line --
   --------------------

   procedure Parse_Cmd_Line is
   begin
      loop
         case
           GNAT.Command_Line.Getopt
             ("a aI: aO: d e f g h I: nostdinc nostdlib p: r s t -RTS=")
         is
            when ASCII.NUL =>
               exit;

            when 'a'    =>
               if GNAT.Command_Line.Full_Switch = "a" then
                  Read_Only := True;
               elsif GNAT.Command_Line.Full_Switch = "aI" then
                  Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
               else
                  Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
               end if;

            when 'd'    =>
               Der_Info := True;

            when 'e'    =>
               Glob_Mode := False;

            when 'f'    =>
               Full_Path_Name := True;

            when 'g'    =>
               Local_Symbols := False;

            when 'h'    =>
               Write_Usage;

            when 'I'    =>
               Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
               Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);

            when 'n'    =>
               if GNAT.Command_Line.Full_Switch = "nostdinc" then
                  Opt.No_Stdinc := True;
               elsif GNAT.Command_Line.Full_Switch = "nostlib" then
                  Opt.No_Stdlib := True;
               end if;

            when 'p'    =>
               declare
                  S : constant String := GNAT.Command_Line.Parameter;
               begin
                  Prj_File_Length := S'Length;
                  Prj_File (1 .. Prj_File_Length) := S;
               end;

            when 'r'    =>
               Output_Ref := True;

            when 's' =>
               Source_Lines := True;

            when 't' =>
               Type_Tree := True;

            --  Only switch starting with -- recognized is --RTS

            when '-'    =>
               --  Check that it is the first time we see this switch

               if RTS_Specified = null then
                  RTS_Specified := new String'(GNAT.Command_Line.Parameter);

               elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
                  Osint.Fail ("--RTS cannot be specified multiple times");
               end if;

               Opt.No_Stdinc := True;
               Opt.RTS_Switch := True;

               declare
                  Src_Path_Name : constant String_Ptr :=
                                    Get_RTS_Search_Dir
                                      (GNAT.Command_Line.Parameter, Include);
                  Lib_Path_Name : constant String_Ptr :=
                                    Get_RTS_Search_Dir
                                      (GNAT.Command_Line.Parameter, Objects);

               begin
                  if Src_Path_Name /= null and then Lib_Path_Name /= null then
                     Add_Search_Dirs (Src_Path_Name, Include);
                     Add_Search_Dirs (Lib_Path_Name, Objects);

                  elsif Src_Path_Name = null and then Lib_Path_Name = null then
                     Osint.Fail ("RTS path not valid: missing " &
                                 "adainclude and adalib directories");

                  elsif Src_Path_Name = null then
                     Osint.Fail ("RTS path not valid: missing " &
                                 "adainclude directory");

                  elsif Lib_Path_Name = null then
                     Osint.Fail ("RTS path not valid: missing " &
                                 "adalib directory");
                  end if;
               end;

            when others =>
               Write_Usage;
         end case;
      end loop;

      --  Get the other arguments

      loop
         declare
            S : constant String := GNAT.Command_Line.Get_Argument;

         begin
            exit when S'Length = 0;

            --  First argument is the pattern

            if not Have_Entity then
               Add_Entity (Pattern, S, Glob_Mode);
               Have_Entity := True;

               if not Has_File_In_Entity
                 and then Index (S, ":") /= 0
               then
                  Has_File_In_Entity := True;
               end if;

            --  Next arguments are the files to search

            else
               Add_Xref_File (S);
               Wide_Search := False;
               Nb_File := Nb_File + 1;
            end if;
         end;
      end loop;

   exception
      when GNAT.Command_Line.Invalid_Switch =>
         Ada.Text_IO.Put_Line ("Invalid switch : "
                               & GNAT.Command_Line.Full_Switch);
         Write_Usage;

      when GNAT.Command_Line.Invalid_Parameter =>
         Ada.Text_IO.Put_Line ("Parameter missing for : "
                               & GNAT.Command_Line.Full_Switch);
         Write_Usage;

      when Xref_Lib.Invalid_Argument =>
         Ada.Text_IO.Put_Line ("Invalid line or column in the pattern");
         Write_Usage;
   end Parse_Cmd_Line;

   -----------------
   -- Write_Usage --
   -----------------

   procedure Write_Usage is
   begin
      Put_Line ("GNATFIND " & Gnatvsn.Gnat_Version_String);
      Put_Line ("Copyright 1998-2005, AdaCore");
      Put_Line ("Usage: gnatfind pattern[:sourcefile[:line[:column]]] "
                & "[file1 file2 ...]");
      New_Line;
      Put_Line ("  pattern     Name of the entity to look for (can have "
                & "wildcards)");
      Put_Line ("  sourcefile  Only find entities referenced from this "
                & "file");
      Put_Line ("  line        Only find entities referenced from this line "
                & "of file");
      Put_Line ("  column      Only find entities referenced from this columns"
                & " of file");
      Put_Line ("  file ...    Set of Ada source files to search for "
                & "references. This parameters are optional");
      New_Line;
      Put_Line ("gnatfind switches:");
      Put_Line ("   -a        Consider all files, even when the ali file is "
                & "readonly");
      Put_Line ("   -aIdir    Specify source files search path");
      Put_Line ("   -aOdir    Specify library/object files search path");
      Put_Line ("   -d        Output derived type information");
      Put_Line ("   -e        Use the full regular expression set for "
                & "pattern");
      Put_Line ("   -f        Output full path name");
      Put_Line ("   -g        Output information only for global symbols");
      Put_Line ("   -Idir     Like -aIdir -aOdir");
      Put_Line ("   -nostdinc Don't look for sources in the system default"
                & " directory");
      Put_Line ("   -nostdlib Don't look for library files in the system"
                & " default directory");
      Put_Line ("   --RTS=dir specify the default source and object search"
                & " path");
      Put_Line ("   -p file   Use file as the default project file");
      Put_Line ("   -r        Find all references (default to find declaration"
                & " only)");
      Put_Line ("   -s        Print source line");
      Put_Line ("   -t        Print type hierarchy");
      New_Line;

      raise Usage_Error;
   end Write_Usage;

--  Start of processing for Gnatfind

begin
   Parse_Cmd_Line;

   if not Have_Entity then
      Write_Usage;
   end if;

   --  Special case to speed things up: if the user has a command line of the
   --  form 'gnatfind entity:file', ie has specified a file and only wants the
   --  bodies and specs, then we can restrict the search to the .ali file
   --  associated with 'file'.

   if Has_File_In_Entity
     and then not Output_Ref
   then
      Wide_Search := False;
   end if;

   --  Find the project file

   if Prj_File_Length = 0 then
      Xr_Tabls.Create_Project_File (Default_Project_File ("."));
   else
      Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
   end if;

   --  Fill up the table

   if Type_Tree and then Nb_File > 1 then
      Ada.Text_IO.Put_Line ("Error: for type hierarchy output you must "
                            & "specify only one file.");
      Ada.Text_IO.New_Line;
      Write_Usage;
   end if;

   Search (Pattern, Local_Symbols, Wide_Search, Read_Only,
           Der_Info, Type_Tree);

   if Source_Lines then
      Xr_Tabls.Grep_Source_Files;
   end if;

   Print_Gnatfind (Output_Ref, Full_Path_Name);

exception
   when Usage_Error =>
      null;
end Gnatfind;