1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1998-2002 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 ------------------------------------------------------------------------------
28 with Output; use Output;
29 with Types; use Types;
30 with Unchecked_Deallocation;
32 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
34 with GNAT.Command_Line; use GNAT.Command_Line;
35 with GNAT.IO_Aux; use GNAT.IO_Aux;
37 package body Xref_Lib is
39 Type_Position : constant := 50;
40 -- Column for label identifying type of entity
46 Pipe : constant Character := '|';
47 -- First character on xref lines in the .ali file
49 EOF : constant Character := ASCII.SUB;
50 -- Special character to signal end of file. Not required in input file,
51 -- but should be properly treated if present. See also Read_File.
53 No_Xref_Information : exception;
54 -- Exception raised when there is no cross-referencing information in
57 subtype File_Offset is Natural;
60 (FD : File_Descriptor;
61 Contents : out String_Access;
62 Success : out Boolean);
63 -- Reads file associated with FS into the newly allocated
64 -- string Contents. An EOF character will be added to the
65 -- returned Contents to simplify parsing.
66 -- [VMS] Success is true iff the number of bytes read is less than or
67 -- equal to the file size.
68 -- [Other] Success is true iff the number of bytes read is equal to
71 procedure Parse_EOL (Source : access String; Ptr : in out Positive);
72 -- On return Source (Ptr) is the first character of the next line
73 -- or EOF. Source.all must be terminated by EOF.
75 procedure Parse_Identifier_Info
76 (Pattern : Search_Pattern;
77 File : in out ALI_File;
78 Local_Symbols : Boolean;
79 Der_Info : Boolean := False;
80 Type_Tree : Boolean := False;
81 Wide_Search : Boolean := True);
82 -- Output the file and the line where the identifier was referenced,
83 -- If Local_Symbols is False then only the publicly visible symbols
87 (Source : access String;
88 Ptr : in out Positive;
89 Token_Ptr : out Positive);
90 -- Skips any separators and stores the start of the token in Token_Ptr.
91 -- Then stores the position of the next separator in Ptr.
92 -- On return Source (Token_Ptr .. Ptr - 1) is the token.
93 -- Separators are space and ASCII.HT.
94 -- Parse_Token will never skip to the next line.
96 procedure Parse_Number
97 (Source : access String;
98 Ptr : in out Positive;
99 Number : out Natural);
100 -- Skips any separators and parses Source upto the first character that
101 -- is not a decimal digit. Returns value of parsed digits or 0 if none.
103 procedure Parse_X_Filename (File : in out ALI_File);
104 -- Reads and processes "X..." lines in the ALI file
105 -- and updates the File.X_File information.
112 (Pattern : in out Search_Pattern;
114 Glob : Boolean := False)
116 File_Start : Natural;
117 Line_Start : Natural;
119 Line_Num : Natural := 0;
120 Col_Num : Natural := 0;
121 File_Ref : File_Reference := Empty_File;
122 File_Existed : Boolean;
123 Has_Pattern : Boolean := False;
126 -- Find the end of the first item in Entity (pattern or file?)
127 -- If there is no ':', we only have a pattern
129 File_Start := Index (Entity, ":");
130 if File_Start = 0 then
132 -- If the regular expression is invalid, just consider it as a string
135 Pattern.Entity := Compile (Entity, Glob, False);
136 Pattern.Initialized := True;
139 when Error_In_Regexp =>
141 -- The basic idea is to insert a \ before every character
144 Tmp_Regexp : String (1 .. 2 * Entity'Length);
145 Index : Positive := 1;
148 for J in Entity'Range loop
149 Tmp_Regexp (Index) := '\';
150 Tmp_Regexp (Index + 1) := Entity (J);
154 Pattern.Entity := Compile (Tmp_Regexp, True, False);
155 Pattern.Initialized := True;
159 Set_Default_Match (True);
163 -- If there is a dot in the pattern, then it is a file name
166 Index (Entity (Entity'First .. File_Start - 1), ".") /= 0)
169 and then Index (Entity (Entity'First .. File_Start - 1),
172 Pattern.Entity := Compile (".*", False);
173 Pattern.Initialized := True;
174 File_Start := Entity'First;
177 -- If the regular expression is invalid,
178 -- just consider it as a string
182 Compile (Entity (Entity'First .. File_Start - 1), Glob, False);
183 Pattern.Initialized := True;
186 when Error_In_Regexp =>
188 -- The basic idea is to insert a \ before every character
191 Tmp_Regexp : String (1 .. 2 * (File_Start - Entity'First));
192 Index : Positive := 1;
195 for J in Entity'First .. File_Start - 1 loop
196 Tmp_Regexp (Index) := '\';
197 Tmp_Regexp (Index + 1) := Entity (J);
201 Pattern.Entity := Compile (Tmp_Regexp, True, False);
202 Pattern.Initialized := True;
206 File_Start := File_Start + 1;
210 -- Parse the file name
212 Line_Start := Index (Entity (File_Start .. Entity'Last), ":");
214 -- Check if it was a disk:\directory item (for NT and OS/2)
216 if File_Start = Line_Start - 1
217 and then Line_Start < Entity'Last
218 and then Entity (Line_Start + 1) = '\'
220 Line_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
223 if Line_Start = 0 then
224 Line_Start := Entity'Length + 1;
226 elsif Line_Start /= Entity'Last then
227 Col_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
229 if Col_Start = 0 then
230 Col_Start := Entity'Last + 1;
233 if Col_Start > Line_Start + 1 then
235 Line_Num := Natural'Value
236 (Entity (Line_Start + 1 .. Col_Start - 1));
239 when Constraint_Error =>
240 raise Invalid_Argument;
244 if Col_Start < Entity'Last then
246 Col_Num := Natural'Value (Entity
247 (Col_Start + 1 .. Entity'Last));
250 when Constraint_Error => raise Invalid_Argument;
255 Add_To_Xref_File (Entity (File_Start .. Line_Start - 1),
259 Add_Line (File_Ref, Line_Num, Col_Num);
261 (ALI_File_Name (Entity (File_Start .. Line_Start - 1)),
262 File_Existed, File_Ref,
264 Emit_Warning => True);
271 procedure Add_Xref_File (File : String) is
272 File_Ref : File_Reference := Empty_File;
273 File_Existed : Boolean;
274 Iterator : Expansion_Iterator;
276 procedure Add_Xref_File_Internal (File : String);
277 -- Do the actual addition of the file
279 ----------------------------
280 -- Add_Xref_File_Internal --
281 ----------------------------
283 procedure Add_Xref_File_Internal (File : String) is
285 -- Case where we have an ALI file, accept it even though this is
286 -- not official usage, since the intention is obvious
288 if Tail (File, 4) = ".ali" then
294 Emit_Warning => True);
296 -- Normal non-ali file case
306 (ALI_File_Name (File),
310 Emit_Warning => True);
312 end Add_Xref_File_Internal;
314 -- Start of processing for Add_Xref_File
317 -- Check if we need to do the expansion
319 if Ada.Strings.Fixed.Index (File, "*") /= 0
320 or else Ada.Strings.Fixed.Index (File, "?") /= 0
322 Start_Expansion (Iterator, File);
326 S : constant String := Expansion (Iterator);
329 exit when S'Length = 0;
330 Add_Xref_File_Internal (S);
335 Add_Xref_File_Internal (File);
339 -----------------------
340 -- Current_Xref_File --
341 -----------------------
343 function Current_Xref_File (File : ALI_File) return File_Reference is
346 end Current_Xref_File;
348 --------------------------
349 -- Default_Project_File --
350 --------------------------
352 function Default_Project_File
357 Dir_Ent : File_Name_String;
361 Open (My_Dir, Dir_Name);
364 Read (My_Dir, Dir_Ent, Last);
367 if Tail (Dir_Ent (1 .. Last), 4) = ".adp" then
369 -- The first project file found is the good one.
372 return Dir_Ent (1 .. Last);
377 return String'(1 .. 0 => ' ');
380 when Directory_Error => return String'(1 .. 0 => ' ');
381 end Default_Project_File;
390 return File_Reference
393 return File.Dep.Table (Num);
400 procedure Find_ALI_Files is
402 Dir_Ent : File_Name_String;
404 File_Existed : Boolean;
405 File_Ref : File_Reference;
407 function Open_Next_Dir return Boolean;
408 -- Tries to open the next object directory, and return False if
409 -- the directory cannot be opened.
415 function Open_Next_Dir return Boolean is
417 -- Until we are able to open a new directory
421 Obj_Dir : constant String := Next_Obj_Dir;
424 -- If there was no more Obj_Dir line
426 if Obj_Dir'Length = 0 then
430 Open (My_Dir.Dir, Obj_Dir);
434 -- Could not open the directory
436 when Directory_Error => null;
443 -- Start of processing for Find_ALI_Files
446 if Open_Next_Dir then
448 Read (My_Dir.Dir, Dir_Ent, Last);
453 if not Open_Next_Dir then
457 elsif Last > 4 and then Dir_Ent (Last - 3 .. Last) = ".ali" then
459 (Dir_Ent (1 .. Last),
463 Set_Directory (File_Ref, Current_Obj_Dir);
473 function Get_Full_Type (Abbrev : Character) return String is
476 when 'A' => return "array type";
477 when 'B' => return "boolean type";
478 when 'C' => return "class-wide type";
479 when 'D' => return "decimal type";
480 when 'E' => return "enumeration type";
481 when 'F' => return "float type";
482 when 'I' => return "integer type";
483 when 'M' => return "modular type";
484 when 'O' => return "fixed type";
485 when 'P' => return "access type";
486 when 'R' => return "record type";
487 when 'S' => return "string type";
488 when 'T' => return "task type";
489 when 'W' => return "protected type";
491 when 'a' => return "array type";
492 when 'b' => return "boolean object";
493 when 'c' => return "class-wide object";
494 when 'd' => return "decimal object";
495 when 'e' => return "enumeration object";
496 when 'f' => return "float object";
497 when 'i' => return "integer object";
498 when 'm' => return "modular object";
499 when 'o' => return "fixed object";
500 when 'p' => return "access object";
501 when 'r' => return "record object";
502 when 's' => return "string object";
503 when 't' => return "task object";
504 when 'w' => return "protected object";
506 when 'K' => return "package";
507 when 'k' => return "generic package";
508 when 'L' => return "statement label";
509 when 'l' => return "loop label";
510 when 'N' => return "named number";
511 when 'n' => return "enumeration literal";
512 when 'q' => return "block label";
513 when 'U' => return "procedure";
514 when 'u' => return "generic procedure";
515 when 'V' => return "function";
516 when 'v' => return "generic function";
517 when 'X' => return "exception";
518 when 'Y' => return "entry";
520 -- The above should be the only possibilities, but for a
521 -- tool like this we don't want to bomb if we find something
522 -- else, so just return ??? when we have an unknown Abbrev value
534 (Pattern : Search_Pattern;
539 -- Get the entity name
541 return Match (Symbol, Pattern.Entity);
551 Dependencies : Boolean := False)
553 Name_0 : constant String := Name & ASCII.NUL;
554 Num_Dependencies : Natural := 0;
555 File_Existed : Boolean;
556 File_Ref : File_Reference;
557 FD : File_Descriptor;
558 Success : Boolean := False;
559 Ali : String_Access renames File.Buffer;
562 File_Start : Positive;
564 Gnatchop_Offset : Integer;
565 Gnatchop_Name : Positive;
568 if File.Buffer /= null then
574 FD := Open_Read (Name_0'Address, Binary);
576 if FD = Invalid_FD then
577 raise No_Xref_Information;
580 Read_File (FD, Ali, Success);
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
592 if Ali (Ptr) = 'D' then
593 -- Found dependency information. Format looks like:
594 -- D source-name time-stamp checksum [subunit-name] \
597 -- Skip the D and parse the filename
600 Parse_Token (Ali, Ptr, Token);
604 Num_Dependencies := Num_Dependencies + 1;
605 Set_Last (File.Dep, Num_Dependencies);
607 Parse_Token (Ali, Ptr, Token); -- Skip time-stamp
608 Parse_Token (Ali, Ptr, Token); -- Skip checksum
609 Parse_Token (Ali, Ptr, Token); -- Read next entity on the line
611 if not (Ali (Token) in '0' .. '9') then
612 Parse_Token (Ali, Ptr, Token); -- Was a subunit name
615 -- 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;
624 2 - Natural'Value (Ali (Token .. Gnatchop_Name - 1));
625 Token := Gnatchop_Name + 1;
629 (Ali (File_Start .. File_End),
631 File.Dep.Table (Num_Dependencies),
632 Gnatchop_File => Ali (Token .. Ptr - 1),
633 Gnatchop_Offset => Gnatchop_Offset);
635 elsif Dependencies 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 (Ali (Token .. Ptr - 1),
651 elsif Ali (Ptr) = 'X' then
652 -- Found a cross-referencing line - stop processing
654 File.Current_Line := Ptr;
655 File.Xref_Line := Ptr;
659 Parse_EOL (Ali, Ptr);
662 raise No_Xref_Information;
669 procedure Parse_EOL (Source : access String; Ptr : in out Positive) is
671 -- Skip to end of line
673 while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
674 and then Source (Ptr) /= EOF
679 if Source (Ptr) /= EOF then
680 Ptr := Ptr + 1; -- skip CR or LF
683 -- Skip past CR/LF or LF/CR combination
685 if (Source (Ptr) = ASCII.CR or else Source (Ptr) = ASCII.LF)
686 and then Source (Ptr) /= Source (Ptr - 1)
692 ---------------------------
693 -- Parse_Identifier_Info --
694 ---------------------------
696 procedure Parse_Identifier_Info
697 (Pattern : Search_Pattern;
698 File : in out ALI_File;
699 Local_Symbols : Boolean;
700 Der_Info : Boolean := False;
701 Type_Tree : Boolean := False;
702 Wide_Search : Boolean := True)
704 Ptr : Positive renames File.Current_Line;
705 Ali : String_Access renames File.Buffer;
707 E_Line : Natural; -- Line number of current entity
708 E_Col : Natural; -- Column number of current entity
709 E_Type : Character; -- Type of current entity
710 E_Name : Positive; -- Pointer to begin of entity name
711 E_Global : Boolean; -- True iff entity is global
713 R_Line : Natural; -- Line number of current reference
714 R_Col : Natural; -- Column number of current reference
715 R_Type : Character; -- Type of current reference
717 Decl_Ref : Declaration_Reference;
718 File_Ref : File_Reference := Current_Xref_File (File);
720 function Get_Symbol_Name (Eun, Line, Col : Natural) return String;
721 -- Returns the symbol name for the entity defined at the specified
722 -- line and column in the dependent unit number Eun. For this we need
723 -- to parse the ali file again because the parent entity is not in
724 -- the declaration table if it did not match the search pattern.
726 ---------------------
727 -- Get_Symbol_Name --
728 ---------------------
730 function Get_Symbol_Name (Eun, Line, Col : Natural) return String is
732 E_Eun : Positive; -- Unit number of current entity
733 E_Line : Natural; -- Line number of current entity
734 E_Col : Natural; -- Column number of current entity
735 E_Name : Positive; -- Pointer to begin of entity name
736 E_Type : Character; -- Type of current entity
739 -- skip current line and continuation line
741 procedure Skip_Line is
744 Parse_EOL (Ali, Ptr);
745 exit when Ali (Ptr) /= '.';
749 -- Start of processing for Get_Symbol_Name
752 -- Look for the X lines corresponding to unit Eun
755 if Ali (Ptr) = 'X' then
757 Parse_Number (Ali, Ptr, E_Eun);
758 exit when E_Eun = Eun;
764 -- Here we are in the right Ali section, we now look for the entity
765 -- declared at position (Line, Col).
768 Parse_Number (Ali, Ptr, E_Line);
771 Parse_Number (Ali, Ptr, E_Col);
774 if Line = E_Line and then Col = E_Col then
775 Parse_Token (Ali, Ptr, E_Name);
776 return Ali (E_Name .. Ptr - 1);
782 -- We were not able to find the symbol, this should not happend but
783 -- since we don't want to stop here we return a string of three
784 -- question marks as the symbol name.
789 -- Start of processing for Parse_Identifier_Info
792 -- The identifier info looks like:
793 -- "38U9*Debug 12|36r6 36r19"
795 -- Extract the line, column and entity name information
797 Parse_Number (Ali, Ptr, E_Line);
799 if Ali (Ptr) > ' ' then
804 Parse_Number (Ali, Ptr, E_Col);
807 if Ali (Ptr) >= ' ' then
808 E_Global := (Ali (Ptr) = '*');
812 Parse_Token (Ali, Ptr, E_Name);
814 -- Exit if the symbol does not match
815 -- or if we have a local symbol and we do not want it
817 if (not Local_Symbols and not E_Global)
818 or else (Pattern.Initialized
819 and then not Match (Pattern, Ali (E_Name .. Ptr - 1)))
820 or else (E_Name >= Ptr)
822 -- Skip rest of this line and all continuation lines
825 Parse_EOL (Ali, Ptr);
826 exit when Ali (Ptr) /= '.';
831 -- Insert the declaration in the table
833 Decl_Ref := Add_Declaration
834 (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type);
837 or else Ali (Ptr) = '('
838 or else Ali (Ptr) = '{'
841 -- Here we have a type derivation information. The format is
842 -- <3|12I45> which means that the current entity is derived from the
843 -- type defined in unit number 3, line 12 column 45. The pipe and
844 -- unit number is optional. It is specified only if the parent type
845 -- is not defined in the current unit.
847 -- We could also have something like
849 -- that indicates that I derives from the predefined type integer.
853 if Ali (Ptr) in '0' .. '9' then
854 Parse_Derived_Info : declare
855 P_Line : Natural; -- parent entity line
856 P_Column : Natural; -- parent entity column
857 P_Type : Character; -- parent entity type
858 P_Eun : Positive; -- parent entity file number
861 Parse_Number (Ali, Ptr, P_Line);
863 -- If we have a pipe then the first number was the unit number
865 if Ali (Ptr) = '|' then
869 -- Now we have the line number
871 Parse_Number (Ali, Ptr, P_Line);
874 -- We don't have a unit number specified, so we set P_Eun to
877 for K in Dependencies_Tables.First .. Last (File.Dep) loop
879 exit when File.Dep.Table (K) = File_Ref;
883 -- Then parse the type and column number
887 Parse_Number (Ali, Ptr, P_Column);
889 -- Skip '>', or ')' or '>'
893 -- The derived info is needed only is the derived info mode is
894 -- on or if we want to output the type hierarchy
896 if Der_Info or else Type_Tree then
899 Get_Symbol_Name (P_Eun, P_Line, P_Column),
902 File.Dep.Table (P_Eun));
906 Search_Parent_Tree : declare
907 Pattern : Search_Pattern; -- Parent type pattern
908 File_Pos_Backup : Positive;
913 Get_Symbol_Name (P_Eun, P_Line, P_Column)
914 & ':' & Get_Gnatchop_File (File.Dep.Table (P_Eun))
915 & ':' & Get_Line (Get_Parent (Decl_Ref))
916 & ':' & Get_Column (Get_Parent (Decl_Ref)),
919 -- No default match is needed to look for the parent type
920 -- since we are using the fully qualified symbol name:
921 -- symbol:file:line:column
923 Set_Default_Match (False);
925 -- The parent type is defined in the same unit as the
926 -- derived type. So we want to revisit the unit.
928 File_Pos_Backup := File.Current_Line;
930 if File.Dep.Table (P_Eun) = File_Ref then
932 -- set file pointer at the start of the xref lines
934 File.Current_Line := File.Xref_Line;
936 Revisit_ALI_File : declare
937 File_Existed : Boolean;
938 File_Ref : File_Reference;
943 (Get_File (File.Dep.Table (P_Eun))),
947 Set_Unvisited (File_Ref);
948 end Revisit_ALI_File;
952 Local_Symbols, False, False, Der_Info, Type_Tree);
954 File.Current_Line := File_Pos_Backup;
955 end Search_Parent_Tree;
957 end Parse_Derived_Info;
960 while Ali (Ptr) /= '>'
961 and then Ali (Ptr) /= ')'
962 and then Ali (Ptr) /= '}'
969 elsif Ali (Ptr) = '=' then
971 P_Line, P_Column : Natural;
974 Parse_Number (Ali, Ptr, P_Line);
976 Parse_Number (Ali, Ptr, P_Column);
980 -- To find the body, we will have to parse the file too
984 File_Existed : Boolean;
985 File_Ref : File_Reference;
986 File_Name : constant String :=
987 Get_Gnatchop_File (File.X_File);
991 (ALI_File_Name (File_Name), File_Existed, File_Ref, False);
995 -- Parse references to this entity.
996 -- Ptr points to next reference with leading blanks
999 -- Process references on current line
1001 while Ali (Ptr) = ' ' or Ali (Ptr) = ASCII.HT loop
1003 -- For every reference read the line, type and column,
1004 -- optionally preceded by a file number and a pipe symbol.
1006 Parse_Number (Ali, Ptr, R_Line);
1008 if Ali (Ptr) = Pipe then
1010 File_Ref := File_Name (File, R_Line);
1012 Parse_Number (Ali, Ptr, R_Line);
1015 if Ali (Ptr) > ' ' then
1016 R_Type := Ali (Ptr);
1020 Parse_Number (Ali, Ptr, R_Col);
1022 -- Insert the reference or body in the table
1024 Add_Reference (Decl_Ref, File_Ref, R_Line, R_Col, R_Type);
1026 -- Skip generic information, if any
1028 if Ali (Ptr) = '[' then
1030 Num_Nested : Integer := 1;
1033 while Num_Nested /= 0 loop
1034 if Ali (Ptr) = ']' then
1035 Num_Nested := Num_Nested - 1;
1036 elsif Ali (Ptr) = '[' then
1037 Num_Nested := Num_Nested + 1;
1046 Parse_EOL (Ali, Ptr);
1048 -- Loop until new line is no continuation line
1050 exit when Ali (Ptr) /= '.';
1053 end Parse_Identifier_Info;
1059 procedure Parse_Number
1060 (Source : access String;
1061 Ptr : in out Positive;
1062 Number : out Natural)
1067 while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1072 while Source (Ptr) in '0' .. '9' loop
1073 Number := 10 * Number
1074 + (Character'Pos (Source (Ptr)) - Character'Pos ('0'));
1083 procedure Parse_Token
1084 (Source : access String;
1085 Ptr : in out Positive;
1086 Token_Ptr : out Positive)
1088 In_Quotes : Boolean := False;
1093 while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
1099 -- Find end-of-token
1101 while (In_Quotes or else
1102 not (Source (Ptr) = ' '
1103 or else Source (Ptr) = ASCII.HT
1104 or else Source (Ptr) = '<'
1105 or else Source (Ptr) = '{'
1106 or else Source (Ptr) = '='
1107 or else Source (Ptr) = '('))
1108 and then Source (Ptr) >= ' '
1110 if Source (Ptr) = '"' then
1111 In_Quotes := not In_Quotes;
1118 ----------------------
1119 -- Parse_X_Filename --
1120 ----------------------
1122 procedure Parse_X_Filename (File : in out ALI_File) is
1123 Ali : String_Access renames File.Buffer;
1124 Ptr : Positive renames File.Current_Line;
1128 while Ali (Ptr) = 'X' loop
1130 -- The current line is the start of a new Xref file section,
1131 -- whose format looks like:
1135 -- Skip the X and read the file number for the new X_File
1138 Parse_Number (Ali, Ptr, File_Nr);
1141 File.X_File := File.Dep.Table (File_Nr);
1144 Parse_EOL (Ali, Ptr);
1147 end Parse_X_Filename;
1149 --------------------
1150 -- Print_Gnatfind --
1151 --------------------
1153 procedure Print_Gnatfind
1154 (References : Boolean;
1155 Full_Path_Name : Boolean)
1157 Decl : Declaration_Reference := First_Declaration;
1163 Msg : String := " ");
1164 -- Print a reference, according to the extended tag of the output
1172 Msg : String := " ")
1174 Buffer : constant String :=
1175 Osint.To_Host_File_Spec
1176 (Get_Gnatchop_File (Ref, Full_Path_Name)).all
1177 & ":" & Get_Line (Ref)
1178 & ":" & Get_Column (Ref)
1180 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1183 Num_Blanks := Integer'Max (0, Num_Blanks);
1186 & String'(1 .. Num_Blanks => ' ')
1187 & Msg & " " & Get_Symbol (Decl));
1188 if Get_Source_Line (Ref)'Length /= 0 then
1189 Write_Line (" " & Get_Source_Line (Ref));
1193 -- Start of processing for Print_Gnatfind
1196 while Decl /= Empty_Declaration loop
1197 if Match (Decl) then
1199 -- Output the declaration
1202 Parent : constant Declaration_Reference := Get_Parent (Decl);
1203 Buffer : constant String :=
1204 Osint.To_Host_File_Spec
1205 (Get_Gnatchop_File (Decl, Full_Path_Name)).all
1206 & ":" & Get_Line (Decl)
1207 & ":" & Get_Column (Decl)
1209 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1212 Num_Blanks := Integer'Max (0, Num_Blanks);
1214 (Buffer & String'(1 .. Num_Blanks => ' ')
1215 & "(spec) " & Get_Symbol (Decl));
1217 if Parent /= Empty_Declaration then
1219 (Buffer & String'(1 .. Num_Blanks => ' ')
1220 & " derived from " & Get_Symbol (Parent)
1222 & Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent)).all
1223 & ':' & Get_Line (Parent)
1224 & ':' & Get_Column (Parent) & ')');
1228 if Get_Source_Line (Decl)'Length /= 0 then
1229 Write_Line (" " & Get_Source_Line (Decl));
1232 -- Output the body (sorted)
1234 Ref1 := First_Body (Decl);
1235 while Ref1 /= Empty_Reference loop
1236 Print_Ref (Ref1, "(body)");
1237 Ref1 := Next (Ref1);
1241 Ref1 := First_Modif (Decl);
1242 Ref2 := First_Reference (Decl);
1243 while Ref1 /= Empty_Reference
1244 or else Ref2 /= Empty_Reference
1246 if Compare (Ref1, Ref2) = LessThan then
1248 Ref1 := Next (Ref1);
1251 Ref2 := Next (Ref2);
1257 Decl := Next (Decl);
1265 procedure Print_Unused (Full_Path_Name : in Boolean) is
1266 Decl : Declaration_Reference := First_Declaration;
1270 while Decl /= Empty_Declaration loop
1271 if First_Modif (Decl) = Empty_Reference
1272 and then First_Reference (Decl) = Empty_Reference
1274 Write_Str (Get_Symbol (Decl)
1278 & Osint.To_Host_File_Spec
1279 (Get_Gnatchop_File (Decl, Full_Path_Name)).all
1283 & Get_Column (Decl));
1285 -- Print the body if any
1287 Ref := First_Body (Decl);
1289 if Ref /= Empty_Reference then
1291 & Osint.To_Host_File_Spec
1292 (Get_Gnatchop_File (Ref, Full_Path_Name)).all
1293 & ':' & Get_Line (Ref)
1294 & ':' & Get_Column (Ref));
1300 Decl := Next (Decl);
1308 procedure Print_Vi (Full_Path_Name : in Boolean) is
1309 Tab : constant Character := ASCII.HT;
1310 Decl : Declaration_Reference := First_Declaration;
1314 while Decl /= Empty_Declaration loop
1315 Write_Line (Get_Symbol (Decl) & Tab
1316 & Get_File (Decl, Full_Path_Name) & Tab
1319 -- Print the body if any
1321 Ref := First_Body (Decl);
1323 if Ref /= Empty_Reference then
1324 Write_Line (Get_Symbol (Decl) & Tab
1325 & Get_File (Ref, Full_Path_Name)
1330 -- Print the modifications
1332 Ref := First_Modif (Decl);
1334 while Ref /= Empty_Reference loop
1335 Write_Line (Get_Symbol (Decl) & Tab
1336 & Get_File (Ref, Full_Path_Name)
1342 Decl := Next (Decl);
1350 procedure Print_Xref (Full_Path_Name : in Boolean) is
1351 Decl : Declaration_Reference := First_Declaration;
1353 File : File_Reference;
1355 Margin : constant := 10;
1356 -- Column where file names start
1358 procedure New_Line80;
1359 -- Go to start of new line
1361 procedure Print80 (S : in String);
1362 -- Print the text, respecting the 80 columns rule.
1364 procedure Print_Ref (Line, Column : String);
1365 -- The beginning of the output is aligned on a column multiple of 9
1371 procedure New_Line80 is
1374 Write_Str (String'(1 .. Margin - 1 => ' '));
1381 procedure Print80 (S : in String) is
1382 Align : Natural := Margin - (Integer (Column) mod Margin);
1384 if Align = Margin then
1388 Write_Str (String'(1 .. Align => ' ') & S);
1395 procedure Print_Ref (Line, Column : String) is
1396 Line_Align : constant Integer := 4 - Line'Length;
1398 S : constant String := String'(1 .. Line_Align => ' ')
1399 & Line & ':' & Column;
1401 Align : Natural := Margin - (Integer (Output.Column) mod Margin);
1404 if Align = Margin then
1408 if Integer (Output.Column) + Align + S'Length > 79 then
1413 Write_Str (String'(1 .. Align => ' ') & S);
1416 -- Start of processing for Print_Xref
1419 while Decl /= Empty_Declaration loop
1420 Write_Str (Get_Symbol (Decl));
1422 while Column < Type_Position loop
1426 Write_Line (Get_Full_Type (Get_Type (Decl)));
1428 Write_Parent_Info : declare
1429 Parent : constant Declaration_Reference := Get_Parent (Decl);
1431 if Parent /= Empty_Declaration then
1432 Write_Str (" Ptype: ");
1434 (Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent)).all);
1435 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1436 Print80 (" " & Get_Symbol (Parent));
1439 end Write_Parent_Info;
1441 Write_Str (" Decl: ");
1443 (Osint.To_Host_File_Spec
1444 (Get_Gnatchop_File (Decl, Full_Path_Name)).all & ' ');
1445 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1447 -- Print the body if any
1449 Ref := First_Body (Decl);
1451 if Ref /= Empty_Reference then
1453 Write_Str (" Body: ");
1455 (Osint.To_Host_File_Spec
1456 (Get_Gnatchop_File (Ref, Full_Path_Name)).all & ' ');
1457 Print_Ref (Get_Line (Ref), Get_Column (Ref));
1460 -- Print the modifications if any
1462 Ref := First_Modif (Decl);
1464 if Ref /= Empty_Reference then
1466 Write_Str (" Modi: ");
1471 while Ref /= Empty_Reference loop
1472 if Get_File_Ref (Ref) /= File then
1473 if File /= Empty_File then
1477 File := Get_File_Ref (Ref);
1479 (Get_Gnatchop_File (Ref, Full_Path_Name) & ' ');
1480 Print_Ref (Get_Line (Ref), Get_Column (Ref));
1483 Print_Ref (Get_Line (Ref), Get_Column (Ref));
1489 -- Print the references
1491 Ref := First_Reference (Decl);
1493 if Ref /= Empty_Reference then
1495 Write_Str (" Ref: ");
1500 while Ref /= Empty_Reference loop
1501 if Get_File_Ref (Ref) /= File then
1502 if File /= Empty_File then
1506 File := Get_File_Ref (Ref);
1508 (Osint.To_Host_File_Spec
1509 (Get_Gnatchop_File (Ref, Full_Path_Name)).all & ' ');
1510 Print_Ref (Get_Line (Ref), Get_Column (Ref));
1513 Print_Ref (Get_Line (Ref), Get_Column (Ref));
1520 Decl := Next (Decl);
1529 (FD : File_Descriptor;
1530 Contents : out String_Access;
1531 Success : out Boolean)
1533 Length : constant File_Offset := File_Offset (File_Length (FD));
1534 -- Include room for EOF char
1536 Buffer : String (1 .. Length + 1);
1538 This_Read : Integer;
1539 Read_Ptr : File_Offset := 1;
1544 This_Read := Read (FD,
1545 A => Buffer (Read_Ptr)'Address,
1546 N => Length + 1 - Read_Ptr);
1547 Read_Ptr := Read_Ptr + Integer'Max (This_Read, 0);
1548 exit when This_Read <= 0;
1551 Buffer (Read_Ptr) := EOF;
1552 Contents := new String'(Buffer (1 .. Read_Ptr));
1554 -- Things aren't simple on VMS due to the plethora of file types
1555 -- and organizations. It seems clear that there shouldn't be more
1556 -- bytes read than are contained in the file though.
1558 if Hostparm.OpenVMS then
1559 Success := Read_Ptr <= Length + 1;
1561 Success := Read_Ptr = Length + 1;
1570 (Pattern : Search_Pattern;
1571 Local_Symbols : Boolean;
1572 Wide_Search : Boolean;
1573 Read_Only : Boolean;
1575 Type_Tree : Boolean)
1577 type String_Access is access String;
1578 procedure Free is new Unchecked_Deallocation (String, String_Access);
1581 File_Ref : File_Reference;
1582 Strip_Num : Natural := 0;
1583 Ali_Name : String_Access;
1586 -- If we want all the .ali files, then find them
1593 -- Get the next unread ali file
1595 File_Ref := Next_Unvisited_File;
1597 exit when File_Ref = Empty_File;
1599 -- Find the ALI file to use. Most of the time, it will be the unit
1600 -- name, with a different extension. However, when dealing with
1601 -- separates the ALI file is in fact the parent's ALI file (and this
1602 -- is recursive, in case the parent itself is a separate).
1607 Ali_Name := new String'
1608 (Get_File (File_Ref, With_Dir => True, Strip => Strip_Num));
1610 -- Striped too many things...
1611 if Ali_Name.all = "" then
1612 if Get_Emit_Warning (File_Ref) then
1615 ("warning : file " & Get_File (File_Ref, With_Dir => True)
1617 Set_Standard_Output;
1622 -- If not found, try the parent's ALI file (this is needed for
1623 -- separate units and subprograms).
1624 elsif not File_Exists (Ali_Name.all) then
1625 Strip_Num := Strip_Num + 1;
1627 -- Else we finally found it
1633 -- Now that we have a file name, parse it to find any reference to
1637 and then (Read_Only or else Is_Writable_File (Ali_Name.all))
1640 Open (Ali_Name.all, ALIfile);
1641 while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1642 Parse_X_Filename (ALIfile);
1643 Parse_Identifier_Info (Pattern, ALIfile, Local_Symbols,
1644 Der_Info, Type_Tree, Wide_Search);
1648 when No_Xref_Information =>
1649 if Get_Emit_Warning (File_Ref) then
1652 ("warning : No cross-referencing information in "
1654 Set_Standard_Output;
1667 procedure Search_Xref
1668 (Local_Symbols : Boolean;
1669 Read_Only : Boolean;
1673 File_Ref : File_Reference;
1674 Null_Pattern : Search_Pattern;
1677 -- Find the next unvisited file
1679 File_Ref := Next_Unvisited_File;
1680 exit when File_Ref = Empty_File;
1682 -- Search the object directories for the .ali file
1685 or else Is_Writable_File (Get_File (File_Ref, With_Dir => True))
1688 Open (Get_File (File_Ref, With_Dir => True), ALIfile, True);
1690 while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
1691 Parse_X_Filename (ALIfile);
1692 Parse_Identifier_Info
1693 (Null_Pattern, ALIfile, Local_Symbols, Der_Info);
1697 when No_Xref_Information => null;