1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2008, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Err_Vars
; use Err_Vars
;
28 with Osint
; use Osint
;
29 with Output
; use Output
;
30 with Prj
.Com
; use Prj
.Com
;
32 with Prj
.Err
; use Prj
.Err
;
33 with Prj
.Ext
; use Prj
.Ext
;
34 with Sinput
; use Sinput
;
35 with Sinput
.P
; use Sinput
.P
;
39 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
40 with Ada
.Exceptions
; use Ada
.Exceptions
;
42 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
44 with System
.HTable
; use System
.HTable
;
46 package body Prj
.Part
is
48 Buffer
: String_Access
;
49 Buffer_Last
: Natural := 0;
51 Dir_Sep
: Character renames GNAT
.OS_Lib
.Directory_Separator
;
53 ------------------------------------
54 -- Local Packages and Subprograms --
55 ------------------------------------
57 type With_Id
is new Nat
;
58 No_With
: constant With_Id
:= 0;
60 type With_Record
is record
61 Path
: Path_Name_Type
;
62 Location
: Source_Ptr
;
63 Limited_With
: Boolean;
64 Node
: Project_Node_Id
;
67 -- Information about an imported project, to be put in table Withs below
69 package Withs
is new Table
.Table
70 (Table_Component_Type
=> With_Record
,
71 Table_Index_Type
=> With_Id
,
74 Table_Increment
=> 100,
75 Table_Name
=> "Prj.Part.Withs");
76 -- Table used to store temporarily paths and locations of imported
77 -- projects. These imported projects will be effectively parsed later: just
78 -- before parsing the current project for the non limited withed projects,
79 -- after getting its name; after complete parsing of the current project
80 -- for the limited withed projects.
82 type Names_And_Id
is record
83 Path_Name
: Path_Name_Type
;
84 Canonical_Path_Name
: Path_Name_Type
;
86 Limited_With
: Boolean;
89 package Project_Stack
is new Table
.Table
90 (Table_Component_Type
=> Names_And_Id
,
91 Table_Index_Type
=> Nat
,
94 Table_Increment
=> 100,
95 Table_Name
=> "Prj.Part.Project_Stack");
96 -- This table is used to detect circular dependencies
97 -- for imported and extended projects and to get the project ids of
98 -- limited imported projects when there is a circularity with at least
99 -- one limited imported project file.
101 package Virtual_Hash
is new System
.HTable
.Simple_HTable
102 (Header_Num
=> Header_Num
,
103 Element
=> Project_Node_Id
,
104 No_Element
=> Empty_Node
,
105 Key
=> Project_Node_Id
,
106 Hash
=> Prj
.Tree
.Hash
,
108 -- Hash table to store the node id of the project for which a virtual
109 -- extending project need to be created.
111 package Processed_Hash
is new System
.HTable
.Simple_HTable
112 (Header_Num
=> Header_Num
,
115 Key
=> Project_Node_Id
,
116 Hash
=> Prj
.Tree
.Hash
,
118 -- Hash table to store the project process when looking for project that
119 -- need to have a virtual extending project, to avoid processing the same
122 package Projects_Paths
is new System
.HTable
.Simple_HTable
123 (Header_Num
=> Header_Num
,
124 Element
=> Path_Name_Type
,
125 No_Element
=> No_Path
,
129 -- Hash table to cache project path to avoid looking for them on the path
131 procedure Create_Virtual_Extending_Project
132 (For_Project
: Project_Node_Id
;
133 Main_Project
: Project_Node_Id
;
134 In_Tree
: Project_Node_Tree_Ref
);
135 -- Create a virtual extending project of For_Project. Main_Project is
136 -- the extending all project.
138 -- The String_Value_Of is not set for the automatically added with
139 -- clause and keeps the default value of No_Name. This enables Prj.PP
140 -- to skip these automatically added with clauses to be processed.
142 procedure Look_For_Virtual_Projects_For
143 (Proj
: Project_Node_Id
;
144 In_Tree
: Project_Node_Tree_Ref
;
145 Potentially_Virtual
: Boolean);
146 -- Look for projects that need to have a virtual extending project.
147 -- This procedure is recursive. If called with Potentially_Virtual set to
148 -- True, then Proj may need an virtual extending project; otherwise it
149 -- does not (because it is already extended), but other projects that it
150 -- imports may need to be virtually extended.
152 type Extension_Origin
is (None
, Extending_Simple
, Extending_All
);
153 -- Type of parameter From_Extended for procedures Parse_Single_Project and
154 -- Post_Parse_Context_Clause. Extending_All means that we are parsing the
155 -- tree rooted at an extending all project.
157 procedure Parse_Single_Project
158 (In_Tree
: Project_Node_Tree_Ref
;
159 Project
: out Project_Node_Id
;
160 Extends_All
: out Boolean;
163 From_Extended
: Extension_Origin
;
164 In_Limited
: Boolean;
165 Packages_To_Check
: String_List_Access
;
167 Current_Dir
: String);
168 -- Parse a project file. This is a recursive procedure: it calls itself for
169 -- imported and extended projects. When From_Extended is not None, if the
170 -- project has already been parsed and is an extended project A, return the
171 -- ultimate (not extended) project that extends A. When In_Limited is True,
172 -- the importing path includes at least one "limited with". When parsing
173 -- configuration projects, do not allow a depth > 1.
175 procedure Pre_Parse_Context_Clause
176 (In_Tree
: Project_Node_Tree_Ref
;
177 Context_Clause
: out With_Id
);
178 -- Parse the context clause of a project. Store the paths and locations of
179 -- the imported projects in table Withs. Does nothing if there is no
180 -- context clause (if the current token is not "with" or "limited" followed
183 procedure Post_Parse_Context_Clause
184 (Context_Clause
: With_Id
;
185 In_Tree
: Project_Node_Tree_Ref
;
186 Limited_Withs
: Boolean;
187 Imported_Projects
: in out Project_Node_Id
;
188 Project_Directory
: Path_Name_Type
;
189 From_Extended
: Extension_Origin
;
190 In_Limited
: Boolean;
191 Packages_To_Check
: String_List_Access
;
193 Current_Dir
: String);
194 -- Parse the imported projects that have been stored in table Withs, if
195 -- any. From_Extended is used for the call to Parse_Single_Project below.
196 -- When In_Limited is True, the importing path includes at least one
197 -- "limited with". When Limited_Withs is False, only non limited withed
198 -- projects are parsed. When Limited_Withs is True, only limited withed
199 -- projects are parsed.
201 function Project_Path_Name_Of
202 (Project_File_Name
: String;
203 Directory
: String) return String;
204 -- Returns the path name of a project file. Returns an empty string
205 -- if project file cannot be found.
207 function Immediate_Directory_Of
208 (Path_Name
: Path_Name_Type
) return Path_Name_Type
;
209 -- Get the directory of the file with the specified path name.
210 -- This includes the directory separator as the last character.
211 -- Returns "./" if Path_Name contains no directory separator.
213 function Project_Name_From
(Path_Name
: String) return Name_Id
;
214 -- Returns the name of the project that corresponds to its path name.
215 -- Returns No_Name if the path name is invalid, because the corresponding
216 -- project name does not have the syntax of an ada identifier.
218 --------------------------------------
219 -- Create_Virtual_Extending_Project --
220 --------------------------------------
222 procedure Create_Virtual_Extending_Project
223 (For_Project
: Project_Node_Id
;
224 Main_Project
: Project_Node_Id
;
225 In_Tree
: Project_Node_Tree_Ref
)
228 Virtual_Name
: constant String :=
230 Get_Name_String
(Name_Of
(For_Project
, In_Tree
));
231 -- The name of the virtual extending project
233 Virtual_Name_Id
: Name_Id
;
234 -- Virtual extending project name id
236 Virtual_Path_Id
: Path_Name_Type
;
237 -- Fake path name of the virtual extending project. The directory is
238 -- the same directory as the extending all project.
240 Virtual_Dir_Id
: constant Path_Name_Type
:=
241 Immediate_Directory_Of
(Path_Name_Of
(Main_Project
, In_Tree
));
242 -- The directory of the extending all project
244 -- The source of the virtual extending project is something like:
246 -- project V$<project name> extends <project path> is
248 -- for Source_Dirs use ();
250 -- end V$<project name>;
252 -- The project directory cannot be specified during parsing; it will be
253 -- put directly in the virtual extending project data during processing.
255 -- Nodes that made up the virtual extending project
257 Virtual_Project
: constant Project_Node_Id
:=
259 (In_Tree
, N_Project
);
260 With_Clause
: constant Project_Node_Id
:=
262 (In_Tree
, N_With_Clause
);
263 Project_Declaration
: constant Project_Node_Id
:=
265 (In_Tree
, N_Project_Declaration
);
266 Source_Dirs_Declaration
: constant Project_Node_Id
:=
268 (In_Tree
, N_Declarative_Item
);
269 Source_Dirs_Attribute
: constant Project_Node_Id
:=
271 (In_Tree
, N_Attribute_Declaration
, List
);
272 Source_Dirs_Expression
: constant Project_Node_Id
:=
274 (In_Tree
, N_Expression
, List
);
275 Source_Dirs_Term
: constant Project_Node_Id
:=
277 (In_Tree
, N_Term
, List
);
278 Source_Dirs_List
: constant Project_Node_Id
:=
280 (In_Tree
, N_Literal_String_List
, List
);
283 -- Get the virtual name id
285 Name_Len
:= Virtual_Name
'Length;
286 Name_Buffer
(1 .. Name_Len
) := Virtual_Name
;
287 Virtual_Name_Id
:= Name_Find
;
289 -- Get the virtual path name
291 Get_Name_String
(Path_Name_Of
(Main_Project
, In_Tree
));
294 and then Name_Buffer
(Name_Len
) /= Directory_Separator
295 and then Name_Buffer
(Name_Len
) /= '/'
297 Name_Len
:= Name_Len
- 1;
300 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ Virtual_Name
'Length) :=
302 Name_Len
:= Name_Len
+ Virtual_Name
'Length;
303 Virtual_Path_Id
:= Name_Find
;
307 Set_Name_Of
(With_Clause
, In_Tree
, Virtual_Name_Id
);
308 Set_Path_Name_Of
(With_Clause
, In_Tree
, Virtual_Path_Id
);
309 Set_Project_Node_Of
(With_Clause
, In_Tree
, Virtual_Project
);
310 Set_Next_With_Clause_Of
311 (With_Clause
, In_Tree
, First_With_Clause_Of
(Main_Project
, In_Tree
));
312 Set_First_With_Clause_Of
(Main_Project
, In_Tree
, With_Clause
);
314 -- Virtual project node
316 Set_Name_Of
(Virtual_Project
, In_Tree
, Virtual_Name_Id
);
317 Set_Path_Name_Of
(Virtual_Project
, In_Tree
, Virtual_Path_Id
);
319 (Virtual_Project
, In_Tree
, Location_Of
(Main_Project
, In_Tree
));
320 Set_Directory_Of
(Virtual_Project
, In_Tree
, Virtual_Dir_Id
);
321 Set_Project_Declaration_Of
322 (Virtual_Project
, In_Tree
, Project_Declaration
);
323 Set_Extended_Project_Path_Of
324 (Virtual_Project
, In_Tree
, Path_Name_Of
(For_Project
, In_Tree
));
326 -- Project declaration
328 Set_First_Declarative_Item_Of
329 (Project_Declaration
, In_Tree
, Source_Dirs_Declaration
);
330 Set_Extended_Project_Of
(Project_Declaration
, In_Tree
, For_Project
);
332 -- Source_Dirs declaration
334 Set_Current_Item_Node
335 (Source_Dirs_Declaration
, In_Tree
, Source_Dirs_Attribute
);
337 -- Source_Dirs attribute
339 Set_Name_Of
(Source_Dirs_Attribute
, In_Tree
, Snames
.Name_Source_Dirs
);
341 (Source_Dirs_Attribute
, In_Tree
, Source_Dirs_Expression
);
343 -- Source_Dirs expression
345 Set_First_Term
(Source_Dirs_Expression
, In_Tree
, Source_Dirs_Term
);
349 Set_Current_Term
(Source_Dirs_Term
, In_Tree
, Source_Dirs_List
);
351 -- Source_Dirs empty list: nothing to do
353 -- Put virtual project into Projects_Htable
355 Prj
.Tree
.Tree_Private_Part
.Projects_Htable
.Set
356 (T
=> In_Tree
.Projects_HT
,
357 K
=> Virtual_Name_Id
,
358 E
=> (Name
=> Virtual_Name_Id
,
359 Node
=> Virtual_Project
,
360 Canonical_Path
=> No_Path
,
362 Proj_Qualifier
=> Unspecified
));
363 end Create_Virtual_Extending_Project
;
365 ----------------------------
366 -- Immediate_Directory_Of --
367 ----------------------------
369 function Immediate_Directory_Of
370 (Path_Name
: Path_Name_Type
) return Path_Name_Type
373 Get_Name_String
(Path_Name
);
375 for Index
in reverse 1 .. Name_Len
loop
376 if Name_Buffer
(Index
) = '/'
377 or else Name_Buffer
(Index
) = Dir_Sep
379 -- Remove all chars after last directory separator from name
382 Name_Len
:= Index
- 1;
392 -- There is no directory separator in name. Return "./" or ".\"
395 Name_Buffer
(1) := '.';
396 Name_Buffer
(2) := Dir_Sep
;
398 end Immediate_Directory_Of
;
400 -----------------------------------
401 -- Look_For_Virtual_Projects_For --
402 -----------------------------------
404 procedure Look_For_Virtual_Projects_For
405 (Proj
: Project_Node_Id
;
406 In_Tree
: Project_Node_Tree_Ref
;
407 Potentially_Virtual
: Boolean)
409 Declaration
: Project_Node_Id
:= Empty_Node
;
410 -- Node for the project declaration of Proj
412 With_Clause
: Project_Node_Id
:= Empty_Node
;
413 -- Node for a with clause of Proj
415 Imported
: Project_Node_Id
:= Empty_Node
;
416 -- Node for a project imported by Proj
418 Extended
: Project_Node_Id
:= Empty_Node
;
419 -- Node for the eventual project extended by Proj
422 -- Nothing to do if Proj is not defined or if it has already been
425 if Present
(Proj
) and then not Processed_Hash
.Get
(Proj
) then
426 -- Make sure the project will not be processed again
428 Processed_Hash
.Set
(Proj
, True);
430 Declaration
:= Project_Declaration_Of
(Proj
, In_Tree
);
432 if Present
(Declaration
) then
433 Extended
:= Extended_Project_Of
(Declaration
, In_Tree
);
436 -- If this is a project that may need a virtual extending project
437 -- and it is not itself an extending project, put it in the list.
439 if Potentially_Virtual
and then No
(Extended
) then
440 Virtual_Hash
.Set
(Proj
, Proj
);
443 -- Now check the projects it imports
445 With_Clause
:= First_With_Clause_Of
(Proj
, In_Tree
);
447 while Present
(With_Clause
) loop
448 Imported
:= Project_Node_Of
(With_Clause
, In_Tree
);
450 if Present
(Imported
) then
451 Look_For_Virtual_Projects_For
452 (Imported
, In_Tree
, Potentially_Virtual
=> True);
455 With_Clause
:= Next_With_Clause_Of
(With_Clause
, In_Tree
);
458 -- Check also the eventual project extended by Proj. As this project
459 -- is already extended, call recursively with Potentially_Virtual
462 Look_For_Virtual_Projects_For
463 (Extended
, In_Tree
, Potentially_Virtual
=> False);
465 end Look_For_Virtual_Projects_For
;
472 (In_Tree
: Project_Node_Tree_Ref
;
473 Project
: out Project_Node_Id
;
474 Project_File_Name
: String;
475 Always_Errout_Finalize
: Boolean;
476 Packages_To_Check
: String_List_Access
:= All_Packages
;
477 Store_Comments
: Boolean := False;
478 Current_Directory
: String := "")
481 pragma Warnings
(Off
, Dummy
);
483 Real_Project_File_Name
: String_Access
:=
484 Osint
.To_Canonical_File_Spec
488 if Real_Project_File_Name
= null then
489 Real_Project_File_Name
:= new String'(Project_File_Name);
492 Project := Empty_Node;
494 Projects_Paths.Reset;
496 if Current_Verbosity >= Medium then
497 Write_Str ("GPR_PROJECT_PATH=""");
498 Write_Str (Project_Path);
503 Path_Name : constant String :=
504 Project_Path_Name_Of (Real_Project_File_Name.all,
505 Directory => Current_Directory);
508 Free (Real_Project_File_Name);
511 Prj.Err.Scanner.Set_Comment_As_Token (Store_Comments);
512 Prj.Err.Scanner.Set_End_Of_Line_As_Token (Store_Comments);
514 -- Parse the main project file
516 if Path_Name = "" then
522 Project := Empty_Node;
529 Extends_All => Dummy,
530 Path_Name => Path_Name,
532 From_Extended => None,
534 Packages_To_Check => Packages_To_Check,
536 Current_Dir => Current_Directory);
538 -- If Project is an extending-all project, create the eventual
539 -- virtual extending projects and check that there are no illegally
540 -- imported projects.
543 and then Is_Extending_All (Project, In_Tree)
545 -- First look for projects that potentially need a virtual
546 -- extending project.
549 Processed_Hash.Reset;
551 -- Mark the extending all project as processed, to avoid checking
552 -- the imported projects in case of a "limited with" on this
553 -- extending all project.
555 Processed_Hash.Set (Project, True);
558 Declaration : constant Project_Node_Id :=
559 Project_Declaration_Of (Project, In_Tree);
561 Look_For_Virtual_Projects_For
562 (Extended_Project_Of (Declaration, In_Tree), In_Tree,
563 Potentially_Virtual => False);
566 -- Now, check the projects directly imported by the main project.
567 -- Remove from the potentially virtual any project extended by one
568 -- of these imported projects. For non extending imported
569 -- projects, check that they do not belong to the project tree of
570 -- the project being "extended-all" by the main project.
573 With_Clause : Project_Node_Id;
574 Imported : Project_Node_Id := Empty_Node;
575 Declaration : Project_Node_Id := Empty_Node;
578 With_Clause := First_With_Clause_Of (Project, In_Tree);
579 while Present (With_Clause) loop
580 Imported := Project_Node_Of (With_Clause, In_Tree);
582 if Present (Imported) then
583 Declaration := Project_Declaration_Of (Imported, In_Tree);
585 if Extended_Project_Of (Declaration, In_Tree) /=
590 Extended_Project_Of (Declaration, In_Tree);
591 exit when No (Imported);
592 Virtual_Hash.Remove (Imported);
594 Project_Declaration_Of (Imported, In_Tree);
599 With_Clause := Next_With_Clause_Of (With_Clause, In_Tree);
603 -- Now create all the virtual extending projects
606 Proj : Project_Node_Id := Virtual_Hash.Get_First;
608 while Present (Proj) loop
609 Create_Virtual_Extending_Project (Proj, Project, In_Tree);
610 Proj := Virtual_Hash.Get_Next;
615 -- If there were any kind of error during the parsing, serious
616 -- or not, then the parsing fails.
618 if Err_Vars.Total_Errors_Detected > 0 then
619 Project := Empty_Node;
622 if No (Project) or else Always_Errout_Finalize then
632 Write_Line (Exception_Information (X));
633 Write_Str ("Exception ");
634 Write_Str (Exception_Name (X));
635 Write_Line (" raised, while processing project file");
636 Project := Empty_Node;
639 ------------------------------
640 -- Pre_Parse_Context_Clause --
641 ------------------------------
643 procedure Pre_Parse_Context_Clause
644 (In_Tree : Project_Node_Tree_Ref;
645 Context_Clause : out With_Id)
647 Current_With_Clause : With_Id := No_With;
648 Limited_With : Boolean := False;
649 Current_With : With_Record;
650 Current_With_Node : Project_Node_Id := Empty_Node;
653 -- Assume no context clause
655 Context_Clause := No_With;
658 -- If Token is not WITH or LIMITED, there is no context clause, or we
659 -- have exhausted the with clauses.
661 while Token = Tok_With or else Token = Tok_Limited loop
663 Default_Project_Node (Of_Kind => N_With_Clause, In_Tree => In_Tree);
664 Limited_With := Token = Tok_Limited;
666 if In_Configuration then
668 ("configuration project cannot import " &
669 "other configuration projects",
674 Scan (In_Tree); -- scan past LIMITED
675 Expect (Tok_With, "WITH");
676 exit With_Loop when Token /= Tok_With;
681 Scan (In_Tree); -- past WITH or ","
683 Expect (Tok_String_Literal, "literal string");
685 if Token /= Tok_String_Literal then
689 -- Store path and location in table Withs
692 (Path => Path_Name_Type (Token_Name),
693 Location => Token_Ptr,
694 Limited_With => Limited_With,
695 Node => Current_With_Node,
698 Withs.Increment_Last;
699 Withs.Table (Withs.Last) := Current_With;
701 if Current_With_Clause = No_With then
702 Context_Clause := Withs.Last;
705 Withs.Table (Current_With_Clause).Next := Withs.Last;
708 Current_With_Clause := Withs.Last;
712 if Token = Tok_Semicolon then
713 Set_End_Of_Line (Current_With_Node);
714 Set_Previous_Line_Node (Current_With_Node);
716 -- End of (possibly multiple) with clause;
718 Scan (In_Tree); -- past the semicolon
721 elsif Token = Tok_Comma then
722 Set_Is_Not_Last_In_List (Current_With_Node, In_Tree);
725 Error_Msg ("expected comma or semi colon", Token_Ptr);
731 (Of_Kind => N_With_Clause, In_Tree => In_Tree);
734 end Pre_Parse_Context_Clause;
736 -------------------------------
737 -- Post_Parse_Context_Clause --
738 -------------------------------
740 procedure Post_Parse_Context_Clause
741 (Context_Clause : With_Id;
742 In_Tree : Project_Node_Tree_Ref;
743 Limited_Withs : Boolean;
744 Imported_Projects : in out Project_Node_Id;
745 Project_Directory : Path_Name_Type;
746 From_Extended : Extension_Origin;
747 In_Limited : Boolean;
748 Packages_To_Check : String_List_Access;
750 Current_Dir : String)
752 Current_With_Clause : With_Id := Context_Clause;
754 Current_Project : Project_Node_Id := Imported_Projects;
755 Previous_Project : Project_Node_Id := Empty_Node;
756 Next_Project : Project_Node_Id := Empty_Node;
758 Project_Directory_Path : constant String :=
759 Get_Name_String (Project_Directory);
761 Current_With : With_Record;
762 Extends_All : Boolean := False;
765 -- Set Current_Project to the last project in the current list, if the
766 -- list is not empty.
768 if Present (Current_Project) then
770 Present (Next_With_Clause_Of (Current_Project, In_Tree))
772 Current_Project := Next_With_Clause_Of (Current_Project, In_Tree);
776 while Current_With_Clause /= No_With loop
777 Current_With := Withs.Table (Current_With_Clause);
778 Current_With_Clause := Current_With.Next;
780 if Limited_Withs = Current_With.Limited_With then
782 Original_Path : constant String :=
783 Get_Name_String (Current_With.Path);
785 Imported_Path_Name : constant String :=
788 Project_Directory_Path);
790 Resolved_Path : constant String :=
793 Directory => Current_Dir,
795 Opt.Follow_Links_For_Files,
796 Case_Sensitive => True);
798 Withed_Project : Project_Node_Id := Empty_Node;
801 if Imported_Path_Name = "" then
803 -- The project file cannot be found
805 Error_Msg_File_1 := File_Name_Type (Current_With.Path);
807 ("unknown project file: {", Current_With.Location);
809 -- If this is not imported by the main project file, display
812 if Project_Stack.Last > 1 then
813 for Index in reverse 1 .. Project_Stack.Last loop
816 (Project_Stack.Table (Index).Path_Name);
818 ("\imported by {", Current_With.Location);
825 Previous_Project := Current_Project;
827 if No (Current_Project) then
829 -- First with clause of the context clause
831 Current_Project := Current_With.Node;
832 Imported_Projects := Current_Project;
835 Next_Project := Current_With.Node;
836 Set_Next_With_Clause_Of
837 (Current_Project, In_Tree, Next_Project);
838 Current_Project := Next_Project;
844 Name_Id (Current_With.Path));
846 (Current_Project, In_Tree, Current_With.Location);
848 -- If it is a limited with, check if we have a circularity.
849 -- If we have one, get the project id of the limited
850 -- imported project file, and do not parse it.
852 if Limited_Withs and then Project_Stack.Last > 1 then
854 Canonical_Path_Name : Path_Name_Type;
857 Name_Len := Resolved_Path'Length;
858 Name_Buffer (1 .. Name_Len) := Resolved_Path;
859 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
860 Canonical_Path_Name := Name_Find;
862 for Index in 1 .. Project_Stack.Last loop
863 if Project_Stack.Table (Index).Canonical_Path_Name =
866 -- We have found the limited imported project,
867 -- get its project id, and do not parse it.
869 Withed_Project := Project_Stack.Table (Index).Id;
876 -- Parse the imported project, if its project id is unknown
878 if No (Withed_Project) then
881 Project => Withed_Project,
882 Extends_All => Extends_All,
883 Path_Name => Imported_Path_Name,
885 From_Extended => From_Extended,
886 In_Limited => Limited_Withs,
887 Packages_To_Check => Packages_To_Check,
889 Current_Dir => Current_Dir);
892 Extends_All := Is_Extending_All (Withed_Project, In_Tree);
895 if No (Withed_Project) then
897 -- If parsing unsuccessful, remove the context clause
899 Current_Project := Previous_Project;
901 if No (Current_Project) then
902 Imported_Projects := Empty_Node;
905 Set_Next_With_Clause_Of
906 (Current_Project, In_Tree, Empty_Node);
909 -- If parsing was successful, record project name and
910 -- path name in with clause
913 (Node => Current_Project,
915 To => Withed_Project,
916 Limited_With => Current_With.Limited_With);
920 Name_Of (Withed_Project, In_Tree));
922 Name_Len := Resolved_Path'Length;
923 Name_Buffer (1 .. Name_Len) := Resolved_Path;
924 Set_Path_Name_Of (Current_Project, In_Tree, Name_Find);
927 Set_Is_Extending_All (Current_Project, In_Tree);
934 end Post_Parse_Context_Clause;
936 --------------------------
937 -- Parse_Single_Project --
938 --------------------------
940 procedure Parse_Single_Project
941 (In_Tree : Project_Node_Tree_Ref;
942 Project : out Project_Node_Id;
943 Extends_All : out Boolean;
946 From_Extended : Extension_Origin;
947 In_Limited : Boolean;
948 Packages_To_Check : String_List_Access;
950 Current_Dir : String)
952 Normed_Path_Name : Path_Name_Type;
953 Canonical_Path_Name : Path_Name_Type;
954 Project_Directory : Path_Name_Type;
955 Project_Scan_State : Saved_Project_Scan_State;
956 Source_Index : Source_File_Index;
958 Extending : Boolean := False;
960 Extended_Project : Project_Node_Id := Empty_Node;
962 A_Project_Name_And_Node : Tree_Private_Part.Project_Name_And_Node :=
963 Tree_Private_Part.Projects_Htable.Get_First
964 (In_Tree.Projects_HT);
966 Name_From_Path : constant Name_Id := Project_Name_From (Path_Name);
967 Name_Of_Project : Name_Id := No_Name;
969 Duplicated : Boolean := False;
971 First_With : With_Id;
972 Imported_Projects : Project_Node_Id := Empty_Node;
974 use Tree_Private_Part;
976 Project_Comment_State : Tree.Comment_State;
978 Proj_Qualifier : Project_Qualifier := Unspecified;
979 Qualifier_Location : Source_Ptr;
982 Extends_All := False;
985 Normed_Path : constant String := Normalize_Pathname
987 Directory => Current_Dir,
988 Resolve_Links => False,
989 Case_Sensitive => True);
990 Canonical_Path : constant String := Normalize_Pathname
992 Directory => Current_Dir,
993 Resolve_Links => Opt.Follow_Links_For_Files,
994 Case_Sensitive => False);
996 Name_Len := Normed_Path'Length;
997 Name_Buffer (1 .. Name_Len) := Normed_Path;
998 Normed_Path_Name := Name_Find;
999 Name_Len := Canonical_Path'Length;
1000 Name_Buffer (1 .. Name_Len) := Canonical_Path;
1001 Canonical_Path_Name := Name_Find;
1004 -- Check for a circular dependency
1006 for Index in reverse 1 .. Project_Stack.Last loop
1007 exit when Project_Stack.Table (Index).Limited_With;
1009 if Canonical_Path_Name =
1010 Project_Stack.Table (Index).Canonical_Path_Name
1012 Error_Msg ("circular dependency detected", Token_Ptr);
1013 Error_Msg_Name_1 := Name_Id (Normed_Path_Name);
1014 Error_Msg ("\ %% is imported by", Token_Ptr);
1016 for Current in reverse 1 .. Project_Stack.Last loop
1018 Name_Id (Project_Stack.Table (Current).Path_Name);
1020 if Project_Stack.Table (Current).Canonical_Path_Name /=
1024 ("\ %% which itself is imported by", Token_Ptr);
1027 Error_Msg ("\ %%", Token_Ptr);
1032 Project := Empty_Node;
1037 -- Put the new path name on the stack
1039 Project_Stack.Append
1040 ((Path_Name => Normed_Path_Name,
1041 Canonical_Path_Name => Canonical_Path_Name,
1043 Limited_With => In_Limited));
1045 -- Check if the project file has already been parsed
1048 A_Project_Name_And_Node /= Tree_Private_Part.No_Project_Name_And_Node
1050 if A_Project_Name_And_Node.Canonical_Path = Canonical_Path_Name then
1053 if A_Project_Name_And_Node.Extended then
1054 if A_Project_Name_And_Node.Proj_Qualifier /= Dry then
1056 ("cannot extend the same project file several times",
1061 ("cannot extend an already imported project file",
1065 elsif A_Project_Name_And_Node.Extended then
1067 Is_Extending_All (A_Project_Name_And_Node.Node, In_Tree);
1069 -- If the imported project is an extended project A, and we are
1070 -- in an extended project, replace A with the ultimate project
1073 if From_Extended /= None then
1075 Decl : Project_Node_Id :=
1076 Project_Declaration_Of
1077 (A_Project_Name_And_Node.Node, In_Tree);
1079 Prj : Project_Node_Id :=
1080 Extending_Project_Of (Decl, In_Tree);
1084 Decl := Project_Declaration_Of (Prj, In_Tree);
1085 exit when Extending_Project_Of (Decl, In_Tree) =
1087 Prj := Extending_Project_Of (Decl, In_Tree);
1090 A_Project_Name_And_Node.Node := Prj;
1094 ("cannot import an already extended project file",
1099 Project := A_Project_Name_And_Node.Node;
1100 Project_Stack.Decrement_Last;
1104 A_Project_Name_And_Node :=
1105 Tree_Private_Part.Projects_Htable.Get_Next (In_Tree.Projects_HT);
1108 -- We never encountered this project file. Save the scan state, load the
1109 -- project file and start to scan it.
1111 Save_Project_Scan_State (Project_Scan_State);
1112 Source_Index := Load_Project_File (Path_Name);
1113 Tree.Save (Project_Comment_State);
1115 -- If we cannot find it, we stop
1117 if Source_Index = No_Source_File then
1118 Project := Empty_Node;
1119 Project_Stack.Decrement_Last;
1123 Prj.Err.Scanner.Initialize_Scanner (Source_Index);
1127 if not In_Configuration and then Name_From_Path = No_Name then
1129 -- The project file name is not correct (no or bad extension, or not
1130 -- following Ada identifier's syntax).
1132 Error_Msg_File_1 := File_Name_Type (Canonical_Path_Name);
1133 Error_Msg ("?{ is not a valid path name for a project file",
1137 if Current_Verbosity >= Medium then
1138 Write_Str ("Parsing """);
1139 Write_Str (Path_Name);
1144 Project_Directory := Immediate_Directory_Of (Normed_Path_Name);
1146 -- Is there any imported project?
1148 Pre_Parse_Context_Clause
1149 (In_Tree => In_Tree,
1150 Context_Clause => First_With);
1152 Project := Default_Project_Node
1153 (Of_Kind => N_Project, In_Tree => In_Tree);
1154 Project_Stack.Table (Project_Stack.Last).Id := Project;
1155 Set_Directory_Of (Project, In_Tree, Project_Directory);
1156 Set_Path_Name_Of (Project, In_Tree, Normed_Path_Name);
1158 -- Check if there is a qualifier before the reserved word "project
"
1160 Qualifier_Location := Token_Ptr;
1162 if Token = Tok_Abstract then
1163 Proj_Qualifier := Dry;
1166 elsif Token = Tok_Identifier then
1168 when Snames.Name_Standard =>
1169 Proj_Qualifier := Standard;
1172 when Snames.Name_Aggregate =>
1173 Proj_Qualifier := Aggregate;
1176 if Token = Tok_Identifier and then
1177 Token_Name = Snames.Name_Library
1179 Proj_Qualifier := Aggregate_Library;
1183 when Snames.Name_Library =>
1184 Proj_Qualifier := Library;
1187 when Snames.Name_Configuration =>
1188 if not In_Configuration then
1189 Error_Msg ("configuration projects cannot belong to a user
" &
1201 if Proj_Qualifier /= Unspecified then
1202 if In_Configuration then
1203 Error_Msg ("a configuration project cannot be qualified except
" &
1204 "as configuration project
",
1205 Qualifier_Location);
1208 Set_Project_Qualifier_Of (Project, In_Tree, Proj_Qualifier);
1211 Set_Location_Of (Project, In_Tree, Token_Ptr);
1213 Expect (Tok_Project, "PROJECT
");
1215 -- Mark location of PROJECT token if present
1217 if Token = Tok_Project then
1218 Scan (In_Tree); -- past PROJECT
1219 Set_Location_Of (Project, In_Tree, Token_Ptr);
1226 Expect (Tok_Identifier, "identifier
");
1228 -- If the token is not an identifier, clear the buffer before
1229 -- exiting to indicate that the name of the project is ill-formed.
1231 if Token /= Tok_Identifier then
1236 -- Add the identifier name to the buffer
1238 Get_Name_String (Token_Name);
1239 Add_To_Buffer (Name_Buffer (1 .. Name_Len), Buffer, Buffer_Last);
1241 -- Scan past the identifier
1245 -- If we have a dot, add a dot to the Buffer and look for the next
1248 exit when Token /= Tok_Dot;
1249 Add_To_Buffer (".", Buffer, Buffer_Last);
1251 -- Scan past the dot
1256 -- See if this is an extending project
1258 if Token = Tok_Extends then
1260 if In_Configuration then
1262 ("extending configuration project
not allowed
", Token_Ptr);
1265 -- Make sure that gnatmake will use mapping files
1267 Create_Mapping_File := True;
1269 -- We are extending another project
1273 Scan (In_Tree); -- past EXTENDS
1275 if Token = Tok_All then
1276 Extends_All := True;
1277 Set_Is_Extending_All (Project, In_Tree);
1278 Scan (In_Tree); -- scan past ALL
1282 -- If the name is well formed, Buffer_Last is > 0
1284 if Buffer_Last > 0 then
1286 -- The Buffer contains the name of the project
1288 Name_Len := Buffer_Last;
1289 Name_Buffer (1 .. Name_Len) := Buffer (1 .. Buffer_Last);
1290 Name_Of_Project := Name_Find;
1291 Set_Name_Of (Project, In_Tree, Name_Of_Project);
1293 -- To get expected name of the project file, replace dots by dashes
1295 Name_Len := Buffer_Last;
1296 Name_Buffer (1 .. Name_Len) := Buffer (1 .. Buffer_Last);
1298 for Index in 1 .. Name_Len loop
1299 if Name_Buffer (Index) = '.' then
1300 Name_Buffer (Index) := '-';
1304 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1307 Expected_Name : constant Name_Id := Name_Find;
1308 Extension : String_Access;
1311 -- Output a warning if the actual name is not the expected name
1313 if not In_Configuration
1314 and then (Name_From_Path /= No_Name)
1315 and then Expected_Name /= Name_From_Path
1317 Error_Msg_Name_1 := Expected_Name;
1319 if In_Configuration then
1320 Extension := new String'(Config_Project_File_Extension);
1323 Extension := new String'(Project_File_Extension);
1326 Error_Msg ("?file name does
not match project name
, " &
1327 "should be `
%%" & Extension.all & "`
",
1333 From_Ext : Extension_Origin := None;
1336 -- Extending_All is always propagated
1338 if From_Extended = Extending_All or else Extends_All then
1339 From_Ext := Extending_All;
1341 -- Otherwise, From_Extended is set to Extending_Single if the
1342 -- current project is an extending project.
1345 From_Ext := Extending_Simple;
1348 Post_Parse_Context_Clause
1349 (In_Tree => In_Tree,
1350 Context_Clause => First_With,
1351 Limited_Withs => False,
1352 Imported_Projects => Imported_Projects,
1353 Project_Directory => Project_Directory,
1354 From_Extended => From_Ext,
1355 In_Limited => In_Limited,
1356 Packages_To_Check => Packages_To_Check,
1358 Current_Dir => Current_Dir);
1359 Set_First_With_Clause_Of (Project, In_Tree, Imported_Projects);
1362 if not In_Configuration then
1364 Name_And_Node : Tree_Private_Part.Project_Name_And_Node :=
1365 Tree_Private_Part.Projects_Htable.Get_First
1366 (In_Tree.Projects_HT);
1367 Project_Name : Name_Id := Name_And_Node.Name;
1370 -- Check if we already have a project with this name
1372 while Project_Name /= No_Name
1373 and then Project_Name /= Name_Of_Project
1376 Tree_Private_Part.Projects_Htable.Get_Next
1377 (In_Tree.Projects_HT);
1378 Project_Name := Name_And_Node.Name;
1381 -- Report an error if we already have a project with this name
1383 if Project_Name /= No_Name then
1385 Error_Msg_Name_1 := Project_Name;
1387 ("duplicate project name
%%",
1388 Location_Of (Project, In_Tree));
1390 Name_Id (Path_Name_Of (Name_And_Node.Node, In_Tree));
1392 ("\already
in %%", Location_Of (Project, In_Tree));
1400 Expect (Tok_String_Literal, "literal
string");
1402 if Token = Tok_String_Literal then
1403 Set_Extended_Project_Path_Of
1406 Path_Name_Type (Token_Name));
1409 Original_Path_Name : constant String :=
1410 Get_Name_String (Token_Name);
1412 Extended_Project_Path_Name : constant String :=
1413 Project_Path_Name_Of
1414 (Original_Path_Name,
1416 (Project_Directory));
1419 if Extended_Project_Path_Name = "" then
1421 -- We could not find the project file to extend
1423 Error_Msg_Name_1 := Token_Name;
1425 Error_Msg ("unknown project file
: %%", Token_Ptr);
1427 -- If we are not in the main project file, display the
1430 if Project_Stack.Last > 1 then
1433 (Project_Stack.Table (Project_Stack.Last).Path_Name);
1434 Error_Msg ("\extended by
%%", Token_Ptr);
1436 for Index in reverse 1 .. Project_Stack.Last - 1 loop
1439 (Project_Stack.Table (Index).Path_Name);
1440 Error_Msg ("\imported by
%%", Token_Ptr);
1446 From_Ext : Extension_Origin := None;
1449 if From_Extended = Extending_All or else Extends_All then
1450 From_Ext := Extending_All;
1453 Parse_Single_Project
1454 (In_Tree => In_Tree,
1455 Project => Extended_Project,
1456 Extends_All => Extends_All,
1457 Path_Name => Extended_Project_Path_Name,
1459 From_Extended => From_Ext,
1460 In_Limited => In_Limited,
1461 Packages_To_Check => Packages_To_Check,
1463 Current_Dir => Current_Dir);
1466 if Present (Extended_Project) then
1468 -- A project that extends an extending-all project is
1469 -- also an extending-all project.
1471 if Is_Extending_All (Extended_Project, In_Tree) then
1472 Set_Is_Extending_All (Project, In_Tree);
1475 -- An abstract project can only extend an abstract
1476 -- project, otherwise we may have an abstract project
1477 -- with sources, if it inherits sources from the project
1480 if Proj_Qualifier = Dry and then
1481 Project_Qualifier_Of (Extended_Project, In_Tree) /= Dry
1484 ("an
abstract project can only extend
" &
1485 "another
abstract project
",
1486 Qualifier_Location);
1492 Scan (In_Tree); -- past the extended project path
1496 -- Check that a non extending-all project does not import an
1497 -- extending-all project.
1499 if not Is_Extending_All (Project, In_Tree) then
1501 With_Clause : Project_Node_Id :=
1502 First_With_Clause_Of (Project, In_Tree);
1503 Imported : Project_Node_Id := Empty_Node;
1507 while Present (With_Clause) loop
1508 Imported := Project_Node_Of (With_Clause, In_Tree);
1510 if Is_Extending_All (With_Clause, In_Tree) then
1511 Error_Msg_Name_1 := Name_Of (Imported, In_Tree);
1512 Error_Msg ("cannot import extending
-all project
%%",
1514 exit With_Clause_Loop;
1517 With_Clause := Next_With_Clause_Of (With_Clause, In_Tree);
1518 end loop With_Clause_Loop;
1522 -- Check that a project with a name including a dot either imports
1523 -- or extends the project whose name precedes the last dot.
1525 if Name_Of_Project /= No_Name then
1526 Get_Name_String (Name_Of_Project);
1532 -- Look for the last dot
1534 while Name_Len > 0 and then Name_Buffer (Name_Len) /= '.' loop
1535 Name_Len := Name_Len - 1;
1538 -- If a dot was find, check if the parent project is imported
1541 if Name_Len > 0 then
1542 Name_Len := Name_Len - 1;
1545 Parent_Name : constant Name_Id := Name_Find;
1546 Parent_Found : Boolean := False;
1547 Parent_Node : Project_Node_Id := Empty_Node;
1548 With_Clause : Project_Node_Id :=
1549 First_With_Clause_Of (Project, In_Tree);
1552 -- If there is an extended project, check its name
1554 if Present (Extended_Project) then
1555 Parent_Node := Extended_Project;
1557 Name_Of (Extended_Project, In_Tree) = Parent_Name;
1560 -- If the parent project is not the extended project,
1561 -- check each imported project until we find the parent project.
1563 while not Parent_Found and then Present (With_Clause) loop
1564 Parent_Node := Project_Node_Of (With_Clause, In_Tree);
1565 Parent_Found := Name_Of (Parent_Node, In_Tree) = Parent_Name;
1566 With_Clause := Next_With_Clause_Of (With_Clause, In_Tree);
1569 if Parent_Found then
1570 Set_Parent_Project_Of (Project, In_Tree, To => Parent_Node);
1573 -- If the parent project was not found, report an error
1575 Error_Msg_Name_1 := Name_Of_Project;
1576 Error_Msg_Name_2 := Parent_Name;
1577 Error_Msg ("project
%% does
not import
or extend project
%%",
1578 Location_Of (Project, In_Tree));
1583 Expect (Tok_Is, "IS");
1584 Set_End_Of_Line (Project);
1585 Set_Previous_Line_Node (Project);
1586 Set_Next_End_Node (Project);
1589 Project_Declaration : Project_Node_Id := Empty_Node;
1592 -- No need to Scan past "is", Prj.Dect.Parse will do it
1595 (In_Tree => In_Tree,
1596 Declarations => Project_Declaration,
1597 Current_Project => Project,
1598 Extends => Extended_Project,
1599 Packages_To_Check => Packages_To_Check);
1600 Set_Project_Declaration_Of (Project, In_Tree, Project_Declaration);
1602 if Present (Extended_Project)
1603 and then Project_Qualifier_Of (Extended_Project, In_Tree) /= Dry
1605 Set_Extending_Project_Of
1606 (Project_Declaration_Of (Extended_Project, In_Tree), In_Tree,
1611 Expect (Tok_End, "END");
1612 Remove_Next_End_Node;
1614 -- Skip "end" if present
1616 if Token = Tok_End then
1624 -- Store the name following "end" in the Buffer. The name may be made of
1625 -- several simple names.
1628 Expect (Tok_Identifier, "identifier
");
1630 -- If we don't have an identifier, clear the buffer before exiting to
1631 -- avoid checking the name.
1633 if Token /= Tok_Identifier then
1638 -- Add the identifier to the Buffer
1639 Get_Name_String (Token_Name);
1640 Add_To_Buffer (Name_Buffer (1 .. Name_Len), Buffer, Buffer_Last);
1642 -- Scan past the identifier
1645 exit when Token /= Tok_Dot;
1646 Add_To_Buffer (".", Buffer, Buffer_Last);
1650 -- If we have a valid name, check if it is the name of the project
1652 if Name_Of_Project /= No_Name and then Buffer_Last > 0 then
1653 if To_Lower (Buffer (1 .. Buffer_Last)) /=
1654 Get_Name_String (Name_Of (Project, In_Tree))
1656 -- Invalid name: report an error
1658 Error_Msg ("expected
""" &
1659 Get_Name_String (Name_Of (Project, In_Tree)) & """",
1664 Expect (Tok_Semicolon, "`
;`
");
1666 -- Check that there is no more text following the end of the project
1669 if Token = Tok_Semicolon then
1670 Set_Previous_End_Node (Project);
1673 if Token /= Tok_EOF then
1675 ("unexpected text following
end of project
", Token_Ptr);
1679 if not Duplicated and then Name_Of_Project /= No_Name then
1681 -- Add the name of the project to the hash table, so that we can
1682 -- check that no other subsequent project will have the same name.
1684 Tree_Private_Part.Projects_Htable.Set
1685 (T => In_Tree.Projects_HT,
1686 K => Name_Of_Project,
1687 E => (Name => Name_Of_Project,
1689 Canonical_Path => Canonical_Path_Name,
1690 Extended => Extended,
1691 Proj_Qualifier => Proj_Qualifier));
1695 From_Ext : Extension_Origin := None;
1698 -- Extending_All is always propagated
1700 if From_Extended = Extending_All or else Extends_All then
1701 From_Ext := Extending_All;
1703 -- Otherwise, From_Extended is set to Extending_Single if the
1704 -- current project is an extending project.
1707 From_Ext := Extending_Simple;
1710 Post_Parse_Context_Clause
1711 (In_Tree => In_Tree,
1712 Context_Clause => First_With,
1713 Limited_Withs => True,
1714 Imported_Projects => Imported_Projects,
1715 Project_Directory => Project_Directory,
1716 From_Extended => From_Ext,
1717 In_Limited => In_Limited,
1718 Packages_To_Check => Packages_To_Check,
1720 Current_Dir => Current_Dir);
1721 Set_First_With_Clause_Of (Project, In_Tree, Imported_Projects);
1724 -- Restore the scan state, in case we are not the main project
1726 Restore_Project_Scan_State (Project_Scan_State);
1728 -- And remove the project from the project stack
1730 Project_Stack.Decrement_Last;
1732 -- Indicate if there are unkept comments
1734 Tree.Set_Project_File_Includes_Unkept_Comments
1737 To => Tree.There_Are_Unkept_Comments);
1739 -- And restore the comment state that was saved
1741 Tree.Restore_And_Free (Project_Comment_State);
1742 end Parse_Single_Project;
1744 -----------------------
1745 -- Project_Name_From --
1746 -----------------------
1748 function Project_Name_From (Path_Name : String) return Name_Id is
1749 Canonical : String (1 .. Path_Name'Length) := Path_Name;
1750 First : Natural := Canonical'Last;
1751 Last : Natural := First;
1755 if Current_Verbosity = High then
1756 Write_Str ("Project_Name_From
(""");
1757 Write_Str (Canonical);
1761 -- If the path name is empty, return No_Name to indicate failure
1767 Canonical_Case_File_Name (Canonical);
1769 -- Look for the last dot in the path name
1773 Canonical (First) /= '.'
1778 -- If we have a dot, check that it is followed by the correct extension
1780 if First > 0 and then Canonical (First) = '.' then
1781 if (not In_Configuration
1782 and then Canonical (First .. Last) = Project_File_Extension
1783 and then First /= 1)
1787 Canonical (First .. Last) = Config_Project_File_Extension
1788 and then First /= 1)
1790 -- Look for the last directory separator, if any
1795 and then Canonical (First) /= '/'
1796 and then Canonical (First) /= Dir_Sep
1802 -- Not the correct extension, return No_Name to indicate failure
1807 -- If no dot in the path name, return No_Name to indicate failure
1815 -- If the extension is the file name, return No_Name to indicate failure
1817 if First > Last then
1821 -- Put the name in lower case into Name_Buffer
1823 Name_Len := Last - First + 1;
1824 Name_Buffer (1 .. Name_Len) := To_Lower (Canonical (First .. Last));
1828 -- Check if it is a well formed project name. Return No_Name if it is
1832 if not Is_Letter (Name_Buffer (Index)) then
1839 exit when Index >= Name_Len;
1841 if Name_Buffer (Index) = '_' then
1842 if Name_Buffer (Index + 1) = '_' then
1847 exit when Name_Buffer (Index) = '-';
1849 if Name_Buffer (Index) /= '_'
1850 and then not Is_Alphanumeric (Name_Buffer (Index))
1858 if Index >= Name_Len then
1859 if Is_Alphanumeric (Name_Buffer (Name_Len)) then
1861 -- All checks have succeeded. Return name in Name_Buffer
1869 elsif Name_Buffer (Index) = '-' then
1873 end Project_Name_From;
1875 --------------------------
1876 -- Project_Path_Name_Of --
1877 --------------------------
1879 function Project_Path_Name_Of
1880 (Project_File_Name : String;
1881 Directory : String) return String
1884 function Try_Path_Name (Path : String) return String_Access;
1885 pragma Inline (Try_Path_Name);
1886 -- Try the specified Path
1892 function Try_Path_Name (Path : String) return String_Access is
1893 Prj_Path : constant String := Project_Path;
1896 Result : String_Access := null;
1899 if Current_Verbosity = High then
1900 Write_Str (" Trying
");
1904 if Is_Absolute_Path (Path) then
1905 if Is_Regular_File (Path) then
1906 Result := new String'(Path);
1910 -- Because we don't want to resolve symbolic links, we cannot use
1911 -- Locate_Regular_File. So, we try each possible path
1914 First := Prj_Path'First;
1915 while First <= Prj_Path'Last loop
1916 while First <= Prj_Path'Last
1917 and then Prj_Path (First) = Path_Separator
1922 exit when First > Prj_Path'Last;
1925 while Last < Prj_Path'Last
1926 and then Prj_Path (Last + 1) /= Path_Separator
1933 if not Is_Absolute_Path (Prj_Path (First .. Last)) then
1934 Add_Str_To_Name_Buffer (Get_Current_Dir);
1935 Add_Char_To_Name_Buffer (Directory_Separator);
1938 Add_Str_To_Name_Buffer (Prj_Path (First .. Last));
1939 Add_Char_To_Name_Buffer (Directory_Separator);
1940 Add_Str_To_Name_Buffer (Path);
1942 if Is_Regular_File (Name_Buffer (1 .. Name_Len)) then
1943 Result := new String'(Name_Buffer (1 .. Name_Len));
1954 -- Local Declarations
1956 Result : String_Access;
1957 Result_Id : Path_Name_Type;
1958 Has_Dot : Boolean := False;
1961 -- Start of processing for Project_Path_Name_Of
1964 if Current_Verbosity = High then
1965 Write_Str ("Project_Path_Name_Of
(""");
1966 Write_Str (Project_File_Name);
1967 Write_Str (""", """);
1968 Write_Str (Directory);
1969 Write_Line (""");");
1972 -- Check the project cache
1974 Name_Len := Project_File_Name'Length;
1975 Name_Buffer (1 .. Name_Len) := Project_File_Name;
1977 Result_Id := Projects_Paths.Get (Key);
1979 if Result_Id /= No_Path then
1980 return Get_Name_String (Result_Id);
1983 -- Check if Project_File_Name contains an extension (a dot before a
1984 -- directory separator). If it is the case we do not try project file
1985 -- with an added extension as it is not possible to have multiple dots
1986 -- on a project file name.
1988 Check_Dot : for K in reverse Project_File_Name'Range loop
1989 if Project_File_Name (K) = '.' then
1994 exit Check_Dot when Project_File_Name (K) = Directory_Separator
1995 or else Project_File_Name (K) = '/';
1998 if not Is_Absolute_Path (Project_File_Name) then
2000 -- First we try <directory>/<file_name>.<extension>
2003 Result := Try_Path_Name
2004 (Directory & Directory_Separator &
2005 Project_File_Name & Project_File_Extension);
2008 -- Then we try <directory>/<file_name>
2010 if Result = null then
2011 Result := Try_Path_Name
2012 (Directory & Directory_Separator & Project_File_Name);
2016 -- Then we try <file_name>.<extension>
2018 if Result = null and then not Has_Dot then
2019 Result := Try_Path_Name (Project_File_Name & Project_File_Extension);
2022 -- Then we try <file_name>
2024 if Result = null then
2025 Result := Try_Path_Name (Project_File_Name);
2028 -- If we cannot find the project file, we return an empty string
2030 if Result = null then
2035 Final_Result : constant String :=
2036 GNAT.OS_Lib.Normalize_Pathname
2038 Directory => Directory,
2039 Resolve_Links => False,
2040 Case_Sensitive => True);
2043 Name_Len := Final_Result'Length;
2044 Name_Buffer (1 .. Name_Len) := Final_Result;
2045 Result_Id := Name_Find;
2047 Projects_Paths.Set (Key, Result_Id);
2048 return Final_Result;
2051 end Project_Path_Name_Of;