OSDN Git Service

2007-04-20 Arnaud Charlet <charlet@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / prj-makr.adb
index dd16d03..6606afb 100644 (file)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 2001-2004 Free Software Foundation, Inc.          --
+--          Copyright (C) 2001-2007, Free Software Foundation, Inc.         --
 --                                                                          --
 -- 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- --
@@ -16,8 +16,8 @@
 -- 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 --
 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
+-- Boston, MA 02110-1301, USA.                                              --
 --                                                                          --
 -- GNAT was originally developed  by the GNAT team at  New York University. --
 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
@@ -25,7 +25,6 @@
 ------------------------------------------------------------------------------
 
 with Csets;
-with Namet;    use Namet;
 with Opt;
 with Output;
 with Osint;    use Osint;
@@ -40,18 +39,30 @@ with Table;    use Table;
 
 with Ada.Characters.Handling;   use Ada.Characters.Handling;
 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
-with GNAT.OS_Lib;               use GNAT.OS_Lib;
-with GNAT.Regexp;               use GNAT.Regexp;
+
+with System.Case_Util;          use System.Case_Util;
+with System.CRTL;
+with System.Regexp;             use System.Regexp;
 
 package body Prj.Makr is
 
+   --  Packages of project files where unknown attributes are errors
+
+   --  All the following need comments ??? All global variables and
+   --  subprograms must be fully commented.
+
+   Naming_String : aliased String := "naming";
+
+   Gnatname_Packages : aliased String_List := (1 => Naming_String'Access);
+
+   Packages_To_Check_By_Gnatname : constant String_List_Access :=
+                                     Gnatname_Packages'Access;
+
    function Dup (Fd : File_Descriptor) return File_Descriptor;
-   pragma Import (C, Dup);
 
    procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
-   pragma Import (C, Dup2);
 
-   Gcc : constant String := "gcc";
+   Gcc      : constant String := "gcc";
    Gcc_Path : String_Access := null;
 
    Non_Empty_Node : constant Project_Node_Id := 1;
@@ -62,11 +73,11 @@ package body Prj.Makr is
    Naming_File_Suffix      : constant String := "_naming";
    Source_List_File_Suffix : constant String := "_source_list.txt";
 
-   Output_FD   : File_Descriptor;
-   --  To save the project file and its naming project file.
+   Output_FD : File_Descriptor;
+   --  To save the project file and its naming project file
 
    procedure Write_Eol;
-   --  Output an empty line.
+   --  Output an empty line
 
    procedure Write_A_Char (C : Character);
    --  Write one character to Output_FD
@@ -79,9 +90,29 @@ package body Prj.Makr is
       Table_Index_Type     => Natural,
       Table_Low_Bound      => 0,
       Table_Initial        => 10,
-      Table_Increment      => 10,
+      Table_Increment      => 100,
       Table_Name           => "Prj.Makr.Processed_Directories");
 
+   ---------
+   -- Dup --
+   ---------
+
+   function Dup  (Fd : File_Descriptor) return File_Descriptor is
+   begin
+      return File_Descriptor (System.CRTL.dup (Integer (Fd)));
+   end Dup;
+
+   ----------
+   -- Dup2 --
+   ----------
+
+   procedure Dup2 (Old_Fd, New_Fd : File_Descriptor) is
+      Fd : Integer;
+      pragma Warnings (Off, Fd);
+   begin
+      Fd := System.CRTL.dup2 (Integer (Old_Fd), Integer (New_Fd));
+   end Dup2;
+
    ----------
    -- Make --
    ----------
@@ -96,6 +127,8 @@ package body Prj.Makr is
       Preproc_Switches  : Argument_List;
       Very_Verbose      : Boolean)
    is
