1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2008, 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 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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Output; use Output;
28 with Types; use Types;
30 with Unchecked_Deallocation;
32 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
33 with Ada.Text_IO; use Ada.Text_IO;
35 with GNAT.Command_Line; use GNAT.Command_Line;
36 with GNAT.IO_Aux; use GNAT.IO_Aux;
38 package body Xref_Lib is
40 Type_Position : constant := 50;
41 -- Column for label identifying type of entity
47 Pipe : constant Character := '|';
48 -- First character on xref lines in the .ali file
50 No_Xref_Information : exception;
51 -- Exception raised when there is no cross-referencing information in
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.
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).
65 function Current_Xref_File (File : ALI_File) return File_Reference;
66 -- Return the file matching the last 'X' line we found while parsing
69 function File_Name (File : ALI_File; Num : Positive) return File_Reference;
70 -- Returns the dependency file name number Num
72 function Get_Full_Type (Decl : Declaration_Reference) return String;
73 -- Returns the full type corresponding to a type letter as found in
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)
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
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
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.
108 procedure Parse_Number
109 (Source : not null access String;
110 Ptr : in out Positive;
111 Number : out Natural);
112 -- Skips any separators and parses Source up to the first character that
113 -- is not a decimal digit. Returns value of parsed digits or 0 if none.
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.
119 procedure Skip_To_First_X_Line
120 (File : in out ALI_File;
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.
134 (Pattern : in out Search_Pattern;
136 Glob : Boolean := False)
138 File_Start : Natural;
139 Line_Start : Natural;
141 Line_Num : Natural := 0;
142 Col_Num : Natural := 0;
144 File_Ref : File_Reference := Empty_File;
145 pragma Warnings (Off, File_Ref);
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;
230 -- Parse the file name
232 Line_Start := Index (Entity (File_Start .. Entity'Last), ":");
234 -- Check if it was a disk:\directory item (for NT and OS/2)
236 if File_Start = Line_Start - 1
237 and then Line_Start < Entity'Last
238 and then Entity (Line_Start + 1) = '\'
240 Line_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
243 if Line_Start = 0 then
244 Line_Start := Entity'Length + 1;
246 elsif Line_Start /= Entity'Last then
247 Col_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
249 if Col_Start = 0 then
250 Col_Start := Entity'Last + 1;
253 if Col_Start > Line_Start + 1 then
255 Line_Num := Natural'Value
256 (Entity (Line_Start + 1 .. Col_Start - 1));
259 when Constraint_Error =>
260 raise Invalid_Argument;
264 if Col_Start < Entity'Last then
266 Col_Num := Natural'Value (Entity
267 (Col_Start + 1 .. Entity'Last));
270 when Constraint_Error => raise Invalid_Argument;
277 (Entity (File_Start .. Line_Start - 1), Visited => True);
278 Pattern.File_Ref := File_Ref;
280 Add_Line (Pattern.File_Ref, Line_Num, Col_Num);
284 (ALI_File_Name (Entity (File_Start .. Line_Start - 1)),
286 Emit_Warning => True);
293 procedure Add_Xref_File (File : String) is
294 File_Ref : File_Reference := Empty_File;
295 pragma Unreferenced (File_Ref);
297 Iterator : Expansion_Iterator;
299 procedure Add_Xref_File_Internal (File : String);
300 -- Do the actual addition of the file
302 ----------------------------
303 -- Add_Xref_File_Internal --
304 ----------------------------
306 procedure Add_Xref_File_Internal (File : String) is
308 -- Case where we have an ALI file, accept it even though this is
309 -- not official usage, since the intention is obvious
311 if Tail (File, 4) = ".ali" then
312 File_Ref := Add_To_Xref_File
313 (File, Visited => False, Emit_Warning => True);
315 -- Normal non-ali file case
318 File_Ref := Add_To_Xref_File (File, Visited => True);
320 File_Ref := Add_To_Xref_File
321 (ALI_File_Name (File),
322 Visited => False, Emit_Warning => True);
324 end Add_Xref_File_Internal;
326 -- Start of processing for Add_Xref_File
329 -- Check if we need to do the expansion
331 if Ada.Strings.Fixed.Index (File, "*") /= 0
332 or else Ada.Strings.Fixed.Index (File, "?") /= 0
334 Start_Expansion (Iterator, File);
338 S : constant String := Expansion (Iterator);
341 exit when S'Length = 0;
342 Add_Xref_File_Internal (S);
347 Add_Xref_File_Internal (File);
351 -----------------------
352 -- Current_Xref_File --
353 -----------------------
355 function Current_Xref_File (File : ALI_File) return File_Reference is
358 end Current_Xref_File;
360 --------------------------
361 -- Default_Project_File --
362 --------------------------
364 function Default_Project_File (Dir_Name : String) return String is
366 Dir_Ent : File_Name_String;
370 Open (My_Dir, Dir_Name);
373 Read (My_Dir, Dir_Ent, Last);
376 if Tail (Dir_Ent (1 .. Last), 4) = ".adp" then
378 -- The first project file found is the good one
381 return Dir_Ent (1 .. Last);
386 return String'(1 .. 0 => ' ');
389 when Directory_Error => return String'(1 .. 0 => ' ');
390 end Default_Project_File;
398 Num : Positive) return File_Reference
401 return File.Dep.Table (Num);
408 procedure Find_ALI_Files is
410 Dir_Ent : File_Name_String;
413 File_Ref : File_Reference;
414 pragma Unreferenced (File_Ref);
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 'h' => return "interface";
526 when 'i' => return Param_String & "integer object";
527 when 'm' => return Param_String & "modular object";
528 when 'o' => return Param_String & "fixed object";
529 when 'p' => return Param_String & "access object";
530 when 'r' => return Param_String & "record object";
531 when 's' => return Param_String & "string object";
532 when 't' => return Param_String & "task object";
533 when 'w' => return Param_String & "protected object";
534 when 'x' => return Param_String & "abstract procedure";
535 when 'y' => return Param_String & "abstract function";
537 when 'K' => return "package";
538 when 'k' => return "generic package";
539 when 'L' => return "statement label";
540 when 'l' => return "loop label";
541 when 'N' => return "named number";
542 when 'n' => return "enumeration literal";
543 when 'q' => return "block label";
544 when 'U' => return "procedure";
545 when 'u' => return "generic procedure";
546 when 'V' => return "function";
547 when 'v' => return "generic function";
548 when 'X' => return "exception";
549 when 'Y' => return "entry";
551 when '+' => return "private type";
553 -- The above should be the only possibilities, but for this kind
554 -- of informational output, we don't want to bomb if we find
555 -- something else, so just return three question marks when we
556 -- have an unknown Abbrev value
559 return "??? (" & Get_Type (Decl) & ")";
563 --------------------------
564 -- Skip_To_First_X_Line --
565 --------------------------
567 procedure Skip_To_First_X_Line
568 (File : in out ALI_File;
572 Ali : String_Access renames File.Buffer;
574 Ptr : Positive := Ali'First;
575 Num_Dependencies : Natural := 0;
576 File_Start : Positive;
578 Gnatchop_Offset : Integer;
579 Gnatchop_Name : Positive;
581 File_Ref : File_Reference;
582 pragma Unreferenced (File_Ref);
585 -- Read all the lines possibly processing with-clauses and dependency
586 -- information and exit on finding the first Xref line.
587 -- A fall-through of the loop means that there is no xref information
588 -- which is an error condition.
590 while Ali (Ptr) /= EOF loop
591 if D_Lines and then Ali (Ptr) = 'D' then
593 -- Found dependency information. Format looks like:
594 -- D src-nam time-stmp checksum [subunit-name] [line:file-name]
596 -- Skip the D and parse the filenam
599 Parse_Token (Ali, Ptr, Token);
603 Num_Dependencies := Num_Dependencies + 1;
604 Set_Last (File.Dep, Num_Dependencies);
606 Parse_Token (Ali, Ptr, Token); -- Skip time-stamp
607 Parse_Token (Ali, Ptr, Token); -- Skip checksum
608 Parse_Token (Ali, Ptr, Token); -- Read next entity on the line
610 if not (Ali (Token) in '0' .. '9') then
611 Parse_Token (Ali, Ptr, Token); -- Was a subunit name
614 -- Did we have a gnatchop-ed file with a pragma Source_Reference ?
616 Gnatchop_Offset := 0;
618 if Ali (Token) in '0' .. '9' then
619 Gnatchop_Name := Token;
620 while Ali (Gnatchop_Name) /= ':' loop
621 Gnatchop_Name := Gnatchop_Name + 1;
625 2 - Natural'Value (Ali (Token .. Gnatchop_Name - 1));
626 Token := Gnatchop_Name + 1;
629 File.Dep.Table (Num_Dependencies) := Add_To_Xref_File
630 (Ali (File_Start .. File_End),
631 Gnatchop_File => Ali (Token .. Ptr - 1),
632 Gnatchop_Offset => Gnatchop_Offset);
634 elsif W_Lines and then Ali (Ptr) = 'W' then
636 -- Found with-clause information. Format looks like:
637 -- "W debug%s debug.adb debug.ali"
639 -- Skip the W and parse the .ali filename (3rd token)
641 Parse_Token (Ali, Ptr, Token);
642 Parse_Token (Ali, Ptr, Token);
643 Parse_Token (Ali, Ptr, Token);
646 Add_To_Xref_File (Ali (Token .. Ptr - 1), Visited => False);
648 elsif Ali (Ptr) = 'X' then
650 -- Found a cross-referencing line - stop processing
652 File.Current_Line := Ptr;
653 File.Xref_Line := Ptr;
657 Parse_EOL (Ali, Ptr);
660 raise No_Xref_Information;
661 end Skip_To_First_X_Line;
670 Dependencies : Boolean := False)
672 Ali : String_Access renames File.Buffer;
673 pragma Warnings (Off, Ali);
676 if File.Buffer /= null then
683 Read_File (Name, Ali);
686 when Ada.Text_IO.Name_Error | Ada.Text_IO.End_Error =>
687 raise No_Xref_Information;
690 Skip_To_First_X_Line (File, D_Lines => True, W_Lines => Dependencies);
698 (Source : not null access String;
699 Ptr : in out Positive;
700 Skip_Continuation_Line : Boolean := False)
704 -- Skip to end of line
706 while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
707 and then Source (Ptr) /= EOF
712 if Source (Ptr) /= EOF then
713 Ptr := Ptr + 1; -- skip CR or LF
716 -- Skip past CR/LF or LF/CR combination
718 if (Source (Ptr) = ASCII.CR or else Source (Ptr) = ASCII.LF)
719 and then Source (Ptr) /= Source (Ptr - 1)
724 exit when not Skip_Continuation_Line or else Source (Ptr) /= '.';
728 ---------------------------
729 -- Parse_Identifier_Info --
730 ---------------------------
732 procedure Parse_Identifier_Info
733 (Pattern : Search_Pattern;
734 File : in out ALI_File;
735 Local_Symbols : Boolean;
736 Der_Info : Boolean := False;
737 Type_Tree : Boolean := False;
738 Wide_Search : Boolean := True;
739 Labels_As_Ref : Boolean := True)
741 Ptr : Positive renames File.Current_Line;
742 Ali : String_Access renames File.Buffer;
744 E_Line : Natural; -- Line number of current entity
745 E_Col : Natural; -- Column number of current entity
746 E_Type : Character; -- Type of current entity
747 E_Name : Positive; -- Pointer to begin of entity name
748 E_Global : Boolean; -- True iff entity is global
750 R_Line : Natural; -- Line number of current reference
751 R_Col : Natural; -- Column number of current reference
752 R_Type : Character; -- Type of current reference
754 Decl_Ref : Declaration_Reference;
755 File_Ref : File_Reference := Current_Xref_File (File);
757 function Get_Symbol_Name (Eun, Line, Col : Natural) return String;
758 -- Returns the symbol name for the entity defined at the specified
759 -- line and column in the dependent unit number Eun. For this we need
760 -- to parse the ali file again because the parent entity is not in
761 -- the declaration table if it did not match the search pattern.
763 procedure Skip_To_Matching_Closing_Bracket;
764 -- When Ptr points to an opening square bracket, moves it to the
765 -- character following the matching closing bracket
767 ---------------------
768 -- Get_Symbol_Name --
769 ---------------------
771 function Get_Symbol_Name (Eun, Line, Col : Natural) return String is
773 E_Eun : Positive; -- Unit number of current entity
774 E_Line : Natural; -- Line number of current entity
775 E_Col : Natural; -- Column number of current entity
776 E_Name : Positive; -- Pointer to begin of entity name
779 -- Look for the X lines corresponding to unit Eun
782 if Ali (Ptr) = 'X' then
784 Parse_Number (Ali, Ptr, E_Eun);
785 exit when E_Eun = Eun;
788 Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
791 -- Here we are in the right Ali section, we now look for the entity
792 -- declared at position (Line, Col).
795 Parse_Number (Ali, Ptr, E_Line);
796 exit when Ali (Ptr) = EOF;
798 Parse_Number (Ali, Ptr, E_Col);
799 exit when Ali (Ptr) = EOF;
802 if Line = E_Line and then Col = E_Col then
803 Parse_Token (Ali, Ptr, E_Name);
804 return Ali (E_Name .. Ptr - 1);
807 Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
808 exit when Ali (Ptr) = EOF;
811 -- We were not able to find the symbol, this should not happen but
812 -- since we don't want to stop here we return a string of three
813 -- question marks as the symbol name.
818 --------------------------------------
819 -- Skip_To_Matching_Closing_Bracket --
820 --------------------------------------
822 procedure Skip_To_Matching_Closing_Bracket is
823 Num_Brackets : Natural;
827 while Num_Brackets /= 0 loop
829 if Ali (Ptr) = '[' then
830 Num_Brackets := Num_Brackets + 1;
831 elsif Ali (Ptr) = ']' then
832 Num_Brackets := Num_Brackets - 1;
837 end Skip_To_Matching_Closing_Bracket;
839 -- Start of processing for Parse_Identifier_Info
842 -- The identifier info looks like:
843 -- "38U9*Debug 12|36r6 36r19"
845 -- Extract the line, column and entity name information
847 Parse_Number (Ali, Ptr, E_Line);
849 if Ali (Ptr) > ' ' then
854 -- Ignore some of the entities (labels,...)
857 when 'l' | 'L' | 'q' =>
858 Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
865 Parse_Number (Ali, Ptr, E_Col);
868 if Ali (Ptr) >= ' ' then
869 E_Global := (Ali (Ptr) = '*');
873 Parse_Token (Ali, Ptr, E_Name);
875 -- Exit if the symbol does not match
876 -- or if we have a local symbol and we do not want it
878 if (not Local_Symbols and not E_Global)
879 or else (Pattern.Initialized
880 and then not Match (Ali (E_Name .. Ptr - 1), Pattern.Entity))
881 or else (E_Name >= Ptr)
883 Decl_Ref := Add_Declaration
884 (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type,
885 Remove_Only => True);
886 Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
890 -- Insert the declaration in the table
892 Decl_Ref := Add_Declaration
893 (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type);
895 if Ali (Ptr) = '[' then
896 Skip_To_Matching_Closing_Bracket;
900 or else Ali (Ptr) = '('
901 or else Ali (Ptr) = '{'
903 -- Here we have a type derivation information. The format is
904 -- <3|12I45> which means that the current entity is derived from the
905 -- type defined in unit number 3, line 12 column 45. The pipe and
906 -- unit number is optional. It is specified only if the parent type
907 -- is not defined in the current unit.
909 -- We also have the format for generic instantiations, as in
910 -- 7a5*Uid(3|5I8[4|2]) 2|4r74
912 -- We could also have something like
914 -- that indicates that I derives from the predefined type integer.
918 if Ali (Ptr) in '0' .. '9' then
919 Parse_Derived_Info : declare
920 P_Line : Natural; -- parent entity line
921 P_Column : Natural; -- parent entity column
922 P_Eun : Positive; -- parent entity file number
925 Parse_Number (Ali, Ptr, P_Line);
927 -- If we have a pipe then the first number was the unit number
929 if Ali (Ptr) = '|' then
933 -- Now we have the line number
935 Parse_Number (Ali, Ptr, P_Line);
938 -- We don't have a unit number specified, so we set P_Eun to
941 for K in Dependencies_Tables.First .. Last (File.Dep) loop
943 exit when File.Dep.Table (K) = File_Ref;
947 -- Then parse the type and column number
950 Parse_Number (Ali, Ptr, P_Column);
952 -- Skip the information for generics instantiations
954 if Ali (Ptr) = '[' then
955 Skip_To_Matching_Closing_Bracket;
958 -- Skip '>', or ')' or '>'
962 -- The derived info is needed only is the derived info mode is
963 -- on or if we want to output the type hierarchy
965 if Der_Info or else Type_Tree then
967 Symbol : constant String :=
968 Get_Symbol_Name (P_Eun, P_Line, P_Column);
970 if Symbol /= "???" then
976 File.Dep.Table (P_Eun));
982 and then (Pattern.File_Ref = Empty_File
984 Pattern.File_Ref = Current_Xref_File (File))
986 Search_Parent_Tree : declare
987 Pattern : Search_Pattern; -- Parent type pattern
988 File_Pos_Backup : Positive;
993 Get_Symbol_Name (P_Eun, P_Line, P_Column)
994 & ':' & Get_Gnatchop_File (File.Dep.Table (P_Eun))
995 & ':' & Get_Line (Get_Parent (Decl_Ref))
996 & ':' & Get_Column (Get_Parent (Decl_Ref)),
999 -- No default match is needed to look for the parent type
1000 -- since we are using the fully qualified symbol name:
1001 -- symbol:file:line:column
1003 Set_Default_Match (False);
1005 -- The parent hierarchy is defined in the same unit as
1006 -- the derived type. So we want to revisit the unit.
1008 File_Pos_Backup := File.Current_Line;
1010 Skip_To_First_X_Line
1011 (File, D_Lines => False, W_Lines => False);
1013 while File.Buffer (File.Current_Line) /= EOF loop
1014 Parse_X_Filename (File);
1015 Parse_Identifier_Info
1016 (Pattern => Pattern,
1018 Local_Symbols => False,
1019 Der_Info => Der_Info,
1021 Wide_Search => False,
1022 Labels_As_Ref => Labels_As_Ref);
1025 File.Current_Line := File_Pos_Backup;
1026 end Search_Parent_Tree;
1028 end Parse_Derived_Info;
1031 while Ali (Ptr) /= '>'
1032 and then Ali (Ptr) /= ')'
1033 and then Ali (Ptr) /= '}'
1040 elsif Ali (Ptr) = '=' then
1042 P_Line, P_Column : Natural;
1043 pragma Warnings (Off, P_Line);
1044 pragma Warnings (Off, P_Column);
1048 Parse_Number (Ali, Ptr, P_Line);
1050 Parse_Number (Ali, Ptr, P_Column);
1054 -- To find the body, we will have to parse the file too
1058 File_Ref : File_Reference;
1059 pragma Unreferenced (File_Ref);
1060 File_Name : constant String := Get_Gnatchop_File (File.X_File);
1062 File_Ref := Add_To_Xref_File (ALI_File_Name (File_Name), False);
1066 -- Parse references to this entity.
1067 -- Ptr points to next reference with leading blanks
1070 -- Process references on current line
1072 while Ali (Ptr) = ' ' or Ali (Ptr) = ASCII.HT loop
1074 -- For every reference read the line, type and column,
1075 -- optionally preceded by a file number and a pipe symbol.
1077 Parse_Number (Ali, Ptr, R_Line);
1079 if Ali (Ptr) = Pipe then
1081 File_Ref := File_Name (File, R_Line);
1083 Parse_Number (Ali, Ptr, R_Line);
1086 if Ali (Ptr) > ' ' then
1087 R_Type := Ali (Ptr);
1091 -- Imported entities might special indication as to their external
1093 -- 5U14*Foo2 5>20 6b<c,myfoo2>22
1096 and then Ali (Ptr) = '<'
1098 while Ptr <= Ali'Last
1099 and then Ali (Ptr) /= '>'
1106 Parse_Number (Ali, Ptr, R_Col);
1108 -- Insert the reference or body in the table
1111 (Decl_Ref, File_Ref, R_Line, R_Col, R_Type, Labels_As_Ref);
1113 -- Skip generic information, if any
1115 if Ali (Ptr) = '[' then
1117 Num_Nested : Integer := 1;
1121 while Num_Nested /= 0 loop
1122 if Ali (Ptr) = ']' then
1123 Num_Nested := Num_Nested - 1;
1124 elsif Ali (Ptr) = '[' then
1125 Num_Nested := Num_Nested + 1;
1135 Parse_EOL (Ali, Ptr);
1137 -- Loop until new line is no continuation line
1139 exit when Ali (Ptr) /= '.';
1142 end Parse_Identifier_Info;
1148 procedure Parse_Number
1149 (Source : not null access String;
1150 Ptr : in out Positive;
1151 Number : out Natural)
1156 while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1161 while Source (Ptr) in '0' .. '9' loop
1163 10 * Number + (Character'Pos (Source (Ptr)) - Character'Pos ('0'));
1172 procedure Parse_Token
1173 (Source : not null access String;
1174 Ptr : in out Positive;
1175 Token_Ptr : out Positive)
1177 In_Quotes : Character := ASCII.NUL;
1182 while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1188 -- Find end-of-token
1190 while (In_Quotes /= ASCII.NUL or else
1191 not (Source (Ptr) = ' '
1192 or else Source (Ptr) = ASCII.HT
1193 or else Source (Ptr) = '<'
1194 or else Source (Ptr) = '{'
1195 or else Source (Ptr) = '['
1196 or else Source (Ptr) = '='
1197 or else Source (Ptr) = '('))
1198 and then Source (Ptr) >= ' '
1200 -- Double-quotes are used for operators
1201 -- Simple-quotes are used for character constants, for instance when
1202 -- they are found in an enumeration type "type A is ('+', '-');"
1204 case Source (Ptr) is
1206 if In_Quotes = Source (Ptr) then
1207 In_Quotes := ASCII.NUL;
1208 elsif In_Quotes = ASCII.NUL then
1209 In_Quotes := Source (Ptr);
1220 ----------------------
1221 -- Parse_X_Filename --
1222 ----------------------
1224 procedure Parse_X_Filename (File : in out ALI_File) is
1225 Ali : String_Access renames File.Buffer;
1226 Ptr : Positive renames File.Current_Line;
1230 while Ali (Ptr) = 'X' loop
1232 -- The current line is the start of a new Xref file section,
1233 -- whose format looks like:
1237 -- Skip the X and read the file number for the new X_File
1240 Parse_Number (Ali, Ptr, File_Nr);
1243 File.X_File := File.Dep.Table (File_Nr);
1246 Parse_EOL (Ali, Ptr);
1248 end Parse_X_Filename;
1250 --------------------
1251 -- Print_Gnatfind --
1252 --------------------
1254 procedure Print_Gnatfind
1255 (References : Boolean;
1256 Full_Path_Name : Boolean)
1258 Decls : constant Declaration_Array_Access := Get_Declarations;
1259 Decl : Declaration_Reference;
1260 Arr : Reference_Array_Access;
1264 Msg : String := " ");
1265 -- Print a reference, according to the extended tag of the output
1273 Msg : String := " ")
1275 F : String_Access :=
1276 Osint.To_Host_File_Spec
1277 (Get_Gnatchop_File (Ref, Full_Path_Name));
1279 Buffer : constant String :=
1281 ":" & Get_Line (Ref) &
1282 ":" & Get_Column (Ref) &
1285 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1289 Num_Blanks := Integer'Max (0, Num_Blanks);
1292 & String'(1 .. Num_Blanks => ' ')
1293 & Msg & " " & Get_Symbol (Decl));
1295 if Get_Source_Line (Ref)'Length /= 0 then
1296 Write_Line (" " & Get_Source_Line (Ref));
1300 -- Start of processing for Print_Gnatfind
1303 for D in Decls'Range loop
1306 if Match (Decl) then
1308 -- Output the declaration
1311 Parent : constant Declaration_Reference := Get_Parent (Decl);
1313 F : String_Access :=
1314 Osint.To_Host_File_Spec
1315 (Get_Gnatchop_File (Decl, Full_Path_Name));
1317 Buffer : constant String :=
1319 ":" & Get_Line (Decl) &
1320 ":" & Get_Column (Decl) &
1323 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1327 Num_Blanks := Integer'Max (0, Num_Blanks);
1329 (Buffer & String'(1 .. Num_Blanks => ' ')
1330 & "(spec) " & Get_Symbol (Decl));
1332 if Parent /= Empty_Declaration then
1333 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1335 (Buffer & String'(1 .. Num_Blanks => ' ')
1336 & " derived from " & Get_Symbol (Parent)
1339 & ':' & Get_Line (Parent)
1340 & ':' & Get_Column (Parent) & ')');
1345 if Get_Source_Line (Decl)'Length /= 0 then
1346 Write_Line (" " & Get_Source_Line (Decl));
1349 -- Output the body (sorted)
1351 Arr := Get_References (Decl, Get_Bodies => True);
1353 for R in Arr'Range loop
1354 Print_Ref (Arr (R), "(body)");
1360 Arr := Get_References
1361 (Decl, Get_Writes => True, Get_Reads => True);
1363 for R in Arr'Range loop
1364 Print_Ref (Arr (R));
1377 procedure Print_Unused (Full_Path_Name : Boolean) is
1378 Decls : constant Declaration_Array_Access := Get_Declarations;
1379 Decl : Declaration_Reference;
1380 Arr : Reference_Array_Access;
1384 for D in Decls'Range loop
1388 (Decl, Get_Reads => True, Get_Writes => True) = 0
1390 F := Osint.To_Host_File_Spec
1391 (Get_Gnatchop_File (Decl, Full_Path_Name));
1392 Write_Str (Get_Symbol (Decl)
1394 & Get_Full_Type (Decl)
1400 & Get_Column (Decl));
1403 -- Print the body if any
1405 Arr := Get_References (Decl, Get_Bodies => True);
1407 for R in Arr'Range loop
1408 F := Osint.To_Host_File_Spec
1409 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1412 & ':' & Get_Line (Arr (R))
1413 & ':' & Get_Column (Arr (R)));
1427 procedure Print_Vi (Full_Path_Name : Boolean) is
1428 Tab : constant Character := ASCII.HT;
1429 Decls : constant Declaration_Array_Access :=
1430 Get_Declarations (Sorted => False);
1431 Decl : Declaration_Reference;
1432 Arr : Reference_Array_Access;
1436 for D in Decls'Range loop
1439 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1440 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1443 -- Print the body if any
1445 Arr := Get_References (Decl, Get_Bodies => True);
1447 for R in Arr'Range loop
1448 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1450 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1456 -- Print the modifications
1458 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1460 for R in Arr'Range loop
1461 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1463 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1475 procedure Print_Xref (Full_Path_Name : Boolean) is
1476 Decls : constant Declaration_Array_Access := Get_Declarations;
1477 Decl : Declaration_Reference;
1479 Margin : constant := 10;
1480 -- Column where file names start
1482 procedure New_Line80;
1483 -- Go to start of new line
1485 procedure Print80 (S : String);
1486 -- Print the text, respecting the 80 columns rule
1488 procedure Print_Ref (Line, Column : String);
1489 -- The beginning of the output is aligned on a column multiple of 9
1491 procedure Print_List
1492 (Decl : Declaration_Reference;
1494 Get_Reads : Boolean := False;
1495 Get_Writes : Boolean := False;
1496 Get_Bodies : Boolean := False);
1497 -- Print a list of references. If the list is not empty, Msg will
1498 -- be printed prior to the list.
1504 procedure New_Line80 is
1507 Write_Str (String'(1 .. Margin - 1 => ' '));
1514 procedure Print80 (S : String) is
1515 Align : Natural := Margin - (Integer (Column) mod Margin);
1518 if Align = Margin then
1522 Write_Str (String'(1 .. Align => ' ') & S);
1529 procedure Print_Ref (Line, Column : String) is
1530 Line_Align : constant Integer := 4 - Line'Length;
1532 S : constant String := String'(1 .. Line_Align => ' ')
1533 & Line & ':' & Column;
1535 Align : Natural := Margin - (Integer (Output.Column) mod Margin);
1538 if Align = Margin then
1542 if Integer (Output.Column) + Align + S'Length > 79 then
1547 Write_Str (String'(1 .. Align => ' ') & S);
1554 procedure Print_List
1555 (Decl : Declaration_Reference;
1557 Get_Reads : Boolean := False;
1558 Get_Writes : Boolean := False;
1559 Get_Bodies : Boolean := False)
1561 Arr : Reference_Array_Access :=
1564 Get_Writes => Get_Writes,
1565 Get_Reads => Get_Reads,
1566 Get_Bodies => Get_Bodies);
1567 File : File_Reference := Empty_File;
1571 if Arr'Length /= 0 then
1576 for R in Arr'Range loop
1577 if Get_File_Ref (Arr (R)) /= File then
1578 if File /= Empty_File then
1582 File := Get_File_Ref (Arr (R));
1583 F := Osint.To_Host_File_Spec
1584 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1585 Write_Str (F.all & ' ');
1589 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1597 -- Start of processing for Print_Xref
1600 for D in Decls'Range loop
1603 Write_Str (Get_Symbol (Decl));
1605 while Column < Type_Position loop
1609 Write_Line (Get_Full_Type (Decl));
1611 Write_Parent_Info : declare
1612 Parent : constant Declaration_Reference := Get_Parent (Decl);
1615 if Parent /= Empty_Declaration then
1616 Write_Str (" Ptype: ");
1617 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1620 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1621 Print80 (" " & Get_Symbol (Parent));
1624 end Write_Parent_Info;
1626 Write_Str (" Decl: ");
1627 F := Osint.To_Host_File_Spec
1628 (Get_Gnatchop_File (Decl, Full_Path_Name));
1629 Print80 (F.all & ' ');
1631 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1634 (Decl, " Body: ", Get_Bodies => True);
1636 (Decl, " Modi: ", Get_Writes => True);
1638 (Decl, " Ref: ", Get_Reads => True);
1648 (Pattern : Search_Pattern;
1649 Local_Symbols : Boolean;
1650 Wide_Search : Boolean;
1651 Read_Only : Boolean;
1653 Type_Tree : Boolean)
1655 type String_Access is access String;
1656 procedure Free is new Unchecked_Deallocation (String, String_Access);
1659 File_Ref : File_Reference;
1660 Strip_Num : Natural := 0;
1661 Ali_Name : String_Access;
1664 -- If we want all the .ali files, then find them
1671 -- Get the next unread ali file
1673 File_Ref := Next_Unvisited_File;
1675 exit when File_Ref = Empty_File;
1677 -- Find the ALI file to use. Most of the time, it will be the unit
1678 -- name, with a different extension. However, when dealing with
1679 -- separates the ALI file is in fact the parent's ALI file (and this
1680 -- is recursive, in case the parent itself is a separate).
1685 Ali_Name := new String'
1686 (Get_File (File_Ref, With_Dir => True, Strip => Strip_Num));
1688 -- Stripped too many things...
1690 if Ali_Name.all = "" then
1691 if Get_Emit_Warning (File_Ref) then
1694 ("warning : file " & Get_File (File_Ref, With_Dir => True)
1696 Set_Standard_Output;
1701 -- If not found, try the parent's ALI file (this is needed for
1702 -- separate units and subprograms).
1704 -- Reset the cached directory first, in case the separate's
1705 -- ALI file is not in the same directory.
1707 elsif not File_Exists (Ali_Name.all) then
1708 Strip_Num := Strip_Num + 1;
1709 Reset_Directory (File_Ref);
1711 -- Else we finally found it
1718 -- If we had to get the parent's ALI, insert it in the list as usual.
1719 -- This is to avoid parsing it twice in case it has already been
1722 if Ali_Name /= null and then Strip_Num /= 0 then
1723 File_Ref := Add_To_Xref_File
1724 (File_Name => Ali_Name.all,
1727 -- Now that we have a file name, parse it to find any reference to
1730 elsif Ali_Name /= null
1731 and then (Read_Only or else Is_Writable_File (Ali_Name.all))
1734 Open (Ali_Name.all, ALIfile);
1735 while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1736 Parse_X_Filename (ALIfile);
1737 Parse_Identifier_Info
1738 (Pattern, ALIfile, Local_Symbols,
1739 Der_Info, Type_Tree, Wide_Search, Labels_As_Ref => True);
1743 when No_Xref_Information =>
1744 if Get_Emit_Warning (File_Ref) then
1747 ("warning : No cross-referencing information in "
1749 Set_Standard_Output;
1762 procedure Search_Xref
1763 (Local_Symbols : Boolean;
1764 Read_Only : Boolean;
1768 File_Ref : File_Reference;
1769 Null_Pattern : Search_Pattern;
1772 Null_Pattern.Initialized := False;
1775 -- Find the next unvisited file
1777 File_Ref := Next_Unvisited_File;
1778 exit when File_Ref = Empty_File;
1780 -- Search the object directories for the .ali file
1783 F : constant String := Get_File (File_Ref, With_Dir => True);
1786 if Read_Only or else Is_Writable_File (F) then
1787 Open (F, ALIfile, True);
1789 while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1790 Parse_X_Filename (ALIfile);
1791 Parse_Identifier_Info
1792 (Null_Pattern, ALIfile, Local_Symbols, Der_Info,
1793 Labels_As_Ref => False);
1798 when No_Xref_Information => null;