with Osint;
with Output; use Output;
with Types; use Types;
with Unchecked_Deallocation;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Command_Line; use GNAT.Command_Line;
with GNAT.IO_Aux; use GNAT.IO_Aux;
package body Xref_Lib is
Type_Position : constant := 50;
Pipe : constant Character := '|';
No_Xref_Information : exception;
procedure Parse_EOL
(Source : access String;
Ptr : in out Positive;
Skip_Continuation_Line : Boolean := False);
function Current_Xref_File (File : ALI_File) return File_Reference;
function File_Name (File : ALI_File; Num : Positive) return File_Reference;
function Get_Full_Type (Decl : Declaration_Reference) return String;
procedure Open
(Name : String;
File : out ALI_File;
Dependencies : Boolean := False);
procedure Parse_Identifier_Info
(Pattern : Search_Pattern;
File : in out ALI_File;
Local_Symbols : Boolean;
Der_Info : Boolean := False;
Type_Tree : Boolean := False;
Wide_Search : Boolean := True;
Labels_As_Ref : Boolean := True);
procedure Parse_Token
(Source : access String;
Ptr : in out Positive;
Token_Ptr : out Positive);
procedure Parse_Number
(Source : access String;
Ptr : in out Positive;
Number : out Natural);
procedure Parse_X_Filename (File : in out ALI_File);
procedure Skip_To_First_X_Line
(File : in out ALI_File;
D_Lines : Boolean;
W_Lines : Boolean);
procedure Add_Entity
(Pattern : in out Search_Pattern;
Entity : String;
Glob : Boolean := False)
is
File_Start : Natural;
Line_Start : Natural;
Col_Start : Natural;
Line_Num : Natural := 0;
Col_Num : Natural := 0;
File_Ref : File_Reference := Empty_File;
begin
File_Start := Index (Entity, ":");
if File_Start = 0 then
begin
Pattern.Entity := Compile (Entity, Glob, False);
Pattern.Initialized := True;
exception
when Error_In_Regexp =>
declare
Tmp_Regexp : String (1 .. 2 * Entity'Length);
Index : Positive := 1;
begin
for J in Entity'Range loop
Tmp_Regexp (Index) := '\';
Tmp_Regexp (Index + 1) := Entity (J);
Index := Index + 2;
end loop;
Pattern.Entity := Compile (Tmp_Regexp, True, False);
Pattern.Initialized := True;
end;
end;
Set_Default_Match (True);
return;
end if;
if (Glob and then
Index (Entity (Entity'First .. File_Start - 1), ".") /= 0)
or else
(not Glob
and then Index (Entity (Entity'First .. File_Start - 1),
"\.") /= 0)
then
Pattern.Entity := Compile (".*", False);
Pattern.Initialized := True;
File_Start := Entity'First;
else
begin
Pattern.Entity :=
Compile (Entity (Entity'First .. File_Start - 1), Glob, False);
Pattern.Initialized := True;
exception
when Error_In_Regexp =>
declare
Tmp_Regexp : String (1 .. 2 * (File_Start - Entity'First));
Index : Positive := 1;
begin
for J in Entity'First .. File_Start - 1 loop
Tmp_Regexp (Index) := '\';
Tmp_Regexp (Index + 1) := Entity (J);
Index := Index + 2;
end loop;
Pattern.Entity := Compile (Tmp_Regexp, True, False);
Pattern.Initialized := True;
end;
end;
File_Start := File_Start + 1;
end if;
Line_Start := Index (Entity (File_Start .. Entity'Last), ":");
if File_Start = Line_Start - 1
and then Line_Start < Entity'Last
and then Entity (Line_Start + 1) = '\'
then
Line_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
end if;
if Line_Start = 0 then
Line_Start := Entity'Length + 1;
elsif Line_Start /= Entity'Last then
Col_Start := Index (Entity (Line_Start + 1 .. Entity'Last), ":");
if Col_Start = 0 then
Col_Start := Entity'Last + 1;
end if;
if Col_Start > Line_Start + 1 then
begin
Line_Num := Natural'Value
(Entity (Line_Start + 1 .. Col_Start - 1));
exception
when Constraint_Error =>
raise Invalid_Argument;
end;
end if;
if Col_Start < Entity'Last then
begin
Col_Num := Natural'Value (Entity
(Col_Start + 1 .. Entity'Last));
exception
when Constraint_Error => raise Invalid_Argument;
end;
end if;
end if;
File_Ref :=
Add_To_Xref_File
(Entity (File_Start .. Line_Start - 1), Visited => True);
Pattern.File_Ref := File_Ref;
Add_Line (Pattern.File_Ref, Line_Num, Col_Num);
File_Ref :=
Add_To_Xref_File
(ALI_File_Name (Entity (File_Start .. Line_Start - 1)),
Visited => False,
Emit_Warning => True);
end Add_Entity;
procedure Add_Xref_File (File : String) is
File_Ref : File_Reference := Empty_File;
pragma Unreferenced (File_Ref);
Iterator : Expansion_Iterator;
procedure Add_Xref_File_Internal (File : String);
procedure Add_Xref_File_Internal (File : String) is
begin
if Tail (File, 4) = ".ali" then
File_Ref := Add_To_Xref_File
(File, Visited => False, Emit_Warning => True);
else
File_Ref := Add_To_Xref_File (File, Visited => True);
File_Ref := Add_To_Xref_File
(ALI_File_Name (File),
Visited => False, Emit_Warning => True);
end if;
end Add_Xref_File_Internal;
begin
if Ada.Strings.Fixed.Index (File, "*") /= 0
or else Ada.Strings.Fixed.Index (File, "?") /= 0
then
Start_Expansion (Iterator, File);
loop
declare
S : constant String := Expansion (Iterator);
begin
exit when S'Length = 0;
Add_Xref_File_Internal (S);
end;
end loop;
else
Add_Xref_File_Internal (File);
end if;
end Add_Xref_File;
function Current_Xref_File (File : ALI_File) return File_Reference is
begin
return File.X_File;
end Current_Xref_File;
function Default_Project_File (Dir_Name : String) return String is
My_Dir : Dir_Type;
Dir_Ent : File_Name_String;
Last : Natural;
begin
Open (My_Dir, Dir_Name);
loop
Read (My_Dir, Dir_Ent, Last);
exit when Last = 0;
if Tail (Dir_Ent (1 .. Last), 4) = ".adp" then
Close (My_Dir);
return Dir_Ent (1 .. Last);
end if;
end loop;
Close (My_Dir);
return String'(1 .. 0 => ' ');
exception
when Directory_Error => return String'(1 .. 0 => ' ');
end Default_Project_File;
function File_Name
(File : ALI_File;
Num : Positive) return File_Reference
is
begin
return File.Dep.Table (Num);
end File_Name;
procedure Find_ALI_Files is
My_Dir : Rec_DIR;
Dir_Ent : File_Name_String;
Last : Natural;
File_Ref : File_Reference;
pragma Unreferenced (File_Ref);
function Open_Next_Dir return Boolean;
function Open_Next_Dir return Boolean is
begin
loop
declare
Obj_Dir : constant String := Next_Obj_Dir;
begin
if Obj_Dir'Length = 0 then
return False;
end if;
Open (My_Dir.Dir, Obj_Dir);
exit;
exception
when Directory_Error => null;
end;
end loop;
return True;
end Open_Next_Dir;
begin
Reset_Obj_Dir;
if Open_Next_Dir then
loop
Read (My_Dir.Dir, Dir_Ent, Last);
if Last = 0 then
Close (My_Dir.Dir);
if not Open_Next_Dir then
return;
end if;
elsif Last > 4 and then Dir_Ent (Last - 3 .. Last) = ".ali" then
File_Ref :=
Add_To_Xref_File (Dir_Ent (1 .. Last), Visited => False);
end if;
end loop;
end if;
end Find_ALI_Files;
function Get_Full_Type (Decl : Declaration_Reference) return String is
function Param_String return String;
function Param_String return String is
begin
if Is_Parameter (Decl) then
return "parameter ";
else
return "";
end if;
end Param_String;
begin
case Get_Type (Decl) is
when 'A' => return "array type";
when 'B' => return "boolean type";
when 'C' => return "class-wide type";
when 'D' => return "decimal type";
when 'E' => return "enumeration type";
when 'F' => return "float type";
when 'I' => return "integer type";
when 'M' => return "modular type";
when 'O' => return "fixed type";
when 'P' => return "access type";
when 'R' => return "record type";
when 'S' => return "string type";
when 'T' => return "task type";
when 'W' => return "protected type";
when 'a' => return "array type";
when 'b' => return Param_String & "boolean object";
when 'c' => return Param_String & "class-wide object";
when 'd' => return Param_String & "decimal object";
when 'e' => return Param_String & "enumeration object";
when 'f' => return Param_String & "float object";
when 'i' => return Param_String & "integer object";
when 'm' => return Param_String & "modular object";
when 'o' => return Param_String & "fixed object";
when 'p' => return Param_String & "access object";
when 'r' => return Param_String & "record object";
when 's' => return Param_String & "string object";
when 't' => return Param_String & "task object";
when 'w' => return Param_String & "protected object";
when 'x' => return Param_String & "abstract procedure";
when 'y' => return Param_String & "abstract function";
when 'K' => return "package";
when 'k' => return "generic package";
when 'L' => return "statement label";
when 'l' => return "loop label";
when 'N' => return "named number";
when 'n' => return "enumeration literal";
when 'q' => return "block label";
when 'U' => return "procedure";
when 'u' => return "generic procedure";
when 'V' => return "function";
when 'v' => return "generic function";
when 'X' => return "exception";
when 'Y' => return "entry";
when '+' => return "private type";
when others =>
return "??? (" & Get_Type (Decl) & ")";
end case;
end Get_Full_Type;
procedure Skip_To_First_X_Line
(File : in out ALI_File;
D_Lines : Boolean;
W_Lines : Boolean)
is
Ali : String_Access renames File.Buffer;
Token : Positive;
Ptr : Positive := Ali'First;
Num_Dependencies : Natural := 0;
File_Start : Positive;
File_End : Positive;
Gnatchop_Offset : Integer;
Gnatchop_Name : Positive;
File_Ref : File_Reference;
pragma Unreferenced (File_Ref);
begin
while Ali (Ptr) /= EOF loop
if D_Lines and then Ali (Ptr) = 'D' then
Ptr := Ptr + 1;
Parse_Token (Ali, Ptr, Token);
File_Start := Token;
File_End := Ptr - 1;
Num_Dependencies := Num_Dependencies + 1;
Set_Last (File.Dep, Num_Dependencies);
Parse_Token (Ali, Ptr, Token); Parse_Token (Ali, Ptr, Token); Parse_Token (Ali, Ptr, Token);
if not (Ali (Token) in '0' .. '9') then
Parse_Token (Ali, Ptr, Token); end if;
Gnatchop_Offset := 0;
if Ali (Token) in '0' .. '9' then
Gnatchop_Name := Token;
while Ali (Gnatchop_Name) /= ':' loop
Gnatchop_Name := Gnatchop_Name + 1;
end loop;
Gnatchop_Offset :=
2 - Natural'Value (Ali (Token .. Gnatchop_Name - 1));
Token := Gnatchop_Name + 1;
end if;
File.Dep.Table (Num_Dependencies) := Add_To_Xref_File
(Ali (File_Start .. File_End),
Gnatchop_File => Ali (Token .. Ptr - 1),
Gnatchop_Offset => Gnatchop_Offset);
elsif W_Lines and then Ali (Ptr) = 'W' then
Parse_Token (Ali, Ptr, Token);
Parse_Token (Ali, Ptr, Token);
Parse_Token (Ali, Ptr, Token);
File_Ref :=
Add_To_Xref_File (Ali (Token .. Ptr - 1), Visited => False);
elsif Ali (Ptr) = 'X' then
File.Current_Line := Ptr;
File.Xref_Line := Ptr;
return;
end if;
Parse_EOL (Ali, Ptr);
end loop;
raise No_Xref_Information;
end Skip_To_First_X_Line;
procedure Open
(Name : String;
File : out ALI_File;
Dependencies : Boolean := False)
is
Ali : String_Access renames File.Buffer;
begin
if File.Buffer /= null then
Free (File.Buffer);
end if;
Init (File.Dep);
begin
Read_File (Name, Ali);
exception
when Ada.Text_IO.Name_Error | Ada.Text_IO.End_Error =>
raise No_Xref_Information;
end;
Skip_To_First_X_Line (File, D_Lines => True, W_Lines => Dependencies);
end Open;
procedure Parse_EOL
(Source : access String;
Ptr : in out Positive;
Skip_Continuation_Line : Boolean := False)
is
begin
loop
while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
and then Source (Ptr) /= EOF
loop
Ptr := Ptr + 1;
end loop;
if Source (Ptr) /= EOF then
Ptr := Ptr + 1; end if;
if (Source (Ptr) = ASCII.CR or else Source (Ptr) = ASCII.LF)
and then Source (Ptr) /= Source (Ptr - 1)
then
Ptr := Ptr + 1;
end if;
exit when not Skip_Continuation_Line or else Source (Ptr) /= '.';
end loop;
end Parse_EOL;
procedure Parse_Identifier_Info
(Pattern : Search_Pattern;
File : in out ALI_File;
Local_Symbols : Boolean;
Der_Info : Boolean := False;
Type_Tree : Boolean := False;
Wide_Search : Boolean := True;
Labels_As_Ref : Boolean := True)
is
Ptr : Positive renames File.Current_Line;
Ali : String_Access renames File.Buffer;
E_Line : Natural; E_Col : Natural; E_Type : Character; E_Name : Positive; E_Global : Boolean;
R_Line : Natural; R_Col : Natural; R_Type : Character;
Decl_Ref : Declaration_Reference;
File_Ref : File_Reference := Current_Xref_File (File);
function Get_Symbol_Name (Eun, Line, Col : Natural) return String;
procedure Skip_To_Matching_Closing_Bracket;
function Get_Symbol_Name (Eun, Line, Col : Natural) return String is
Ptr : Positive := 1;
E_Eun : Positive; E_Line : Natural; E_Col : Natural; E_Name : Positive;
begin
loop
if Ali (Ptr) = 'X' then
Ptr := Ptr + 1;
Parse_Number (Ali, Ptr, E_Eun);
exit when E_Eun = Eun;
end if;
Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
end loop;
loop
Parse_Number (Ali, Ptr, E_Line);
exit when Ali (Ptr) = EOF;
Ptr := Ptr + 1;
Parse_Number (Ali, Ptr, E_Col);
exit when Ali (Ptr) = EOF;
Ptr := Ptr + 1;
if Line = E_Line and then Col = E_Col then
Parse_Token (Ali, Ptr, E_Name);
return Ali (E_Name .. Ptr - 1);
end if;
Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
exit when Ali (Ptr) = EOF;
end loop;
return "???";
end Get_Symbol_Name;
procedure Skip_To_Matching_Closing_Bracket is
Num_Brackets : Natural;
begin
Num_Brackets := 1;
while Num_Brackets /= 0 loop
Ptr := Ptr + 1;
if Ali (Ptr) = '[' then
Num_Brackets := Num_Brackets + 1;
elsif Ali (Ptr) = ']' then
Num_Brackets := Num_Brackets - 1;
end if;
end loop;
Ptr := Ptr + 1;
end Skip_To_Matching_Closing_Bracket;
begin
Parse_Number (Ali, Ptr, E_Line);
if Ali (Ptr) > ' ' then
E_Type := Ali (Ptr);
Ptr := Ptr + 1;
end if;
case E_Type is
when 'l' | 'L' | 'q' =>
Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
return;
when others =>
null;
end case;
Parse_Number (Ali, Ptr, E_Col);
E_Global := False;
if Ali (Ptr) >= ' ' then
E_Global := (Ali (Ptr) = '*');
Ptr := Ptr + 1;
end if;
Parse_Token (Ali, Ptr, E_Name);
if (not Local_Symbols and not E_Global)
or else (Pattern.Initialized
and then not Match (Ali (E_Name .. Ptr - 1), Pattern.Entity))
or else (E_Name >= Ptr)
then
Decl_Ref := Add_Declaration
(File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type,
Remove_Only => True);
Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
return;
end if;
Decl_Ref := Add_Declaration
(File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type);
if Ali (Ptr) = '[' then
Skip_To_Matching_Closing_Bracket;
end if;
if Ali (Ptr) = '<'
or else Ali (Ptr) = '('
or else Ali (Ptr) = '{'
then
Ptr := Ptr + 1;
if Ali (Ptr) in '0' .. '9' then
Parse_Derived_Info : declare
P_Line : Natural; P_Column : Natural; P_Eun : Positive;
begin
Parse_Number (Ali, Ptr, P_Line);
if Ali (Ptr) = '|' then
P_Eun := P_Line;
Ptr := Ptr + 1;
Parse_Number (Ali, Ptr, P_Line);
else
for K in Dependencies_Tables.First .. Last (File.Dep) loop
P_Eun := K;
exit when File.Dep.Table (K) = File_Ref;
end loop;
end if;
Ptr := Ptr + 1;
Parse_Number (Ali, Ptr, P_Column);
if Ali (Ptr) = '[' then
Skip_To_Matching_Closing_Bracket;
end if;
Ptr := Ptr + 1;
if Der_Info or else Type_Tree then
declare
Symbol : constant String :=
Get_Symbol_Name (P_Eun, P_Line, P_Column);
begin
if Symbol /= "???" then
Add_Parent
(Decl_Ref,
Symbol,
P_Line,
P_Column,
File.Dep.Table (P_Eun));
end if;
end;
end if;
if Type_Tree
and then (Pattern.File_Ref = Empty_File
or else
Pattern.File_Ref = Current_Xref_File (File))
then
Search_Parent_Tree : declare
Pattern : Search_Pattern; File_Pos_Backup : Positive;
begin
Add_Entity
(Pattern,
Get_Symbol_Name (P_Eun, P_Line, P_Column)
& ':' & Get_Gnatchop_File (File.Dep.Table (P_Eun))
& ':' & Get_Line (Get_Parent (Decl_Ref))
& ':' & Get_Column (Get_Parent (Decl_Ref)),
False);
Set_Default_Match (False);
File_Pos_Backup := File.Current_Line;
Skip_To_First_X_Line
(File, D_Lines => False, W_Lines => False);
while File.Buffer (File.Current_Line) /= EOF loop
Parse_X_Filename (File);
Parse_Identifier_Info
(Pattern => Pattern,
File => File,
Local_Symbols => False,
Der_Info => Der_Info,
Type_Tree => True,
Wide_Search => False,
Labels_As_Ref => Labels_As_Ref);
end loop;
File.Current_Line := File_Pos_Backup;
end Search_Parent_Tree;
end if;
end Parse_Derived_Info;
else
while Ali (Ptr) /= '>'
and then Ali (Ptr) /= ')'
and then Ali (Ptr) /= '}'
loop
Ptr := Ptr + 1;
end loop;
Ptr := Ptr + 1;
end if;
elsif Ali (Ptr) = '=' then
declare
P_Line, P_Column : Natural;
begin
Ptr := Ptr + 1;
Parse_Number (Ali, Ptr, P_Line);
Ptr := Ptr + 1;
Parse_Number (Ali, Ptr, P_Column);
end;
end if;
if Wide_Search then
declare
File_Ref : File_Reference;
pragma Unreferenced (File_Ref);
File_Name : constant String := Get_Gnatchop_File (File.X_File);
begin
File_Ref := Add_To_Xref_File (ALI_File_Name (File_Name), False);
end;
end if;
loop
while Ali (Ptr) = ' ' or Ali (Ptr) = ASCII.HT loop
Parse_Number (Ali, Ptr, R_Line);
if Ali (Ptr) = Pipe then
Ptr := Ptr + 1;
File_Ref := File_Name (File, R_Line);
Parse_Number (Ali, Ptr, R_Line);
end if;
if Ali (Ptr) > ' ' then
R_Type := Ali (Ptr);
Ptr := Ptr + 1;
end if;
if R_Type = 'b'
and then Ali (Ptr) = '<'
then
while Ptr <= Ali'Last
and then Ali (Ptr) /= '>'
loop
Ptr := Ptr + 1;
end loop;
Ptr := Ptr + 1;
end if;
Parse_Number (Ali, Ptr, R_Col);
Add_Reference
(Decl_Ref, File_Ref, R_Line, R_Col, R_Type, Labels_As_Ref);
if Ali (Ptr) = '[' then
declare
Num_Nested : Integer := 1;
begin
Ptr := Ptr + 1;
while Num_Nested /= 0 loop
if Ali (Ptr) = ']' then
Num_Nested := Num_Nested - 1;
elsif Ali (Ptr) = '[' then
Num_Nested := Num_Nested + 1;
end if;
Ptr := Ptr + 1;
end loop;
end;
end if;
end loop;
Parse_EOL (Ali, Ptr);
exit when Ali (Ptr) /= '.';
Ptr := Ptr + 1;
end loop;
end Parse_Identifier_Info;
procedure Parse_Number
(Source : access String;
Ptr : in out Positive;
Number : out Natural)
is
begin
while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
Ptr := Ptr + 1;
end loop;
Number := 0;
while Source (Ptr) in '0' .. '9' loop
Number :=
10 * Number + (Character'Pos (Source (Ptr)) - Character'Pos ('0'));
Ptr := Ptr + 1;
end loop;
end Parse_Number;
procedure Parse_Token
(Source : access String;
Ptr : in out Positive;
Token_Ptr : out Positive)
is
In_Quotes : Character := ASCII.NUL;
begin
while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
Ptr := Ptr + 1;
end loop;
Token_Ptr := Ptr;
while (In_Quotes /= ASCII.NUL or else
not (Source (Ptr) = ' '
or else Source (Ptr) = ASCII.HT
or else Source (Ptr) = '<'
or else Source (Ptr) = '{'
or else Source (Ptr) = '['
or else Source (Ptr) = '='
or else Source (Ptr) = '('))
and then Source (Ptr) >= ' '
loop
case Source (Ptr) is
when '"' | ''' =>
if In_Quotes = Source (Ptr) then
In_Quotes := ASCII.NUL;
elsif In_Quotes = ASCII.NUL then
In_Quotes := Source (Ptr);
end if;
when others =>
null;
end case;
Ptr := Ptr + 1;
end loop;
end Parse_Token;
procedure Parse_X_Filename (File : in out ALI_File) is
Ali : String_Access renames File.Buffer;
Ptr : Positive renames File.Current_Line;
File_Nr : Natural;
begin
while Ali (Ptr) = 'X' loop
Ptr := Ptr + 1;
Parse_Number (Ali, Ptr, File_Nr);
if File_Nr > 0 then
File.X_File := File.Dep.Table (File_Nr);
end if;
Parse_EOL (Ali, Ptr);
end loop;
end Parse_X_Filename;
procedure Print_Gnatfind
(References : Boolean;
Full_Path_Name : Boolean)
is
Decls : constant Declaration_Array_Access := Get_Declarations;
Decl : Declaration_Reference;
Arr : Reference_Array_Access;
procedure Print_Ref
(Ref : Reference;
Msg : String := " ");
procedure Print_Ref
(Ref : Reference;
Msg : String := " ")
is
F : String_Access :=
Osint.To_Host_File_Spec
(Get_Gnatchop_File (Ref, Full_Path_Name));
Buffer : constant String :=
F.all &
":" & Get_Line (Ref) &
":" & Get_Column (Ref) &
": ";
Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
begin
Free (F);
Num_Blanks := Integer'Max (0, Num_Blanks);
Write_Line
(Buffer
& String'(1 .. Num_Blanks => ' ')
& Msg & " " & Get_Symbol (Decl));
if Get_Source_Line (Ref)'Length /= 0 then
Write_Line (" " & Get_Source_Line (Ref));
end if;
end Print_Ref;
begin
for D in Decls'Range loop
Decl := Decls (D);
if Match (Decl) then
declare
Parent : constant Declaration_Reference := Get_Parent (Decl);
F : String_Access :=
Osint.To_Host_File_Spec
(Get_Gnatchop_File (Decl, Full_Path_Name));
Buffer : constant String :=
F.all &
":" & Get_Line (Decl) &
":" & Get_Column (Decl) &
": ";
Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;
begin
Free (F);
Num_Blanks := Integer'Max (0, Num_Blanks);
Write_Line
(Buffer & String'(1 .. Num_Blanks => ' ')
& "(spec) " & Get_Symbol (Decl));
if Parent /= Empty_Declaration then
F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
Write_Line
(Buffer & String'(1 .. Num_Blanks => ' ')
& " derived from " & Get_Symbol (Parent)
& " ("
& F.all
& ':' & Get_Line (Parent)
& ':' & Get_Column (Parent) & ')');
Free (F);
end if;
end;
if Get_Source_Line (Decl)'Length /= 0 then
Write_Line (" " & Get_Source_Line (Decl));
end if;
Arr := Get_References (Decl, Get_Bodies => True);
for R in Arr'Range loop
Print_Ref (Arr (R), "(body)");
end loop;
Free (Arr);
if References then
Arr := Get_References
(Decl, Get_Writes => True, Get_Reads => True);
for R in Arr'Range loop
Print_Ref (Arr (R));
end loop;
Free (Arr);
end if;
end if;
end loop;
end Print_Gnatfind;
procedure Print_Unused (Full_Path_Name : Boolean) is
Decls : constant Declaration_Array_Access := Get_Declarations;
Decl : Declaration_Reference;
Arr : Reference_Array_Access;
F : String_Access;
begin
for D in Decls'Range loop
Decl := Decls (D);
if References_Count
(Decl, Get_Reads => True, Get_Writes => True) = 0
then
F := Osint.To_Host_File_Spec
(Get_Gnatchop_File (Decl, Full_Path_Name));
Write_Str (Get_Symbol (Decl)
& " ("
& Get_Full_Type (Decl)
& ") "
& F.all
& ':'
& Get_Line (Decl)
& ':'
& Get_Column (Decl));
Free (F);
Arr := Get_References (Decl, Get_Bodies => True);
for R in Arr'Range loop
F := Osint.To_Host_File_Spec
(Get_Gnatchop_File (Arr (R), Full_Path_Name));
Write_Str (' '
& F.all
& ':' & Get_Line (Arr (R))
& ':' & Get_Column (Arr (R)));
Free (F);
end loop;
Write_Eol;
Free (Arr);
end if;
end loop;
end Print_Unused;
procedure Print_Vi (Full_Path_Name : Boolean) is
Tab : constant Character := ASCII.HT;
Decls : constant Declaration_Array_Access :=
Get_Declarations (Sorted => False);
Decl : Declaration_Reference;
Arr : Reference_Array_Access;
F : String_Access;
begin
for D in Decls'Range loop
Decl := Decls (D);
F := Osint.To_Host_File_Spec (Get_File (Decl, Full_Path_Name));
Write_Line (Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Decl));
Free (F);
Arr := Get_References (Decl, Get_Bodies => True);
for R in Arr'Range loop
F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
Write_Line
(Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
Free (F);
end loop;
Free (Arr);
Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
for R in Arr'Range loop
F := Osint.To_Host_File_Spec (Get_File (Arr (R), Full_Path_Name));
Write_Line
(Get_Symbol (Decl) & Tab & F.all & Tab & Get_Line (Arr (R)));
Free (F);
end loop;
Free (Arr);
end loop;
end Print_Vi;
procedure Print_Xref (Full_Path_Name : Boolean) is
Decls : constant Declaration_Array_Access := Get_Declarations;
Decl : Declaration_Reference;
Margin : constant := 10;
procedure New_Line80;
procedure Print80 (S : String);
procedure Print_Ref (Line, Column : String);
procedure Print_List
(Decl : Declaration_Reference;
Msg : String;
Get_Reads : Boolean := False;
Get_Writes : Boolean := False;
Get_Bodies : Boolean := False);
procedure New_Line80 is
begin
Write_Eol;
Write_Str (String'(1 .. Margin - 1 => ' '));
end New_Line80;
procedure Print80 (S : String) is
Align : Natural := Margin - (Integer (Column) mod Margin);
begin
if Align = Margin then
Align := 0;
end if;
Write_Str (String'(1 .. Align => ' ') & S);
end Print80;
procedure Print_Ref (Line, Column : String) is
Line_Align : constant Integer := 4 - Line'Length;
S : constant String := String'(1 .. Line_Align => ' ')
& Line & ':' & Column;
Align : Natural := Margin - (Integer (Output.Column) mod Margin);
begin
if Align = Margin then
Align := 0;
end if;
if Integer (Output.Column) + Align + S'Length > 79 then
New_Line80;
Align := 0;
end if;
Write_Str (String'(1 .. Align => ' ') & S);
end Print_Ref;
procedure Print_List
(Decl : Declaration_Reference;
Msg : String;
Get_Reads : Boolean := False;
Get_Writes : Boolean := False;
Get_Bodies : Boolean := False)
is
Arr : Reference_Array_Access :=
Get_References
(Decl,
Get_Writes => Get_Writes,
Get_Reads => Get_Reads,
Get_Bodies => Get_Bodies);
File : File_Reference := Empty_File;
F : String_Access;
begin
if Arr'Length /= 0 then
Write_Eol;
Write_Str (Msg);
end if;
for R in Arr'Range loop
if Get_File_Ref (Arr (R)) /= File then
if File /= Empty_File then
New_Line80;
end if;
File := Get_File_Ref (Arr (R));
F := Osint.To_Host_File_Spec
(Get_Gnatchop_File (Arr (R), Full_Path_Name));
Write_Str (F.all & ' ');
Free (F);
end if;
Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
end loop;
Free (Arr);
end Print_List;
F : String_Access;
begin
for D in Decls'Range loop
Decl := Decls (D);
Write_Str (Get_Symbol (Decl));
while Column < Type_Position loop
Write_Char (' ');
end loop;
Write_Line (Get_Full_Type (Decl));
Write_Parent_Info : declare
Parent : constant Declaration_Reference := Get_Parent (Decl);
begin
if Parent /= Empty_Declaration then
Write_Str (" Ptype: ");
F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
Print80 (F.all);
Free (F);
Print_Ref (Get_Line (Parent), Get_Column (Parent));
Print80 (" " & Get_Symbol (Parent));
Write_Eol;
end if;
end Write_Parent_Info;
Write_Str (" Decl: ");
F := Osint.To_Host_File_Spec
(Get_Gnatchop_File (Decl, Full_Path_Name));
Print80 (F.all & ' ');
Free (F);
Print_Ref (Get_Line (Decl), Get_Column (Decl));
Print_List
(Decl, " Body: ", Get_Bodies => True);
Print_List
(Decl, " Modi: ", Get_Writes => True);
Print_List
(Decl, " Ref: ", Get_Reads => True);
Write_Eol;
end loop;
end Print_Xref;
procedure Search
(Pattern : Search_Pattern;
Local_Symbols : Boolean;
Wide_Search : Boolean;
Read_Only : Boolean;
Der_Info : Boolean;
Type_Tree : Boolean)
is
type String_Access is access String;
procedure Free is new Unchecked_Deallocation (String, String_Access);
ALIfile : ALI_File;
File_Ref : File_Reference;
Strip_Num : Natural := 0;
Ali_Name : String_Access;
begin
if Wide_Search then
Find_ALI_Files;
end if;
loop
File_Ref := Next_Unvisited_File;
exit when File_Ref = Empty_File;
Strip_Num := 0;
loop
Free (Ali_Name);
Ali_Name := new String'
(Get_File (File_Ref, With_Dir => True, Strip => Strip_Num));
if Ali_Name.all = "" then
if Get_Emit_Warning (File_Ref) then
Set_Standard_Error;
Write_Line
("warning : file " & Get_File (File_Ref, With_Dir => True)
& " not found");
Set_Standard_Output;
end if;
Free (Ali_Name);
exit;
elsif not File_Exists (Ali_Name.all) then
Strip_Num := Strip_Num + 1;
Reset_Directory (File_Ref);
else
exit;
end if;
end loop;
if Ali_Name /= null and then Strip_Num /= 0 then
File_Ref := Add_To_Xref_File
(File_Name => Ali_Name.all,
Visited => False);
elsif Ali_Name /= null
and then (Read_Only or else Is_Writable_File (Ali_Name.all))
then
begin
Open (Ali_Name.all, ALIfile);
while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
Parse_X_Filename (ALIfile);
Parse_Identifier_Info
(Pattern, ALIfile, Local_Symbols,
Der_Info, Type_Tree, Wide_Search, Labels_As_Ref => True);
end loop;
exception
when No_Xref_Information =>
if Get_Emit_Warning (File_Ref) then
Set_Standard_Error;
Write_Line
("warning : No cross-referencing information in "
& Ali_Name.all);
Set_Standard_Output;
end if;
end;
end if;
end loop;
Free (Ali_Name);
end Search;
procedure Search_Xref
(Local_Symbols : Boolean;
Read_Only : Boolean;
Der_Info : Boolean)
is
ALIfile : ALI_File;
File_Ref : File_Reference;
Null_Pattern : Search_Pattern;
begin
Null_Pattern.Initialized := False;
loop
File_Ref := Next_Unvisited_File;
exit when File_Ref = Empty_File;
declare
F : constant String := Get_File (File_Ref, With_Dir => True);
begin
if Read_Only or else Is_Writable_File (F) then
Open (F, ALIfile, True);
while ALIfile.Buffer (ALIfile.Current_Line) /= EOF loop
Parse_X_Filename (ALIfile);
Parse_Identifier_Info
(Null_Pattern, ALIfile, Local_Symbols, Der_Info,
Labels_As_Ref => False);
end loop;
end if;
exception
when No_Xref_Information => null;
end;
end loop;
end Search_Xref;
end Xref_Lib;