1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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
: 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
;
147 -- Find the end of the first item in Entity (pattern or file?)
148 -- If there is no ':', we only have a pattern
150 File_Start
:= Index
(Entity
, ":");
152 -- If the regular expression is invalid, just consider it as a string
154 if File_Start
= 0 then
156 Pattern
.Entity
:= Compile
(Entity
, Glob
, False);
157 Pattern
.Initialized
:= True;
160 when Error_In_Regexp
=>
162 -- The basic idea is to insert a \ before every character
165 Tmp_Regexp
: String (1 .. 2 * Entity
'Length);
166 Index
: Positive := 1;
169 for J
in Entity
'Range loop
170 Tmp_Regexp
(Index
) := '\';
171 Tmp_Regexp
(Index
+ 1) := Entity
(J
);
175 Pattern
.Entity
:= Compile
(Tmp_Regexp
, True, False);
176 Pattern
.Initialized
:= True;
180 Set_Default_Match
(True);
184 -- If there is a dot in the pattern, then it is a file name
187 Index
(Entity
(Entity
'First .. File_Start
- 1), ".") /= 0)
190 and then Index
(Entity
(Entity
'First .. File_Start
- 1),
193 Pattern
.Entity
:= Compile
(".*", False);
194 Pattern
.Initialized
:= True;
195 File_Start
:= Entity
'First;
198 -- If the regular expression is invalid, just consider it as a string
202 Compile
(Entity
(Entity
'First .. File_Start
- 1), Glob
, False);
203 Pattern
.Initialized
:= True;
206 when Error_In_Regexp
=>
208 -- The basic idea is to insert a \ before every character
211 Tmp_Regexp
: String (1 .. 2 * (File_Start
- Entity
'First));
212 Index
: Positive := 1;
215 for J
in Entity
'First .. File_Start
- 1 loop
216 Tmp_Regexp
(Index
) := '\';
217 Tmp_Regexp
(Index
+ 1) := Entity
(J
);
221 Pattern
.Entity
:= Compile
(Tmp_Regexp
, True, False);
222 Pattern
.Initialized
:= True;
226 File_Start
:= File_Start
+ 1;
229 -- Parse the file name
231 Line_Start
:= Index
(Entity
(File_Start
.. Entity
'Last), ":");
233 -- Check if it was a disk:\directory item (for NT and OS/2)
235 if File_Start
= Line_Start
- 1
236 and then Line_Start
< Entity
'Last
237 and then Entity
(Line_Start
+ 1) = '\'
239 Line_Start
:= Index
(Entity
(Line_Start
+ 1 .. Entity
'Last), ":");
242 if Line_Start
= 0 then
243 Line_Start
:= Entity
'Length + 1;
245 elsif Line_Start
/= Entity
'Last then
246 Col_Start
:= Index
(Entity
(Line_Start
+ 1 .. Entity
'Last), ":");
248 if Col_Start
= 0 then
249 Col_Start
:= Entity
'Last + 1;
252 if Col_Start
> Line_Start
+ 1 then
254 Line_Num
:= Natural'Value
255 (Entity
(Line_Start
+ 1 .. Col_Start
- 1));
258 when Constraint_Error
=>
259 raise Invalid_Argument
;
263 if Col_Start
< Entity
'Last then
265 Col_Num
:= Natural'Value (Entity
266 (Col_Start
+ 1 .. Entity
'Last));
269 when Constraint_Error
=> raise Invalid_Argument
;
276 (Entity
(File_Start
.. Line_Start
- 1), Visited
=> True);
277 Pattern
.File_Ref
:= File_Ref
;
278 Add_Line
(Pattern
.File_Ref
, Line_Num
, Col_Num
);
281 (ALI_File_Name
(Entity
(File_Start
.. Line_Start
- 1)),
283 Emit_Warning
=> True);
290 procedure Add_Xref_File
(File
: String) is
291 File_Ref
: File_Reference
:= Empty_File
;
292 pragma Unreferenced
(File_Ref
);
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
),
319 Visited
=> False, Emit_Warning
=> True);
321 end Add_Xref_File_Internal
;
323 -- Start of processing for Add_Xref_File
326 -- Check if we need to do the expansion
328 if Ada
.Strings
.Fixed
.Index
(File
, "*") /= 0
329 or else Ada
.Strings
.Fixed
.Index
(File
, "?") /= 0
331 Start_Expansion
(Iterator
, File
);
335 S
: constant String := Expansion
(Iterator
);
338 exit when S
'Length = 0;
339 Add_Xref_File_Internal
(S
);
344 Add_Xref_File_Internal
(File
);
348 -----------------------
349 -- Current_Xref_File --
350 -----------------------
352 function Current_Xref_File
(File
: ALI_File
) return File_Reference
is
355 end Current_Xref_File
;
357 --------------------------
358 -- Default_Project_File --
359 --------------------------
361 function Default_Project_File
(Dir_Name
: String) return String is
363 Dir_Ent
: File_Name_String
;
367 Open
(My_Dir
, Dir_Name
);
370 Read
(My_Dir
, Dir_Ent
, Last
);
373 if Tail
(Dir_Ent
(1 .. Last
), 4) = ".adp" then
375 -- The first project file found is the good one
378 return Dir_Ent
(1 .. Last
);
383 return String'(1 .. 0 => ' ');
386 when Directory_Error => return String'(1 .. 0 => ' ');
387 end Default_Project_File
;
395 Num
: Positive) return File_Reference
398 return File
.Dep
.Table
(Num
);
405 procedure Find_ALI_Files
is
407 Dir_Ent
: File_Name_String
;
410 File_Ref
: File_Reference
;
411 pragma Unreferenced
(File_Ref
);
413 function Open_Next_Dir
return Boolean;
414 -- Tries to open the next object directory, and return False if
415 -- the directory cannot be opened.
421 function Open_Next_Dir
return Boolean is
423 -- Until we are able to open a new directory
427 Obj_Dir
: constant String := Next_Obj_Dir
;
430 -- Case of no more Obj_Dir lines
432 if Obj_Dir
'Length = 0 then
436 Open
(My_Dir
.Dir
, Obj_Dir
);
441 -- Could not open the directory
443 when Directory_Error
=> null;
450 -- Start of processing for Find_ALI_Files
455 if Open_Next_Dir
then
457 Read
(My_Dir
.Dir
, Dir_Ent
, Last
);
462 if not Open_Next_Dir
then
466 elsif Last
> 4 and then Dir_Ent
(Last
- 3 .. Last
) = ".ali" then
468 Add_To_Xref_File
(Dir_Ent
(1 .. Last
), Visited
=> False);
478 function Get_Full_Type
(Decl
: Declaration_Reference
) return String is
480 function Param_String
return String;
481 -- Return the string to display depending on whether Decl is a
488 function Param_String
return String is
490 if Is_Parameter
(Decl
) then
497 -- Start of processing for Get_Full_Type
500 case Get_Type
(Decl
) is
501 when 'A' => return "array type";
502 when 'B' => return "boolean type";
503 when 'C' => return "class-wide type";
504 when 'D' => return "decimal type";
505 when 'E' => return "enumeration type";
506 when 'F' => return "float type";
507 when 'I' => return "integer type";
508 when 'M' => return "modular type";
509 when 'O' => return "fixed type";
510 when 'P' => return "access type";
511 when 'R' => return "record type";
512 when 'S' => return "string type";
513 when 'T' => return "task type";
514 when 'W' => return "protected type";
516 when 'a' => return "array type";
517 when 'b' => return Param_String
& "boolean object";
518 when 'c' => return Param_String
& "class-wide object";
519 when 'd' => return Param_String
& "decimal object";
520 when 'e' => return Param_String
& "enumeration object";
521 when 'f' => return Param_String
& "float object";
522 when 'i' => return Param_String
& "integer object";
523 when 'm' => return Param_String
& "modular object";
524 when 'o' => return Param_String
& "fixed object";
525 when 'p' => return Param_String
& "access object";
526 when 'r' => return Param_String
& "record object";
527 when 's' => return Param_String
& "string object";
528 when 't' => return Param_String
& "task object";
529 when 'w' => return Param_String
& "protected object";
530 when 'x' => return Param_String
& "abstract procedure";
531 when 'y' => return Param_String
& "abstract function";
533 when 'K' => return "package";
534 when 'k' => return "generic package";
535 when 'L' => return "statement label";
536 when 'l' => return "loop label";
537 when 'N' => return "named number";
538 when 'n' => return "enumeration literal";
539 when 'q' => return "block label";
540 when 'U' => return "procedure";
541 when 'u' => return "generic procedure";
542 when 'V' => return "function";
543 when 'v' => return "generic function";
544 when 'X' => return "exception";
545 when 'Y' => return "entry";
547 when '+' => return "private type";
549 -- The above should be the only possibilities, but for this kind
550 -- of informational output, we don't want to bomb if we find
551 -- something else, so just return three question marks when we
552 -- have an unknown Abbrev value
555 return "??? (" & Get_Type
(Decl
) & ")";
559 --------------------------
560 -- Skip_To_First_X_Line --
561 --------------------------
563 procedure Skip_To_First_X_Line
564 (File
: in out ALI_File
;
568 Ali
: String_Access
renames File
.Buffer
;
570 Ptr
: Positive := Ali
'First;
571 Num_Dependencies
: Natural := 0;
572 File_Start
: Positive;
574 Gnatchop_Offset
: Integer;
575 Gnatchop_Name
: Positive;
577 File_Ref
: File_Reference
;
578 pragma Unreferenced
(File_Ref
);
581 -- Read all the lines possibly processing with-clauses and dependency
582 -- information and exit on finding the first Xref line.
583 -- A fall-through of the loop means that there is no xref information
584 -- which is an error condition.
586 while Ali
(Ptr
) /= EOF
loop
587 if D_Lines
and then Ali
(Ptr
) = 'D' then
589 -- Found dependency information. Format looks like:
590 -- D src-nam time-stmp checksum [subunit-name] [line:file-name]
592 -- Skip the D and parse the filenam
595 Parse_Token
(Ali
, Ptr
, Token
);
599 Num_Dependencies
:= Num_Dependencies
+ 1;
600 Set_Last
(File
.Dep
, Num_Dependencies
);
602 Parse_Token
(Ali
, Ptr
, Token
); -- Skip time-stamp
603 Parse_Token
(Ali
, Ptr
, Token
); -- Skip checksum
604 Parse_Token
(Ali
, Ptr
, Token
); -- Read next entity on the line
606 if not (Ali
(Token
) in '0' .. '9') then
607 Parse_Token
(Ali
, Ptr
, Token
); -- Was a subunit name
610 -- Did we have a gnatchop-ed file with a pragma Source_Reference ?
612 Gnatchop_Offset
:= 0;
614 if Ali
(Token
) in '0' .. '9' then
615 Gnatchop_Name
:= Token
;
616 while Ali
(Gnatchop_Name
) /= ':' loop
617 Gnatchop_Name
:= Gnatchop_Name
+ 1;
621 2 - Natural'Value (Ali
(Token
.. Gnatchop_Name
- 1));
622 Token
:= Gnatchop_Name
+ 1;
625 File
.Dep
.Table
(Num_Dependencies
) := Add_To_Xref_File
626 (Ali
(File_Start
.. File_End
),
627 Gnatchop_File
=> Ali
(Token
.. Ptr
- 1),
628 Gnatchop_Offset
=> Gnatchop_Offset
);
630 elsif W_Lines
and then Ali
(Ptr
) = 'W' then
632 -- Found with-clause information. Format looks like:
633 -- "W debug%s debug.adb debug.ali"
635 -- Skip the W and parse the .ali filename (3rd token)
637 Parse_Token
(Ali
, Ptr
, Token
);
638 Parse_Token
(Ali
, Ptr
, Token
);
639 Parse_Token
(Ali
, Ptr
, Token
);
642 Add_To_Xref_File
(Ali
(Token
.. Ptr
- 1), Visited
=> False);
644 elsif Ali
(Ptr
) = 'X' then
646 -- Found a cross-referencing line - stop processing
648 File
.Current_Line
:= Ptr
;
649 File
.Xref_Line
:= Ptr
;
653 Parse_EOL
(Ali
, Ptr
);
656 raise No_Xref_Information
;
657 end Skip_To_First_X_Line
;
666 Dependencies
: Boolean := False)
668 Ali
: String_Access
renames File
.Buffer
;
671 if File
.Buffer
/= null then
678 Read_File
(Name
, Ali
);
681 when Ada
.Text_IO
.Name_Error | Ada
.Text_IO
.End_Error
=>
682 raise No_Xref_Information
;
685 Skip_To_First_X_Line
(File
, D_Lines
=> True, W_Lines
=> Dependencies
);
693 (Source
: access String;
694 Ptr
: in out Positive;
695 Skip_Continuation_Line
: Boolean := False)
699 -- Skip to end of line
701 while Source
(Ptr
) /= ASCII
.CR
and then Source
(Ptr
) /= ASCII
.LF
702 and then Source
(Ptr
) /= EOF
707 if Source
(Ptr
) /= EOF
then
708 Ptr
:= Ptr
+ 1; -- skip CR or LF
711 -- Skip past CR/LF or LF/CR combination
713 if (Source
(Ptr
) = ASCII
.CR
or else Source
(Ptr
) = ASCII
.LF
)
714 and then Source
(Ptr
) /= Source
(Ptr
- 1)
719 exit when not Skip_Continuation_Line
or else Source
(Ptr
) /= '.';
723 ---------------------------
724 -- Parse_Identifier_Info --
725 ---------------------------
727 procedure Parse_Identifier_Info
728 (Pattern
: Search_Pattern
;
729 File
: in out ALI_File
;
730 Local_Symbols
: Boolean;
731 Der_Info
: Boolean := False;
732 Type_Tree
: Boolean := False;
733 Wide_Search
: Boolean := True;
734 Labels_As_Ref
: Boolean := True)
736 Ptr
: Positive renames File
.Current_Line
;
737 Ali
: String_Access
renames File
.Buffer
;
739 E_Line
: Natural; -- Line number of current entity
740 E_Col
: Natural; -- Column number of current entity
741 E_Type
: Character; -- Type of current entity
742 E_Name
: Positive; -- Pointer to begin of entity name
743 E_Global
: Boolean; -- True iff entity is global
745 R_Line
: Natural; -- Line number of current reference
746 R_Col
: Natural; -- Column number of current reference
747 R_Type
: Character; -- Type of current reference
749 Decl_Ref
: Declaration_Reference
;
750 File_Ref
: File_Reference
:= Current_Xref_File
(File
);
752 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String;
753 -- Returns the symbol name for the entity defined at the specified
754 -- line and column in the dependent unit number Eun. For this we need
755 -- to parse the ali file again because the parent entity is not in
756 -- the declaration table if it did not match the search pattern.
758 procedure Skip_To_Matching_Closing_Bracket
;
759 -- When Ptr points to an opening square bracket, moves it to the
760 -- character following the matching closing bracket
762 ---------------------
763 -- Get_Symbol_Name --
764 ---------------------
766 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String is
768 E_Eun
: Positive; -- Unit number of current entity
769 E_Line
: Natural; -- Line number of current entity
770 E_Col
: Natural; -- Column number of current entity
771 E_Name
: Positive; -- Pointer to begin of entity name
774 -- Look for the X lines corresponding to unit Eun
777 if Ali
(Ptr
) = 'X' then
779 Parse_Number
(Ali
, Ptr
, E_Eun
);
780 exit when E_Eun
= Eun
;
783 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
786 -- Here we are in the right Ali section, we now look for the entity
787 -- declared at position (Line, Col).
790 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 --------------------------------------
814 -- Skip_To_Matching_Closing_Bracket --
815 --------------------------------------
817 procedure Skip_To_Matching_Closing_Bracket
is
818 Num_Brackets
: Natural;
822 while Num_Brackets
/= 0 loop
824 if Ali
(Ptr
) = '[' then
825 Num_Brackets
:= Num_Brackets
+ 1;
826 elsif Ali
(Ptr
) = ']' then
827 Num_Brackets
:= Num_Brackets
- 1;
832 end Skip_To_Matching_Closing_Bracket
;
834 -- Start of processing for Parse_Identifier_Info
837 -- The identifier info looks like:
838 -- "38U9*Debug 12|36r6 36r19"
840 -- Extract the line, column and entity name information
842 Parse_Number
(Ali
, Ptr
, E_Line
);
844 if Ali
(Ptr
) > ' ' then
849 -- Ignore some of the entities (labels,...)
852 when 'l' |
'L' |
'q' =>
853 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
860 Parse_Number
(Ali
, Ptr
, E_Col
);
863 if Ali
(Ptr
) >= ' ' then
864 E_Global
:= (Ali
(Ptr
) = '*');
868 Parse_Token
(Ali
, Ptr
, E_Name
);
870 -- Exit if the symbol does not match
871 -- or if we have a local symbol and we do not want it
873 if (not Local_Symbols
and not E_Global
)
874 or else (Pattern
.Initialized
875 and then not Match
(Ali
(E_Name
.. Ptr
- 1), Pattern
.Entity
))
876 or else (E_Name
>= Ptr
)
878 Decl_Ref
:= Add_Declaration
879 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
,
880 Remove_Only
=> True);
881 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
885 -- Insert the declaration in the table
887 Decl_Ref
:= Add_Declaration
888 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
);
890 if Ali
(Ptr
) = '[' then
891 Skip_To_Matching_Closing_Bracket
;
895 or else Ali
(Ptr
) = '('
896 or else Ali
(Ptr
) = '{'
898 -- Here we have a type derivation information. The format is
899 -- <3|12I45> which means that the current entity is derived from the
900 -- type defined in unit number 3, line 12 column 45. The pipe and
901 -- unit number is optional. It is specified only if the parent type
902 -- is not defined in the current unit.
904 -- We also have the format for generic instantiations, as in
905 -- 7a5*Uid(3|5I8[4|2]) 2|4r74
907 -- We could also have something like
909 -- that indicates that I derives from the predefined type integer.
913 if Ali
(Ptr
) in '0' .. '9' then
914 Parse_Derived_Info
: declare
915 P_Line
: Natural; -- parent entity line
916 P_Column
: Natural; -- parent entity column
917 P_Eun
: Positive; -- parent entity file number
920 Parse_Number
(Ali
, Ptr
, P_Line
);
922 -- If we have a pipe then the first number was the unit number
924 if Ali
(Ptr
) = '|' then
928 -- Now we have the line number
930 Parse_Number
(Ali
, Ptr
, P_Line
);
933 -- We don't have a unit number specified, so we set P_Eun to
936 for K
in Dependencies_Tables
.First
.. Last
(File
.Dep
) loop
938 exit when File
.Dep
.Table
(K
) = File_Ref
;
942 -- Then parse the type and column number
945 Parse_Number
(Ali
, Ptr
, P_Column
);
947 -- Skip the information for generics instantiations
949 if Ali
(Ptr
) = '[' then
950 Skip_To_Matching_Closing_Bracket
;
953 -- Skip '>', or ')' or '>'
957 -- The derived info is needed only is the derived info mode is
958 -- on or if we want to output the type hierarchy
960 if Der_Info
or else Type_Tree
then
962 Symbol
: constant String :=
963 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
);
965 if Symbol
/= "???" then
971 File
.Dep
.Table
(P_Eun
));
977 and then (Pattern
.File_Ref
= Empty_File
979 Pattern
.File_Ref
= Current_Xref_File
(File
))
981 Search_Parent_Tree
: declare
982 Pattern
: Search_Pattern
; -- Parent type pattern
983 File_Pos_Backup
: Positive;
988 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
)
989 & ':' & Get_Gnatchop_File
(File
.Dep
.Table
(P_Eun
))
990 & ':' & Get_Line
(Get_Parent
(Decl_Ref
))
991 & ':' & Get_Column
(Get_Parent
(Decl_Ref
)),
994 -- No default match is needed to look for the parent type
995 -- since we are using the fully qualified symbol name:
996 -- symbol:file:line:column
998 Set_Default_Match
(False);
1000 -- The parent hierarchy is defined in the same unit as
1001 -- the derived type. So we want to revisit the unit.
1003 File_Pos_Backup
:= File
.Current_Line
;
1005 Skip_To_First_X_Line
1006 (File
, D_Lines
=> False, W_Lines
=> False);
1008 while File
.Buffer
(File
.Current_Line
) /= EOF
loop
1009 Parse_X_Filename
(File
);
1010 Parse_Identifier_Info
1011 (Pattern
=> Pattern
,
1013 Local_Symbols
=> False,
1014 Der_Info
=> Der_Info
,
1016 Wide_Search
=> False,
1017 Labels_As_Ref
=> Labels_As_Ref
);
1020 File
.Current_Line
:= File_Pos_Backup
;
1021 end Search_Parent_Tree
;
1023 end Parse_Derived_Info
;
1026 while Ali
(Ptr
) /= '>'
1027 and then Ali
(Ptr
) /= ')'
1028 and then Ali
(Ptr
) /= '}'
1035 elsif Ali
(Ptr
) = '=' then
1037 P_Line
, P_Column
: Natural;
1041 Parse_Number
(Ali
, Ptr
, P_Line
);
1043 Parse_Number
(Ali
, Ptr
, P_Column
);
1047 -- To find the body, we will have to parse the file too
1051 File_Ref
: File_Reference
;
1052 pragma Unreferenced
(File_Ref
);
1053 File_Name
: constant String := Get_Gnatchop_File
(File
.X_File
);
1055 File_Ref
:= Add_To_Xref_File
(ALI_File_Name
(File_Name
), False);
1059 -- Parse references to this entity.
1060 -- Ptr points to next reference with leading blanks
1063 -- Process references on current line
1065 while Ali
(Ptr
) = ' ' or Ali
(Ptr
) = ASCII
.HT
loop
1067 -- For every reference read the line, type and column,
1068 -- optionally preceded by a file number and a pipe symbol.
1070 Parse_Number
(Ali
, Ptr
, R_Line
);
1072 if Ali
(Ptr
) = Pipe
then
1074 File_Ref
:= File_Name
(File
, R_Line
);
1076 Parse_Number
(Ali
, Ptr
, R_Line
);
1079 if Ali
(Ptr
) > ' ' then
1080 R_Type
:= Ali
(Ptr
);
1084 -- Imported entities might special indication as to their external
1086 -- 5U14*Foo2 5>20 6b<c,myfoo2>22
1089 and then Ali
(Ptr
) = '<'
1091 while Ptr
<= Ali
'Last
1092 and then Ali
(Ptr
) /= '>'
1099 Parse_Number
(Ali
, Ptr
, R_Col
);
1101 -- Insert the reference or body in the table
1104 (Decl_Ref
, File_Ref
, R_Line
, R_Col
, R_Type
, Labels_As_Ref
);
1106 -- Skip generic information, if any
1108 if Ali
(Ptr
) = '[' then
1110 Num_Nested
: Integer := 1;
1114 while Num_Nested
/= 0 loop
1115 if Ali
(Ptr
) = ']' then
1116 Num_Nested
:= Num_Nested
- 1;
1117 elsif Ali
(Ptr
) = '[' then
1118 Num_Nested
:= Num_Nested
+ 1;
1128 Parse_EOL
(Ali
, Ptr
);
1130 -- Loop until new line is no continuation line
1132 exit when Ali
(Ptr
) /= '.';
1135 end Parse_Identifier_Info
;
1141 procedure Parse_Number
1142 (Source
: access String;
1143 Ptr
: in out Positive;
1144 Number
: out Natural)
1149 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1154 while Source
(Ptr
) in '0' .. '9' loop
1156 10 * Number
+ (Character'Pos (Source
(Ptr
)) - Character'Pos ('0'));
1165 procedure Parse_Token
1166 (Source
: access String;
1167 Ptr
: in out Positive;
1168 Token_Ptr
: out Positive)
1170 In_Quotes
: Character := ASCII
.NUL
;
1175 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1181 -- Find end-of-token
1183 while (In_Quotes
/= ASCII
.NUL
or else
1184 not (Source
(Ptr
) = ' '
1185 or else Source
(Ptr
) = ASCII
.HT
1186 or else Source
(Ptr
) = '<'
1187 or else Source
(Ptr
) = '{'
1188 or else Source
(Ptr
) = '['
1189 or else Source
(Ptr
) = '='
1190 or else Source
(Ptr
) = '('))
1191 and then Source
(Ptr
) >= ' '
1193 -- Double-quotes are used for operators
1194 -- Simple-quotes are used for character constants, for instance when
1195 -- they are found in an enumeration type "type A is ('+', '-');"
1197 case Source
(Ptr
) is
1199 if In_Quotes
= Source
(Ptr
) then
1200 In_Quotes
:= ASCII
.NUL
;
1201 elsif In_Quotes
= ASCII
.NUL
then
1202 In_Quotes
:= Source
(Ptr
);
1213 ----------------------
1214 -- Parse_X_Filename --
1215 ----------------------
1217 procedure Parse_X_Filename
(File
: in out ALI_File
) is
1218 Ali
: String_Access
renames File
.Buffer
;
1219 Ptr
: Positive renames File
.Current_Line
;
1223 while Ali
(Ptr
) = 'X' loop
1225 -- The current line is the start of a new Xref file section,
1226 -- whose format looks like:
1230 -- Skip the X and read the file number for the new X_File
1233 Parse_Number
(Ali
, Ptr
, File_Nr
);
1236 File
.X_File
:= File
.Dep
.Table
(File_Nr
);
1239 Parse_EOL
(Ali
, Ptr
);
1241 end Parse_X_Filename
;
1243 --------------------
1244 -- Print_Gnatfind --
1245 --------------------
1247 procedure Print_Gnatfind
1248 (References
: Boolean;
1249 Full_Path_Name
: Boolean)
1251 Decls
: constant Declaration_Array_Access
:= Get_Declarations
;
1252 Decl
: Declaration_Reference
;
1253 Arr
: Reference_Array_Access
;
1257 Msg
: String := " ");
1258 -- Print a reference, according to the extended tag of the output
1266 Msg
: String := " ")
1268 F
: String_Access
:=
1269 Osint
.To_Host_File_Spec
1270 (Get_Gnatchop_File
(Ref
, Full_Path_Name
));
1272 Buffer
: constant String :=
1274 ":" & Get_Line
(Ref
) &
1275 ":" & Get_Column
(Ref
) &
1278 Num_Blanks
: Integer := Longest_File_Name
+ 10 - Buffer
'Length;
1282 Num_Blanks
:= Integer'Max (0, Num_Blanks
);
1285 & String'(1 .. Num_Blanks => ' ')
1286 & Msg & " " & Get_Symbol (Decl));
1288 if Get_Source_Line (Ref)'Length /= 0 then
1289 Write_Line (" " & Get_Source_Line (Ref));
1293 -- Start of processing for Print_Gnatfind
1296 for D in Decls'Range loop
1299 if Match (Decl) then
1301 -- Output the declaration
1304 Parent : constant Declaration_Reference := Get_Parent (Decl);
1306 F : String_Access :=
1307 Osint.To_Host_File_Spec
1308 (Get_Gnatchop_File (Decl, Full_Path_Name));
1310 Buffer : constant String :=
1312 ":" & Get_Line (Decl) &
1313 ":" & Get_Column (Decl) &
1316 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1320 Num_Blanks := Integer'Max (0, Num_Blanks);
1322 (Buffer & String'(1 .. Num_Blanks
=> ' ')
1323 & "(spec) " & Get_Symbol
(Decl
));
1325 if Parent
/= Empty_Declaration
then
1326 F
:= Osint
.To_Host_File_Spec
(Get_Gnatchop_File
(Parent
));
1328 (Buffer
& String'(1 .. Num_Blanks => ' ')
1329 & " derived from " & Get_Symbol (Parent)
1332 & ':' & Get_Line (Parent)
1333 & ':' & Get_Column (Parent) & ')');
1338 if Get_Source_Line (Decl)'Length /= 0 then
1339 Write_Line (" " & Get_Source_Line (Decl));
1342 -- Output the body (sorted)
1344 Arr := Get_References (Decl, Get_Bodies => True);
1346 for R in Arr'Range loop
1347 Print_Ref (Arr (R), "(body)");
1353 Arr := Get_References
1354 (Decl, Get_Writes => True, Get_Reads => True);
1356 for R in Arr'Range loop
1357 Print_Ref (Arr (R));
1370 procedure Print_Unused (Full_Path_Name : Boolean) is
1371 Decls : constant Declaration_Array_Access := Get_Declarations;
1372 Decl : Declaration_Reference;
1373 Arr : Reference_Array_Access;
1377 for D in Decls'Range loop
1381 (Decl, Get_Reads => True, Get_Writes => True) = 0
1383 F := Osint.To_Host_File_Spec
1384 (Get_Gnatchop_File (Decl, Full_Path_Name));
1385 Write_Str (Get_Symbol (Decl)
1387 & Get_Full_Type (Decl)
1393 & Get_Column (Decl));
1396 -- Print the body if any
1398 Arr := Get_References (Decl, Get_Bodies => True);
1400 for R in Arr'Range loop
1401 F := Osint.To_Host_File_Spec
1402 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1405 & ':' & Get_Line (Arr (R))
1406 & ':' & Get_Column (Arr (R)));
1420 procedure Print_Vi (Full_Path_Name : Boolean) is
1421 Tab : constant Character := ASCII.HT;
1422 Decls : constant Declaration_Array_Access :=
1423 Get_Declarations (Sorted => False);
1424 Decl : Declaration_Reference;
1425 Arr : Reference_Array_Access;
1429 for D in Decls'Range loop
1432 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1433 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1436 -- Print the body if any
1438 Arr := Get_References (Decl, Get_Bodies => True);
1440 for R in Arr'Range loop
1441 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1443 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1449 -- Print the modifications
1451 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1453 for R in Arr'Range loop
1454 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1456 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1468 procedure Print_Xref (Full_Path_Name : Boolean) is
1469 Decls : constant Declaration_Array_Access := Get_Declarations;
1470 Decl : Declaration_Reference;
1472 Margin : constant := 10;
1473 -- Column where file names start
1475 procedure New_Line80;
1476 -- Go to start of new line
1478 procedure Print80 (S : String);
1479 -- Print the text, respecting the 80 columns rule
1481 procedure Print_Ref (Line, Column : String);
1482 -- The beginning of the output is aligned on a column multiple of 9
1484 procedure Print_List
1485 (Decl : Declaration_Reference;
1487 Get_Reads : Boolean := False;
1488 Get_Writes : Boolean := False;
1489 Get_Bodies : Boolean := False);
1490 -- Print a list of references. If the list is not empty, Msg will
1491 -- be printed prior to the list.
1497 procedure New_Line80 is
1500 Write_Str (String'(1 .. Margin
- 1 => ' '));
1507 procedure Print80
(S
: String) is
1508 Align
: Natural := Margin
- (Integer (Column
) mod Margin
);
1511 if Align
= Margin
then
1515 Write_Str
(String'(1 .. Align => ' ') & S);
1522 procedure Print_Ref (Line, Column : String) is
1523 Line_Align : constant Integer := 4 - Line'Length;
1525 S : constant String := String'(1 .. Line_Align
=> ' ')
1526 & Line
& ':' & Column
;
1528 Align
: Natural := Margin
- (Integer (Output
.Column
) mod Margin
);
1531 if Align
= Margin
then
1535 if Integer (Output
.Column
) + Align
+ S
'Length > 79 then
1540 Write_Str
(String'(1 .. Align => ' ') & S);
1547 procedure Print_List
1548 (Decl : Declaration_Reference;
1550 Get_Reads : Boolean := False;
1551 Get_Writes : Boolean := False;
1552 Get_Bodies : Boolean := False)
1554 Arr : Reference_Array_Access :=
1557 Get_Writes => Get_Writes,
1558 Get_Reads => Get_Reads,
1559 Get_Bodies => Get_Bodies);
1560 File : File_Reference := Empty_File;
1564 if Arr'Length /= 0 then
1569 for R in Arr'Range loop
1570 if Get_File_Ref (Arr (R)) /= File then
1571 if File /= Empty_File then
1575 File := Get_File_Ref (Arr (R));
1576 F := Osint.To_Host_File_Spec
1577 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1578 Write_Str (F.all & ' ');
1582 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1590 -- Start of processing for Print_Xref
1593 for D in Decls'Range loop
1596 Write_Str (Get_Symbol (Decl));
1598 while Column < Type_Position loop
1602 Write_Line (Get_Full_Type (Decl));
1604 Write_Parent_Info : declare
1605 Parent : constant Declaration_Reference := Get_Parent (Decl);
1608 if Parent /= Empty_Declaration then
1609 Write_Str (" Ptype: ");
1610 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1613 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1614 Print80 (" " & Get_Symbol (Parent));
1617 end Write_Parent_Info;
1619 Write_Str (" Decl: ");
1620 F := Osint.To_Host_File_Spec
1621 (Get_Gnatchop_File (Decl, Full_Path_Name));
1622 Print80 (F.all & ' ');
1624 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1627 (Decl, " Body: ", Get_Bodies => True);
1629 (Decl, " Modi: ", Get_Writes => True);
1631 (Decl, " Ref: ", Get_Reads => True);
1641 (Pattern : Search_Pattern;
1642 Local_Symbols : Boolean;
1643 Wide_Search : Boolean;
1644 Read_Only : Boolean;
1646 Type_Tree : Boolean)
1648 type String_Access is access String;
1649 procedure Free is new Unchecked_Deallocation (String, String_Access);
1652 File_Ref : File_Reference;
1653 Strip_Num : Natural := 0;
1654 Ali_Name : String_Access;
1657 -- If we want all the .ali files, then find them
1664 -- Get the next unread ali file
1666 File_Ref := Next_Unvisited_File;
1668 exit when File_Ref = Empty_File;
1670 -- Find the ALI file to use. Most of the time, it will be the unit
1671 -- name, with a different extension. However, when dealing with
1672 -- separates the ALI file is in fact the parent's ALI file (and this
1673 -- is recursive, in case the parent itself is a separate).
1678 Ali_Name := new String'
1679 (Get_File
(File_Ref
, With_Dir
=> True, Strip
=> Strip_Num
));
1681 -- Stripped too many things...
1683 if Ali_Name
.all = "" then
1684 if Get_Emit_Warning
(File_Ref
) then
1687 ("warning : file " & Get_File
(File_Ref
, With_Dir
=> True)
1689 Set_Standard_Output
;
1694 -- If not found, try the parent's ALI file (this is needed for
1695 -- separate units and subprograms).
1697 -- Reset the cached directory first, in case the separate's
1698 -- ALI file is not in the same directory.
1700 elsif not File_Exists
(Ali_Name
.all) then
1701 Strip_Num
:= Strip_Num
+ 1;
1702 Reset_Directory
(File_Ref
);
1704 -- Else we finally found it
1711 -- If we had to get the parent's ALI, insert it in the list as usual.
1712 -- This is to avoid parsing it twice in case it has already been
1715 if Ali_Name
/= null and then Strip_Num
/= 0 then
1716 File_Ref
:= Add_To_Xref_File
1717 (File_Name
=> Ali_Name
.all,
1720 -- Now that we have a file name, parse it to find any reference to
1723 elsif Ali_Name
/= null
1724 and then (Read_Only
or else Is_Writable_File
(Ali_Name
.all))
1727 Open
(Ali_Name
.all, ALIfile
);
1728 while ALIfile
.Buffer
(ALIfile
.Current_Line
) /= EOF
loop
1729 Parse_X_Filename
(ALIfile
);
1730 Parse_Identifier_Info
1731 (Pattern
, ALIfile
, Local_Symbols
,
1732 Der_Info
, Type_Tree
, Wide_Search
, Labels_As_Ref
=> True);
1736 when No_Xref_Information
=>
1737 if Get_Emit_Warning
(File_Ref
) then
1740 ("warning : No cross-referencing information in "
1742 Set_Standard_Output
;
1755 procedure Search_Xref
1756 (Local_Symbols
: Boolean;
1757 Read_Only
: Boolean;
1761 File_Ref
: File_Reference
;
1762 Null_Pattern
: Search_Pattern
;
1765 Null_Pattern
.Initialized
:= False;
1768 -- Find the next unvisited file
1770 File_Ref
:= Next_Unvisited_File
;
1771 exit when File_Ref
= Empty_File
;
1773 -- Search the object directories for the .ali file
1776 F
: constant String := Get_File
(File_Ref
, With_Dir
=> True);
1779 if Read_Only
or else Is_Writable_File
(F
) then
1780 Open
(F
, ALIfile
, True);
1782 while ALIfile
.Buffer
(ALIfile
.Current_Line
) /= EOF
loop
1783 Parse_X_Filename
(ALIfile
);
1784 Parse_Identifier_Info
1785 (Null_Pattern
, ALIfile
, Local_Symbols
, Der_Info
,
1786 Labels_As_Ref
=> False);
1791 when No_Xref_Information
=> null;