1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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
: 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;
145 File_Ref
: File_Reference
:= Empty_File
;
146 pragma Warnings
(Off
, File_Ref
);
149 -- Find the end of the first item in Entity (pattern or file?)
150 -- If there is no ':', we only have a pattern
152 File_Start
:= Index
(Entity
, ":");
154 -- If the regular expression is invalid, just consider it as a string
156 if File_Start
= 0 then
158 Pattern
.Entity
:= Compile
(Entity
, Glob
, False);
159 Pattern
.Initialized
:= True;
162 when Error_In_Regexp
=>
164 -- The basic idea is to insert a \ before every character
167 Tmp_Regexp
: String (1 .. 2 * Entity
'Length);
168 Index
: Positive := 1;
171 for J
in Entity
'Range loop
172 Tmp_Regexp
(Index
) := '\';
173 Tmp_Regexp
(Index
+ 1) := Entity
(J
);
177 Pattern
.Entity
:= Compile
(Tmp_Regexp
, True, False);
178 Pattern
.Initialized
:= True;
182 Set_Default_Match
(True);
186 -- If there is a dot in the pattern, then it is a file name
189 Index
(Entity
(Entity
'First .. File_Start
- 1), ".") /= 0)
192 and then Index
(Entity
(Entity
'First .. File_Start
- 1),
195 Pattern
.Entity
:= Compile
(".*", False);
196 Pattern
.Initialized
:= True;
197 File_Start
:= Entity
'First;
200 -- If the regular expression is invalid, just consider it as a string
204 Compile
(Entity
(Entity
'First .. File_Start
- 1), Glob
, False);
205 Pattern
.Initialized
:= True;
208 when Error_In_Regexp
=>
210 -- The basic idea is to insert a \ before every character
213 Tmp_Regexp
: String (1 .. 2 * (File_Start
- Entity
'First));
214 Index
: Positive := 1;
217 for J
in Entity
'First .. File_Start
- 1 loop
218 Tmp_Regexp
(Index
) := '\';
219 Tmp_Regexp
(Index
+ 1) := Entity
(J
);
223 Pattern
.Entity
:= Compile
(Tmp_Regexp
, True, False);
224 Pattern
.Initialized
:= True;
228 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
;
281 Add_Line
(Pattern
.File_Ref
, Line_Num
, Col_Num
);
285 (ALI_File_Name
(Entity
(File_Start
.. Line_Start
- 1)),
287 Emit_Warning
=> True);
294 procedure Add_Xref_File
(File
: String) is
295 File_Ref
: File_Reference
:= Empty_File
;
296 pragma Unreferenced
(File_Ref
);
298 Iterator
: Expansion_Iterator
;
300 procedure Add_Xref_File_Internal
(File
: String);
301 -- Do the actual addition of the file
303 ----------------------------
304 -- Add_Xref_File_Internal --
305 ----------------------------
307 procedure Add_Xref_File_Internal
(File
: String) is
309 -- Case where we have an ALI file, accept it even though this is
310 -- not official usage, since the intention is obvious
312 if Tail
(File
, 4) = ".ali" then
313 File_Ref
:= Add_To_Xref_File
314 (File
, Visited
=> False, Emit_Warning
=> True);
316 -- Normal non-ali file case
319 File_Ref
:= Add_To_Xref_File
(File
, Visited
=> True);
321 File_Ref
:= Add_To_Xref_File
322 (ALI_File_Name
(File
),
323 Visited
=> False, Emit_Warning
=> True);
325 end Add_Xref_File_Internal
;
327 -- Start of processing for Add_Xref_File
330 -- Check if we need to do the expansion
332 if Ada
.Strings
.Fixed
.Index
(File
, "*") /= 0
333 or else Ada
.Strings
.Fixed
.Index
(File
, "?") /= 0
335 Start_Expansion
(Iterator
, File
);
339 S
: constant String := Expansion
(Iterator
);
342 exit when S
'Length = 0;
343 Add_Xref_File_Internal
(S
);
348 Add_Xref_File_Internal
(File
);
352 -----------------------
353 -- Current_Xref_File --
354 -----------------------
356 function Current_Xref_File
(File
: ALI_File
) return File_Reference
is
359 end Current_Xref_File
;
361 --------------------------
362 -- Default_Project_File --
363 --------------------------
365 function Default_Project_File
(Dir_Name
: String) return String is
367 Dir_Ent
: File_Name_String
;
371 Open
(My_Dir
, Dir_Name
);
374 Read
(My_Dir
, Dir_Ent
, Last
);
377 if Tail
(Dir_Ent
(1 .. Last
), 4) = ".adp" then
379 -- The first project file found is the good one
382 return Dir_Ent
(1 .. Last
);
387 return String'(1 .. 0 => ' ');
390 when Directory_Error => return String'(1 .. 0 => ' ');
391 end Default_Project_File
;
399 Num
: Positive) return File_Reference
402 return File
.Dep
.Table
(Num
);
409 procedure Find_ALI_Files
is
411 Dir_Ent
: File_Name_String
;
414 File_Ref
: File_Reference
;
415 pragma Unreferenced
(File_Ref
);
417 function Open_Next_Dir
return Boolean;
418 -- Tries to open the next object directory, and return False if
419 -- the directory cannot be opened.
425 function Open_Next_Dir
return Boolean is
427 -- Until we are able to open a new directory
431 Obj_Dir
: constant String := Next_Obj_Dir
;
434 -- Case of no more Obj_Dir lines
436 if Obj_Dir
'Length = 0 then
440 Open
(My_Dir
.Dir
, Obj_Dir
);
445 -- Could not open the directory
447 when Directory_Error
=> null;
454 -- Start of processing for Find_ALI_Files
459 if Open_Next_Dir
then
461 Read
(My_Dir
.Dir
, Dir_Ent
, Last
);
466 if not Open_Next_Dir
then
470 elsif Last
> 4 and then Dir_Ent
(Last
- 3 .. Last
) = ".ali" then
472 Add_To_Xref_File
(Dir_Ent
(1 .. Last
), Visited
=> False);
482 function Get_Full_Type
(Decl
: Declaration_Reference
) return String is
484 function Param_String
return String;
485 -- Return the string to display depending on whether Decl is a
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 'i' => return Param_String
& "integer object";
527 when 'm' => return Param_String
& "modular object";
528 when 'o' => return Param_String
& "fixed object";
529 when 'p' => return Param_String
& "access object";
530 when 'r' => return Param_String
& "record object";
531 when 's' => return Param_String
& "string object";
532 when 't' => return Param_String
& "task object";
533 when 'w' => return Param_String
& "protected object";
534 when 'x' => return Param_String
& "abstract procedure";
535 when 'y' => return Param_String
& "abstract function";
537 when 'K' => return "package";
538 when 'k' => return "generic package";
539 when 'L' => return "statement label";
540 when 'l' => return "loop label";
541 when 'N' => return "named number";
542 when 'n' => return "enumeration literal";
543 when 'q' => return "block label";
544 when 'U' => return "procedure";
545 when 'u' => return "generic procedure";
546 when 'V' => return "function";
547 when 'v' => return "generic function";
548 when 'X' => return "exception";
549 when 'Y' => return "entry";
551 when '+' => return "private type";
553 -- The above should be the only possibilities, but for this kind
554 -- of informational output, we don't want to bomb if we find
555 -- something else, so just return three question marks when we
556 -- have an unknown Abbrev value
559 return "??? (" & Get_Type
(Decl
) & ")";
563 --------------------------
564 -- Skip_To_First_X_Line --
565 --------------------------
567 procedure Skip_To_First_X_Line
568 (File
: in out ALI_File
;
572 Ali
: String_Access
renames File
.Buffer
;
574 Ptr
: Positive := Ali
'First;
575 Num_Dependencies
: Natural := 0;
576 File_Start
: Positive;
578 Gnatchop_Offset
: Integer;
579 Gnatchop_Name
: Positive;
581 File_Ref
: File_Reference
;
582 pragma Unreferenced
(File_Ref
);
585 -- Read all the lines possibly processing with-clauses and dependency
586 -- information and exit on finding the first Xref line.
587 -- A fall-through of the loop means that there is no xref information
588 -- which is an error condition.
590 while Ali
(Ptr
) /= EOF
loop
591 if D_Lines
and then Ali
(Ptr
) = 'D' then
593 -- Found dependency information. Format looks like:
594 -- D src-nam time-stmp checksum [subunit-name] [line:file-name]
596 -- Skip the D and parse the filenam
599 Parse_Token
(Ali
, Ptr
, Token
);
603 Num_Dependencies
:= Num_Dependencies
+ 1;
604 Set_Last
(File
.Dep
, Num_Dependencies
);
606 Parse_Token
(Ali
, Ptr
, Token
); -- Skip time-stamp
607 Parse_Token
(Ali
, Ptr
, Token
); -- Skip checksum
608 Parse_Token
(Ali
, Ptr
, Token
); -- Read next entity on the line
610 if not (Ali
(Token
) in '0' .. '9') then
611 Parse_Token
(Ali
, Ptr
, Token
); -- Was a subunit name
614 -- Did we have a gnatchop-ed file with a pragma Source_Reference ?
616 Gnatchop_Offset
:= 0;
618 if Ali
(Token
) in '0' .. '9' then
619 Gnatchop_Name
:= Token
;
620 while Ali
(Gnatchop_Name
) /= ':' loop
621 Gnatchop_Name
:= Gnatchop_Name
+ 1;
625 2 - Natural'Value (Ali
(Token
.. Gnatchop_Name
- 1));
626 Token
:= Gnatchop_Name
+ 1;
629 File
.Dep
.Table
(Num_Dependencies
) := Add_To_Xref_File
630 (Ali
(File_Start
.. File_End
),
631 Gnatchop_File
=> Ali
(Token
.. Ptr
- 1),
632 Gnatchop_Offset
=> Gnatchop_Offset
);
634 elsif W_Lines
and then Ali
(Ptr
) = 'W' then
636 -- Found with-clause information. Format looks like:
637 -- "W debug%s debug.adb debug.ali"
639 -- Skip the W and parse the .ali filename (3rd token)
641 Parse_Token
(Ali
, Ptr
, Token
);
642 Parse_Token
(Ali
, Ptr
, Token
);
643 Parse_Token
(Ali
, Ptr
, Token
);
646 Add_To_Xref_File
(Ali
(Token
.. Ptr
- 1), Visited
=> False);
648 elsif Ali
(Ptr
) = 'X' then
650 -- Found a cross-referencing line - stop processing
652 File
.Current_Line
:= Ptr
;
653 File
.Xref_Line
:= Ptr
;
657 Parse_EOL
(Ali
, Ptr
);
660 raise No_Xref_Information
;
661 end Skip_To_First_X_Line
;
670 Dependencies
: Boolean := False)
672 Ali
: String_Access
renames File
.Buffer
;
675 if File
.Buffer
/= null then
682 Read_File
(Name
, Ali
);
685 when Ada
.Text_IO
.Name_Error | Ada
.Text_IO
.End_Error
=>
686 raise No_Xref_Information
;
689 Skip_To_First_X_Line
(File
, D_Lines
=> True, W_Lines
=> Dependencies
);
697 (Source
: access String;
698 Ptr
: in out Positive;
699 Skip_Continuation_Line
: Boolean := False)
703 -- Skip to end of line
705 while Source
(Ptr
) /= ASCII
.CR
and then Source
(Ptr
) /= ASCII
.LF
706 and then Source
(Ptr
) /= EOF
711 if Source
(Ptr
) /= EOF
then
712 Ptr
:= Ptr
+ 1; -- skip CR or LF
715 -- Skip past CR/LF or LF/CR combination
717 if (Source
(Ptr
) = ASCII
.CR
or else Source
(Ptr
) = ASCII
.LF
)
718 and then Source
(Ptr
) /= Source
(Ptr
- 1)
723 exit when not Skip_Continuation_Line
or else Source
(Ptr
) /= '.';
727 ---------------------------
728 -- Parse_Identifier_Info --
729 ---------------------------
731 procedure Parse_Identifier_Info
732 (Pattern
: Search_Pattern
;
733 File
: in out ALI_File
;
734 Local_Symbols
: Boolean;
735 Der_Info
: Boolean := False;
736 Type_Tree
: Boolean := False;
737 Wide_Search
: Boolean := True;
738 Labels_As_Ref
: Boolean := True)
740 Ptr
: Positive renames File
.Current_Line
;
741 Ali
: String_Access
renames File
.Buffer
;
743 E_Line
: Natural; -- Line number of current entity
744 E_Col
: Natural; -- Column number of current entity
745 E_Type
: Character; -- Type of current entity
746 E_Name
: Positive; -- Pointer to begin of entity name
747 E_Global
: Boolean; -- True iff entity is global
749 R_Line
: Natural; -- Line number of current reference
750 R_Col
: Natural; -- Column number of current reference
751 R_Type
: Character; -- Type of current reference
753 Decl_Ref
: Declaration_Reference
;
754 File_Ref
: File_Reference
:= Current_Xref_File
(File
);
756 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String;
757 -- Returns the symbol name for the entity defined at the specified
758 -- line and column in the dependent unit number Eun. For this we need
759 -- to parse the ali file again because the parent entity is not in
760 -- the declaration table if it did not match the search pattern.
762 procedure Skip_To_Matching_Closing_Bracket
;
763 -- When Ptr points to an opening square bracket, moves it to the
764 -- character following the matching closing bracket
766 ---------------------
767 -- Get_Symbol_Name --
768 ---------------------
770 function Get_Symbol_Name
(Eun
, Line
, Col
: Natural) return String is
772 E_Eun
: Positive; -- Unit number of current entity
773 E_Line
: Natural; -- Line number of current entity
774 E_Col
: Natural; -- Column number of current entity
775 E_Name
: Positive; -- Pointer to begin of entity name
778 -- Look for the X lines corresponding to unit Eun
781 if Ali
(Ptr
) = 'X' then
783 Parse_Number
(Ali
, Ptr
, E_Eun
);
784 exit when E_Eun
= Eun
;
787 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
790 -- Here we are in the right Ali section, we now look for the entity
791 -- declared at position (Line, Col).
794 Parse_Number
(Ali
, Ptr
, E_Line
);
795 exit when Ali
(Ptr
) = EOF
;
797 Parse_Number
(Ali
, Ptr
, E_Col
);
798 exit when Ali
(Ptr
) = EOF
;
801 if Line
= E_Line
and then Col
= E_Col
then
802 Parse_Token
(Ali
, Ptr
, E_Name
);
803 return Ali
(E_Name
.. Ptr
- 1);
806 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
807 exit when Ali
(Ptr
) = EOF
;
810 -- We were not able to find the symbol, this should not happend but
811 -- since we don't want to stop here we return a string of three
812 -- question marks as the symbol name.
817 --------------------------------------
818 -- Skip_To_Matching_Closing_Bracket --
819 --------------------------------------
821 procedure Skip_To_Matching_Closing_Bracket
is
822 Num_Brackets
: Natural;
826 while Num_Brackets
/= 0 loop
828 if Ali
(Ptr
) = '[' then
829 Num_Brackets
:= Num_Brackets
+ 1;
830 elsif Ali
(Ptr
) = ']' then
831 Num_Brackets
:= Num_Brackets
- 1;
836 end Skip_To_Matching_Closing_Bracket
;
838 -- Start of processing for Parse_Identifier_Info
841 -- The identifier info looks like:
842 -- "38U9*Debug 12|36r6 36r19"
844 -- Extract the line, column and entity name information
846 Parse_Number
(Ali
, Ptr
, E_Line
);
848 if Ali
(Ptr
) > ' ' then
853 -- Ignore some of the entities (labels,...)
856 when 'l' |
'L' |
'q' =>
857 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
864 Parse_Number
(Ali
, Ptr
, E_Col
);
867 if Ali
(Ptr
) >= ' ' then
868 E_Global
:= (Ali
(Ptr
) = '*');
872 Parse_Token
(Ali
, Ptr
, E_Name
);
874 -- Exit if the symbol does not match
875 -- or if we have a local symbol and we do not want it
877 if (not Local_Symbols
and not E_Global
)
878 or else (Pattern
.Initialized
879 and then not Match
(Ali
(E_Name
.. Ptr
- 1), Pattern
.Entity
))
880 or else (E_Name
>= Ptr
)
882 Decl_Ref
:= Add_Declaration
883 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
,
884 Remove_Only
=> True);
885 Parse_EOL
(Ali
, Ptr
, Skip_Continuation_Line
=> True);
889 -- Insert the declaration in the table
891 Decl_Ref
:= Add_Declaration
892 (File
.X_File
, Ali
(E_Name
.. Ptr
- 1), E_Line
, E_Col
, E_Type
);
894 if Ali
(Ptr
) = '[' then
895 Skip_To_Matching_Closing_Bracket
;
899 or else Ali
(Ptr
) = '('
900 or else Ali
(Ptr
) = '{'
902 -- Here we have a type derivation information. The format is
903 -- <3|12I45> which means that the current entity is derived from the
904 -- type defined in unit number 3, line 12 column 45. The pipe and
905 -- unit number is optional. It is specified only if the parent type
906 -- is not defined in the current unit.
908 -- We also have the format for generic instantiations, as in
909 -- 7a5*Uid(3|5I8[4|2]) 2|4r74
911 -- We could also have something like
913 -- that indicates that I derives from the predefined type integer.
917 if Ali
(Ptr
) in '0' .. '9' then
918 Parse_Derived_Info
: declare
919 P_Line
: Natural; -- parent entity line
920 P_Column
: Natural; -- parent entity column
921 P_Eun
: Positive; -- parent entity file number
924 Parse_Number
(Ali
, Ptr
, P_Line
);
926 -- If we have a pipe then the first number was the unit number
928 if Ali
(Ptr
) = '|' then
932 -- Now we have the line number
934 Parse_Number
(Ali
, Ptr
, P_Line
);
937 -- We don't have a unit number specified, so we set P_Eun to
940 for K
in Dependencies_Tables
.First
.. Last
(File
.Dep
) loop
942 exit when File
.Dep
.Table
(K
) = File_Ref
;
946 -- Then parse the type and column number
949 Parse_Number
(Ali
, Ptr
, P_Column
);
951 -- Skip the information for generics instantiations
953 if Ali
(Ptr
) = '[' then
954 Skip_To_Matching_Closing_Bracket
;
957 -- Skip '>', or ')' or '>'
961 -- The derived info is needed only is the derived info mode is
962 -- on or if we want to output the type hierarchy
964 if Der_Info
or else Type_Tree
then
966 Symbol
: constant String :=
967 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
);
969 if Symbol
/= "???" then
975 File
.Dep
.Table
(P_Eun
));
981 and then (Pattern
.File_Ref
= Empty_File
983 Pattern
.File_Ref
= Current_Xref_File
(File
))
985 Search_Parent_Tree
: declare
986 Pattern
: Search_Pattern
; -- Parent type pattern
987 File_Pos_Backup
: Positive;
992 Get_Symbol_Name
(P_Eun
, P_Line
, P_Column
)
993 & ':' & Get_Gnatchop_File
(File
.Dep
.Table
(P_Eun
))
994 & ':' & Get_Line
(Get_Parent
(Decl_Ref
))
995 & ':' & Get_Column
(Get_Parent
(Decl_Ref
)),
998 -- No default match is needed to look for the parent type
999 -- since we are using the fully qualified symbol name:
1000 -- symbol:file:line:column
1002 Set_Default_Match
(False);
1004 -- The parent hierarchy is defined in the same unit as
1005 -- the derived type. So we want to revisit the unit.
1007 File_Pos_Backup
:= File
.Current_Line
;
1009 Skip_To_First_X_Line
1010 (File
, D_Lines
=> False, W_Lines
=> False);
1012 while File
.Buffer
(File
.Current_Line
) /= EOF
loop
1013 Parse_X_Filename
(File
);
1014 Parse_Identifier_Info
1015 (Pattern
=> Pattern
,
1017 Local_Symbols
=> False,
1018 Der_Info
=> Der_Info
,
1020 Wide_Search
=> False,
1021 Labels_As_Ref
=> Labels_As_Ref
);
1024 File
.Current_Line
:= File_Pos_Backup
;
1025 end Search_Parent_Tree
;
1027 end Parse_Derived_Info
;
1030 while Ali
(Ptr
) /= '>'
1031 and then Ali
(Ptr
) /= ')'
1032 and then Ali
(Ptr
) /= '}'
1039 elsif Ali
(Ptr
) = '=' then
1041 P_Line
, P_Column
: Natural;
1045 Parse_Number
(Ali
, Ptr
, P_Line
);
1047 Parse_Number
(Ali
, Ptr
, P_Column
);
1051 -- To find the body, we will have to parse the file too
1055 File_Ref
: File_Reference
;
1056 pragma Unreferenced
(File_Ref
);
1057 File_Name
: constant String := Get_Gnatchop_File
(File
.X_File
);
1059 File_Ref
:= Add_To_Xref_File
(ALI_File_Name
(File_Name
), False);
1063 -- Parse references to this entity.
1064 -- Ptr points to next reference with leading blanks
1067 -- Process references on current line
1069 while Ali
(Ptr
) = ' ' or Ali
(Ptr
) = ASCII
.HT
loop
1071 -- For every reference read the line, type and column,
1072 -- optionally preceded by a file number and a pipe symbol.
1074 Parse_Number
(Ali
, Ptr
, R_Line
);
1076 if Ali
(Ptr
) = Pipe
then
1078 File_Ref
:= File_Name
(File
, R_Line
);
1080 Parse_Number
(Ali
, Ptr
, R_Line
);
1083 if Ali
(Ptr
) > ' ' then
1084 R_Type
:= Ali
(Ptr
);
1088 -- Imported entities might special indication as to their external
1090 -- 5U14*Foo2 5>20 6b<c,myfoo2>22
1093 and then Ali
(Ptr
) = '<'
1095 while Ptr
<= Ali
'Last
1096 and then Ali
(Ptr
) /= '>'
1103 Parse_Number
(Ali
, Ptr
, R_Col
);
1105 -- Insert the reference or body in the table
1108 (Decl_Ref
, File_Ref
, R_Line
, R_Col
, R_Type
, Labels_As_Ref
);
1110 -- Skip generic information, if any
1112 if Ali
(Ptr
) = '[' then
1114 Num_Nested
: Integer := 1;
1118 while Num_Nested
/= 0 loop
1119 if Ali
(Ptr
) = ']' then
1120 Num_Nested
:= Num_Nested
- 1;
1121 elsif Ali
(Ptr
) = '[' then
1122 Num_Nested
:= Num_Nested
+ 1;
1132 Parse_EOL
(Ali
, Ptr
);
1134 -- Loop until new line is no continuation line
1136 exit when Ali
(Ptr
) /= '.';
1139 end Parse_Identifier_Info
;
1145 procedure Parse_Number
1146 (Source
: access String;
1147 Ptr
: in out Positive;
1148 Number
: out Natural)
1153 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1158 while Source
(Ptr
) in '0' .. '9' loop
1160 10 * Number
+ (Character'Pos (Source
(Ptr
)) - Character'Pos ('0'));
1169 procedure Parse_Token
1170 (Source
: access String;
1171 Ptr
: in out Positive;
1172 Token_Ptr
: out Positive)
1174 In_Quotes
: Character := ASCII
.NUL
;
1179 while Source
(Ptr
) = ' ' or else Source
(Ptr
) = ASCII
.HT
loop
1185 -- Find end-of-token
1187 while (In_Quotes
/= ASCII
.NUL
or else
1188 not (Source
(Ptr
) = ' '
1189 or else Source
(Ptr
) = ASCII
.HT
1190 or else Source
(Ptr
) = '<'
1191 or else Source
(Ptr
) = '{'
1192 or else Source
(Ptr
) = '['
1193 or else Source
(Ptr
) = '='
1194 or else Source
(Ptr
) = '('))
1195 and then Source
(Ptr
) >= ' '
1197 -- Double-quotes are used for operators
1198 -- Simple-quotes are used for character constants, for instance when
1199 -- they are found in an enumeration type "type A is ('+', '-');"
1201 case Source
(Ptr
) is
1203 if In_Quotes
= Source
(Ptr
) then
1204 In_Quotes
:= ASCII
.NUL
;
1205 elsif In_Quotes
= ASCII
.NUL
then
1206 In_Quotes
:= Source
(Ptr
);
1217 ----------------------
1218 -- Parse_X_Filename --
1219 ----------------------
1221 procedure Parse_X_Filename
(File
: in out ALI_File
) is
1222 Ali
: String_Access
renames File
.Buffer
;
1223 Ptr
: Positive renames File
.Current_Line
;
1227 while Ali
(Ptr
) = 'X' loop
1229 -- The current line is the start of a new Xref file section,
1230 -- whose format looks like:
1234 -- Skip the X and read the file number for the new X_File
1237 Parse_Number
(Ali
, Ptr
, File_Nr
);
1240 File
.X_File
:= File
.Dep
.Table
(File_Nr
);
1243 Parse_EOL
(Ali
, Ptr
);
1245 end Parse_X_Filename
;
1247 --------------------
1248 -- Print_Gnatfind --
1249 --------------------
1251 procedure Print_Gnatfind
1252 (References
: Boolean;
1253 Full_Path_Name
: Boolean)
1255 Decls
: constant Declaration_Array_Access
:= Get_Declarations
;
1256 Decl
: Declaration_Reference
;
1257 Arr
: Reference_Array_Access
;
1261 Msg
: String := " ");
1262 -- Print a reference, according to the extended tag of the output
1270 Msg
: String := " ")
1272 F
: String_Access
:=
1273 Osint
.To_Host_File_Spec
1274 (Get_Gnatchop_File
(Ref
, Full_Path_Name
));
1276 Buffer
: constant String :=
1278 ":" & Get_Line
(Ref
) &
1279 ":" & Get_Column
(Ref
) &
1282 Num_Blanks
: Integer := Longest_File_Name
+ 10 - Buffer
'Length;
1286 Num_Blanks
:= Integer'Max (0, Num_Blanks
);
1289 & String'(1 .. Num_Blanks => ' ')
1290 & Msg & " " & Get_Symbol (Decl));
1292 if Get_Source_Line (Ref)'Length /= 0 then
1293 Write_Line (" " & Get_Source_Line (Ref));
1297 -- Start of processing for Print_Gnatfind
1300 for D in Decls'Range loop
1303 if Match (Decl) then
1305 -- Output the declaration
1308 Parent : constant Declaration_Reference := Get_Parent (Decl);
1310 F : String_Access :=
1311 Osint.To_Host_File_Spec
1312 (Get_Gnatchop_File (Decl, Full_Path_Name));
1314 Buffer : constant String :=
1316 ":" & Get_Line (Decl) &
1317 ":" & Get_Column (Decl) &
1320 Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
1324 Num_Blanks := Integer'Max (0, Num_Blanks);
1326 (Buffer & String'(1 .. Num_Blanks
=> ' ')
1327 & "(spec) " & Get_Symbol
(Decl
));
1329 if Parent
/= Empty_Declaration
then
1330 F
:= Osint
.To_Host_File_Spec
(Get_Gnatchop_File
(Parent
));
1332 (Buffer
& String'(1 .. Num_Blanks => ' ')
1333 & " derived from " & Get_Symbol (Parent)
1336 & ':' & Get_Line (Parent)
1337 & ':' & Get_Column (Parent) & ')');
1342 if Get_Source_Line (Decl)'Length /= 0 then
1343 Write_Line (" " & Get_Source_Line (Decl));
1346 -- Output the body (sorted)
1348 Arr := Get_References (Decl, Get_Bodies => True);
1350 for R in Arr'Range loop
1351 Print_Ref (Arr (R), "(body)");
1357 Arr := Get_References
1358 (Decl, Get_Writes => True, Get_Reads => True);
1360 for R in Arr'Range loop
1361 Print_Ref (Arr (R));
1374 procedure Print_Unused (Full_Path_Name : Boolean) is
1375 Decls : constant Declaration_Array_Access := Get_Declarations;
1376 Decl : Declaration_Reference;
1377 Arr : Reference_Array_Access;
1381 for D in Decls'Range loop
1385 (Decl, Get_Reads => True, Get_Writes => True) = 0
1387 F := Osint.To_Host_File_Spec
1388 (Get_Gnatchop_File (Decl, Full_Path_Name));
1389 Write_Str (Get_Symbol (Decl)
1391 & Get_Full_Type (Decl)
1397 & Get_Column (Decl));
1400 -- Print the body if any
1402 Arr := Get_References (Decl, Get_Bodies => True);
1404 for R in Arr'Range loop
1405 F := Osint.To_Host_File_Spec
1406 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1409 & ':' & Get_Line (Arr (R))
1410 & ':' & Get_Column (Arr (R)));
1424 procedure Print_Vi (Full_Path_Name : Boolean) is
1425 Tab : constant Character := ASCII.HT;
1426 Decls : constant Declaration_Array_Access :=
1427 Get_Declarations (Sorted => False);
1428 Decl : Declaration_Reference;
1429 Arr : Reference_Array_Access;
1433 for D in Decls'Range loop
1436 F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
1437 Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
1440 -- Print the body if any
1442 Arr := Get_References (Decl, Get_Bodies => True);
1444 for R in Arr'Range loop
1445 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1447 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1453 -- Print the modifications
1455 Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
1457 for R in Arr'Range loop
1458 F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
1460 (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
1472 procedure Print_Xref (Full_Path_Name : Boolean) is
1473 Decls : constant Declaration_Array_Access := Get_Declarations;
1474 Decl : Declaration_Reference;
1476 Margin : constant := 10;
1477 -- Column where file names start
1479 procedure New_Line80;
1480 -- Go to start of new line
1482 procedure Print80 (S : String);
1483 -- Print the text, respecting the 80 columns rule
1485 procedure Print_Ref (Line, Column : String);
1486 -- The beginning of the output is aligned on a column multiple of 9
1488 procedure Print_List
1489 (Decl : Declaration_Reference;
1491 Get_Reads : Boolean := False;
1492 Get_Writes : Boolean := False;
1493 Get_Bodies : Boolean := False);
1494 -- Print a list of references. If the list is not empty, Msg will
1495 -- be printed prior to the list.
1501 procedure New_Line80 is
1504 Write_Str (String'(1 .. Margin
- 1 => ' '));
1511 procedure Print80
(S
: String) is
1512 Align
: Natural := Margin
- (Integer (Column
) mod Margin
);
1515 if Align
= Margin
then
1519 Write_Str
(String'(1 .. Align => ' ') & S);
1526 procedure Print_Ref (Line, Column : String) is
1527 Line_Align : constant Integer := 4 - Line'Length;
1529 S : constant String := String'(1 .. Line_Align
=> ' ')
1530 & Line
& ':' & Column
;
1532 Align
: Natural := Margin
- (Integer (Output
.Column
) mod Margin
);
1535 if Align
= Margin
then
1539 if Integer (Output
.Column
) + Align
+ S
'Length > 79 then
1544 Write_Str
(String'(1 .. Align => ' ') & S);
1551 procedure Print_List
1552 (Decl : Declaration_Reference;
1554 Get_Reads : Boolean := False;
1555 Get_Writes : Boolean := False;
1556 Get_Bodies : Boolean := False)
1558 Arr : Reference_Array_Access :=
1561 Get_Writes => Get_Writes,
1562 Get_Reads => Get_Reads,
1563 Get_Bodies => Get_Bodies);
1564 File : File_Reference := Empty_File;
1568 if Arr'Length /= 0 then
1573 for R in Arr'Range loop
1574 if Get_File_Ref (Arr (R)) /= File then
1575 if File /= Empty_File then
1579 File := Get_File_Ref (Arr (R));
1580 F := Osint.To_Host_File_Spec
1581 (Get_Gnatchop_File (Arr (R), Full_Path_Name));
1582 Write_Str (F.all & ' ');
1586 Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
1594 -- Start of processing for Print_Xref
1597 for D in Decls'Range loop
1600 Write_Str (Get_Symbol (Decl));
1602 while Column < Type_Position loop
1606 Write_Line (Get_Full_Type (Decl));
1608 Write_Parent_Info : declare
1609 Parent : constant Declaration_Reference := Get_Parent (Decl);
1612 if Parent /= Empty_Declaration then
1613 Write_Str (" Ptype: ");
1614 F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
1617 Print_Ref (Get_Line (Parent), Get_Column (Parent));
1618 Print80 (" " & Get_Symbol (Parent));
1621 end Write_Parent_Info;
1623 Write_Str (" Decl: ");
1624 F := Osint.To_Host_File_Spec
1625 (Get_Gnatchop_File (Decl, Full_Path_Name));
1626 Print80 (F.all & ' ');
1628 Print_Ref (Get_Line (Decl), Get_Column (Decl));
1631 (Decl, " Body: ", Get_Bodies => True);
1633 (Decl, " Modi: ", Get_Writes => True);
1635 (Decl, " Ref: ", Get_Reads => True);
1645 (Pattern : Search_Pattern;
1646 Local_Symbols : Boolean;
1647 Wide_Search : Boolean;
1648 Read_Only : Boolean;
1650 Type_Tree : Boolean)
1652 type String_Access is access String;
1653 procedure Free is new Unchecked_Deallocation (String, String_Access);
1656 File_Ref : File_Reference;
1657 Strip_Num : Natural := 0;
1658 Ali_Name : String_Access;
1661 -- If we want all the .ali files, then find them
1668 -- Get the next unread ali file
1670 File_Ref := Next_Unvisited_File;
1672 exit when File_Ref = Empty_File;
1674 -- Find the ALI file to use. Most of the time, it will be the unit
1675 -- name, with a different extension. However, when dealing with
1676 -- separates the ALI file is in fact the parent's ALI file (and this
1677 -- is recursive, in case the parent itself is a separate).
1682 Ali_Name := new String'
1683 (Get_File
(File_Ref
, With_Dir
=> True, Strip
=> Strip_Num
));
1685 -- Stripped too many things...
1687 if Ali_Name
.all = "" then
1688 if Get_Emit_Warning
(File_Ref
) then
1691 ("warning : file " & Get_File
(File_Ref
, With_Dir
=> True)
1693 Set_Standard_Output
;
1698 -- If not found, try the parent's ALI file (this is needed for
1699 -- separate units and subprograms).
1701 -- Reset the cached directory first, in case the separate's
1702 -- ALI file is not in the same directory.
1704 elsif not File_Exists
(Ali_Name
.all) then
1705 Strip_Num
:= Strip_Num
+ 1;
1706 Reset_Directory
(File_Ref
);
1708 -- Else we finally found it
1715 -- If we had to get the parent's ALI, insert it in the list as usual.
1716 -- This is to avoid parsing it twice in case it has already been
1719 if Ali_Name
/= null and then Strip_Num
/= 0 then
1720 File_Ref
:= Add_To_Xref_File
1721 (File_Name
=> Ali_Name
.all,
1724 -- Now that we have a file name, parse it to find any reference to
1727 elsif Ali_Name
/= null
1728 and then (Read_Only
or else Is_Writable_File
(Ali_Name
.all))
1731 Open
(Ali_Name
.all, ALIfile
);
1732 while ALIfile
.Buffer
(ALIfile
.Current_Line
) /= EOF
loop
1733 Parse_X_Filename
(ALIfile
);
1734 Parse_Identifier_Info
1735 (Pattern
, ALIfile
, Local_Symbols
,
1736 Der_Info
, Type_Tree
, Wide_Search
, Labels_As_Ref
=> True);
1740 when No_Xref_Information
=>
1741 if Get_Emit_Warning
(File_Ref
) then
1744 ("warning : No cross-referencing information in "
1746 Set_Standard_Output
;
1759 procedure Search_Xref
1760 (Local_Symbols
: Boolean;
1761 Read_Only
: Boolean;
1765 File_Ref
: File_Reference
;
1766 Null_Pattern
: Search_Pattern
;
1769 Null_Pattern
.Initialized
:= False;
1772 -- Find the next unvisited file
1774 File_Ref
:= Next_Unvisited_File
;
1775 exit when File_Ref
= Empty_File
;
1777 -- Search the object directories for the .ali file
1780 F
: constant String := Get_File
(File_Ref
, With_Dir
=> True);
1783 if Read_Only
or else Is_Writable_File
(F
) then
1784 Open
(F
, ALIfile
, True);
1786 while ALIfile
.Buffer
(ALIfile
.Current_Line
) /= EOF
loop
1787 Parse_X_Filename
(ALIfile
);
1788 Parse_Identifier_Info
1789 (Null_Pattern
, ALIfile
, Local_Symbols
, Der_Info
,
1790 Labels_As_Ref
=> False);
1795 when No_Xref_Information
=> null;