1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2019, 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
78 File
: in out ALI_File
;
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
;
691 File
: in out ALI_File
;
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 pragma Assert
(Source
(Ptr
) /= EOF
);
728 -- Skip to end of line
730 while Source
(Ptr
) /= ASCII
.CR
and then Source
(Ptr
) /= ASCII
.LF
731 and then Source
(Ptr
) /= EOF
736 -- Skip CR or LF if not at end of file
738 if Source
(Ptr
) /= EOF
then
744 if Source
(Ptr
- 1) = ASCII
.CR
and then Source
(Ptr
) = ASCII
.LF
then
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.
786 -- If the symbol is not found, we return "???".
788 procedure Skip_To_Matching_Closing_Bracket
;
789 -- When Ptr points to an opening square bracket, moves it to the
790 -- character following the matching closing bracket
792 ---------------------
793 -- Get_Symbol_Name --
794 ---------------------
796 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String is
798 E_Eun
: Positive; -- Unit number of current entity
799 E_Line
: Natural; -- Line number of current entity
800 E_Col
: Natural; -- Column number of current entity
801 E_Name
: Positive; -- Pointer to begin of entity name
804 -- Look for the X lines corresponding to unit Eun
807 if Ali
(Ptr
) = EOF
then
811 if Ali
(Ptr
) = 'X' then
813 Parse_Number
(Ali
, Ptr
, E_Eun
);
814 exit when E_Eun
= Eun
;
817 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
820 -- Here we are in the right Ali section, we now look for the entity
821 -- declared at position (Line, Col).
824 Parse_Number
(Ali
, Ptr
, E_Line
);
825 exit when Ali
(Ptr
) = EOF
;
827 Parse_Number
(Ali
, Ptr
, E_Col
);
828 exit when Ali
(Ptr
) = EOF
;
831 if Line
= E_Line
and then Col
= E_Col
then
832 Parse_Token
(Ali
, Ptr
, E_Name
);
833 return Ali
(E_Name
.. Ptr
- 1);
836 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
837 exit when Ali
(Ptr
) = EOF
;
843 --------------------------------------
844 -- Skip_To_Matching_Closing_Bracket --
845 --------------------------------------
847 procedure Skip_To_Matching_Closing_Bracket
is
848 Num_Brackets
: Natural;
852 while Num_Brackets
/= 0 loop
854 if Ali
(Ptr
) = '[' then
855 Num_Brackets
:= Num_Brackets
+ 1;
856 elsif Ali
(Ptr
) = ']' then
857 Num_Brackets
:= Num_Brackets
- 1;
862 end Skip_To_Matching_Closing_Bracket
;
864 Table
: Table_Type
renames File
.Dep
.Table
(1 .. Last
(File
.Dep
));
866 -- Start of processing for Parse_Identifier_Info
869 -- The identifier info looks like:
870 -- "38U9*Debug 12|36r6 36r19"
872 -- Extract the line, column and entity name information
874 Parse_Number
(Ali
, Ptr
, E_Line
);
876 if Ali
(Ptr
) > ' ' then
881 -- Ignore some of the entities (labels,...)
884 when 'l' |
'L' |
'q' =>
885 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
892 Parse_Number
(Ali
, Ptr
, E_Col
);
895 if Ali
(Ptr
) >= ' ' then
896 E_Global
:= (Ali
(Ptr
) = '*');
900 Parse_Token
(Ali
, Ptr
, E_Name
);
902 -- Exit if the symbol does not match or if we have a local symbol and we
903 -- do not want it or if the file is unknown.
905 if File
.X_File
= Empty_File
then
909 if (not Local_Symbols
and not E_Global
)
910 or else (Pattern
.Initialized
911 and then not Match
(Ali
(E_Name
.. Ptr
- 1), Pattern
.Entity
))
912 or else (E_Name
>= Ptr
)
914 Decl_Ref
:= Add_Declaration
915 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
,
916 Remove_Only
=> True);
917 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
921 -- Insert the declaration in the table
923 Decl_Ref
:= Add_Declaration
924 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
);
926 if Ali
(Ptr
) = '[' then
927 Skip_To_Matching_Closing_Bracket
;
930 -- Skip any renaming indication
932 if Ali
(Ptr
) = '=' then
934 P_Line
, P_Column
: Natural;
935 pragma Warnings
(Off
, P_Line
);
936 pragma Warnings
(Off
, P_Column
);
939 Parse_Number
(Ali
, Ptr
, P_Line
);
941 Parse_Number
(Ali
, Ptr
, P_Column
);
945 while Ptr
<= Ali
'Last
946 and then (Ali
(Ptr
) = '<'
947 or else Ali
(Ptr
) = '('
948 or else Ali
(Ptr
) = '{')
950 -- Here we have a type derivation information. The format is
951 -- <3|12I45> which means that the current entity is derived from the
952 -- type defined in unit number 3, line 12 column 45. The pipe and
953 -- unit number is optional. It is specified only if the parent type
954 -- is not defined in the current unit.
956 -- We also have the format for generic instantiations, as in
957 -- 7a5*Uid(3|5I8[4|2]) 2|4r74
959 -- We could also have something like
961 -- that indicates that I derives from the predefined type integer.
965 if Ali
(Ptr
) in '0' .. '9' then
966 Parse_Derived_Info
: declare
967 P_Line
: Natural; -- parent entity line
968 P_Column
: Natural; -- parent entity column
969 P_Eun
: Positive; -- parent entity file number
972 Parse_Number
(Ali
, Ptr
, P_Line
);
974 -- If we have a pipe then the first number was the unit number
976 if Ali
(Ptr
) = '|' then
980 -- Now we have the line number
982 Parse_Number
(Ali
, Ptr
, P_Line
);
985 -- We don't have a unit number specified, so we set P_Eun to
988 for K
in Table
'Range loop
990 exit when Table
(K
) = File_Ref
;
994 -- Then parse the type and column number
997 Parse_Number
(Ali
, Ptr
, P_Column
);
999 -- Skip the information for generics instantiations
1001 if Ali
(Ptr
) = '[' then
1002 Skip_To_Matching_Closing_Bracket
;
1005 -- Skip '>', or ')' or '>'
1009 -- The derived info is needed only is the derived info mode is
1010 -- on or if we want to output the type hierarchy
1012 if Der_Info
or else Type_Tree
then
1014 Symbol
: constant String :=
1015 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
);
1017 if Symbol
/= "???" then
1029 and then (Pattern
.File_Ref
= Empty_File
1031 Pattern
.File_Ref
= Current_Xref_File
(File
))
1033 Search_Parent_Tree
: declare
1034 Pattern
: Search_Pattern
; -- Parent type pattern
1035 File_Pos_Backup
: Positive;
1040 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
)
1041 & ':' & Get_Gnatchop_File
(Table
(P_Eun
))
1042 & ':' & Get_Line
(Get_Parent
(Decl_Ref
))
1043 & ':' & Get_Column
(Get_Parent
(Decl_Ref
)),
1046 -- No default match is needed to look for the parent type
1047 -- since we are using the fully qualified symbol name:
1048 -- symbol:file:line:column
1050 Set_Default_Match
(False);
1052 -- The parent hierarchy is defined in the same unit as
1053 -- the derived type. So we want to revisit the unit.
1055 File_Pos_Backup
:= File
.Current_Line
;
1057 Skip_To_First_X_Line
1058 (File
, D_Lines
=> False, W_Lines
=> False);
1060 while File
.Buffer
(File
.Current_Line
) /= EOF
loop
1061 Parse_X_Filename
(File
);
1062 Parse_Identifier_Info
1063 (Pattern
=> Pattern
,
1065 Local_Symbols
=> False,
1066 Der_Info
=> Der_Info
,
1068 Wide_Search
=> False,
1069 Labels_As_Ref
=> Labels_As_Ref
);
1072 File
.Current_Line
:= File_Pos_Backup
;
1073 end Search_Parent_Tree
;
1075 end Parse_Derived_Info
;
1078 while Ali
(Ptr
) /= '>'
1079 and then Ali
(Ptr
) /= ')'
1080 and then Ali
(Ptr
) /= '}'
1088 -- To find the body, we will have to parse the file too
1092 File_Name
: constant String := Get_Gnatchop_File
(File
.X_File
);
1093 Ignored
: File_Reference
;
1095 Ignored
:= Add_To_Xref_File
(ALI_File_Name
(File_Name
), False);
1099 -- Parse references to this entity.
1100 -- Ptr points to next reference with leading blanks
1103 -- Process references on current line
1105 while Ali
(Ptr
) = ' ' or else Ali
(Ptr
) = ASCII
.HT
loop
1107 -- For every reference read the line, type and column,
1108 -- optionally preceded by a file number and a pipe symbol.
1110 Parse_Number
(Ali
, Ptr
, R_Line
);
1112 if Ali
(Ptr
) = Pipe
then
1114 File_Ref
:= File_Name
(File
, R_Line
);
1116 Parse_Number
(Ali
, Ptr
, R_Line
);
1119 if Ali
(Ptr
) > ' ' then
1120 R_Type
:= Ali
(Ptr
);
1124 -- Imported entities may have an indication specifying information
1125 -- about the corresponding external name:
1126 -- 5U14*Foo2 5>20 6b<c,myfoo2>22 # Imported entity
1127 -- 5U14*Foo2 5>20 6i<c,myfoo2>22 # Exported entity
1129 if (R_Type
= 'b' or else R_Type
= 'i')
1130 and then Ali
(Ptr
) = '<'
1132 while Ptr
<= Ali
'Last
1133 and then Ali
(Ptr
) /= '>'
1140 Parse_Number
(Ali
, Ptr
, R_Col
);
1142 -- Insert the reference or body in the table
1145 (Decl_Ref
, File_Ref
, R_Line
, R_Col
, R_Type
, Labels_As_Ref
);
1147 -- Skip generic information, if any
1149 if Ali
(Ptr
) = '[' then
1151 Num_Nested
: Integer := 1;
1155 while Num_Nested
/= 0 loop
1156 if Ali
(Ptr
) = ']' then
1157 Num_Nested
:= Num_Nested
- 1;
1158 elsif Ali
(Ptr
) = '[' then
1159 Num_Nested
:= Num_Nested
+ 1;
1169 Parse_EOL
(Ali
, Ptr
);
1171 -- Loop until new line is no continuation line
1173 exit when Ali
(Ptr
) /= '.';
1176 end Parse_Identifier_Info
;
1182 procedure Parse_Number
1183 (Source
: not null access String;
1184 Ptr
: in out Positive;
1185 Number
: out Natural)
1190 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1195 while Source
(Ptr
) in '0' .. '9' loop
1197 10 * Number
+ (Character'Pos (Source
(Ptr
)) - Character'Pos ('0'));
1206 procedure Parse_Token
1207 (Source
: not null access String;
1208 Ptr
: in out Positive;
1209 Token_Ptr
: out Positive)
1211 In_Quotes
: Character := ASCII
.NUL
;
1216 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1222 -- Find end-of-token
1224 while (In_Quotes
/= ASCII
.NUL
or else
1225 not (Source
(Ptr
) = ' '
1226 or else Source
(Ptr
) = ASCII
.HT
1227 or else Source
(Ptr
) = '<'
1228 or else Source
(Ptr
) = '{'
1229 or else Source
(Ptr
) = '['
1230 or else Source
(Ptr
) = '='
1231 or else Source
(Ptr
) = '('))
1232 and then Source
(Ptr
) >= ' '
1234 -- Double-quotes are used for operators
1235 -- Simple-quotes are used for character constants, for instance when
1236 -- they are found in an enumeration type "type A is ('+', '-');"
1238 case Source
(Ptr
) is
1240 if In_Quotes
= Source
(Ptr
) then
1241 In_Quotes
:= ASCII
.NUL
;
1242 elsif In_Quotes
= ASCII
.NUL
then
1243 In_Quotes
:= Source
(Ptr
);
1254 ----------------------
1255 -- Parse_X_Filename --
1256 ----------------------
1258 procedure Parse_X_Filename
(File
: in out ALI_File
) is
1259 Ali
: String_Access
renames File
.Buffer
;
1260 Ptr
: Positive renames File
.Current_Line
;
1263 Table
: Table_Type
renames File
.Dep
.Table
(1 .. Last
(File
.Dep
));
1266 while Ali
(Ptr
) = 'X' loop
1268 -- The current line is the start of a new Xref file section,
1269 -- whose format looks like:
1273 -- Skip the X and read the file number for the new X_File
1276 Parse_Number
(Ali
, Ptr
, File_Nr
);
1278 -- If the referenced file is unknown, we simply ignore it
1280 if File_Nr
in Table
'Range then
1281 File
.X_File
:= Table
(File_Nr
);
1283 File
.X_File
:= Empty_File
;
1286 Parse_EOL
(Ali
, Ptr
);
1288 end Parse_X_Filename
;
1290 --------------------
1291 -- Print_Gnatfind --
1292 --------------------
1294 procedure Print_Gnatfind
1295 (References
: Boolean;
1296 Full_Path_Name
: Boolean)
1298 Decls
: constant Declaration_Array_Access
:= Get_Declarations
;
1299 Decl
: Declaration_Reference
;
1300 Arr
: Reference_Array_Access
;
1304 Msg
: String := " ");
1305 -- Print a reference, according to the extended tag of the output
1313 Msg
: String := " ")
1315 F
: String_Access
:=
1316 Osint
.To_Host_File_Spec
1317 (Get_Gnatchop_File
(Ref
, Full_Path_Name
));
1319 Buffer
: constant String :=
1321 ":" & Get_Line
(Ref
) &
1322 ":" & Get_Column
(Ref
) &
1325 Num_Blanks
: Integer := Longest_File_Name
+ 10 - Buffer
'Length;
1329 Num_Blanks
:= Integer'Max (0, Num_Blanks
);
1332 & String'(1 .. Num_Blanks => ' ')
1333 & Msg & " " & Get_Symbol (Decl));
1335 if Get_Source_Line (Ref)'Length /= 0 then
1336 Write_Line (" " & Get_Source_Line (Ref));
1340 -- Start of processing for Print_Gnatfind
1343 for D in Decls'Range loop
1346 if Match (Decl) then
1348 -- Output the declaration
1351 Parent : constant Declaration_Reference := Get_Parent (Decl);
1353 F : String_Access :=
1354 Osint.To_Host_File_Spec
1355 (Get_Gnatchop_File (Decl, Full_Path_Name));
1357 Buffer : constant String :=
1359 ":" & Get_Line (Decl) &
1360 ":" & Get_Column (Decl) &
1363 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1367 Num_Blanks := Integer'Max (0, Num_Blanks);
1369 (Buffer & String'(1 .. Num_Blanks
=> ' ')
1370 & "(spec) " & Get_Symbol
(Decl
));
1372 if Parent
/= Empty_Declaration
then
1373 F
:= Osint
.To_Host_File_Spec
(Get_Gnatchop_File
(Parent
));
1375 (Buffer
& String'(1 .. Num_Blanks => ' ')
1376 & " derived from " & Get_Symbol (Parent)
1379 & ':' & Get_Line (Parent)
1380 & ':' & Get_Column (Parent) & ')');
1385 if Get_Source_Line (Decl)'Length /= 0 then
1386 Write_Line (" " & Get_Source_Line (Decl));
1389 -- Output the body (sorted)
1391 Arr := Get_References (Decl, Get_Bodies => True);
1393 for R in Arr'Range loop
1394 Print_Ref (Arr (R), "(body)");
1400 Arr := Get_References
1401 (Decl, Get_Writes => True, Get_Reads => True);
1403 for R in Arr'Range loop
1404 Print_Ref (Arr (R));
1417 procedure Print_Unused (Full_Path_Name : Boolean) is
1418 Decls : constant Declaration_Array_Access := Get_Declarations;
1419 Decl : Declaration_Reference;
1420 Arr : Reference_Array_Access;
1424 for D in Decls'Range loop
1428 (Decl, Get_Reads => True, Get_Writes => True) = 0
1430 F := Osint.To_Host_File_Spec
1431 (Get_Gnatchop_File (Decl, Full_Path_Name));
1432 Write_Str (Get_Symbol (Decl)
1434 & Get_Full_Type (Decl)
1440 & Get_Column (Decl));
1443 -- Print the body if any
1445 Arr := Get_References (Decl, Get_Bodies => True);
1447 for R in Arr'Range loop
1448 F := Osint.To_Host_File_Spec
1449 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1452 & ':' & Get_Line (Arr (R))
1453 & ':' & Get_Column (Arr (R)));
1467 procedure Print_Vi (Full_Path_Name : Boolean) is
1468 Tab : constant Character := ASCII.HT;
1469 Decls : constant Declaration_Array_Access :=
1470 Get_Declarations (Sorted => False);
1471 Decl : Declaration_Reference;
1472 Arr : Reference_Array_Access;
1476 for D in Decls'Range loop
1479 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1480 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1483 -- Print the body if any
1485 Arr := Get_References (Decl, Get_Bodies => True);
1487 for R in Arr'Range loop
1488 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1490 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1496 -- Print the modifications
1498 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1500 for R in Arr'Range loop
1501 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1503 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1515 procedure Print_Xref (Full_Path_Name : Boolean) is
1516 Decls : constant Declaration_Array_Access := Get_Declarations;
1517 Decl : Declaration_Reference;
1519 Margin : constant := 10;
1520 -- Column where file names start
1522 procedure New_Line80;
1523 -- Go to start of new line
1525 procedure Print80 (S : String);
1526 -- Print the text, respecting the 80 columns rule
1528 procedure Print_Ref (Line, Column : String);
1529 -- The beginning of the output is aligned on a column multiple of 9
1531 procedure Print_List
1532 (Decl : Declaration_Reference;
1534 Get_Reads : Boolean := False;
1535 Get_Writes : Boolean := False;
1536 Get_Bodies : Boolean := False);
1537 -- Print a list of references. If the list is not empty, Msg will
1538 -- be printed prior to the list.
1544 procedure New_Line80 is
1547 Write_Str (String'(1 .. Margin
- 1 => ' '));
1554 procedure Print80
(S
: String) is
1555 Align
: Natural := Margin
- (Integer (Column
) mod Margin
);
1558 if Align
= Margin
then
1562 Write_Str
(String'(1 .. Align => ' ') & S);
1569 procedure Print_Ref (Line, Column : String) is
1570 Line_Align : constant Integer := 4 - Line'Length;
1572 S : constant String := String'(1 .. Line_Align
=> ' ')
1573 & Line
& ':' & Column
;
1575 Align
: Natural := Margin
- (Integer (Output
.Column
) mod Margin
);
1578 if Align
= Margin
then
1582 if Integer (Output
.Column
) + Align
+ S
'Length > 79 then
1587 Write_Str
(String'(1 .. Align => ' ') & S);
1594 procedure Print_List
1595 (Decl : Declaration_Reference;
1597 Get_Reads : Boolean := False;
1598 Get_Writes : Boolean := False;
1599 Get_Bodies : Boolean := False)
1601 Arr : Reference_Array_Access :=
1604 Get_Writes => Get_Writes,
1605 Get_Reads => Get_Reads,
1606 Get_Bodies => Get_Bodies);
1607 File : File_Reference := Empty_File;
1611 if Arr'Length /= 0 then
1616 for R in Arr'Range loop
1617 if Get_File_Ref (Arr (R)) /= File then
1618 if File /= Empty_File then
1622 File := Get_File_Ref (Arr (R));
1623 F := Osint.To_Host_File_Spec
1624 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1627 Write_Str ("<unknown> ");
1629 Write_Str (F.all & ' ');
1634 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1642 -- Start of processing for Print_Xref
1645 for D in Decls'Range loop
1648 Write_Str (Get_Symbol (Decl));
1650 -- Put the declaration type in column Type_Position, but if the
1651 -- declaration name is too long, put at least one space between its
1652 -- name and its type.
1654 while Column < Type_Position - 1 loop
1660 Write_Line (Get_Full_Type (Decl));
1662 Write_Parent_Info : declare
1663 Parent : constant Declaration_Reference := Get_Parent (Decl);
1666 if Parent /= Empty_Declaration then
1667 Write_Str (" Ptype: ");
1668 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1671 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1672 Print80 (" " & Get_Symbol (Parent));
1675 end Write_Parent_Info;
1677 Write_Str (" Decl: ");
1678 F := Osint.To_Host_File_Spec
1679 (Get_Gnatchop_File (Decl, Full_Path_Name));
1682 Print80 ("<unknown> ");
1684 Print80 (F.all & ' ');
1688 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1691 (Decl, " Body: ", Get_Bodies => True);
1693 (Decl, " Modi: ", Get_Writes => True);
1695 (Decl, " Ref: ", Get_Reads => True);
1705 (Pattern : Search_Pattern;
1706 Local_Symbols : Boolean;
1707 Wide_Search : Boolean;
1708 Read_Only : Boolean;
1710 Type_Tree : Boolean)
1712 type String_Access is access String;
1713 procedure Free is new Unchecked_Deallocation (String, String_Access);
1716 File_Ref : File_Reference;
1717 Strip_Num : Natural := 0;
1718 Ali_Name : String_Access;
1721 -- If we want all the .ali files, then find them
1728 -- Get the next unread ali file
1730 File_Ref := Next_Unvisited_File;
1732 exit when File_Ref = Empty_File;
1734 -- Find the ALI file to use. Most of the time, it will be the unit
1735 -- name, with a different extension. However, when dealing with
1736 -- separates the ALI file is in fact the parent's ALI file (and this
1737 -- is recursive, in case the parent itself is a separate).
1742 Ali_Name := new String'
1743 (Get_File
(File_Ref
, With_Dir
=> True, Strip
=> Strip_Num
));
1745 -- Stripped too many things...
1747 if Ali_Name
.all = "" then
1748 if Get_Emit_Warning
(File_Ref
) then
1751 ("warning : file " & Get_File
(File_Ref
, With_Dir
=> True)
1753 Set_Standard_Output
;
1758 -- If not found, try the parent's ALI file (this is needed for
1759 -- separate units and subprograms).
1761 -- Reset the cached directory first, in case the separate's
1762 -- ALI file is not in the same directory.
1764 elsif not File_Exists
(Ali_Name
.all) then
1765 Strip_Num
:= Strip_Num
+ 1;
1766 Reset_Directory
(File_Ref
);
1768 -- Else we finally found it
1775 -- If we had to get the parent's ALI, insert it in the list as usual.
1776 -- This is to avoid parsing it twice in case it has already been
1779 if Ali_Name
/= null and then Strip_Num
/= 0 then
1780 File_Ref
:= Add_To_Xref_File
1781 (File_Name
=> Ali_Name
.all,
1784 -- Now that we have a file name, parse it to find any reference to
1787 elsif Ali_Name
/= null
1788 and then (Read_Only
or else Is_Writable_File
(Ali_Name
.all))
1791 Open
(Ali_Name
.all, ALIfile
);
1793 -- The cross-reference section in the ALI file may be followed
1794 -- by other sections, which can be identified by the starting
1795 -- character of every line, which should neither be 'X' nor a
1796 -- figure in '1' .. '9'.
1798 -- The loop tests below also take into account the end-of-file
1801 while ALIfile
.Buffer
(ALIfile
.Current_Line
) = 'X' loop
1802 Parse_X_Filename
(ALIfile
);
1804 while ALIfile
.Buffer
(ALIfile
.Current_Line
) in '1' .. '9'
1806 Parse_Identifier_Info
1807 (Pattern
, ALIfile
, Local_Symbols
, Der_Info
, Type_Tree
,
1808 Wide_Search
, Labels_As_Ref
=> True);
1813 when No_Xref_Information
=>
1814 if Get_Emit_Warning
(File_Ref
) then
1817 ("warning : No cross-referencing information in "
1819 Set_Standard_Output
;
1832 procedure Search_Xref
1833 (Local_Symbols
: Boolean;
1834 Read_Only
: Boolean;
1838 File_Ref
: File_Reference
;
1839 Null_Pattern
: Search_Pattern
;
1842 Null_Pattern
.Initialized
:= False;
1845 -- Find the next unvisited file
1847 File_Ref
:= Next_Unvisited_File
;
1848 exit when File_Ref
= Empty_File
;
1850 -- Search the object directories for the .ali file
1853 F
: constant String := Get_File
(File_Ref
, With_Dir
=> True);
1856 if Read_Only
or else Is_Writable_File
(F
) then
1857 Open
(F
, ALIfile
, True);
1859 -- The cross-reference section in the ALI file may be followed
1860 -- by other sections, which can be identified by the starting
1861 -- character of every line, which should neither be 'X' nor a
1862 -- figure in '1' .. '9'.
1864 -- The loop tests below also take into account the end-of-file
1867 while ALIfile
.Buffer
(ALIfile
.Current_Line
) = 'X' loop
1868 Parse_X_Filename
(ALIfile
);
1870 while ALIfile
.Buffer
(ALIfile
.Current_Line
) in '1' .. '9'
1872 Parse_Identifier_Info
1873 (Null_Pattern
, ALIfile
, Local_Symbols
, Der_Info
,
1874 Labels_As_Ref
=> False);
1880 when No_Xref_Information
=> null;