OSDN Git Service

2009-02-27 Tobias Burnus <burnus@net-b.de>
[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-2008, 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
485       --  parameter or not
486
487       ------------------
488       -- Param_String --
489       ------------------
490
491       function Param_String return String is
492       begin
493          if Is_Parameter (Decl) then
494             return "parameter ";
495          else
496             return "";
497          end if;
498       end Param_String;
499
500    --  Start of processing for Get_Full_Type
501
502    begin
503       case Get_Type (Decl) is
504          when 'A' => return "array type";
505          when 'B' => return "boolean type";
506          when 'C' => return "class-wide type";
507          when 'D' => return "decimal type";
508          when 'E' => return "enumeration type";
509          when 'F' => return "float type";
510          when 'I' => return "integer type";
511          when 'M' => return "modular type";
512          when 'O' => return "fixed type";
513          when 'P' => return "access type";
514          when 'R' => return "record type";
515          when 'S' => return "string type";
516          when 'T' => return "task type";
517          when 'W' => return "protected type";
518
519          when 'a' => return "array type";
520          when 'b' => return Param_String & "boolean object";
521          when 'c' => return Param_String & "class-wide object";
522          when 'd' => return Param_String & "decimal object";
523          when 'e' => return Param_String & "enumeration object";
524          when 'f' => return Param_String & "float object";
525          when 'h' => return "interface";
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       pragma Warnings (Off, Ali);
674
675    begin
676       if File.Buffer /= null then
677          Free (File.Buffer);
678       end if;
679
680       Init (File.Dep);
681
682       begin
683          Read_File (Name, Ali);
684
685       exception
686          when Ada.Text_IO.Name_Error | Ada.Text_IO.End_Error =>
687             raise No_Xref_Information;
688       end;
689
690       Skip_To_First_X_Line (File, D_Lines => True, W_Lines => Dependencies);
691    end Open;
692
693    ---------------
694    -- Parse_EOL --
695    ---------------
696
697    procedure Parse_EOL
698      (Source                 : not null access String;
699       Ptr                    : in out Positive;
700       Skip_Continuation_Line : Boolean := False)
701    is
702    begin
703       loop
704          --  Skip to end of line
705
706          while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
707            and then Source (Ptr) /= EOF
708          loop
709             Ptr := Ptr + 1;
710          end loop;
711
712          if Source (Ptr) /= EOF then
713             Ptr := Ptr + 1;      -- skip CR or LF
714          end if;
715
716          --  Skip past CR/LF or LF/CR combination
717
718          if (Source (Ptr) = ASCII.CR or else Source (Ptr) = ASCII.LF)
719            and then Source (Ptr) /= Source (Ptr - 1)
720          then
721             Ptr := Ptr + 1;
722          end if;
723
724          exit when not Skip_Continuation_Line or else Source (Ptr) /= '.';
725       end loop;
726    end Parse_EOL;
727
728    ---------------------------
729    -- Parse_Identifier_Info --
730    ---------------------------
731
732    procedure Parse_Identifier_Info
733      (Pattern       : Search_Pattern;
734       File          : in out ALI_File;
735       Local_Symbols : Boolean;
736       Der_Info      : Boolean := False;
737       Type_Tree     : Boolean := False;
738       Wide_Search   : Boolean := True;
739       Labels_As_Ref : Boolean := True)
740    is
741       Ptr      : Positive renames File.Current_Line;
742       Ali      : String_Access renames File.Buffer;
743
744       E_Line   : Natural;   --  Line number of current entity
745       E_Col    : Natural;   --  Column number of current entity
746       E_Type   : Character; --  Type of current entity
747       E_Name   : Positive;  --  Pointer to begin of entity name
748       E_Global : Boolean;   --  True iff entity is global
749
750       R_Line   : Natural;   --  Line number of current reference
751       R_Col    : Natural;   --  Column number of current reference
752       R_Type   : Character; --  Type of current reference
753
754       Decl_Ref : Declaration_Reference;
755       File_Ref : File_Reference := Current_Xref_File (File);
756
757       function Get_Symbol_Name (Eun, Line, Col : Natural) return String;
758       --  Returns the symbol name for the entity defined at the specified
759       --  line and column in the dependent unit number Eun. For this we need
760       --  to parse the ali file again because the parent entity is not in
761       --  the declaration table if it did not match the search pattern.
762
763       procedure Skip_To_Matching_Closing_Bracket;
764       --  When Ptr points to an opening square bracket, moves it to the
765       --  character following the matching closing bracket
766
767       ---------------------
768       -- Get_Symbol_Name --
769       ---------------------
770
771       function Get_Symbol_Name (Eun, Line, Col : Natural) return String is
772          Ptr    : Positive := 1;
773          E_Eun  : Positive;   --  Unit number of current entity
774          E_Line : Natural;    --  Line number of current entity
775          E_Col  : Natural;    --  Column number of current entity
776          E_Name : Positive;   --  Pointer to begin of entity name
777
778       begin
779          --  Look for the X lines corresponding to unit Eun
780
781          loop
782             if Ali (Ptr) = 'X' then
783                Ptr := Ptr + 1;
784                Parse_Number (Ali, Ptr, E_Eun);
785                exit when E_Eun = Eun;
786             end if;
787
788             Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
789          end loop;
790
791          --  Here we are in the right Ali section, we now look for the entity
792          --  declared at position (Line, Col).
793
794          loop
795             Parse_Number (Ali, Ptr, E_Line);
796             exit when Ali (Ptr) = EOF;
797             Ptr := Ptr + 1;
798             Parse_Number (Ali, Ptr, E_Col);
799             exit when Ali (Ptr) = EOF;
800             Ptr := Ptr + 1;
801
802             if Line = E_Line and then Col = E_Col then
803                Parse_Token (Ali, Ptr, E_Name);
804                return Ali (E_Name .. Ptr - 1);
805             end if;
806
807             Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
808             exit when Ali (Ptr) = EOF;
809          end loop;
810
811          --  We were not able to find the symbol, this should not happen but
812          --  since we don't want to stop here we return a string of three
813          --  question marks as the symbol name.
814
815          return "???";
816       end Get_Symbol_Name;
817
818       --------------------------------------
819       -- Skip_To_Matching_Closing_Bracket --
820       --------------------------------------
821
822       procedure Skip_To_Matching_Closing_Bracket is
823          Num_Brackets : Natural;
824
825       begin
826          Num_Brackets := 1;
827          while Num_Brackets /= 0 loop
828             Ptr := Ptr + 1;
829             if Ali (Ptr) = '[' then
830                Num_Brackets := Num_Brackets + 1;
831             elsif Ali (Ptr) = ']' then
832                Num_Brackets := Num_Brackets - 1;
833             end if;
834          end loop;
835
836          Ptr := Ptr + 1;
837       end Skip_To_Matching_Closing_Bracket;
838
839    --  Start of processing for Parse_Identifier_Info
840
841    begin
842       --  The identifier info looks like:
843       --     "38U9*Debug 12|36r6 36r19"
844
845       --  Extract the line, column and entity name information
846
847       Parse_Number (Ali, Ptr, E_Line);
848
849       if Ali (Ptr) > ' ' then
850          E_Type := Ali (Ptr);
851          Ptr := Ptr + 1;
852       end if;
853
854       --  Ignore some of the entities (labels,...)
855
856       case E_Type is
857          when 'l' | 'L' | 'q' =>
858             Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
859             return;
860
861          when others =>
862             null;
863       end case;
864
865       Parse_Number (Ali, Ptr, E_Col);
866
867       E_Global := False;
868       if Ali (Ptr) >= ' ' then
869          E_Global := (Ali (Ptr) = '*');
870          Ptr := Ptr + 1;
871       end if;
872
873       Parse_Token (Ali, Ptr, E_Name);
874
875       --  Exit if the symbol does not match
876       --  or if we have a local symbol and we do not want it
877
878       if (not Local_Symbols and not E_Global)
879         or else (Pattern.Initialized
880                   and then not Match (Ali (E_Name .. Ptr - 1), Pattern.Entity))
881         or else (E_Name >= Ptr)
882       then
883          Decl_Ref := Add_Declaration
884            (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type,
885             Remove_Only => True);
886          Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
887          return;
888       end if;
889
890       --  Insert the declaration in the table
891
892       Decl_Ref := Add_Declaration
893         (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type);
894
895       if Ali (Ptr) = '[' then
896          Skip_To_Matching_Closing_Bracket;
897       end if;
898
899       --  Skip any renaming indication
900
901       if Ali (Ptr) = '=' then
902          declare
903             P_Line, P_Column : Natural;
904             pragma Warnings (Off, P_Line);
905             pragma Warnings (Off, P_Column);
906          begin
907             Ptr := Ptr + 1;
908             Parse_Number (Ali, Ptr, P_Line);
909             Ptr := Ptr + 1;
910             Parse_Number (Ali, Ptr, P_Column);
911          end;
912       end if;
913
914       if Ali (Ptr) = '<'
915         or else Ali (Ptr) = '('
916         or else Ali (Ptr) = '{'
917       then
918          --  Here we have a type derivation information. The format is
919          --  <3|12I45> which means that the current entity is derived from the
920          --  type defined in unit number 3, line 12 column 45. The pipe and
921          --  unit number is optional. It is specified only if the parent type
922          --  is not defined in the current unit.
923
924          --  We also have the format for generic instantiations, as in
925          --  7a5*Uid(3|5I8[4|2]) 2|4r74
926
927          --  We could also have something like
928          --  16I9*I<integer>
929          --  that indicates that I derives from the predefined type integer.
930
931          Ptr := Ptr + 1;
932
933          if Ali (Ptr) in '0' .. '9' then
934             Parse_Derived_Info : declare
935                P_Line   : Natural;          --  parent entity line
936                P_Column : Natural;          --  parent entity column
937                P_Eun    : Positive;         --  parent entity file number
938
939             begin
940                Parse_Number (Ali, Ptr, P_Line);
941
942                --  If we have a pipe then the first number was the unit number
943
944                if Ali (Ptr) = '|' then
945                   P_Eun := P_Line;
946                   Ptr := Ptr + 1;
947
948                   --  Now we have the line number
949
950                   Parse_Number (Ali, Ptr, P_Line);
951
952                else
953                   --  We don't have a unit number specified, so we set P_Eun to
954                   --  the current unit.
955
956                   for K in Dependencies_Tables.First .. Last (File.Dep) loop
957                      P_Eun := K;
958                      exit when File.Dep.Table (K) = File_Ref;
959                   end loop;
960                end if;
961
962                --  Then parse the type and column number
963
964                Ptr := Ptr + 1;
965                Parse_Number (Ali, Ptr, P_Column);
966
967                --  Skip the information for generics instantiations
968
969                if Ali (Ptr) = '[' then
970                   Skip_To_Matching_Closing_Bracket;
971                end if;
972
973                --  Skip '>', or ')' or '>'
974
975                Ptr := Ptr + 1;
976
977                --  The derived info is needed only is the derived info mode is
978                --  on or if we want to output the type hierarchy
979
980                if Der_Info or else Type_Tree then
981                   declare
982                      Symbol : constant String :=
983                                 Get_Symbol_Name (P_Eun, P_Line, P_Column);
984                   begin
985                      if Symbol /= "???" then
986                         Add_Parent
987                           (Decl_Ref,
988                            Symbol,
989                            P_Line,
990                            P_Column,
991                            File.Dep.Table (P_Eun));
992                      end if;
993                   end;
994                end if;
995
996                if Type_Tree
997                  and then (Pattern.File_Ref = Empty_File
998                              or else
999                            Pattern.File_Ref = Current_Xref_File (File))
1000                then
1001                   Search_Parent_Tree : declare
1002                      Pattern         : Search_Pattern;  --  Parent type pattern
1003                      File_Pos_Backup : Positive;
1004
1005                   begin
1006                      Add_Entity
1007                        (Pattern,
1008                         Get_Symbol_Name (P_Eun, P_Line, P_Column)
1009                         & ':' & Get_Gnatchop_File (File.Dep.Table (P_Eun))
1010                         & ':' & Get_Line (Get_Parent (Decl_Ref))
1011                         & ':' & Get_Column (Get_Parent (Decl_Ref)),
1012                         False);
1013
1014                      --  No default match is needed to look for the parent type
1015                      --  since we are using the fully qualified symbol name:
1016                      --  symbol:file:line:column
1017
1018                      Set_Default_Match (False);
1019
1020                      --  The parent hierarchy is defined in the same unit as
1021                      --  the derived type. So we want to revisit the unit.
1022
1023                      File_Pos_Backup   := File.Current_Line;
1024
1025                      Skip_To_First_X_Line
1026                        (File, D_Lines => False, W_Lines => False);
1027
1028                      while File.Buffer (File.Current_Line) /= EOF loop
1029                         Parse_X_Filename (File);
1030                         Parse_Identifier_Info
1031                           (Pattern       => Pattern,
1032                            File          => File,
1033                            Local_Symbols => False,
1034                            Der_Info      => Der_Info,
1035                            Type_Tree     => True,
1036                            Wide_Search   => False,
1037                            Labels_As_Ref => Labels_As_Ref);
1038                      end loop;
1039
1040                      File.Current_Line := File_Pos_Backup;
1041                   end Search_Parent_Tree;
1042                end if;
1043             end Parse_Derived_Info;
1044
1045          else
1046             while Ali (Ptr) /= '>'
1047               and then Ali (Ptr) /= ')'
1048               and then Ali (Ptr) /= '}'
1049             loop
1050                Ptr := Ptr + 1;
1051             end loop;
1052             Ptr := Ptr + 1;
1053          end if;
1054       end if;
1055
1056       --  To find the body, we will have to parse the file too
1057
1058       if Wide_Search then
1059          declare
1060             File_Ref : File_Reference;
1061             pragma Unreferenced (File_Ref);
1062             File_Name : constant String := Get_Gnatchop_File (File.X_File);
1063          begin
1064             File_Ref := Add_To_Xref_File (ALI_File_Name (File_Name), False);
1065          end;
1066       end if;
1067
1068       --  Parse references to this entity.
1069       --  Ptr points to next reference with leading blanks
1070
1071       loop
1072          --  Process references on current line
1073
1074          while Ali (Ptr) = ' ' or Ali (Ptr) = ASCII.HT loop
1075
1076             --  For every reference read the line, type and column,
1077             --  optionally preceded by a file number and a pipe symbol.
1078
1079             Parse_Number (Ali, Ptr, R_Line);
1080
1081             if Ali (Ptr) = Pipe then
1082                Ptr := Ptr + 1;
1083                File_Ref := File_Name (File, R_Line);
1084
1085                Parse_Number (Ali, Ptr, R_Line);
1086             end if;
1087
1088             if Ali (Ptr) > ' ' then
1089                R_Type := Ali (Ptr);
1090                Ptr := Ptr + 1;
1091             end if;
1092
1093             --  Imported entities might special indication as to their external
1094             --  name:
1095             --    5U14*Foo2 5>20 6b<c,myfoo2>22
1096
1097             if R_Type = 'b'
1098               and then Ali (Ptr) = '<'
1099             then
1100                while Ptr <= Ali'Last
1101                  and then Ali (Ptr) /= '>'
1102                loop
1103                   Ptr := Ptr + 1;
1104                end loop;
1105                Ptr := Ptr + 1;
1106             end if;
1107
1108             Parse_Number (Ali, Ptr, R_Col);
1109
1110             --  Insert the reference or body in the table
1111
1112             Add_Reference
1113               (Decl_Ref, File_Ref, R_Line, R_Col, R_Type, Labels_As_Ref);
1114
1115             --  Skip generic information, if any
1116
1117             if Ali (Ptr) = '[' then
1118                declare
1119                   Num_Nested : Integer := 1;
1120
1121                begin
1122                   Ptr := Ptr + 1;
1123                   while Num_Nested /= 0 loop
1124                      if Ali (Ptr) = ']' then
1125                         Num_Nested := Num_Nested - 1;
1126                      elsif Ali (Ptr) = '[' then
1127                         Num_Nested := Num_Nested + 1;
1128                      end if;
1129
1130                      Ptr := Ptr + 1;
1131                   end loop;
1132                end;
1133             end if;
1134
1135          end loop;
1136
1137          Parse_EOL (Ali, Ptr);
1138
1139          --   Loop until new line is no continuation line
1140
1141          exit when Ali (Ptr) /= '.';
1142          Ptr := Ptr + 1;
1143       end loop;
1144    end Parse_Identifier_Info;
1145
1146    ------------------
1147    -- Parse_Number --
1148    ------------------
1149
1150    procedure Parse_Number
1151      (Source : not null access String;
1152       Ptr    : in out Positive;
1153       Number : out Natural)
1154    is
1155    begin
1156       --  Skip separators
1157
1158       while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1159          Ptr := Ptr + 1;
1160       end loop;
1161
1162       Number := 0;
1163       while Source (Ptr) in '0' .. '9' loop
1164          Number :=
1165            10 * Number + (Character'Pos (Source (Ptr)) - Character'Pos ('0'));
1166          Ptr := Ptr + 1;
1167       end loop;
1168    end Parse_Number;
1169
1170    -----------------
1171    -- Parse_Token --
1172    -----------------
1173
1174    procedure Parse_Token
1175      (Source    : not null access String;
1176       Ptr       : in out Positive;
1177       Token_Ptr : out Positive)
1178    is
1179       In_Quotes : Character := ASCII.NUL;
1180
1181    begin
1182       --  Skip separators
1183
1184       while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1185          Ptr := Ptr + 1;
1186       end loop;
1187
1188       Token_Ptr := Ptr;
1189
1190       --  Find end-of-token
1191
1192       while (In_Quotes /= ASCII.NUL or else
1193                not (Source (Ptr) = ' '
1194                      or else Source (Ptr) = ASCII.HT
1195                      or else Source (Ptr) = '<'
1196                      or else Source (Ptr) = '{'
1197                      or else Source (Ptr) = '['
1198                      or else Source (Ptr) = '='
1199                      or else Source (Ptr) = '('))
1200         and then Source (Ptr) >= ' '
1201       loop
1202          --  Double-quotes are used for operators
1203          --  Simple-quotes are used for character constants, for instance when
1204          --  they are found in an enumeration type "type A is ('+', '-');"
1205
1206          case Source (Ptr) is
1207             when '"' | ''' =>
1208                if In_Quotes = Source (Ptr) then
1209                   In_Quotes := ASCII.NUL;
1210                elsif In_Quotes = ASCII.NUL then
1211                   In_Quotes := Source (Ptr);
1212                end if;
1213
1214             when others =>
1215                null;
1216          end case;
1217
1218          Ptr := Ptr + 1;
1219       end loop;
1220    end Parse_Token;
1221
1222    ----------------------
1223    -- Parse_X_Filename --
1224    ----------------------
1225
1226    procedure Parse_X_Filename (File : in out ALI_File) is
1227       Ali     : String_Access renames File.Buffer;
1228       Ptr     : Positive renames File.Current_Line;
1229       File_Nr : Natural;
1230
1231    begin
1232       while Ali (Ptr) = 'X' loop
1233
1234          --  The current line is the start of a new Xref file section,
1235          --  whose format looks like:
1236
1237          --     " X 1 debug.ads"
1238
1239          --  Skip the X and read the file number for the new X_File
1240
1241          Ptr := Ptr + 1;
1242          Parse_Number (Ali, Ptr, File_Nr);
1243
1244          if File_Nr > 0 then
1245             File.X_File := File.Dep.Table (File_Nr);
1246          end if;
1247
1248          Parse_EOL (Ali, Ptr);
1249       end loop;
1250    end Parse_X_Filename;
1251
1252    --------------------
1253    -- Print_Gnatfind --
1254    --------------------
1255
1256    procedure Print_Gnatfind
1257      (References     : Boolean;
1258       Full_Path_Name : Boolean)
1259    is
1260       Decls : constant Declaration_Array_Access := Get_Declarations;
1261       Decl  : Declaration_Reference;
1262       Arr   : Reference_Array_Access;
1263
1264       procedure Print_Ref
1265         (Ref : Reference;
1266          Msg : String := "      ");
1267       --  Print a reference, according to the extended tag of the output
1268
1269       ---------------
1270       -- Print_Ref --
1271       ---------------
1272
1273       procedure Print_Ref
1274         (Ref : Reference;
1275          Msg : String := "      ")
1276       is
1277          F : String_Access :=
1278                Osint.To_Host_File_Spec
1279                 (Get_Gnatchop_File (Ref, Full_Path_Name));
1280
1281          Buffer : constant String :=
1282                     F.all &
1283                     ":" & Get_Line (Ref)   &
1284                     ":" & Get_Column (Ref) &
1285                     ": ";
1286
1287          Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1288
1289       begin
1290          Free (F);
1291          Num_Blanks := Integer'Max (0, Num_Blanks);
1292          Write_Line
1293            (Buffer
1294             & String'(1 .. Num_Blanks => ' ')
1295             & Msg & " " & Get_Symbol (Decl));
1296
1297          if Get_Source_Line (Ref)'Length /= 0 then
1298             Write_Line ("   " & Get_Source_Line (Ref));
1299          end if;
1300       end Print_Ref;
1301
1302    --  Start of processing for Print_Gnatfind
1303
1304    begin
1305       for D in Decls'Range loop
1306          Decl := Decls (D);
1307
1308          if Match (Decl) then
1309
1310             --  Output the declaration
1311
1312             declare
1313                Parent : constant Declaration_Reference := Get_Parent (Decl);
1314
1315                F : String_Access :=
1316                      Osint.To_Host_File_Spec
1317                       (Get_Gnatchop_File (Decl, Full_Path_Name));
1318
1319                Buffer : constant String :=
1320                           F.all &
1321                           ":" & Get_Line (Decl)   &
1322                           ":" & Get_Column (Decl) &
1323                           ": ";
1324
1325                Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1326
1327             begin
1328                Free (F);
1329                Num_Blanks := Integer'Max (0, Num_Blanks);
1330                Write_Line
1331                  (Buffer & String'(1 .. Num_Blanks => ' ')
1332                   & "(spec) " & Get_Symbol (Decl));
1333
1334                if Parent /= Empty_Declaration then
1335                   F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1336                   Write_Line
1337                     (Buffer & String'(1 .. Num_Blanks => ' ')
1338                      & "   derived from " & Get_Symbol (Parent)
1339                      & " ("
1340                      & F.all
1341                      & ':' & Get_Line (Parent)
1342                      & ':' & Get_Column (Parent) & ')');
1343                   Free (F);
1344                end if;
1345             end;
1346
1347             if Get_Source_Line (Decl)'Length /= 0 then
1348                Write_Line ("   " & Get_Source_Line (Decl));
1349             end if;
1350
1351             --  Output the body (sorted)
1352
1353             Arr := Get_References (Decl, Get_Bodies => True);
1354
1355             for R in Arr'Range loop
1356                Print_Ref (Arr (R), "(body)");
1357             end loop;
1358
1359             Free (Arr);
1360
1361             if References then
1362                Arr := Get_References
1363                  (Decl, Get_Writes => True, Get_Reads => True);
1364
1365                for R in Arr'Range loop
1366                   Print_Ref (Arr (R));
1367                end loop;
1368
1369                Free (Arr);
1370             end if;
1371          end if;
1372       end loop;
1373    end Print_Gnatfind;
1374
1375    ------------------
1376    -- Print_Unused --
1377    ------------------
1378
1379    procedure Print_Unused (Full_Path_Name : Boolean) is
1380       Decls : constant Declaration_Array_Access := Get_Declarations;
1381       Decl  : Declaration_Reference;
1382       Arr   : Reference_Array_Access;
1383       F     : String_Access;
1384
1385    begin
1386       for D in Decls'Range loop
1387          Decl := Decls (D);
1388
1389          if References_Count
1390              (Decl, Get_Reads => True, Get_Writes => True) = 0
1391          then
1392             F := Osint.To_Host_File_Spec
1393               (Get_Gnatchop_File (Decl, Full_Path_Name));
1394             Write_Str (Get_Symbol (Decl)
1395                         & " ("
1396                         & Get_Full_Type (Decl)
1397                         & ") "
1398                         & F.all
1399                         & ':'
1400                         & Get_Line (Decl)
1401                         & ':'
1402                         & Get_Column (Decl));
1403             Free (F);
1404
1405             --  Print the body if any
1406
1407             Arr := Get_References (Decl, Get_Bodies => True);
1408
1409             for R in Arr'Range loop
1410                F := Osint.To_Host_File_Spec
1411                       (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1412                Write_Str (' '
1413                            & F.all
1414                            & ':' & Get_Line (Arr (R))
1415                            & ':' & Get_Column (Arr (R)));
1416                Free (F);
1417             end loop;
1418
1419             Write_Eol;
1420             Free (Arr);
1421          end if;
1422       end loop;
1423    end Print_Unused;
1424
1425    --------------
1426    -- Print_Vi --
1427    --------------
1428
1429    procedure Print_Vi (Full_Path_Name : Boolean) is
1430       Tab   : constant Character := ASCII.HT;
1431       Decls : constant Declaration_Array_Access :=
1432                 Get_Declarations (Sorted => False);
1433       Decl  : Declaration_Reference;
1434       Arr   : Reference_Array_Access;
1435       F     : String_Access;
1436
1437    begin
1438       for D in Decls'Range loop
1439          Decl := Decls (D);
1440
1441          F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1442          Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1443          Free (F);
1444
1445          --  Print the body if any
1446
1447          Arr := Get_References (Decl, Get_Bodies => True);
1448
1449          for R in Arr'Range loop
1450             F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1451             Write_Line
1452               (Get_Symbol (Decl) & Tab & F.all & Tab  & Get_Line (Arr (R)));
1453             Free (F);
1454          end loop;
1455
1456          Free (Arr);
1457
1458          --  Print the modifications
1459
1460          Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1461
1462          for R in Arr'Range loop
1463             F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1464             Write_Line
1465               (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1466             Free (F);
1467          end loop;
1468
1469          Free (Arr);
1470       end loop;
1471    end Print_Vi;
1472
1473    ----------------
1474    -- Print_Xref --
1475    ----------------
1476
1477    procedure Print_Xref (Full_Path_Name : Boolean) is
1478       Decls : constant Declaration_Array_Access := Get_Declarations;
1479       Decl : Declaration_Reference;
1480
1481       Margin : constant := 10;
1482       --  Column where file names start
1483
1484       procedure New_Line80;
1485       --  Go to start of new line
1486
1487       procedure Print80 (S : String);
1488       --  Print the text, respecting the 80 columns rule
1489
1490       procedure Print_Ref (Line, Column : String);
1491       --  The beginning of the output is aligned on a column multiple of 9
1492
1493       procedure Print_List
1494         (Decl       : Declaration_Reference;
1495          Msg        : String;
1496          Get_Reads  : Boolean := False;
1497          Get_Writes : Boolean := False;
1498          Get_Bodies : Boolean := False);
1499       --  Print a list of references. If the list is not empty, Msg will
1500       --  be printed prior to the list.
1501
1502       ----------------
1503       -- New_Line80 --
1504       ----------------
1505
1506       procedure New_Line80 is
1507       begin
1508          Write_Eol;
1509          Write_Str (String'(1 .. Margin - 1 => ' '));
1510       end New_Line80;
1511
1512       -------------
1513       -- Print80 --
1514       -------------
1515
1516       procedure Print80 (S : String) is
1517          Align : Natural := Margin - (Integer (Column) mod Margin);
1518
1519       begin
1520          if Align = Margin then
1521             Align := 0;
1522          end if;
1523
1524          Write_Str (String'(1 .. Align => ' ') & S);
1525       end Print80;
1526
1527       ---------------
1528       -- Print_Ref --
1529       ---------------
1530
1531       procedure Print_Ref (Line, Column : String) is
1532          Line_Align : constant Integer := 4 - Line'Length;
1533
1534          S : constant String := String'(1 .. Line_Align => ' ')
1535                                   & Line & ':' & Column;
1536
1537          Align : Natural := Margin - (Integer (Output.Column) mod Margin);
1538
1539       begin
1540          if Align = Margin then
1541             Align := 0;
1542          end if;
1543
1544          if Integer (Output.Column) + Align + S'Length > 79 then
1545             New_Line80;
1546             Align := 0;
1547          end if;
1548
1549          Write_Str (String'(1 .. Align => ' ') & S);
1550       end Print_Ref;
1551
1552       ----------------
1553       -- Print_List --
1554       ----------------
1555
1556       procedure Print_List
1557         (Decl       : Declaration_Reference;
1558          Msg        : String;
1559          Get_Reads  : Boolean := False;
1560          Get_Writes : Boolean := False;
1561          Get_Bodies : Boolean := False)
1562       is
1563          Arr : Reference_Array_Access :=
1564                  Get_References
1565                    (Decl,
1566                     Get_Writes => Get_Writes,
1567                     Get_Reads  => Get_Reads,
1568                     Get_Bodies => Get_Bodies);
1569          File : File_Reference := Empty_File;
1570          F    : String_Access;
1571
1572       begin
1573          if Arr'Length /= 0 then
1574             Write_Eol;
1575             Write_Str (Msg);
1576          end if;
1577
1578          for R in Arr'Range loop
1579             if Get_File_Ref (Arr (R)) /= File then
1580                if File /= Empty_File then
1581                   New_Line80;
1582                end if;
1583
1584                File := Get_File_Ref (Arr (R));
1585                F := Osint.To_Host_File_Spec
1586                  (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1587                Write_Str (F.all & ' ');
1588                Free (F);
1589             end if;
1590
1591             Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1592          end loop;
1593
1594          Free (Arr);
1595       end Print_List;
1596
1597       F : String_Access;
1598
1599    --  Start of processing for Print_Xref
1600
1601    begin
1602       for D in Decls'Range loop
1603          Decl := Decls (D);
1604
1605          Write_Str (Get_Symbol (Decl));
1606
1607          while Column < Type_Position loop
1608             Write_Char (' ');
1609          end loop;
1610
1611          Write_Line (Get_Full_Type (Decl));
1612
1613          Write_Parent_Info : declare
1614             Parent : constant Declaration_Reference := Get_Parent (Decl);
1615
1616          begin
1617             if Parent /= Empty_Declaration then
1618                Write_Str ("  Ptype: ");
1619                F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1620                Print80 (F.all);
1621                Free (F);
1622                Print_Ref (Get_Line (Parent), Get_Column (Parent));
1623                Print80 ("  " & Get_Symbol (Parent));
1624                Write_Eol;
1625             end if;
1626          end Write_Parent_Info;
1627
1628          Write_Str ("  Decl:  ");
1629          F := Osint.To_Host_File_Spec
1630                (Get_Gnatchop_File (Decl, Full_Path_Name));
1631          Print80 (F.all & ' ');
1632          Free (F);
1633          Print_Ref (Get_Line (Decl), Get_Column (Decl));
1634
1635          Print_List
1636            (Decl, "  Body:  ", Get_Bodies => True);
1637          Print_List
1638            (Decl, "  Modi:  ", Get_Writes => True);
1639          Print_List
1640            (Decl, "  Ref:   ", Get_Reads => True);
1641          Write_Eol;
1642       end loop;
1643    end Print_Xref;
1644
1645    ------------
1646    -- Search --
1647    ------------
1648
1649    procedure Search
1650      (Pattern       : Search_Pattern;
1651       Local_Symbols : Boolean;
1652       Wide_Search   : Boolean;
1653       Read_Only     : Boolean;
1654       Der_Info      : Boolean;
1655       Type_Tree     : Boolean)
1656    is
1657       type String_Access is access String;
1658       procedure Free is new Unchecked_Deallocation (String, String_Access);
1659
1660       ALIfile   : ALI_File;
1661       File_Ref  : File_Reference;
1662       Strip_Num : Natural := 0;
1663       Ali_Name  : String_Access;
1664
1665    begin
1666       --  If we want all the .ali files, then find them
1667
1668       if Wide_Search then
1669          Find_ALI_Files;
1670       end if;
1671
1672       loop
1673          --  Get the next unread ali file
1674
1675          File_Ref := Next_Unvisited_File;
1676
1677          exit when File_Ref = Empty_File;
1678
1679          --  Find the ALI file to use. Most of the time, it will be the unit
1680          --  name, with a different extension. However, when dealing with
1681          --  separates the ALI file is in fact the parent's ALI file (and this
1682          --  is recursive, in case the parent itself is a separate).
1683
1684          Strip_Num := 0;
1685          loop
1686             Free (Ali_Name);
1687             Ali_Name := new String'
1688               (Get_File (File_Ref, With_Dir => True, Strip => Strip_Num));
1689
1690             --  Stripped too many things...
1691
1692             if Ali_Name.all = "" then
1693                if Get_Emit_Warning (File_Ref) then
1694                   Set_Standard_Error;
1695                   Write_Line
1696                     ("warning : file " & Get_File (File_Ref, With_Dir => True)
1697                      & " not found");
1698                   Set_Standard_Output;
1699                end if;
1700                Free (Ali_Name);
1701                exit;
1702
1703             --  If not found, try the parent's ALI file (this is needed for
1704             --  separate units and subprograms).
1705
1706             --  Reset the cached directory first, in case the separate's
1707             --  ALI file is not in the same directory.
1708
1709             elsif not File_Exists (Ali_Name.all) then
1710                Strip_Num := Strip_Num + 1;
1711                Reset_Directory (File_Ref);
1712
1713             --  Else we finally found it
1714
1715             else
1716                exit;
1717             end if;
1718          end loop;
1719
1720          --  If we had to get the parent's ALI, insert it in the list as usual.
1721          --  This is to avoid parsing it twice in case it has already been
1722          --  parsed.
1723
1724          if Ali_Name /= null and then Strip_Num /= 0 then
1725             File_Ref := Add_To_Xref_File
1726               (File_Name => Ali_Name.all,
1727                Visited   => False);
1728
1729          --  Now that we have a file name, parse it to find any reference to
1730          --  the entity.
1731
1732          elsif Ali_Name /= null
1733            and then (Read_Only or else Is_Writable_File (Ali_Name.all))
1734          then
1735             begin
1736                Open (Ali_Name.all, ALIfile);
1737                while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1738                   Parse_X_Filename (ALIfile);
1739                   Parse_Identifier_Info
1740                     (Pattern, ALIfile, Local_Symbols,
1741                      Der_Info, Type_Tree, Wide_Search, Labels_As_Ref => True);
1742                end loop;
1743
1744             exception
1745                when No_Xref_Information   =>
1746                   if Get_Emit_Warning (File_Ref) then
1747                      Set_Standard_Error;
1748                      Write_Line
1749                        ("warning : No cross-referencing information in  "
1750                         & Ali_Name.all);
1751                      Set_Standard_Output;
1752                   end if;
1753             end;
1754          end if;
1755       end loop;
1756
1757       Free (Ali_Name);
1758    end Search;
1759
1760    -----------------
1761    -- Search_Xref --
1762    -----------------
1763
1764    procedure Search_Xref
1765      (Local_Symbols : Boolean;
1766       Read_Only     : Boolean;
1767       Der_Info      : Boolean)
1768    is
1769       ALIfile      : ALI_File;
1770       File_Ref     : File_Reference;
1771       Null_Pattern : Search_Pattern;
1772
1773    begin
1774       Null_Pattern.Initialized := False;
1775
1776       loop
1777          --  Find the next unvisited file
1778
1779          File_Ref := Next_Unvisited_File;
1780          exit when File_Ref = Empty_File;
1781
1782          --  Search the object directories for the .ali file
1783
1784          declare
1785             F : constant String := Get_File (File_Ref, With_Dir => True);
1786
1787          begin
1788             if Read_Only or else Is_Writable_File (F) then
1789                Open (F, ALIfile, True);
1790
1791                while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1792                   Parse_X_Filename (ALIfile);
1793                   Parse_Identifier_Info
1794                     (Null_Pattern, ALIfile, Local_Symbols, Der_Info,
1795                      Labels_As_Ref => False);
1796                end loop;
1797             end if;
1798
1799          exception
1800             when No_Xref_Information =>  null;
1801          end;
1802       end loop;
1803    end Search_Xref;
1804
1805 end Xref_Lib;