OSDN Git Service

gcc/ada/
[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 3,  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 COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Opt;
27 with Osint;    use Osint;
28 with Types;    use Types;
29 with Switch;   use Switch;
30 with Xr_Tabls; use Xr_Tabls;
31 with Xref_Lib; use Xref_Lib;
32
33 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
34 with Ada.Text_IO;       use Ada.Text_IO;
35
36 with GNAT.Command_Line; use GNAT.Command_Line;
37
38 with System.Strings;    use System.Strings;
39
40 procedure Gnatxref is
41    Search_Unused   : Boolean := False;
42    Local_Symbols   : Boolean := True;
43    Prj_File        : File_Name_String;
44    Prj_File_Length : Natural := 0;
45    Usage_Error     : exception;
46    Full_Path_Name  : Boolean := False;
47    Vi_Mode         : Boolean := False;
48    Read_Only       : Boolean := False;
49    Have_File       : Boolean := False;
50    Der_Info        : Boolean := False;
51
52    RTS_Specified : String_Access := null;
53    --  Used to detect multiple use of --RTS= switch
54
55    procedure Parse_Cmd_Line;
56    --  Parse every switch on the command line
57
58    procedure Usage;
59    --  Display the usage
60
61    procedure Write_Usage;
62    --  Print a small help page for program usage
63
64    --------------------
65    -- Parse_Cmd_Line --
66    --------------------
67
68    procedure Parse_Cmd_Line is
69
70       procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
71
72       --  Start of processing for Parse_Cmd_Line
73
74    begin
75       --  First check for --version or --help
76
77       Check_Version_And_Help ("GNATXREF", "1998");
78
79       loop
80          case
81            GNAT.Command_Line.Getopt
82              ("a aI: aO: d f g h I: nostdinc nostdlib p: u v -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 'f' =>
102                Full_Path_Name := True;
103
104             when 'g' =>
105                Local_Symbols := False;
106
107             when 'h' =>
108                Write_Usage;
109
110             when 'I' =>
111                Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
112                Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
113
114             when 'n' =>
115                if GNAT.Command_Line.Full_Switch = "nostdinc" then
116                   Opt.No_Stdinc := True;
117                elsif GNAT.Command_Line.Full_Switch = "nostlib" then
118                   Opt.No_Stdlib := True;
119                end if;
120
121             when 'p' =>
122                declare
123                   S : constant String := GNAT.Command_Line.Parameter;
124                begin
125                   Prj_File_Length := S'Length;
126                   Prj_File (1 .. Prj_File_Length) := S;
127                end;
128
129             when 'u' =>
130                Search_Unused := True;
131                Vi_Mode := False;
132
133             when 'v' =>
134                Vi_Mode := True;
135                Search_Unused := False;
136
137             --  The only switch starting with -- recognized is --RTS
138
139             when '-' =>
140
141                --  Check that it is the first time we see this switch
142
143                if RTS_Specified = null then
144                   RTS_Specified := new String'(GNAT.Command_Line.Parameter);
145
146                elsif RTS_Specified.all /= GNAT.Command_Line.Parameter then
147                   Osint.Fail ("--RTS cannot be specified multiple times");
148                end if;
149
150                Opt.No_Stdinc  := True;
151                Opt.RTS_Switch := True;
152
153                declare
154                   Src_Path_Name : constant String_Ptr :=
155                                     Get_RTS_Search_Dir
156                                       (GNAT.Command_Line.Parameter, Include);
157
158                   Lib_Path_Name : constant String_Ptr :=
159                                     Get_RTS_Search_Dir
160                                       (GNAT.Command_Line.Parameter, Objects);
161
162                begin
163                   if Src_Path_Name /= null and then Lib_Path_Name /= null then
164                      Add_Search_Dirs (Src_Path_Name, Include);
165                      Add_Search_Dirs (Lib_Path_Name, Objects);
166
167                   elsif Src_Path_Name = null and then Lib_Path_Name = null then
168                      Osint.Fail ("RTS path not valid: missing " &
169                                  "adainclude and adalib directories");
170
171                   elsif Src_Path_Name = null then
172                      Osint.Fail ("RTS path not valid: missing " &
173                                  "adainclude directory");
174
175                   elsif  Lib_Path_Name = null then
176                      Osint.Fail ("RTS path not valid: missing " &
177                                  "adalib directory");
178                   end if;
179                end;
180
181             when others =>
182                Write_Usage;
183          end case;
184       end loop;
185
186       --  Get the other arguments
187
188       loop
189          declare
190             S : constant String := GNAT.Command_Line.Get_Argument;
191
192          begin
193             exit when S'Length = 0;
194
195             if Ada.Strings.Fixed.Index (S, ":") /= 0 then
196                Ada.Text_IO.Put_Line
197                  ("Only file names are allowed on the command line");
198                Write_Usage;
199             end if;
200
201             Add_Xref_File (S);
202             Have_File := True;
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    end Parse_Cmd_Line;
217
218    -----------
219    -- Usage --
220    -----------
221
222    procedure Usage is
223    begin
224       Put_Line ("Usage: gnatxref [switches] file1 file2 ...");
225       New_Line;
226       Put_Line ("  file ... list of source files to xref, " &
227                 "including with'ed units");
228       New_Line;
229       Put_Line ("gnatxref switches:");
230       Put_Line ("   -a        Consider all files, even when the ali file is"
231                 & " readonly");
232       Put_Line ("   -aIdir    Specify source files search path");
233       Put_Line ("   -aOdir    Specify library/object files search path");
234       Put_Line ("   -d        Output derived type information");
235       Put_Line ("   -f        Output full path name");
236       Put_Line ("   -g        Output information only for global symbols");
237       Put_Line ("   -Idir     Like -aIdir -aOdir");
238       Put_Line ("   -nostdinc Don't look for sources in the system default"
239                 & " directory");
240       Put_Line ("   -nostdlib Don't look for library files in the system"
241                 & " default directory");
242       Put_Line ("   --RTS=dir specify the default source and object search"
243                 & " path");
244       Put_Line ("   -p file   Use file as the default project file");
245       Put_Line ("   -u        List unused entities");
246       Put_Line ("   -v        Print a 'tags' file for vi");
247       New_Line;
248
249    end Usage;
250
251    -----------------
252    -- Write_Usage --
253    -----------------
254
255    procedure Write_Usage is
256    begin
257       Display_Version ("GNATXREF", "1998");
258       New_Line;
259       Usage;
260       raise Usage_Error;
261    end Write_Usage;
262
263 begin
264    Parse_Cmd_Line;
265
266    if not Have_File then
267       Write_Usage;
268    end if;
269
270    Xr_Tabls.Set_Default_Match (True);
271
272    --  Find the project file
273
274    if Prj_File_Length = 0 then
275       Xr_Tabls.Create_Project_File
276         (Default_Project_File (Osint.To_Host_Dir_Spec (".", False).all));
277    else
278       Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
279    end if;
280
281    --  Fill up the table
282
283    Search_Xref (Local_Symbols, Read_Only, Der_Info);
284
285    if Search_Unused then
286       Print_Unused (Full_Path_Name);
287    elsif Vi_Mode then
288       Print_Vi (Full_Path_Name);
289    else
290       Print_Xref (Full_Path_Name);
291    end if;
292
293 exception
294    when Usage_Error =>
295       null;
296 end Gnatxref;