OSDN Git Service

2003-11-19 Arnaud Charlet <charlet@act-europe.fr>
[pf3gnuchains/gcc-fork.git] / gcc / ada / xref_lib.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             X R E F _ L I B                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1998-2003 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 -- 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 Osint;
28 with Output; use Output;
29 with Types;  use Types;
30
31 with Unchecked_Deallocation;
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 with GNAT.IO_Aux;       use GNAT.IO_Aux;
38
39 package body Xref_Lib is
40
41    Type_Position : constant := 50;
42    --  Column for label identifying type of entity
43
44    ---------------------
45    -- Local Variables --
46    ---------------------
47
48    Pipe : constant Character := '|';
49    --  First character on xref lines in the .ali file
50
51    No_Xref_Information : exception;
52    --  Exception raised when there is no cross-referencing information in
53    --  the .ali files
54
55    procedure Parse_EOL
56      (Source                 : access String;
57       Ptr                    : in out Positive;
58       Skip_Continuation_Line : Boolean := False);
59    --  On return Source (Ptr) is the first character of the next line
60    --  or EOF. Source.all must be terminated by EOF.
61    --
62    --  If Skip_Continuation_Line is True, this subprogram skips as many
63    --  lines as required when the second or more lines starts with '.'
64    --  (continuation lines in ALI files).
65
66    function Current_Xref_File (File : ALI_File) return File_Reference;
67    --  Return the file matching the last 'X' line we found while parsing
68    --  the ALI file.
69
70    function File_Name (File : ALI_File; Num : Positive) return File_Reference;
71    --  Returns the dependency file name number Num
72
73    function Get_Full_Type (Decl : Declaration_Reference) return String;
74    --  Returns the full type corresponding to a type letter as found in
75    --  the .ali files.
76
77    procedure Open
78      (Name         : in  String;
79       File         : out ALI_File;
80       Dependencies : in  Boolean := False);
81    --  Open a new ALI file. If Dependencies is True, the insert every library
82    --  file 'with'ed in the files database (used for gnatxref)
83
84    procedure Parse_Identifier_Info
85      (Pattern       : Search_Pattern;
86       File          : in out ALI_File;
87       Local_Symbols : Boolean;
88       Der_Info      : Boolean := False;
89       Type_Tree     : Boolean := False;
90       Wide_Search   : Boolean := True;
91       Labels_As_Ref : Boolean := True);
92    --  Output the file and the line where the identifier was referenced,
93    --  If Local_Symbols is False then only the publicly visible symbols
94    --  will be processed.
95    --
96    --  If Labels_As_Ref is true, then the references to the entities after
97    --  the end statements ("end Foo") will be counted as actual references.
98    --  The entity will never be reported as unreferenced by gnatxref -u
99
100    procedure Parse_Token
101      (Source    : access String;
102       Ptr       : in out Positive;
103       Token_Ptr : out Positive);
104    --  Skips any separators and stores the start of the token in Token_Ptr.
105    --  Then stores the position of the next separator in Ptr. On return
106    --  Source (Token_Ptr .. Ptr - 1) is the token. Separators are space
107    --  and ASCII.HT. Parse_Token will never skip to the next line.
108
109    procedure Parse_Number
110      (Source : access String;
111       Ptr    : in out Positive;
112       Number : out Natural);
113    --  Skips any separators and parses Source upto the first character that
114    --  is not a decimal digit. Returns value of parsed digits or 0 if none.
115
116    procedure Parse_X_Filename (File : in out ALI_File);
117    --  Reads and processes "X..." lines in the ALI file
118    --  and updates the File.X_File information.
119
120    procedure Skip_To_First_X_Line
121      (File    : in out ALI_File;
122       D_Lines : Boolean;
123       W_Lines : Boolean);
124    --  Skip the lines in the ALI file until the first cross-reference line
125    --  (^X...) is found. Search is started from the beginning of the file.
126    --  If not such line is found, No_Xref_Information is raised.
127    --  If W_Lines is false, then the lines "^W" are not parsed.
128    --  If D_Lines is false, then the lines "^D" are not parsed.
129
130    ----------------
131    -- Add_Entity --
132    ----------------
133
134    procedure Add_Entity
135      (Pattern : in out Search_Pattern;
136       Entity  : String;
137       Glob    : Boolean := False)
138    is
139       File_Start  : Natural;
140       Line_Start  : Natural;
141       Col_Start   : Natural;
142       Line_Num    : Natural := 0;
143       Col_Num     : Natural := 0;
144       File_Ref    : File_Reference := Empty_File;
145       Has_Pattern : Boolean := False;
146
147    begin
148       --  Find the end of the first item in Entity (pattern or file?)
149       --  If there is no ':', we only have a pattern
150
151       File_Start := Index (Entity, ":");
152
153       --  If the regular expression is invalid, just consider it as a string
154
155       if File_Start = 0 then
156          begin
157             Pattern.Entity := Compile (Entity, Glob, False);
158             Pattern.Initialized := True;
159
160          exception
161             when Error_In_Regexp =>
162
163                --  The basic idea is to insert a \ before every character
164
165                declare
166                   Tmp_Regexp : String (1 .. 2 * Entity'Length);
167                   Index      : Positive := 1;
168
169                begin
170                   for J in Entity'Range loop
171                      Tmp_Regexp (Index) := '\';
172                      Tmp_Regexp (Index + 1) := Entity (J);
173                      Index := Index + 2;
174                   end loop;
175
176                   Pattern.Entity := Compile (Tmp_Regexp, True, False);
177                   Pattern.Initialized := True;
178                end;
179          end;
180
181          Set_Default_Match (True);
182          return;
183       end if;
184
185       --  If there is a dot in the pattern, then it is a file name
186
187       if (Glob and then
188            Index (Entity (Entity'First .. File_Start - 1), ".") /= 0)
189              or else
190               (not Glob
191                  and then Index (Entity (Entity'First .. File_Start - 1),
192                                    "\.") /= 0)
193       then
194          Pattern.Entity      := Compile (".*", False);
195          Pattern.Initialized := True;
196          File_Start          := Entity'First;
197
198       else
199          --  If the regular expression is invalid, just consider it as a string
200
201          begin
202             Pattern.Entity :=
203               Compile (Entity (Entity'First .. File_Start - 1), Glob, False);
204             Pattern.Initialized := True;
205
206          exception
207             when Error_In_Regexp =>
208
209                --  The basic idea is to insert a \ before every character
210
211                declare
212                   Tmp_Regexp : String (1 .. 2 * (File_Start - Entity'First));
213                   Index      : Positive := 1;
214
215                begin
216                   for J in Entity'First .. File_Start - 1 loop
217                      Tmp_Regexp (Index) := '\';
218                      Tmp_Regexp (Index + 1) := Entity (J);
219                      Index := Index + 2;
220                   end loop;
221
222                   Pattern.Entity := Compile (Tmp_Regexp, True, False);
223                   Pattern.Initialized := True;
224                end;
225          end;
226
227          File_Start  := File_Start + 1;
228          Has_Pattern := True;
229       end if;
230
231       --  Parse the file name
232
233       Line_Start := Index (Entity (File_Start .. Entity'Last), ":");
234
235       --  Check if it was a disk:\directory item (for NT and OS/2)
236
237       if File_Start = Line_Start - 1
238         and then Line_Start < Entity'Last
239         and then Entity (Line_Start + 1) = '\'
240       then
241          Line_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
242       end if;
243
244       if Line_Start = 0 then
245          Line_Start := Entity'Length + 1;
246
247       elsif Line_Start /= Entity'Last then
248          Col_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
249
250          if Col_Start = 0 then
251             Col_Start := Entity'Last + 1;
252          end if;
253
254          if Col_Start > Line_Start + 1 then
255             begin
256                Line_Num := Natural'Value
257                  (Entity (Line_Start + 1 .. Col_Start - 1));
258
259             exception
260                when Constraint_Error =>
261                   raise Invalid_Argument;
262             end;
263          end if;
264
265          if Col_Start < Entity'Last then
266             begin
267                Col_Num := Natural'Value (Entity
268                                          (Col_Start + 1 .. Entity'Last));
269
270             exception
271                when Constraint_Error => raise Invalid_Argument;
272             end;
273          end if;
274       end if;
275
276       File_Ref :=
277         Add_To_Xref_File
278           (Entity (File_Start .. Line_Start - 1), Visited => True);
279       Pattern.File_Ref := File_Ref;
280       Add_Line (Pattern.File_Ref, Line_Num, Col_Num);
281       File_Ref :=
282         Add_To_Xref_File
283           (ALI_File_Name (Entity (File_Start .. Line_Start - 1)),
284            Visited      => False,
285            Emit_Warning => True);
286    end Add_Entity;
287
288    -------------------
289    -- Add_Xref_File --
290    -------------------
291
292    procedure Add_Xref_File (File : String) is
293       File_Ref : File_Reference := Empty_File;
294       Iterator : Expansion_Iterator;
295
296       procedure Add_Xref_File_Internal (File : String);
297       --  Do the actual addition of the file
298
299       ----------------------------
300       -- Add_Xref_File_Internal --
301       ----------------------------
302
303       procedure Add_Xref_File_Internal (File : String) is
304       begin
305          --  Case where we have an ALI file, accept it even though this is
306          --  not official usage, since the intention is obvious
307
308          if Tail (File, 4) = ".ali" then
309             File_Ref := Add_To_Xref_File
310               (File, Visited => False, Emit_Warning => True);
311
312          --  Normal non-ali file case
313
314          else
315             File_Ref := Add_To_Xref_File (File, Visited => True);
316
317             File_Ref := Add_To_Xref_File
318               (ALI_File_Name (File),
319                Visited => False,
320                Emit_Warning => True);
321          end if;
322       end Add_Xref_File_Internal;
323
324    --  Start of processing for Add_Xref_File
325
326    begin
327       --  Check if we need to do the expansion
328
329       if Ada.Strings.Fixed.Index (File, "*") /= 0
330         or else Ada.Strings.Fixed.Index (File, "?") /= 0
331       then
332          Start_Expansion (Iterator, File);
333
334          loop
335             declare
336                S : constant String := Expansion (Iterator);
337
338             begin
339                exit when S'Length = 0;
340                Add_Xref_File_Internal (S);
341             end;
342          end loop;
343
344       else
345          Add_Xref_File_Internal (File);
346       end if;
347    end Add_Xref_File;
348
349    -----------------------
350    -- Current_Xref_File --
351    -----------------------
352
353    function Current_Xref_File (File : ALI_File) return File_Reference is
354    begin
355       return File.X_File;
356    end Current_Xref_File;
357
358    --------------------------
359    -- Default_Project_File --
360    --------------------------
361
362    function Default_Project_File (Dir_Name : String) return String is
363       My_Dir  : Dir_Type;
364       Dir_Ent : File_Name_String;
365       Last    : Natural;
366
367    begin
368       Open (My_Dir, Dir_Name);
369
370       loop
371          Read (My_Dir, Dir_Ent, Last);
372          exit when Last = 0;
373
374          if Tail (Dir_Ent (1 .. Last), 4) = ".adp" then
375
376             --  The first project file found is the good one.
377
378             Close (My_Dir);
379             return Dir_Ent (1 .. Last);
380          end if;
381       end loop;
382
383       Close (My_Dir);
384       return String'(1 .. 0 => ' ');
385
386    exception
387       when Directory_Error => return String'(1 .. 0 => ' ');
388    end Default_Project_File;
389
390    ---------------
391    -- File_Name --
392    ---------------
393
394    function File_Name
395      (File : ALI_File;
396       Num  : Positive) return File_Reference
397    is
398    begin
399       return File.Dep.Table (Num);
400    end File_Name;
401
402    --------------------
403    -- Find_ALI_Files --
404    --------------------
405
406    procedure Find_ALI_Files is
407       My_Dir       : Rec_DIR;
408       Dir_Ent      : File_Name_String;
409       Last         : Natural;
410       File_Ref     : File_Reference;
411
412       function Open_Next_Dir return Boolean;
413       --  Tries to open the next object directory, and return False if
414       --  the directory cannot be opened.
415
416       -------------------
417       -- Open_Next_Dir --
418       -------------------
419
420       function Open_Next_Dir return Boolean is
421       begin
422          --  Until we are able to open a new directory
423
424          loop
425             declare
426                Obj_Dir : constant String := Next_Obj_Dir;
427
428             begin
429                --  Case of no more Obj_Dir lines
430
431                if Obj_Dir'Length = 0 then
432                   return False;
433                end if;
434
435                Open (My_Dir.Dir, Obj_Dir);
436                exit;
437
438             exception
439
440                --  Could not open the directory
441
442                when Directory_Error => null;
443             end;
444          end loop;
445
446          return True;
447       end Open_Next_Dir;
448
449    --  Start of processing for Find_ALI_Files
450
451    begin
452       Reset_Obj_Dir;
453
454       if Open_Next_Dir then
455          loop
456             Read (My_Dir.Dir, Dir_Ent, Last);
457
458             if Last = 0 then
459                Close (My_Dir.Dir);
460
461                if not Open_Next_Dir then
462                   return;
463                end if;
464
465             elsif Last > 4 and then Dir_Ent (Last - 3 .. Last) = ".ali" then
466                File_Ref :=
467                  Add_To_Xref_File (Dir_Ent (1 .. Last), Visited => False);
468             end if;
469          end loop;
470       end if;
471    end Find_ALI_Files;
472
473    -------------------
474    -- Get_Full_Type --
475    -------------------
476
477    function Get_Full_Type (Decl : Declaration_Reference) return String is
478
479       function Param_String return String;
480       --  Return the string to display depending on whether Decl is a
481       --  parameter or not
482
483       ------------------
484       -- Param_String --
485       ------------------
486
487       function Param_String return String is
488       begin
489          if Is_Parameter (Decl) then
490             return "parameter ";
491          else
492             return "";
493          end if;
494       end Param_String;
495
496    --  Start of processing for Get_Full_Type
497
498    begin
499       case Get_Type (Decl) is
500          when 'A' => return "array type";
501          when 'B' => return "boolean type";
502          when 'C' => return "class-wide type";
503          when 'D' => return "decimal type";
504          when 'E' => return "enumeration type";
505          when 'F' => return "float type";
506          when 'I' => return "integer type";
507          when 'M' => return "modular type";
508          when 'O' => return "fixed type";
509          when 'P' => return "access type";
510          when 'R' => return "record type";
511          when 'S' => return "string type";
512          when 'T' => return "task type";
513          when 'W' => return "protected type";
514
515          when 'a' => return "array type";
516          when 'b' => return Param_String & "boolean object";
517          when 'c' => return Param_String & "class-wide object";
518          when 'd' => return Param_String & "decimal object";
519          when 'e' => return Param_String & "enumeration object";
520          when 'f' => return Param_String & "float object";
521          when 'i' => return Param_String & "integer object";
522          when 'm' => return Param_String & "modular object";
523          when 'o' => return Param_String & "fixed object";
524          when 'p' => return Param_String & "access object";
525          when 'r' => return Param_String & "record object";
526          when 's' => return Param_String & "string object";
527          when 't' => return Param_String & "task object";
528          when 'w' => return Param_String & "protected object";
529          when 'x' => return Param_String & "abstract procedure";
530          when 'y' => return Param_String & "abstract function";
531
532          when 'K' => return "package";
533          when 'k' => return "generic package";
534          when 'L' => return "statement label";
535          when 'l' => return "loop label";
536          when 'N' => return "named number";
537          when 'n' => return "enumeration literal";
538          when 'q' => return "block label";
539          when 'U' => return "procedure";
540          when 'u' => return "generic procedure";
541          when 'V' => return "function";
542          when 'v' => return "generic function";
543          when 'X' => return "exception";
544          when 'Y' => return "entry";
545
546          when '+' => return "private type";
547
548          --  The above should be the only possibilities, but for this kind
549          --  of informational output, we don't want to bomb if we find
550          --  something else, so just return three question marks when we
551          --  have an unknown Abbrev value
552
553          when others =>
554             return "??? (" & Get_Type (Decl) & ")";
555       end case;
556    end Get_Full_Type;
557
558    --------------------------
559    -- Skip_To_First_X_Line --
560    --------------------------
561
562    procedure Skip_To_First_X_Line
563      (File    : in out ALI_File;
564       D_Lines : Boolean;
565       W_Lines : Boolean)
566    is
567       Ali              : String_Access renames File.Buffer;
568       Token            : Positive;
569       Ptr              : Positive := Ali'First;
570       Num_Dependencies : Natural  := 0;
571       File_Ref         : File_Reference;
572       File_Start       : Positive;
573       File_End         : Positive;
574       Gnatchop_Offset  : Integer;
575       Gnatchop_Name    : Positive;
576
577    begin
578       --  Read all the lines possibly processing with-clauses and dependency
579       --  information and exit on finding the first Xref line.
580       --  A fall-through of the loop means that there is no xref information
581       --  which is an error condition.
582
583       while Ali (Ptr) /= EOF loop
584
585          if D_Lines and then Ali (Ptr) = 'D' then
586
587             --  Found dependency information. Format looks like:
588             --  D src-nam time-stmp checksum [subunit-name] [line:file-name]
589
590             --  Skip the D and parse the filenam
591
592             Ptr := Ptr + 1;
593             Parse_Token (Ali, Ptr, Token);
594             File_Start := Token;
595             File_End := Ptr - 1;
596
597             Num_Dependencies := Num_Dependencies + 1;
598             Set_Last (File.Dep, Num_Dependencies);
599
600             Parse_Token (Ali, Ptr, Token); --  Skip time-stamp
601             Parse_Token (Ali, Ptr, Token); --  Skip checksum
602             Parse_Token (Ali, Ptr, Token); --  Read next entity on the line
603
604             if not (Ali (Token) in '0' .. '9') then
605                Parse_Token (Ali, Ptr, Token); --  Was a subunit name
606             end if;
607
608             --  Did we have a gnatchop-ed file with a pragma Source_Reference ?
609
610             Gnatchop_Offset := 0;
611
612             if Ali (Token) in '0' .. '9' then
613                Gnatchop_Name := Token;
614                while Ali (Gnatchop_Name) /= ':' loop
615                   Gnatchop_Name := Gnatchop_Name + 1;
616                end loop;
617
618                Gnatchop_Offset :=
619                  2 - Natural'Value (Ali (Token .. Gnatchop_Name - 1));
620                Token := Gnatchop_Name + 1;
621             end if;
622
623             File.Dep.Table (Num_Dependencies) := Add_To_Xref_File
624               (Ali (File_Start .. File_End),
625                Gnatchop_File => Ali (Token .. Ptr - 1),
626                Gnatchop_Offset => Gnatchop_Offset);
627
628          elsif W_Lines and then Ali (Ptr) = 'W' then
629
630             --  Found with-clause information. Format looks like:
631             --     "W debug%s               debug.adb               debug.ali"
632
633             --  Skip the W and parse the .ali filename (3rd token)
634
635             Parse_Token (Ali, Ptr, Token);
636             Parse_Token (Ali, Ptr, Token);
637             Parse_Token (Ali, Ptr, Token);
638
639             File_Ref := Add_To_Xref_File
640               (Ali (Token .. Ptr - 1), Visited => False);
641
642          elsif Ali (Ptr) = 'X' then
643
644             --  Found a cross-referencing line - stop processing
645
646             File.Current_Line := Ptr;
647             File.Xref_Line    := Ptr;
648             return;
649          end if;
650
651          Parse_EOL (Ali, Ptr);
652       end loop;
653
654       raise No_Xref_Information;
655    end Skip_To_First_X_Line;
656
657    ----------
658    -- Open --
659    ----------
660
661    procedure Open
662      (Name         : String;
663       File         : out ALI_File;
664       Dependencies : Boolean := False)
665    is
666       Ali : String_Access renames File.Buffer;
667
668    begin
669       if File.Buffer /= null then
670          Free (File.Buffer);
671       end if;
672
673       Init (File.Dep);
674
675       begin
676          Read_File (Name, Ali);
677
678       exception
679          when Ada.Text_IO.Name_Error | Ada.Text_IO.End_Error =>
680             raise No_Xref_Information;
681       end;
682
683       Skip_To_First_X_Line (File, D_Lines => True, W_Lines => Dependencies);
684    end Open;
685
686    ---------------
687    -- Parse_EOL --
688    ---------------
689
690    procedure Parse_EOL
691      (Source                 : access String;
692       Ptr                    : in out Positive;
693       Skip_Continuation_Line : Boolean := False)
694    is
695    begin
696       loop
697          --  Skip to end of line
698
699          while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
700            and then Source (Ptr) /= EOF
701          loop
702             Ptr := Ptr + 1;
703          end loop;
704
705          if Source (Ptr) /= EOF then
706             Ptr := Ptr + 1;      -- skip CR or LF
707          end if;
708
709          --  Skip past CR/LF or LF/CR combination
710
711          if (Source (Ptr) = ASCII.CR or else Source (Ptr) = ASCII.LF)
712            and then Source (Ptr) /= Source (Ptr - 1)
713          then
714             Ptr := Ptr + 1;
715          end if;
716
717          exit when not Skip_Continuation_Line or else Source (Ptr) /= '.';
718       end loop;
719    end Parse_EOL;
720
721    ---------------------------
722    -- Parse_Identifier_Info --
723    ---------------------------
724
725    procedure Parse_Identifier_Info
726      (Pattern       : Search_Pattern;
727       File          : in out ALI_File;
728       Local_Symbols : Boolean;
729       Der_Info      : Boolean := False;
730       Type_Tree     : Boolean := False;
731       Wide_Search   : Boolean := True;
732       Labels_As_Ref : Boolean := True)
733    is
734       Ptr      : Positive renames File.Current_Line;
735       Ali      : String_Access renames File.Buffer;
736
737       E_Line   : Natural;   --  Line number of current entity
738       E_Col    : Natural;   --  Column number of current entity
739       E_Type   : Character; --  Type of current entity
740       E_Name   : Positive;  --  Pointer to begin of entity name
741       E_Global : Boolean;   --  True iff entity is global
742
743       R_Line   : Natural;   --  Line number of current reference
744       R_Col    : Natural;   --  Column number of current reference
745       R_Type   : Character; --  Type of current reference
746
747       Decl_Ref : Declaration_Reference;
748       File_Ref : File_Reference := Current_Xref_File (File);
749
750       function Get_Symbol_Name (Eun, Line, Col : Natural) return String;
751       --  Returns the symbol name for the entity defined at the specified
752       --  line and column in the dependent unit number Eun. For this we need
753       --  to parse the ali file again because the parent entity is not in
754       --  the declaration table if it did not match the search pattern.
755
756       ---------------------
757       -- Get_Symbol_Name --
758       ---------------------
759
760       function Get_Symbol_Name (Eun, Line, Col : Natural) return String is
761          Ptr    : Positive := 1;
762          E_Eun  : Positive;   --  Unit number of current entity
763          E_Line : Natural;    --  Line number of current entity
764          E_Col  : Natural;    --  Column number of current entity
765          E_Name : Positive;   --  Pointer to begin of entity name
766          E_Type : Character;  --  Type of current entity
767
768       begin
769          --  Look for the X lines corresponding to unit Eun
770
771          loop
772             if Ali (Ptr) = 'X' then
773                Ptr := Ptr + 1;
774                Parse_Number (Ali, Ptr, E_Eun);
775                exit when E_Eun = Eun;
776             end if;
777
778             Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
779          end loop;
780
781          --  Here we are in the right Ali section, we now look for the entity
782          --  declared at position (Line, Col).
783
784          loop
785             Parse_Number (Ali, Ptr, E_Line);
786             E_Type := Ali (Ptr);
787             exit when Ali (Ptr) = EOF;
788             Ptr := Ptr + 1;
789             Parse_Number (Ali, Ptr, E_Col);
790             exit when Ali (Ptr) = EOF;
791             Ptr := Ptr + 1;
792
793             if Line = E_Line and then Col = E_Col then
794                Parse_Token (Ali, Ptr, E_Name);
795                return Ali (E_Name .. Ptr - 1);
796             end if;
797
798             Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
799             exit when Ali (Ptr) = EOF;
800          end loop;
801
802          --  We were not able to find the symbol, this should not happend but
803          --  since we don't want to stop here we return a string of three
804          --  question marks as the symbol name.
805
806          return "???";
807       end Get_Symbol_Name;
808
809    --  Start of processing for Parse_Identifier_Info
810
811    begin
812       --  The identifier info looks like:
813       --     "38U9*Debug 12|36r6 36r19"
814
815       --  Extract the line, column and entity name information
816
817       Parse_Number (Ali, Ptr, E_Line);
818
819       if Ali (Ptr) > ' ' then
820          E_Type := Ali (Ptr);
821          Ptr := Ptr + 1;
822       end if;
823
824       --  Ignore some of the entities (labels,...)
825
826       case E_Type is
827          when 'l' | 'L' | 'q' =>
828             Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
829             return;
830
831          when others =>
832             null;
833       end case;
834
835       Parse_Number (Ali, Ptr, E_Col);
836
837       E_Global := False;
838       if Ali (Ptr) >= ' ' then
839          E_Global := (Ali (Ptr) = '*');
840          Ptr := Ptr + 1;
841       end if;
842
843       Parse_Token (Ali, Ptr, E_Name);
844
845       --  Exit if the symbol does not match
846       --  or if we have a local symbol and we do not want it
847
848       if (not Local_Symbols and not E_Global)
849         or else (Pattern.Initialized
850                   and then not Match (Ali (E_Name .. Ptr - 1), Pattern.Entity))
851         or else (E_Name >= Ptr)
852       then
853          Decl_Ref := Add_Declaration
854            (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type,
855             Remove_Only => True);
856          Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
857          return;
858       end if;
859
860       --  Insert the declaration in the table
861
862       Decl_Ref := Add_Declaration
863         (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type);
864
865       if Ali (Ptr) = '<'
866         or else Ali (Ptr) = '('
867         or else Ali (Ptr) = '{'
868       then
869          --  Here we have a type derivation information. The format is
870          --  <3|12I45> which means that the current entity is derived from the
871          --  type defined in unit number 3, line 12 column 45. The pipe and
872          --  unit number is optional. It is specified only if the parent type
873          --  is not defined in the current unit.
874
875          --  We also have the format for generic instantiations, as in
876          --  7a5*Uid(3|5I8[4|2]) 2|4r74
877
878          --  We could also have something like
879          --  16I9*I<integer>
880          --  that indicates that I derives from the predefined type integer.
881
882          Ptr := Ptr + 1;
883
884          if Ali (Ptr) in '0' .. '9' then
885             Parse_Derived_Info : declare
886                P_Line   : Natural;          --  parent entity line
887                P_Column : Natural;          --  parent entity column
888                P_Type   : Character;        --  parent entity type
889                P_Eun    : Positive;         --  parent entity file number
890
891             begin
892                Parse_Number (Ali, Ptr, P_Line);
893
894                --  If we have a pipe then the first number was the unit number
895
896                if Ali (Ptr) = '|' then
897                   P_Eun := P_Line;
898                   Ptr := Ptr + 1;
899
900                   --  Now we have the line number
901
902                   Parse_Number (Ali, Ptr, P_Line);
903
904                else
905                   --  We don't have a unit number specified, so we set P_Eun to
906                   --  the current unit.
907
908                   for K in Dependencies_Tables.First .. Last (File.Dep) loop
909                      P_Eun := K;
910                      exit when File.Dep.Table (K) = File_Ref;
911                   end loop;
912                end if;
913
914                --  Then parse the type and column number
915
916                P_Type := Ali (Ptr);
917                Ptr := Ptr + 1;
918                Parse_Number (Ali, Ptr, P_Column);
919
920                --  Skip the information for generics instantiations
921
922                if Ali (Ptr) = '[' then
923                   declare
924                      Num_Brackets : Natural := 1;
925                   begin
926                      while Num_Brackets /= 0 loop
927                         Ptr := Ptr + 1;
928                         if Ali (Ptr) = '[' then
929                            Num_Brackets := Num_Brackets + 1;
930                         elsif Ali (Ptr) = ']' then
931                            Num_Brackets := Num_Brackets - 1;
932                         end if;
933                      end loop;
934
935                      Ptr := Ptr + 1;
936                   end;
937                end if;
938
939                --  Skip '>', or ')' or '>'
940
941                Ptr := Ptr + 1;
942
943                --  The derived info is needed only is the derived info mode is
944                --  on or if we want to output the type hierarchy
945
946                if Der_Info or else Type_Tree then
947                   declare
948                      Symbol : constant String :=
949                                 Get_Symbol_Name (P_Eun, P_Line, P_Column);
950                   begin
951                      if Symbol /= "???" then
952                         Add_Parent
953                           (Decl_Ref,
954                            Symbol,
955                            P_Line,
956                            P_Column,
957                            File.Dep.Table (P_Eun));
958                      end if;
959                   end;
960                end if;
961
962                if Type_Tree
963                  and then (Pattern.File_Ref = Empty_File
964                              or else
965                            Pattern.File_Ref = Current_Xref_File (File))
966                then
967                   Search_Parent_Tree : declare
968                      Pattern         : Search_Pattern;  --  Parent type pattern
969                      File_Pos_Backup : Positive;
970
971                   begin
972                      Add_Entity
973                        (Pattern,
974                         Get_Symbol_Name (P_Eun, P_Line, P_Column)
975                         & ':' & Get_Gnatchop_File (File.Dep.Table (P_Eun))
976                         & ':' & Get_Line (Get_Parent (Decl_Ref))
977                         & ':' & Get_Column (Get_Parent (Decl_Ref)),
978                         False);
979
980                      --  No default match is needed to look for the parent type
981                      --  since we are using the fully qualified symbol name:
982                      --  symbol:file:line:column
983
984                      Set_Default_Match (False);
985
986                      --  The parent hierarchy is defined in the same unit as
987                      --  the derived type. So we want to revisit the unit.
988
989                      File_Pos_Backup   := File.Current_Line;
990
991                      Skip_To_First_X_Line
992                        (File, D_Lines => False, W_Lines => False);
993
994                      while File.Buffer (File.Current_Line) /= EOF loop
995                         Parse_X_Filename (File);
996                         Parse_Identifier_Info
997                           (Pattern       => Pattern,
998                            File          => File,
999                            Local_Symbols => False,
1000                            Der_Info      => Der_Info,
1001                            Type_Tree     => True,
1002                            Wide_Search   => False,
1003                            Labels_As_Ref => Labels_As_Ref);
1004                      end loop;
1005
1006                      File.Current_Line := File_Pos_Backup;
1007                   end Search_Parent_Tree;
1008                end if;
1009             end Parse_Derived_Info;
1010
1011          else
1012             while Ali (Ptr) /= '>'
1013               and then Ali (Ptr) /= ')'
1014               and then Ali (Ptr) /= '}'
1015             loop
1016                Ptr := Ptr + 1;
1017             end loop;
1018             Ptr := Ptr + 1;
1019          end if;
1020
1021       elsif Ali (Ptr) = '=' then
1022          declare
1023             P_Line, P_Column : Natural;
1024
1025          begin
1026             Ptr := Ptr + 1;
1027             Parse_Number (Ali, Ptr, P_Line);
1028             Ptr := Ptr + 1;
1029             Parse_Number (Ali, Ptr, P_Column);
1030          end;
1031       end if;
1032
1033       --  To find the body, we will have to parse the file too
1034
1035       if Wide_Search then
1036          declare
1037             File_Ref     : File_Reference;
1038             File_Name    : constant String :=
1039                              Get_Gnatchop_File (File.X_File);
1040          begin
1041             File_Ref := Add_To_Xref_File (ALI_File_Name (File_Name), False);
1042          end;
1043       end if;
1044
1045       --  Parse references to this entity.
1046       --  Ptr points to next reference with leading blanks
1047
1048       loop
1049          --  Process references on current line
1050
1051          while Ali (Ptr) = ' ' or Ali (Ptr) = ASCII.HT loop
1052
1053             --  For every reference read the line, type and column,
1054             --  optionally preceded by a file number and a pipe symbol.
1055
1056             Parse_Number (Ali, Ptr, R_Line);
1057
1058             if Ali (Ptr) = Pipe then
1059                Ptr := Ptr + 1;
1060                File_Ref := File_Name (File, R_Line);
1061
1062                Parse_Number (Ali, Ptr, R_Line);
1063             end if;
1064
1065             if Ali (Ptr) > ' ' then
1066                R_Type := Ali (Ptr);
1067                Ptr := Ptr + 1;
1068             end if;
1069
1070             --  Imported entities might special indication as to their external
1071             --  name:
1072             --    5U14*Foo2 5>20 6b<c,myfoo2>22
1073
1074             if R_Type = 'b'
1075               and then Ali (Ptr) = '<'
1076             then
1077                while Ptr <= Ali'Last
1078                  and then Ali (Ptr) /= '>'
1079                loop
1080                   Ptr := Ptr + 1;
1081                end loop;
1082                Ptr := Ptr + 1;
1083             end if;
1084
1085             Parse_Number (Ali, Ptr, R_Col);
1086
1087             --  Insert the reference or body in the table
1088
1089             Add_Reference
1090               (Decl_Ref, File_Ref, R_Line, R_Col, R_Type, Labels_As_Ref);
1091
1092             --  Skip generic information, if any
1093
1094             if Ali (Ptr) = '[' then
1095                declare
1096                   Num_Nested : Integer := 1;
1097
1098                begin
1099                   Ptr := Ptr + 1;
1100                   while Num_Nested /= 0 loop
1101                      if Ali (Ptr) = ']' then
1102                         Num_Nested := Num_Nested - 1;
1103                      elsif Ali (Ptr) = '[' then
1104                         Num_Nested := Num_Nested + 1;
1105                      end if;
1106
1107                      Ptr := Ptr + 1;
1108                   end loop;
1109                end;
1110             end if;
1111
1112          end loop;
1113
1114          Parse_EOL (Ali, Ptr);
1115
1116          --   Loop until new line is no continuation line
1117
1118          exit when Ali (Ptr) /= '.';
1119          Ptr := Ptr + 1;
1120       end loop;
1121    end Parse_Identifier_Info;
1122
1123    ------------------
1124    -- Parse_Number --
1125    ------------------
1126
1127    procedure Parse_Number
1128      (Source : access String;
1129       Ptr    : in out Positive;
1130       Number : out Natural)
1131    is
1132    begin
1133       --  Skip separators
1134
1135       while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1136          Ptr := Ptr + 1;
1137       end loop;
1138
1139       Number := 0;
1140       while Source (Ptr) in '0' .. '9' loop
1141          Number :=
1142            10 * Number + (Character'Pos (Source (Ptr)) - Character'Pos ('0'));
1143          Ptr := Ptr + 1;
1144       end loop;
1145    end Parse_Number;
1146
1147    -----------------
1148    -- Parse_Token --
1149    -----------------
1150
1151    procedure Parse_Token
1152      (Source    : access String;
1153       Ptr       : in out Positive;
1154       Token_Ptr : out Positive)
1155    is
1156       In_Quotes : Character := ASCII.NUL;
1157
1158    begin
1159       --  Skip separators
1160
1161       while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1162          Ptr := Ptr + 1;
1163       end loop;
1164
1165       Token_Ptr := Ptr;
1166
1167       --  Find end-of-token
1168
1169       while (In_Quotes /= ASCII.NUL or else
1170                not (Source (Ptr) = ' '
1171                      or else Source (Ptr) = ASCII.HT
1172                      or else Source (Ptr) = '<'
1173                      or else Source (Ptr) = '{'
1174                      or else Source (Ptr) = '='
1175                      or else Source (Ptr) = '('))
1176         and then Source (Ptr) >= ' '
1177       loop
1178          --  Double-quotes are used for operators
1179          --  Simple-quotes are used for character constants, for instance when
1180          --  they are found in an enumeration type "type A is ('+', '-');"
1181
1182          case Source (Ptr) is
1183             when '"' | ''' =>
1184                if In_Quotes = Source (Ptr) then
1185                   In_Quotes := ASCII.NUL;
1186                elsif In_Quotes = ASCII.NUL then
1187                   In_Quotes := Source (Ptr);
1188                end if;
1189
1190             when others =>
1191                null;
1192          end case;
1193
1194          Ptr := Ptr + 1;
1195       end loop;
1196    end Parse_Token;
1197
1198    ----------------------
1199    -- Parse_X_Filename --
1200    ----------------------
1201
1202    procedure Parse_X_Filename (File : in out ALI_File) is
1203       Ali     : String_Access renames File.Buffer;
1204       Ptr     : Positive renames File.Current_Line;
1205       File_Nr : Natural;
1206
1207    begin
1208       while Ali (Ptr) = 'X' loop
1209
1210          --  The current line is the start of a new Xref file section,
1211          --  whose format looks like:
1212
1213          --     " X 1 debug.ads"
1214
1215          --  Skip the X and read the file number for the new X_File
1216
1217          Ptr := Ptr + 1;
1218          Parse_Number (Ali, Ptr, File_Nr);
1219
1220          if File_Nr > 0 then
1221             File.X_File := File.Dep.Table (File_Nr);
1222          end if;
1223
1224          Parse_EOL (Ali, Ptr);
1225       end loop;
1226    end Parse_X_Filename;
1227
1228    --------------------
1229    -- Print_Gnatfind --
1230    --------------------
1231
1232    procedure Print_Gnatfind
1233      (References     : Boolean;
1234       Full_Path_Name : Boolean)
1235    is
1236       Decls : constant Declaration_Array_Access := Get_Declarations;
1237       Decl  : Declaration_Reference;
1238       Arr   : Reference_Array_Access;
1239
1240       procedure Print_Ref
1241         (Ref : Reference;
1242          Msg : String := "      ");
1243       --  Print a reference, according to the extended tag of the output
1244
1245       ---------------
1246       -- Print_Ref --
1247       ---------------
1248
1249       procedure Print_Ref
1250         (Ref : Reference;
1251          Msg : String := "      ")
1252       is
1253          F : String_Access :=
1254                Osint.To_Host_File_Spec
1255                 (Get_Gnatchop_File (Ref, Full_Path_Name));
1256
1257          Buffer : constant String :=
1258                     F.all &
1259                     ":" & Get_Line (Ref)   &
1260                     ":" & Get_Column (Ref) &
1261                     ": ";
1262
1263          Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1264
1265       begin
1266          Free (F);
1267          Num_Blanks := Integer'Max (0, Num_Blanks);
1268          Write_Line
1269            (Buffer
1270             & String'(1 .. Num_Blanks => ' ')
1271             & Msg & " " & Get_Symbol (Decl));
1272
1273          if Get_Source_Line (Ref)'Length /= 0 then
1274             Write_Line ("   " & Get_Source_Line (Ref));
1275          end if;
1276       end Print_Ref;
1277
1278    --  Start of processing for Print_Gnatfind
1279
1280    begin
1281       for D in Decls'Range loop
1282          Decl := Decls (D);
1283
1284          if Match (Decl) then
1285
1286             --  Output the declaration
1287
1288             declare
1289                Parent : constant Declaration_Reference := Get_Parent (Decl);
1290
1291                F : String_Access :=
1292                      Osint.To_Host_File_Spec
1293                       (Get_Gnatchop_File (Decl, Full_Path_Name));
1294
1295                Buffer : constant String :=
1296                           F.all &
1297                           ":" & Get_Line (Decl)   &
1298                           ":" & Get_Column (Decl) &
1299                           ": ";
1300
1301                Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1302
1303             begin
1304                Free (F);
1305                Num_Blanks := Integer'Max (0, Num_Blanks);
1306                Write_Line
1307                  (Buffer & String'(1 .. Num_Blanks => ' ')
1308                   & "(spec) " & Get_Symbol (Decl));
1309
1310                if Parent /= Empty_Declaration then
1311                   F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1312                   Write_Line
1313                     (Buffer & String'(1 .. Num_Blanks => ' ')
1314                      & "   derived from " & Get_Symbol (Parent)
1315                      & " ("
1316                      & F.all
1317                      & ':' & Get_Line (Parent)
1318                      & ':' & Get_Column (Parent) & ')');
1319                   Free (F);
1320                end if;
1321             end;
1322
1323             if Get_Source_Line (Decl)'Length /= 0 then
1324                Write_Line ("   " & Get_Source_Line (Decl));
1325             end if;
1326
1327             --  Output the body (sorted)
1328
1329             Arr := Get_References (Decl, Get_Bodies => True);
1330
1331             for R in Arr'Range loop
1332                Print_Ref (Arr (R), "(body)");
1333             end loop;
1334
1335             Free (Arr);
1336
1337             if References then
1338                Arr := Get_References
1339                  (Decl, Get_Writes => True, Get_Reads => True);
1340
1341                for R in Arr'Range loop
1342                   Print_Ref (Arr (R));
1343                end loop;
1344
1345                Free (Arr);
1346             end if;
1347          end if;
1348       end loop;
1349    end Print_Gnatfind;
1350
1351    ------------------
1352    -- Print_Unused --
1353    ------------------
1354
1355    procedure Print_Unused (Full_Path_Name : in Boolean) is
1356       Decls : constant Declaration_Array_Access := Get_Declarations;
1357       Decl  : Declaration_Reference;
1358       Arr   : Reference_Array_Access;
1359       F     : String_Access;
1360
1361    begin
1362       for D in Decls'Range loop
1363          Decl := Decls (D);
1364
1365          if References_Count
1366              (Decl, Get_Reads => True, Get_Writes => True) = 0
1367          then
1368             F := Osint.To_Host_File_Spec
1369               (Get_Gnatchop_File (Decl, Full_Path_Name));
1370             Write_Str (Get_Symbol (Decl)
1371                         & " ("
1372                         & Get_Full_Type (Decl)
1373                         & ") "
1374                         & F.all
1375                         & ':'
1376                         & Get_Line (Decl)
1377                         & ':'
1378                         & Get_Column (Decl));
1379             Free (F);
1380
1381             --  Print the body if any
1382
1383             Arr := Get_References (Decl, Get_Bodies => True);
1384
1385             for R in Arr'Range loop
1386                F := Osint.To_Host_File_Spec
1387                       (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1388                Write_Str (' '
1389                            & F.all
1390                            & ':' & Get_Line (Arr (R))
1391                            & ':' & Get_Column (Arr (R)));
1392                Free (F);
1393             end loop;
1394
1395             Write_Eol;
1396             Free (Arr);
1397          end if;
1398       end loop;
1399    end Print_Unused;
1400
1401    --------------
1402    -- Print_Vi --
1403    --------------
1404
1405    procedure Print_Vi (Full_Path_Name : in Boolean) is
1406       Tab   : constant Character := ASCII.HT;
1407       Decls : constant Declaration_Array_Access :=
1408                 Get_Declarations (Sorted => False);
1409       Decl  : Declaration_Reference;
1410       Arr   : Reference_Array_Access;
1411       F     : String_Access;
1412
1413    begin
1414       for D in Decls'Range loop
1415          Decl := Decls (D);
1416
1417          F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1418          Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1419          Free (F);
1420
1421          --  Print the body if any
1422
1423          Arr := Get_References (Decl, Get_Bodies => True);
1424
1425          for R in Arr'Range loop
1426             F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1427             Write_Line
1428               (Get_Symbol (Decl) & Tab & F.all & Tab  & Get_Line (Arr (R)));
1429             Free (F);
1430          end loop;
1431
1432          Free (Arr);
1433
1434          --  Print the modifications
1435
1436          Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1437
1438          for R in Arr'Range loop
1439             F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1440             Write_Line
1441               (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1442             Free (F);
1443          end loop;
1444
1445          Free (Arr);
1446       end loop;
1447    end Print_Vi;
1448
1449    ----------------
1450    -- Print_Xref --
1451    ----------------
1452
1453    procedure Print_Xref (Full_Path_Name : in Boolean) is
1454       Decls : constant Declaration_Array_Access := Get_Declarations;
1455       Decl : Declaration_Reference;
1456
1457       Margin : constant := 10;
1458       --  Column where file names start
1459
1460       procedure New_Line80;
1461       --  Go to start of new line
1462
1463       procedure Print80 (S : in String);
1464       --  Print the text, respecting the 80 columns rule.
1465
1466       procedure Print_Ref (Line, Column : String);
1467       --  The beginning of the output is aligned on a column multiple of 9
1468
1469       procedure Print_List
1470         (Decl       : Declaration_Reference;
1471          Msg        : String;
1472          Get_Reads  : Boolean := False;
1473          Get_Writes : Boolean := False;
1474          Get_Bodies : Boolean := False);
1475       --  Print a list of references. If the list is not empty, Msg will
1476       --  be printed prior to the list.
1477
1478       ----------------
1479       -- New_Line80 --
1480       ----------------
1481
1482       procedure New_Line80 is
1483       begin
1484          Write_Eol;
1485          Write_Str (String'(1 .. Margin - 1 => ' '));
1486       end New_Line80;
1487
1488       -------------
1489       -- Print80 --
1490       -------------
1491
1492       procedure Print80 (S : in String) is
1493          Align : Natural := Margin - (Integer (Column) mod Margin);
1494
1495       begin
1496          if Align = Margin then
1497             Align := 0;
1498          end if;
1499
1500          Write_Str (String'(1 .. Align => ' ') & S);
1501       end Print80;
1502
1503       ---------------
1504       -- Print_Ref --
1505       ---------------
1506
1507       procedure Print_Ref (Line, Column : String) is
1508          Line_Align : constant Integer := 4 - Line'Length;
1509
1510          S : constant String := String'(1 .. Line_Align => ' ')
1511                                   & Line & ':' & Column;
1512
1513          Align : Natural := Margin - (Integer (Output.Column) mod Margin);
1514
1515       begin
1516          if Align = Margin then
1517             Align := 0;
1518          end if;
1519
1520          if Integer (Output.Column) + Align + S'Length > 79 then
1521             New_Line80;
1522             Align := 0;
1523          end if;
1524
1525          Write_Str (String'(1 .. Align => ' ') & S);
1526       end Print_Ref;
1527
1528       ----------------
1529       -- Print_List --
1530       ----------------
1531
1532       procedure Print_List
1533         (Decl       : Declaration_Reference;
1534          Msg        : String;
1535          Get_Reads  : Boolean := False;
1536          Get_Writes : Boolean := False;
1537          Get_Bodies : Boolean := False)
1538       is
1539          Arr : Reference_Array_Access :=
1540                  Get_References
1541                    (Decl,
1542                     Get_Writes => Get_Writes,
1543                     Get_Reads  => Get_Reads,
1544                     Get_Bodies => Get_Bodies);
1545          File : File_Reference := Empty_File;
1546          F    : String_Access;
1547
1548       begin
1549          if Arr'Length /= 0 then
1550             Write_Eol;
1551             Write_Str (Msg);
1552          end if;
1553
1554          for R in Arr'Range loop
1555             if Get_File_Ref (Arr (R)) /= File then
1556                if File /= Empty_File then
1557                   New_Line80;
1558                end if;
1559
1560                File := Get_File_Ref (Arr (R));
1561                F := Osint.To_Host_File_Spec
1562                  (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1563                Write_Str (F.all & ' ');
1564                Free (F);
1565             end if;
1566
1567             Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1568          end loop;
1569
1570          Free (Arr);
1571       end Print_List;
1572
1573       F : String_Access;
1574
1575    --  Start of processing for Print_Xref
1576
1577    begin
1578       for D in Decls'Range loop
1579          Decl := Decls (D);
1580
1581          Write_Str (Get_Symbol (Decl));
1582
1583          while Column < Type_Position loop
1584             Write_Char (' ');
1585          end loop;
1586
1587          Write_Line (Get_Full_Type (Decl));
1588
1589          Write_Parent_Info : declare
1590             Parent : constant Declaration_Reference := Get_Parent (Decl);
1591
1592          begin
1593             if Parent /= Empty_Declaration then
1594                Write_Str ("  Ptype: ");
1595                F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1596                Print80 (F.all);
1597                Free (F);
1598                Print_Ref (Get_Line (Parent), Get_Column (Parent));
1599                Print80 ("  " & Get_Symbol (Parent));
1600                Write_Eol;
1601             end if;
1602          end Write_Parent_Info;
1603
1604          Write_Str ("  Decl:  ");
1605          F := Osint.To_Host_File_Spec
1606                (Get_Gnatchop_File (Decl, Full_Path_Name));
1607          Print80 (F.all & ' ');
1608          Free (F);
1609          Print_Ref (Get_Line (Decl), Get_Column (Decl));
1610
1611          Print_List
1612            (Decl, "  Body:  ", Get_Bodies => True);
1613          Print_List
1614            (Decl, "  Modi:  ", Get_Writes => True);
1615          Print_List
1616            (Decl, "  Ref:   ", Get_Reads => True);
1617          Write_Eol;
1618       end loop;
1619    end Print_Xref;
1620
1621    ------------
1622    -- Search --
1623    ------------
1624
1625    procedure Search
1626      (Pattern       : Search_Pattern;
1627       Local_Symbols : Boolean;
1628       Wide_Search   : Boolean;
1629       Read_Only     : Boolean;
1630       Der_Info      : Boolean;
1631       Type_Tree     : Boolean)
1632    is
1633       type String_Access is access String;
1634       procedure Free is new Unchecked_Deallocation (String, String_Access);
1635
1636       ALIfile   : ALI_File;
1637       File_Ref  : File_Reference;
1638       Strip_Num : Natural := 0;
1639       Ali_Name  : String_Access;
1640
1641    begin
1642       --  If we want all the .ali files, then find them
1643
1644       if Wide_Search then
1645          Find_ALI_Files;
1646       end if;
1647
1648       loop
1649          --  Get the next unread ali file
1650
1651          File_Ref := Next_Unvisited_File;
1652
1653          exit when File_Ref = Empty_File;
1654
1655          --  Find the ALI file to use. Most of the time, it will be the unit
1656          --  name, with a different extension. However, when dealing with
1657          --  separates the ALI file is in fact the parent's ALI file (and this
1658          --  is recursive, in case the parent itself is a separate).
1659
1660          Strip_Num := 0;
1661          loop
1662             Free (Ali_Name);
1663             Ali_Name := new String'
1664               (Get_File (File_Ref, With_Dir => True, Strip => Strip_Num));
1665
1666             --  Stripped too many things...
1667
1668             if Ali_Name.all = "" then
1669                if Get_Emit_Warning (File_Ref) then
1670                   Set_Standard_Error;
1671                   Write_Line
1672                     ("warning : file " & Get_File (File_Ref, With_Dir => True)
1673                      & " not found");
1674                   Set_Standard_Output;
1675                end if;
1676                Free (Ali_Name);
1677                exit;
1678
1679             --  If not found, try the parent's ALI file (this is needed for
1680             --  separate units and subprograms).
1681
1682             --  Reset the cached directory first, in case the separate's
1683             --  ALI file is not in the same directory.
1684
1685             elsif not File_Exists (Ali_Name.all) then
1686                Strip_Num := Strip_Num + 1;
1687                Reset_Directory (File_Ref);
1688
1689             --  Else we finally found it
1690
1691             else
1692                exit;
1693             end if;
1694          end loop;
1695
1696          --  If we had to get the parent's ALI, insert it in the list as usual.
1697          --  This is to avoid parsing it twice in case it has already been
1698          --  parsed.
1699
1700          if Ali_Name /= null and then Strip_Num /= 0 then
1701             File_Ref := Add_To_Xref_File
1702               (File_Name => Ali_Name.all,
1703                Visited   => False);
1704
1705          --  Now that we have a file name, parse it to find any reference to
1706          --  the entity.
1707
1708          elsif Ali_Name /= null
1709            and then (Read_Only or else Is_Writable_File (Ali_Name.all))
1710          then
1711             begin
1712                Open (Ali_Name.all, ALIfile);
1713                while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1714                   Parse_X_Filename (ALIfile);
1715                   Parse_Identifier_Info
1716                     (Pattern, ALIfile, Local_Symbols,
1717                      Der_Info, Type_Tree, Wide_Search, Labels_As_Ref => True);
1718                end loop;
1719
1720             exception
1721                when No_Xref_Information   =>
1722                   if Get_Emit_Warning (File_Ref) then
1723                      Set_Standard_Error;
1724                      Write_Line
1725                        ("warning : No cross-referencing information in  "
1726                         & Ali_Name.all);
1727                      Set_Standard_Output;
1728                   end if;
1729             end;
1730          end if;
1731       end loop;
1732
1733       Free (Ali_Name);
1734    end Search;
1735
1736    -----------------
1737    -- Search_Xref --
1738    -----------------
1739
1740    procedure Search_Xref
1741      (Local_Symbols : Boolean;
1742       Read_Only     : Boolean;
1743       Der_Info      : Boolean)
1744    is
1745       ALIfile      : ALI_File;
1746       File_Ref     : File_Reference;
1747       Null_Pattern : Search_Pattern;
1748
1749    begin
1750       Null_Pattern.Initialized := False;
1751
1752       loop
1753          --  Find the next unvisited file
1754
1755          File_Ref := Next_Unvisited_File;
1756          exit when File_Ref = Empty_File;
1757
1758          --  Search the object directories for the .ali file
1759
1760          declare
1761             F : constant String := Get_File (File_Ref, With_Dir => True);
1762
1763          begin
1764             if Read_Only or else Is_Writable_File (F) then
1765                Open (F, ALIfile, True);
1766
1767                while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1768                   Parse_X_Filename (ALIfile);
1769                   Parse_Identifier_Info
1770                     (Null_Pattern, ALIfile, Local_Symbols, Der_Info,
1771                      Labels_As_Ref => False);
1772                end loop;
1773             end if;
1774
1775          exception
1776             when No_Xref_Information =>  null;
1777          end;
1778       end loop;
1779    end Search_Xref;
1780
1781 end Xref_Lib;