xref_lib.adb 57.4 KB
Newer Older
Richard Kenner committed
1 2 3 4
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
5
--                             X R E F _ L I B                              --
Richard Kenner committed
6 7 8
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--          Copyright (C) 1998-2018, Free Software Foundation, Inc.         --
Richard Kenner committed
10 11 12
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
13
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
Richard Kenner committed
14 15 16 17
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
18 19
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
-- http://www.gnu.org/licenses for a complete copy of the license.          --
Richard Kenner committed
20
--                                                                          --
21 22
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
Richard Kenner committed
23 24 25 26
--                                                                          --
------------------------------------------------------------------------------

with Osint;
27 28 29
with Output; use Output;
with Types;  use Types;

Richard Kenner committed
30 31
with Unchecked_Deallocation;

32
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
33
with Ada.Text_IO;
34 35 36 37

with GNAT.Command_Line; use GNAT.Command_Line;
with GNAT.IO_Aux;       use GNAT.IO_Aux;

Richard Kenner committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51
package body Xref_Lib is

   Type_Position : constant := 50;
   --  Column for label identifying type of entity

   ---------------------
   -- Local Variables --
   ---------------------

   Pipe : constant Character := '|';
   --  First character on xref lines in the .ali file

   No_Xref_Information : exception;
   --  Exception raised when there is no cross-referencing information in
Arnaud Charlet committed
52
   --  the .ali files.
Richard Kenner committed
53

54
   procedure Parse_EOL
55
     (Source                 : not null access String;
56 57
      Ptr                    : in out Positive;
      Skip_Continuation_Line : Boolean := False);
Richard Kenner committed
58 59
   --  On return Source (Ptr) is the first character of the next line
   --  or EOF. Source.all must be terminated by EOF.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
   --
   --  If Skip_Continuation_Line is True, this subprogram skips as many
   --  lines as required when the second or more lines starts with '.'
   --  (continuation lines in ALI files).

   function Current_Xref_File (File : ALI_File) return File_Reference;
   --  Return the file matching the last 'X' line we found while parsing
   --  the ALI file.

   function File_Name (File : ALI_File; Num : Positive) return File_Reference;
   --  Returns the dependency file name number Num

   function Get_Full_Type (Decl : Declaration_Reference) return String;
   --  Returns the full type corresponding to a type letter as found in
   --  the .ali files.

   procedure Open
77
     (Name         : String;
78
      File         : out ALI_File;
79
      Dependencies : Boolean := False);
80 81
   --  Open a new ALI file. If Dependencies is True, the insert every library
   --  file 'with'ed in the files database (used for gnatxref)
Richard Kenner committed
82 83 84 85 86 87 88

   procedure Parse_Identifier_Info
     (Pattern       : Search_Pattern;
      File          : in out ALI_File;
      Local_Symbols : Boolean;
      Der_Info      : Boolean := False;
      Type_Tree     : Boolean := False;
89 90
      Wide_Search   : Boolean := True;
      Labels_As_Ref : Boolean := True);
Richard Kenner committed
91 92
   --  Output the file and the line where the identifier was referenced,
   --  If Local_Symbols is False then only the publicly visible symbols
93 94 95 96 97
   --  will be processed.
   --
   --  If Labels_As_Ref is true, then the references to the entities after
   --  the end statements ("end Foo") will be counted as actual references.
   --  The entity will never be reported as unreferenced by gnatxref -u
Richard Kenner committed
98 99

   procedure Parse_Token
100
     (Source    : not null access String;
Richard Kenner committed
101 102 103
      Ptr       : in out Positive;
      Token_Ptr : out Positive);
   --  Skips any separators and stores the start of the token in Token_Ptr.
104 105 106
   --  Then stores the position of the next separator in Ptr. On return
   --  Source (Token_Ptr .. Ptr - 1) is the token. Separators are space
   --  and ASCII.HT. Parse_Token will never skip to the next line.
Richard Kenner committed
107 108

   procedure Parse_Number
109
     (Source : not null access String;
Richard Kenner committed
110 111
      Ptr    : in out Positive;
      Number : out Natural);
112
   --  Skips any separators and parses Source up to the first character that
Richard Kenner committed
113 114 115 116 117 118
   --  is not a decimal digit. Returns value of parsed digits or 0 if none.

   procedure Parse_X_Filename (File : in out ALI_File);
   --  Reads and processes "X..." lines in the ALI file
   --  and updates the File.X_File information.

119 120 121 122 123 124 125 126 127 128
   procedure Skip_To_First_X_Line
     (File    : in out ALI_File;
      D_Lines : Boolean;
      W_Lines : Boolean);
   --  Skip the lines in the ALI file until the first cross-reference line
   --  (^X...) is found. Search is started from the beginning of the file.
   --  If not such line is found, No_Xref_Information is raised.
   --  If W_Lines is false, then the lines "^W" are not parsed.
   --  If D_Lines is false, then the lines "^D" are not parsed.

Richard Kenner committed
129 130 131 132 133 134 135 136 137
   ----------------
   -- Add_Entity --
   ----------------

   procedure Add_Entity
     (Pattern : in out Search_Pattern;
      Entity  : String;
      Glob    : Boolean := False)
   is
138 139 140 141 142 143 144 145
      File_Start : Natural;
      Line_Start : Natural;
      Col_Start  : Natural;
      Line_Num   : Natural := 0;
      Col_Num    : Natural := 0;

      File_Ref : File_Reference := Empty_File;
      pragma Warnings (Off, File_Ref);
Richard Kenner committed
146 147 148 149 150 151 152

   begin
      --  Find the end of the first item in Entity (pattern or file?)
      --  If there is no ':', we only have a pattern

      File_Start := Index (Entity, ":");

153
      --  If the regular expression is invalid, just consider it as a string
Richard Kenner committed
154

155
      if File_Start = 0 then
