OSDN Git Service

New Language: 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 --                            $Revision: 1.21 $
10 --                                                                          --
11 --         Copyright (C) 1998-2001 Free Software Foundation, Inc.           --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
25 --                                                                          --
26 ------------------------------------------------------------------------------
27
28 with Xr_Tabls;
29 with Xref_Lib; use Xref_Lib;
30 with Ada.Text_IO;
31 with Ada.Strings.Fixed;
32 with GNAT.Command_Line;
33 with Gnatvsn;
34 with Osint;
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 GNAT.Command_Line.Getopt ("a aI: aO: d f g h I: p: u v") is
63             when ASCII.NUL =>
64                exit;
65
66             when 'a'    =>
67                if GNAT.Command_Line.Full_Switch = "a" then
68                   Read_Only := True;
69                elsif GNAT.Command_Line.Full_Switch = "aI" then
70                   Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
71                else
72                   Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
73                end if;
74
75             when 'd' =>
76                Der_Info := True;
77
78             when 'f'    =>
79                Full_Path_Name := True;
80
81             when 'g'    =>
82                Local_Symbols := False;
83
84             when 'h'    =>
85                Write_Usage;
86
87             when 'I'    =>
88                Osint.Add_Src_Search_Dir (GNAT.Command_Line.Parameter);
89                Osint.Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
90
91             when 'p'    =>
92                declare
93                   S : constant String := GNAT.Command_Line.Parameter;
94
95                begin
96                   Prj_File_Length := S'Length;
97                   Prj_File (1 .. Prj_File_Length) := S;
98                end;
99
100             when 'u'    =>
101                Search_Unused := True;
102                Vi_Mode := False;
103
104             when 'v'    =>
105                Vi_Mode := True;
106                Search_Unused := False;
107
108             when others =>
109                Write_Usage;
110          end case;
111       end loop;
112
113       --  Get the other arguments
114
115       loop
116          declare
117             S : constant String := GNAT.Command_Line.Get_Argument;
118
119          begin
120             exit when S'Length = 0;
121
122             if Ada.Strings.Fixed.Index (S, ":") /= 0 then
123                Ada.Text_IO.Put_Line
124                  ("Only file names are allowed on the command line");
125                Write_Usage;
126             end if;
127
128             Add_File (S);
129             Have_File := True;
130          end;
131       end loop;
132
133    exception
134       when GNAT.Command_Line.Invalid_Switch =>
135          Ada.Text_IO.Put_Line ("Invalid switch : "
136                                & GNAT.Command_Line.Full_Switch);
137          Write_Usage;
138
139       when GNAT.Command_Line.Invalid_Parameter =>
140          Ada.Text_IO.Put_Line ("Parameter missing for : "
141                                & GNAT.Command_Line.Parameter);
142          Write_Usage;
143    end Parse_Cmd_Line;
144
145    -----------------
146    -- Write_Usage --
147    -----------------
148
149    procedure Write_Usage is
150       use Ada.Text_IO;
151
152    begin
153       Put_Line ("GNATXREF " & Gnatvsn.Gnat_Version_String
154                 & " Copyright 1998-2001, Ada Core Technologies Inc.");
155       Put_Line ("Usage: gnatxref [switches] file1 file2 ...");
156       New_Line;
157       Put_Line ("  file ... list of source files to xref, " &
158                 "including with'ed units");
159       New_Line;
160       Put_Line ("gnatxref switches:");
161       Put_Line ("   -a      Consider all files, even when the ali file is"
162                 & " readonly");
163       Put_Line ("   -aIdir  Specify source files search path");
164       Put_Line ("   -aOdir  Specify library/object files search path");
165       Put_Line ("   -d      Output derived type information");
166       Put_Line ("   -f      Output full path name");
167       Put_Line ("   -g      Output information only for global symbols");
168       Put_Line ("   -Idir   Like -aIdir -aOdir");
169       Put_Line ("   -p file Use file as the default project file");
170       Put_Line ("   -u      List unused entities");
171       Put_Line ("   -v      Print a 'tags' file for vi");
172       New_Line;
173
174       raise Usage_Error;
175    end Write_Usage;
176
177 begin
178    Parse_Cmd_Line;
179
180    if not Have_File then
181       Write_Usage;
182    end if;
183
184    Xr_Tabls.Set_Default_Match (True);
185
186    --  Find the project file
187
188    if Prj_File_Length = 0 then
189       Xr_Tabls.Create_Project_File
190         (Default_Project_File (Osint.To_Host_Dir_Spec (".", False).all));
191    else
192       Xr_Tabls.Create_Project_File (Prj_File (1 .. Prj_File_Length));
193    end if;
194
195    --  Fill up the table
196
197    Search_Xref (Local_Symbols, Read_Only, Der_Info);
198
199    if Search_Unused then
200       Print_Unused (Full_Path_Name);
201    elsif Vi_Mode then
202       Print_Vi (Full_Path_Name);
203    else
204       Print_Xref (Full_Path_Name);
205    end if;
206
207 exception
208    when Usage_Error =>
209       null;
210 end Gnatxref;