1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2016, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Output
; use Output
;
28 with Types
; use Types
;
30 with Unchecked_Deallocation
;
32 with Ada
.Strings
.Fixed
; use Ada
.Strings
.Fixed
;
33 with Ada
.Text_IO
; use Ada
.Text_IO
;
35 with GNAT
.Command_Line
; use GNAT
.Command_Line
;
36 with GNAT
.IO_Aux
; use GNAT
.IO_Aux
;
38 package body Xref_Lib
is
40 Type_Position
: constant := 50;
41 -- Column for label identifying type of entity
47 Pipe
: constant Character := '|';
48 -- First character on xref lines in the .ali file
50 No_Xref_Information
: exception;
51 -- Exception raised when there is no cross-referencing information in
55 (Source
: not null access String;
56 Ptr
: in out Positive;
57 Skip_Continuation_Line
: Boolean := False);
58 -- On return Source (Ptr) is the first character of the next line
59 -- or EOF. Source.all must be terminated by EOF.
61 -- If Skip_Continuation_Line is True, this subprogram skips as many
62 -- lines as required when the second or more lines starts with '.'
63 -- (continuation lines in ALI files).
65 function Current_Xref_File
(File
: ALI_File
) return File_Reference
;
66 -- Return the file matching the last 'X' line we found while parsing
69 function File_Name
(File
: ALI_File
; Num
: Positive) return File_Reference
;
70 -- Returns the dependency file name number Num
72 function Get_Full_Type
(Decl
: Declaration_Reference
) return String;
73 -- Returns the full type corresponding to a type letter as found in
79 Dependencies
: Boolean := False);
80 -- Open a new ALI file. If Dependencies is True, the insert every library
81 -- file 'with'ed in the files database (used for gnatxref)
83 procedure Parse_Identifier_Info
84 (Pattern
: Search_Pattern
;
85 File
: in out ALI_File
;
86 Local_Symbols
: Boolean;
87 Der_Info
: Boolean := False;
88 Type_Tree
: Boolean := False;
89 Wide_Search
: Boolean := True;
90 Labels_As_Ref
: Boolean := True);
91 -- Output the file and the line where the identifier was referenced,
92 -- If Local_Symbols is False then only the publicly visible symbols
95 -- If Labels_As_Ref is true, then the references to the entities after
96 -- the end statements ("end Foo") will be counted as actual references.
97 -- The entity will never be reported as unreferenced by gnatxref -u
100 (Source
: not null access String;
101 Ptr
: in out Positive;
102 Token_Ptr
: out Positive);
103 -- Skips any separators and stores the start of the token in Token_Ptr.
104 -- Then stores the position of the next separator in Ptr. On return
105 -- Source (Token_Ptr .. Ptr - 1) is the token. Separators are space
106 -- and ASCII.HT. Parse_Token will never skip to the next line.
108 procedure Parse_Number
109 (Source
: not null access String;
110 Ptr
: in out Positive;
111 Number
: out Natural);
112 -- Skips any separators and parses Source up to the first character that
113 -- is not a decimal digit. Returns value of parsed digits or 0 if none.
115 procedure Parse_X_Filename
(File
: in out ALI_File
);
116 -- Reads and processes "X..." lines in the ALI file
117 -- and updates the File.X_File information.
119 procedure Skip_To_First_X_Line
120 (File
: in out ALI_File
;
123 -- Skip the lines in the ALI file until the first cross-reference line
124 -- (^X...) is found. Search is started from the beginning of the file.
125 -- If not such line is found, No_Xref_Information is raised.
126 -- If W_Lines is false, then the lines "^W" are not parsed.
127 -- If D_Lines is false, then the lines "^D" are not parsed.
134 (Pattern
: in out Search_Pattern
;
136 Glob
: Boolean := False)
138 File_Start
: Natural;
139 Line_Start
: Natural;
141 Line_Num
: Natural := 0;
142 Col_Num
: Natural := 0;
144 File_Ref
: File_Reference
:= Empty_File
;
145 pragma Warnings
(Off
, File_Ref
);
148 -- Find the end of the first item in Entity (pattern or file?)
149 -- If there is no ':', we only have a pattern
151 File_Start
:= Index
(Entity
, ":");
153 -- If the regular expression is invalid, just consider it as a string
155 if File_Start
= 0 then
157 Pattern
.Entity
:= Compile
(Entity
, Glob
, False);
158 Pattern
.Initialized
:= True;
161 when Error_In_Regexp
=>
163 -- The basic idea is to insert a \ before every character
166 Tmp_Regexp
: String (1 .. 2 * Entity
'Length);
167 Index
: Positive := 1;
170 for J
in Entity
'Range loop
171 Tmp_Regexp
(Index
) := '\';
172 Tmp_Regexp
(Index
+ 1) := Entity
(J
);
176 Pattern
.Entity
:= Compile
(Tmp_Regexp
, True, False);
177 Pattern
.Initialized
:= True;
181 Set_Default_Match
(True);
185 -- If there is a dot in the pattern, then it is a file name
188 Index
(Entity
(Entity
'First .. File_Start
- 1), ".") /= 0)
191 and then Index
(Entity
(Entity
'First .. File_Start
- 1),
194 Pattern
.Entity
:= Compile
(".*", False);
195 Pattern
.Initialized
:= True;
196 File_Start
:= Entity
'First;
199 -- If the regular expression is invalid, just consider it as a string
203 Compile
(Entity
(Entity
'First .. File_Start
- 1), Glob
, False);
204 Pattern
.Initialized
:= True;
207 when Error_In_Regexp
=>
209 -- The basic idea is to insert a \ before every character
212 Tmp_Regexp
: String (1 .. 2 * (File_Start
- Entity
'First));
213 Index
: Positive := 1;
216 for J
in Entity
'First .. File_Start
- 1 loop
217 Tmp_Regexp
(Index
) := '\';
218 Tmp_Regexp
(Index
+ 1) := Entity
(J
);
222 Pattern
.Entity
:= Compile
(Tmp_Regexp
, True, False);
223 Pattern
.Initialized
:= True;
227 File_Start
:= File_Start
+ 1;
230 -- Parse the file name
232 Line_Start
:= Index
(Entity
(File_Start
.. Entity
'Last), ":");
234 -- Check if it was a disk:\directory item (for 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
405 return File
.Dep
.Table
(Num
);
412 procedure Find_ALI_Files
is
414 Dir_Ent
: File_Name_String
;
417 File_Ref
: File_Reference
;
418 pragma Unreferenced
(File_Ref
);
420 function Open_Next_Dir
return Boolean;
421 -- Tries to open the next object directory, and return False if
422 -- the directory cannot be opened.
428 function Open_Next_Dir
return Boolean is
430 -- Until we are able to open a new directory
434 Obj_Dir
: constant String := Next_Obj_Dir
;
437 -- Case of no more Obj_Dir lines
439 if Obj_Dir
'Length = 0 then
443 Open
(My_Dir
.Dir
, Obj_Dir
);
448 -- Could not open the directory
450 when Directory_Error
=> null;
457 -- Start of processing for Find_ALI_Files
462 if Open_Next_Dir
then
464 Read
(My_Dir
.Dir
, Dir_Ent
, Last
);
469 if not Open_Next_Dir
then
474 and then Dir_Ent
(Last
- 3 .. Last
) = "." & Osint
.ALI_Suffix
.all
477 Add_To_Xref_File
(Dir_Ent
(1 .. Last
), Visited
=> False);
487 function Get_Full_Type
(Decl
: Declaration_Reference
) return String is
489 function Param_String
return String;
490 -- Return the string to display depending on whether Decl is a parameter
496 function Param_String
return String is
498 if Is_Parameter
(Decl
) then
505 -- Start of processing for Get_Full_Type
508 case Get_Type
(Decl
) is
509 when 'A' => return "array type";
510 when 'B' => return "boolean type";
511 when 'C' => return "class-wide type";
512 when 'D' => return "decimal type";
513 when 'E' => return "enumeration type";
514 when 'F' => return "float type";
515 when 'H' => return "abstract type";
516 when 'I' => return "integer type";
517 when 'M' => return "modular type";
518 when 'O' => return "fixed type";
519 when 'P' => return "access type";
520 when 'R' => return "record type";
521 when 'S' => return "string type";
522 when 'T' => return "task type";
523 when 'W' => return "protected type";
525 when 'a' => return Param_String
& "array object";
526 when 'b' => return Param_String
& "boolean object";
527 when 'c' => return Param_String
& "class-wide object";
528 when 'd' => return Param_String
& "decimal object";
529 when 'e' => return Param_String
& "enumeration object";
530 when 'f' => return Param_String
& "float object";
531 when 'i' => return Param_String
& "integer object";
532 when 'j' => return Param_String
& "class object";
533 when 'm' => return Param_String
& "modular object";
534 when 'o' => return Param_String
& "fixed object";
535 when 'p' => return Param_String
& "access object";
536 when 'r' => return Param_String
& "record object";
537 when 's' => return Param_String
& "string object";
538 when 't' => return Param_String
& "task object";
539 when 'w' => return Param_String
& "protected object";
540 when 'x' => return Param_String
& "abstract procedure";
541 when 'y' => return Param_String
& "abstract function";
543 when 'h' => return "interface";
544 when 'g' => return "macro";
545 when 'G' => return "function macro";
546 when 'J' => return "class";
547 when 'K' => return "package";
548 when 'k' => return "generic package";
549 when 'L' => return "statement label";
550 when 'l' => return "loop label";
551 when 'N' => return "named number";
552 when 'n' => return "enumeration literal";
553 when 'q' => return "block label";
554 when 'Q' => return "include file";
555 when 'U' => return "procedure";
556 when 'u' => return "generic procedure";
557 when 'V' => return "function";
558 when 'v' => return "generic function";
559 when 'X' => return "exception";
560 when 'Y' => return "entry";
562 when '+' => return "private type";
563 when '*' => return "private variable";
565 -- The above should be the only possibilities, but for this kind
566 -- of informational output, we don't want to bomb if we find
567 -- something else, so just return three question marks when we
568 -- have an unknown Abbrev value
571 if Is_Parameter
(Decl
) then
574 return "??? (" & Get_Type
(Decl
) & ")";
579 --------------------------
580 -- Skip_To_First_X_Line --
581 --------------------------
583 procedure Skip_To_First_X_Line
584 (File
: in out ALI_File
;
588 Ali
: String_Access
renames File
.Buffer
;
590 Ptr
: Positive := Ali
'First;
591 Num_Dependencies
: Natural := 0;
592 File_Start
: Positive;
594 Gnatchop_Offset
: Integer;
595 Gnatchop_Name
: Positive;
597 File_Ref
: File_Reference
;
598 pragma Unreferenced
(File_Ref
);
601 -- Read all the lines possibly processing with-clauses and dependency
602 -- information and exit on finding the first Xref line.
603 -- A fall-through of the loop means that there is no xref information
604 -- which is an error condition.
606 while Ali
(Ptr
) /= EOF
loop
607 if D_Lines
and then Ali
(Ptr
) = 'D' then
609 -- Found dependency information. Format looks like:
610 -- D src-nam time-stmp checksum [subunit-name] [line:file-name]
612 -- Skip the D and parse the filenam
615 Parse_Token
(Ali
, Ptr
, Token
);
619 Num_Dependencies
:= Num_Dependencies
+ 1;
620 Set_Last
(File
.Dep
, Num_Dependencies
);
622 Parse_Token
(Ali
, Ptr
, Token
); -- Skip time-stamp
623 Parse_Token
(Ali
, Ptr
, Token
); -- Skip checksum
624 Parse_Token
(Ali
, Ptr
, Token
); -- Read next entity on the line
626 if not (Ali
(Token
) in '0' .. '9') then
627 Parse_Token
(Ali
, Ptr
, Token
); -- Was a subunit name
630 -- Did we have a gnatchop-ed file with a pragma Source_Reference ?
632 Gnatchop_Offset
:= 0;
634 if Ali
(Token
) in '0' .. '9' then
635 Gnatchop_Name
:= Token
;
636 while Ali
(Gnatchop_Name
) /= ':' loop
637 Gnatchop_Name
:= Gnatchop_Name
+ 1;
641 2 - Natural'Value (Ali
(Token
.. Gnatchop_Name
- 1));
642 Token
:= Gnatchop_Name
+ 1;
645 File
.Dep
.Table
(Num_Dependencies
) := Add_To_Xref_File
646 (Ali
(File_Start
.. File_End
),
647 Gnatchop_File
=> Ali
(Token
.. Ptr
- 1),
648 Gnatchop_Offset
=> Gnatchop_Offset
);
650 elsif W_Lines
and then Ali
(Ptr
) = 'W' then
652 -- Found with-clause information. Format looks like:
653 -- "W debug%s debug.adb debug.ali"
655 -- Skip the W and parse the .ali filename (3rd token)
657 Parse_Token
(Ali
, Ptr
, Token
);
658 Parse_Token
(Ali
, Ptr
, Token
);
659 Parse_Token
(Ali
, Ptr
, Token
);
662 Add_To_Xref_File
(Ali
(Token
.. Ptr
- 1), Visited
=> False);
664 elsif Ali
(Ptr
) = 'X' then
666 -- Found a cross-referencing line - stop processing
668 File
.Current_Line
:= Ptr
;
669 File
.Xref_Line
:= Ptr
;
673 Parse_EOL
(Ali
, Ptr
);
676 raise No_Xref_Information
;
677 end Skip_To_First_X_Line
;
686 Dependencies
: Boolean := False)
688 Ali
: String_Access
renames File
.Buffer
;
689 pragma Warnings
(Off
, Ali
);
692 if File
.Buffer
/= null then
699 Read_File
(Name
, Ali
);
702 when Ada
.Text_IO
.Name_Error | Ada
.Text_IO
.End_Error
=>
703 raise No_Xref_Information
;
706 Skip_To_First_X_Line
(File
, D_Lines
=> True, W_Lines
=> Dependencies
);
714 (Source
: not null access String;
715 Ptr
: in out Positive;
716 Skip_Continuation_Line
: Boolean := False)
720 -- Skip to end of line
722 while Source
(Ptr
) /= ASCII
.CR
and then Source
(Ptr
) /= ASCII
.LF
723 and then Source
(Ptr
) /= EOF
728 -- Skip CR or LF if not at end of file
730 if Source
(Ptr
) /= EOF
then
734 -- Skip past CR/LF or LF/CR combination
736 if (Source
(Ptr
) = ASCII
.CR
or else Source
(Ptr
) = ASCII
.LF
)
737 and then Source
(Ptr
) /= Source
(Ptr
- 1)
742 exit when not Skip_Continuation_Line
or else Source
(Ptr
) /= '.';
746 ---------------------------
747 -- Parse_Identifier_Info --
748 ---------------------------
750 procedure Parse_Identifier_Info
751 (Pattern
: Search_Pattern
;
752 File
: in out ALI_File
;
753 Local_Symbols
: Boolean;
754 Der_Info
: Boolean := False;
755 Type_Tree
: Boolean := False;
756 Wide_Search
: Boolean := True;
757 Labels_As_Ref
: Boolean := True)
759 Ptr
: Positive renames File
.Current_Line
;
760 Ali
: String_Access
renames File
.Buffer
;
762 E_Line
: Natural; -- Line number of current entity
763 E_Col
: Natural; -- Column number of current entity
764 E_Type
: Character; -- Type of current entity
765 E_Name
: Positive; -- Pointer to begin of entity name
766 E_Global
: Boolean; -- True iff entity is global
768 R_Line
: Natural; -- Line number of current reference
769 R_Col
: Natural; -- Column number of current reference
770 R_Type
: Character; -- Type of current reference
772 Decl_Ref
: Declaration_Reference
;
773 File_Ref
: File_Reference
:= Current_Xref_File
(File
);
775 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String;
776 -- Returns the symbol name for the entity defined at the specified
777 -- line and column in the dependent unit number Eun. For this we need
778 -- to parse the ali file again because the parent entity is not in
779 -- the declaration table if it did not match the search pattern.
781 procedure Skip_To_Matching_Closing_Bracket
;
782 -- When Ptr points to an opening square bracket, moves it to the
783 -- character following the matching closing bracket
785 ---------------------
786 -- Get_Symbol_Name --
787 ---------------------
789 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String is
791 E_Eun
: Positive; -- Unit number of current entity
792 E_Line
: Natural; -- Line number of current entity
793 E_Col
: Natural; -- Column number of current entity
794 E_Name
: Positive; -- Pointer to begin of entity name
797 -- Look for the X lines corresponding to unit Eun
800 if Ali
(Ptr
) = 'X' then
802 Parse_Number
(Ali
, Ptr
, E_Eun
);
803 exit when E_Eun
= Eun
;
806 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
809 -- Here we are in the right Ali section, we now look for the entity
810 -- declared at position (Line, Col).
813 Parse_Number
(Ali
, Ptr
, E_Line
);
814 exit when Ali
(Ptr
) = EOF
;
816 Parse_Number
(Ali
, Ptr
, E_Col
);
817 exit when Ali
(Ptr
) = EOF
;
820 if Line
= E_Line
and then Col
= E_Col
then
821 Parse_Token
(Ali
, Ptr
, E_Name
);
822 return Ali
(E_Name
.. Ptr
- 1);
825 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
826 exit when Ali
(Ptr
) = EOF
;
829 -- We were not able to find the symbol, this should not happen but
830 -- since we don't want to stop here we return a string of three
831 -- question marks as the symbol name.
836 --------------------------------------
837 -- Skip_To_Matching_Closing_Bracket --
838 --------------------------------------
840 procedure Skip_To_Matching_Closing_Bracket
is
841 Num_Brackets
: Natural;
845 while Num_Brackets
/= 0 loop
847 if Ali
(Ptr
) = '[' then
848 Num_Brackets
:= Num_Brackets
+ 1;
849 elsif Ali
(Ptr
) = ']' then
850 Num_Brackets
:= Num_Brackets
- 1;
855 end Skip_To_Matching_Closing_Bracket
;
857 -- Start of processing for Parse_Identifier_Info
860 -- The identifier info looks like:
861 -- "38U9*Debug 12|36r6 36r19"
863 -- Extract the line, column and entity name information
865 Parse_Number
(Ali
, Ptr
, E_Line
);
867 if Ali
(Ptr
) > ' ' then
872 -- Ignore some of the entities (labels,...)
875 when 'l' |
'L' |
'q' =>
876 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
883 Parse_Number
(Ali
, Ptr
, E_Col
);
886 if Ali
(Ptr
) >= ' ' then
887 E_Global
:= (Ali
(Ptr
) = '*');
891 Parse_Token
(Ali
, Ptr
, E_Name
);
893 -- Exit if the symbol does not match or if we have a local symbol and we
894 -- do not want it or if the file is unknown.
896 if File
.X_File
= Empty_File
then
900 if (not Local_Symbols
and not E_Global
)
901 or else (Pattern
.Initialized
902 and then not Match
(Ali
(E_Name
.. Ptr
- 1), Pattern
.Entity
))
903 or else (E_Name
>= Ptr
)
905 Decl_Ref
:= Add_Declaration
906 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
,
907 Remove_Only
=> True);
908 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
912 -- Insert the declaration in the table
914 Decl_Ref
:= Add_Declaration
915 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
);
917 if Ali
(Ptr
) = '[' then
918 Skip_To_Matching_Closing_Bracket
;
921 -- Skip any renaming indication
923 if Ali
(Ptr
) = '=' then
925 P_Line
, P_Column
: Natural;
926 pragma Warnings
(Off
, P_Line
);
927 pragma Warnings
(Off
, P_Column
);
930 Parse_Number
(Ali
, Ptr
, P_Line
);
932 Parse_Number
(Ali
, Ptr
, P_Column
);
936 while Ptr
<= Ali
'Last
937 and then (Ali
(Ptr
) = '<'
938 or else Ali
(Ptr
) = '('
939 or else Ali
(Ptr
) = '{')
941 -- Here we have a type derivation information. The format is
942 -- <3|12I45> which means that the current entity is derived from the
943 -- type defined in unit number 3, line 12 column 45. The pipe and
944 -- unit number is optional. It is specified only if the parent type
945 -- is not defined in the current unit.
947 -- We also have the format for generic instantiations, as in
948 -- 7a5*Uid(3|5I8[4|2]) 2|4r74
950 -- We could also have something like
952 -- that indicates that I derives from the predefined type integer.
956 if Ali
(Ptr
) in '0' .. '9' then
957 Parse_Derived_Info
: declare
958 P_Line
: Natural; -- parent entity line
959 P_Column
: Natural; -- parent entity column
960 P_Eun
: Positive; -- parent entity file number
963 Parse_Number
(Ali
, Ptr
, P_Line
);
965 -- If we have a pipe then the first number was the unit number
967 if Ali
(Ptr
) = '|' then
971 -- Now we have the line number
973 Parse_Number
(Ali
, Ptr
, P_Line
);
976 -- We don't have a unit number specified, so we set P_Eun to
979 for K
in Dependencies_Tables
.First
.. Last
(File
.Dep
) loop
981 exit when File
.Dep
.Table
(K
) = File_Ref
;
985 -- Then parse the type and column number
988 Parse_Number
(Ali
, Ptr
, P_Column
);
990 -- Skip the information for generics instantiations
992 if Ali
(Ptr
) = '[' then
993 Skip_To_Matching_Closing_Bracket
;
996 -- Skip '>', or ')' or '>'
1000 -- The derived info is needed only is the derived info mode is
1001 -- on or if we want to output the type hierarchy
1003 if Der_Info
or else Type_Tree
then
1005 Symbol
: constant String :=
1006 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
);
1008 if Symbol
/= "???" then
1014 File
.Dep
.Table
(P_Eun
));
1020 and then (Pattern
.File_Ref
= Empty_File
1022 Pattern
.File_Ref
= Current_Xref_File
(File
))
1024 Search_Parent_Tree
: declare
1025 Pattern
: Search_Pattern
; -- Parent type pattern
1026 File_Pos_Backup
: Positive;
1031 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
)
1032 & ':' & Get_Gnatchop_File
(File
.Dep
.Table
(P_Eun
))
1033 & ':' & Get_Line
(Get_Parent
(Decl_Ref
))
1034 & ':' & Get_Column
(Get_Parent
(Decl_Ref
)),
1037 -- No default match is needed to look for the parent type
1038 -- since we are using the fully qualified symbol name:
1039 -- symbol:file:line:column
1041 Set_Default_Match
(False);
1043 -- The parent hierarchy is defined in the same unit as
1044 -- the derived type. So we want to revisit the unit.
1046 File_Pos_Backup
:= File
.Current_Line
;
1048 Skip_To_First_X_Line
1049 (File
, D_Lines
=> False, W_Lines
=> False);
1051 while File
.Buffer
(File
.Current_Line
) /= EOF
loop
1052 Parse_X_Filename
(File
);
1053 Parse_Identifier_Info
1054 (Pattern
=> Pattern
,
1056 Local_Symbols
=> False,
1057 Der_Info
=> Der_Info
,
1059 Wide_Search
=> False,
1060 Labels_As_Ref
=> Labels_As_Ref
);
1063 File
.Current_Line
:= File_Pos_Backup
;
1064 end Search_Parent_Tree
;
1066 end Parse_Derived_Info
;
1069 while Ali
(Ptr
) /= '>'
1070 and then Ali
(Ptr
) /= ')'
1071 and then Ali
(Ptr
) /= '}'
1079 -- To find the body, we will have to parse the file too
1083 File_Ref
: File_Reference
;
1084 pragma Unreferenced
(File_Ref
);
1085 File_Name
: constant String := Get_Gnatchop_File
(File
.X_File
);
1087 File_Ref
:= Add_To_Xref_File
(ALI_File_Name
(File_Name
), False);
1091 -- Parse references to this entity.
1092 -- Ptr points to next reference with leading blanks
1095 -- Process references on current line
1097 while Ali
(Ptr
) = ' ' or else Ali
(Ptr
) = ASCII
.HT
loop
1099 -- For every reference read the line, type and column,
1100 -- optionally preceded by a file number and a pipe symbol.
1102 Parse_Number
(Ali
, Ptr
, R_Line
);
1104 if Ali
(Ptr
) = Pipe
then
1106 File_Ref
:= File_Name
(File
, R_Line
);
1108 Parse_Number
(Ali
, Ptr
, R_Line
);
1111 if Ali
(Ptr
) > ' ' then
1112 R_Type
:= Ali
(Ptr
);
1116 -- Imported entities may have an indication specifying information
1117 -- about the corresponding external name:
1118 -- 5U14*Foo2 5>20 6b<c,myfoo2>22 # Imported entity
1119 -- 5U14*Foo2 5>20 6i<c,myfoo2>22 # Exported entity
1121 if (R_Type
= 'b' or else R_Type
= 'i')
1122 and then Ali
(Ptr
) = '<'
1124 while Ptr
<= Ali
'Last
1125 and then Ali
(Ptr
) /= '>'
1132 Parse_Number
(Ali
, Ptr
, R_Col
);
1134 -- Insert the reference or body in the table
1137 (Decl_Ref
, File_Ref
, R_Line
, R_Col
, R_Type
, Labels_As_Ref
);
1139 -- Skip generic information, if any
1141 if Ali
(Ptr
) = '[' then
1143 Num_Nested
: Integer := 1;
1147 while Num_Nested
/= 0 loop
1148 if Ali
(Ptr
) = ']' then
1149 Num_Nested
:= Num_Nested
- 1;
1150 elsif Ali
(Ptr
) = '[' then
1151 Num_Nested
:= Num_Nested
+ 1;
1161 Parse_EOL
(Ali
, Ptr
);
1163 -- Loop until new line is no continuation line
1165 exit when Ali
(Ptr
) /= '.';
1168 end Parse_Identifier_Info
;
1174 procedure Parse_Number
1175 (Source
: not null access String;
1176 Ptr
: in out Positive;
1177 Number
: out Natural)
1182 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1187 while Source
(Ptr
) in '0' .. '9' loop
1189 10 * Number
+ (Character'Pos (Source
(Ptr
)) - Character'Pos ('0'));
1198 procedure Parse_Token
1199 (Source
: not null access String;
1200 Ptr
: in out Positive;
1201 Token_Ptr
: out Positive)
1203 In_Quotes
: Character := ASCII
.NUL
;
1208 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1214 -- Find end-of-token
1216 while (In_Quotes
/= ASCII
.NUL
or else
1217 not (Source
(Ptr
) = ' '
1218 or else Source
(Ptr
) = ASCII
.HT
1219 or else Source
(Ptr
) = '<'
1220 or else Source
(Ptr
) = '{'
1221 or else Source
(Ptr
) = '['
1222 or else Source
(Ptr
) = '='
1223 or else Source
(Ptr
) = '('))
1224 and then Source
(Ptr
) >= ' '
1226 -- Double-quotes are used for operators
1227 -- Simple-quotes are used for character constants, for instance when
1228 -- they are found in an enumeration type "type A is ('+', '-');"
1230 case Source
(Ptr
) is
1232 if In_Quotes
= Source
(Ptr
) then
1233 In_Quotes
:= ASCII
.NUL
;
1234 elsif In_Quotes
= ASCII
.NUL
then
1235 In_Quotes
:= Source
(Ptr
);
1246 ----------------------
1247 -- Parse_X_Filename --
1248 ----------------------
1250 procedure Parse_X_Filename
(File
: in out ALI_File
) is
1251 Ali
: String_Access
renames File
.Buffer
;
1252 Ptr
: Positive renames File
.Current_Line
;
1256 while Ali
(Ptr
) = 'X' loop
1258 -- The current line is the start of a new Xref file section,
1259 -- whose format looks like:
1263 -- Skip the X and read the file number for the new X_File
1266 Parse_Number
(Ali
, Ptr
, File_Nr
);
1268 -- If the referenced file is unknown, we simply ignore it
1270 if File_Nr
in Dependencies_Tables
.First
.. Last
(File
.Dep
) then
1271 File
.X_File
:= File
.Dep
.Table
(File_Nr
);
1273 File
.X_File
:= Empty_File
;
1276 Parse_EOL
(Ali
, Ptr
);
1278 end Parse_X_Filename
;
1280 --------------------
1281 -- Print_Gnatfind --
1282 --------------------
1284 procedure Print_Gnatfind
1285 (References
: Boolean;
1286 Full_Path_Name
: Boolean)
1288 Decls
: constant Declaration_Array_Access
:= Get_Declarations
;
1289 Decl
: Declaration_Reference
;
1290 Arr
: Reference_Array_Access
;
1294 Msg
: String := " ");
1295 -- Print a reference, according to the extended tag of the output
1303 Msg
: String := " ")
1305 F
: String_Access
:=
1306 Osint
.To_Host_File_Spec
1307 (Get_Gnatchop_File
(Ref
, Full_Path_Name
));
1309 Buffer
: constant String :=
1311 ":" & Get_Line
(Ref
) &
1312 ":" & Get_Column
(Ref
) &
1315 Num_Blanks
: Integer := Longest_File_Name
+ 10 - Buffer
'Length;
1319 Num_Blanks
:= Integer'Max (0, Num_Blanks
);
1322 & String'(1 .. Num_Blanks => ' ')
1323 & Msg & " " & Get_Symbol (Decl));
1325 if Get_Source_Line (Ref)'Length /= 0 then
1326 Write_Line (" " & Get_Source_Line (Ref));
1330 -- Start of processing for Print_Gnatfind
1333 for D in Decls'Range loop
1336 if Match (Decl) then
1338 -- Output the declaration
1341 Parent : constant Declaration_Reference := Get_Parent (Decl);
1343 F : String_Access :=
1344 Osint.To_Host_File_Spec
1345 (Get_Gnatchop_File (Decl, Full_Path_Name));
1347 Buffer : constant String :=
1349 ":" & Get_Line (Decl) &
1350 ":" & Get_Column (Decl) &
1353 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1357 Num_Blanks := Integer'Max (0, Num_Blanks);
1359 (Buffer & String'(1 .. Num_Blanks
=> ' ')
1360 & "(spec) " & Get_Symbol
(Decl
));
1362 if Parent
/= Empty_Declaration
then
1363 F
:= Osint
.To_Host_File_Spec
(Get_Gnatchop_File
(Parent
));
1365 (Buffer
& String'(1 .. Num_Blanks => ' ')
1366 & " derived from " & Get_Symbol (Parent)
1369 & ':' & Get_Line (Parent)
1370 & ':' & Get_Column (Parent) & ')');
1375 if Get_Source_Line (Decl)'Length /= 0 then
1376 Write_Line (" " & Get_Source_Line (Decl));
1379 -- Output the body (sorted)
1381 Arr := Get_References (Decl, Get_Bodies => True);
1383 for R in Arr'Range loop
1384 Print_Ref (Arr (R), "(body)");
1390 Arr := Get_References
1391 (Decl, Get_Writes => True, Get_Reads => True);
1393 for R in Arr'Range loop
1394 Print_Ref (Arr (R));
1407 procedure Print_Unused (Full_Path_Name : Boolean) is
1408 Decls : constant Declaration_Array_Access := Get_Declarations;
1409 Decl : Declaration_Reference;
1410 Arr : Reference_Array_Access;
1414 for D in Decls'Range loop
1418 (Decl, Get_Reads => True, Get_Writes => True) = 0
1420 F := Osint.To_Host_File_Spec
1421 (Get_Gnatchop_File (Decl, Full_Path_Name));
1422 Write_Str (Get_Symbol (Decl)
1424 & Get_Full_Type (Decl)
1430 & Get_Column (Decl));
1433 -- Print the body if any
1435 Arr := Get_References (Decl, Get_Bodies => True);
1437 for R in Arr'Range loop
1438 F := Osint.To_Host_File_Spec
1439 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1442 & ':' & Get_Line (Arr (R))
1443 & ':' & Get_Column (Arr (R)));
1457 procedure Print_Vi (Full_Path_Name : Boolean) is
1458 Tab : constant Character := ASCII.HT;
1459 Decls : constant Declaration_Array_Access :=
1460 Get_Declarations (Sorted => False);
1461 Decl : Declaration_Reference;
1462 Arr : Reference_Array_Access;
1466 for D in Decls'Range loop
1469 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1470 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1473 -- Print the body if any
1475 Arr := Get_References (Decl, Get_Bodies => True);
1477 for R in Arr'Range loop
1478 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1480 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1486 -- Print the modifications
1488 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1490 for R in Arr'Range loop
1491 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1493 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1505 procedure Print_Xref (Full_Path_Name : Boolean) is
1506 Decls : constant Declaration_Array_Access := Get_Declarations;
1507 Decl : Declaration_Reference;
1509 Margin : constant := 10;
1510 -- Column where file names start
1512 procedure New_Line80;
1513 -- Go to start of new line
1515 procedure Print80 (S : String);
1516 -- Print the text, respecting the 80 columns rule
1518 procedure Print_Ref (Line, Column : String);
1519 -- The beginning of the output is aligned on a column multiple of 9
1521 procedure Print_List
1522 (Decl : Declaration_Reference;
1524 Get_Reads : Boolean := False;
1525 Get_Writes : Boolean := False;
1526 Get_Bodies : Boolean := False);
1527 -- Print a list of references. If the list is not empty, Msg will
1528 -- be printed prior to the list.
1534 procedure New_Line80 is
1537 Write_Str (String'(1 .. Margin
- 1 => ' '));
1544 procedure Print80
(S
: String) is
1545 Align
: Natural := Margin
- (Integer (Column
) mod Margin
);
1548 if Align
= Margin
then
1552 Write_Str
(String'(1 .. Align => ' ') & S);
1559 procedure Print_Ref (Line, Column : String) is
1560 Line_Align : constant Integer := 4 - Line'Length;
1562 S : constant String := String'(1 .. Line_Align
=> ' ')
1563 & Line
& ':' & Column
;
1565 Align
: Natural := Margin
- (Integer (Output
.Column
) mod Margin
);
1568 if Align
= Margin
then
1572 if Integer (Output
.Column
) + Align
+ S
'Length > 79 then
1577 Write_Str
(String'(1 .. Align => ' ') & S);
1584 procedure Print_List
1585 (Decl : Declaration_Reference;
1587 Get_Reads : Boolean := False;
1588 Get_Writes : Boolean := False;
1589 Get_Bodies : Boolean := False)
1591 Arr : Reference_Array_Access :=
1594 Get_Writes => Get_Writes,
1595 Get_Reads => Get_Reads,
1596 Get_Bodies => Get_Bodies);
1597 File : File_Reference := Empty_File;
1601 if Arr'Length /= 0 then
1606 for R in Arr'Range loop
1607 if Get_File_Ref (Arr (R)) /= File then
1608 if File /= Empty_File then
1612 File := Get_File_Ref (Arr (R));
1613 F := Osint.To_Host_File_Spec
1614 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1617 Write_Str ("<unknown> ");
1619 Write_Str (F.all & ' ');
1624 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1632 -- Start of processing for Print_Xref
1635 for D in Decls'Range loop
1638 Write_Str (Get_Symbol (Decl));
1640 -- Put the declaration type in column Type_Position, but if the
1641 -- declaration name is too long, put at least one space between its
1642 -- name and its type.
1644 while Column < Type_Position - 1 loop
1650 Write_Line (Get_Full_Type (Decl));
1652 Write_Parent_Info : declare
1653 Parent : constant Declaration_Reference := Get_Parent (Decl);
1656 if Parent /= Empty_Declaration then
1657 Write_Str (" Ptype: ");
1658 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1661 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1662 Print80 (" " & Get_Symbol (Parent));
1665 end Write_Parent_Info;
1667 Write_Str (" Decl: ");
1668 F := Osint.To_Host_File_Spec
1669 (Get_Gnatchop_File (Decl, Full_Path_Name));
1672 Print80 ("<unknown> ");
1674 Print80 (F.all & ' ');
1678 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1681 (Decl, " Body: ", Get_Bodies => True);
1683 (Decl, " Modi: ", Get_Writes => True);
1685 (Decl, " Ref: ", Get_Reads => True);
1695 (Pattern : Search_Pattern;
1696 Local_Symbols : Boolean;
1697 Wide_Search : Boolean;
1698 Read_Only : Boolean;
1700 Type_Tree : Boolean)
1702 type String_Access is access String;
1703 procedure Free is new Unchecked_Deallocation (String, String_Access);
1706 File_Ref : File_Reference;
1707 Strip_Num : Natural := 0;
1708 Ali_Name : String_Access;
1711 -- If we want all the .ali files, then find them
1718 -- Get the next unread ali file
1720 File_Ref := Next_Unvisited_File;
1722 exit when File_Ref = Empty_File;
1724 -- Find the ALI file to use. Most of the time, it will be the unit
1725 -- name, with a different extension. However, when dealing with
1726 -- separates the ALI file is in fact the parent's ALI file (and this
1727 -- is recursive, in case the parent itself is a separate).
1732 Ali_Name := new String'
1733 (Get_File
(File_Ref
, With_Dir
=> True, Strip
=> Strip_Num
));
1735 -- Stripped too many things...
1737 if Ali_Name
.all = "" then
1738 if Get_Emit_Warning
(File_Ref
) then
1741 ("warning : file " & Get_File
(File_Ref
, With_Dir
=> True)
1743 Set_Standard_Output
;
1748 -- If not found, try the parent's ALI file (this is needed for
1749 -- separate units and subprograms).
1751 -- Reset the cached directory first, in case the separate's
1752 -- ALI file is not in the same directory.
1754 elsif not File_Exists
(Ali_Name
.all) then
1755 Strip_Num
:= Strip_Num
+ 1;
1756 Reset_Directory
(File_Ref
);
1758 -- Else we finally found it
1765 -- If we had to get the parent's ALI, insert it in the list as usual.
1766 -- This is to avoid parsing it twice in case it has already been
1769 if Ali_Name
/= null and then Strip_Num
/= 0 then
1770 File_Ref
:= Add_To_Xref_File
1771 (File_Name
=> Ali_Name
.all,
1774 -- Now that we have a file name, parse it to find any reference to
1777 elsif Ali_Name
/= null
1778 and then (Read_Only
or else Is_Writable_File
(Ali_Name
.all))
1781 Open
(Ali_Name
.all, ALIfile
);
1783 -- The cross-reference section in the ALI file may be followed
1784 -- by other sections, which can be identified by the starting
1785 -- character of every line, which should neither be 'X' nor a
1786 -- figure in '1' .. '9'.
1788 -- The loop tests below also take into account the end-of-file
1791 while ALIfile
.Buffer
(ALIfile
.Current_Line
) = 'X' loop
1792 Parse_X_Filename
(ALIfile
);
1794 while ALIfile
.Buffer
(ALIfile
.Current_Line
) in '1' .. '9'
1796 Parse_Identifier_Info
1797 (Pattern
, ALIfile
, Local_Symbols
, Der_Info
, Type_Tree
,
1798 Wide_Search
, Labels_As_Ref
=> True);
1803 when No_Xref_Information
=>
1804 if Get_Emit_Warning
(File_Ref
) then
1807 ("warning : No cross-referencing information in "
1809 Set_Standard_Output
;
1822 procedure Search_Xref
1823 (Local_Symbols
: Boolean;
1824 Read_Only
: Boolean;
1828 File_Ref
: File_Reference
;
1829 Null_Pattern
: Search_Pattern
;
1832 Null_Pattern
.Initialized
:= False;
1835 -- Find the next unvisited file
1837 File_Ref
:= Next_Unvisited_File
;
1838 exit when File_Ref
= Empty_File
;
1840 -- Search the object directories for the .ali file
1843 F
: constant String := Get_File
(File_Ref
, With_Dir
=> True);
1846 if Read_Only
or else Is_Writable_File
(F
) then
1847 Open
(F
, ALIfile
, True);
1849 -- The cross-reference section in the ALI file may be followed
1850 -- by other sections, which can be identified by the starting
1851 -- character of every line, which should neither be 'X' nor a
1852 -- figure in '1' .. '9'.
1854 -- The loop tests below also take into account the end-of-file
1857 while ALIfile
.Buffer
(ALIfile
.Current_Line
) = 'X' loop
1858 Parse_X_Filename
(ALIfile
);
1860 while ALIfile
.Buffer
(ALIfile
.Current_Line
) in '1' .. '9'
1862 Parse_Identifier_Info
1863 (Null_Pattern
, ALIfile
, Local_Symbols
, Der_Info
,
1864 Labels_As_Ref
=> False);
1870 when No_Xref_Information
=> null;