1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1998-2005, 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 ------------------------------------------------------------------------------
27 with Types; use Types;
31 with Ada.Unchecked_Conversion;
32 with Ada.Unchecked_Deallocation;
33 with Ada.Strings.Fixed;
36 with Ada.Characters.Handling; use Ada.Characters.Handling;
37 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
39 with GNAT.OS_Lib; use GNAT.OS_Lib;
40 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
41 with GNAT.HTable; use GNAT.HTable;
42 with GNAT.Heap_Sort_G;
44 package body Xr_Tabls is
46 type HTable_Headers is range 1 .. 10000;
48 procedure Set_Next (E : File_Reference; Next : File_Reference);
49 function Next (E : File_Reference) return File_Reference;
50 function Get_Key (E : File_Reference) return Cst_String_Access;
51 function Hash (F : Cst_String_Access) return HTable_Headers;
52 function Equal (F1, F2 : Cst_String_Access) return Boolean;
53 -- The five subprograms above are used to instanciate the static
54 -- htable to store the files that should be processed.
56 package File_HTable is new GNAT.HTable.Static_HTable
57 (Header_Num => HTable_Headers,
58 Element => File_Record,
59 Elmt_Ptr => File_Reference,
63 Key => Cst_String_Access,
67 -- A hash table to store all the files referenced in the
68 -- application. The keys in this htable are the name of the files
69 -- themselves, therefore it is assumed that the source path
70 -- doesn't contain twice the same source or ALI file name
72 type Unvisited_Files_Record;
73 type Unvisited_Files_Access is access Unvisited_Files_Record;
74 type Unvisited_Files_Record is record
75 File : File_Reference;
76 Next : Unvisited_Files_Access;
78 -- A special list, in addition to File_HTable, that only stores
79 -- the files that haven't been visited so far. Note that the File
80 -- list points to some data in File_HTable, and thus should never be freed.
82 function Next (E : Declaration_Reference) return Declaration_Reference;
83 procedure Set_Next (E, Next : Declaration_Reference);
84 function Get_Key (E : Declaration_Reference) return Cst_String_Access;
85 -- The subprograms above are used to instanciate the static
86 -- htable to store the entities that have been found in the application
88 package Entities_HTable is new GNAT.HTable.Static_HTable
89 (Header_Num => HTable_Headers,
90 Element => Declaration_Record,
91 Elmt_Ptr => Declaration_Reference,
95 Key => Cst_String_Access,
99 -- A hash table to store all the entities defined in the
100 -- application. For each entity, we store a list of its reference
101 -- locations as well.
102 -- The keys in this htable should be created with Key_From_Ref,
103 -- and are the file, line and column of the declaration, which are
104 -- unique for every entity.
106 Entities_Count : Natural := 0;
107 -- Number of entities in Entities_HTable. This is used in the end
108 -- when sorting the table.
110 Longest_File_Name_In_Table : Natural := 0;
111 Unvisited_Files : Unvisited_Files_Access := null;
112 Directories : Project_File_Ptr;
113 Default_Match : Boolean := False;
114 -- The above need commenting ???
116 function Parse_Gnatls_Src return String;
117 -- Return the standard source directories (taking into account the
118 -- ADA_INCLUDE_PATH environment variable, if Osint.Add_Default_Search_Dirs
119 -- was called first).
121 function Parse_Gnatls_Obj return String;
122 -- Return the standard object directories (taking into account the
123 -- ADA_OBJECTS_PATH environment variable).
125 function Key_From_Ref
126 (File_Ref : File_Reference;
130 -- Return a key for the symbol declared at File_Ref, Line,
131 -- Column. This key should be used for lookup in Entity_HTable
133 function Is_Less_Than (Decl1, Decl2 : Declaration_Reference) return Boolean;
134 -- Compare two declarations (the comparison is case-insensitive)
136 function Is_Less_Than (Ref1, Ref2 : Reference) return Boolean;
137 -- Compare two references
139 procedure Store_References
140 (Decl : Declaration_Reference;
141 Get_Writes : Boolean := False;
142 Get_Reads : Boolean := False;
143 Get_Bodies : Boolean := False;
144 Get_Declaration : Boolean := False;
145 Arr : in out Reference_Array;
146 Index : in out Natural);
147 -- Store in Arr, starting at Index, all the references to Decl. The Get_*
148 -- parameters can be used to indicate which references should be stored.
149 -- Constraint_Error will be raised if Arr is not big enough.
151 procedure Sort (Arr : in out Reference_Array);
152 -- Sort an array of references (Arr'First must be 1)
158 procedure Set_Next (E : File_Reference; Next : File_Reference) is
164 (E : Declaration_Reference; Next : Declaration_Reference) is
173 function Get_Key (E : File_Reference) return Cst_String_Access is
178 function Get_Key (E : Declaration_Reference) return Cst_String_Access is
187 function Hash (F : Cst_String_Access) return HTable_Headers is
188 function H is new GNAT.HTable.Hash (HTable_Headers);
198 function Equal (F1, F2 : Cst_String_Access) return Boolean is
200 return F1.all = F2.all;
207 function Key_From_Ref
208 (File_Ref : File_Reference;
214 return File_Ref.File.all & Natural'Image (Line) & Natural'Image (Column);
217 ---------------------
218 -- Add_Declaration --
219 ---------------------
221 function Add_Declaration
222 (File_Ref : File_Reference;
226 Decl_Type : Character;
227 Remove_Only : Boolean := False;
228 Symbol_Match : Boolean := True)
229 return Declaration_Reference
231 procedure Unchecked_Free is new Ada.Unchecked_Deallocation
232 (Declaration_Record, Declaration_Reference);
234 Key : aliased constant String := Key_From_Ref (File_Ref, Line, Column);
236 New_Decl : Declaration_Reference :=
237 Entities_HTable.Get (Key'Unchecked_Access);
239 Is_Parameter : Boolean := False;
242 -- Insert the Declaration in the table. There might already be a
243 -- declaration in the table if the entity is a parameter, so we
244 -- need to check that first.
246 if New_Decl /= null and then New_Decl.Symbol_Length = 0 then
247 Is_Parameter := New_Decl.Is_Parameter;
248 Entities_HTable.Remove (Key'Unrestricted_Access);
249 Entities_Count := Entities_Count - 1;
251 Unchecked_Free (New_Decl);
255 -- The declaration might also already be there for parent types. In
256 -- this case, we should keep the entry, since some other entries are
260 and then not Remove_Only
263 new Declaration_Record'
264 (Symbol_Length => Symbol'Length,
266 Key => new String'(Key),
267 Decl => new Reference_Record'
273 Is_Parameter => Is_Parameter,
274 Decl_Type => Decl_Type,
278 Match => Symbol_Match
281 or else Match (File_Ref, Line, Column)),
285 Entities_HTable.Set (New_Decl);
286 Entities_Count := Entities_Count + 1;
288 if New_Decl.Match then
289 Longest_File_Name_In_Table :=
290 Natural'Max (File_Ref.File'Length, Longest_File_Name_In_Table);
293 elsif New_Decl /= null
294 and then not New_Decl.Match
296 New_Decl.Match := Default_Match
297 or else Match (File_Ref, Line, Column);
303 ----------------------
304 -- Add_To_Xref_File --
305 ----------------------
307 function Add_To_Xref_File
309 Visited : Boolean := True;
310 Emit_Warning : Boolean := False;
311 Gnatchop_File : String := "";
312 Gnatchop_Offset : Integer := 0) return File_Reference
314 Base : aliased constant String := Base_Name (File_Name);
315 Dir : constant String := Dir_Name (File_Name);
316 Dir_Acc : GNAT.OS_Lib.String_Access := null;
317 Ref : File_Reference;
320 -- Do we have a directory name as well?
322 if File_Name /= Base then
323 Dir_Acc := new String'(Dir);
326 Ref := File_HTable.Get (Base'Unchecked_Access);
328 Ref := new File_Record'
329 (File => new String'(Base),
333 Emit_Warning => Emit_Warning,
334 Gnatchop_File => new String'(Gnatchop_File),
335 Gnatchop_Offset => Gnatchop_Offset,
337 File_HTable.Set (Ref);
341 -- Keep a separate list for faster access
347 end Add_To_Xref_File;
354 (File : File_Reference;
359 File.Lines := new Ref_In_File'(Line => Line,
369 (Declaration : in out Declaration_Reference;
373 File_Ref : File_Reference)
376 Declaration.Par_Symbol :=
378 (File_Ref, Symbol, Line, Column,
380 Symbol_Match => False);
387 procedure Add_Reference
388 (Declaration : Declaration_Reference;
389 File_Ref : File_Reference;
392 Ref_Type : Character;
393 Labels_As_Ref : Boolean)
399 when 'b' | 'c' | 'm' | 'r' | 'i' | ' ' | 'x' =>
403 if not Labels_As_Ref then
407 when '=' | '<' | '>' | '^' =>
409 -- Create a dummy declaration in the table to report it as a
410 -- parameter. Note that the current declaration for the subprogram
411 -- comes before the declaration of the parameter.
414 Key : constant String :=
415 Key_From_Ref (File_Ref, Line, Column);
416 New_Decl : Declaration_Reference;
419 New_Decl := new Declaration_Record'
422 Key => new String'(Key),
424 Is_Parameter => True,
432 Entities_HTable.Set (New_Decl);
433 Entities_Count := Entities_Count + 1;
436 when 'e' | 'z' | 't' | 'p' | 'P' | 'k' | 'd' =>
440 Ada.Text_IO.Put_Line ("Unknown reference type: " & Ref_Type);
444 New_Ref := new Reference_Record'
451 -- We can insert the reference in the list directly, since all
452 -- the references will appear only once in the ALI file
453 -- corresponding to the file where they are referenced.
454 -- This saves a lot of time compared to checking the list to check
459 New_Ref.Next := Declaration.Body_Ref;
460 Declaration.Body_Ref := New_Ref;
462 when 'r' | 'i' | 'l' | ' ' | 'x' | 'w' =>
463 New_Ref.Next := Declaration.Ref_Ref;
464 Declaration.Ref_Ref := New_Ref;
467 New_Ref.Next := Declaration.Modif_Ref;
468 Declaration.Modif_Ref := New_Ref;
474 if not Declaration.Match then
475 Declaration.Match := Match (File_Ref, Line, Column);
478 if Declaration.Match then
479 Longest_File_Name_In_Table :=
480 Natural'Max (File_Ref.File'Length, Longest_File_Name_In_Table);
488 function ALI_File_Name (Ada_File_Name : String) return String is
490 -- ??? Should ideally be based on the naming scheme defined in
493 Index : constant Natural :=
494 Ada.Strings.Fixed.Index
495 (Ada_File_Name, ".", Going => Ada.Strings.Backward);
499 return Ada_File_Name (Ada_File_Name'First .. Index) & "ali";
501 return Ada_File_Name & ".ali";
509 function Is_Less_Than (Ref1, Ref2 : Reference) return Boolean is
513 elsif Ref2 = null then
517 if Ref1.File.File.all < Ref2.File.File.all then
520 elsif Ref1.File.File.all = Ref2.File.File.all then
521 return (Ref1.Line < Ref2.Line
522 or else (Ref1.Line = Ref2.Line
523 and then Ref1.Column < Ref2.Column));
533 function Is_Less_Than (Decl1, Decl2 : Declaration_Reference) return Boolean
535 -- We cannot store the data case-insensitive in the table,
536 -- since we wouldn't be able to find the right casing for the
539 S1 : constant String := To_Lower (Decl1.Symbol);
540 S2 : constant String := To_Lower (Decl2.Symbol);
549 return Decl1.Key.all < Decl2.Key.all;
552 -------------------------
553 -- Create_Project_File --
554 -------------------------
556 procedure Create_Project_File (Name : String) is
557 Obj_Dir : Unbounded_String := Null_Unbounded_String;
558 Src_Dir : Unbounded_String := Null_Unbounded_String;
559 Build_Dir : GNAT.OS_Lib.String_Access := new String'("");
563 File_Name : aliased String := Name & ASCII.NUL;
566 -- Read the size of the file
568 F := Open_Read (File_Name'Address, Text);
570 -- Project file not found
572 if F /= Invalid_FD then
573 Len := Positive (File_Length (F));
576 Buffer : String (1 .. Len);
577 Index : Positive := Buffer'First;
581 Len := Read (F, Buffer'Address, Len);
584 -- First, look for Build_Dir, since all the source and object
585 -- path are relative to it.
587 while Index <= Buffer'Last loop
589 -- Find the end of line
592 while Last <= Buffer'Last
593 and then Buffer (Last) /= ASCII.LF
594 and then Buffer (Last) /= ASCII.CR
599 if Index <= Buffer'Last - 9
600 and then Buffer (Index .. Index + 9) = "build_dir="
604 and then (Buffer (Index) = ' '
605 or else Buffer (Index) = ASCII.HT)
611 Build_Dir := new String'(Buffer (Index .. Last - 1));
616 -- In case we had a ASCII.CR/ASCII.LF end of line, skip the
619 if Index <= Buffer'Last
620 and then Buffer (Index) = ASCII.LF
626 -- Now parse the source and object paths
628 Index := Buffer'First;
629 while Index <= Buffer'Last loop
631 -- Find the end of line
634 while Last <= Buffer'Last
635 and then Buffer (Last) /= ASCII.LF
636 and then Buffer (Last) /= ASCII.CR
641 if Index <= Buffer'Last - 7
642 and then Buffer (Index .. Index + 7) = "src_dir="
644 Append (Src_Dir, Normalize_Pathname
645 (Name => Ada.Strings.Fixed.Trim
646 (Buffer (Index + 8 .. Last - 1), Ada.Strings.Both),
647 Directory => Build_Dir.all) & Path_Separator);
649 elsif Index <= Buffer'Last - 7
650 and then Buffer (Index .. Index + 7) = "obj_dir="
652 Append (Obj_Dir, Normalize_Pathname
653 (Name => Ada.Strings.Fixed.Trim
654 (Buffer (Index + 8 .. Last - 1), Ada.Strings.Both),
655 Directory => Build_Dir.all) & Path_Separator);
658 -- In case we had a ASCII.CR/ASCII.LF end of line, skip the
662 if Index <= Buffer'Last
663 and then Buffer (Index) = ASCII.LF
671 Osint.Add_Default_Search_Dirs;
674 Src : constant String := Parse_Gnatls_Src;
675 Obj : constant String := Parse_Gnatls_Obj;
678 Directories := new Project_File'
679 (Src_Dir_Length => Length (Src_Dir) + Src'Length,
680 Obj_Dir_Length => Length (Obj_Dir) + Obj'Length,
681 Src_Dir => To_String (Src_Dir) & Src,
682 Obj_Dir => To_String (Obj_Dir) & Obj,
685 Last_Obj_Dir_Start => 0);
689 end Create_Project_File;
691 ---------------------
692 -- Current_Obj_Dir --
693 ---------------------
695 function Current_Obj_Dir return String is
697 return Directories.Obj_Dir
698 (Directories.Last_Obj_Dir_Start .. Directories.Obj_Dir_Index - 2);
705 function Get_Column (Decl : Declaration_Reference) return String is
707 return Ada.Strings.Fixed.Trim (Natural'Image (Decl.Decl.Column),
711 function Get_Column (Ref : Reference) return String is
713 return Ada.Strings.Fixed.Trim (Natural'Image (Ref.Column),
717 ---------------------
718 -- Get_Declaration --
719 ---------------------
721 function Get_Declaration
722 (File_Ref : File_Reference;
725 return Declaration_Reference
727 Key : aliased constant String := Key_From_Ref (File_Ref, Line, Column);
730 return Entities_HTable.Get (Key'Unchecked_Access);
733 ----------------------
734 -- Get_Emit_Warning --
735 ----------------------
737 function Get_Emit_Warning (File : File_Reference) return Boolean is
739 return File.Emit_Warning;
740 end Get_Emit_Warning;
747 (Decl : Declaration_Reference;
748 With_Dir : Boolean := False) return String
751 return Get_File (Decl.Decl.File, With_Dir);
756 With_Dir : Boolean := False) return String
759 return Get_File (Ref.File, With_Dir);
763 (File : File_Reference;
764 With_Dir : in Boolean := False;
765 Strip : Natural := 0) return String
767 Tmp : GNAT.OS_Lib.String_Access;
769 function Internal_Strip (Full_Name : String) return String;
770 -- Internal function to process the Strip parameter
776 function Internal_Strip (Full_Name : String) return String is
778 Extension_Start : Natural;
786 -- Isolate the file extension
788 Extension_Start := Full_Name'Last;
789 while Extension_Start >= Full_Name'First
790 and then Full_Name (Extension_Start) /= '.'
792 Extension_Start := Extension_Start - 1;
795 -- Strip the right number of subunit_names
798 Unit_End := Extension_Start - 1;
799 while Unit_End >= Full_Name'First
802 if Full_Name (Unit_End) = '-' then
806 Unit_End := Unit_End - 1;
809 if Unit_End < Full_Name'First then
812 return Full_Name (Full_Name'First .. Unit_End)
813 & Full_Name (Extension_Start .. Full_Name'Last);
817 -- Start of processing for Get_File;
820 -- If we do not want the full path name
823 return Internal_Strip (File.File.all);
826 if File.Dir = null then
827 if Ada.Strings.Fixed.Tail (File.File.all, 3) = "ali" then
828 Tmp := Locate_Regular_File
829 (Internal_Strip (File.File.all), Directories.Obj_Dir);
831 Tmp := Locate_Regular_File
832 (File.File.all, Directories.Src_Dir);
836 File.Dir := new String'("");
838 File.Dir := new String'(Dir_Name (Tmp.all));
843 return Internal_Strip (File.Dir.all & File.File.all);
850 function Get_File_Ref (Ref : Reference) return File_Reference is
855 -----------------------
856 -- Get_Gnatchop_File --
857 -----------------------
859 function Get_Gnatchop_File
860 (File : File_Reference;
861 With_Dir : Boolean := False)
865 if File.Gnatchop_File.all = "" then
866 return Get_File (File, With_Dir);
868 return File.Gnatchop_File.all;
870 end Get_Gnatchop_File;
872 function Get_Gnatchop_File
874 With_Dir : Boolean := False)
878 return Get_Gnatchop_File (Ref.File, With_Dir);
879 end Get_Gnatchop_File;
881 function Get_Gnatchop_File
882 (Decl : Declaration_Reference;
883 With_Dir : Boolean := False)
887 return Get_Gnatchop_File (Decl.Decl.File, With_Dir);
888 end Get_Gnatchop_File;
894 function Get_Line (Decl : Declaration_Reference) return String is
896 return Ada.Strings.Fixed.Trim (Natural'Image (Decl.Decl.Line),
900 function Get_Line (Ref : Reference) return String is
902 return Ada.Strings.Fixed.Trim (Natural'Image (Ref.Line),
911 (Decl : Declaration_Reference)
912 return Declaration_Reference
915 return Decl.Par_Symbol;
918 ---------------------
919 -- Get_Source_Line --
920 ---------------------
922 function Get_Source_Line (Ref : Reference) return String is
924 if Ref.Source_Line /= null then
925 return Ref.Source_Line.all;
931 function Get_Source_Line (Decl : Declaration_Reference) return String is
933 if Decl.Decl.Source_Line /= null then
934 return Decl.Decl.Source_Line.all;
944 function Get_Symbol (Decl : Declaration_Reference) return String is
953 function Get_Type (Decl : Declaration_Reference) return Character is
955 return Decl.Decl_Type;
962 procedure Sort (Arr : in out Reference_Array) is
965 function Lt (Op1, Op2 : Natural) return Boolean;
966 procedure Move (From, To : Natural);
967 -- See GNAT.Heap_Sort_G
973 function Lt (Op1, Op2 : Natural) return Boolean is
976 return Is_Less_Than (Tmp, Arr (Op2));
978 return Is_Less_Than (Arr (Op1), Tmp);
980 return Is_Less_Than (Arr (Op1), Arr (Op2));
988 procedure Move (From, To : Natural) is
995 Arr (To) := Arr (From);
999 package Ref_Sort is new GNAT.Heap_Sort_G (Move, Lt);
1001 -- Start of processing for Sort
1004 Ref_Sort.Sort (Arr'Last);
1007 -----------------------
1008 -- Grep_Source_Files --
1009 -----------------------
1011 procedure Grep_Source_Files is
1012 Length : Natural := 0;
1013 Decl : Declaration_Reference := Entities_HTable.Get_First;
1014 Arr : Reference_Array_Access;
1016 End_Index : Natural;
1017 Current_File : File_Reference;
1018 Current_Line : Cst_String_Access;
1019 Buffer : GNAT.OS_Lib.String_Access;
1024 -- Create a temporary array, where all references will be
1025 -- sorted by files. This way, we only have to read the source
1028 while Decl /= null loop
1030 -- Add 1 for the declaration itself
1032 Length := Length + References_Count (Decl, True, True, True) + 1;
1033 Decl := Entities_HTable.Get_Next;
1036 Arr := new Reference_Array (1 .. Length);
1039 Decl := Entities_HTable.Get_First;
1040 while Decl /= null loop
1041 Store_References (Decl, True, True, True, True, Arr.all, Index);
1042 Decl := Entities_HTable.Get_Next;
1047 -- Now traverse the whole array and find the appropriate source
1050 for R in Arr'Range loop
1053 if Ref.File /= Current_File then
1056 Read_File (Get_File (Ref.File, With_Dir => True), Buffer);
1057 End_Index := Buffer'First - 1;
1060 when Ada.Text_IO.Name_Error | Ada.Text_IO.End_Error =>
1061 Line := Natural'Last;
1063 Current_File := Ref.File;
1066 if Ref.Line > Line then
1068 -- Do not free Current_Line, it is referenced by the last
1069 -- Ref we processed.
1072 Index := End_Index + 1;
1075 End_Index := End_Index + 1;
1076 exit when End_Index > Buffer'Last
1077 or else Buffer (End_Index) = ASCII.LF;
1080 -- Skip spaces at beginning of line
1082 while Index < End_Index and then
1083 (Buffer (Index) = ' ' or else Buffer (Index) = ASCII.HT)
1089 exit when Ref.Line = Line;
1092 Current_Line := new String'(Buffer (Index .. End_Index - 1));
1095 Ref.Source_Line := Current_Line;
1100 end Grep_Source_Files;
1107 (File_Name : String;
1108 Contents : out GNAT.OS_Lib.String_Access)
1110 Name_0 : constant String := File_Name & ASCII.NUL;
1111 FD : constant File_Descriptor := Open_Read (Name_0'Address, Binary);
1115 if FD = Invalid_FD then
1116 raise Ada.Text_IO.Name_Error;
1119 -- Include room for EOF char
1121 Length := Natural (File_Length (FD));
1124 Buffer : String (1 .. Length + 1);
1125 This_Read : Integer;
1126 Read_Ptr : Natural := 1;
1130 This_Read := Read (FD,
1131 A => Buffer (Read_Ptr)'Address,
1132 N => Length + 1 - Read_Ptr);
1133 Read_Ptr := Read_Ptr + Integer'Max (This_Read, 0);
1134 exit when This_Read <= 0;
1137 Buffer (Read_Ptr) := EOF;
1138 Contents := new String'(Buffer (1 .. Read_Ptr));
1140 -- Things are not simple on VMS due to the plethora of file types
1141 -- and organizations. It seems clear that there shouldn't be more
1142 -- bytes read than are contained in the file though.
1144 if (Hostparm.OpenVMS and then Read_Ptr > Length + 1)
1145 or else (not Hostparm.OpenVMS and then Read_Ptr /= Length + 1)
1147 raise Ada.Text_IO.End_Error;
1154 -----------------------
1155 -- Longest_File_Name --
1156 -----------------------
1158 function Longest_File_Name return Natural is
1160 return Longest_File_Name_In_Table;
1161 end Longest_File_Name;
1168 (File : File_Reference;
1173 Ref : Ref_In_File_Ptr := File.Lines;
1176 while Ref /= null loop
1177 if (Ref.Line = 0 or else Ref.Line = Line)
1178 and then (Ref.Column = 0 or else Ref.Column = Column)
1193 function Match (Decl : Declaration_Reference) return Boolean is
1202 function Next (E : File_Reference) return File_Reference is
1207 function Next (E : Declaration_Reference) return Declaration_Reference is
1216 function Next_Obj_Dir return String is
1217 First : constant Integer := Directories.Obj_Dir_Index;
1221 Last := Directories.Obj_Dir_Index;
1223 if Last > Directories.Obj_Dir_Length then
1224 return String'(1 .. 0 => ' ');
1227 while Directories.Obj_Dir (Last) /= Path_Separator loop
1231 Directories.Obj_Dir_Index := Last + 1;
1232 Directories.Last_Obj_Dir_Start := First;
1233 return Directories.Obj_Dir (First .. Last - 1);
1236 -------------------------
1237 -- Next_Unvisited_File --
1238 -------------------------
1240 function Next_Unvisited_File return File_Reference is
1241 procedure Unchecked_Free is new Ada.Unchecked_Deallocation
1242 (Unvisited_Files_Record, Unvisited_Files_Access);
1244 Ref : File_Reference;
1245 Tmp : Unvisited_Files_Access;
1248 if Unvisited_Files = null then
1251 Tmp := Unvisited_Files;
1252 Ref := Unvisited_Files.File;
1253 Unvisited_Files := Unvisited_Files.Next;
1254 Unchecked_Free (Tmp);
1257 end Next_Unvisited_File;
1259 ----------------------
1260 -- Parse_Gnatls_Src --
1261 ----------------------
1263 function Parse_Gnatls_Src return String is
1268 for J in 1 .. Osint.Nb_Dir_In_Src_Search_Path loop
1269 if Osint.Dir_In_Src_Search_Path (J)'Length = 0 then
1270 Length := Length + 2;
1272 Length := Length + Osint.Dir_In_Src_Search_Path (J)'Length + 1;
1277 Result : String (1 .. Length);
1282 for J in 1 .. Osint.Nb_Dir_In_Src_Search_Path loop
1283 if Osint.Dir_In_Src_Search_Path (J)'Length = 0 then
1284 Result (L .. L + 1) := "." & Path_Separator;
1288 Result (L .. L + Osint.Dir_In_Src_Search_Path (J)'Length - 1) :=
1289 Osint.Dir_In_Src_Search_Path (J).all;
1290 L := L + Osint.Dir_In_Src_Search_Path (J)'Length;
1291 Result (L) := Path_Separator;
1298 end Parse_Gnatls_Src;
1300 ----------------------
1301 -- Parse_Gnatls_Obj --
1302 ----------------------
1304 function Parse_Gnatls_Obj return String is
1309 for J in 1 .. Osint.Nb_Dir_In_Obj_Search_Path loop
1310 if Osint.Dir_In_Obj_Search_Path (J)'Length = 0 then
1311 Length := Length + 2;
1313 Length := Length + Osint.Dir_In_Obj_Search_Path (J)'Length + 1;
1318 Result : String (1 .. Length);
1323 for J in 1 .. Osint.Nb_Dir_In_Obj_Search_Path loop
1324 if Osint.Dir_In_Obj_Search_Path (J)'Length = 0 then
1325 Result (L .. L + 1) := "." & Path_Separator;
1328 Result (L .. L + Osint.Dir_In_Obj_Search_Path (J)'Length - 1) :=
1329 Osint.Dir_In_Obj_Search_Path (J).all;
1330 L := L + Osint.Dir_In_Obj_Search_Path (J)'Length;
1331 Result (L) := Path_Separator;
1338 end Parse_Gnatls_Obj;
1344 procedure Reset_Obj_Dir is
1346 Directories.Obj_Dir_Index := 1;
1349 -----------------------
1350 -- Set_Default_Match --
1351 -----------------------
1353 procedure Set_Default_Match (Value : Boolean) is
1355 Default_Match := Value;
1356 end Set_Default_Match;
1362 procedure Free (Str : in out Cst_String_Access) is
1363 function Convert is new Ada.Unchecked_Conversion
1364 (Cst_String_Access, GNAT.OS_Lib.String_Access);
1366 S : GNAT.OS_Lib.String_Access := Convert (Str);
1373 ---------------------
1374 -- Reset_Directory --
1375 ---------------------
1377 procedure Reset_Directory (File : File_Reference) is
1380 end Reset_Directory;
1386 procedure Set_Unvisited (File_Ref : File_Reference) is
1387 F : constant String := Get_File (File_Ref, With_Dir => False);
1390 File_Ref.Visited := False;
1392 -- ??? Do not add a source file to the list. This is true at
1393 -- least for gnatxref, and probably for gnatfind as wel
1396 and then F (F'Last - 3 .. F'Last) = ".ali"
1398 Unvisited_Files := new Unvisited_Files_Record'
1400 Next => Unvisited_Files);
1404 ----------------------
1405 -- Get_Declarations --
1406 ----------------------
1408 function Get_Declarations
1409 (Sorted : Boolean := True)
1410 return Declaration_Array_Access
1412 Arr : constant Declaration_Array_Access :=
1413 new Declaration_Array (1 .. Entities_Count);
1414 Decl : Declaration_Reference := Entities_HTable.Get_First;
1415 Index : Natural := Arr'First;
1416 Tmp : Declaration_Reference;
1418 procedure Move (From : Natural; To : Natural);
1419 function Lt (Op1, Op2 : Natural) return Boolean;
1420 -- See GNAT.Heap_Sort_G
1426 function Lt (Op1, Op2 : Natural) return Boolean is
1429 return Is_Less_Than (Tmp, Arr (Op2));
1431 return Is_Less_Than (Arr (Op1), Tmp);
1433 return Is_Less_Than (Arr (Op1), Arr (Op2));
1441 procedure Move (From : Natural; To : Natural) is
1448 Arr (To) := Arr (From);
1452 package Decl_Sort is new GNAT.Heap_Sort_G (Move, Lt);
1454 -- Start of processing for Get_Declarations
1457 while Decl /= null loop
1458 Arr (Index) := Decl;
1460 Decl := Entities_HTable.Get_Next;
1463 if Sorted and then Arr'Length /= 0 then
1464 Decl_Sort.Sort (Entities_Count);
1468 end Get_Declarations;
1470 ----------------------
1471 -- References_Count --
1472 ----------------------
1474 function References_Count
1475 (Decl : Declaration_Reference;
1476 Get_Reads : Boolean := False;
1477 Get_Writes : Boolean := False;
1478 Get_Bodies : Boolean := False)
1481 function List_Length (E : Reference) return Natural;
1482 -- Return the number of references in E
1488 function List_Length (E : Reference) return Natural is
1490 E1 : Reference := E;
1493 while E1 /= null loop
1501 Length : Natural := 0;
1503 -- Start of processing for References_Count
1507 Length := List_Length (Decl.Ref_Ref);
1511 Length := Length + List_Length (Decl.Modif_Ref);
1515 Length := Length + List_Length (Decl.Body_Ref);
1519 end References_Count;
1521 ----------------------
1522 -- Store_References --
1523 ----------------------
1525 procedure Store_References
1526 (Decl : Declaration_Reference;
1527 Get_Writes : Boolean := False;
1528 Get_Reads : Boolean := False;
1529 Get_Bodies : Boolean := False;
1530 Get_Declaration : Boolean := False;
1531 Arr : in out Reference_Array;
1532 Index : in out Natural)
1534 procedure Add (List : Reference);
1535 -- Add all the references in List to Arr
1541 procedure Add (List : Reference) is
1542 E : Reference := List;
1544 while E /= null loop
1551 -- Start of processing for Store_References
1554 if Get_Declaration then
1563 Add (Decl.Modif_Ref);
1567 Add (Decl.Body_Ref);
1569 end Store_References;
1571 --------------------
1572 -- Get_References --
1573 --------------------
1575 function Get_References
1576 (Decl : Declaration_Reference;
1577 Get_Reads : Boolean := False;
1578 Get_Writes : Boolean := False;
1579 Get_Bodies : Boolean := False)
1580 return Reference_Array_Access
1582 Length : constant Natural :=
1583 References_Count (Decl, Get_Reads, Get_Writes, Get_Bodies);
1585 Arr : constant Reference_Array_Access :=
1586 new Reference_Array (1 .. Length);
1588 Index : Natural := Arr'First;
1593 Get_Writes => Get_Writes,
1594 Get_Reads => Get_Reads,
1595 Get_Bodies => Get_Bodies,
1596 Get_Declaration => False,
1600 if Arr'Length /= 0 then
1611 procedure Free (Arr : in out Reference_Array_Access) is
1612 procedure Internal is new Ada.Unchecked_Deallocation
1613 (Reference_Array, Reference_Array_Access);
1622 function Is_Parameter (Decl : Declaration_Reference) return Boolean is
1624 return Decl.Is_Parameter;