Richard Kenner committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
         begin
            Pattern.Entity := Compile (Entity, Glob, False);
            Pattern.Initialized := True;

         exception
            when Error_In_Regexp =>

               --  The basic idea is to insert a \ before every character

               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 there is a dot in the pattern, then it is a file name

      if (Glob and then
188 189 190 191
           Index (Entity (Entity'First .. File_Start - 1), ".") /= 0)
             or else
              (not Glob
                 and then Index (Entity (Entity'First .. File_Start - 1),
Richard Kenner committed
192 193
                                   "\.") /= 0)
      then
194
         Pattern.Entity      := Compile (".*", False);
Richard Kenner committed
195
         Pattern.Initialized := True;
196
         File_Start          := Entity'First;
Richard Kenner committed
197 198

      else
199
         --  If the regular expression is invalid, just consider it as a string
Richard Kenner committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

         begin
            Pattern.Entity :=
              Compile (Entity (Entity'First .. File_Start - 1), Glob, False);
            Pattern.Initialized := True;

         exception
            when Error_In_Regexp =>

               --  The basic idea is to insert a \ before every character

               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;

Arnaud Charlet committed
227
         File_Start := File_Start + 1;
Richard Kenner committed
228 229 230 231 232 233
      end if;

      --  Parse the file name

      Line_Start := Index (Entity (File_Start .. Entity'Last), ":");

Arnaud Charlet committed
234
      --  Check if it was a disk:\directory item (for Windows)
Richard Kenner committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

      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;

275 276
      declare
         File_Name : String := Entity (File_Start .. Line_Start - 1);
Arnaud Charlet committed
277

278 279 280 281 282 283 284 285 286 287 288 289 290
      begin
         Osint.Canonical_Case_File_Name (File_Name);
         File_Ref := Add_To_Xref_File (File_Name, 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 (File_Name),
              Visited      => False,
              Emit_Warning => True);
      end;
Richard Kenner committed
291 292
   end Add_Entity;

293 294 295
   -------------------
   -- Add_Xref_File --
   -------------------
Richard Kenner committed
296

297
   procedure Add_Xref_File (File : String) is
298
      File_Ref : File_Reference := Empty_File;
Arnaud Charlet committed
299 300
      pragma Unreferenced (File_Ref);

301
      Iterator : Expansion_Iterator;
Richard Kenner committed
302

303
      procedure Add_Xref_File_Internal (File : String);
Richard Kenner committed
304 305
      --  Do the actual addition of the file

306 307 308
      ----------------------------
      -- Add_Xref_File_Internal --
      ----------------------------
Richard Kenner committed
309

310
      procedure Add_Xref_File_Internal (File : String) is
Richard Kenner committed
311 312 313 314
      begin
         --  Case where we have an ALI file, accept it even though this is
         --  not official usage, since the intention is obvious

Arnaud Charlet committed
315
         if Tail (File, 4) = "." & Osint.ALI_Suffix.all then
316
            File_Ref := Add_To_Xref_File
Arnaud Charlet committed
317
                          (File, Visited => False, Emit_Warning => True);
Richard Kenner committed
318 319 320 321

         --  Normal non-ali file case

         else
322
            File_Ref := Add_To_Xref_File (File, Visited => True);
Richard Kenner committed
323

324
            File_Ref := Add_To_Xref_File
Arnaud Charlet committed
325 326
                         (ALI_File_Name (File),
                          Visited => False, Emit_Warning => True);
Richard Kenner committed
327
         end if;
328
      end Add_Xref_File_Internal;
Richard Kenner committed
329

330
   --  Start of processing for Add_Xref_File
Richard Kenner committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

   begin
      --  Check if we need to do the expansion

      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;
346
               Add_Xref_File_Internal (S);
Richard Kenner committed
347 348 349 350
            end;
         end loop;

      else
351
         Add_Xref_File_Internal (File);
Richard Kenner committed
352
      end if;
353
   end Add_Xref_File;
Richard Kenner committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367

   -----------------------
   -- Current_Xref_File --
   -----------------------

   function Current_Xref_File (File : ALI_File) return File_Reference is
   begin
      return File.X_File;
   end Current_Xref_File;

   --------------------------
   -- Default_Project_File --
   --------------------------

Arnaud Charlet committed
368
   function Default_Project_File (Dir_Name : String) return String is
Richard Kenner committed
369 370 371 372 373 374 375 376 377 378 379 380 381
      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

382
            --  The first project file found is the good one
Richard Kenner committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

            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;

   ---------------
   -- File_Name --
   ---------------

   function File_Name
     (File : ALI_File;
Arnaud Charlet committed
402
      Num  : Positive) return File_Reference
Richard Kenner committed
403
   is
Arnaud Charlet committed
404
      Table : Table_Type renames File.Dep.Table (1 .. Last (File.Dep));
Richard Kenner committed
405
   begin
Arnaud Charlet committed
406
      return Table (Num);
Richard Kenner committed
407 408 409 410 411 412 413
   end File_Name;

   --------------------
   -- Find_ALI_Files --
   --------------------

   procedure Find_ALI_Files is
Arnaud Charlet committed
414 415 416 417 418 419
      My_Dir  : Rec_DIR;
      Dir_Ent : File_Name_String;
      Last    : Natural;

      File_Ref : File_Reference;
      pragma Unreferenced (File_Ref);
Richard Kenner committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

      function Open_Next_Dir return Boolean;
      --  Tries to open the next object directory, and return False if
      --  the directory cannot be opened.

      -------------------
      -- Open_Next_Dir --
      -------------------

      function Open_Next_Dir return Boolean is
      begin
         --  Until we are able to open a new directory

         loop
            declare
               Obj_Dir : constant String := Next_Obj_Dir;

            begin
438
               --  Case of no more Obj_Dir lines
Richard Kenner committed
439 440 441 442 443 444 445 446 447

               if Obj_Dir'Length = 0 then
                  return False;
               end if;

               Open (My_Dir.Dir, Obj_Dir);
               exit;

            exception
448

Richard Kenner committed
449 450 451 452 453 454 455 456 457 458 459 460
               --  Could not open the directory

               when Directory_Error => null;
            end;
         end loop;

         return True;
      end Open_Next_Dir;

   --  Start of processing for Find_ALI_Files

   begin
461 462
      Reset_Obj_Dir;

Richard Kenner committed
463 464 465 466 467 468 469 470 471 472 473
      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;

Arnaud Charlet committed
474 475 476
            elsif Last > 4
              and then Dir_Ent (Last - 3 .. Last) = "." & Osint.ALI_Suffix.all
            then
477 478
               File_Ref :=
                 Add_To_Xref_File (Dir_Ent (1 .. Last), Visited => False);
Richard Kenner committed
479 480 481 482 483 484 485 486 487
            end if;
         end loop;
      end if;
   end Find_ALI_Files;

   -------------------
   -- Get_Full_Type --
   -------------------

488 489 490
   function Get_Full_Type (Decl : Declaration_Reference) return String is

      function Param_String return String;
Arnaud Charlet committed
491
      --  Return the string to display depending on whether Decl is a parameter
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

      ------------------
      -- Param_String --
      ------------------

      function Param_String return String is
      begin
         if Is_Parameter (Decl) then
            return "parameter ";
         else
            return "";
         end if;
      end Param_String;

   --  Start of processing for Get_Full_Type

Richard Kenner committed
508
   begin
509
      case Get_Type (Decl) is
Richard Kenner committed
510 511 512 513 514 515
         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";
516
         when 'H' => return "abstract type";
Richard Kenner committed
517 518 519 520 521 522 523 524 525
         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";

Arnaud Charlet committed
526
         when 'a' => return Param_String & "array object";
527 528 529 530 531 532
         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";
Arnaud Charlet committed
533
         when 'j' => return Param_String & "class object";
534 535 536 537 538 539 540 541 542
         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";
Richard Kenner committed
543

544 545
         when 'h' => return "interface";
         when 'g' => return "macro";
Arnaud Charlet committed
546
         when 'G' => return "function macro";
Arnaud Charlet committed
547
         when 'J' => return "class";
Richard Kenner committed
548 549 550 551 552 553 554
         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";
555
         when 'Q' => return "include file";
Richard Kenner committed
556 557 558 559 560 561 562
         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";

563
         when '+' => return "private type";
Arnaud Charlet committed
564
         when '*' => return "private variable";
565 566 567 568 569

         --  The above should be the only possibilities, but for this kind
         --  of informational output, we don't want to bomb if we find
         --  something else, so just return three question marks when we
         --  have an unknown Abbrev value
Richard Kenner committed
570 571

         when others =>
572 573 574 575 576
            if Is_Parameter (Decl) then
               return "parameter";
            else
               return "??? (" & Get_Type (Decl) & ")";
            end if;
Richard Kenner committed
577 578 579
      end case;
   end Get_Full_Type;

580 581 582
   --------------------------
   -- Skip_To_First_X_Line --
   --------------------------
Richard Kenner committed
583

584 585 586 587
   procedure Skip_To_First_X_Line
     (File    : in out ALI_File;
      D_Lines : Boolean;
      W_Lines : Boolean)
Richard Kenner committed
588 589 590
   is
      Ali              : String_Access renames File.Buffer;
      Token            : Positive;
591 592
      Ptr              : Positive := Ali'First;
      Num_Dependencies : Natural  := 0;
Richard Kenner committed
593 594 595 596 597
      File_Start       : Positive;
      File_End         : Positive;
      Gnatchop_Offset  : Integer;
      Gnatchop_Name    : Positive;

Arnaud Charlet committed
598 599 600
      File_Ref : File_Reference;
      pragma Unreferenced (File_Ref);

Richard Kenner committed
601 602 603 604 605 606 607
   begin
      --  Read all the lines possibly processing with-clauses and dependency
      --  information and exit on finding the first Xref line.
      --  A fall-through of the loop means that there is no xref information
      --  which is an error condition.

      while Ali (Ptr) /= EOF loop
608 609
         if D_Lines and then Ali (Ptr) = 'D' then

Richard Kenner committed
610
            --  Found dependency information. Format looks like:
611
            --  D src-nam time-stmp checksum [subunit-name] [line:file-name]
Richard Kenner committed
612

613
            --  Skip the D and parse the filenam
Richard Kenner committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

            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); --  Skip time-stamp
            Parse_Token (Ali, Ptr, Token); --  Skip checksum
            Parse_Token (Ali, Ptr, Token); --  Read next entity on the line

            if not (Ali (Token) in '0' .. '9') then
               Parse_Token (Ali, Ptr, Token); --  Was a subunit name
            end if;

            --  Did we have a gnatchop-ed file with a pragma Source_Reference ?
632

Richard Kenner committed
633 634 635 636 637 638 639
            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;
640

Richard Kenner committed
641 642 643 644 645
               Gnatchop_Offset :=
                 2 - Natural'Value (Ali (Token .. Gnatchop_Name - 1));
               Token := Gnatchop_Name + 1;
            end if;

Arnaud Charlet committed
646 647
            declare
               Table : Table_Type renames
648
                         File.Dep.Table (1 .. Last (File.Dep));
Arnaud Charlet committed
649 650 651 652 653 654
            begin
               Table (Num_Dependencies) := Add_To_Xref_File
                 (Ali (File_Start .. File_End),
                  Gnatchop_File => Ali (Token .. Ptr - 1),
                  Gnatchop_Offset => Gnatchop_Offset);
            end;
Richard Kenner committed
655

656 657
         elsif W_Lines and then Ali (Ptr) = 'W' then

Richard Kenner committed
658 659 660 661 662 663 664 665 666
            --  Found with-clause information. Format looks like:
            --     "W debug%s               debug.adb               debug.ali"

            --  Skip the W and parse the .ali filename (3rd token)

            Parse_Token (Ali, Ptr, Token);
            Parse_Token (Ali, Ptr, Token);
            Parse_Token (Ali, Ptr, Token);

Arnaud Charlet committed
667 668
            File_Ref :=
              Add_To_Xref_File (Ali (Token .. Ptr - 1), Visited => False);
Richard Kenner committed
669

670
         elsif Ali (Ptr) = 'X' then
671

Richard Kenner committed
672 673 674 675 676 677 678 679 680 681 682
            --  Found a cross-referencing line - stop processing

            File.Current_Line := Ptr;
            File.Xref_Line    := Ptr;
            return;
         end if;

         Parse_EOL (Ali, Ptr);
      end loop;

      raise No_Xref_Information;
683 684 685 686 687 688 689 690 691 692 693 694
   end Skip_To_First_X_Line;

   ----------
   -- Open --
   ----------

   procedure Open
     (Name         : String;
      File         : out ALI_File;
      Dependencies : Boolean := False)
   is
      Ali : String_Access renames File.Buffer;
695
      pragma Warnings (Off, Ali);
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

   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);
Richard Kenner committed
713 714 715 716 717 718
   end Open;

   ---------------
   -- Parse_EOL --
   ---------------

719
   procedure Parse_EOL
720
     (Source                 : not null access String;
721 722 723
      Ptr                    : in out Positive;
      Skip_Continuation_Line : Boolean := False)
   is
Richard Kenner committed
724 725
   begin
      loop
726
         --  Skip to end of line
Richard Kenner committed
727

728 729 730 731 732 733
         while Source (Ptr) /= ASCII.CR and then Source (Ptr) /= ASCII.LF
           and then Source (Ptr) /= EOF
         loop
            Ptr := Ptr + 1;
         end loop;

Arnaud Charlet committed
734
         --  Skip CR or LF if not at end of file
Arnaud Charlet committed
735

Arnaud Charlet committed
736
         if Source (Ptr) /= EOF then
Arnaud Charlet committed
737
            Ptr := Ptr + 1;
738
         end if;
Richard Kenner committed
739

740
         --  Skip past CR/LF or LF/CR combination
Richard Kenner committed
741

742 743 744 745 746 747 748 749
         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;
Richard Kenner committed
750 751 752 753 754 755 756 757 758 759 760 761
   end Parse_EOL;

   ---------------------------
   -- Parse_Identifier_Info --
   ---------------------------

   procedure Parse_Identifier_Info
     (Pattern       : Search_Pattern;
      File          : in out ALI_File;
      Local_Symbols : Boolean;
      Der_Info      : Boolean := False;
      Type_Tree     : Boolean := False;
762 763
      Wide_Search   : Boolean := True;
      Labels_As_Ref : Boolean := True)
Richard Kenner committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
   is
      Ptr      : Positive renames File.Current_Line;
      Ali      : String_Access renames File.Buffer;

      E_Line   : Natural;   --  Line number of current entity
      E_Col    : Natural;   --  Column number of current entity
      E_Type   : Character; --  Type of current entity
      E_Name   : Positive;  --  Pointer to begin of entity name
      E_Global : Boolean;   --  True iff entity is global

      R_Line   : Natural;   --  Line number of current reference
      R_Col    : Natural;   --  Column number of current reference
      R_Type   : Character; --  Type of current reference

      Decl_Ref : Declaration_Reference;
      File_Ref : File_Reference := Current_Xref_File (File);

      function Get_Symbol_Name (Eun, Line, Col : Natural) return String;
      --  Returns the symbol name for the entity defined at the specified
      --  line and column in the dependent unit number Eun. For this we need
      --  to parse the ali file again because the parent entity is not in
      --  the declaration table if it did not match the search pattern.

787 788 789 790
      procedure Skip_To_Matching_Closing_Bracket;
      --  When Ptr points to an opening square bracket, moves it to the
      --  character following the matching closing bracket

Richard Kenner committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
      ---------------------
      -- Get_Symbol_Name --
      ---------------------

      function Get_Symbol_Name (Eun, Line, Col : Natural) return String is
         Ptr    : Positive := 1;
         E_Eun  : Positive;   --  Unit number of current entity
         E_Line : Natural;    --  Line number of current entity
         E_Col  : Natural;    --  Column number of current entity
         E_Name : Positive;   --  Pointer to begin of entity name

      begin
         --  Look for the X lines corresponding to unit Eun

         loop
            if Ali (Ptr) = 'X' then
               Ptr := Ptr + 1;
               Parse_Number (Ali, Ptr, E_Eun);
               exit when E_Eun = Eun;
            end if;

812
            Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
Richard Kenner committed
813 814 815 816 817 818 819
         end loop;

         --  Here we are in the right Ali section, we now look for the entity
         --  declared at position (Line, Col).

         loop
            Parse_Number (Ali, Ptr, E_Line);
820
            exit when Ali (Ptr) = EOF;
Richard Kenner committed
821 822
            Ptr := Ptr + 1;
            Parse_Number (Ali, Ptr, E_Col);
823
            exit when Ali (Ptr) = EOF;
Richard Kenner committed
824 825 826 827 828 829 830
            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;

831 832
            Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
            exit when Ali (Ptr) = EOF;
Richard Kenner committed
833 834
         end loop;

835
         --  We were not able to find the symbol, this should not happen but
Richard Kenner committed
836 837 838 839 840 841
         --  since we don't want to stop here we return a string of three
         --  question marks as the symbol name.

         return "???";
      end Get_Symbol_Name;

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
      --------------------------------------
      -- Skip_To_Matching_Closing_Bracket --
      --------------------------------------

      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;

Arnaud Charlet committed
863 864
      Table : Table_Type renames File.Dep.Table (1 .. Last (File.Dep));

Richard Kenner committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
   --  Start of processing for Parse_Identifier_Info

   begin
      --  The identifier info looks like:
      --     "38U9*Debug 12|36r6 36r19"

      --  Extract the line, column and entity name information

      Parse_Number (Ali, Ptr, E_Line);

      if Ali (Ptr) > ' ' then
         E_Type := Ali (Ptr);
         Ptr := Ptr + 1;
      end if;

880 881 882 883 884 885 886 887 888 889 890
      --  Ignore some of the entities (labels,...)

      case E_Type is
         when 'l' | 'L' | 'q' =>
            Parse_EOL (Ali, Ptr, Skip_Continuation_Line => True);
            return;

         when others =>
            null;
      end case;

Richard Kenner committed
891 892 893 894 895 896 897 898 899 900
      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);

901 902
      --  Exit if the symbol does not match or if we have a local symbol and we
      --  do not want it or if the file is unknown.
Arnaud Charlet committed
903 904 905 906

      if File.X_File = Empty_File then
         return;
      end if;
Richard Kenner committed
907 908 909

      if (not Local_Symbols and not E_Global)
        or else (Pattern.Initialized
910
                  and then not Match (Ali (E_Name .. Ptr - 1), Pattern.Entity))
Richard Kenner committed
911 912
        or else (E_Name >= Ptr)
      then
913 914 915 916
         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);
Richard Kenner committed
917 918 919 920 921 922 923 924
         return;
      end if;

      --  Insert the declaration in the table

      Decl_Ref := Add_Declaration
        (File.X_File, Ali (E_Name .. Ptr - 1), E_Line, E_Col, E_Type);

925 926
      if Ali (Ptr) = '[' then
         Skip_To_Matching_Closing_Bracket;
927
      end if;
928

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
      --  Skip any renaming indication

      if Ali (Ptr) = '=' then
         declare
            P_Line, P_Column : Natural;
            pragma Warnings (Off, P_Line);
            pragma Warnings (Off, P_Column);
         begin
            Ptr := Ptr + 1;
            Parse_Number (Ali, Ptr, P_Line);
            Ptr := Ptr + 1;
            Parse_Number (Ali, Ptr, P_Column);
         end;
      end if;

Arnaud Charlet committed
944 945 946 947 948
      while Ptr <= Ali'Last
         and then (Ali (Ptr) = '<'
                   or else Ali (Ptr) = '('
                   or else Ali (Ptr) = '{')
      loop
Richard Kenner committed
949 950 951 952 953 954
         --  Here we have a type derivation information. The format is
         --  <3|12I45> which means that the current entity is derived from the
         --  type defined in unit number 3, line 12 column 45. The pipe and
         --  unit number is optional. It is specified only if the parent type
         --  is not defined in the current unit.

Arnaud Charlet committed
955 956 957
         --  We also have the format for generic instantiations, as in
         --  7a5*Uid(3|5I8[4|2]) 2|4r74

958 959 960 961
         --  We could also have something like
         --  16I9*I<integer>
         --  that indicates that I derives from the predefined type integer.

Richard Kenner committed
962 963
         Ptr := Ptr + 1;

964 965 966 967 968
         if Ali (Ptr) in '0' .. '9' then
            Parse_Derived_Info : declare
               P_Line   : Natural;          --  parent entity line
               P_Column : Natural;          --  parent entity column
               P_Eun    : Positive;         --  parent entity file number
Richard Kenner committed
969

970 971
            begin
               Parse_Number (Ali, Ptr, P_Line);
Richard Kenner committed
972

973
               --  If we have a pipe then the first number was the unit number
Richard Kenner committed
974

975 976 977
               if Ali (Ptr) = '|' then
                  P_Eun := P_Line;
                  Ptr := Ptr + 1;
Richard Kenner committed
978

979
                  --  Now we have the line number
Richard Kenner committed
980

981
                  Parse_Number (Ali, Ptr, P_Line);
Richard Kenner committed
982

983 984 985
               else
                  --  We don't have a unit number specified, so we set P_Eun to
                  --  the current unit.
Richard Kenner committed
986

Arnaud Charlet committed
987
                  for K in Table'Range loop
988
                     P_Eun := K;
Arnaud Charlet committed
989
                     exit when Table (K) = File_Ref;
990 991
                  end loop;
               end if;
Richard Kenner committed
992

993
               --  Then parse the type and column number
Richard Kenner committed
994

995 996
               Ptr := Ptr + 1;
               Parse_Number (Ali, Ptr, P_Column);
Richard Kenner committed
997

Arnaud Charlet committed
998 999 1000
               --  Skip the information for generics instantiations

               if Ali (Ptr) = '[' then
1001
                  Skip_To_Matching_Closing_Bracket;
Arnaud Charlet committed
1002 1003
               end if;

1004
               --  Skip '>', or ')' or '>'
Richard Kenner committed
1005

1006
               Ptr := Ptr + 1;
Richard Kenner committed
1007

1008 1009
               --  The derived info is needed only is the derived info mode is
               --  on or if we want to output the type hierarchy
Richard Kenner committed
1010

1011
               if Der_Info or else Type_Tree then
1012 1013
                  declare
                     Symbol : constant String :=
Arnaud Charlet committed
1014
                                Get_Symbol_Name (P_Eun, P_Line, P_Column);
1015 1016 1017 1018 1019 1020 1021
                  begin
                     if Symbol /= "???" then
                        Add_Parent
                          (Decl_Ref,
                           Symbol,
                           P_Line,
                           P_Column,
Arnaud Charlet committed
1022
                           Table (P_Eun));
1023 1024
                     end if;
                  end;
1025
               end if;
Richard Kenner committed
1026

1027 1028 1029 1030 1031
               if Type_Tree
                 and then (Pattern.File_Ref = Empty_File
                             or else
                           Pattern.File_Ref = Current_Xref_File (File))
               then
1032 1033 1034 1035 1036 1037 1038 1039
                  Search_Parent_Tree : declare
                     Pattern         : Search_Pattern;  --  Parent type pattern
                     File_Pos_Backup : Positive;

                  begin
                     Add_Entity
                       (Pattern,
                        Get_Symbol_Name (P_Eun, P_Line, P_Column)
Arnaud Charlet committed
1040
                        & ':' & Get_Gnatchop_File (Table (P_Eun))
1041 1042
                        & ':' & Get_Line (Get_Parent (Decl_Ref))
                        & ':' & Get_Column (Get_Parent (Decl_Ref)),
1043
                        False);
Richard Kenner committed
1044

1045 1046 1047
                     --  No default match is needed to look for the parent type
                     --  since we are using the fully qualified symbol name:
                     --  symbol:file:line:column
Richard Kenner committed
1048

1049
                     Set_Default_Match (False);
Richard Kenner committed
1050

1051 1052
                     --  The parent hierarchy is defined in the same unit as
                     --  the derived type. So we want to revisit the unit.
Richard Kenner committed
1053

1054
                     File_Pos_Backup   := File.Current_Line;
Richard Kenner committed
1055

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
                     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;
Richard Kenner committed
1070

1071 1072 1073 1074
                     File.Current_Line := File_Pos_Backup;
                  end Search_Parent_Tree;
               end if;
            end Parse_Derived_Info;
Richard Kenner committed
1075

1076 1077 1078 1079 1080 1081 1082 1083 1084
         else
            while Ali (Ptr) /= '>'
              and then Ali (Ptr) /= ')'
              and then Ali (Ptr) /= '}'
            loop
               Ptr := Ptr + 1;
            end loop;
            Ptr := Ptr + 1;
         end if;
Arnaud Charlet committed
1085
      end loop;
Richard Kenner committed
1086 1087 1088 1089 1090

      --  To find the body, we will have to parse the file too

      if Wide_Search then
         declare
Arnaud Charlet committed
1091
            File_Name : constant String := Get_Gnatchop_File (File.X_File);
Arnaud Charlet committed
1092
            Ignored : File_Reference;
Richard Kenner committed
1093
         begin
Arnaud Charlet committed
1094
            Ignored := Add_To_Xref_File (ALI_File_Name (File_Name), False);
Richard Kenner committed
1095 1096 1097 1098 1099 1100 1101 1102 1103
         end;
      end if;

      --  Parse references to this entity.
      --  Ptr points to next reference with leading blanks

      loop
         --  Process references on current line

Arnaud Charlet committed
1104
         while Ali (Ptr) = ' ' or else Ali (Ptr) = ASCII.HT loop
Richard Kenner committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

            --  For every reference read the line, type and column,
            --  optionally preceded by a file number and a pipe symbol.

            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;

Arnaud Charlet committed
1123 1124
            --  Imported entities may have an indication specifying information
            --  about the corresponding external name:
Arnaud Charlet committed
1125 1126
            --    5U14*Foo2 5>20 6b<c,myfoo2>22   # Imported entity
            --    5U14*Foo2 5>20 6i<c,myfoo2>22   # Exported entity
1127

Arnaud Charlet committed
1128
            if (R_Type = 'b' or else R_Type = 'i')
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
              and then Ali (Ptr) = '<'
            then
               while Ptr <= Ali'Last
                 and then Ali (Ptr) /= '>'
               loop
                  Ptr := Ptr + 1;
               end loop;
               Ptr := Ptr + 1;
            end if;

Richard Kenner committed
1139 1140 1141 1142
            Parse_Number (Ali, Ptr, R_Col);

            --  Insert the reference or body in the table

1143 1144
            Add_Reference
              (Decl_Ref, File_Ref, R_Line, R_Col, R_Type, Labels_As_Ref);
Richard Kenner committed
1145

1146 1147 1148 1149 1150
            --  Skip generic information, if any

            if Ali (Ptr) = '[' then
               declare
                  Num_Nested : Integer := 1;
1151

1152 1153 1154 1155 1156 1157 1158 1159
               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;
1160

1161 1162 1163 1164 1165
                     Ptr := Ptr + 1;
                  end loop;
               end;
            end if;

Richard Kenner committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
         end loop;

         Parse_EOL (Ali, Ptr);

         --   Loop until new line is no continuation line

         exit when Ali (Ptr) /= '.';
         Ptr := Ptr + 1;
      end loop;
   end Parse_Identifier_Info;

   ------------------
   -- Parse_Number --
   ------------------

   procedure Parse_Number
1182
     (Source : not null access String;
1183 1184
      Ptr    : in out Positive;
      Number : out Natural)
Richard Kenner committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
   is
   begin
      --  Skip separators

      while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
         Ptr := Ptr + 1;
      end loop;

      Number := 0;
      while Source (Ptr) in '0' .. '9' loop
1195 1196
         Number :=
           10 * Number + (Character'Pos (Source (Ptr)) - Character'Pos ('0'));
Richard Kenner committed
1197 1198 1199 1200 1201 1202 1203 1204 1205
         Ptr := Ptr + 1;
      end loop;
   end Parse_Number;

   -----------------
   -- Parse_Token --
   -----------------

   procedure Parse_Token
1206
     (Source    : not null access String;
Richard Kenner committed
1207 1208 1209
      Ptr       : in out Positive;
      Token_Ptr : out Positive)
   is
1210
      In_Quotes : Character := ASCII.NUL;
Richard Kenner committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222

   begin
      --  Skip separators

      while Source (Ptr) = ' ' or else Source (Ptr) = ASCII.HT loop
         Ptr := Ptr + 1;
      end loop;

      Token_Ptr := Ptr;

      --  Find end-of-token

1223
      while (In_Quotes /= ASCII.NUL or else
Richard Kenner committed
1224
               not (Source (Ptr) = ' '
1225 1226 1227
                     or else Source (Ptr) = ASCII.HT
                     or else Source (Ptr) = '<'
                     or else Source (Ptr) = '{'
1228
                     or else Source (Ptr) = '['
1229 1230
                     or else Source (Ptr) = '='
                     or else Source (Ptr) = '('))
Richard Kenner committed
1231 1232
        and then Source (Ptr) >= ' '
      loop
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
         --  Double-quotes are used for operators
         --  Simple-quotes are used for character constants, for instance when
         --  they are found in an enumeration type "type A is ('+', '-');"

         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;
Richard Kenner committed
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

         Ptr := Ptr + 1;
      end loop;
   end Parse_Token;

   ----------------------
   -- Parse_X_Filename --
   ----------------------

   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;

Arnaud Charlet committed
1262 1263
      Table : Table_Type renames File.Dep.Table (1 .. Last (File.Dep));

Richard Kenner committed
1264
   begin
1265
      while Ali (Ptr) = 'X' loop
Richard Kenner committed
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

         --  The current line is the start of a new Xref file section,
         --  whose format looks like:

         --     " X 1 debug.ads"

         --  Skip the X and read the file number for the new X_File

         Ptr := Ptr + 1;
         Parse_Number (Ali, Ptr, File_Nr);

Arnaud Charlet committed
1277 1278
         --  If the referenced file is unknown, we simply ignore it

Arnaud Charlet committed
1279 1280
         if File_Nr in Table'Range then
            File.X_File := Table (File_Nr);
Arnaud Charlet committed
1281 1282
         else
            File.X_File := Empty_File;
Richard Kenner committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
         end if;

         Parse_EOL (Ali, Ptr);
      end loop;
   end Parse_X_Filename;

   --------------------
   -- Print_Gnatfind --
   --------------------

   procedure Print_Gnatfind
     (References     : Boolean;
      Full_Path_Name : Boolean)
   is
1297 1298 1299
      Decls : constant Declaration_Array_Access := Get_Declarations;
      Decl  : Declaration_Reference;
      Arr   : Reference_Array_Access;
Richard Kenner committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313

      procedure Print_Ref
        (Ref : Reference;
         Msg : String := "      ");
      --  Print a reference, according to the extended tag of the output

      ---------------
      -- Print_Ref --
      ---------------

      procedure Print_Ref
        (Ref : Reference;
         Msg : String := "      ")
      is
1314 1315 1316 1317
         F : String_Access :=
               Osint.To_Host_File_Spec
                (Get_Gnatchop_File (Ref, Full_Path_Name));

Richard Kenner committed
1318
         Buffer : constant String :=
1319 1320 1321 1322 1323
                    F.all &
                    ":" & Get_Line (Ref)   &
                    ":" & Get_Column (Ref) &
                    ": ";

Richard Kenner committed
1324 1325 1326
         Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;

      begin
1327
         Free (F);
Richard Kenner committed
1328 1329 1330 1331 1332
         Num_Blanks := Integer'Max (0, Num_Blanks);
         Write_Line
           (Buffer
            & String'(1 .. Num_Blanks => ' ')
            & Msg & " " & Get_Symbol (Decl));
1333

Richard Kenner committed
1334 1335 1336 1337 1338 1339 1340 1341
         if Get_Source_Line (Ref)'Length /= 0 then
            Write_Line ("   " & Get_Source_Line (Ref));
         end if;
      end Print_Ref;

   --  Start of processing for Print_Gnatfind

   begin
1342 1343 1344
      for D in Decls'Range loop
         Decl := Decls (D);

Richard Kenner committed
1345 1346 1347 1348 1349 1350
         if Match (Decl) then

            --  Output the declaration

            declare
               Parent : constant Declaration_Reference := Get_Parent (Decl);
1351 1352 1353 1354 1355

               F : String_Access :=
                     Osint.To_Host_File_Spec
                      (Get_Gnatchop_File (Decl, Full_Path_Name));

Richard Kenner committed
1356
               Buffer : constant String :=
1357 1358 1359 1360 1361
                          F.all &
                          ":" & Get_Line (Decl)   &
                          ":" & Get_Column (Decl) &
                          ": ";

Richard Kenner committed
1362 1363 1364
               Num_Blanks : Integer := Longest_File_Name + 10 - Buffer'Length;

            begin
1365
               Free (F);
Richard Kenner committed
1366 1367 1368 1369 1370 1371
               Num_Blanks := Integer'Max (0, Num_Blanks);
               Write_Line
                 (Buffer & String'(1 .. Num_Blanks => ' ')
                  & "(spec) " & Get_Symbol (Decl));

               if Parent /= Empty_Declaration then
1372
                  F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
Richard Kenner committed
1373 1374 1375 1376
                  Write_Line
                    (Buffer & String'(1 .. Num_Blanks => ' ')
                     & "   derived from " & Get_Symbol (Parent)
                     & " ("
1377
                     & F.all
Richard Kenner committed
1378 1379
                     & ':' & Get_Line (Parent)
                     & ':' & Get_Column (Parent) & ')');
1380
                  Free (F);
Richard Kenner committed
1381 1382 1383 1384 1385 1386 1387 1388 1389
               end if;
            end;

            if Get_Source_Line (Decl)'Length /= 0 then
               Write_Line ("   " & Get_Source_Line (Decl));
            end if;

            --  Output the body (sorted)

1390 1391 1392 1393
            Arr := Get_References (Decl, Get_Bodies => True);

            for R in Arr'Range loop
               Print_Ref (Arr (R), "(body)");
Richard Kenner committed
1394 1395
            end loop;

1396 1397
            Free (Arr);

Richard Kenner committed
1398
            if References then
1399 1400 1401 1402 1403
               Arr := Get_References
                 (Decl, Get_Writes => True, Get_Reads => True);

               for R in Arr'Range loop
                  Print_Ref (Arr (R));
Richard Kenner committed
1404
               end loop;
1405 1406

               Free (Arr);
Richard Kenner committed
1407 1408 1409 1410 1411 1412 1413 1414 1415
            end if;
         end if;
      end loop;
   end Print_Gnatfind;

   ------------------
   -- Print_Unused --
   ------------------

1416
   procedure Print_Unused (Full_Path_Name : Boolean) is
1417 1418 1419 1420
      Decls : constant Declaration_Array_Access := Get_Declarations;
      Decl  : Declaration_Reference;
      Arr   : Reference_Array_Access;
      F     : String_Access;
Richard Kenner committed
1421 1422

   begin
1423 1424 1425 1426 1427
      for D in Decls'Range loop
         Decl := Decls (D);

         if References_Count
             (Decl, Get_Reads => True, Get_Writes => True) = 0
Richard Kenner committed
1428
         then
1429 1430
            F := Osint.To_Host_File_Spec
              (Get_Gnatchop_File (Decl, Full_Path_Name));
Richard Kenner committed
1431
            Write_Str (Get_Symbol (Decl)
1432 1433 1434 1435 1436 1437 1438 1439 1440
                        & " ("
                        & Get_Full_Type (Decl)
                        & ") "
                        & F.all
                        & ':'
                        & Get_Line (Decl)
                        & ':'
                        & Get_Column (Decl));
            Free (F);
Richard Kenner committed
1441 1442 1443

            --  Print the body if any

1444
            Arr := Get_References (Decl, Get_Bodies => True);
Richard Kenner committed
1445

1446 1447 1448 1449 1450 1451 1452 1453 1454
            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;
Richard Kenner committed
1455

1456 1457 1458
            Write_Eol;
            Free (Arr);
         end if;
Richard Kenner committed
1459 1460 1461 1462 1463 1464 1465
      end loop;
   end Print_Unused;

   --------------
   -- Print_Vi --
   --------------

1466
   procedure Print_Vi (Full_Path_Name : Boolean) is
1467 1468 1469 1470 1471 1472
      Tab   : constant Character := ASCII.HT;
      Decls : constant Declaration_Array_Access :=
                Get_Declarations (Sorted => False);
      Decl  : Declaration_Reference;
      Arr   : Reference_Array_Access;
      F     : String_Access;
Richard Kenner committed
1473 1474

   begin
1475 1476 1477 1478 1479 1480
      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);
Richard Kenner committed
1481 1482 1483

         --  Print the body if any

1484
         Arr := Get_References (Decl, Get_Bodies => True);
Richard Kenner committed
1485

1486 1487 1488 1489 1490 1491 1492 1493
         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);
Richard Kenner committed
1494 1495 1496

         --  Print the modifications

1497
         Arr := Get_References (Decl, Get_Writes => True, Get_Reads => True);
Richard Kenner committed
1498

1499 1500 1501 1502 1503
         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);
Richard Kenner committed
1504 1505
         end loop;

1506
         Free (Arr);
Richard Kenner committed
1507 1508 1509 1510 1511 1512 1513
      end loop;
   end Print_Vi;

   ----------------
   -- Print_Xref --
   ----------------

1514
   procedure Print_Xref (Full_Path_Name : Boolean) is
1515 1516
      Decls : constant Declaration_Array_Access := Get_Declarations;
      Decl : Declaration_Reference;
Richard Kenner committed
1517 1518 1519 1520 1521 1522 1523

      Margin : constant := 10;
      --  Column where file names start

      procedure New_Line80;
      --  Go to start of new line

1524
      procedure Print80 (S : String);
1525
      --  Print the text, respecting the 80 columns rule
Richard Kenner committed
1526 1527 1528 1529

      procedure Print_Ref (Line, Column : String);
      --  The beginning of the output is aligned on a column multiple of 9

1530 1531 1532 1533 1534 1535 1536 1537 1538
      procedure Print_List
        (Decl       : Declaration_Reference;
         Msg        : String;
         Get_Reads  : Boolean := False;
         Get_Writes : Boolean := False;
         Get_Bodies : Boolean := False);
      --  Print a list of references. If the list is not empty, Msg will
      --  be printed prior to the list.

Richard Kenner committed
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
      ----------------
      -- New_Line80 --
      ----------------

      procedure New_Line80 is
      begin
         Write_Eol;
         Write_Str (String'(1 .. Margin - 1 => ' '));
      end New_Line80;

      -------------
      -- Print80 --
      -------------

1553
      procedure Print80 (S : String) is
Richard Kenner committed
1554
         Align : Natural := Margin - (Integer (Column) mod Margin);
1555

Richard Kenner committed
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
      begin
         if Align = Margin then
            Align := 0;
         end if;

         Write_Str (String'(1 .. Align => ' ') & S);
      end Print80;

      ---------------
      -- Print_Ref --
      ---------------

      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;

1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623
      ----------------
      -- Print_List --
      ----------------

      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));
1624 1625 1626 1627 1628 1629 1630

               if F = null then
                  Write_Str ("<unknown> ");
               else
                  Write_Str (F.all & ' ');
                  Free (F);
               end if;
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640
            end if;

            Print_Ref (Get_Line (Arr (R)), Get_Column (Arr (R)));
         end loop;

         Free (Arr);
      end Print_List;

      F : String_Access;

Richard Kenner committed
1641 1642 1643
   --  Start of processing for Print_Xref

   begin
1644 1645 1646
      for D in Decls'Range loop
         Decl := Decls (D);

Richard Kenner committed
1647 1648
         Write_Str (Get_Symbol (Decl));

1649 1650 1651 1652 1653
         --  Put the declaration type in column Type_Position, but if the
         --  declaration name is too long, put at least one space between its
         --  name and its type.

         while Column < Type_Position - 1 loop
Richard Kenner committed
1654 1655 1656
            Write_Char (' ');
         end loop;

1657 1658
         Write_Char (' ');

1659
         Write_Line (Get_Full_Type (Decl));
Richard Kenner committed
1660 1661 1662

         Write_Parent_Info : declare
            Parent : constant Declaration_Reference := Get_Parent (Decl);
1663

Richard Kenner committed
1664 1665 1666
         begin
            if Parent /= Empty_Declaration then
               Write_Str ("  Ptype: ");
1667 1668 1669
               F := Osint.To_Host_File_Spec (Get_Gnatchop_File (Parent));
               Print80 (F.all);
               Free (F);
Richard Kenner committed
1670 1671 1672 1673 1674 1675 1676
               Print_Ref (Get_Line (Parent), Get_Column (Parent));
               Print80 ("  " & Get_Symbol (Parent));
               Write_Eol;
            end if;
         end Write_Parent_Info;

         Write_Str ("  Decl:  ");
1677 1678
         F := Osint.To_Host_File_Spec
               (Get_Gnatchop_File (Decl, Full_Path_Name));
1679 1680 1681 1682 1683 1684 1685 1686

         if F = null then
            Print80 ("<unknown> ");
         else
            Print80 (F.all & ' ');
            Free (F);
         end if;

Richard Kenner committed
1687 1688
         Print_Ref (Get_Line (Decl), Get_Column (Decl));

1689 1690 1691 1692 1693 1694
         Print_List
           (Decl, "  Body:  ", Get_Bodies => True);
         Print_List
           (Decl, "  Modi:  ", Get_Writes => True);
         Print_List
           (Decl, "  Ref:   ", Get_Reads => True);
Richard Kenner committed
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
         Write_Eol;
      end loop;
   end Print_Xref;

   ------------
   -- Search --
   ------------

   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);

