OSDN Git Service

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