+      Tree : constant Project_Node_Tree_Ref := new Project_Node_Tree_Data;
+
       Path_Name : String (1 .. File_Path'Length +
                             Project_File_Extension'Length);
       Path_Last : Natural := File_Path'Length;
@@ -111,9 +144,14 @@ package body Prj.Makr is
       Source_Dirs_List    : Project_Node_Id := Empty_Node;
       Current_Source_Dir  : Project_Node_Id := Empty_Node;
 
-      Project_Naming_Node : Project_Node_Id := Empty_Node;
-      Project_Naming_Decl : Project_Node_Id := Empty_Node;
-      Naming_Package      : Project_Node_Id := Empty_Node;
+      Project_Naming_Node     : Project_Node_Id := Empty_Node;
+      Project_Naming_Decl     : Project_Node_Id := Empty_Node;
+      Naming_Package          : Project_Node_Id := Empty_Node;
+      Naming_Package_Comments : Project_Node_Id := Empty_Node;
+
+      Source_Files_Comments     : Project_Node_Id := Empty_Node;
+      Source_Dirs_Comments      : Project_Node_Id := Empty_Node;
+      Source_List_File_Comments : Project_Node_Id := Empty_Node;
 
       Project_Naming_File_Name : String (1 .. Output_Name'Length +
                                            Naming_File_Suffix'Length);
@@ -134,9 +172,10 @@ package body Prj.Makr is
       Args : Argument_List  (1 .. Preproc_Switches'Length + 6);
 
       type SFN_Pragma is record
-         Unit : String_Access;
-         File : String_Access;
-         Spec : Boolean;
+         Unit  : Name_Id;
+         File  : Name_Id;
+         Index : Int := 0;
+         Spec  : Boolean;
       end record;
 
       package SFN_Pragmas is new Table.Table
@@ -144,7 +183,7 @@ package body Prj.Makr is
          Table_Index_Type     => Natural,
          Table_Low_Bound      => 0,
          Table_Initial        => 50,
-         Table_Increment      => 50,
+         Table_Increment      => 100,
          Table_Name           => "Prj.Makr.SFN_Pragmas");
 
       procedure Process_Directory (Dir_Name : String; Recursively : Boolean);
@@ -157,16 +196,20 @@ package body Prj.Makr is
       -----------------------
 
       procedure Process_Directory (Dir_Name : String; Recursively : Boolean) is
-         Matched  : Matched_Type := False;
-         Str      : String (1 .. 2_000);
-         Last     : Natural;
-         Dir      : Dir_Type;
-         Process  : Boolean := True;
-
-         Temp_File_Name : String_Access := null;
+         Matched : Matched_Type := False;
+         Str     : String (1 .. 2_000);
+         Canon   : String (1 .. 2_000);
+         Last    : Natural;
+         Dir     : Dir_Type;
+         Process : Boolean := True;
+
+         Temp_File_Name         : String_Access := null;
+         Save_Last_Pragma_Index : Natural := 0;
+         File_Name_Id           : Name_Id := No_Name;
+         SFN_Prag               : SFN_Pragma;
 
       begin
-         --  Avoid processing several times the same directory.
+         --  Avoid processing the same directory more than once
 
          for Index in 1 .. Processed_Directories.Last loop
             if Processed_Directories.Table (Index).all = Dir_Name then
@@ -186,12 +229,11 @@ package body Prj.Makr is
             Processed_Directories.Table (Processed_Directories.Last) :=
               new String'(Dir_Name);
 
-            --  Get the source file names from the directory.
-            --  Fails if the directory does not exist.
+            --  Get the source file names from the directory. Fails if the
+            --  directory does not exist.
 
             begin
                Open (Dir, Dir_Name);
-
             exception
                when Directory_Error =>
                   Prj.Com.Fail ("cannot open directory """, Dir_Name, """");
@@ -199,21 +241,32 @@ package body Prj.Makr is
 
             --  Process each regular file in the directory
 
-            loop
+            File_Loop : loop
                Read (Dir, Str, Last);
-               exit when Last = 0;
+               exit File_Loop when Last = 0;
+
+               --  Copy the file name and put it in canonical case to match
+               --  against the patterns that have themselves already been put
+               --  in canonical case.
+
+               Canon (1 .. Last) := Str (1 .. Last);
+               Canonical_Case_File_Name (Canon (1 .. Last));
 
                if Is_Regular_File
                  (Dir_Name & Directory_Separator & Str (1 .. Last))
                then
                   Matched := True;
 
+                  Name_Len := Last;
+                  Name_Buffer (1 .. Name_Len) := Str (1 .. Last);
+                  File_Name_Id := Name_Find;
+
                   --  First, check if the file name matches at least one of
                   --  the excluded expressions;
 
                   for Index in Excluded_Expressions'Range loop
                      if
-                       Match (Str (1 .. Last), Excluded_Expressions (Index))
+                       Match (Canon (1 .. Last), Excluded_Expressions (Index))
                      then
                         Matched := Excluded;
                         exit;
@@ -229,7 +282,8 @@ package body Prj.Makr is
 
                      for Index in Regular_Expressions'Range loop
                         if
-                          Match (Str (1 .. Last), Regular_Expressions (Index))
+                          Match
+                            (Canon (1 .. Last), Regular_Expressions (Index))
                         then
                            Matched := True;
                            exit;
@@ -242,7 +296,7 @@ package body Prj.Makr is
                   then
                      Output.Write_Str ("   Checking """);
                      Output.Write_Str (Str (1 .. Last));
-                     Output.Write_Str (""": ");
+                     Output.Write_Line (""": ");
                   end if;
 
                   --  If the file name matches one of the regular expressions,
@@ -256,11 +310,19 @@ package body Prj.Makr is
                         Saved_Error  : File_Descriptor;
 
                      begin
-                        --  If we don't have yet the path of the compiler,
-                        --  get it now.
+                        --  If we don't have the path of the compiler yet,
+                        --  get it now. The compiler name may have a prefix,
+                        --  so we get the potentially prefixed name.
 
                         if Gcc_Path = null then
-                           Gcc_Path := Locate_Exec_On_Path (Gcc);
+                           declare
+                              Prefix_Gcc : String_Access :=
+                                             Program_Name (Gcc);
+                           begin
+                              Gcc_Path :=
+                                Locate_Exec_On_Path (Prefix_Gcc.all);
+                              Free (Prefix_Gcc);
+                           end;
 
                            if Gcc_Path = null then
                               Prj.Com.Fail ("could not locate " & Gcc);
@@ -302,8 +364,7 @@ package body Prj.Makr is
                         Saved_Output := Dup (Standout);
                         Saved_Error  := Dup (Standerr);
 
-                        --  Set the standard output and error to the temporary
-                        --  file.
+                        --  Set standard output and error to the temporary file
 
                         Dup2 (FD, Standout);
                         Dup2 (FD, Standerr);
@@ -313,6 +374,7 @@ package body Prj.Makr is
                         Spawn (Gcc_Path.all, Args, Success);
 
                         --  Restore the standard output and error
+
                         Dup2 (Saved_Output, Standout);
                         Dup2 (Saved_Error, Standerr);
 
@@ -329,11 +391,11 @@ package body Prj.Makr is
                         --  Now that standard output is restored, check if
                         --  the compiler ran correctly.
 
-                        --  Read the first line of the temporary file:
-                        --  it should contain the kind and name of the unit.
+                        --  Read the lines of the temporary file:
+                        --  they should contain the kind and name of the unit.
 
                         declare
-                           File : Text_File;
+                           File      : Text_File;
                            Text_Line : String (1 .. 1_000);
                            Text_Last : Natural;
 
@@ -345,175 +407,199 @@ package body Prj.Makr is
                                 ("could not read temporary file");
                            end if;
 
+                           Save_Last_Pragma_Index := SFN_Pragmas.Last;
+
                            if End_Of_File (File) then
                               if Opt.Verbose_Mode then
                                  if not Success then
-                                    Output.Write_Str ("(process died) ");
+                                    Output.Write_Str ("      (process died) ");
                                  end if;
-
-                                 Output.Write_Line ("not a unit");
                               end if;
 
                            else
-                              Get_Line (File, Text_Line, Text_Last);
-                              Close (File);
+                              Line_Loop : while not End_Of_File (File) loop
+                                 Get_Line (File, Text_Line, Text_Last);
+
+                                 --  Find the first closing parenthesis
+
+                                 Char_Loop : for J in 1 .. Text_Last loop
+                                    if Text_Line (J) = ')' then
+                                       if J >= 13 and then
+                                         Text_Line (1 .. 4) = "Unit"
+                                       then
+                                          --  Add entry to SFN_Pragmas table
 
-                              --  Now that we have read the line, delete the
-                              --  temporary file, it is not needed anymore.
-                              --  On VMS, this avoids several version of the
-                              --  file, if it were only delete after all
-                              --  sources were parsed.
+                                          Name_Len := J - 12;
+                                          Name_Buffer (1 .. Name_Len) :=
+                                            Text_Line (6 .. J - 7);
+                                          SFN_Prag :=
+                                            (Unit  => Name_Find,
+                                             File  => File_Name_Id,
+                                             Index => 0,
+                                             Spec  => Text_Line (J - 5 .. J) =
+                                                        "(spec)");
+
+                                          SFN_Pragmas.Increment_Last;
+                                          SFN_Pragmas.Table
+                                            (SFN_Pragmas.Last) := SFN_Prag;
+                                       end if;
+                                       exit Char_Loop;
+                                    end if;
+                                 end loop Char_Loop;
+                              end loop Line_Loop;
+                           end if;
 
-                              Delete_File (Temp_File_Name.all, Success);
+                           if Save_Last_Pragma_Index = SFN_Pragmas.Last then
+                              if Opt.Verbose_Mode then
+                                 Output.Write_Line ("      not a unit");
+                              end if;
 
-                              --  Find the first closing parenthesis
+                           else
+                              if SFN_Pragmas.Last >
+                                   Save_Last_Pragma_Index + 1
+                              then
+                                 for Index in Save_Last_Pragma_Index + 1 ..
+                                                SFN_Pragmas.Last
+                                 loop
+                                    SFN_Pragmas.Table (Index).Index :=
+                                      Int (Index - Save_Last_Pragma_Index);
+                                 end loop;
+                              end if;
 
-                              for J in 1 .. Text_Last loop
-                                 if Text_Line (J) = ')' then
-                                    Text_Last := J;
-                                    exit;
-                                 end if;
-                              end loop;
+                              for Index in Save_Last_Pragma_Index + 1 ..
+                                             SFN_Pragmas.Last
+                              loop
+                                 SFN_Prag := SFN_Pragmas.Table (Index);
 
-                              declare
-                                 S : constant String :=
-                                       Text_Line (1 .. Text_Last);
+                                 if Opt.Verbose_Mode then
+                                    if SFN_Prag.Spec then
+                                       Output.Write_Str ("      spec of ");
 
-                              begin
-                                 if S'Length >= 13
-                                   and then S (S'First .. S'First + 3) = "Unit"
-                                 then
-                                    if Opt.Verbose_Mode then
-                                       Output.Write_Str
-                                         (S (S'Last - 4 .. S'Last - 1));
-                                       Output.Write_Str (" of ");
-                                       Output.Write_Line
-                                         (S (S'First + 5 .. S'Last - 7));
+                                    else
+                                       Output.Write_Str ("      body of ");
                                     end if;
 
-                                    if Project_File then
-
-                                       --  Add the corresponding attribute in
-                                       --  the Naming package of the naming
-                                       --  project.
-
-                                       declare
-                                          Decl_Item : constant Project_Node_Id
-                                            := Default_Project_Node
-                                              (Of_Kind =>
-                                                   N_Declarative_Item);
-
-                                          Attribute : constant Project_Node_Id
-                                            := Default_Project_Node
-                                              (Of_Kind =>
-                                                   N_Attribute_Declaration);
-
-                                          Expression : constant Project_Node_Id
-                                            := Default_Project_Node
-                                              (Of_Kind => N_Expression,
-                                               And_Expr_Kind => Single);
-
-                                          Term : constant Project_Node_Id :=
-                                                   Default_Project_Node
-                                                     (Of_Kind => N_Term,
-                                                      And_Expr_Kind => Single);
-
-                                          Value : constant Project_Node_Id :=
-                                                    Default_Project_Node
-                                                      (Of_Kind =>
-                                                         N_Literal_String,
-                                                       And_Expr_Kind =>
-                                                         Single);
-
-                                       begin
-                                          Set_Next_Declarative_Item
-                                            (Decl_Item,
-                                             To => First_Declarative_Item_Of
-                                               (Naming_Package));
-                                          Set_First_Declarative_Item_Of
-                                            (Naming_Package, To => Decl_Item);
-                                          Set_Current_Item_Node
-                                            (Decl_Item, To => Attribute);
-
-                                          --  Is it a spec or a body?
-
-                                          if S (S'Last - 5 .. S'Last) =
-                                            "(spec)"
-                                          then
-                                             Set_Name_Of
-                                               (Attribute, To => Name_Spec);
-                                          else
-                                             Set_Name_Of
-                                               (Attribute,
-                                                To => Name_Body);
-                                          end if;
-
-                                          --  Get the name of the unit
-
-                                          Name_Len := S'Last - S'First - 11;
-                                          Name_Buffer (1 .. Name_Len) :=
-                                            (To_Lower
-                                               (S (S'First + 5 ..
-                                                     S'Last - 7)));
-                                          Set_Associative_Array_Index_Of
-                                            (Attribute, To => Name_Find);
+                                    Output.Write_Line
+                                      (Get_Name_String (SFN_Prag.Unit));
+                                 end if;
 
-                                          Set_Expression_Of
-                                            (Attribute, To => Expression);
-                                          Set_First_Term
-                                            (Expression, To => Term);
-                                          Set_Current_Term (Term, To => Value);
+                                 if Project_File then
+
+                                    --  Add the corresponding attribute in the
+                                    --  Naming package of the naming project.
+
+                                    declare
+                                       Decl_Item : constant Project_Node_Id :=
+                                         Default_Project_Node
+                                           (Of_Kind =>
+                                                N_Declarative_Item,
+                                            In_Tree => Tree);
+
+                                       Attribute : constant Project_Node_Id :=
+                                         Default_Project_Node
+                                           (Of_Kind =>
+                                                N_Attribute_Declaration,
+                                            In_Tree => Tree);
+
+                                       Expression : constant Project_Node_Id :=
+                                         Default_Project_Node
+                                           (Of_Kind => N_Expression,
+                                            And_Expr_Kind => Single,
+                                            In_Tree => Tree);
+
+                                       Term : constant Project_Node_Id :=
+                                         Default_Project_Node
+                                           (Of_Kind => N_Term,
+                                            And_Expr_Kind => Single,
+                                            In_Tree => Tree);
+
+                                       Value : constant Project_Node_Id :=
+                                         Default_Project_Node
+                                           (Of_Kind       => N_Literal_String,
+                                            And_Expr_Kind => Single,
+                                            In_Tree       => Tree);
+
+                                    begin
+                                       Set_Next_Declarative_Item
+                                         (Decl_Item,
+                                          To => First_Declarative_Item_Of
+                                            (Naming_Package, Tree),
+                                          In_Tree => Tree);
+                                       Set_First_Declarative_Item_Of
+                                         (Naming_Package,
+                                          To => Decl_Item,
+                                          In_Tree => Tree);
+                                       Set_Current_Item_Node
+                                         (Decl_Item,
+                                          To => Attribute,
+                                          In_Tree => Tree);
+
+                                       --  Is it a spec or a body?
+
+                                       if SFN_Prag.Spec then
+                                          Set_Name_Of
+                                            (Attribute, Tree,
+                                             To => Name_Spec);
+                                       else
+                                          Set_Name_Of
+                                            (Attribute, Tree,
+                                             To => Name_Body);
+                                       end if;
 
-                                          --  And set the name of the file
+                                       --  Get the name of the unit
 
-                                          Name_Len := Last;
-                                          Name_Buffer (1 .. Name_Len) :=
-                                            Str (1 .. Last);
-                                          Set_String_Value_Of
-                                            (Value, To => Name_Find);
-                                       end;
+                                       Get_Name_String (SFN_Prag.Unit);
+                                       To_Lower (Name_Buffer (1 .. Name_Len));
+                                       Set_Associative_Array_Index_Of
+                                         (Attribute, Tree, To => Name_Find);
 
-                                       --  Add source file name to source list
-                                       --  file.
+                                       Set_Expression_Of
+                                         (Attribute, Tree, To => Expression);
+                                       Set_First_Term
+                                         (Expression, Tree, To => Term);
+                                       Set_Current_Term
+                                         (Term, Tree, To => Value);
 
-                                       Last := Last + 1;
-                                       Str (Last) := ASCII.LF;
+                                       --  And set the name of the file
 
-                                       if Write (Source_List_FD,
-                                                 Str (1)'Address,
-                                                 Last) /= Last
-                                       then
-                                          Prj.Com.Fail ("disk full");
-                                       end if;
-                                    else
-                                       --  Add an entry in the SFN_Pragmas
-                                       --  table.
-
-                                       SFN_Pragmas.Increment_Last;
-                                       SFN_Pragmas.Table (SFN_Pragmas.Last) :=
-                                         (Unit => new String'
-                                            (S (S'First + 5 .. S'Last - 7)),
-                                          File => new String'(Str (1 .. Last)),
-                                          Spec => S (S'Last - 5 .. S'Last)
-                                          = "(spec)");
-                                    end if;
+                                       Set_String_Value_Of
+                                         (Value, Tree, To => File_Name_Id);
+                                       Set_Source_Index_Of
+                                         (Value, Tree, To => SFN_Prag.Index);
+                                    end;
+                                 end if;
+                              end loop;
 
-                                 else
-                                    if Opt.Verbose_Mode then
-                                       Output.Write_Line ("not a unit");
-                                    end if;
+                              if Project_File then
+                                 --  Add source file name to source list
+                                 --  file.
+
+                                 Last := Last + 1;
+                                 Str (Last) := ASCII.LF;
+
+                                 if Write (Source_List_FD,
+                                           Str (1)'Address,
+                                           Last) /= Last
+                                 then
+                                    Prj.Com.Fail ("disk full");
                                  end if;
-                              end;
+                              end if;
                            end if;
+
+                           Close (File);
+
+                           Delete_File (Temp_File_Name.all, Success);
                         end;
                      end;
 
+                  --  File name matches none of the regular expressions
+
                   else
-                     if Matched = False then
-                        --  Look if this is a foreign source
+                     --  If file is not excluded, see if this is foreign source
 
+                     if Matched /= Excluded then
                         for Index in Foreign_Expressions'Range loop
-                           if Match (Str (1 .. Last),
+                           if Match (Canon (1 .. Last),
                                      Foreign_Expressions (Index))
                            then
                               Matched := True;
@@ -551,7 +637,7 @@ package body Prj.Makr is
                      end if;
                   end if;
                end if;
-            end loop;
+            end loop File_Loop;
 
             Close (Dir);
          end if;
@@ -592,7 +678,8 @@ package body Prj.Makr is
       Csets.Initialize;
       Namet.Initialize;
       Snames.Initialize;
-      Prj.Initialize;
+      Prj.Initialize (No_Project_Tree);
+      Prj.Tree.Initialize (Tree);
 
       SFN_Pragmas.Set_Last (0);
 
@@ -639,6 +726,139 @@ package body Prj.Makr is
          Output_Name (1 .. Path_Last) := To_Lower (Path_Name (1 .. Path_Last));
          Output_Name_Last := Path_Last - Project_File_Extension'Length;
 
+         --  If there is already a project file with the specified name, parse
+         --  it to get the components that are not automatically generated.
+
+         if Is_Regular_File (Output_Name (1 .. Path_Last)) then
+            if Opt.Verbose_Mode then
+               Output.Write_Str ("Parsing already existing project file """);
+               Output.Write_Str (Output_Name (1 .. Output_Name_Last));
+               Output.Write_Line ("""");
+            end if;
+
+            Part.Parse
+              (In_Tree                => Tree,
+               Project                => Project_Node,
+               Project_File_Name      => Output_Name (1 .. Output_Name_Last),
+               Always_Errout_Finalize => False,
+               Store_Comments         => True,
+               Packages_To_Check      => Packages_To_Check_By_Gnatname);
+
+            --  Fail if parsing was not successful
+
+            if Project_Node = Empty_Node then
+               Fail ("parsing of existing project file failed");
+
+            else
+               --  If parsing was successful, remove the components that are
+               --  automatically generated, if any, so that they will be
+               --  unconditionally added later.
+
+               --  Remove the with clause for the naming project file
+
+               declare
+                  With_Clause : Project_Node_Id :=
+                                  First_With_Clause_Of (Project_Node, Tree);
+                  Previous    : Project_Node_Id := Empty_Node;
+
+               begin
+                  while With_Clause /= Empty_Node loop
+                     if Prj.Tree.Name_Of (With_Clause, Tree) =
+                          Project_Naming_Id
+                     then
+                        if Previous = Empty_Node then
+                           Set_First_With_Clause_Of
+                             (Project_Node, Tree,
+                              To => Next_With_Clause_Of (With_Clause, Tree));
+                        else
+                           Set_Next_With_Clause_Of
+                             (Previous, Tree,
+                              To => Next_With_Clause_Of (With_Clause, Tree));
+                        end if;
+
+                        exit;
+                     end if;
+
+                     Previous := With_Clause;
+                     With_Clause := Next_With_Clause_Of (With_Clause, Tree);
+                  end loop;
+               end;
+
+               --  Remove attribute declarations of Source_Files,
+               --  Source_List_File, Source_Dirs, and the declaration of
+               --  package Naming, if they exist, but preserve the comments
+               --  attached to these nodes.
+
+               declare
+                  Declaration  : Project_Node_Id :=
+                                   First_Declarative_Item_Of
+                                     (Project_Declaration_Of
+                                        (Project_Node, Tree),
+                                      Tree);
+                  Previous     : Project_Node_Id := Empty_Node;
+                  Current_Node : Project_Node_Id := Empty_Node;
+
+                  Name         : Name_Id;
+                  Kind_Of_Node : Project_Node_Kind;
+                  Comments     : Project_Node_Id;
+
+               begin
+                  while Declaration /= Empty_Node loop
+                     Current_Node := Current_Item_Node (Declaration, Tree);
+
+                     Kind_Of_Node := Kind_Of (Current_Node, Tree);
+
+                     if Kind_Of_Node = N_Attribute_Declaration or else
+                       Kind_Of_Node = N_Package_Declaration
+                     then
+                        Name := Prj.Tree.Name_Of (Current_Node, Tree);
+
+                        if Name = Name_Source_Files     or else
+                           Name = Name_Source_List_File or else
+                           Name = Name_Source_Dirs      or else
+                           Name = Name_Naming
+                        then
+                           Comments :=
+                             Tree.Project_Nodes.Table (Current_Node).Comments;
+
+                           if Name = Name_Source_Files then
+                              Source_Files_Comments := Comments;
+
+                           elsif Name = Name_Source_List_File then
+                              Source_List_File_Comments := Comments;
+
+                           elsif Name = Name_Source_Dirs then
+                              Source_Dirs_Comments := Comments;
+
+                           elsif Name = Name_Naming then
+                              Naming_Package_Comments := Comments;
+                           end if;
+
+                           if Previous = Empty_Node then
+                              Set_First_Declarative_Item_Of
+                                (Project_Declaration_Of (Project_Node, Tree),
+                                 Tree,
+                                 To => Next_Declarative_Item
+                                         (Declaration, Tree));
+
+                           else
+                              Set_Next_Declarative_Item
+                                (Previous, Tree,
+                                 To => Next_Declarative_Item
+                                         (Declaration, Tree));
+                           end if;
+
+                        else
+                           Previous := Declaration;
+                        end if;
+                     end if;
+
+                     Declaration := Next_Declarative_Item (Declaration, Tree);
+                  end loop;
+               end;
+            end if;
+         end if;
+
          if Directory_Last /= 0 then
             Output_Name (1 .. Output_Name_Last - Directory_Last) :=
               Output_Name (Directory_Last + 1 .. Output_Name_Last);
@@ -718,7 +938,6 @@ package body Prj.Makr is
 
          declare
             Discard : Boolean;
-
          begin
             Delete_File
               (Source_List_Path (1 .. Source_List_Last),
@@ -753,7 +972,6 @@ package body Prj.Makr is
          begin
             Excluded_Expressions (Index) :=
               Compile (Pattern => Excluded_Patterns (Index).all, Glob => True);
-
          exception
             when Error_In_Regexp =>
                Prj.Com.Fail
@@ -773,7 +991,6 @@ package body Prj.Makr is
          begin
             Foreign_Expressions (Index) :=
               Compile (Pattern => Foreign_Patterns (Index).all, Glob => True);
-
          exception
             when Error_In_Regexp =>
                Prj.Com.Fail
@@ -811,114 +1028,18 @@ package body Prj.Makr is
             Output.Write_Line ("""");
          end if;
 
-         --  If there is already a project file with the specified name,
-         --  parse it to get the components that are not automatically
-         --  generated.
-
-         if Is_Regular_File (Output_Name (1 .. Output_Name_Last)) then
-            if Opt.Verbose_Mode then
-               Output.Write_Str ("Parsing already existing project file """);
-               Output.Write_Str (Output_Name (1 .. Output_Name_Last));
-               Output.Write_Line ("""");
-            end if;
-
-            Part.Parse
-              (Project           => Project_Node,
-               Project_File_Name => Output_Name (1 .. Output_Name_Last),
-               Always_Errout_Finalize => False);
-
-            --  If parsing was successful, remove the components that are
-            --  automatically generated, if any, so that they will be
-            --  unconditionally added later.
-
-            if Project_Node /= Empty_Node then
-
-               --  Remove the with clause for the naming project file
-
-               declare
-                  With_Clause : Project_Node_Id :=
-                    First_With_Clause_Of (Project_Node);
-                  Previous    : Project_Node_Id := Empty_Node;
-
-               begin
-                  while With_Clause /= Empty_Node loop
-                     if Tree.Name_Of (With_Clause) = Project_Naming_Id then
-                        if Previous = Empty_Node then
-                           Set_First_With_Clause_Of
-                             (Project_Node,
-                              To => Next_With_Clause_Of (With_Clause));
-                        else
-                           Set_Next_With_Clause_Of
-                             (Previous,
-                              To => Next_With_Clause_Of (With_Clause));
-                        end if;
-
-                        exit;
-                     end if;
-
-                     Previous := With_Clause;
-                     With_Clause := Next_With_Clause_Of (With_Clause);
-                  end loop;
-               end;
-
-               --  Remove attribute declarations of Source_Files,
-               --  Source_List_File, Source_Dirs, and the declaration of
-               --  package Naming, if they exist.
-
-               declare
-                  Declaration  : Project_Node_Id :=
-                                   First_Declarative_Item_Of
-                                     (Project_Declaration_Of
-                                       (Project_Node));
-                  Previous     : Project_Node_Id := Empty_Node;
-                  Current_Node : Project_Node_Id := Empty_Node;
-
-               begin
-                  while Declaration /= Empty_Node loop
-                     Current_Node := Current_Item_Node (Declaration);
-
-                     if (Kind_Of (Current_Node) = N_Attribute_Declaration
-                           and then
-                           (Tree.Name_Of (Current_Node) = Name_Source_Files
-                             or else Tree.Name_Of (Current_Node) =
-                                               Name_Source_List_File
-                              or else Tree.Name_Of (Current_Node) =
-                              Name_Source_Dirs))
-                       or else
-                       (Kind_Of (Current_Node) = N_Package_Declaration
-                          and then Tree.Name_Of (Current_Node) = Name_Naming)
-                     then
-                        if Previous = Empty_Node then
-                           Set_First_Declarative_Item_Of
-                             (Project_Declaration_Of (Project_Node),
-                              To => Next_Declarative_Item (Declaration));
-
-                        else
-                           Set_Next_Declarative_Item
-                             (Previous,
-                              To => Next_Declarative_Item (Declaration));
-                        end if;
-
-                     else
-                        Previous := Declaration;
-                     end if;
-
-                     Declaration := Next_Declarative_Item (Declaration);
-                  end loop;
-               end;
-            end if;
-         end if;
-
          --  If there were no already existing project file, or if the parsing
          --  was unsuccessful, create an empty project node with the correct
          --  name and its project declaration node.
 
          if Project_Node = Empty_Node then
-            Project_Node := Default_Project_Node (Of_Kind => N_Project);
-            Set_Name_Of (Project_Node, To => Output_Name_Id);
+            Project_Node :=
+              Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
+            Set_Name_Of (Project_Node, Tree, To => Output_Name_Id);
             Set_Project_Declaration_Of
-              (Project_Node,
-               To => Default_Project_Node (Of_Kind => N_Project_Declaration));
+              (Project_Node, Tree,
+               To => Default_Project_Node
+                 (Of_Kind => N_Project_Declaration, In_Tree => Tree));
 
          end if;
 
@@ -926,93 +1047,126 @@ package body Prj.Makr is
          --  for Source_Files as an empty list, to indicate there are no
          --  sources in the naming project.
 
-         Project_Naming_Node := Default_Project_Node (Of_Kind => N_Project);
-         Set_Name_Of (Project_Naming_Node, To => Project_Naming_Id);
+         Project_Naming_Node :=
+           Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
+         Set_Name_Of (Project_Naming_Node, Tree, To => Project_Naming_Id);
          Project_Naming_Decl :=
-           Default_Project_Node (Of_Kind => N_Project_Declaration);
-         Set_Project_Declaration_Of (Project_Naming_Node, Project_Naming_Decl);
+           Default_Project_Node
+             (Of_Kind => N_Project_Declaration, In_Tree => Tree);
+         Set_Project_Declaration_Of
+           (Project_Naming_Node, Tree, Project_Naming_Decl);
          Naming_Package :=
-           Default_Project_Node (Of_Kind => N_Package_Declaration);
-         Set_Name_Of (Naming_Package, To => Name_Naming);
+           Default_Project_Node
+             (Of_Kind => N_Package_Declaration, In_Tree => Tree);
+         Set_Name_Of (Naming_Package, Tree, To => Name_Naming);
 
          declare
             Decl_Item : constant Project_Node_Id :=
-              Default_Project_Node (Of_Kind => N_Declarative_Item);
+                          Default_Project_Node
+                            (Of_Kind => N_Declarative_Item, In_Tree => Tree);
 
             Attribute : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Attribute_Declaration,
-               And_Expr_Kind => List);
+                          Default_Project_Node
+                            (Of_Kind       => N_Attribute_Declaration,
+                             In_Tree       => Tree,
+                             And_Expr_Kind => List);
 
             Expression : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Expression,
-               And_Expr_Kind => List);
+                           Default_Project_Node
+                             (Of_Kind       => N_Expression,
+                              In_Tree       => Tree,
+                              And_Expr_Kind => List);
 
-            Term  : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Term,
-               And_Expr_Kind => List);
+            Term      : constant Project_Node_Id :=
+                          Default_Project_Node
+                            (Of_Kind       => N_Term,
+                             In_Tree       => Tree,
+                             And_Expr_Kind => List);
 
             Empty_List : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Literal_String_List);
+                           Default_Project_Node
+                             (Of_Kind => N_Literal_String_List,
+                              In_Tree => Tree);
 
          begin
             Set_First_Declarative_Item_Of
-              (Project_Naming_Decl, To => Decl_Item);
-            Set_Next_Declarative_Item (Decl_Item, Naming_Package);
-            Set_Current_Item_Node (Decl_Item, To => Attribute);
-            Set_Name_Of (Attribute, To => Name_Source_Files);
-            Set_Expression_Of (Attribute, To => Expression);
-            Set_First_Term (Expression, To => Term);
-            Set_Current_Term (Term, To => Empty_List);
+              (Project_Naming_Decl, Tree, To => Decl_Item);
+            Set_Next_Declarative_Item (Decl_Item, Tree, Naming_Package);
+            Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
+            Set_Name_Of (Attribute, Tree, To => Name_Source_Files);
+            Set_Expression_Of (Attribute, Tree, To => Expression);
+            Set_First_Term (Expression, Tree, To => Term);
+            Set_Current_Term (Term, Tree, To => Empty_List);
          end;
 
-         --  Add a with clause on the naming project in the main project
+         --  Add a with clause on the naming project in the main project, if
+         --  there is not already one.
 
          declare
-            With_Clause : constant Project_Node_Id :=
-              Default_Project_Node (Of_Kind => N_With_Clause);
+            With_Clause : Project_Node_Id :=
+                                  First_With_Clause_Of (Project_Node, Tree);
 
          begin
-            Set_Next_With_Clause_Of
-              (With_Clause, To => First_With_Clause_Of (Project_Node));
-            Set_First_With_Clause_Of (Project_Node, To => With_Clause);
-            Set_Name_Of (With_Clause, To => Project_Naming_Id);
-
-            --  We set the project node to something different than
-            --  Empty_Node, so that Prj.PP does not generate a limited
-            --  with clause.
-
-            Set_Project_Node_Of (With_Clause, Non_Empty_Node);
+            while With_Clause /= Empty_Node loop
+               exit when
+                 Prj.Tree.Name_Of (With_Clause, Tree) = Project_Naming_Id;
+               With_Clause := Next_With_Clause_Of (With_Clause, Tree);
+            end loop;
 
-            Name_Len := Project_Naming_Last;
-            Name_Buffer (1 .. Name_Len) :=
-              Project_Naming_File_Name (1 .. Project_Naming_Last);
-            Set_String_Value_Of (With_Clause, To => Name_Find);
+            if With_Clause = Empty_Node then
+               With_Clause := Default_Project_Node
+                 (Of_Kind => N_With_Clause, In_Tree => Tree);
+               Set_Next_With_Clause_Of
+                 (With_Clause, Tree,
+                  To => First_With_Clause_Of (Project_Node, Tree));
+               Set_First_With_Clause_Of
+                 (Project_Node, Tree, To => With_Clause);
+               Set_Name_Of (With_Clause, Tree, To => Project_Naming_Id);
+
+               --  We set the project node to something different than
+               --  Empty_Node, so that Prj.PP does not generate a limited
+               --  with clause.
+
+               Set_Project_Node_Of (With_Clause, Tree, Non_Empty_Node);
+
+               Name_Len := Project_Naming_Last;
+               Name_Buffer (1 .. Name_Len) :=
+                 Project_Naming_File_Name (1 .. Project_Naming_Last);
+               Set_String_Value_Of (With_Clause, Tree, To => Name_Find);
+            end if;
          end;
 
-         Project_Declaration := Project_Declaration_Of (Project_Node);
+         Project_Declaration := Project_Declaration_Of (Project_Node, Tree);
 
          --  Add a renaming declaration for package Naming in the main project
 
          declare
             Decl_Item  : constant Project_Node_Id :=
-              Default_Project_Node (Of_Kind => N_Declarative_Item);
+                           Default_Project_Node
+                             (Of_Kind => N_Declarative_Item,
+                              In_Tree => Tree);
 
             Naming : constant Project_Node_Id :=
-              Default_Project_Node (Of_Kind => N_Package_Declaration);
+                           Default_Project_Node
+                             (Of_Kind => N_Package_Declaration,
+                              In_Tree => Tree);
+
          begin
             Set_Next_Declarative_Item
-              (Decl_Item,
-               To => First_Declarative_Item_Of (Project_Declaration));
+              (Decl_Item, Tree,
+               To => First_Declarative_Item_Of (Project_Declaration, Tree));
             Set_First_Declarative_Item_Of
-              (Project_Declaration, To => Decl_Item);
-            Set_Current_Item_Node (Decl_Item, To => Naming);
-            Set_Name_Of (Naming, To => Name_Naming);
+              (Project_Declaration, Tree, To => Decl_Item);
+            Set_Current_Item_Node (Decl_Item, Tree, To => Naming);
+            Set_Name_Of (Naming, Tree, To => Name_Naming);
             Set_Project_Of_Renamed_Package_Of
-              (Naming, To => Project_Naming_Node);
+              (Naming, Tree, To => Project_Naming_Node);
+
+            --  Attach the comments, if any, that were saved for package
+            --  Naming.
+
+            Tree.Project_Nodes.Table (Naming).Comments :=
+              Naming_Package_Comments;
          end;
 
          --  Add an attribute declaration for Source_Dirs, initialized as an
@@ -1021,36 +1175,49 @@ package body Prj.Makr is
 
          declare
             Decl_Item  : constant Project_Node_Id :=
-              Default_Project_Node (Of_Kind => N_Declarative_Item);
+                           Default_Project_Node
+                             (Of_Kind => N_Declarative_Item,
+                              In_Tree => Tree);
 
             Attribute : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Attribute_Declaration,
-               And_Expr_Kind => List);
+                           Default_Project_Node
+                             (Of_Kind       => N_Attribute_Declaration,
+                              In_Tree       => Tree,
+                              And_Expr_Kind => List);
 
             Expression : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Expression,
-               And_Expr_Kind => List);
+                           Default_Project_Node
+                             (Of_Kind       => N_Expression,
+                              In_Tree       => Tree,
+                              And_Expr_Kind => List);
 
             Term  : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Term, And_Expr_Kind => List);
+                           Default_Project_Node
+                             (Of_Kind       => N_Term, In_Tree => Tree,
+                              And_Expr_Kind => List);
 
          begin
             Set_Next_Declarative_Item
-              (Decl_Item,
-               To => First_Declarative_Item_Of (Project_Declaration));
+              (Decl_Item, Tree,
+               To => First_Declarative_Item_Of (Project_Declaration, Tree));
             Set_First_Declarative_Item_Of
-              (Project_Declaration, To => Decl_Item);
-            Set_Current_Item_Node (Decl_Item, To => Attribute);
-            Set_Name_Of (Attribute, To => Name_Source_Dirs);
-            Set_Expression_Of (Attribute, To => Expression);
-            Set_First_Term (Expression, To => Term);
+              (Project_Declaration, Tree, To => Decl_Item);
+            Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
+            Set_Name_Of (Attribute, Tree, To => Name_Source_Dirs);
+            Set_Expression_Of (Attribute, Tree, To => Expression);
+            Set_First_Term (Expression, Tree, To => Term);
             Source_Dirs_List :=
-              Default_Project_Node (Of_Kind => N_Literal_String_List,
-                                    And_Expr_Kind => List);
-            Set_Current_Term (Term, To => Source_Dirs_List);
+              Default_Project_Node
+                (Of_Kind       => N_Literal_String_List,
+                 In_Tree       => Tree,
+                 And_Expr_Kind => List);
+            Set_Current_Term (Term, Tree, To => Source_Dirs_List);
+
+            --  Attach the comments, if any, that were saved for attribute
+            --  Source_Dirs.
+
+            Tree.Project_Nodes.Table (Attribute).Comments :=
+              Source_Dirs_Comments;
          end;
 
          --  Add an attribute declaration for Source_List_File with the
@@ -1058,43 +1225,60 @@ package body Prj.Makr is
 
          declare
             Decl_Item  : constant Project_Node_Id :=
-              Default_Project_Node (Of_Kind => N_Declarative_Item);
+                           Default_Project_Node
+                             (Of_Kind => N_Declarative_Item,
+                              In_Tree => Tree);
 
-            Attribute : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Attribute_Declaration,
-               And_Expr_Kind => Single);
+            Attribute  : constant Project_Node_Id :=
+                            Default_Project_Node
+                              (Of_Kind       => N_Attribute_Declaration,
+                               In_Tree       => Tree,
+                               And_Expr_Kind => Single);
 
             Expression : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Expression,
-               And_Expr_Kind => Single);
-
-            Term  : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Term,
-               And_Expr_Kind => Single);
-
-            Value : constant Project_Node_Id :=
-              Default_Project_Node
-              (Of_Kind => N_Literal_String,
-               And_Expr_Kind => Single);
+                           Default_Project_Node
+                             (Of_Kind       => N_Expression,
+                              In_Tree       => Tree,
+                              And_Expr_Kind => Single);
+
+            Term       : constant Project_Node_Id :=
+                           Default_Project_Node
+                             (Of_Kind       => N_Term,
+                              In_Tree       => Tree,
+                              And_Expr_Kind => Single);
+
+            Value      : constant Project_Node_Id :=
+                           Default_Project_Node
+                             (Of_Kind       => N_Literal_String,
+                              In_Tree       => Tree,
+                              And_Expr_Kind => Single);
 
          begin
             Set_Next_Declarative_Item
-              (Decl_Item,
-               To => First_Declarative_Item_Of (Project_Declaration));
+              (Decl_Item, Tree,
+               To => First_Declarative_Item_Of (Project_Declaration, Tree));
             Set_First_Declarative_Item_Of
-              (Project_Declaration, To => Decl_Item);
-            Set_Current_Item_Node (Decl_Item, To => Attribute);
-            Set_Name_Of (Attribute, To => Name_Source_List_File);
-            Set_Expression_Of (Attribute, To => Expression);
-            Set_First_Term (Expression, To => Term);
-            Set_Current_Term (Term, To => Value);
+              (Project_Declaration, Tree, To => Decl_Item);
+            Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
+            Set_Name_Of (Attribute, Tree, To => Name_Source_List_File);
+            Set_Expression_Of (Attribute, Tree, To => Expression);
+            Set_First_Term (Expression, Tree, To => Term);
+            Set_Current_Term (Term, Tree, To => Value);
             Name_Len := Source_List_Last;
             Name_Buffer (1 .. Name_Len) :=
               Source_List_Path (1 .. Source_List_Last);
-            Set_String_Value_Of (Value, To => Name_Find);
+            Set_String_Value_Of (Value, Tree, To => Name_Find);
+
+            --  If there was no comments for attribute Source_List_File, put
+            --  those for Source_Files, if they exist.
+
+            if Source_List_File_Comments /= Empty_Node then
+               Tree.Project_Nodes.Table (Attribute).Comments :=
+                 Source_List_File_Comments;
+            else
+               Tree.Project_Nodes.Table (Attribute).Comments :=
+                 Source_Files_Comments;
+            end if;
          end;
       end if;
 
@@ -1106,6 +1290,7 @@ package body Prj.Makr is
             Dir_Name    : constant String := Directories (Index).all;
             Last        : Natural := Dir_Name'Last;
             Recursively : Boolean := False;
+
          begin
             if Dir_Name'Length >= 4
               and then (Dir_Name (Last - 2 .. Last) = "/**")
@@ -1120,35 +1305,38 @@ package body Prj.Makr is
 
                declare
                   Expression : constant Project_Node_Id :=
-                    Default_Project_Node
-                    (Of_Kind => N_Expression,
-                     And_Expr_Kind => Single);
-
-                  Term : constant Project_Node_Id :=
-                    Default_Project_Node
-                    (Of_Kind => N_Term,
-                     And_Expr_Kind => Single);
-
-                  Value : constant Project_Node_Id :=
-                    Default_Project_Node
-                    (Of_Kind => N_Literal_String,
-                     And_Expr_Kind => Single);
+                                 Default_Project_Node
+                                   (Of_Kind       => N_Expression,
+                                    In_Tree       => Tree,
+                                    And_Expr_Kind => Single);
+
+                  Term       : constant Project_Node_Id :=
+                                 Default_Project_Node
+                                   (Of_Kind       => N_Term,
+                                    In_Tree       => Tree,
+                                    And_Expr_Kind => Single);
+
+                  Value      : constant Project_Node_Id :=
+                                 Default_Project_Node
+                                   (Of_Kind       => N_Literal_String,
+                                    In_Tree       => Tree,
+                                    And_Expr_Kind => Single);
 
                begin
                   if Current_Source_Dir = Empty_Node then
                      Set_First_Expression_In_List
-                       (Source_Dirs_List, To => Expression);
+                       (Source_Dirs_List, Tree, To => Expression);
                   else
                      Set_Next_Expression_In_List
-                       (Current_Source_Dir, To => Expression);
+                       (Current_Source_Dir, Tree, To => Expression);
                   end if;
 
                   Current_Source_Dir := Expression;
-                  Set_First_Term (Expression, To => Term);
-                  Set_Current_Term (Term, To => Value);
+                  Set_First_Term (Expression, Tree, To => Term);
+                  Set_Current_Term (Term, Tree, To => Value);
                   Name_Len := Dir_Name'Length;
                   Name_Buffer (1 .. Name_Len) := Dir_Name;
-                  Set_String_Value_Of (Value, To => Name_Find);
+                  Set_String_Value_Of (Value, Tree, To => Name_Find);
                end;
             end if;
 
@@ -1195,7 +1383,7 @@ package body Prj.Makr is
             --  Output the project file
 
             Prj.PP.Pretty_Print
-              (Project_Node,
+              (Project_Node, Tree,
                W_Char => Write_A_Char'Access,
                W_Eol  => Write_Eol'Access,
                W_Str  => Write_A_String'Access,
@@ -1233,7 +1421,7 @@ package body Prj.Makr is
             --  Output the naming project file
 
             Prj.PP.Pretty_Print
-              (Project_Naming_Node,
+              (Project_Naming_Node, Tree,
                W_Char => Write_A_Char'Access,
                W_Eol  => Write_Eol'Access,
                W_Str  => Write_A_String'Access,
@@ -1248,7 +1436,8 @@ package body Prj.Makr is
                Write_A_String ("pragma Source_File_Name");
                Write_Eol;
                Write_A_String ("  (");
-               Write_A_String (SFN_Pragmas.Table (Index).Unit.all);
+               Write_A_String
+                 (Get_Name_String (SFN_Pragmas.Table (Index).Unit));
                Write_A_String (",");
                Write_Eol;
 
@@ -1259,8 +1448,17 @@ package body Prj.Makr is
                   Write_A_String ("   Body_File_Name => """);
                end if;
 
-               Write_A_String (SFN_Pragmas.Table (Index).File.all);
-               Write_A_String (""");");
+               Write_A_String
+                 (Get_Name_String (SFN_Pragmas.Table (Index).File));
+
+               Write_A_String ("""");
+
+               if SFN_Pragmas.Table (Index).Index /= 0 then
+                  Write_A_String (", Index =>");
+                  Write_A_String (SFN_Pragmas.Table (Index).Index'Img);
+               end if;
+
+               Write_A_String (");");
                Write_Eol;
             end loop;