1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2003 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with Output
; use Output
;
29 with Types
; use Types
;
31 with Unchecked_Deallocation
;
33 with Ada
.Strings
.Fixed
; use Ada
.Strings
.Fixed
;
34 with Ada
.Text_IO
; use Ada
.Text_IO
;
36 with GNAT
.Command_Line
; use GNAT
.Command_Line
;
37 with GNAT
.IO_Aux
; use GNAT
.IO_Aux
;
39 package body Xref_Lib
is
41 Type_Position
: constant := 50;
42 -- Column for label identifying type of entity
48 Pipe
: constant Character := '|';
49 -- First character on xref lines in the .ali file
51 No_Xref_Information
: exception;
52 -- Exception raised when there is no cross-referencing information in
56 (Source
: access String;
57 Ptr
: in out Positive;
58 Skip_Continuation_Line
: Boolean := False);
59 -- On return Source (Ptr) is the first character of the next line
60 -- or EOF. Source.all must be terminated by EOF.
62 -- If Skip_Continuation_Line is True, this subprogram skips as many
63 -- lines as required when the second or more lines starts with '.'
64 -- (continuation lines in ALI files).
66 function Current_Xref_File
(File
: ALI_File
) return File_Reference
;
67 -- Return the file matching the last 'X' line we found while parsing
70 function File_Name
(File
: ALI_File
; Num
: Positive) return File_Reference
;
71 -- Returns the dependency file name number Num
73 function Get_Full_Type
(Decl
: Declaration_Reference
) return String;
74 -- Returns the full type corresponding to a type letter as found in
80 Dependencies
: in Boolean := False);
81 -- Open a new ALI file. If Dependencies is True, the insert every library
82 -- file 'with'ed in the files database (used for gnatxref)
84 procedure Parse_Identifier_Info
85 (Pattern
: Search_Pattern
;
86 File
: in out ALI_File
;
87 Local_Symbols
: Boolean;
88 Der_Info
: Boolean := False;
89 Type_Tree
: Boolean := False;
90 Wide_Search
: Boolean := True;
91 Labels_As_Ref
: Boolean := True);
92 -- Output the file and the line where the identifier was referenced,
93 -- If Local_Symbols is False then only the publicly visible symbols
96 -- If Labels_As_Ref is true, then the references to the entities after
97 -- the end statements ("end Foo") will be counted as actual references.
98 -- The entity will never be reported as unreferenced by gnatxref -u
100 procedure Parse_Token
101 (Source
: access String;
102 Ptr
: in out Positive;
103 Token_Ptr
: out Positive);
104 -- Skips any separators and stores the start of the token in Token_Ptr.
105 -- Then stores the position of the next separator in Ptr. On return
106 -- Source (Token_Ptr .. Ptr - 1) is the token. Separators are space
107 -- and ASCII.HT. Parse_Token will never skip to the next line.
109 procedure Parse_Number
110 (Source
: access String;
111 Ptr
: in out Positive;
112 Number
: out Natural);
113 -- Skips any separators and parses Source upto the first character that
114 -- is not a decimal digit. Returns value of parsed digits or 0 if none.
116 procedure Parse_X_Filename
(File
: in out ALI_File
);
117 -- Reads and processes "X..." lines in the ALI file
118 -- and updates the File.X_File information.
120 procedure Skip_To_First_X_Line
121 (File
: in out ALI_File
;
124 -- Skip the lines in the ALI file until the first cross-reference line
125 -- (^X...) is found. Search is started from the beginning of the file.
126 -- If not such line is found, No_Xref_Information is raised.
127 -- If W_Lines is false, then the lines "^W" are not parsed.
128 -- If D_Lines is false, then the lines "^D" are not parsed.
135 (Pattern
: in out Search_Pattern
;
137 Glob
: Boolean := False)
139 File_Start
: Natural;
140 Line_Start
: Natural;
142 Line_Num
: Natural := 0;
143 Col_Num
: Natural := 0;
144 File_Ref
: File_Reference
:= Empty_File
;
145 Has_Pattern
: Boolean := False;
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;
231 -- Parse the file name
233 Line_Start
:= Index
(Entity
(File_Start
.. Entity
'Last), ":");
235 -- Check if it was a disk:\directory item (for NT and OS/2)
237 if File_Start
= Line_Start
- 1
238 and then Line_Start
< Entity
'Last
239 and then Entity
(Line_Start
+ 1) = '\'
241 Line_Start
:= Index
(Entity
(Line_Start
+ 1 .. Entity
'Last), ":");
244 if Line_Start
= 0 then
245 Line_Start
:= Entity
'Length + 1;
247 elsif Line_Start
/= Entity
'Last then
248 Col_Start
:= Index
(Entity
(Line_Start
+ 1 .. Entity
'Last), ":");
250 if Col_Start
= 0 then
251 Col_Start
:= Entity
'Last + 1;
254 if Col_Start
> Line_Start
+ 1 then
256 Line_Num
:= Natural'Value
257 (Entity
(Line_Start
+ 1 .. Col_Start
- 1));
260 when Constraint_Error
=>
261 raise Invalid_Argument
;
265 if Col_Start
< Entity
'Last then
267 Col_Num
:= Natural'Value (Entity
268 (Col_Start
+ 1 .. Entity
'Last));
271 when Constraint_Error
=> raise Invalid_Argument
;
278 (Entity
(File_Start
.. Line_Start
- 1), Visited
=> True);
279 Pattern
.File_Ref
:= File_Ref
;
280 Add_Line
(Pattern
.File_Ref
, Line_Num
, Col_Num
);
283 (ALI_File_Name
(Entity
(File_Start
.. Line_Start
- 1)),
285 Emit_Warning
=> True);
292 procedure Add_Xref_File
(File
: String) is
293 File_Ref
: File_Reference
:= Empty_File
;
294 Iterator
: Expansion_Iterator
;
296 procedure Add_Xref_File_Internal
(File
: String);
297 -- Do the actual addition of the file
299 ----------------------------
300 -- Add_Xref_File_Internal --
301 ----------------------------
303 procedure Add_Xref_File_Internal
(File
: String) is
305 -- Case where we have an ALI file, accept it even though this is
306 -- not official usage, since the intention is obvious
308 if Tail
(File
, 4) = ".ali" then
309 File_Ref
:= Add_To_Xref_File
310 (File
, Visited
=> False, Emit_Warning
=> True);
312 -- Normal non-ali file case
315 File_Ref
:= Add_To_Xref_File
(File
, Visited
=> True);
317 File_Ref
:= Add_To_Xref_File
318 (ALI_File_Name
(File
),
320 Emit_Warning
=> True);
322 end Add_Xref_File_Internal
;
324 -- Start of processing for Add_Xref_File
327 -- Check if we need to do the expansion
329 if Ada
.Strings
.Fixed
.Index
(File
, "*") /= 0
330 or else Ada
.Strings
.Fixed
.Index
(File
, "?") /= 0
332 Start_Expansion
(Iterator
, File
);
336 S
: constant String := Expansion
(Iterator
);
339 exit when S
'Length = 0;
340 Add_Xref_File_Internal
(S
);
345 Add_Xref_File_Internal
(File
);
349 -----------------------
350 -- Current_Xref_File --
351 -----------------------
353 function Current_Xref_File
(File
: ALI_File
) return File_Reference
is
356 end Current_Xref_File
;
358 --------------------------
359 -- Default_Project_File --
360 --------------------------
362 function Default_Project_File
(Dir_Name
: String) return String is
364 Dir_Ent
: File_Name_String
;
368 Open
(My_Dir
, Dir_Name
);
371 Read
(My_Dir
, Dir_Ent
, Last
);
374 if Tail
(Dir_Ent
(1 .. Last
), 4) = ".adp" then
376 -- The first project file found is the good one.
379 return Dir_Ent
(1 .. Last
);
384 return String'(1 .. 0 => ' ');
387 when Directory_Error => return String'(1 .. 0 => ' ');
388 end Default_Project_File
;
396 Num
: Positive) return File_Reference
399 return File
.Dep
.Table
(Num
);
406 procedure Find_ALI_Files
is
408 Dir_Ent
: File_Name_String
;
410 File_Ref
: File_Reference
;
412 function Open_Next_Dir
return Boolean;
413 -- Tries to open the next object directory, and return False if
414 -- the directory cannot be opened.
420 function Open_Next_Dir
return Boolean is
422 -- Until we are able to open a new directory
426 Obj_Dir
: constant String := Next_Obj_Dir
;
429 -- Case of no more Obj_Dir lines
431 if Obj_Dir
'Length = 0 then
435 Open
(My_Dir
.Dir
, Obj_Dir
);
440 -- Could not open the directory
442 when Directory_Error
=> null;
449 -- Start of processing for Find_ALI_Files
454 if Open_Next_Dir
then
456 Read
(My_Dir
.Dir
, Dir_Ent
, Last
);
461 if not Open_Next_Dir
then
465 elsif Last
> 4 and then Dir_Ent
(Last
- 3 .. Last
) = ".ali" then
467 Add_To_Xref_File
(Dir_Ent
(1 .. Last
), Visited
=> False);
477 function Get_Full_Type
(Decl
: Declaration_Reference
) return String is
479 function Param_String
return String;
480 -- Return the string to display depending on whether Decl is a
487 function Param_String
return String is
489 if Is_Parameter
(Decl
) then
496 -- Start of processing for Get_Full_Type
499 case Get_Type
(Decl
) is
500 when 'A' => return "array type";
501 when 'B' => return "boolean type";
502 when 'C' => return "class-wide type";
503 when 'D' => return "decimal type";
504 when 'E' => return "enumeration type";
505 when 'F' => return "float type";
506 when 'I' => return "integer type";
507 when 'M' => return "modular type";
508 when 'O' => return "fixed type";
509 when 'P' => return "access type";
510 when 'R' => return "record type";
511 when 'S' => return "string type";
512 when 'T' => return "task type";
513 when 'W' => return "protected type";
515 when 'a' => return "array type";
516 when 'b' => return Param_String
& "boolean object";
517 when 'c' => return Param_String
& "class-wide object";
518 when 'd' => return Param_String
& "decimal object";
519 when 'e' => return Param_String
& "enumeration object";
520 when 'f' => return Param_String
& "float object";
521 when 'i' => return Param_String
& "integer object";
522 when 'm' => return Param_String
& "modular object";
523 when 'o' => return Param_String
& "fixed object";
524 when 'p' => return Param_String
& "access object";
525 when 'r' => return Param_String
& "record object";
526 when 's' => return Param_String
& "string object";
527 when 't' => return Param_String
& "task object";
528 when 'w' => return Param_String
& "protected object";
529 when 'x' => return Param_String
& "abstract procedure";
530 when 'y' => return Param_String
& "abstract function";
532 when 'K' => return "package";
533 when 'k' => return "generic package";
534 when 'L' => return "statement label";
535 when 'l' => return "loop label";
536 when 'N' => return "named number";
537 when 'n' => return "enumeration literal";
538 when 'q' => return "block label";
539 when 'U' => return "procedure";
540 when 'u' => return "generic procedure";
541 when 'V' => return "function";
542 when 'v' => return "generic function";
543 when 'X' => return "exception";
544 when 'Y' => return "entry";
546 when '+' => return "private type";
548 -- The above should be the only possibilities, but for this kind
549 -- of informational output, we don't want to bomb if we find
550 -- something else, so just return three question marks when we
551 -- have an unknown Abbrev value
554 return "??? (" & Get_Type
(Decl
) & ")";
558 --------------------------
559 -- Skip_To_First_X_Line --
560 --------------------------
562 procedure Skip_To_First_X_Line
563 (File
: in out ALI_File
;
567 Ali
: String_Access
renames File
.Buffer
;
569 Ptr
: Positive := Ali
'First;
570 Num_Dependencies
: Natural := 0;
571 File_Ref
: File_Reference
;
572 File_Start
: Positive;
574 Gnatchop_Offset
: Integer;
575 Gnatchop_Name
: Positive;
578 -- Read all the lines possibly processing with-clauses and dependency
579 -- information and exit on finding the first Xref line.
580 -- A fall-through of the loop means that there is no xref information
581 -- which is an error condition.
583 while Ali
(Ptr
) /= EOF
loop
585 if D_Lines
and then Ali
(Ptr
) = 'D' then
587 -- Found dependency information. Format looks like:
588 -- D src-nam time-stmp checksum [subunit-name] [line:file-name]
590 -- Skip the D and parse the filenam
593 Parse_Token
(Ali
, Ptr
, Token
);
597 Num_Dependencies
:= Num_Dependencies
+ 1;
598 Set_Last
(File
.Dep
, Num_Dependencies
);
600 Parse_Token
(Ali
, Ptr
, Token
); -- Skip time-stamp
601 Parse_Token
(Ali
, Ptr
, Token
); -- Skip checksum
602 Parse_Token
(Ali
, Ptr
, Token
); -- Read next entity on the line
604 if not (Ali
(Token
) in '0' .. '9') then
605 Parse_Token
(Ali
, Ptr
, Token
); -- Was a subunit name
608 -- Did we have a gnatchop-ed file with a pragma Source_Reference ?
610 Gnatchop_Offset
:= 0;
612 if Ali
(Token
) in '0' .. '9' then
613 Gnatchop_Name
:= Token
;
614 while Ali
(Gnatchop_Name
) /= ':' loop
615 Gnatchop_Name
:= Gnatchop_Name
+ 1;
619 2 - Natural'Value (Ali
(Token
.. Gnatchop_Name
- 1));
620 Token
:= Gnatchop_Name
+ 1;
623 File
.Dep
.Table
(Num_Dependencies
) := Add_To_Xref_File
624 (Ali
(File_Start
.. File_End
),
625 Gnatchop_File
=> Ali
(Token
.. Ptr
- 1),
626 Gnatchop_Offset
=> Gnatchop_Offset
);
628 elsif W_Lines
and then Ali
(Ptr
) = 'W' then
630 -- Found with-clause information. Format looks like:
631 -- "W debug%s debug.adb debug.ali"
633 -- Skip the W and parse the .ali filename (3rd token)
635 Parse_Token
(Ali
, Ptr
, Token
);
636 Parse_Token
(Ali
, Ptr
, Token
);
637 Parse_Token
(Ali
, Ptr
, Token
);
639 File_Ref
:= Add_To_Xref_File
640 (Ali
(Token
.. Ptr
- 1), Visited
=> False);
642 elsif Ali
(Ptr
) = 'X' then
644 -- Found a cross-referencing line - stop processing
646 File
.Current_Line
:= Ptr
;
647 File
.Xref_Line
:= Ptr
;
651 Parse_EOL
(Ali
, Ptr
);
654 raise No_Xref_Information
;
655 end Skip_To_First_X_Line
;
664 Dependencies
: Boolean := False)
666 Ali
: String_Access
renames File
.Buffer
;
669 if File
.Buffer
/= null then
676 Read_File
(Name
, Ali
);
679 when Ada
.Text_IO
.Name_Error | Ada
.Text_IO
.End_Error
=>
680 raise No_Xref_Information
;
683 Skip_To_First_X_Line
(File
, D_Lines
=> True, W_Lines
=> Dependencies
);
691 (Source
: access String;
692 Ptr
: in out Positive;
693 Skip_Continuation_Line
: Boolean := False)
697 -- Skip to end of line
699 while Source
(Ptr
) /= ASCII
.CR
and then Source
(Ptr
) /= ASCII
.LF
700 and then Source
(Ptr
) /= EOF
705 if Source
(Ptr
) /= EOF
then
706 Ptr
:= Ptr
+ 1; -- skip CR or LF
709 -- Skip past CR/LF or LF/CR combination
711 if (Source
(Ptr
) = ASCII
.CR
or else Source
(Ptr
) = ASCII
.LF
)
712 and then Source
(Ptr
) /= Source
(Ptr
- 1)
717 exit when not Skip_Continuation_Line
or else Source
(Ptr
) /= '.';
721 ---------------------------
722 -- Parse_Identifier_Info --
723 ---------------------------
725 procedure Parse_Identifier_Info
726 (Pattern
: Search_Pattern
;
727 File
: in out ALI_File
;
728 Local_Symbols
: Boolean;
729 Der_Info
: Boolean := False;
730 Type_Tree
: Boolean := False;
731 Wide_Search
: Boolean := True;
732 Labels_As_Ref
: Boolean := True)
734 Ptr
: Positive renames File
.Current_Line
;
735 Ali
: String_Access
renames File
.Buffer
;
737 E_Line
: Natural; -- Line number of current entity
738 E_Col
: Natural; -- Column number of current entity
739 E_Type
: Character; -- Type of current entity
740 E_Name
: Positive; -- Pointer to begin of entity name
741 E_Global
: Boolean; -- True iff entity is global
743 R_Line
: Natural; -- Line number of current reference
744 R_Col
: Natural; -- Column number of current reference
745 R_Type
: Character; -- Type of current reference
747 Decl_Ref
: Declaration_Reference
;
748 File_Ref
: File_Reference
:= Current_Xref_File
(File
);
750 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String;
751 -- Returns the symbol name for the entity defined at the specified
752 -- line and column in the dependent unit number Eun. For this we need
753 -- to parse the ali file again because the parent entity is not in
754 -- the declaration table if it did not match the search pattern.
756 ---------------------
757 -- Get_Symbol_Name --
758 ---------------------
760 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String is
762 E_Eun
: Positive; -- Unit number of current entity
763 E_Line
: Natural; -- Line number of current entity
764 E_Col
: Natural; -- Column number of current entity
765 E_Name
: Positive; -- Pointer to begin of entity name
766 E_Type
: Character; -- Type of current entity
769 -- Look for the X lines corresponding to unit Eun
772 if Ali
(Ptr
) = 'X' then
774 Parse_Number
(Ali
, Ptr
, E_Eun
);
775 exit when E_Eun
= Eun
;
778 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
781 -- Here we are in the right Ali section, we now look for the entity
782 -- declared at position (Line, Col).
785 Parse_Number
(Ali
, Ptr
, E_Line
);
787 exit when Ali
(Ptr
) = EOF
;
789 Parse_Number
(Ali
, Ptr
, E_Col
);
790 exit when Ali
(Ptr
) = EOF
;
793 if Line
= E_Line
and then Col
= E_Col
then
794 Parse_Token
(Ali
, Ptr
, E_Name
);
795 return Ali
(E_Name
.. Ptr
- 1);
798 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
799 exit when Ali
(Ptr
) = EOF
;
802 -- We were not able to find the symbol, this should not happend but
803 -- since we don't want to stop here we return a string of three
804 -- question marks as the symbol name.
809 -- Start of processing for Parse_Identifier_Info
812 -- The identifier info looks like:
813 -- "38U9*Debug 12|36r6 36r19"
815 -- Extract the line, column and entity name information
817 Parse_Number
(Ali
, Ptr
, E_Line
);
819 if Ali
(Ptr
) > ' ' then
824 -- Ignore some of the entities (labels,...)
827 when 'l' |
'L' |
'q' =>
828 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
835 Parse_Number
(Ali
, Ptr
, E_Col
);
838 if Ali
(Ptr
) >= ' ' then
839 E_Global
:= (Ali
(Ptr
) = '*');
843 Parse_Token
(Ali
, Ptr
, E_Name
);
845 -- Exit if the symbol does not match
846 -- or if we have a local symbol and we do not want it
848 if (not Local_Symbols
and not E_Global
)
849 or else (Pattern
.Initialized
850 and then not Match
(Ali
(E_Name
.. Ptr
- 1), Pattern
.Entity
))
851 or else (E_Name
>= Ptr
)
853 Decl_Ref
:= Add_Declaration
854 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
,
855 Remove_Only
=> True);
856 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
860 -- Insert the declaration in the table
862 Decl_Ref
:= Add_Declaration
863 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
);
866 or else Ali
(Ptr
) = '('
867 or else Ali
(Ptr
) = '{'
869 -- Here we have a type derivation information. The format is
870 -- <3|12I45> which means that the current entity is derived from the
871 -- type defined in unit number 3, line 12 column 45. The pipe and
872 -- unit number is optional. It is specified only if the parent type
873 -- is not defined in the current unit.
875 -- We also have the format for generic instantiations, as in
876 -- 7a5*Uid(3|5I8[4|2]) 2|4r74
878 -- We could also have something like
880 -- that indicates that I derives from the predefined type integer.
884 if Ali
(Ptr
) in '0' .. '9' then
885 Parse_Derived_Info
: declare
886 P_Line
: Natural; -- parent entity line
887 P_Column
: Natural; -- parent entity column
888 P_Type
: Character; -- parent entity type
889 P_Eun
: Positive; -- parent entity file number
892 Parse_Number
(Ali
, Ptr
, P_Line
);
894 -- If we have a pipe then the first number was the unit number
896 if Ali
(Ptr
) = '|' then
900 -- Now we have the line number
902 Parse_Number
(Ali
, Ptr
, P_Line
);
905 -- We don't have a unit number specified, so we set P_Eun to
908 for K
in Dependencies_Tables
.First
.. Last
(File
.Dep
) loop
910 exit when File
.Dep
.Table
(K
) = File_Ref
;
914 -- Then parse the type and column number
918 Parse_Number
(Ali
, Ptr
, P_Column
);
920 -- Skip the information for generics instantiations
922 if Ali
(Ptr
) = '[' then
924 Num_Brackets
: Natural := 1;
926 while Num_Brackets
/= 0 loop
928 if Ali
(Ptr
) = '[' then
929 Num_Brackets
:= Num_Brackets
+ 1;
930 elsif Ali
(Ptr
) = ']' then
931 Num_Brackets
:= Num_Brackets
- 1;
939 -- Skip '>', or ')' or '>'
943 -- The derived info is needed only is the derived info mode is
944 -- on or if we want to output the type hierarchy
946 if Der_Info
or else Type_Tree
then
948 Symbol
: constant String :=
949 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
);
951 if Symbol
/= "???" then
957 File
.Dep
.Table
(P_Eun
));
963 and then (Pattern
.File_Ref
= Empty_File
965 Pattern
.File_Ref
= Current_Xref_File
(File
))
967 Search_Parent_Tree
: declare
968 Pattern
: Search_Pattern
; -- Parent type pattern
969 File_Pos_Backup
: Positive;
974 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
)
975 & ':' & Get_Gnatchop_File
(File
.Dep
.Table
(P_Eun
))
976 & ':' & Get_Line
(Get_Parent
(Decl_Ref
))
977 & ':' & Get_Column
(Get_Parent
(Decl_Ref
)),
980 -- No default match is needed to look for the parent type
981 -- since we are using the fully qualified symbol name:
982 -- symbol:file:line:column
984 Set_Default_Match
(False);
986 -- The parent hierarchy is defined in the same unit as
987 -- the derived type. So we want to revisit the unit.
989 File_Pos_Backup
:= File
.Current_Line
;
992 (File
, D_Lines
=> False, W_Lines
=> False);
994 while File
.Buffer
(File
.Current_Line
) /= EOF
loop
995 Parse_X_Filename
(File
);
996 Parse_Identifier_Info
999 Local_Symbols
=> False,
1000 Der_Info
=> Der_Info
,
1002 Wide_Search
=> False,
1003 Labels_As_Ref
=> Labels_As_Ref
);
1006 File
.Current_Line
:= File_Pos_Backup
;
1007 end Search_Parent_Tree
;
1009 end Parse_Derived_Info
;
1012 while Ali
(Ptr
) /= '>'
1013 and then Ali
(Ptr
) /= ')'
1014 and then Ali
(Ptr
) /= '}'
1021 elsif Ali
(Ptr
) = '=' then
1023 P_Line
, P_Column
: Natural;
1027 Parse_Number
(Ali
, Ptr
, P_Line
);
1029 Parse_Number
(Ali
, Ptr
, P_Column
);
1033 -- To find the body, we will have to parse the file too
1037 File_Ref
: File_Reference
;
1038 File_Name
: constant String :=
1039 Get_Gnatchop_File
(File
.X_File
);
1041 File_Ref
:= Add_To_Xref_File
(ALI_File_Name
(File_Name
), False);
1045 -- Parse references to this entity.
1046 -- Ptr points to next reference with leading blanks
1049 -- Process references on current line
1051 while Ali
(Ptr
) = ' ' or Ali
(Ptr
) = ASCII
.HT
loop
1053 -- For every reference read the line, type and column,
1054 -- optionally preceded by a file number and a pipe symbol.
1056 Parse_Number
(Ali
, Ptr
, R_Line
);
1058 if Ali
(Ptr
) = Pipe
then
1060 File_Ref
:= File_Name
(File
, R_Line
);
1062 Parse_Number
(Ali
, Ptr
, R_Line
);
1065 if Ali
(Ptr
) > ' ' then
1066 R_Type
:= Ali
(Ptr
);
1070 -- Imported entities might special indication as to their external
1072 -- 5U14*Foo2 5>20 6b<c,myfoo2>22
1075 and then Ali
(Ptr
) = '<'
1077 while Ptr
<= Ali
'Last
1078 and then Ali
(Ptr
) /= '>'
1085 Parse_Number
(Ali
, Ptr
, R_Col
);
1087 -- Insert the reference or body in the table
1090 (Decl_Ref
, File_Ref
, R_Line
, R_Col
, R_Type
, Labels_As_Ref
);
1092 -- Skip generic information, if any
1094 if Ali
(Ptr
) = '[' then
1096 Num_Nested
: Integer := 1;
1100 while Num_Nested
/= 0 loop
1101 if Ali
(Ptr
) = ']' then
1102 Num_Nested
:= Num_Nested
- 1;
1103 elsif Ali
(Ptr
) = '[' then
1104 Num_Nested
:= Num_Nested
+ 1;
1114 Parse_EOL
(Ali
, Ptr
);
1116 -- Loop until new line is no continuation line
1118 exit when Ali
(Ptr
) /= '.';
1121 end Parse_Identifier_Info
;
1127 procedure Parse_Number
1128 (Source
: access String;
1129 Ptr
: in out Positive;
1130 Number
: out Natural)
1135 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1140 while Source
(Ptr
) in '0' .. '9' loop
1142 10 * Number
+ (Character'Pos (Source
(Ptr
)) - Character'Pos ('0'));
1151 procedure Parse_Token
1152 (Source
: access String;
1153 Ptr
: in out Positive;
1154 Token_Ptr
: out Positive)
1156 In_Quotes
: Character := ASCII
.NUL
;
1161 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1167 -- Find end-of-token
1169 while (In_Quotes
/= ASCII
.NUL
or else
1170 not (Source
(Ptr
) = ' '
1171 or else Source
(Ptr
) = ASCII
.HT
1172 or else Source
(Ptr
) = '<'
1173 or else Source
(Ptr
) = '{'
1174 or else Source
(Ptr
) = '='
1175 or else Source
(Ptr
) = '('))
1176 and then Source
(Ptr
) >= ' '
1178 -- Double-quotes are used for operators
1179 -- Simple-quotes are used for character constants, for instance when
1180 -- they are found in an enumeration type "type A is ('+', '-');"
1182 case Source
(Ptr
) is
1184 if In_Quotes
= Source
(Ptr
) then
1185 In_Quotes
:= ASCII
.NUL
;
1186 elsif In_Quotes
= ASCII
.NUL
then
1187 In_Quotes
:= Source
(Ptr
);
1198 ----------------------
1199 -- Parse_X_Filename --
1200 ----------------------
1202 procedure Parse_X_Filename
(File
: in out ALI_File
) is
1203 Ali
: String_Access
renames File
.Buffer
;
1204 Ptr
: Positive renames File
.Current_Line
;
1208 while Ali
(Ptr
) = 'X' loop
1210 -- The current line is the start of a new Xref file section,
1211 -- whose format looks like:
1215 -- Skip the X and read the file number for the new X_File
1218 Parse_Number
(Ali
, Ptr
, File_Nr
);
1221 File
.X_File
:= File
.Dep
.Table
(File_Nr
);
1224 Parse_EOL
(Ali
, Ptr
);
1226 end Parse_X_Filename
;
1228 --------------------
1229 -- Print_Gnatfind --
1230 --------------------
1232 procedure Print_Gnatfind
1233 (References
: Boolean;
1234 Full_Path_Name
: Boolean)
1236 Decls
: constant Declaration_Array_Access
:= Get_Declarations
;
1237 Decl
: Declaration_Reference
;
1238 Arr
: Reference_Array_Access
;
1242 Msg
: String := " ");
1243 -- Print a reference, according to the extended tag of the output
1251 Msg
: String := " ")
1253 F
: String_Access
:=
1254 Osint
.To_Host_File_Spec
1255 (Get_Gnatchop_File
(Ref
, Full_Path_Name
));
1257 Buffer
: constant String :=
1259 ":" & Get_Line
(Ref
) &
1260 ":" & Get_Column
(Ref
) &
1263 Num_Blanks
: Integer := Longest_File_Name
+ 10 - Buffer
'Length;
1267 Num_Blanks
:= Integer'Max (0, Num_Blanks
);
1270 & String'(1 .. Num_Blanks => ' ')
1271 & Msg & " " & Get_Symbol (Decl));
1273 if Get_Source_Line (Ref)'Length /= 0 then
1274 Write_Line (" " & Get_Source_Line (Ref));
1278 -- Start of processing for Print_Gnatfind
1281 for D in Decls'Range loop
1284 if Match (Decl) then
1286 -- Output the declaration
1289 Parent : constant Declaration_Reference := Get_Parent (Decl);
1291 F : String_Access :=
1292 Osint.To_Host_File_Spec
1293 (Get_Gnatchop_File (Decl, Full_Path_Name));
1295 Buffer : constant String :=
1297 ":" & Get_Line (Decl) &
1298 ":" & Get_Column (Decl) &
1301 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1305 Num_Blanks := Integer'Max (0, Num_Blanks);
1307 (Buffer & String'(1 .. Num_Blanks
=> ' ')
1308 & "(spec) " & Get_Symbol
(Decl
));
1310 if Parent
/= Empty_Declaration
then
1311 F
:= Osint
.To_Host_File_Spec
(Get_Gnatchop_File
(Parent
));
1313 (Buffer
& String'(1 .. Num_Blanks => ' ')
1314 & " derived from " & Get_Symbol (Parent)
1317 & ':' & Get_Line (Parent)
1318 & ':' & Get_Column (Parent) & ')');
1323 if Get_Source_Line (Decl)'Length /= 0 then
1324 Write_Line (" " & Get_Source_Line (Decl));
1327 -- Output the body (sorted)
1329 Arr := Get_References (Decl, Get_Bodies => True);
1331 for R in Arr'Range loop
1332 Print_Ref (Arr (R), "(body)");
1338 Arr := Get_References
1339 (Decl, Get_Writes => True, Get_Reads => True);
1341 for R in Arr'Range loop
1342 Print_Ref (Arr (R));
1355 procedure Print_Unused (Full_Path_Name : in Boolean) is
1356 Decls : constant Declaration_Array_Access := Get_Declarations;
1357 Decl : Declaration_Reference;
1358 Arr : Reference_Array_Access;
1362 for D in Decls'Range loop
1366 (Decl, Get_Reads => True, Get_Writes => True) = 0
1368 F := Osint.To_Host_File_Spec
1369 (Get_Gnatchop_File (Decl, Full_Path_Name));
1370 Write_Str (Get_Symbol (Decl)
1372 & Get_Full_Type (Decl)
1378 & Get_Column (Decl));
1381 -- Print the body if any
1383 Arr := Get_References (Decl, Get_Bodies => True);
1385 for R in Arr'Range loop
1386 F := Osint.To_Host_File_Spec
1387 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1390 & ':' & Get_Line (Arr (R))
1391 & ':' & Get_Column (Arr (R)));
1405 procedure Print_Vi (Full_Path_Name : in Boolean) is
1406 Tab : constant Character := ASCII.HT;
1407 Decls : constant Declaration_Array_Access :=
1408 Get_Declarations (Sorted => False);
1409 Decl : Declaration_Reference;
1410 Arr : Reference_Array_Access;
1414 for D in Decls'Range loop
1417 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1418 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1421 -- Print the body if any
1423 Arr := Get_References (Decl, Get_Bodies => True);
1425 for R in Arr'Range loop
1426 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1428 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1434 -- Print the modifications
1436 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1438 for R in Arr'Range loop
1439 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1441 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1453 procedure Print_Xref (Full_Path_Name : in Boolean) is
1454 Decls : constant Declaration_Array_Access := Get_Declarations;
1455 Decl : Declaration_Reference;
1457 Margin : constant := 10;
1458 -- Column where file names start
1460 procedure New_Line80;
1461 -- Go to start of new line
1463 procedure Print80 (S : in String);
1464 -- Print the text, respecting the 80 columns rule.
1466 procedure Print_Ref (Line, Column : String);
1467 -- The beginning of the output is aligned on a column multiple of 9
1469 procedure Print_List
1470 (Decl : Declaration_Reference;
1472 Get_Reads : Boolean := False;
1473 Get_Writes : Boolean := False;
1474 Get_Bodies : Boolean := False);
1475 -- Print a list of references. If the list is not empty, Msg will
1476 -- be printed prior to the list.
1482 procedure New_Line80 is
1485 Write_Str (String'(1 .. Margin
- 1 => ' '));
1492 procedure Print80
(S
: in String) is
1493 Align
: Natural := Margin
- (Integer (Column
) mod Margin
);
1496 if Align
= Margin
then
1500 Write_Str
(String'(1 .. Align => ' ') & S);
1507 procedure Print_Ref (Line, Column : String) is
1508 Line_Align : constant Integer := 4 - Line'Length;
1510 S : constant String := String'(1 .. Line_Align
=> ' ')
1511 & Line
& ':' & Column
;
1513 Align
: Natural := Margin
- (Integer (Output
.Column
) mod Margin
);
1516 if Align
= Margin
then
1520 if Integer (Output
.Column
) + Align
+ S
'Length > 79 then
1525 Write_Str
(String'(1 .. Align => ' ') & S);
1532 procedure Print_List
1533 (Decl : Declaration_Reference;
1535 Get_Reads : Boolean := False;
1536 Get_Writes : Boolean := False;
1537 Get_Bodies : Boolean := False)
1539 Arr : Reference_Array_Access :=
1542 Get_Writes => Get_Writes,
1543 Get_Reads => Get_Reads,
1544 Get_Bodies => Get_Bodies);
1545 File : File_Reference := Empty_File;
1549 if Arr'Length /= 0 then
1554 for R in Arr'Range loop
1555 if Get_File_Ref (Arr (R)) /= File then
1556 if File /= Empty_File then
1560 File := Get_File_Ref (Arr (R));
1561 F := Osint.To_Host_File_Spec
1562 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1563 Write_Str (F.all & ' ');
1567 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1575 -- Start of processing for Print_Xref
1578 for D in Decls'Range loop
1581 Write_Str (Get_Symbol (Decl));
1583 while Column < Type_Position loop
1587 Write_Line (Get_Full_Type (Decl));
1589 Write_Parent_Info : declare
1590 Parent : constant Declaration_Reference := Get_Parent (Decl);
1593 if Parent /= Empty_Declaration then
1594 Write_Str (" Ptype: ");
1595 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1598 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1599 Print80 (" " & Get_Symbol (Parent));
1602 end Write_Parent_Info;
1604 Write_Str (" Decl: ");
1605 F := Osint.To_Host_File_Spec
1606 (Get_Gnatchop_File (Decl, Full_Path_Name));
1607 Print80 (F.all & ' ');
1609 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1612 (Decl, " Body: ", Get_Bodies => True);
1614 (Decl, " Modi: ", Get_Writes => True);
1616 (Decl, " Ref: ", Get_Reads => True);
1626 (Pattern : Search_Pattern;
1627 Local_Symbols : Boolean;
1628 Wide_Search : Boolean;
1629 Read_Only : Boolean;
1631 Type_Tree : Boolean)
1633 type String_Access is access String;
1634 procedure Free is new Unchecked_Deallocation (String, String_Access);
1637 File_Ref : File_Reference;
1638 Strip_Num : Natural := 0;
1639 Ali_Name : String_Access;
1642 -- If we want all the .ali files, then find them
1649 -- Get the next unread ali file
1651 File_Ref := Next_Unvisited_File;
1653 exit when File_Ref = Empty_File;
1655 -- Find the ALI file to use. Most of the time, it will be the unit
1656 -- name, with a different extension. However, when dealing with
1657 -- separates the ALI file is in fact the parent's ALI file (and this
1658 -- is recursive, in case the parent itself is a separate).
1663 Ali_Name := new String'
1664 (Get_File
(File_Ref
, With_Dir
=> True, Strip
=> Strip_Num
));
1666 -- Stripped too many things...
1668 if Ali_Name
.all = "" then
1669 if Get_Emit_Warning
(File_Ref
) then
1672 ("warning : file " & Get_File
(File_Ref
, With_Dir
=> True)
1674 Set_Standard_Output
;
1679 -- If not found, try the parent's ALI file (this is needed for
1680 -- separate units and subprograms).
1682 -- Reset the cached directory first, in case the separate's
1683 -- ALI file is not in the same directory.
1685 elsif not File_Exists
(Ali_Name
.all) then
1686 Strip_Num
:= Strip_Num
+ 1;
1687 Reset_Directory
(File_Ref
);
1689 -- Else we finally found it
1696 -- If we had to get the parent's ALI, insert it in the list as usual.
1697 -- This is to avoid parsing it twice in case it has already been
1700 if Ali_Name
/= null and then Strip_Num
/= 0 then
1701 File_Ref
:= Add_To_Xref_File
1702 (File_Name
=> Ali_Name
.all,
1705 -- Now that we have a file name, parse it to find any reference to
1708 elsif Ali_Name
/= null
1709 and then (Read_Only
or else Is_Writable_File
(Ali_Name
.all))
1712 Open
(Ali_Name
.all, ALIfile
);
1713 while ALIfile
.Buffer
(ALIfile
.Current_Line
) /= EOF
loop
1714 Parse_X_Filename
(ALIfile
);
1715 Parse_Identifier_Info
1716 (Pattern
, ALIfile
, Local_Symbols
,
1717 Der_Info
, Type_Tree
, Wide_Search
, Labels_As_Ref
=> True);
1721 when No_Xref_Information
=>
1722 if Get_Emit_Warning
(File_Ref
) then
1725 ("warning : No cross-referencing information in "
1727 Set_Standard_Output
;
1740 procedure Search_Xref
1741 (Local_Symbols
: Boolean;
1742 Read_Only
: Boolean;
1746 File_Ref
: File_Reference
;
1747 Null_Pattern
: Search_Pattern
;
1750 Null_Pattern
.Initialized
:= False;
1753 -- Find the next unvisited file
1755 File_Ref
:= Next_Unvisited_File
;
1756 exit when File_Ref
= Empty_File
;
1758 -- Search the object directories for the .ali file
1761 F
: constant String := Get_File
(File_Ref
, With_Dir
=> True);
1764 if Read_Only
or else Is_Writable_File
(F
) then
1765 Open
(F
, ALIfile
, True);
1767 while ALIfile
.Buffer
(ALIfile
.Current_Line
) /= EOF
loop
1768 Parse_X_Filename
(ALIfile
);
1769 Parse_Identifier_Info
1770 (Null_Pattern
, ALIfile
, Local_Symbols
, Der_Info
,
1771 Labels_As_Ref
=> False);
1776 when No_Xref_Information
=> null;