1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2002 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with Output; use Output;
29 with Types; use Types;
31 with Unchecked_Deallocation;
33 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
34 with Ada.Text_IO; use Ada.Text_IO;
36 with GNAT.Command_Line; use GNAT.Command_Line;
37 with GNAT.IO_Aux; use GNAT.IO_Aux;
39 package body Xref_Lib is
41 Type_Position : constant := 50;
42 -- Column for label identifying type of entity
48 Pipe : constant Character := '|';
49 -- First character on xref lines in the .ali file
51 No_Xref_Information : exception;
52 -- Exception raised when there is no cross-referencing information in
56 (Source : access String;
57 Ptr : in out Positive;
58 Skip_Continuation_Line : Boolean := False);
59 -- On return Source (Ptr) is the first character of the next line
60 -- or EOF. Source.all must be terminated by EOF.
62 -- If Skip_Continuation_Line is True, this subprogram skips as many
63 -- lines as required when the second or more lines starts with '.'
64 -- (continuation lines in ALI files).
66 function Current_Xref_File (File : ALI_File) return File_Reference;
67 -- Return the file matching the last 'X' line we found while parsing
70 function File_Name (File : ALI_File; Num : Positive) return File_Reference;
71 -- Returns the dependency file name number Num
73 function Get_Full_Type (Decl : Declaration_Reference) return String;
74 -- Returns the full type corresponding to a type letter as found in
80 Dependencies : in Boolean := False);
81 -- Open a new ALI file. If Dependencies is True, the insert every library
82 -- file 'with'ed in the files database (used for gnatxref)
84 procedure Parse_Identifier_Info
85 (Pattern : Search_Pattern;
86 File : in out ALI_File;
87 Local_Symbols : Boolean;
88 Der_Info : Boolean := False;
89 Type_Tree : Boolean := False;
90 Wide_Search : Boolean := True;
91 Labels_As_Ref : Boolean := True);
92 -- Output the file and the line where the identifier was referenced,
93 -- If Local_Symbols is False then only the publicly visible symbols
96 -- If Labels_As_Ref is true, then the references to the entities after
97 -- the end statements ("end Foo") will be counted as actual references.
98 -- The entity will never be reported as unreferenced by gnatxref -u
100 procedure Parse_Token
101 (Source : access String;
102 Ptr : in out Positive;
103 Token_Ptr : out Positive);
104 -- Skips any separators and stores the start of the token in Token_Ptr.
105 -- Then stores the position of the next separator in Ptr. On return
106 -- Source (Token_Ptr .. Ptr - 1) is the token. Separators are space
107 -- and ASCII.HT. Parse_Token will never skip to the next line.
109 procedure Parse_Number
110 (Source : access String;
111 Ptr : in out Positive;
112 Number : out Natural);
113 -- Skips any separators and parses Source upto the first character that
114 -- is not a decimal digit. Returns value of parsed digits or 0 if none.
116 procedure Parse_X_Filename (File : in out ALI_File);
117 -- Reads and processes "X..." lines in the ALI file
118 -- and updates the File.X_File information.
120 procedure Skip_To_First_X_Line
121 (File : in out ALI_File;
124 -- Skip the lines in the ALI file until the first cross-reference line
125 -- (^X...) is found. Search is started from the beginning of the file.
126 -- If not such line is found, No_Xref_Information is raised.
127 -- If W_Lines is false, then the lines "^W" are not parsed.
128 -- If D_Lines is false, then the lines "^D" are not parsed.
135 (Pattern : in out Search_Pattern;
137 Glob : Boolean := False)
139 File_Start : Natural;
140 Line_Start : Natural;
142 Line_Num : Natural := 0;
143 Col_Num : Natural := 0;
144 File_Ref : File_Reference := Empty_File;
145 Has_Pattern : Boolean := False;
148 -- Find the end of the first item in Entity (pattern or file?)
149 -- If there is no ':', we only have a pattern
151 File_Start := Index (Entity, ":");
153 -- If the regular expression is invalid, just consider it as a string
155 if File_Start = 0 then
157 Pattern.Entity := Compile (Entity, Glob, False);
158 Pattern.Initialized := True;
161 when Error_In_Regexp =>
163 -- The basic idea is to insert a \ before every character
166 Tmp_Regexp : String (1 .. 2 * Entity'Length);
167 Index : Positive := 1;
170 for J in Entity'Range loop
171 Tmp_Regexp (Index) := '\';
172 Tmp_Regexp (Index + 1) := Entity (J);
176 Pattern.Entity := Compile (Tmp_Regexp, True, False);
177 Pattern.Initialized := True;
181 Set_Default_Match (True);
185 -- If there is a dot in the pattern, then it is a file name
188 Index (Entity (Entity'First .. File_Start - 1), ".") /= 0)
191 and then Index (Entity (Entity'First .. File_Start - 1),
194 Pattern.Entity := Compile (".*", False);
195 Pattern.Initialized := True;
196 File_Start := Entity'First;
199 -- If the regular expression is invalid, just consider it as a string
203 Compile (Entity (Entity'First .. File_Start - 1), Glob, False);
204 Pattern.Initialized := True;
207 when Error_In_Regexp =>
209 -- The basic idea is to insert a \ before every character
212 Tmp_Regexp : String (1 .. 2 * (File_Start - Entity'First));
213 Index : Positive := 1;
216 for J in Entity'First .. File_Start - 1 loop
217 Tmp_Regexp (Index) := '\';
218 Tmp_Regexp (Index + 1) := Entity (J);
222 Pattern.Entity := Compile (Tmp_Regexp, True, False);
223 Pattern.Initialized := True;
227 File_Start := File_Start + 1;
231 -- Parse the file name
233 Line_Start := Index (Entity (File_Start .. Entity'Last), ":");
235 -- Check if it was a disk:\directory item (for NT and OS/2)
237 if File_Start = Line_Start - 1
238 and then Line_Start < Entity'Last
239 and then Entity (Line_Start + 1) = '\'
241 Line_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
244 if Line_Start = 0 then
245 Line_Start := Entity'Length + 1;
247 elsif Line_Start /= Entity'Last then
248 Col_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
250 if Col_Start = 0 then
251 Col_Start := Entity'Last + 1;
254 if Col_Start > Line_Start + 1 then
256 Line_Num := Natural'Value
257 (Entity (Line_Start + 1 .. Col_Start - 1));
260 when Constraint_Error =>
261 raise Invalid_Argument;
265 if Col_Start < Entity'Last then
267 Col_Num := Natural'Value (Entity
268 (Col_Start + 1 .. Entity'Last));
271 when Constraint_Error => raise Invalid_Argument;
278 (Entity (File_Start .. Line_Start - 1), Visited => True);
279 Pattern.File_Ref := File_Ref;
280 Add_Line (Pattern.File_Ref, Line_Num, Col_Num);
283 (ALI_File_Name (Entity (File_Start .. Line_Start - 1)),
285 Emit_Warning => True);
292 procedure Add_Xref_File (File : String) is
293 File_Ref : File_Reference := Empty_File;
294 Iterator : Expansion_Iterator;
296 procedure Add_Xref_File_Internal (File : String);
297 -- Do the actual addition of the file
299 ----------------------------
300 -- Add_Xref_File_Internal --
301 ----------------------------
303 procedure Add_Xref_File_Internal (File : String) is
305 -- Case where we have an ALI file, accept it even though this is
306 -- not official usage, since the intention is obvious
308 if Tail (File, 4) = ".ali" then
309 File_Ref := Add_To_Xref_File
310 (File, Visited => False, Emit_Warning => True);
312 -- Normal non-ali file case
315 File_Ref := Add_To_Xref_File (File, Visited => True);
317 File_Ref := Add_To_Xref_File
318 (ALI_File_Name (File),
320 Emit_Warning => True);
322 end Add_Xref_File_Internal;
324 -- Start of processing for Add_Xref_File
327 -- Check if we need to do the expansion
329 if Ada.Strings.Fixed.Index (File, "*") /= 0
330 or else Ada.Strings.Fixed.Index (File, "?") /= 0
332 Start_Expansion (Iterator, File);
336 S : constant String := Expansion (Iterator);
339 exit when S'Length = 0;
340 Add_Xref_File_Internal (S);
345 Add_Xref_File_Internal (File);
349 -----------------------
350 -- Current_Xref_File --
351 -----------------------
353 function Current_Xref_File (File : ALI_File) return File_Reference is
356 end Current_Xref_File;
358 --------------------------
359 -- Default_Project_File --
360 --------------------------
362 function Default_Project_File
367 Dir_Ent : File_Name_String;
371 Open (My_Dir, Dir_Name);
374 Read (My_Dir, Dir_Ent, Last);
377 if Tail (Dir_Ent (1 .. Last), 4) = ".adp" then
379 -- The first project file found is the good one.
382 return Dir_Ent (1 .. Last);
387 return String'(1 .. 0 => ' ');
390 when Directory_Error => return String'(1 .. 0 => ' ');
391 end Default_Project_File;
400 return File_Reference
403 return File.Dep.Table (Num);
410 procedure Find_ALI_Files is
412 Dir_Ent : File_Name_String;
414 File_Ref : File_Reference;
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.
424 function Open_Next_Dir return Boolean is
426 -- Until we are able to open a new directory
430 Obj_Dir : constant String := Next_Obj_Dir;
433 -- Case of no more Obj_Dir lines
435 if Obj_Dir'Length = 0 then
439 Open (My_Dir.Dir, Obj_Dir);
444 -- Could not open the directory
446 when Directory_Error => null;
453 -- Start of processing for Find_ALI_Files
458 if Open_Next_Dir then
460 Read (My_Dir.Dir, Dir_Ent, Last);
465 if not Open_Next_Dir then
469 elsif Last > 4 and then Dir_Ent (Last - 3 .. Last) = ".ali" then
471 Add_To_Xref_File (Dir_Ent (1 .. Last), Visited => False);
481 function Get_Full_Type (Decl : Declaration_Reference) return String is
483 function Param_String return String;
484 -- Return the string to display depending on whether Decl is a
491 function Param_String return String is
493 if Is_Parameter (Decl) then
500 -- Start of processing for Get_Full_Type
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";
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";
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";
550 when '+' => return "private type";
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
558 return "??? (" & Get_Type (Decl) & ")";
562 --------------------------
563 -- Skip_To_First_X_Line --
564 --------------------------
566 procedure Skip_To_First_X_Line
567 (File : in out ALI_File;
571 Ali : String_Access renames File.Buffer;
573 Ptr : Positive := Ali'First;
574 Num_Dependencies : Natural := 0;
575 File_Ref : File_Reference;
576 File_Start : Positive;
578 Gnatchop_Offset : Integer;
579 Gnatchop_Name : Positive;
582 -- Read all the lines possibly processing with-clauses and dependency
583 -- information and exit on finding the first Xref line.
584 -- A fall-through of the loop means that there is no xref information
585 -- which is an error condition.
587 while Ali (Ptr) /= EOF loop
589 if D_Lines and then Ali (Ptr) = 'D' then
591 -- Found dependency information. Format looks like:
592 -- D src-nam time-stmp checksum [subunit-name] [line:file-name]
594 -- Skip the D and parse the filenam
597 Parse_Token (Ali, Ptr, Token);
601 Num_Dependencies := Num_Dependencies + 1;
602 Set_Last (File.Dep, Num_Dependencies);
604 Parse_Token (Ali, Ptr, Token); -- Skip time-stamp
605 Parse_Token (Ali, Ptr, Token); -- Skip checksum
606 Parse_Token (Ali, Ptr, Token); -- Read next entity on the line
608 if not (Ali (Token) in '0' .. '9') then
609 Parse_Token (Ali, Ptr, Token); -- Was a subunit name
612 -- Did we have a gnatchop-ed file with a pragma Source_Reference ?
614 Gnatchop_Offset := 0;
616 if Ali (Token) in '0' .. '9' then
617 Gnatchop_Name := Token;
618 while Ali (Gnatchop_Name) /= ':' loop
619 Gnatchop_Name := Gnatchop_Name + 1;
623 2 - Natural'Value (Ali (Token .. Gnatchop_Name - 1));
624 Token := Gnatchop_Name + 1;
627 File.Dep.Table (Num_Dependencies) := Add_To_Xref_File
628 (Ali (File_Start .. File_End),
629 Gnatchop_File => Ali (Token .. Ptr - 1),
630 Gnatchop_Offset => Gnatchop_Offset);
632 elsif W_Lines and then Ali (Ptr) = 'W' then
634 -- Found with-clause information. Format looks like:
635 -- "W debug%s debug.adb debug.ali"
637 -- Skip the W and parse the .ali filename (3rd token)
639 Parse_Token (Ali, Ptr, Token);
640 Parse_Token (Ali, Ptr, Token);
641 Parse_Token (Ali, Ptr, Token);
643 File_Ref := Add_To_Xref_File
644 (Ali (Token .. Ptr - 1), Visited => False);
646 elsif Ali (Ptr) = 'X' then
648 -- Found a cross-referencing line - stop processing
650 File.Current_Line := Ptr;
651 File.Xref_Line := Ptr;
655 Parse_EOL (Ali, Ptr);
658 raise No_Xref_Information;
659 end Skip_To_First_X_Line;
668 Dependencies : Boolean := False)
670 Ali : String_Access renames File.Buffer;
673 if File.Buffer /= null then
680 Read_File (Name, Ali);
683 when Ada.Text_IO.Name_Error | Ada.Text_IO.End_Error =>
684 raise No_Xref_Information;
687 Skip_To_First_X_Line (File, D_Lines => True, W_Lines => Dependencies);
695 (Source : access String;
696 Ptr : in out Positive;
697 Skip_Continuation_Line : Boolean := False)
701 -- Skip to end of line
703 while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
704 and then Source (Ptr) /= EOF
709 if Source (Ptr) /= EOF then
710 Ptr := Ptr + 1; -- skip CR or LF
713 -- Skip past CR/LF or LF/CR combination
715 if (Source (Ptr) = ASCII.CR or else Source (Ptr) = ASCII.LF)
716 and then Source (Ptr) /= Source (Ptr - 1)
721 exit when not Skip_Continuation_Line or else Source (Ptr) /= '.';
725 ---------------------------
726 -- Parse_Identifier_Info --
727 ---------------------------
729 procedure Parse_Identifier_Info
730 (Pattern : Search_Pattern;
731 File : in out ALI_File;
732 Local_Symbols : Boolean;
733 Der_Info : Boolean := False;
734 Type_Tree : Boolean := False;
735 Wide_Search : Boolean := True;
736 Labels_As_Ref : Boolean := True)
738 Ptr : Positive renames File.Current_Line;
739 Ali : String_Access renames File.Buffer;
741 E_Line : Natural; -- Line number of current entity
742 E_Col : Natural; -- Column number of current entity
743 E_Type : Character; -- Type of current entity
744 E_Name : Positive; -- Pointer to begin of entity name
745 E_Global : Boolean; -- True iff entity is global
747 R_Line : Natural; -- Line number of current reference
748 R_Col : Natural; -- Column number of current reference
749 R_Type : Character; -- Type of current reference
751 Decl_Ref : Declaration_Reference;
752 File_Ref : File_Reference := Current_Xref_File (File);
754 function Get_Symbol_Name (Eun, Line, Col : Natural) return String;
755 -- Returns the symbol name for the entity defined at the specified
756 -- line and column in the dependent unit number Eun. For this we need
757 -- to parse the ali file again because the parent entity is not in
758 -- the declaration table if it did not match the search pattern.
760 ---------------------
761 -- Get_Symbol_Name --
762 ---------------------
764 function Get_Symbol_Name (Eun, Line, Col : Natural) return String is
766 E_Eun : Positive; -- Unit number of current entity
767 E_Line : Natural; -- Line number of current entity
768 E_Col : Natural; -- Column number of current entity
769 E_Name : Positive; -- Pointer to begin of entity name
770 E_Type : Character; -- Type of current entity
773 -- Look for the X lines corresponding to unit Eun
776 if Ali (Ptr) = 'X' then
778 Parse_Number (Ali, Ptr, E_Eun);
779 exit when E_Eun = Eun;
782 Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
785 -- Here we are in the right Ali section, we now look for the entity
786 -- declared at position (Line, Col).
789 Parse_Number (Ali, Ptr, E_Line);
791 exit when Ali (Ptr) = EOF;
793 Parse_Number (Ali, Ptr, E_Col);
794 exit when Ali (Ptr) = EOF;
797 if Line = E_Line and then Col = E_Col then
798 Parse_Token (Ali, Ptr, E_Name);
799 return Ali (E_Name .. Ptr - 1);
802 Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
803 exit when Ali (Ptr) = EOF;
806 -- We were not able to find the symbol, this should not happend but
807 -- since we don't want to stop here we return a string of three
808 -- question marks as the symbol name.
813 -- Start of processing for Parse_Identifier_Info
816 -- The identifier info looks like:
817 -- "38U9*Debug 12|36r6 36r19"
819 -- Extract the line, column and entity name information
821 Parse_Number (Ali, Ptr, E_Line);
823 if Ali (Ptr) > ' ' then
828 -- Ignore some of the entities (labels,...)
831 when 'l' | 'L' | 'q' =>
832 Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
839 Parse_Number (Ali, Ptr, E_Col);
842 if Ali (Ptr) >= ' ' then
843 E_Global := (Ali (Ptr) = '*');
847 Parse_Token (Ali, Ptr, E_Name);
849 -- Exit if the symbol does not match
850 -- or if we have a local symbol and we do not want it
852 if (not Local_Symbols and not E_Global)
853 or else (Pattern.Initialized
854 and then not Match (Ali (E_Name .. Ptr - 1), Pattern.Entity))
855 or else (E_Name >= Ptr)
857 Decl_Ref := Add_Declaration
858 (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type,
859 Remove_Only => True);
860 Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
864 -- Insert the declaration in the table
866 Decl_Ref := Add_Declaration
867 (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type);
870 or else Ali (Ptr) = '('
871 or else Ali (Ptr) = '{'
873 -- Here we have a type derivation information. The format is
874 -- <3|12I45> which means that the current entity is derived from the
875 -- type defined in unit number 3, line 12 column 45. The pipe and
876 -- unit number is optional. It is specified only if the parent type
877 -- is not defined in the current unit.
879 -- We could also have something like
881 -- that indicates that I derives from the predefined type integer.
885 if Ali (Ptr) in '0' .. '9' then
886 Parse_Derived_Info : declare
887 P_Line : Natural; -- parent entity line
888 P_Column : Natural; -- parent entity column
889 P_Type : Character; -- parent entity type
890 P_Eun : Positive; -- parent entity file number
893 Parse_Number (Ali, Ptr, P_Line);
895 -- If we have a pipe then the first number was the unit number
897 if Ali (Ptr) = '|' then
901 -- Now we have the line number
903 Parse_Number (Ali, Ptr, P_Line);
906 -- We don't have a unit number specified, so we set P_Eun to
909 for K in Dependencies_Tables.First .. Last (File.Dep) loop
911 exit when File.Dep.Table (K) = File_Ref;
915 -- Then parse the type and column number
919 Parse_Number (Ali, Ptr, P_Column);
921 -- Skip '>', or ')' or '>'
925 -- The derived info is needed only is the derived info mode is
926 -- on or if we want to output the type hierarchy
928 if Der_Info or else Type_Tree then
930 Symbol : constant String :=
931 Get_Symbol_Name (P_Eun, P_Line, P_Column);
934 if Symbol /= "???" then
940 File.Dep.Table (P_Eun));
946 and then (Pattern.File_Ref = Empty_File
948 Pattern.File_Ref = Current_Xref_File (File))
950 Search_Parent_Tree : declare
951 Pattern : Search_Pattern; -- Parent type pattern
952 File_Pos_Backup : Positive;
957 Get_Symbol_Name (P_Eun, P_Line, P_Column)
958 & ':' & Get_Gnatchop_File (File.Dep.Table (P_Eun))
959 & ':' & Get_Line (Get_Parent (Decl_Ref))
960 & ':' & Get_Column (Get_Parent (Decl_Ref)),
963 -- No default match is needed to look for the parent type
964 -- since we are using the fully qualified symbol name:
965 -- symbol:file:line:column
967 Set_Default_Match (False);
969 -- The parent hierarchy is defined in the same unit as
970 -- the derived type. So we want to revisit the unit.
972 File_Pos_Backup := File.Current_Line;
975 (File, D_Lines => False, W_Lines => False);
977 while File.Buffer (File.Current_Line) /= EOF loop
978 Parse_X_Filename (File);
979 Parse_Identifier_Info
982 Local_Symbols => False,
983 Der_Info => Der_Info,
985 Wide_Search => False,
986 Labels_As_Ref => Labels_As_Ref);
989 File.Current_Line := File_Pos_Backup;
990 end Search_Parent_Tree;
992 end Parse_Derived_Info;
995 while Ali (Ptr) /= '>'
996 and then Ali (Ptr) /= ')'
997 and then Ali (Ptr) /= '}'
1004 elsif Ali (Ptr) = '=' then
1006 P_Line, P_Column : Natural;
1010 Parse_Number (Ali, Ptr, P_Line);
1012 Parse_Number (Ali, Ptr, P_Column);
1016 -- To find the body, we will have to parse the file too
1020 File_Ref : File_Reference;
1021 File_Name : constant String :=
1022 Get_Gnatchop_File (File.X_File);
1024 File_Ref := Add_To_Xref_File (ALI_File_Name (File_Name), False);
1028 -- Parse references to this entity.
1029 -- Ptr points to next reference with leading blanks
1032 -- Process references on current line
1034 while Ali (Ptr) = ' ' or Ali (Ptr) = ASCII.HT loop
1036 -- For every reference read the line, type and column,
1037 -- optionally preceded by a file number and a pipe symbol.
1039 Parse_Number (Ali, Ptr, R_Line);
1041 if Ali (Ptr) = Pipe then
1043 File_Ref := File_Name (File, R_Line);
1045 Parse_Number (Ali, Ptr, R_Line);
1048 if Ali (Ptr) > ' ' then
1049 R_Type := Ali (Ptr);
1053 -- Imported entities might special indication as to their external
1055 -- 5U14*Foo2 5>20 6b<c,myfoo2>22
1058 and then Ali (Ptr) = '<'
1060 while Ptr <= Ali'Last
1061 and then Ali (Ptr) /= '>'
1068 Parse_Number (Ali, Ptr, R_Col);
1070 -- Insert the reference or body in the table
1073 (Decl_Ref, File_Ref, R_Line, R_Col, R_Type, Labels_As_Ref);
1075 -- Skip generic information, if any
1077 if Ali (Ptr) = '[' then
1079 Num_Nested : Integer := 1;
1083 while Num_Nested /= 0 loop
1084 if Ali (Ptr) = ']' then
1085 Num_Nested := Num_Nested - 1;
1086 elsif Ali (Ptr) = '[' then
1087 Num_Nested := Num_Nested + 1;
1097 Parse_EOL (Ali, Ptr);
1099 -- Loop until new line is no continuation line
1101 exit when Ali (Ptr) /= '.';
1104 end Parse_Identifier_Info;
1110 procedure Parse_Number
1111 (Source : access String;
1112 Ptr : in out Positive;
1113 Number : out Natural)
1118 while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1123 while Source (Ptr) in '0' .. '9' loop
1125 10 * Number + (Character'Pos (Source (Ptr)) - Character'Pos ('0'));
1134 procedure Parse_Token
1135 (Source : access String;
1136 Ptr : in out Positive;
1137 Token_Ptr : out Positive)
1139 In_Quotes : Character := ASCII.NUL;
1144 while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1150 -- Find end-of-token
1152 while (In_Quotes /= ASCII.NUL or else
1153 not (Source (Ptr) = ' '
1154 or else Source (Ptr) = ASCII.HT
1155 or else Source (Ptr) = '<'
1156 or else Source (Ptr) = '{'
1157 or else Source (Ptr) = '='
1158 or else Source (Ptr) = '('))
1159 and then Source (Ptr) >= ' '
1161 -- Double-quotes are used for operators
1162 -- Simple-quotes are used for character constants, for instance when
1163 -- they are found in an enumeration type "type A is ('+', '-');"
1165 case Source (Ptr) is
1167 if In_Quotes = Source (Ptr) then
1168 In_Quotes := ASCII.NUL;
1169 elsif In_Quotes = ASCII.NUL then
1170 In_Quotes := Source (Ptr);
1181 ----------------------
1182 -- Parse_X_Filename --
1183 ----------------------
1185 procedure Parse_X_Filename (File : in out ALI_File) is
1186 Ali : String_Access renames File.Buffer;
1187 Ptr : Positive renames File.Current_Line;
1191 while Ali (Ptr) = 'X' loop
1193 -- The current line is the start of a new Xref file section,
1194 -- whose format looks like:
1198 -- Skip the X and read the file number for the new X_File
1201 Parse_Number (Ali, Ptr, File_Nr);
1204 File.X_File := File.Dep.Table (File_Nr);
1207 Parse_EOL (Ali, Ptr);
1209 end Parse_X_Filename;
1211 --------------------
1212 -- Print_Gnatfind --
1213 --------------------
1215 procedure Print_Gnatfind
1216 (References : Boolean;
1217 Full_Path_Name : Boolean)
1219 Decls : constant Declaration_Array_Access := Get_Declarations;
1220 Decl : Declaration_Reference;
1221 Arr : Reference_Array_Access;
1225 Msg : String := " ");
1226 -- Print a reference, according to the extended tag of the output
1234 Msg : String := " ")
1236 F : String_Access :=
1237 Osint.To_Host_File_Spec
1238 (Get_Gnatchop_File (Ref, Full_Path_Name));
1240 Buffer : constant String :=
1242 ":" & Get_Line (Ref) &
1243 ":" & Get_Column (Ref) &
1246 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1250 Num_Blanks := Integer'Max (0, Num_Blanks);
1253 & String'(1 .. Num_Blanks => ' ')
1254 & Msg & " " & Get_Symbol (Decl));
1256 if Get_Source_Line (Ref)'Length /= 0 then
1257 Write_Line (" " & Get_Source_Line (Ref));
1261 -- Start of processing for Print_Gnatfind
1264 for D in Decls'Range loop
1267 if Match (Decl) then
1269 -- Output the declaration
1272 Parent : constant Declaration_Reference := Get_Parent (Decl);
1274 F : String_Access :=
1275 Osint.To_Host_File_Spec
1276 (Get_Gnatchop_File (Decl, Full_Path_Name));
1278 Buffer : constant String :=
1280 ":" & Get_Line (Decl) &
1281 ":" & Get_Column (Decl) &
1284 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1288 Num_Blanks := Integer'Max (0, Num_Blanks);
1290 (Buffer & String'(1 .. Num_Blanks => ' ')
1291 & "(spec) " & Get_Symbol (Decl));
1293 if Parent /= Empty_Declaration then
1294 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1296 (Buffer & String'(1 .. Num_Blanks => ' ')
1297 & " derived from " & Get_Symbol (Parent)
1300 & ':' & Get_Line (Parent)
1301 & ':' & Get_Column (Parent) & ')');
1306 if Get_Source_Line (Decl)'Length /= 0 then
1307 Write_Line (" " & Get_Source_Line (Decl));
1310 -- Output the body (sorted)
1312 Arr := Get_References (Decl, Get_Bodies => True);
1314 for R in Arr'Range loop
1315 Print_Ref (Arr (R), "(body)");
1321 Arr := Get_References
1322 (Decl, Get_Writes => True, Get_Reads => True);
1324 for R in Arr'Range loop
1325 Print_Ref (Arr (R));
1338 procedure Print_Unused (Full_Path_Name : in Boolean) is
1339 Decls : constant Declaration_Array_Access := Get_Declarations;
1340 Decl : Declaration_Reference;
1341 Arr : Reference_Array_Access;
1345 for D in Decls'Range loop
1349 (Decl, Get_Reads => True, Get_Writes => True) = 0
1351 F := Osint.To_Host_File_Spec
1352 (Get_Gnatchop_File (Decl, Full_Path_Name));
1353 Write_Str (Get_Symbol (Decl)
1355 & Get_Full_Type (Decl)
1361 & Get_Column (Decl));
1364 -- Print the body if any
1366 Arr := Get_References (Decl, Get_Bodies => True);
1368 for R in Arr'Range loop
1369 F := Osint.To_Host_File_Spec
1370 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1373 & ':' & Get_Line (Arr (R))
1374 & ':' & Get_Column (Arr (R)));
1388 procedure Print_Vi (Full_Path_Name : in Boolean) is
1389 Tab : constant Character := ASCII.HT;
1390 Decls : constant Declaration_Array_Access :=
1391 Get_Declarations (Sorted => False);
1392 Decl : Declaration_Reference;
1393 Arr : Reference_Array_Access;
1397 for D in Decls'Range loop
1400 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1401 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1404 -- Print the body if any
1406 Arr := Get_References (Decl, Get_Bodies => True);
1408 for R in Arr'Range loop
1409 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1411 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1417 -- Print the modifications
1419 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1421 for R in Arr'Range loop
1422 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1424 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1436 procedure Print_Xref (Full_Path_Name : in Boolean) is
1437 Decls : constant Declaration_Array_Access := Get_Declarations;
1438 Decl : Declaration_Reference;
1440 Margin : constant := 10;
1441 -- Column where file names start
1443 procedure New_Line80;
1444 -- Go to start of new line
1446 procedure Print80 (S : in String);
1447 -- Print the text, respecting the 80 columns rule.
1449 procedure Print_Ref (Line, Column : String);
1450 -- The beginning of the output is aligned on a column multiple of 9
1452 procedure Print_List
1453 (Decl : Declaration_Reference;
1455 Get_Reads : Boolean := False;
1456 Get_Writes : Boolean := False;
1457 Get_Bodies : Boolean := False);
1458 -- Print a list of references. If the list is not empty, Msg will
1459 -- be printed prior to the list.
1465 procedure New_Line80 is
1468 Write_Str (String'(1 .. Margin - 1 => ' '));
1475 procedure Print80 (S : in String) is
1476 Align : Natural := Margin - (Integer (Column) mod Margin);
1479 if Align = Margin then
1483 Write_Str (String'(1 .. Align => ' ') & S);
1490 procedure Print_Ref (Line, Column : String) is
1491 Line_Align : constant Integer := 4 - Line'Length;
1493 S : constant String := String'(1 .. Line_Align => ' ')
1494 & Line & ':' & Column;
1496 Align : Natural := Margin - (Integer (Output.Column) mod Margin);
1499 if Align = Margin then
1503 if Integer (Output.Column) + Align + S'Length > 79 then
1508 Write_Str (String'(1 .. Align => ' ') & S);
1515 procedure Print_List
1516 (Decl : Declaration_Reference;
1518 Get_Reads : Boolean := False;
1519 Get_Writes : Boolean := False;
1520 Get_Bodies : Boolean := False)
1522 Arr : Reference_Array_Access :=
1525 Get_Writes => Get_Writes,
1526 Get_Reads => Get_Reads,
1527 Get_Bodies => Get_Bodies);
1528 File : File_Reference := Empty_File;
1532 if Arr'Length /= 0 then
1537 for R in Arr'Range loop
1538 if Get_File_Ref (Arr (R)) /= File then
1539 if File /= Empty_File then
1543 File := Get_File_Ref (Arr (R));
1544 F := Osint.To_Host_File_Spec
1545 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1546 Write_Str (F.all & ' ');
1550 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1558 -- Start of processing for Print_Xref
1561 for D in Decls'Range loop
1564 Write_Str (Get_Symbol (Decl));
1566 while Column < Type_Position loop
1570 Write_Line (Get_Full_Type (Decl));
1572 Write_Parent_Info : declare
1573 Parent : constant Declaration_Reference := Get_Parent (Decl);
1576 if Parent /= Empty_Declaration then
1577 Write_Str (" Ptype: ");
1578 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1581 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1582 Print80 (" " & Get_Symbol (Parent));
1585 end Write_Parent_Info;
1587 Write_Str (" Decl: ");
1588 F := Osint.To_Host_File_Spec
1589 (Get_Gnatchop_File (Decl, Full_Path_Name));
1590 Print80 (F.all & ' ');
1592 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1595 (Decl, " Body: ", Get_Bodies => True);
1597 (Decl, " Modi: ", Get_Writes => True);
1599 (Decl, " Ref: ", Get_Reads => True);
1609 (Pattern : Search_Pattern;
1610 Local_Symbols : Boolean;
1611 Wide_Search : Boolean;
1612 Read_Only : Boolean;
1614 Type_Tree : Boolean)
1616 type String_Access is access String;
1617 procedure Free is new Unchecked_Deallocation (String, String_Access);
1620 File_Ref : File_Reference;
1621 Strip_Num : Natural := 0;
1622 Ali_Name : String_Access;
1625 -- If we want all the .ali files, then find them
1632 -- Get the next unread ali file
1634 File_Ref := Next_Unvisited_File;
1636 exit when File_Ref = Empty_File;
1638 -- Find the ALI file to use. Most of the time, it will be the unit
1639 -- name, with a different extension. However, when dealing with
1640 -- separates the ALI file is in fact the parent's ALI file (and this
1641 -- is recursive, in case the parent itself is a separate).
1646 Ali_Name := new String'
1647 (Get_File (File_Ref, With_Dir => True, Strip => Strip_Num));
1649 -- Stripped too many things...
1651 if Ali_Name.all = "" then
1652 if Get_Emit_Warning (File_Ref) then
1655 ("warning : file " & Get_File (File_Ref, With_Dir => True)
1657 Set_Standard_Output;
1662 -- If not found, try the parent's ALI file (this is needed for
1663 -- separate units and subprograms).
1665 -- Reset the cached directory first, in case the separate's
1666 -- ALI file is not in the same directory.
1668 elsif not File_Exists (Ali_Name.all) then
1669 Strip_Num := Strip_Num + 1;
1670 Reset_Directory (File_Ref);
1672 -- Else we finally found it
1679 -- If we had to get the parent's ALI, insert it in the list as usual.
1680 -- This is to avoid parsing it twice in case it has already been
1683 if Ali_Name /= null and then Strip_Num /= 0 then
1684 File_Ref := Add_To_Xref_File
1685 (File_Name => Ali_Name.all,
1688 -- Now that we have a file name, parse it to find any reference to
1691 elsif Ali_Name /= null
1692 and then (Read_Only or else Is_Writable_File (Ali_Name.all))
1695 Open (Ali_Name.all, ALIfile);
1696 while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1697 Parse_X_Filename (ALIfile);
1698 Parse_Identifier_Info
1699 (Pattern, ALIfile, Local_Symbols,
1700 Der_Info, Type_Tree, Wide_Search, Labels_As_Ref => True);
1704 when No_Xref_Information =>
1705 if Get_Emit_Warning (File_Ref) then
1708 ("warning : No cross-referencing information in "
1710 Set_Standard_Output;
1723 procedure Search_Xref
1724 (Local_Symbols : Boolean;
1725 Read_Only : Boolean;
1729 File_Ref : File_Reference;
1730 Null_Pattern : Search_Pattern;
1733 Null_Pattern.Initialized := False;
1736 -- Find the next unvisited file
1738 File_Ref := Next_Unvisited_File;
1739 exit when File_Ref = Empty_File;
1741 -- Search the object directories for the .ali file
1744 F : constant String := Get_File (File_Ref, With_Dir => True);
1747 if Read_Only or else Is_Writable_File (F) then
1748 Open (F, ALIfile, True);
1750 while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1751 Parse_X_Filename (ALIfile);
1752 Parse_Identifier_Info
1753 (Null_Pattern, ALIfile, Local_Symbols, Der_Info,
1754 Labels_As_Ref => False);
1759 when No_Xref_Information => null;