OSDN Git Service

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