1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2017, 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
;
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 Windows)
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
;
276 File_Name
: String := Entity
(File_Start
.. Line_Start
- 1);
279 Osint
.Canonical_Case_File_Name
(File_Name
);
280 File_Ref
:= Add_To_Xref_File
(File_Name
, Visited
=> True);
281 Pattern
.File_Ref
:= File_Ref
;
283 Add_Line
(Pattern
.File_Ref
, Line_Num
, Col_Num
);
287 (ALI_File_Name
(File_Name
),
289 Emit_Warning
=> True);
297 procedure Add_Xref_File
(File
: String) is
298 File_Ref
: File_Reference
:= Empty_File
;
299 pragma Unreferenced
(File_Ref
);
301 Iterator
: Expansion_Iterator
;
303 procedure Add_Xref_File_Internal
(File
: String);
304 -- Do the actual addition of the file
306 ----------------------------
307 -- Add_Xref_File_Internal --
308 ----------------------------
310 procedure Add_Xref_File_Internal
(File
: String) is
312 -- Case where we have an ALI file, accept it even though this is
313 -- not official usage, since the intention is obvious
315 if Tail
(File
, 4) = "." & Osint
.ALI_Suffix
.all then
316 File_Ref
:= Add_To_Xref_File
317 (File
, Visited
=> False, Emit_Warning
=> True);
319 -- Normal non-ali file case
322 File_Ref
:= Add_To_Xref_File
(File
, Visited
=> True);
324 File_Ref
:= Add_To_Xref_File
325 (ALI_File_Name
(File
),
326 Visited
=> False, Emit_Warning
=> True);
328 end Add_Xref_File_Internal
;
330 -- Start of processing for Add_Xref_File
333 -- Check if we need to do the expansion
335 if Ada
.Strings
.Fixed
.Index
(File
, "*") /= 0
336 or else Ada
.Strings
.Fixed
.Index
(File
, "?") /= 0
338 Start_Expansion
(Iterator
, File
);
342 S
: constant String := Expansion
(Iterator
);
345 exit when S
'Length = 0;
346 Add_Xref_File_Internal
(S
);
351 Add_Xref_File_Internal
(File
);
355 -----------------------
356 -- Current_Xref_File --
357 -----------------------
359 function Current_Xref_File
(File
: ALI_File
) return File_Reference
is
362 end Current_Xref_File
;
364 --------------------------
365 -- Default_Project_File --
366 --------------------------
368 function Default_Project_File
(Dir_Name
: String) return String is
370 Dir_Ent
: File_Name_String
;
374 Open
(My_Dir
, Dir_Name
);
377 Read
(My_Dir
, Dir_Ent
, Last
);
380 if Tail
(Dir_Ent
(1 .. Last
), 4) = ".adp" then
382 -- The first project file found is the good one
385 return Dir_Ent
(1 .. Last
);
390 return String'(1 .. 0 => ' ');
393 when Directory_Error => return String'(1 .. 0 => ' ');
394 end Default_Project_File
;
402 Num
: Positive) return File_Reference
404 Table
: Table_Type
renames File
.Dep
.Table
(1 .. Last
(File
.Dep
));
413 procedure Find_ALI_Files
is
415 Dir_Ent
: File_Name_String
;
418 File_Ref
: File_Reference
;
419 pragma Unreferenced
(File_Ref
);
421 function Open_Next_Dir
return Boolean;
422 -- Tries to open the next object directory, and return False if
423 -- the directory cannot be opened.
429 function Open_Next_Dir
return Boolean is
431 -- Until we are able to open a new directory
435 Obj_Dir
: constant String := Next_Obj_Dir
;
438 -- Case of no more Obj_Dir lines
440 if Obj_Dir
'Length = 0 then
444 Open
(My_Dir
.Dir
, Obj_Dir
);
449 -- Could not open the directory
451 when Directory_Error
=> null;
458 -- Start of processing for Find_ALI_Files
463 if Open_Next_Dir
then
465 Read
(My_Dir
.Dir
, Dir_Ent
, Last
);
470 if not Open_Next_Dir
then
475 and then Dir_Ent
(Last
- 3 .. Last
) = "." & Osint
.ALI_Suffix
.all
478 Add_To_Xref_File
(Dir_Ent
(1 .. Last
), Visited
=> False);
488 function Get_Full_Type
(Decl
: Declaration_Reference
) return String is
490 function Param_String
return String;
491 -- Return the string to display depending on whether Decl is a parameter
497 function Param_String
return String is
499 if Is_Parameter
(Decl
) then
506 -- Start of processing for Get_Full_Type
509 case Get_Type
(Decl
) is
510 when 'A' => return "array type";
511 when 'B' => return "boolean type";
512 when 'C' => return "class-wide type";
513 when 'D' => return "decimal type";
514 when 'E' => return "enumeration type";
515 when 'F' => return "float type";
516 when 'H' => return "abstract type";
517 when 'I' => return "integer type";
518 when 'M' => return "modular type";
519 when 'O' => return "fixed type";
520 when 'P' => return "access type";
521 when 'R' => return "record type";
522 when 'S' => return "string type";
523 when 'T' => return "task type";
524 when 'W' => return "protected type";
526 when 'a' => return Param_String
& "array object";
527 when 'b' => return Param_String
& "boolean object";
528 when 'c' => return Param_String
& "class-wide object";
529 when 'd' => return Param_String
& "decimal object";
530 when 'e' => return Param_String
& "enumeration object";
531 when 'f' => return Param_String
& "float object";
532 when 'i' => return Param_String
& "integer object";
533 when 'j' => return Param_String
& "class object";
534 when 'm' => return Param_String
& "modular object";
535 when 'o' => return Param_String
& "fixed object";
536 when 'p' => return Param_String
& "access object";
537 when 'r' => return Param_String
& "record object";
538 when 's' => return Param_String
& "string object";
539 when 't' => return Param_String
& "task object";
540 when 'w' => return Param_String
& "protected object";
541 when 'x' => return Param_String
& "abstract procedure";
542 when 'y' => return Param_String
& "abstract function";
544 when 'h' => return "interface";
545 when 'g' => return "macro";
546 when 'G' => return "function macro";
547 when 'J' => return "class";
548 when 'K' => return "package";
549 when 'k' => return "generic package";
550 when 'L' => return "statement label";
551 when 'l' => return "loop label";
552 when 'N' => return "named number";
553 when 'n' => return "enumeration literal";
554 when 'q' => return "block label";
555 when 'Q' => return "include file";
556 when 'U' => return "procedure";
557 when 'u' => return "generic procedure";
558 when 'V' => return "function";
559 when 'v' => return "generic function";
560 when 'X' => return "exception";
561 when 'Y' => return "entry";
563 when '+' => return "private type";
564 when '*' => return "private variable";
566 -- The above should be the only possibilities, but for this kind
567 -- of informational output, we don't want to bomb if we find
568 -- something else, so just return three question marks when we
569 -- have an unknown Abbrev value
572 if Is_Parameter
(Decl
) then
575 return "??? (" & Get_Type
(Decl
) & ")";
580 --------------------------
581 -- Skip_To_First_X_Line --
582 --------------------------
584 procedure Skip_To_First_X_Line
585 (File
: in out ALI_File
;
589 Ali
: String_Access
renames File
.Buffer
;
591 Ptr
: Positive := Ali
'First;
592 Num_Dependencies
: Natural := 0;
593 File_Start
: Positive;
595 Gnatchop_Offset
: Integer;
596 Gnatchop_Name
: Positive;
598 File_Ref
: File_Reference
;
599 pragma Unreferenced
(File_Ref
);
602 -- Read all the lines possibly processing with-clauses and dependency
603 -- information and exit on finding the first Xref line.
604 -- A fall-through of the loop means that there is no xref information
605 -- which is an error condition.
607 while Ali
(Ptr
) /= EOF
loop
608 if D_Lines
and then Ali
(Ptr
) = 'D' then
610 -- Found dependency information. Format looks like:
611 -- D src-nam time-stmp checksum [subunit-name] [line:file-name]
613 -- Skip the D and parse the filenam
616 Parse_Token
(Ali
, Ptr
, Token
);
620 Num_Dependencies
:= Num_Dependencies
+ 1;
621 Set_Last
(File
.Dep
, Num_Dependencies
);
623 Parse_Token
(Ali
, Ptr
, Token
); -- Skip time-stamp
624 Parse_Token
(Ali
, Ptr
, Token
); -- Skip checksum
625 Parse_Token
(Ali
, Ptr
, Token
); -- Read next entity on the line
627 if not (Ali
(Token
) in '0' .. '9') then
628 Parse_Token
(Ali
, Ptr
, Token
); -- Was a subunit name
631 -- Did we have a gnatchop-ed file with a pragma Source_Reference ?
633 Gnatchop_Offset
:= 0;
635 if Ali
(Token
) in '0' .. '9' then
636 Gnatchop_Name
:= Token
;
637 while Ali
(Gnatchop_Name
) /= ':' loop
638 Gnatchop_Name
:= Gnatchop_Name
+ 1;
642 2 - Natural'Value (Ali
(Token
.. Gnatchop_Name
- 1));
643 Token
:= Gnatchop_Name
+ 1;
647 Table
: Table_Type
renames
648 File
.Dep
.Table
(1 .. Last
(File
.Dep
));
650 Table
(Num_Dependencies
) := Add_To_Xref_File
651 (Ali
(File_Start
.. File_End
),
652 Gnatchop_File
=> Ali
(Token
.. Ptr
- 1),
653 Gnatchop_Offset
=> Gnatchop_Offset
);
656 elsif W_Lines
and then Ali
(Ptr
) = 'W' then
658 -- Found with-clause information. Format looks like:
659 -- "W debug%s debug.adb debug.ali"
661 -- Skip the W and parse the .ali filename (3rd token)
663 Parse_Token
(Ali
, Ptr
, Token
);
664 Parse_Token
(Ali
, Ptr
, Token
);
665 Parse_Token
(Ali
, Ptr
, Token
);
668 Add_To_Xref_File
(Ali
(Token
.. Ptr
- 1), Visited
=> False);
670 elsif Ali
(Ptr
) = 'X' then
672 -- Found a cross-referencing line - stop processing
674 File
.Current_Line
:= Ptr
;
675 File
.Xref_Line
:= Ptr
;
679 Parse_EOL
(Ali
, Ptr
);
682 raise No_Xref_Information
;
683 end Skip_To_First_X_Line
;
692 Dependencies
: Boolean := False)
694 Ali
: String_Access
renames File
.Buffer
;
695 pragma Warnings
(Off
, Ali
);
698 if File
.Buffer
/= null then
705 Read_File
(Name
, Ali
);
708 when Ada
.Text_IO
.Name_Error | Ada
.Text_IO
.End_Error
=>
709 raise No_Xref_Information
;
712 Skip_To_First_X_Line
(File
, D_Lines
=> True, W_Lines
=> Dependencies
);
720 (Source
: not null access String;
721 Ptr
: in out Positive;
722 Skip_Continuation_Line
: Boolean := False)
726 -- Skip to end of line
728 while Source
(Ptr
) /= ASCII
.CR
and then Source
(Ptr
) /= ASCII
.LF
729 and then Source
(Ptr
) /= EOF
734 -- Skip CR or LF if not at end of file
736 if Source
(Ptr
) /= EOF
then
740 -- Skip past CR/LF or LF/CR combination
742 if (Source
(Ptr
) = ASCII
.CR
or else Source
(Ptr
) = ASCII
.LF
)
743 and then Source
(Ptr
) /= Source
(Ptr
- 1)
748 exit when not Skip_Continuation_Line
or else Source
(Ptr
) /= '.';
752 ---------------------------
753 -- Parse_Identifier_Info --
754 ---------------------------
756 procedure Parse_Identifier_Info
757 (Pattern
: Search_Pattern
;
758 File
: in out ALI_File
;
759 Local_Symbols
: Boolean;
760 Der_Info
: Boolean := False;
761 Type_Tree
: Boolean := False;
762 Wide_Search
: Boolean := True;
763 Labels_As_Ref
: Boolean := True)
765 Ptr
: Positive renames File
.Current_Line
;
766 Ali
: String_Access
renames File
.Buffer
;
768 E_Line
: Natural; -- Line number of current entity
769 E_Col
: Natural; -- Column number of current entity
770 E_Type
: Character; -- Type of current entity
771 E_Name
: Positive; -- Pointer to begin of entity name
772 E_Global
: Boolean; -- True iff entity is global
774 R_Line
: Natural; -- Line number of current reference
775 R_Col
: Natural; -- Column number of current reference
776 R_Type
: Character; -- Type of current reference
778 Decl_Ref
: Declaration_Reference
;
779 File_Ref
: File_Reference
:= Current_Xref_File
(File
);
781 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String;
782 -- Returns the symbol name for the entity defined at the specified
783 -- line and column in the dependent unit number Eun. For this we need
784 -- to parse the ali file again because the parent entity is not in
785 -- the declaration table if it did not match the search pattern.
787 procedure Skip_To_Matching_Closing_Bracket
;
788 -- When Ptr points to an opening square bracket, moves it to the
789 -- character following the matching closing bracket
791 ---------------------
792 -- Get_Symbol_Name --
793 ---------------------
795 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String is
797 E_Eun
: Positive; -- Unit number of current entity
798 E_Line
: Natural; -- Line number of current entity
799 E_Col
: Natural; -- Column number of current entity
800 E_Name
: Positive; -- Pointer to begin of entity name
803 -- Look for the X lines corresponding to unit Eun
806 if Ali
(Ptr
) = 'X' then
808 Parse_Number
(Ali
, Ptr
, E_Eun
);
809 exit when E_Eun
= Eun
;
812 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
815 -- Here we are in the right Ali section, we now look for the entity
816 -- declared at position (Line, Col).
819 Parse_Number
(Ali
, Ptr
, E_Line
);
820 exit when Ali
(Ptr
) = EOF
;
822 Parse_Number
(Ali
, Ptr
, E_Col
);
823 exit when Ali
(Ptr
) = EOF
;
826 if Line
= E_Line
and then Col
= E_Col
then
827 Parse_Token
(Ali
, Ptr
, E_Name
);
828 return Ali
(E_Name
.. Ptr
- 1);
831 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
832 exit when Ali
(Ptr
) = EOF
;
835 -- We were not able to find the symbol, this should not happen but
836 -- since we don't want to stop here we return a string of three
837 -- question marks as the symbol name.
842 --------------------------------------
843 -- Skip_To_Matching_Closing_Bracket --
844 --------------------------------------
846 procedure Skip_To_Matching_Closing_Bracket
is
847 Num_Brackets
: Natural;
851 while Num_Brackets
/= 0 loop
853 if Ali
(Ptr
) = '[' then
854 Num_Brackets
:= Num_Brackets
+ 1;
855 elsif Ali
(Ptr
) = ']' then
856 Num_Brackets
:= Num_Brackets
- 1;
861 end Skip_To_Matching_Closing_Bracket
;
863 Table
: Table_Type
renames File
.Dep
.Table
(1 .. Last
(File
.Dep
));
865 -- Start of processing for Parse_Identifier_Info
868 -- The identifier info looks like:
869 -- "38U9*Debug 12|36r6 36r19"
871 -- Extract the line, column and entity name information
873 Parse_Number
(Ali
, Ptr
, E_Line
);
875 if Ali
(Ptr
) > ' ' then
880 -- Ignore some of the entities (labels,...)
883 when 'l' |
'L' |
'q' =>
884 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
891 Parse_Number
(Ali
, Ptr
, E_Col
);
894 if Ali
(Ptr
) >= ' ' then
895 E_Global
:= (Ali
(Ptr
) = '*');
899 Parse_Token
(Ali
, Ptr
, E_Name
);
901 -- Exit if the symbol does not match or if we have a local symbol and we
902 -- do not want it or if the file is unknown.
904 if File
.X_File
= Empty_File
then
908 if (not Local_Symbols
and not E_Global
)
909 or else (Pattern
.Initialized
910 and then not Match
(Ali
(E_Name
.. Ptr
- 1), Pattern
.Entity
))
911 or else (E_Name
>= Ptr
)
913 Decl_Ref
:= Add_Declaration
914 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
,
915 Remove_Only
=> True);
916 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
920 -- Insert the declaration in the table
922 Decl_Ref
:= Add_Declaration
923 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
);
925 if Ali
(Ptr
) = '[' then
926 Skip_To_Matching_Closing_Bracket
;
929 -- Skip any renaming indication
931 if Ali
(Ptr
) = '=' then
933 P_Line
, P_Column
: Natural;
934 pragma Warnings
(Off
, P_Line
);
935 pragma Warnings
(Off
, P_Column
);
938 Parse_Number
(Ali
, Ptr
, P_Line
);
940 Parse_Number
(Ali
, Ptr
, P_Column
);
944 while Ptr
<= Ali
'Last
945 and then (Ali
(Ptr
) = '<'
946 or else Ali
(Ptr
) = '('
947 or else Ali
(Ptr
) = '{')
949 -- Here we have a type derivation information. The format is
950 -- <3|12I45> which means that the current entity is derived from the
951 -- type defined in unit number 3, line 12 column 45. The pipe and
952 -- unit number is optional. It is specified only if the parent type
953 -- is not defined in the current unit.
955 -- We also have the format for generic instantiations, as in
956 -- 7a5*Uid(3|5I8[4|2]) 2|4r74
958 -- We could also have something like
960 -- that indicates that I derives from the predefined type integer.
964 if Ali
(Ptr
) in '0' .. '9' then
965 Parse_Derived_Info
: declare
966 P_Line
: Natural; -- parent entity line
967 P_Column
: Natural; -- parent entity column
968 P_Eun
: Positive; -- parent entity file number
971 Parse_Number
(Ali
, Ptr
, P_Line
);
973 -- If we have a pipe then the first number was the unit number
975 if Ali
(Ptr
) = '|' then
979 -- Now we have the line number
981 Parse_Number
(Ali
, Ptr
, P_Line
);
984 -- We don't have a unit number specified, so we set P_Eun to
987 for K
in Table
'Range loop
989 exit when Table
(K
) = File_Ref
;
993 -- Then parse the type and column number
996 Parse_Number
(Ali
, Ptr
, P_Column
);
998 -- Skip the information for generics instantiations
1000 if Ali
(Ptr
) = '[' then
1001 Skip_To_Matching_Closing_Bracket
;
1004 -- Skip '>', or ')' or '>'
1008 -- The derived info is needed only is the derived info mode is
1009 -- on or if we want to output the type hierarchy
1011 if Der_Info
or else Type_Tree
then
1013 Symbol
: constant String :=
1014 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
);
1016 if Symbol
/= "???" then
1028 and then (Pattern
.File_Ref
= Empty_File
1030 Pattern
.File_Ref
= Current_Xref_File
(File
))
1032 Search_Parent_Tree
: declare
1033 Pattern
: Search_Pattern
; -- Parent type pattern
1034 File_Pos_Backup
: Positive;
1039 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
)
1040 & ':' & Get_Gnatchop_File
(Table
(P_Eun
))
1041 & ':' & Get_Line
(Get_Parent
(Decl_Ref
))
1042 & ':' & Get_Column
(Get_Parent
(Decl_Ref
)),
1045 -- No default match is needed to look for the parent type
1046 -- since we are using the fully qualified symbol name:
1047 -- symbol:file:line:column
1049 Set_Default_Match
(False);
1051 -- The parent hierarchy is defined in the same unit as
1052 -- the derived type. So we want to revisit the unit.
1054 File_Pos_Backup
:= File
.Current_Line
;
1056 Skip_To_First_X_Line
1057 (File
, D_Lines
=> False, W_Lines
=> False);
1059 while File
.Buffer
(File
.Current_Line
) /= EOF
loop
1060 Parse_X_Filename
(File
);
1061 Parse_Identifier_Info
1062 (Pattern
=> Pattern
,
1064 Local_Symbols
=> False,
1065 Der_Info
=> Der_Info
,
1067 Wide_Search
=> False,
1068 Labels_As_Ref
=> Labels_As_Ref
);
1071 File
.Current_Line
:= File_Pos_Backup
;
1072 end Search_Parent_Tree
;
1074 end Parse_Derived_Info
;
1077 while Ali
(Ptr
) /= '>'
1078 and then Ali
(Ptr
) /= ')'
1079 and then Ali
(Ptr
) /= '}'
1087 -- To find the body, we will have to parse the file too
1091 File_Name
: constant String := Get_Gnatchop_File
(File
.X_File
);
1092 Ignored
: File_Reference
;
1094 Ignored
:= Add_To_Xref_File
(ALI_File_Name
(File_Name
), False);
1098 -- Parse references to this entity.
1099 -- Ptr points to next reference with leading blanks
1102 -- Process references on current line
1104 while Ali
(Ptr
) = ' ' or else Ali
(Ptr
) = ASCII
.HT
loop
1106 -- For every reference read the line, type and column,
1107 -- optionally preceded by a file number and a pipe symbol.
1109 Parse_Number
(Ali
, Ptr
, R_Line
);
1111 if Ali
(Ptr
) = Pipe
then
1113 File_Ref
:= File_Name
(File
, R_Line
);
1115 Parse_Number
(Ali
, Ptr
, R_Line
);
1118 if Ali
(Ptr
) > ' ' then
1119 R_Type
:= Ali
(Ptr
);
1123 -- Imported entities may have an indication specifying information
1124 -- about the corresponding external name:
1125 -- 5U14*Foo2 5>20 6b<c,myfoo2>22 # Imported entity
1126 -- 5U14*Foo2 5>20 6i<c,myfoo2>22 # Exported entity
1128 if (R_Type
= 'b' or else R_Type
= 'i')
1129 and then Ali
(Ptr
) = '<'
1131 while Ptr
<= Ali
'Last
1132 and then Ali
(Ptr
) /= '>'
1139 Parse_Number
(Ali
, Ptr
, R_Col
);
1141 -- Insert the reference or body in the table
1144 (Decl_Ref
, File_Ref
, R_Line
, R_Col
, R_Type
, Labels_As_Ref
);
1146 -- Skip generic information, if any
1148 if Ali
(Ptr
) = '[' then
1150 Num_Nested
: Integer := 1;
1154 while Num_Nested
/= 0 loop
1155 if Ali
(Ptr
) = ']' then
1156 Num_Nested
:= Num_Nested
- 1;
1157 elsif Ali
(Ptr
) = '[' then
1158 Num_Nested
:= Num_Nested
+ 1;
1168 Parse_EOL
(Ali
, Ptr
);
1170 -- Loop until new line is no continuation line
1172 exit when Ali
(Ptr
) /= '.';
1175 end Parse_Identifier_Info
;
1181 procedure Parse_Number
1182 (Source
: not null access String;
1183 Ptr
: in out Positive;
1184 Number
: out Natural)
1189 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1194 while Source
(Ptr
) in '0' .. '9' loop
1196 10 * Number
+ (Character'Pos (Source
(Ptr
)) - Character'Pos ('0'));
1205 procedure Parse_Token
1206 (Source
: not null access String;
1207 Ptr
: in out Positive;
1208 Token_Ptr
: out Positive)
1210 In_Quotes
: Character := ASCII
.NUL
;
1215 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1221 -- Find end-of-token
1223 while (In_Quotes
/= ASCII
.NUL
or else
1224 not (Source
(Ptr
) = ' '
1225 or else Source
(Ptr
) = ASCII
.HT
1226 or else Source
(Ptr
) = '<'
1227 or else Source
(Ptr
) = '{'
1228 or else Source
(Ptr
) = '['
1229 or else Source
(Ptr
) = '='
1230 or else Source
(Ptr
) = '('))
1231 and then Source
(Ptr
) >= ' '
1233 -- Double-quotes are used for operators
1234 -- Simple-quotes are used for character constants, for instance when
1235 -- they are found in an enumeration type "type A is ('+', '-');"
1237 case Source
(Ptr
) is
1239 if In_Quotes
= Source
(Ptr
) then
1240 In_Quotes
:= ASCII
.NUL
;
1241 elsif In_Quotes
= ASCII
.NUL
then
1242 In_Quotes
:= Source
(Ptr
);
1253 ----------------------
1254 -- Parse_X_Filename --
1255 ----------------------
1257 procedure Parse_X_Filename
(File
: in out ALI_File
) is
1258 Ali
: String_Access
renames File
.Buffer
;
1259 Ptr
: Positive renames File
.Current_Line
;
1262 Table
: Table_Type
renames File
.Dep
.Table
(1 .. Last
(File
.Dep
));
1265 while Ali
(Ptr
) = 'X' loop
1267 -- The current line is the start of a new Xref file section,
1268 -- whose format looks like:
1272 -- Skip the X and read the file number for the new X_File
1275 Parse_Number
(Ali
, Ptr
, File_Nr
);
1277 -- If the referenced file is unknown, we simply ignore it
1279 if File_Nr
in Table
'Range then
1280 File
.X_File
:= Table
(File_Nr
);
1282 File
.X_File
:= Empty_File
;
1285 Parse_EOL
(Ali
, Ptr
);
1287 end Parse_X_Filename
;
1289 --------------------
1290 -- Print_Gnatfind --
1291 --------------------
1293 procedure Print_Gnatfind
1294 (References
: Boolean;
1295 Full_Path_Name
: Boolean)
1297 Decls
: constant Declaration_Array_Access
:= Get_Declarations
;
1298 Decl
: Declaration_Reference
;
1299 Arr
: Reference_Array_Access
;
1303 Msg
: String := " ");
1304 -- Print a reference, according to the extended tag of the output
1312 Msg
: String := " ")
1314 F
: String_Access
:=
1315 Osint
.To_Host_File_Spec
1316 (Get_Gnatchop_File
(Ref
, Full_Path_Name
));
1318 Buffer
: constant String :=
1320 ":" & Get_Line
(Ref
) &
1321 ":" & Get_Column
(Ref
) &
1324 Num_Blanks
: Integer := Longest_File_Name
+ 10 - Buffer
'Length;
1328 Num_Blanks
:= Integer'Max (0, Num_Blanks
);
1331 & String'(1 .. Num_Blanks => ' ')
1332 & Msg & " " & Get_Symbol (Decl));
1334 if Get_Source_Line (Ref)'Length /= 0 then
1335 Write_Line (" " & Get_Source_Line (Ref));
1339 -- Start of processing for Print_Gnatfind
1342 for D in Decls'Range loop
1345 if Match (Decl) then
1347 -- Output the declaration
1350 Parent : constant Declaration_Reference := Get_Parent (Decl);
1352 F : String_Access :=
1353 Osint.To_Host_File_Spec
1354 (Get_Gnatchop_File (Decl, Full_Path_Name));
1356 Buffer : constant String :=
1358 ":" & Get_Line (Decl) &
1359 ":" & Get_Column (Decl) &
1362 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1366 Num_Blanks := Integer'Max (0, Num_Blanks);
1368 (Buffer & String'(1 .. Num_Blanks
=> ' ')
1369 & "(spec) " & Get_Symbol
(Decl
));
1371 if Parent
/= Empty_Declaration
then
1372 F
:= Osint
.To_Host_File_Spec
(Get_Gnatchop_File
(Parent
));
1374 (Buffer
& String'(1 .. Num_Blanks => ' ')
1375 & " derived from " & Get_Symbol (Parent)
1378 & ':' & Get_Line (Parent)
1379 & ':' & Get_Column (Parent) & ')');
1384 if Get_Source_Line (Decl)'Length /= 0 then
1385 Write_Line (" " & Get_Source_Line (Decl));
1388 -- Output the body (sorted)
1390 Arr := Get_References (Decl, Get_Bodies => True);
1392 for R in Arr'Range loop
1393 Print_Ref (Arr (R), "(body)");
1399 Arr := Get_References
1400 (Decl, Get_Writes => True, Get_Reads => True);
1402 for R in Arr'Range loop
1403 Print_Ref (Arr (R));
1416 procedure Print_Unused (Full_Path_Name : Boolean) is
1417 Decls : constant Declaration_Array_Access := Get_Declarations;
1418 Decl : Declaration_Reference;
1419 Arr : Reference_Array_Access;
1423 for D in Decls'Range loop
1427 (Decl, Get_Reads => True, Get_Writes => True) = 0
1429 F := Osint.To_Host_File_Spec
1430 (Get_Gnatchop_File (Decl, Full_Path_Name));
1431 Write_Str (Get_Symbol (Decl)
1433 & Get_Full_Type (Decl)
1439 & Get_Column (Decl));
1442 -- Print the body if any
1444 Arr := Get_References (Decl, Get_Bodies => True);
1446 for R in Arr'Range loop
1447 F := Osint.To_Host_File_Spec
1448 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1451 & ':' & Get_Line (Arr (R))
1452 & ':' & Get_Column (Arr (R)));
1466 procedure Print_Vi (Full_Path_Name : Boolean) is
1467 Tab : constant Character := ASCII.HT;
1468 Decls : constant Declaration_Array_Access :=
1469 Get_Declarations (Sorted => False);
1470 Decl : Declaration_Reference;
1471 Arr : Reference_Array_Access;
1475 for D in Decls'Range loop
1478 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1479 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1482 -- Print the body if any
1484 Arr := Get_References (Decl, Get_Bodies => True);
1486 for R in Arr'Range loop
1487 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1489 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1495 -- Print the modifications
1497 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1499 for R in Arr'Range loop
1500 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1502 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1514 procedure Print_Xref (Full_Path_Name : Boolean) is
1515 Decls : constant Declaration_Array_Access := Get_Declarations;
1516 Decl : Declaration_Reference;
1518 Margin : constant := 10;
1519 -- Column where file names start
1521 procedure New_Line80;
1522 -- Go to start of new line
1524 procedure Print80 (S : String);
1525 -- Print the text, respecting the 80 columns rule
1527 procedure Print_Ref (Line, Column : String);
1528 -- The beginning of the output is aligned on a column multiple of 9
1530 procedure Print_List
1531 (Decl : Declaration_Reference;
1533 Get_Reads : Boolean := False;
1534 Get_Writes : Boolean := False;
1535 Get_Bodies : Boolean := False);
1536 -- Print a list of references. If the list is not empty, Msg will
1537 -- be printed prior to the list.
1543 procedure New_Line80 is
1546 Write_Str (String'(1 .. Margin
- 1 => ' '));
1553 procedure Print80
(S
: String) is
1554 Align
: Natural := Margin
- (Integer (Column
) mod Margin
);
1557 if Align
= Margin
then
1561 Write_Str
(String'(1 .. Align => ' ') & S);
1568 procedure Print_Ref (Line, Column : String) is
1569 Line_Align : constant Integer := 4 - Line'Length;
1571 S : constant String := String'(1 .. Line_Align
=> ' ')
1572 & Line
& ':' & Column
;
1574 Align
: Natural := Margin
- (Integer (Output
.Column
) mod Margin
);
1577 if Align
= Margin
then
1581 if Integer (Output
.Column
) + Align
+ S
'Length > 79 then
1586 Write_Str
(String'(1 .. Align => ' ') & S);
1593 procedure Print_List
1594 (Decl : Declaration_Reference;
1596 Get_Reads : Boolean := False;
1597 Get_Writes : Boolean := False;
1598 Get_Bodies : Boolean := False)
1600 Arr : Reference_Array_Access :=
1603 Get_Writes => Get_Writes,
1604 Get_Reads => Get_Reads,
1605 Get_Bodies => Get_Bodies);
1606 File : File_Reference := Empty_File;
1610 if Arr'Length /= 0 then
1615 for R in Arr'Range loop
1616 if Get_File_Ref (Arr (R)) /= File then
1617 if File /= Empty_File then
1621 File := Get_File_Ref (Arr (R));
1622 F := Osint.To_Host_File_Spec
1623 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1626 Write_Str ("<unknown> ");
1628 Write_Str (F.all & ' ');
1633 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1641 -- Start of processing for Print_Xref
1644 for D in Decls'Range loop
1647 Write_Str (Get_Symbol (Decl));
1649 -- Put the declaration type in column Type_Position, but if the
1650 -- declaration name is too long, put at least one space between its
1651 -- name and its type.
1653 while Column < Type_Position - 1 loop
1659 Write_Line (Get_Full_Type (Decl));
1661 Write_Parent_Info : declare
1662 Parent : constant Declaration_Reference := Get_Parent (Decl);
1665 if Parent /= Empty_Declaration then
1666 Write_Str (" Ptype: ");
1667 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1670 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1671 Print80 (" " & Get_Symbol (Parent));
1674 end Write_Parent_Info;
1676 Write_Str (" Decl: ");
1677 F := Osint.To_Host_File_Spec
1678 (Get_Gnatchop_File (Decl, Full_Path_Name));
1681 Print80 ("<unknown> ");
1683 Print80 (F.all & ' ');
1687 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1690 (Decl, " Body: ", Get_Bodies => True);
1692 (Decl, " Modi: ", Get_Writes => True);
1694 (Decl, " Ref: ", Get_Reads => True);
1704 (Pattern : Search_Pattern;
1705 Local_Symbols : Boolean;
1706 Wide_Search : Boolean;
1707 Read_Only : Boolean;
1709 Type_Tree : Boolean)
1711 type String_Access is access String;
1712 procedure Free is new Unchecked_Deallocation (String, String_Access);
1715 File_Ref : File_Reference;
1716 Strip_Num : Natural := 0;
1717 Ali_Name : String_Access;
1720 -- If we want all the .ali files, then find them
1727 -- Get the next unread ali file
1729 File_Ref := Next_Unvisited_File;
1731 exit when File_Ref = Empty_File;
1733 -- Find the ALI file to use. Most of the time, it will be the unit
1734 -- name, with a different extension. However, when dealing with
1735 -- separates the ALI file is in fact the parent's ALI file (and this
1736 -- is recursive, in case the parent itself is a separate).
1741 Ali_Name := new String'
1742 (Get_File
(File_Ref
, With_Dir
=> True, Strip
=> Strip_Num
));
1744 -- Stripped too many things...
1746 if Ali_Name
.all = "" then
1747 if Get_Emit_Warning
(File_Ref
) then
1750 ("warning : file " & Get_File
(File_Ref
, With_Dir
=> True)
1752 Set_Standard_Output
;
1757 -- If not found, try the parent's ALI file (this is needed for
1758 -- separate units and subprograms).
1760 -- Reset the cached directory first, in case the separate's
1761 -- ALI file is not in the same directory.
1763 elsif not File_Exists
(Ali_Name
.all) then
1764 Strip_Num
:= Strip_Num
+ 1;
1765 Reset_Directory
(File_Ref
);
1767 -- Else we finally found it
1774 -- If we had to get the parent's ALI, insert it in the list as usual.
1775 -- This is to avoid parsing it twice in case it has already been
1778 if Ali_Name
/= null and then Strip_Num
/= 0 then
1779 File_Ref
:= Add_To_Xref_File
1780 (File_Name
=> Ali_Name
.all,
1783 -- Now that we have a file name, parse it to find any reference to
1786 elsif Ali_Name
/= null
1787 and then (Read_Only
or else Is_Writable_File
(Ali_Name
.all))
1790 Open
(Ali_Name
.all, ALIfile
);
1792 -- The cross-reference section in the ALI file may be followed
1793 -- by other sections, which can be identified by the starting
1794 -- character of every line, which should neither be 'X' nor a
1795 -- figure in '1' .. '9'.
1797 -- The loop tests below also take into account the end-of-file
1800 while ALIfile
.Buffer
(ALIfile
.Current_Line
) = 'X' loop
1801 Parse_X_Filename
(ALIfile
);
1803 while ALIfile
.Buffer
(ALIfile
.Current_Line
) in '1' .. '9'
1805 Parse_Identifier_Info
1806 (Pattern
, ALIfile
, Local_Symbols
, Der_Info
, Type_Tree
,
1807 Wide_Search
, Labels_As_Ref
=> True);
1812 when No_Xref_Information
=>
1813 if Get_Emit_Warning
(File_Ref
) then
1816 ("warning : No cross-referencing information in "
1818 Set_Standard_Output
;
1831 procedure Search_Xref
1832 (Local_Symbols
: Boolean;
1833 Read_Only
: Boolean;
1837 File_Ref
: File_Reference
;
1838 Null_Pattern
: Search_Pattern
;
1841 Null_Pattern
.Initialized
:= False;
1844 -- Find the next unvisited file
1846 File_Ref
:= Next_Unvisited_File
;
1847 exit when File_Ref
= Empty_File
;
1849 -- Search the object directories for the .ali file
1852 F
: constant String := Get_File
(File_Ref
, With_Dir
=> True);
1855 if Read_Only
or else Is_Writable_File
(F
) then
1856 Open
(F
, ALIfile
, True);
1858 -- The cross-reference section in the ALI file may be followed
1859 -- by other sections, which can be identified by the starting
1860 -- character of every line, which should neither be 'X' nor a
1861 -- figure in '1' .. '9'.
1863 -- The loop tests below also take into account the end-of-file
1866 while ALIfile
.Buffer
(ALIfile
.Current_Line
) = 'X' loop
1867 Parse_X_Filename
(ALIfile
);
1869 while ALIfile
.Buffer
(ALIfile
.Current_Line
) in '1' .. '9'
1871 Parse_Identifier_Info
1872 (Null_Pattern
, ALIfile
, Local_Symbols
, Der_Info
,
1873 Labels_As_Ref
=> False);
1879 when No_Xref_Information
=> null;