OSDN Git Service

* 1aexcept.adb, 1aexcept.ads, 1ic.ads, 1ssecsta.adb,
[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-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 procedure Gnatxref is
37
38    Search_Unused   : Boolean := False;
39    Local_Symbols   : Boolean := True;
40    Prj_File        : File_Name_String;
41    Prj_File_Length : Natural := 0;
42    Usage_Error     : exception;
43    Full_Path_Name  : Boolean := False;
44    Vi_Mode         : Boolean := False;
45    Read_Only       : Boolean := False;
46    Have_File       : Boolean := False;
47    Der_Info        : Boolean := False;
48
49    procedure Parse_Cmd_Line;
50    --  Parse every switch on the command line
51
52    procedure Write_Usage;
53    --  Print a small help page for program usage
54
55    --------------------
56    -- Parse_Cmd_Line --
57    --------------------
58
59    procedure Parse_Cmd_Line is
60    begin
61       loop
62          case
63            GNAT.Command_Line.Getopt
64              ("a aI: aO: d f g h I: nostdinc nostdlib p: u v -RTS=")
65          is
66             when ASCII.NUL =>
67                exit;
68
69             when 'a'    =>
70                if GNAT.Command_Line.Full_Switch = "a" then
71                   Read_Only := True;
72
73                elsif GNAT.Command_Line.Full_Switch = "aI" then
74                   Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
75
76                else
77                   Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
78                end if;
79
80             when 'd'    =>
81                Der_Info := True;
82
83             when 'f'    =>
84                Full_Path_Name := True;
85
86             when 'g'    =>
87                Local_Symbols := False;
88
89             when 'h'    =>
90                Write_Usage;
91
92             when 'I'    =>
93                Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
94                Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
95
96             when 'n'    =>
97                if GNAT.Command_Line.Full_Switch = "nostdinc" then
98                   Opt.No_Stdinc := True;
99                elsif GNAT.Command_Line.Full_Switch = "nostlib" then
100                   Opt.No_Stdlib := True;
101                end if;
102
103             when 'p'    =>
104                declare
105                   S : constant String := GNAT.Command_Line.Parameter;
106
107                begin
108                   Prj_File_Length := S'Length;
109                   Prj_File (1 .. Prj_File_Length) := S;
110                end;
111
112             when 'u'    =>
113                Search_Unused := True;
114                Vi_Mode := False;
115
116             when 'v'    =>
117                Vi_Mode := True;
118                Search_Unused := False;
119
120             --  The only switch starting with -- recognized is --RTS
121
122             when '-'    =>
123                Opt.No_Stdinc  := True;
124                Opt.RTS_Switch := True;
125
126                declare
127                   Src_Path_Name : String_Ptr :=
128                                     Get_RTS_Search_Dir
129                                       (GNAT.Command_Line.Parameter, Include);
130
131                   Lib_Path_Name : String_Ptr :=
132                                     Get_RTS_Search_Dir
133                                       (GNAT.Command_Line.Parameter, Objects);
134
135                begin
136                   if Src_Path_Name /= null and then Lib_Path_Name /= null then
137                      Add_Search_Dirs (Src_Path_Name, Include);
138                      Add_Search_Dirs (Lib_Path_Name, Objects);
139
140                   elsif Src_Path_Name = null and then Lib_Path_Name = null then
141                      Osint.Fail ("RTS path not valid: missing " &
142                                  "adainclude and adalib directories");
143
144                   elsif Src_Path_Name = null then
145                      Osint.Fail ("RTS path not valid: missing " &
146                                  "adainclude directory");
147
148                   elsif  Lib_Path_Name = null then
149                      Osint.Fail ("RTS path not valid: missing " &
150                                  "adalib directory");
151                   end if;
152                end;
153
154             when others =>
155                Write_Usage;
156          end case;
157       end loop;
158
159       --  Get the other arguments
160
161       loop
162          declare
163             S : constant String := GNAT.Command_Line.Get_Argument;
164
165          begin
166             exit when S'Length = 0;
167
168             if Ada.Strings.Fixed.Index (S, ":") /= 0 then
169                Ada.Text_IO.Put_Line
170                  ("Only file names are allowed on the command line");
171                Write_Usage;
172             end if;
173
174             Add_Xref_File (S);
175             Have_File := True;
176          end;
177       end loop;
178
179    exception
180       when GNAT.Command_Line.Invalid_Switch =>
181          Ada.Text_IO.Put_Line ("Invalid switch : "
182                                & GNAT.Command_Line.Full_Switch);
183          Write_Usage;
184
185       when GNAT.Command_Line.Invalid_Parameter =>
186          Ada.Text_IO.Put_Line ("Parameter missing for : "
187                                & GNAT.Command_Line.Full_Switch);
188          Write_Usage;
189    end Parse_Cmd_Line;
190
191    -----------------
192    -- Write_Usage --
193    -----------------
194
195    procedure Write_Usage is
196       use Ada.Text_IO;
197
198    begin
199       Put_Line ("GNATXREF " & Gnatvsn.Gnat_Version_String
200                 & " Copyright 1998-2002, Ada Core Technologies Inc.");
201       Put_Line ("Usage: gnatxref [switches] file1 file2 ...");
202       New_Line;
203       Put_Line ("  file ... list of source files to xref, " &
204                 "including with'ed units");
205       New_Line;
206       Put_Line ("gnatxref switches:");
207       Put_Line ("   -a        Consider all files, even when the ali file is"
208                 & " readonly");
209       Put_Line ("   -aIdir    Specify source files search path");
210       Put_Line ("   -aOdir    Specify library/object files search path");
211       Put_Line ("   -d        Output derived type information");
212       Put_Line ("   -f        Output full path name");
213       Put_Line ("   -g        Output information only for global symbols");
214       Put_Line ("   -Idir     Like -aIdir -aOdir");
215       Put_Line ("   -nostdinc Don't look for sources in the system default"
216                 & " directory");
217       Put_Line ("   -nostdlib Don't look for library files in the system"
218                 & " default directory");
219       Put_Line ("   --RTS=dir specify the default source and object search"
220                 & " path");
221       Put_Line ("   -p file   Use file as the default project file");
222       Put_Line ("   -u        List unused entities");
223       Put_Line ("   -v        Print a 'tags' file for vi");
224       New_Line;
225
226       raise Usage_Error;
227    end Write_Usage;
228
229 begin
230    Parse_Cmd_Line;
231
232    if not Have_File then
233       Write_Usage;
234    end if;
235
236    Xr_Tabls.Set_Default_Match (True);
237
238    --  Find the project file
239
240    if Prj_File_Length = 0 then
241       Xr_Tabls.Create_Project_File
242         (Default_Project_File (Osint.To_Host_Dir_Spec (".", False).all));
243    else
244       Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
245    end if;
246
247    --  Fill up the table
248
249    Search_Xref (Local_Symbols, Read_Only, Der_Info);
250
251    if Search_Unused then
252       Print_Unused (Full_Path_Name);
253    elsif Vi_Mode then
254       Print_Vi (Full_Path_Name);
255    else
256       Print_Xref (Full_Path_Name);
257    end if;
258
259 exception
260    when Usage_Error =>
261       null;
262 end Gnatxref;