1714 1715 1716 1717
      ALIfile   : ALI_File;
      File_Ref  : File_Reference;
      Strip_Num : Natural := 0;
      Ali_Name  : String_Access;
Richard Kenner committed
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743

   begin
      --  If we want all the .ali files, then find them

      if Wide_Search then
         Find_ALI_Files;
      end if;

      loop
         --  Get the next unread ali file

         File_Ref := Next_Unvisited_File;

         exit when File_Ref = Empty_File;

         --  Find the ALI file to use. Most of the time, it will be the unit
         --  name, with a different extension. However, when dealing with
         --  separates the ALI file is in fact the parent's ALI file (and this
         --  is recursive, in case the parent itself is a separate).

         Strip_Num := 0;
         loop
            Free (Ali_Name);
            Ali_Name := new String'
              (Get_File (File_Ref, With_Dir => True, Strip => Strip_Num));

1744 1745
            --  Stripped too many things...

Richard Kenner committed
1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
            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;

1757 1758 1759 1760 1761 1762
            --  If not found, try the parent's ALI file (this is needed for
            --  separate units and subprograms).

            --  Reset the cached directory first, in case the separate's
            --  ALI file is not in the same directory.

Richard Kenner committed
1763 1764
            elsif not File_Exists (Ali_Name.all) then
               Strip_Num := Strip_Num + 1;
