OSDN Git Service

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