1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2009, 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 NT and OS/2)
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 'I' => return "integer type";
512 when 'M' => return "modular type";
513 when 'O' => return "fixed type";
514 when 'P' => return "access type";
515 when 'R' => return "record type";
516 when 'S' => return "string type";
517 when 'T' => return "task type";
518 when 'W' => return "protected type";
520 when 'a' => return "array type";
521 when 'b' => return Param_String
& "boolean object";
522 when 'c' => return Param_String
& "class-wide object";
523 when 'd' => return Param_String
& "decimal object";
524 when 'e' => return Param_String
& "enumeration object";
525 when 'f' => return Param_String
& "float object";
526 when 'h' => return "interface";
527 when 'i' => return Param_String
& "integer object";
528 when 'm' => return Param_String
& "modular object";
529 when 'o' => return Param_String
& "fixed object";
530 when 'p' => return Param_String
& "access object";
531 when 'r' => return Param_String
& "record object";
532 when 's' => return Param_String
& "string object";
533 when 't' => return Param_String
& "task object";
534 when 'w' => return Param_String
& "protected object";
535 when 'x' => return Param_String
& "abstract procedure";
536 when 'y' => return Param_String
& "abstract function";
538 when 'K' => return "package";
539 when 'k' => return "generic package";
540 when 'L' => return "statement label";
541 when 'l' => return "loop label";
542 when 'N' => return "named number";
543 when 'n' => return "enumeration literal";
544 when 'q' => return "block label";
545 when 'U' => return "procedure";
546 when 'u' => return "generic procedure";
547 when 'V' => return "function";
548 when 'v' => return "generic function";
549 when 'X' => return "exception";
550 when 'Y' => return "entry";
552 when '+' => return "private type";
554 -- The above should be the only possibilities, but for this kind
555 -- of informational output, we don't want to bomb if we find
556 -- something else, so just return three question marks when we
557 -- have an unknown Abbrev value
560 return "??? (" & Get_Type
(Decl
) & ")";
564 --------------------------
565 -- Skip_To_First_X_Line --
566 --------------------------
568 procedure Skip_To_First_X_Line
569 (File
: in out ALI_File
;
573 Ali
: String_Access
renames File
.Buffer
;
575 Ptr
: Positive := Ali
'First;
576 Num_Dependencies
: Natural := 0;
577 File_Start
: Positive;
579 Gnatchop_Offset
: Integer;
580 Gnatchop_Name
: Positive;
582 File_Ref
: File_Reference
;
583 pragma Unreferenced
(File_Ref
);
586 -- Read all the lines possibly processing with-clauses and dependency
587 -- information and exit on finding the first Xref line.
588 -- A fall-through of the loop means that there is no xref information
589 -- which is an error condition.
591 while Ali
(Ptr
) /= EOF
loop
592 if D_Lines
and then Ali
(Ptr
) = 'D' then
594 -- Found dependency information. Format looks like:
595 -- D src-nam time-stmp checksum [subunit-name] [line:file-name]
597 -- Skip the D and parse the filenam
600 Parse_Token
(Ali
, Ptr
, Token
);
604 Num_Dependencies
:= Num_Dependencies
+ 1;
605 Set_Last
(File
.Dep
, Num_Dependencies
);
607 Parse_Token
(Ali
, Ptr
, Token
); -- Skip time-stamp
608 Parse_Token
(Ali
, Ptr
, Token
); -- Skip checksum
609 Parse_Token
(Ali
, Ptr
, Token
); -- Read next entity on the line
611 if not (Ali
(Token
) in '0' .. '9') then
612 Parse_Token
(Ali
, Ptr
, Token
); -- Was a subunit name
615 -- Did we have a gnatchop-ed file with a pragma Source_Reference ?
617 Gnatchop_Offset
:= 0;
619 if Ali
(Token
) in '0' .. '9' then
620 Gnatchop_Name
:= Token
;
621 while Ali
(Gnatchop_Name
) /= ':' loop
622 Gnatchop_Name
:= Gnatchop_Name
+ 1;
626 2 - Natural'Value (Ali
(Token
.. Gnatchop_Name
- 1));
627 Token
:= Gnatchop_Name
+ 1;
630 File
.Dep
.Table
(Num_Dependencies
) := Add_To_Xref_File
631 (Ali
(File_Start
.. File_End
),
632 Gnatchop_File
=> Ali
(Token
.. Ptr
- 1),
633 Gnatchop_Offset
=> Gnatchop_Offset
);
635 elsif W_Lines
and then Ali
(Ptr
) = 'W' then
637 -- Found with-clause information. Format looks like:
638 -- "W debug%s debug.adb debug.ali"
640 -- Skip the W and parse the .ali filename (3rd token)
642 Parse_Token
(Ali
, Ptr
, Token
);
643 Parse_Token
(Ali
, Ptr
, Token
);
644 Parse_Token
(Ali
, Ptr
, Token
);
647 Add_To_Xref_File
(Ali
(Token
.. Ptr
- 1), Visited
=> False);
649 elsif Ali
(Ptr
) = 'X' then
651 -- Found a cross-referencing line - stop processing
653 File
.Current_Line
:= Ptr
;
654 File
.Xref_Line
:= Ptr
;
658 Parse_EOL
(Ali
, Ptr
);
661 raise No_Xref_Information
;
662 end Skip_To_First_X_Line
;
671 Dependencies
: Boolean := False)
673 Ali
: String_Access
renames File
.Buffer
;
674 pragma Warnings
(Off
, Ali
);
677 if File
.Buffer
/= null then
684 Read_File
(Name
, Ali
);
687 when Ada
.Text_IO
.Name_Error | Ada
.Text_IO
.End_Error
=>
688 raise No_Xref_Information
;
691 Skip_To_First_X_Line
(File
, D_Lines
=> True, W_Lines
=> Dependencies
);
699 (Source
: not null access String;
700 Ptr
: in out Positive;
701 Skip_Continuation_Line
: Boolean := False)
705 -- Skip to end of line
707 while Source
(Ptr
) /= ASCII
.CR
and then Source
(Ptr
) /= ASCII
.LF
708 and then Source
(Ptr
) /= EOF
713 -- Skip CR or LF if not at end of file
715 if Source
(Ptr
) /= EOF
then
719 -- Skip past CR/LF or LF/CR combination
721 if (Source
(Ptr
) = ASCII
.CR
or else Source
(Ptr
) = ASCII
.LF
)
722 and then Source
(Ptr
) /= Source
(Ptr
- 1)
727 exit when not Skip_Continuation_Line
or else Source
(Ptr
) /= '.';
731 ---------------------------
732 -- Parse_Identifier_Info --
733 ---------------------------
735 procedure Parse_Identifier_Info
736 (Pattern
: Search_Pattern
;
737 File
: in out ALI_File
;
738 Local_Symbols
: Boolean;
739 Der_Info
: Boolean := False;
740 Type_Tree
: Boolean := False;
741 Wide_Search
: Boolean := True;
742 Labels_As_Ref
: Boolean := True)
744 Ptr
: Positive renames File
.Current_Line
;
745 Ali
: String_Access
renames File
.Buffer
;
747 E_Line
: Natural; -- Line number of current entity
748 E_Col
: Natural; -- Column number of current entity
749 E_Type
: Character; -- Type of current entity
750 E_Name
: Positive; -- Pointer to begin of entity name
751 E_Global
: Boolean; -- True iff entity is global
753 R_Line
: Natural; -- Line number of current reference
754 R_Col
: Natural; -- Column number of current reference
755 R_Type
: Character; -- Type of current reference
757 Decl_Ref
: Declaration_Reference
;
758 File_Ref
: File_Reference
:= Current_Xref_File
(File
);
760 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String;
761 -- Returns the symbol name for the entity defined at the specified
762 -- line and column in the dependent unit number Eun. For this we need
763 -- to parse the ali file again because the parent entity is not in
764 -- the declaration table if it did not match the search pattern.
766 procedure Skip_To_Matching_Closing_Bracket
;
767 -- When Ptr points to an opening square bracket, moves it to the
768 -- character following the matching closing bracket
770 ---------------------
771 -- Get_Symbol_Name --
772 ---------------------
774 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String is
776 E_Eun
: Positive; -- Unit number of current entity
777 E_Line
: Natural; -- Line number of current entity
778 E_Col
: Natural; -- Column number of current entity
779 E_Name
: Positive; -- Pointer to begin of entity name
782 -- Look for the X lines corresponding to unit Eun
785 if Ali
(Ptr
) = 'X' then
787 Parse_Number
(Ali
, Ptr
, E_Eun
);
788 exit when E_Eun
= Eun
;
791 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
794 -- Here we are in the right Ali section, we now look for the entity
795 -- declared at position (Line, Col).
798 Parse_Number
(Ali
, Ptr
, E_Line
);
799 exit when Ali
(Ptr
) = EOF
;
801 Parse_Number
(Ali
, Ptr
, E_Col
);
802 exit when Ali
(Ptr
) = EOF
;
805 if Line
= E_Line
and then Col
= E_Col
then
806 Parse_Token
(Ali
, Ptr
, E_Name
);
807 return Ali
(E_Name
.. Ptr
- 1);
810 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
811 exit when Ali
(Ptr
) = EOF
;
814 -- We were not able to find the symbol, this should not happen but
815 -- since we don't want to stop here we return a string of three
816 -- question marks as the symbol name.
821 --------------------------------------
822 -- Skip_To_Matching_Closing_Bracket --
823 --------------------------------------
825 procedure Skip_To_Matching_Closing_Bracket
is
826 Num_Brackets
: Natural;
830 while Num_Brackets
/= 0 loop
832 if Ali
(Ptr
) = '[' then
833 Num_Brackets
:= Num_Brackets
+ 1;
834 elsif Ali
(Ptr
) = ']' then
835 Num_Brackets
:= Num_Brackets
- 1;
840 end Skip_To_Matching_Closing_Bracket
;
842 -- Start of processing for Parse_Identifier_Info
845 -- The identifier info looks like:
846 -- "38U9*Debug 12|36r6 36r19"
848 -- Extract the line, column and entity name information
850 Parse_Number
(Ali
, Ptr
, E_Line
);
852 if Ali
(Ptr
) > ' ' then
857 -- Ignore some of the entities (labels,...)
860 when 'l' |
'L' |
'q' =>
861 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
868 Parse_Number
(Ali
, Ptr
, E_Col
);
871 if Ali
(Ptr
) >= ' ' then
872 E_Global
:= (Ali
(Ptr
) = '*');
876 Parse_Token
(Ali
, Ptr
, E_Name
);
878 -- Exit if the symbol does not match
879 -- or if we have a local symbol and we do not want it
881 if (not Local_Symbols
and not E_Global
)
882 or else (Pattern
.Initialized
883 and then not Match
(Ali
(E_Name
.. Ptr
- 1), Pattern
.Entity
))
884 or else (E_Name
>= Ptr
)
886 Decl_Ref
:= Add_Declaration
887 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
,
888 Remove_Only
=> True);
889 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
893 -- Insert the declaration in the table
895 Decl_Ref
:= Add_Declaration
896 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
);
898 if Ali
(Ptr
) = '[' then
899 Skip_To_Matching_Closing_Bracket
;
902 -- Skip any renaming indication
904 if Ali
(Ptr
) = '=' then
906 P_Line
, P_Column
: Natural;
907 pragma Warnings
(Off
, P_Line
);
908 pragma Warnings
(Off
, P_Column
);
911 Parse_Number
(Ali
, Ptr
, P_Line
);
913 Parse_Number
(Ali
, Ptr
, P_Column
);
918 or else Ali
(Ptr
) = '('
919 or else Ali
(Ptr
) = '{'
921 -- Here we have a type derivation information. The format is
922 -- <3|12I45> which means that the current entity is derived from the
923 -- type defined in unit number 3, line 12 column 45. The pipe and
924 -- unit number is optional. It is specified only if the parent type
925 -- is not defined in the current unit.
927 -- We also have the format for generic instantiations, as in
928 -- 7a5*Uid(3|5I8[4|2]) 2|4r74
930 -- We could also have something like
932 -- that indicates that I derives from the predefined type integer.
936 if Ali
(Ptr
) in '0' .. '9' then
937 Parse_Derived_Info
: declare
938 P_Line
: Natural; -- parent entity line
939 P_Column
: Natural; -- parent entity column
940 P_Eun
: Positive; -- parent entity file number
943 Parse_Number
(Ali
, Ptr
, P_Line
);
945 -- If we have a pipe then the first number was the unit number
947 if Ali
(Ptr
) = '|' then
951 -- Now we have the line number
953 Parse_Number
(Ali
, Ptr
, P_Line
);
956 -- We don't have a unit number specified, so we set P_Eun to
959 for K
in Dependencies_Tables
.First
.. Last
(File
.Dep
) loop
961 exit when File
.Dep
.Table
(K
) = File_Ref
;
965 -- Then parse the type and column number
968 Parse_Number
(Ali
, Ptr
, P_Column
);
970 -- Skip the information for generics instantiations
972 if Ali
(Ptr
) = '[' then
973 Skip_To_Matching_Closing_Bracket
;
976 -- Skip '>', or ')' or '>'
980 -- The derived info is needed only is the derived info mode is
981 -- on or if we want to output the type hierarchy
983 if Der_Info
or else Type_Tree
then
985 Symbol
: constant String :=
986 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
);
988 if Symbol
/= "???" then
994 File
.Dep
.Table
(P_Eun
));
1000 and then (Pattern
.File_Ref
= Empty_File
1002 Pattern
.File_Ref
= Current_Xref_File
(File
))
1004 Search_Parent_Tree
: declare
1005 Pattern
: Search_Pattern
; -- Parent type pattern
1006 File_Pos_Backup
: Positive;
1011 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
)
1012 & ':' & Get_Gnatchop_File
(File
.Dep
.Table
(P_Eun
))
1013 & ':' & Get_Line
(Get_Parent
(Decl_Ref
))
1014 & ':' & Get_Column
(Get_Parent
(Decl_Ref
)),
1017 -- No default match is needed to look for the parent type
1018 -- since we are using the fully qualified symbol name:
1019 -- symbol:file:line:column
1021 Set_Default_Match
(False);
1023 -- The parent hierarchy is defined in the same unit as
1024 -- the derived type. So we want to revisit the unit.
1026 File_Pos_Backup
:= File
.Current_Line
;
1028 Skip_To_First_X_Line
1029 (File
, D_Lines
=> False, W_Lines
=> False);
1031 while File
.Buffer
(File
.Current_Line
) /= EOF
loop
1032 Parse_X_Filename
(File
);
1033 Parse_Identifier_Info
1034 (Pattern
=> Pattern
,
1036 Local_Symbols
=> False,
1037 Der_Info
=> Der_Info
,
1039 Wide_Search
=> False,
1040 Labels_As_Ref
=> Labels_As_Ref
);
1043 File
.Current_Line
:= File_Pos_Backup
;
1044 end Search_Parent_Tree
;
1046 end Parse_Derived_Info
;
1049 while Ali
(Ptr
) /= '>'
1050 and then Ali
(Ptr
) /= ')'
1051 and then Ali
(Ptr
) /= '}'
1059 -- To find the body, we will have to parse the file too
1063 File_Ref
: File_Reference
;
1064 pragma Unreferenced
(File_Ref
);
1065 File_Name
: constant String := Get_Gnatchop_File
(File
.X_File
);
1067 File_Ref
:= Add_To_Xref_File
(ALI_File_Name
(File_Name
), False);
1071 -- Parse references to this entity.
1072 -- Ptr points to next reference with leading blanks
1075 -- Process references on current line
1077 while Ali
(Ptr
) = ' ' or else Ali
(Ptr
) = ASCII
.HT
loop
1079 -- For every reference read the line, type and column,
1080 -- optionally preceded by a file number and a pipe symbol.
1082 Parse_Number
(Ali
, Ptr
, R_Line
);
1084 if Ali
(Ptr
) = Pipe
then
1086 File_Ref
:= File_Name
(File
, R_Line
);
1088 Parse_Number
(Ali
, Ptr
, R_Line
);
1091 if Ali
(Ptr
) > ' ' then
1092 R_Type
:= Ali
(Ptr
);
1096 -- Imported entities might special indication as to their external
1098 -- 5U14*Foo2 5>20 6b<c,myfoo2>22
1101 and then Ali
(Ptr
) = '<'
1103 while Ptr
<= Ali
'Last
1104 and then Ali
(Ptr
) /= '>'
1111 Parse_Number
(Ali
, Ptr
, R_Col
);
1113 -- Insert the reference or body in the table
1116 (Decl_Ref
, File_Ref
, R_Line
, R_Col
, R_Type
, Labels_As_Ref
);
1118 -- Skip generic information, if any
1120 if Ali
(Ptr
) = '[' then
1122 Num_Nested
: Integer := 1;
1126 while Num_Nested
/= 0 loop
1127 if Ali
(Ptr
) = ']' then
1128 Num_Nested
:= Num_Nested
- 1;
1129 elsif Ali
(Ptr
) = '[' then
1130 Num_Nested
:= Num_Nested
+ 1;
1140 Parse_EOL
(Ali
, Ptr
);
1142 -- Loop until new line is no continuation line
1144 exit when Ali
(Ptr
) /= '.';
1147 end Parse_Identifier_Info
;
1153 procedure Parse_Number
1154 (Source
: not null access String;
1155 Ptr
: in out Positive;
1156 Number
: out Natural)
1161 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1166 while Source
(Ptr
) in '0' .. '9' loop
1168 10 * Number
+ (Character'Pos (Source
(Ptr
)) - Character'Pos ('0'));
1177 procedure Parse_Token
1178 (Source
: not null access String;
1179 Ptr
: in out Positive;
1180 Token_Ptr
: out Positive)
1182 In_Quotes
: Character := ASCII
.NUL
;
1187 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1193 -- Find end-of-token
1195 while (In_Quotes
/= ASCII
.NUL
or else
1196 not (Source
(Ptr
) = ' '
1197 or else Source
(Ptr
) = ASCII
.HT
1198 or else Source
(Ptr
) = '<'
1199 or else Source
(Ptr
) = '{'
1200 or else Source
(Ptr
) = '['
1201 or else Source
(Ptr
) = '='
1202 or else Source
(Ptr
) = '('))
1203 and then Source
(Ptr
) >= ' '
1205 -- Double-quotes are used for operators
1206 -- Simple-quotes are used for character constants, for instance when
1207 -- they are found in an enumeration type "type A is ('+', '-');"
1209 case Source
(Ptr
) is
1211 if In_Quotes
= Source
(Ptr
) then
1212 In_Quotes
:= ASCII
.NUL
;
1213 elsif In_Quotes
= ASCII
.NUL
then
1214 In_Quotes
:= Source
(Ptr
);
1225 ----------------------
1226 -- Parse_X_Filename --
1227 ----------------------
1229 procedure Parse_X_Filename
(File
: in out ALI_File
) is
1230 Ali
: String_Access
renames File
.Buffer
;
1231 Ptr
: Positive renames File
.Current_Line
;
1235 while Ali
(Ptr
) = 'X' loop
1237 -- The current line is the start of a new Xref file section,
1238 -- whose format looks like:
1242 -- Skip the X and read the file number for the new X_File
1245 Parse_Number
(Ali
, Ptr
, File_Nr
);
1248 File
.X_File
:= File
.Dep
.Table
(File_Nr
);
1251 Parse_EOL
(Ali
, Ptr
);
1253 end Parse_X_Filename
;
1255 --------------------
1256 -- Print_Gnatfind --
1257 --------------------
1259 procedure Print_Gnatfind
1260 (References
: Boolean;
1261 Full_Path_Name
: Boolean)
1263 Decls
: constant Declaration_Array_Access
:= Get_Declarations
;
1264 Decl
: Declaration_Reference
;
1265 Arr
: Reference_Array_Access
;
1269 Msg
: String := " ");
1270 -- Print a reference, according to the extended tag of the output
1278 Msg
: String := " ")
1280 F
: String_Access
:=
1281 Osint
.To_Host_File_Spec
1282 (Get_Gnatchop_File
(Ref
, Full_Path_Name
));
1284 Buffer
: constant String :=
1286 ":" & Get_Line
(Ref
) &
1287 ":" & Get_Column
(Ref
) &
1290 Num_Blanks
: Integer := Longest_File_Name
+ 10 - Buffer
'Length;
1294 Num_Blanks
:= Integer'Max (0, Num_Blanks
);
1297 & String'(1 .. Num_Blanks => ' ')
1298 & Msg & " " & Get_Symbol (Decl));
1300 if Get_Source_Line (Ref)'Length /= 0 then
1301 Write_Line (" " & Get_Source_Line (Ref));
1305 -- Start of processing for Print_Gnatfind
1308 for D in Decls'Range loop
1311 if Match (Decl) then
1313 -- Output the declaration
1316 Parent : constant Declaration_Reference := Get_Parent (Decl);
1318 F : String_Access :=
1319 Osint.To_Host_File_Spec
1320 (Get_Gnatchop_File (Decl, Full_Path_Name));
1322 Buffer : constant String :=
1324 ":" & Get_Line (Decl) &
1325 ":" & Get_Column (Decl) &
1328 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1332 Num_Blanks := Integer'Max (0, Num_Blanks);
1334 (Buffer & String'(1 .. Num_Blanks
=> ' ')
1335 & "(spec) " & Get_Symbol
(Decl
));
1337 if Parent
/= Empty_Declaration
then
1338 F
:= Osint
.To_Host_File_Spec
(Get_Gnatchop_File
(Parent
));
1340 (Buffer
& String'(1 .. Num_Blanks => ' ')
1341 & " derived from " & Get_Symbol (Parent)
1344 & ':' & Get_Line (Parent)
1345 & ':' & Get_Column (Parent) & ')');
1350 if Get_Source_Line (Decl)'Length /= 0 then
1351 Write_Line (" " & Get_Source_Line (Decl));
1354 -- Output the body (sorted)
1356 Arr := Get_References (Decl, Get_Bodies => True);
1358 for R in Arr'Range loop
1359 Print_Ref (Arr (R), "(body)");
1365 Arr := Get_References
1366 (Decl, Get_Writes => True, Get_Reads => True);
1368 for R in Arr'Range loop
1369 Print_Ref (Arr (R));
1382 procedure Print_Unused (Full_Path_Name : Boolean) is
1383 Decls : constant Declaration_Array_Access := Get_Declarations;
1384 Decl : Declaration_Reference;
1385 Arr : Reference_Array_Access;
1389 for D in Decls'Range loop
1393 (Decl, Get_Reads => True, Get_Writes => True) = 0
1395 F := Osint.To_Host_File_Spec
1396 (Get_Gnatchop_File (Decl, Full_Path_Name));
1397 Write_Str (Get_Symbol (Decl)
1399 & Get_Full_Type (Decl)
1405 & Get_Column (Decl));
1408 -- Print the body if any
1410 Arr := Get_References (Decl, Get_Bodies => True);
1412 for R in Arr'Range loop
1413 F := Osint.To_Host_File_Spec
1414 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1417 & ':' & Get_Line (Arr (R))
1418 & ':' & Get_Column (Arr (R)));
1432 procedure Print_Vi (Full_Path_Name : Boolean) is
1433 Tab : constant Character := ASCII.HT;
1434 Decls : constant Declaration_Array_Access :=
1435 Get_Declarations (Sorted => False);
1436 Decl : Declaration_Reference;
1437 Arr : Reference_Array_Access;
1441 for D in Decls'Range loop
1444 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1445 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1448 -- Print the body if any
1450 Arr := Get_References (Decl, Get_Bodies => True);
1452 for R in Arr'Range loop
1453 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1455 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1461 -- Print the modifications
1463 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1465 for R in Arr'Range loop
1466 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1468 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1480 procedure Print_Xref (Full_Path_Name : Boolean) is
1481 Decls : constant Declaration_Array_Access := Get_Declarations;
1482 Decl : Declaration_Reference;
1484 Margin : constant := 10;
1485 -- Column where file names start
1487 procedure New_Line80;
1488 -- Go to start of new line
1490 procedure Print80 (S : String);
1491 -- Print the text, respecting the 80 columns rule
1493 procedure Print_Ref (Line, Column : String);
1494 -- The beginning of the output is aligned on a column multiple of 9
1496 procedure Print_List
1497 (Decl : Declaration_Reference;
1499 Get_Reads : Boolean := False;
1500 Get_Writes : Boolean := False;
1501 Get_Bodies : Boolean := False);
1502 -- Print a list of references. If the list is not empty, Msg will
1503 -- be printed prior to the list.
1509 procedure New_Line80 is
1512 Write_Str (String'(1 .. Margin
- 1 => ' '));
1519 procedure Print80
(S
: String) is
1520 Align
: Natural := Margin
- (Integer (Column
) mod Margin
);
1523 if Align
= Margin
then
1527 Write_Str
(String'(1 .. Align => ' ') & S);
1534 procedure Print_Ref (Line, Column : String) is
1535 Line_Align : constant Integer := 4 - Line'Length;
1537 S : constant String := String'(1 .. Line_Align
=> ' ')
1538 & Line
& ':' & Column
;
1540 Align
: Natural := Margin
- (Integer (Output
.Column
) mod Margin
);
1543 if Align
= Margin
then
1547 if Integer (Output
.Column
) + Align
+ S
'Length > 79 then
1552 Write_Str
(String'(1 .. Align => ' ') & S);
1559 procedure Print_List
1560 (Decl : Declaration_Reference;
1562 Get_Reads : Boolean := False;
1563 Get_Writes : Boolean := False;
1564 Get_Bodies : Boolean := False)
1566 Arr : Reference_Array_Access :=
1569 Get_Writes => Get_Writes,
1570 Get_Reads => Get_Reads,
1571 Get_Bodies => Get_Bodies);
1572 File : File_Reference := Empty_File;
1576 if Arr'Length /= 0 then
1581 for R in Arr'Range loop
1582 if Get_File_Ref (Arr (R)) /= File then
1583 if File /= Empty_File then
1587 File := Get_File_Ref (Arr (R));
1588 F := Osint.To_Host_File_Spec
1589 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1590 Write_Str (F.all & ' ');
1594 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1602 -- Start of processing for Print_Xref
1605 for D in Decls'Range loop
1608 Write_Str (Get_Symbol (Decl));
1610 -- Put the declaration type in column Type_Position, but if the
1611 -- declaration name is too long, put at least one space between its
1612 -- name and its type.
1614 while Column < Type_Position - 1 loop
1620 Write_Line (Get_Full_Type (Decl));
1622 Write_Parent_Info : declare
1623 Parent : constant Declaration_Reference := Get_Parent (Decl);
1626 if Parent /= Empty_Declaration then
1627 Write_Str (" Ptype: ");
1628 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1631 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1632 Print80 (" " & Get_Symbol (Parent));
1635 end Write_Parent_Info;
1637 Write_Str (" Decl: ");
1638 F := Osint.To_Host_File_Spec
1639 (Get_Gnatchop_File (Decl, Full_Path_Name));
1640 Print80 (F.all & ' ');
1642 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1645 (Decl, " Body: ", Get_Bodies => True);
1647 (Decl, " Modi: ", Get_Writes => True);
1649 (Decl, " Ref: ", Get_Reads => True);
1659 (Pattern : Search_Pattern;
1660 Local_Symbols : Boolean;
1661 Wide_Search : Boolean;
1662 Read_Only : Boolean;
1664 Type_Tree : Boolean)
1666 type String_Access is access String;
1667 procedure Free is new Unchecked_Deallocation (String, String_Access);
1670 File_Ref : File_Reference;
1671 Strip_Num : Natural := 0;
1672 Ali_Name : String_Access;
1675 -- If we want all the .ali files, then find them
1682 -- Get the next unread ali file
1684 File_Ref := Next_Unvisited_File;
1686 exit when File_Ref = Empty_File;
1688 -- Find the ALI file to use. Most of the time, it will be the unit
1689 -- name, with a different extension. However, when dealing with
1690 -- separates the ALI file is in fact the parent's ALI file (and this
1691 -- is recursive, in case the parent itself is a separate).
1696 Ali_Name := new String'
1697 (Get_File
(File_Ref
, With_Dir
=> True, Strip
=> Strip_Num
));
1699 -- Stripped too many things...
1701 if Ali_Name
.all = "" then
1702 if Get_Emit_Warning
(File_Ref
) then
1705 ("warning : file " & Get_File
(File_Ref
, With_Dir
=> True)
1707 Set_Standard_Output
;
1712 -- If not found, try the parent's ALI file (this is needed for
1713 -- separate units and subprograms).
1715 -- Reset the cached directory first, in case the separate's
1716 -- ALI file is not in the same directory.
1718 elsif not File_Exists
(Ali_Name
.all) then
1719 Strip_Num
:= Strip_Num
+ 1;
1720 Reset_Directory
(File_Ref
);
1722 -- Else we finally found it
1729 -- If we had to get the parent's ALI, insert it in the list as usual.
1730 -- This is to avoid parsing it twice in case it has already been
1733 if Ali_Name
/= null and then Strip_Num
/= 0 then
1734 File_Ref
:= Add_To_Xref_File
1735 (File_Name
=> Ali_Name
.all,
1738 -- Now that we have a file name, parse it to find any reference to
1741 elsif Ali_Name
/= null
1742 and then (Read_Only
or else Is_Writable_File
(Ali_Name
.all))
1745 Open
(Ali_Name
.all, ALIfile
);
1746 while ALIfile
.Buffer
(ALIfile
.Current_Line
) /= EOF
loop
1747 Parse_X_Filename
(ALIfile
);
1748 Parse_Identifier_Info
1749 (Pattern
, ALIfile
, Local_Symbols
,
1750 Der_Info
, Type_Tree
, Wide_Search
, Labels_As_Ref
=> True);
1754 when No_Xref_Information
=>
1755 if Get_Emit_Warning
(File_Ref
) then
1758 ("warning : No cross-referencing information in "
1760 Set_Standard_Output
;
1773 procedure Search_Xref
1774 (Local_Symbols
: Boolean;
1775 Read_Only
: Boolean;
1779 File_Ref
: File_Reference
;
1780 Null_Pattern
: Search_Pattern
;
1783 Null_Pattern
.Initialized
:= False;
1786 -- Find the next unvisited file
1788 File_Ref
:= Next_Unvisited_File
;
1789 exit when File_Ref
= Empty_File
;
1791 -- Search the object directories for the .ali file
1794 F
: constant String := Get_File
(File_Ref
, With_Dir
=> True);
1797 if Read_Only
or else Is_Writable_File
(F
) then
1798 Open
(F
, ALIfile
, True);
1800 while ALIfile
.Buffer
(ALIfile
.Current_Line
) /= EOF
loop
1801 Parse_X_Filename
(ALIfile
);
1802 Parse_Identifier_Info
1803 (Null_Pattern
, ALIfile
, Local_Symbols
, Der_Info
,
1804 Labels_As_Ref
=> False);
1809 when No_Xref_Information
=> null;