1765 1766 1767
               Reset_Directory (File_Ref);

            --  Else we finally found it
Richard Kenner committed
1768 1769 1770 1771 1772 1773

            else
               exit;
            end if;
         end loop;

1774 1775 1776 1777 1778 1779 1780 1781 1782
         --  If we had to get the parent's ALI, insert it in the list as usual.
         --  This is to avoid parsing it twice in case it has already been
         --  parsed.

         if Ali_Name /= null and then Strip_Num /= 0 then
            File_Ref := Add_To_Xref_File
              (File_Name => Ali_Name.all,
               Visited   => False);

Richard Kenner committed
1783 1784 1785
         --  Now that we have a file name, parse it to find any reference to
         --  the entity.

1786
         elsif Ali_Name /= null
Richard Kenner committed
1787 1788 1789 1790
           and then (Read_Only or else Is_Writable_File (Ali_Name.all))
         then
            begin
               Open (Ali_Name.all, ALIfile);
Arnaud Charlet committed
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800

               --  The cross-reference section in the ALI file may be followed
               --  by other sections, which can be identified by the starting
               --  character of every line, which should neither be 'X' nor a
               --  figure in '1' .. '9'.

               --  The loop tests below also take into account the end-of-file
               --  possibility.

               while ALIfile.Buffer (ALIfile.Current_Line) = 'X' loop
