OSDN Git Service

2007-04-20 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatxref.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             G N A T X R E F                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1998-2007, 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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
38 with GNAT.Command_Line; use GNAT.Command_Line;
39
40 with System.Strings;    use System.Strings;
41
42 procedure Gnatxref is
43    Search_Unused   : Boolean := False;
44    Local_Symbols   : Boolean := True;
45    Prj_File        : File_Name_String;
46    Prj_File_Length : Natural := 0;
47    Usage_Error     : exception;
48    Full_Path_Name  : Boolean := False;
49    Vi_Mode         : Boolean := False;
50    Read_Only       : Boolean := False;
51    Have_File       : Boolean := False;
52    Der_Info        : Boolean := False;
53
54    RTS_Specified : String_Access := null;
55    --  Used to detect multiple use of --RTS= switch
56
57    procedure Parse_Cmd_Line;
58    --  Parse every switch on the command line
59
60    procedure Write_Usage;
61    --  Print a small help page for program usage
62
63    --------------------
64    -- Parse_Cmd_Line --
65    --------------------
66
67    procedure Parse_Cmd_Line is
68    begin
69       loop
70          case
71            GNAT.Command_Line.Getopt
72              ("a aI: aO: d f g h I: nostdinc nostdlib p: u v -RTS=")
73          is
74             when ASCII.NUL =>
75                exit;
76
77             when 'a' =>
78                if GNAT.Command_Line.Full_Switch = "a" then
79                   Read_Only := True;
80
81                elsif GNAT.Command_Line.Full_Switch = "aI" then
82                   Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
83
84                else
85                   Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
86                end if;
87
88             when 'd' =>
89                Der_Info := True;
90
91             when 'f' =>
92                Full_Path_Name := True;
93
94             when 'g' =>
95                Local_Symbols := False;
96
97             when 'h' =>
98                Write_Usage;
99
100             when 'I' =>
101                Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
102                Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
103
104             when 'n' =>
105                if GNAT.Command_Line.Full_Switch = "nostdinc" then
106                   Opt.No_Stdinc := True;
107                elsif GNAT.Command_Line.Full_Switch = "nostlib" then
108                   Opt.No_Stdlib := True;
109                end if;
110
111             when 'p' =>
112                declare
113                   S : constant String := GNAT.Command_Line.Parameter;
114                begin
115                   Prj_File_Length := S'Length;
116                   Prj_File (1 .. Prj_File_Length) := S;
117                end;
118
119             when 'u' =>
120                Search_Unused := True;
121                Vi_Mode := False;
122
123             when 'v' =>
124                Vi_Mode := True;
125                Search_Unused := False;
126
127             --  The only switch starting with -- recognized is --RTS
128
129             when '-' =>
130
131                --  Check that it is the first time we see this switch
132
133                if RTS_Specified = null then
134                   RTS_Specified := new String'(GNAT.Command_Line.Parameter);
135
136                elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
137                   Osint.Fail ("--RTS cannot be specified multiple times");
138                end if;
139
140                Opt.No_Stdinc  := True;
141                Opt.RTS_Switch := True;
142
143                declare
144                   Src_Path_Name : constant String_Ptr :=
145                                     Get_RTS_Search_Dir
146                                       (GNAT.Command_Line.Parameter, Include);
147
148                   Lib_Path_Name : constant 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             if Ada.Strings.Fixed.Index (S, ":") /= 0 then
186                Ada.Text_IO.Put_Line
187                  ("Only file names are allowed on the command line");
188                Write_Usage;
189             end if;
190
191             Add_Xref_File (S);
192             Have_File := True;
193          end;
194       end loop;
195
196    exception
197       when GNAT.Command_Line.Invalid_Switch =>
198          Ada.Text_IO.Put_Line ("Invalid switch : "
199                                & GNAT.Command_Line.Full_Switch);
200          Write_Usage;
201
202       when GNAT.Command_Line.Invalid_Parameter =>
203          Ada.Text_IO.Put_Line ("Parameter missing for : "
204                                & GNAT.Command_Line.Full_Switch);
205          Write_Usage;
206    end Parse_Cmd_Line;
207
208    -----------------
209    -- Write_Usage --
210    -----------------
211
212    procedure Write_Usage is
213    begin
214       Put_Line ("GNATXREF " & Gnatvsn.Gnat_Version_String);
215       Put_Line ("Copyright 1998-2005, AdaCore");
216       Put_Line ("Usage: gnatxref [switches] file1 file2 ...");
217       New_Line;
218       Put_Line ("  file ... list of source files to xref, " &
219                 "including with'ed units");
220       New_Line;
221       Put_Line ("gnatxref switches:");
222       Put_Line ("   -a        Consider all files, even when the ali file is"
223                 & " readonly");
224       Put_Line ("   -aIdir    Specify source files search path");
225       Put_Line ("   -aOdir    Specify library/object files search path");
226       Put_Line ("   -d        Output derived type information");
227       Put_Line ("   -f        Output full path name");
228       Put_Line ("   -g        Output information only for global symbols");
229       Put_Line ("   -Idir     Like -aIdir -aOdir");
230       Put_Line ("   -nostdinc Don't look for sources in the system default"
231                 & " directory");
232       Put_Line ("   -nostdlib Don't look for library files in the system"
233                 & " default directory");
234       Put_Line ("   --RTS=dir specify the default source and object search"
235                 & " path");
236       Put_Line ("   -p file   Use file as the default project file");
237       Put_Line ("   -u        List unused entities");
238       Put_Line ("   -v        Print a 'tags' file for vi");
239       New_Line;
240
241       raise Usage_Error;
242    end Write_Usage;
243
244 begin
245    Parse_Cmd_Line;
246
247    if not Have_File then
248       Write_Usage;
249    end if;
250
251    Xr_Tabls.Set_Default_Match (True);
252
253    --  Find the project file
254
255    if Prj_File_Length = 0 then
256       Xr_Tabls.Create_Project_File
257         (Default_Project_File (Osint.To_Host_Dir_Spec (".", False).all));
258    else
259       Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
260    end if;
261
262    --  Fill up the table
263
264    Search_Xref (Local_Symbols, Read_Only, Der_Info);
265
266    if Search_Unused then
267       Print_Unused (Full_Path_Name);
268    elsif Vi_Mode then
269       Print_Vi (Full_Path_Name);
270    else
271       Print_Xref (Full_Path_Name);
272    end if;
273
274 exception
275    when Usage_Error =>
276       null;
277 end Gnatxref;