1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2012, 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
;
277 (Entity
(File_Start
.. Line_Start
- 1), Visited
=> True);
278 Pattern
.File_Ref
:= File_Ref
;
280 Add_Line
(Pattern
.File_Ref
, Line_Num
, Col_Num
);
284 (ALI_File_Name
(Entity
(File_Start
.. Line_Start
- 1)),
286 Emit_Warning
=> True);
293 procedure Add_Xref_File
(File
: String) is
294 File_Ref
: File_Reference
:= Empty_File
;
295 pragma Unreferenced
(File_Ref
);
297 Iterator
: Expansion_Iterator
;
299 procedure Add_Xref_File_Internal
(File
: String);
300 -- Do the actual addition of the file
302 ----------------------------
303 -- Add_Xref_File_Internal --
304 ----------------------------
306 procedure Add_Xref_File_Internal
(File
: String) is
308 -- Case where we have an ALI file, accept it even though this is
309 -- not official usage, since the intention is obvious
311 if Tail
(File
, 4) = "." & Osint
.ALI_Suffix
.all then
312 File_Ref
:= Add_To_Xref_File
313 (File
, Visited
=> False, Emit_Warning
=> True);
315 -- Normal non-ali file case
318 File_Ref
:= Add_To_Xref_File
(File
, Visited
=> True);
320 File_Ref
:= Add_To_Xref_File
321 (ALI_File_Name
(File
),
322 Visited
=> False, Emit_Warning
=> True);
324 end Add_Xref_File_Internal
;
326 -- Start of processing for Add_Xref_File
329 -- Check if we need to do the expansion
331 if Ada
.Strings
.Fixed
.Index
(File
, "*") /= 0
332 or else Ada
.Strings
.Fixed
.Index
(File
, "?") /= 0
334 Start_Expansion
(Iterator
, File
);
338 S
: constant String := Expansion
(Iterator
);
341 exit when S
'Length = 0;
342 Add_Xref_File_Internal
(S
);
347 Add_Xref_File_Internal
(File
);
351 -----------------------
352 -- Current_Xref_File --
353 -----------------------
355 function Current_Xref_File
(File
: ALI_File
) return File_Reference
is
358 end Current_Xref_File
;
360 --------------------------
361 -- Default_Project_File --
362 --------------------------
364 function Default_Project_File
(Dir_Name
: String) return String is
366 Dir_Ent
: File_Name_String
;
370 Open
(My_Dir
, Dir_Name
);
373 Read
(My_Dir
, Dir_Ent
, Last
);
376 if Tail
(Dir_Ent
(1 .. Last
), 4) = ".adp" then
378 -- The first project file found is the good one
381 return Dir_Ent
(1 .. Last
);
386 return String'(1 .. 0 => ' ');
389 when Directory_Error => return String'(1 .. 0 => ' ');
390 end Default_Project_File
;
398 Num
: Positive) return File_Reference
401 return File
.Dep
.Table
(Num
);
408 procedure Find_ALI_Files
is
410 Dir_Ent
: File_Name_String
;
413 File_Ref
: File_Reference
;
414 pragma Unreferenced
(File_Ref
);
416 function Open_Next_Dir
return Boolean;
417 -- Tries to open the next object directory, and return False if
418 -- the directory cannot be opened.
424 function Open_Next_Dir
return Boolean is
426 -- Until we are able to open a new directory
430 Obj_Dir
: constant String := Next_Obj_Dir
;
433 -- Case of no more Obj_Dir lines
435 if Obj_Dir
'Length = 0 then
439 Open
(My_Dir
.Dir
, Obj_Dir
);
444 -- Could not open the directory
446 when Directory_Error
=> null;
453 -- Start of processing for Find_ALI_Files
458 if Open_Next_Dir
then
460 Read
(My_Dir
.Dir
, Dir_Ent
, Last
);
465 if not Open_Next_Dir
then
470 and then Dir_Ent
(Last
- 3 .. Last
) = "." & Osint
.ALI_Suffix
.all
473 Add_To_Xref_File
(Dir_Ent
(1 .. Last
), Visited
=> False);
483 function Get_Full_Type
(Decl
: Declaration_Reference
) return String is
485 function Param_String
return String;
486 -- Return the string to display depending on whether Decl is a parameter
492 function Param_String
return String is
494 if Is_Parameter
(Decl
) then
501 -- Start of processing for Get_Full_Type
504 case Get_Type
(Decl
) is
505 when 'A' => return "array type";
506 when 'B' => return "boolean type";
507 when 'C' => return "class-wide type";
508 when 'D' => return "decimal type";
509 when 'E' => return "enumeration type";
510 when 'F' => return "float type";
511 when 'H' => return "abstract type";
512 when 'I' => return "integer type";
513 when 'M' => return "modular type";
514 when 'O' => return "fixed type";
515 when 'P' => return "access type";
516 when 'R' => return "record type";
517 when 'S' => return "string type";
518 when 'T' => return "task type";
519 when 'W' => return "protected type";
521 when 'a' => return Param_String
& "array object";
522 when 'b' => return Param_String
& "boolean object";
523 when 'c' => return Param_String
& "class-wide object";
524 when 'd' => return Param_String
& "decimal object";
525 when 'e' => return Param_String
& "enumeration object";
526 when 'f' => return Param_String
& "float object";
527 when 'i' => return Param_String
& "integer object";
528 when 'j' => return Param_String
& "class object";
529 when 'm' => return Param_String
& "modular object";
530 when 'o' => return Param_String
& "fixed object";
531 when 'p' => return Param_String
& "access object";
532 when 'r' => return Param_String
& "record object";
533 when 's' => return Param_String
& "string object";
534 when 't' => return Param_String
& "task object";
535 when 'w' => return Param_String
& "protected object";
536 when 'x' => return Param_String
& "abstract procedure";
537 when 'y' => return Param_String
& "abstract function";
539 when 'h' => return "interface";
540 when 'g' => return "macro";
541 when 'G' => return "function macro";
542 when 'J' => return "class";
543 when 'K' => return "package";
544 when 'k' => return "generic package";
545 when 'L' => return "statement label";
546 when 'l' => return "loop label";
547 when 'N' => return "named number";
548 when 'n' => return "enumeration literal";
549 when 'q' => return "block label";
550 when 'Q' => return "include file";
551 when 'U' => return "procedure";
552 when 'u' => return "generic procedure";
553 when 'V' => return "function";
554 when 'v' => return "generic function";
555 when 'X' => return "exception";
556 when 'Y' => return "entry";
558 when '+' => return "private type";
559 when '*' => return "private variable";
561 -- The above should be the only possibilities, but for this kind
562 -- of informational output, we don't want to bomb if we find
563 -- something else, so just return three question marks when we
564 -- have an unknown Abbrev value
567 if Is_Parameter
(Decl
) then
570 return "??? (" & Get_Type
(Decl
) & ")";
575 --------------------------
576 -- Skip_To_First_X_Line --
577 --------------------------
579 procedure Skip_To_First_X_Line
580 (File
: in out ALI_File
;
584 Ali
: String_Access
renames File
.Buffer
;
586 Ptr
: Positive := Ali
'First;
587 Num_Dependencies
: Natural := 0;
588 File_Start
: Positive;
590 Gnatchop_Offset
: Integer;
591 Gnatchop_Name
: Positive;
593 File_Ref
: File_Reference
;
594 pragma Unreferenced
(File_Ref
);
597 -- Read all the lines possibly processing with-clauses and dependency
598 -- information and exit on finding the first Xref line.
599 -- A fall-through of the loop means that there is no xref information
600 -- which is an error condition.
602 while Ali
(Ptr
) /= EOF
loop
603 if D_Lines
and then Ali
(Ptr
) = 'D' then
605 -- Found dependency information. Format looks like:
606 -- D src-nam time-stmp checksum [subunit-name] [line:file-name]
608 -- Skip the D and parse the filenam
611 Parse_Token
(Ali
, Ptr
, Token
);
615 Num_Dependencies
:= Num_Dependencies
+ 1;
616 Set_Last
(File
.Dep
, Num_Dependencies
);
618 Parse_Token
(Ali
, Ptr
, Token
); -- Skip time-stamp
619 Parse_Token
(Ali
, Ptr
, Token
); -- Skip checksum
620 Parse_Token
(Ali
, Ptr
, Token
); -- Read next entity on the line
622 if not (Ali
(Token
) in '0' .. '9') then
623 Parse_Token
(Ali
, Ptr
, Token
); -- Was a subunit name
626 -- Did we have a gnatchop-ed file with a pragma Source_Reference ?
628 Gnatchop_Offset
:= 0;
630 if Ali
(Token
) in '0' .. '9' then
631 Gnatchop_Name
:= Token
;
632 while Ali
(Gnatchop_Name
) /= ':' loop
633 Gnatchop_Name
:= Gnatchop_Name
+ 1;
637 2 - Natural'Value (Ali
(Token
.. Gnatchop_Name
- 1));
638 Token
:= Gnatchop_Name
+ 1;
641 File
.Dep
.Table
(Num_Dependencies
) := Add_To_Xref_File
642 (Ali
(File_Start
.. File_End
),
643 Gnatchop_File
=> Ali
(Token
.. Ptr
- 1),
644 Gnatchop_Offset
=> Gnatchop_Offset
);
646 elsif W_Lines
and then Ali
(Ptr
) = 'W' then
648 -- Found with-clause information. Format looks like:
649 -- "W debug%s debug.adb debug.ali"
651 -- Skip the W and parse the .ali filename (3rd token)
653 Parse_Token
(Ali
, Ptr
, Token
);
654 Parse_Token
(Ali
, Ptr
, Token
);
655 Parse_Token
(Ali
, Ptr
, Token
);
658 Add_To_Xref_File
(Ali
(Token
.. Ptr
- 1), Visited
=> False);
660 elsif Ali
(Ptr
) = 'X' then
662 -- Found a cross-referencing line - stop processing
664 File
.Current_Line
:= Ptr
;
665 File
.Xref_Line
:= Ptr
;
669 Parse_EOL
(Ali
, Ptr
);
672 raise No_Xref_Information
;
673 end Skip_To_First_X_Line
;
682 Dependencies
: Boolean := False)
684 Ali
: String_Access
renames File
.Buffer
;
685 pragma Warnings
(Off
, Ali
);
688 if File
.Buffer
/= null then
695 Read_File
(Name
, Ali
);
698 when Ada
.Text_IO
.Name_Error | Ada
.Text_IO
.End_Error
=>
699 raise No_Xref_Information
;
702 Skip_To_First_X_Line
(File
, D_Lines
=> True, W_Lines
=> Dependencies
);
710 (Source
: not null access String;
711 Ptr
: in out Positive;
712 Skip_Continuation_Line
: Boolean := False)
716 -- Skip to end of line
718 while Source
(Ptr
) /= ASCII
.CR
and then Source
(Ptr
) /= ASCII
.LF
719 and then Source
(Ptr
) /= EOF
724 -- Skip CR or LF if not at end of file
726 if Source
(Ptr
) /= EOF
then
730 -- Skip past CR/LF or LF/CR combination
732 if (Source
(Ptr
) = ASCII
.CR
or else Source
(Ptr
) = ASCII
.LF
)
733 and then Source
(Ptr
) /= Source
(Ptr
- 1)
738 exit when not Skip_Continuation_Line
or else Source
(Ptr
) /= '.';
742 ---------------------------
743 -- Parse_Identifier_Info --
744 ---------------------------
746 procedure Parse_Identifier_Info
747 (Pattern
: Search_Pattern
;
748 File
: in out ALI_File
;
749 Local_Symbols
: Boolean;
750 Der_Info
: Boolean := False;
751 Type_Tree
: Boolean := False;
752 Wide_Search
: Boolean := True;
753 Labels_As_Ref
: Boolean := True)
755 Ptr
: Positive renames File
.Current_Line
;
756 Ali
: String_Access
renames File
.Buffer
;
758 E_Line
: Natural; -- Line number of current entity
759 E_Col
: Natural; -- Column number of current entity
760 E_Type
: Character; -- Type of current entity
761 E_Name
: Positive; -- Pointer to begin of entity name
762 E_Global
: Boolean; -- True iff entity is global
764 R_Line
: Natural; -- Line number of current reference
765 R_Col
: Natural; -- Column number of current reference
766 R_Type
: Character; -- Type of current reference
768 Decl_Ref
: Declaration_Reference
;
769 File_Ref
: File_Reference
:= Current_Xref_File
(File
);
771 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String;
772 -- Returns the symbol name for the entity defined at the specified
773 -- line and column in the dependent unit number Eun. For this we need
774 -- to parse the ali file again because the parent entity is not in
775 -- the declaration table if it did not match the search pattern.
777 procedure Skip_To_Matching_Closing_Bracket
;
778 -- When Ptr points to an opening square bracket, moves it to the
779 -- character following the matching closing bracket
781 ---------------------
782 -- Get_Symbol_Name --
783 ---------------------
785 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String is
787 E_Eun
: Positive; -- Unit number of current entity
788 E_Line
: Natural; -- Line number of current entity
789 E_Col
: Natural; -- Column number of current entity
790 E_Name
: Positive; -- Pointer to begin of entity name
793 -- Look for the X lines corresponding to unit Eun
796 if Ali
(Ptr
) = 'X' then
798 Parse_Number
(Ali
, Ptr
, E_Eun
);
799 exit when E_Eun
= Eun
;
802 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
805 -- Here we are in the right Ali section, we now look for the entity
806 -- declared at position (Line, Col).
809 Parse_Number
(Ali
, Ptr
, E_Line
);
810 exit when Ali
(Ptr
) = EOF
;
812 Parse_Number
(Ali
, Ptr
, E_Col
);
813 exit when Ali
(Ptr
) = EOF
;
816 if Line
= E_Line
and then Col
= E_Col
then
817 Parse_Token
(Ali
, Ptr
, E_Name
);
818 return Ali
(E_Name
.. Ptr
- 1);
821 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
822 exit when Ali
(Ptr
) = EOF
;
825 -- We were not able to find the symbol, this should not happen but
826 -- since we don't want to stop here we return a string of three
827 -- question marks as the symbol name.
832 --------------------------------------
833 -- Skip_To_Matching_Closing_Bracket --
834 --------------------------------------
836 procedure Skip_To_Matching_Closing_Bracket
is
837 Num_Brackets
: Natural;
841 while Num_Brackets
/= 0 loop
843 if Ali
(Ptr
) = '[' then
844 Num_Brackets
:= Num_Brackets
+ 1;
845 elsif Ali
(Ptr
) = ']' then
846 Num_Brackets
:= Num_Brackets
- 1;
851 end Skip_To_Matching_Closing_Bracket
;
853 -- Start of processing for Parse_Identifier_Info
856 -- The identifier info looks like:
857 -- "38U9*Debug 12|36r6 36r19"
859 -- Extract the line, column and entity name information
861 Parse_Number
(Ali
, Ptr
, E_Line
);
863 if Ali
(Ptr
) > ' ' then
868 -- Ignore some of the entities (labels,...)
871 when 'l' |
'L' |
'q' =>
872 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
879 Parse_Number
(Ali
, Ptr
, E_Col
);
882 if Ali
(Ptr
) >= ' ' then
883 E_Global
:= (Ali
(Ptr
) = '*');
887 Parse_Token
(Ali
, Ptr
, E_Name
);
889 -- Exit if the symbol does not match
890 -- or if we have a local symbol and we do not want it
892 if (not Local_Symbols
and not E_Global
)
893 or else (Pattern
.Initialized
894 and then not Match
(Ali
(E_Name
.. Ptr
- 1), Pattern
.Entity
))
895 or else (E_Name
>= Ptr
)
897 Decl_Ref
:= Add_Declaration
898 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
,
899 Remove_Only
=> True);
900 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
904 -- Insert the declaration in the table
906 Decl_Ref
:= Add_Declaration
907 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
);
909 if Ali
(Ptr
) = '[' then
910 Skip_To_Matching_Closing_Bracket
;
913 -- Skip any renaming indication
915 if Ali
(Ptr
) = '=' then
917 P_Line
, P_Column
: Natural;
918 pragma Warnings
(Off
, P_Line
);
919 pragma Warnings
(Off
, P_Column
);
922 Parse_Number
(Ali
, Ptr
, P_Line
);
924 Parse_Number
(Ali
, Ptr
, P_Column
);
929 or else Ali
(Ptr
) = '('
930 or else Ali
(Ptr
) = '{'
932 -- Here we have a type derivation information. The format is
933 -- <3|12I45> which means that the current entity is derived from the
934 -- type defined in unit number 3, line 12 column 45. The pipe and
935 -- unit number is optional. It is specified only if the parent type
936 -- is not defined in the current unit.
938 -- We also have the format for generic instantiations, as in
939 -- 7a5*Uid(3|5I8[4|2]) 2|4r74
941 -- We could also have something like
943 -- that indicates that I derives from the predefined type integer.
947 if Ali
(Ptr
) in '0' .. '9' then
948 Parse_Derived_Info
: declare
949 P_Line
: Natural; -- parent entity line
950 P_Column
: Natural; -- parent entity column
951 P_Eun
: Positive; -- parent entity file number
954 Parse_Number
(Ali
, Ptr
, P_Line
);
956 -- If we have a pipe then the first number was the unit number
958 if Ali
(Ptr
) = '|' then
962 -- Now we have the line number
964 Parse_Number
(Ali
, Ptr
, P_Line
);
967 -- We don't have a unit number specified, so we set P_Eun to
970 for K
in Dependencies_Tables
.First
.. Last
(File
.Dep
) loop
972 exit when File
.Dep
.Table
(K
) = File_Ref
;
976 -- Then parse the type and column number
979 Parse_Number
(Ali
, Ptr
, P_Column
);
981 -- Skip the information for generics instantiations
983 if Ali
(Ptr
) = '[' then
984 Skip_To_Matching_Closing_Bracket
;
987 -- Skip '>', or ')' or '>'
991 -- The derived info is needed only is the derived info mode is
992 -- on or if we want to output the type hierarchy
994 if Der_Info
or else Type_Tree
then
996 Symbol
: constant String :=
997 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
);
999 if Symbol
/= "???" then
1005 File
.Dep
.Table
(P_Eun
));
1011 and then (Pattern
.File_Ref
= Empty_File
1013 Pattern
.File_Ref
= Current_Xref_File
(File
))
1015 Search_Parent_Tree
: declare
1016 Pattern
: Search_Pattern
; -- Parent type pattern
1017 File_Pos_Backup
: Positive;
1022 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
)
1023 & ':' & Get_Gnatchop_File
(File
.Dep
.Table
(P_Eun
))
1024 & ':' & Get_Line
(Get_Parent
(Decl_Ref
))
1025 & ':' & Get_Column
(Get_Parent
(Decl_Ref
)),
1028 -- No default match is needed to look for the parent type
1029 -- since we are using the fully qualified symbol name:
1030 -- symbol:file:line:column
1032 Set_Default_Match
(False);
1034 -- The parent hierarchy is defined in the same unit as
1035 -- the derived type. So we want to revisit the unit.
1037 File_Pos_Backup
:= File
.Current_Line
;
1039 Skip_To_First_X_Line
1040 (File
, D_Lines
=> False, W_Lines
=> False);
1042 while File
.Buffer
(File
.Current_Line
) /= EOF
loop
1043 Parse_X_Filename
(File
);
1044 Parse_Identifier_Info
1045 (Pattern
=> Pattern
,
1047 Local_Symbols
=> False,
1048 Der_Info
=> Der_Info
,
1050 Wide_Search
=> False,
1051 Labels_As_Ref
=> Labels_As_Ref
);
1054 File
.Current_Line
:= File_Pos_Backup
;
1055 end Search_Parent_Tree
;
1057 end Parse_Derived_Info
;
1060 while Ali
(Ptr
) /= '>'
1061 and then Ali
(Ptr
) /= ')'
1062 and then Ali
(Ptr
) /= '}'
1070 -- To find the body, we will have to parse the file too
1074 File_Ref
: File_Reference
;
1075 pragma Unreferenced
(File_Ref
);
1076 File_Name
: constant String := Get_Gnatchop_File
(File
.X_File
);
1078 File_Ref
:= Add_To_Xref_File
(ALI_File_Name
(File_Name
), False);
1082 -- Parse references to this entity.
1083 -- Ptr points to next reference with leading blanks
1086 -- Process references on current line
1088 while Ali
(Ptr
) = ' ' or else Ali
(Ptr
) = ASCII
.HT
loop
1090 -- For every reference read the line, type and column,
1091 -- optionally preceded by a file number and a pipe symbol.
1093 Parse_Number
(Ali
, Ptr
, R_Line
);
1095 if Ali
(Ptr
) = Pipe
then
1097 File_Ref
:= File_Name
(File
, R_Line
);
1099 Parse_Number
(Ali
, Ptr
, R_Line
);
1102 if Ali
(Ptr
) > ' ' then
1103 R_Type
:= Ali
(Ptr
);
1107 -- Imported entities may have an indication specifying information
1108 -- about the corresponding external name:
1109 -- 5U14*Foo2 5>20 6b<c,myfoo2>22 # Imported entity
1110 -- 5U14*Foo2 5>20 6i<c,myfoo2>22 # Exported entity
1112 if (R_Type
= 'b' or else R_Type
= 'i')
1113 and then Ali
(Ptr
) = '<'
1115 while Ptr
<= Ali
'Last
1116 and then Ali
(Ptr
) /= '>'
1123 Parse_Number
(Ali
, Ptr
, R_Col
);
1125 -- Insert the reference or body in the table
1128 (Decl_Ref
, File_Ref
, R_Line
, R_Col
, R_Type
, Labels_As_Ref
);
1130 -- Skip generic information, if any
1132 if Ali
(Ptr
) = '[' then
1134 Num_Nested
: Integer := 1;
1138 while Num_Nested
/= 0 loop
1139 if Ali
(Ptr
) = ']' then
1140 Num_Nested
:= Num_Nested
- 1;
1141 elsif Ali
(Ptr
) = '[' then
1142 Num_Nested
:= Num_Nested
+ 1;
1152 Parse_EOL
(Ali
, Ptr
);
1154 -- Loop until new line is no continuation line
1156 exit when Ali
(Ptr
) /= '.';
1159 end Parse_Identifier_Info
;
1165 procedure Parse_Number
1166 (Source
: not null access String;
1167 Ptr
: in out Positive;
1168 Number
: out Natural)
1173 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1178 while Source
(Ptr
) in '0' .. '9' loop
1180 10 * Number
+ (Character'Pos (Source
(Ptr
)) - Character'Pos ('0'));
1189 procedure Parse_Token
1190 (Source
: not null access String;
1191 Ptr
: in out Positive;
1192 Token_Ptr
: out Positive)
1194 In_Quotes
: Character := ASCII
.NUL
;
1199 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1205 -- Find end-of-token
1207 while (In_Quotes
/= ASCII
.NUL
or else
1208 not (Source
(Ptr
) = ' '
1209 or else Source
(Ptr
) = ASCII
.HT
1210 or else Source
(Ptr
) = '<'
1211 or else Source
(Ptr
) = '{'
1212 or else Source
(Ptr
) = '['
1213 or else Source
(Ptr
) = '='
1214 or else Source
(Ptr
) = '('))
1215 and then Source
(Ptr
) >= ' '
1217 -- Double-quotes are used for operators
1218 -- Simple-quotes are used for character constants, for instance when
1219 -- they are found in an enumeration type "type A is ('+', '-');"
1221 case Source
(Ptr
) is
1223 if In_Quotes
= Source
(Ptr
) then
1224 In_Quotes
:= ASCII
.NUL
;
1225 elsif In_Quotes
= ASCII
.NUL
then
1226 In_Quotes
:= Source
(Ptr
);
1237 ----------------------
1238 -- Parse_X_Filename --
1239 ----------------------
1241 procedure Parse_X_Filename
(File
: in out ALI_File
) is
1242 Ali
: String_Access
renames File
.Buffer
;
1243 Ptr
: Positive renames File
.Current_Line
;
1247 while Ali
(Ptr
) = 'X' loop
1249 -- The current line is the start of a new Xref file section,
1250 -- whose format looks like:
1254 -- Skip the X and read the file number for the new X_File
1257 Parse_Number
(Ali
, Ptr
, File_Nr
);
1260 File
.X_File
:= File
.Dep
.Table
(File_Nr
);
1263 Parse_EOL
(Ali
, Ptr
);
1265 end Parse_X_Filename
;
1267 --------------------
1268 -- Print_Gnatfind --
1269 --------------------
1271 procedure Print_Gnatfind
1272 (References
: Boolean;
1273 Full_Path_Name
: Boolean)
1275 Decls
: constant Declaration_Array_Access
:= Get_Declarations
;
1276 Decl
: Declaration_Reference
;
1277 Arr
: Reference_Array_Access
;
1281 Msg
: String := " ");
1282 -- Print a reference, according to the extended tag of the output
1290 Msg
: String := " ")
1292 F
: String_Access
:=
1293 Osint
.To_Host_File_Spec
1294 (Get_Gnatchop_File
(Ref
, Full_Path_Name
));
1296 Buffer
: constant String :=
1298 ":" & Get_Line
(Ref
) &
1299 ":" & Get_Column
(Ref
) &
1302 Num_Blanks
: Integer := Longest_File_Name
+ 10 - Buffer
'Length;
1306 Num_Blanks
:= Integer'Max (0, Num_Blanks
);
1309 & String'(1 .. Num_Blanks => ' ')
1310 & Msg & " " & Get_Symbol (Decl));
1312 if Get_Source_Line (Ref)'Length /= 0 then
1313 Write_Line (" " & Get_Source_Line (Ref));
1317 -- Start of processing for Print_Gnatfind
1320 for D in Decls'Range loop
1323 if Match (Decl) then
1325 -- Output the declaration
1328 Parent : constant Declaration_Reference := Get_Parent (Decl);
1330 F : String_Access :=
1331 Osint.To_Host_File_Spec
1332 (Get_Gnatchop_File (Decl, Full_Path_Name));
1334 Buffer : constant String :=
1336 ":" & Get_Line (Decl) &
1337 ":" & Get_Column (Decl) &
1340 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1344 Num_Blanks := Integer'Max (0, Num_Blanks);
1346 (Buffer & String'(1 .. Num_Blanks
=> ' ')
1347 & "(spec) " & Get_Symbol
(Decl
));
1349 if Parent
/= Empty_Declaration
then
1350 F
:= Osint
.To_Host_File_Spec
(Get_Gnatchop_File
(Parent
));
1352 (Buffer
& String'(1 .. Num_Blanks => ' ')
1353 & " derived from " & Get_Symbol (Parent)
1356 & ':' & Get_Line (Parent)
1357 & ':' & Get_Column (Parent) & ')');
1362 if Get_Source_Line (Decl)'Length /= 0 then
1363 Write_Line (" " & Get_Source_Line (Decl));
1366 -- Output the body (sorted)
1368 Arr := Get_References (Decl, Get_Bodies => True);
1370 for R in Arr'Range loop
1371 Print_Ref (Arr (R), "(body)");
1377 Arr := Get_References
1378 (Decl, Get_Writes => True, Get_Reads => True);
1380 for R in Arr'Range loop
1381 Print_Ref (Arr (R));
1394 procedure Print_Unused (Full_Path_Name : Boolean) is
1395 Decls : constant Declaration_Array_Access := Get_Declarations;
1396 Decl : Declaration_Reference;
1397 Arr : Reference_Array_Access;
1401 for D in Decls'Range loop
1405 (Decl, Get_Reads => True, Get_Writes => True) = 0
1407 F := Osint.To_Host_File_Spec
1408 (Get_Gnatchop_File (Decl, Full_Path_Name));
1409 Write_Str (Get_Symbol (Decl)
1411 & Get_Full_Type (Decl)
1417 & Get_Column (Decl));
1420 -- Print the body if any
1422 Arr := Get_References (Decl, Get_Bodies => True);
1424 for R in Arr'Range loop
1425 F := Osint.To_Host_File_Spec
1426 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1429 & ':' & Get_Line (Arr (R))
1430 & ':' & Get_Column (Arr (R)));
1444 procedure Print_Vi (Full_Path_Name : Boolean) is
1445 Tab : constant Character := ASCII.HT;
1446 Decls : constant Declaration_Array_Access :=
1447 Get_Declarations (Sorted => False);
1448 Decl : Declaration_Reference;
1449 Arr : Reference_Array_Access;
1453 for D in Decls'Range loop
1456 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1457 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1460 -- Print the body if any
1462 Arr := Get_References (Decl, Get_Bodies => True);
1464 for R in Arr'Range loop
1465 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1467 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1473 -- Print the modifications
1475 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => 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)));
1492 procedure Print_Xref (Full_Path_Name : Boolean) is
1493 Decls : constant Declaration_Array_Access := Get_Declarations;
1494 Decl : Declaration_Reference;
1496 Margin : constant := 10;
1497 -- Column where file names start
1499 procedure New_Line80;
1500 -- Go to start of new line
1502 procedure Print80 (S : String);
1503 -- Print the text, respecting the 80 columns rule
1505 procedure Print_Ref (Line, Column : String);
1506 -- The beginning of the output is aligned on a column multiple of 9
1508 procedure Print_List
1509 (Decl : Declaration_Reference;
1511 Get_Reads : Boolean := False;
1512 Get_Writes : Boolean := False;
1513 Get_Bodies : Boolean := False);
1514 -- Print a list of references. If the list is not empty, Msg will
1515 -- be printed prior to the list.
1521 procedure New_Line80 is
1524 Write_Str (String'(1 .. Margin
- 1 => ' '));
1531 procedure Print80
(S
: String) is
1532 Align
: Natural := Margin
- (Integer (Column
) mod Margin
);
1535 if Align
= Margin
then
1539 Write_Str
(String'(1 .. Align => ' ') & S);
1546 procedure Print_Ref (Line, Column : String) is
1547 Line_Align : constant Integer := 4 - Line'Length;
1549 S : constant String := String'(1 .. Line_Align
=> ' ')
1550 & Line
& ':' & Column
;
1552 Align
: Natural := Margin
- (Integer (Output
.Column
) mod Margin
);
1555 if Align
= Margin
then
1559 if Integer (Output
.Column
) + Align
+ S
'Length > 79 then
1564 Write_Str
(String'(1 .. Align => ' ') & S);
1571 procedure Print_List
1572 (Decl : Declaration_Reference;
1574 Get_Reads : Boolean := False;
1575 Get_Writes : Boolean := False;
1576 Get_Bodies : Boolean := False)
1578 Arr : Reference_Array_Access :=
1581 Get_Writes => Get_Writes,
1582 Get_Reads => Get_Reads,
1583 Get_Bodies => Get_Bodies);
1584 File : File_Reference := Empty_File;
1588 if Arr'Length /= 0 then
1593 for R in Arr'Range loop
1594 if Get_File_Ref (Arr (R)) /= File then
1595 if File /= Empty_File then
1599 File := Get_File_Ref (Arr (R));
1600 F := Osint.To_Host_File_Spec
1601 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1604 Write_Str ("<unknown> ");
1606 Write_Str (F.all & ' ');
1611 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1619 -- Start of processing for Print_Xref
1622 for D in Decls'Range loop
1625 Write_Str (Get_Symbol (Decl));
1627 -- Put the declaration type in column Type_Position, but if the
1628 -- declaration name is too long, put at least one space between its
1629 -- name and its type.
1631 while Column < Type_Position - 1 loop
1637 Write_Line (Get_Full_Type (Decl));
1639 Write_Parent_Info : declare
1640 Parent : constant Declaration_Reference := Get_Parent (Decl);
1643 if Parent /= Empty_Declaration then
1644 Write_Str (" Ptype: ");
1645 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1648 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1649 Print80 (" " & Get_Symbol (Parent));
1652 end Write_Parent_Info;
1654 Write_Str (" Decl: ");
1655 F := Osint.To_Host_File_Spec
1656 (Get_Gnatchop_File (Decl, Full_Path_Name));
1659 Print80 ("<unknown> ");
1661 Print80 (F.all & ' ');
1665 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1668 (Decl, " Body: ", Get_Bodies => True);
1670 (Decl, " Modi: ", Get_Writes => True);
1672 (Decl, " Ref: ", Get_Reads => True);
1682 (Pattern : Search_Pattern;
1683 Local_Symbols : Boolean;
1684 Wide_Search : Boolean;
1685 Read_Only : Boolean;
1687 Type_Tree : Boolean)
1689 type String_Access is access String;
1690 procedure Free is new Unchecked_Deallocation (String, String_Access);
1693 File_Ref : File_Reference;
1694 Strip_Num : Natural := 0;
1695 Ali_Name : String_Access;
1698 -- If we want all the .ali files, then find them
1705 -- Get the next unread ali file
1707 File_Ref := Next_Unvisited_File;
1709 exit when File_Ref = Empty_File;
1711 -- Find the ALI file to use. Most of the time, it will be the unit
1712 -- name, with a different extension. However, when dealing with
1713 -- separates the ALI file is in fact the parent's ALI file (and this
1714 -- is recursive, in case the parent itself is a separate).
1719 Ali_Name := new String'
1720 (Get_File
(File_Ref
, With_Dir
=> True, Strip
=> Strip_Num
));
1722 -- Stripped too many things...
1724 if Ali_Name
.all = "" then
1725 if Get_Emit_Warning
(File_Ref
) then
1728 ("warning : file " & Get_File
(File_Ref
, With_Dir
=> True)
1730 Set_Standard_Output
;
1735 -- If not found, try the parent's ALI file (this is needed for
1736 -- separate units and subprograms).
1738 -- Reset the cached directory first, in case the separate's
1739 -- ALI file is not in the same directory.
1741 elsif not File_Exists
(Ali_Name
.all) then
1742 Strip_Num
:= Strip_Num
+ 1;
1743 Reset_Directory
(File_Ref
);
1745 -- Else we finally found it
1752 -- If we had to get the parent's ALI, insert it in the list as usual.
1753 -- This is to avoid parsing it twice in case it has already been
1756 if Ali_Name
/= null and then Strip_Num
/= 0 then
1757 File_Ref
:= Add_To_Xref_File
1758 (File_Name
=> Ali_Name
.all,
1761 -- Now that we have a file name, parse it to find any reference to
1764 elsif Ali_Name
/= null
1765 and then (Read_Only
or else Is_Writable_File
(Ali_Name
.all))
1768 Open
(Ali_Name
.all, ALIfile
);
1770 -- The cross-reference section in the ALI file may be followed
1771 -- by other sections, which can be identified by the starting
1772 -- character of every line, which should neither be 'X' nor a
1773 -- figure in '1' .. '9'.
1775 -- The loop tests below also take into account the end-of-file
1778 while ALIfile
.Buffer
(ALIfile
.Current_Line
) = 'X' loop
1779 Parse_X_Filename
(ALIfile
);
1781 while ALIfile
.Buffer
(ALIfile
.Current_Line
) in '1' .. '9'
1783 Parse_Identifier_Info
1784 (Pattern
, ALIfile
, Local_Symbols
, Der_Info
, Type_Tree
,
1785 Wide_Search
, Labels_As_Ref
=> True);
1790 when No_Xref_Information
=>
1791 if Get_Emit_Warning
(File_Ref
) then
1794 ("warning : No cross-referencing information in "
1796 Set_Standard_Output
;
1809 procedure Search_Xref
1810 (Local_Symbols
: Boolean;
1811 Read_Only
: Boolean;
1815 File_Ref
: File_Reference
;
1816 Null_Pattern
: Search_Pattern
;
1819 Null_Pattern
.Initialized
:= False;
1822 -- Find the next unvisited file
1824 File_Ref
:= Next_Unvisited_File
;
1825 exit when File_Ref
= Empty_File
;
1827 -- Search the object directories for the .ali file
1830 F
: constant String := Get_File
(File_Ref
, With_Dir
=> True);
1833 if Read_Only
or else Is_Writable_File
(F
) then
1834 Open
(F
, ALIfile
, True);
1836 -- The cross-reference section in the ALI file may be followed
1837 -- by other sections, which can be identified by the starting
1838 -- character of every line, which should neither be 'X' nor a
1839 -- figure in '1' .. '9'.
1841 -- The loop tests below also take into account the end-of-file
1844 while ALIfile
.Buffer
(ALIfile
.Current_Line
) = 'X' loop
1845 Parse_X_Filename
(ALIfile
);
1847 while ALIfile
.Buffer
(ALIfile
.Current_Line
) in '1' .. '9'
1849 Parse_Identifier_Info
1850 (Null_Pattern
, ALIfile
, Local_Symbols
, Der_Info
,
1851 Labels_As_Ref
=> False);
1857 when No_Xref_Information
=> null;