Richard Kenner committed
1801
                  Parse_X_Filename (ALIfile);
Arnaud Charlet committed
1802 1803 1804 1805 1806 1807 1808

                  while ALIfile.Buffer (ALIfile.Current_Line) in '1' .. '9'
                  loop
                     Parse_Identifier_Info
                       (Pattern, ALIfile, Local_Symbols, Der_Info, Type_Tree,
                        Wide_Search, Labels_As_Ref => True);
                  end loop;
Richard Kenner committed
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
               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;

   -----------------
   -- Search_Xref --
   -----------------

   procedure Search_Xref
     (Local_Symbols : Boolean;
      Read_Only     : Boolean;
      Der_Info      : Boolean)
   is
1836 1837
      ALIfile      : ALI_File;
      File_Ref     : File_Reference;
Richard Kenner committed
1838
      Null_Pattern : Search_Pattern;
1839

Richard Kenner committed
1840
   begin
1841 1842
      Null_Pattern.Initialized := False;

Richard Kenner committed
1843 1844 1845 1846 1847 1848 1849 1850
      loop
         --  Find the next unvisited file

         File_Ref := Next_Unvisited_File;
         exit when File_Ref = Empty_File;

         --  Search the object directories for the .ali file

1851 1852 1853 1854 1855 1856
         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);
Richard Kenner committed
1857

Arnaud Charlet committed
1858 1859 1860 1861 1862 1863 1864 1865 1866
               --  The cross-reference section in the ALI file may be followed
               --  by other sections, which can be identified by the starting
               --  character of every line, which should neither be 'X' nor a
               --  figure in '1' .. '9'.

               --  The loop tests below also take into account the end-of-file
               --  possibility.

               while ALIfile.Buffer (ALIfile.Current_Line) = 'X' loop
Richard Kenner committed
1867
                  Parse_X_Filename (ALIfile);
Arnaud Charlet committed
1868 1869 1870 1871 1872 1873 1874

                  while ALIfile.Buffer (ALIfile.Current_Line) in '1' .. '9'
                  loop
                     Parse_Identifier_Info
                       (Null_Pattern, ALIfile, Local_Symbols, Der_Info,
                        Labels_As_Ref => False);
                  end loop;
Richard Kenner committed
1875
               end loop;
1876
            end if;
Richard Kenner committed
1877

1878 1879 1880
         exception
            when No_Xref_Information =>  null;
         end;
Richard Kenner committed
1881 1882 1883 1884
      end loop;
   end Search_Xref;

end Xref_Lib;