xref: /llvm-project/clang/lib/Lex/ModuleMap.cpp (revision 4dc71835f5c1fc2ce9fb04645d0c7093482c9a70)
1 //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ModuleMap implementation, which describes the layout
11 // of a module as it relates to headers.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/ModuleMap.h"
15 #include "clang/Lex/Lexer.h"
16 #include "clang/Lex/LiteralSupport.h"
17 #include "clang/Lex/LexDiagnostic.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/Basic/TargetOptions.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Support/PathV2.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/StringSwitch.h"
29 using namespace clang;
30 
31 Module::ExportDecl
32 ModuleMap::resolveExport(Module *Mod,
33                          const Module::UnresolvedExportDecl &Unresolved,
34                          bool Complain) {
35   // We may have just a wildcard.
36   if (Unresolved.Id.empty()) {
37     assert(Unresolved.Wildcard && "Invalid unresolved export");
38     return Module::ExportDecl(0, true);
39   }
40 
41   // Find the starting module.
42   Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
43   if (!Context) {
44     if (Complain)
45       Diags->Report(Unresolved.Id[0].second,
46                     diag::err_mmap_missing_module_unqualified)
47         << Unresolved.Id[0].first << Mod->getFullModuleName();
48 
49     return Module::ExportDecl();
50   }
51 
52   // Dig into the module path.
53   for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
54     Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
55                                         Context);
56     if (!Sub) {
57       if (Complain)
58         Diags->Report(Unresolved.Id[I].second,
59                       diag::err_mmap_missing_module_qualified)
60           << Unresolved.Id[I].first << Context->getFullModuleName()
61           << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
62 
63       return Module::ExportDecl();
64     }
65 
66     Context = Sub;
67   }
68 
69   return Module::ExportDecl(Context, Unresolved.Wildcard);
70 }
71 
72 ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC) {
73   llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
74   Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>(
75             new DiagnosticsEngine(DiagIDs));
76   Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
77   SourceMgr = new SourceManager(*Diags, FileMgr);
78 }
79 
80 ModuleMap::~ModuleMap() {
81   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
82                                         IEnd = Modules.end();
83        I != IEnd; ++I) {
84     delete I->getValue();
85   }
86 
87   delete SourceMgr;
88 }
89 
90 Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
91   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
92     = Headers.find(File);
93   if (Known != Headers.end())
94     return Known->second;
95 
96   const DirectoryEntry *Dir = File->getDir();
97   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
98   StringRef DirName = Dir->getName();
99 
100   // Keep walking up the directory hierarchy, looking for a directory with
101   // an umbrella header.
102   do {
103     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
104       = UmbrellaDirs.find(Dir);
105     if (KnownDir != UmbrellaDirs.end()) {
106       Module *Result = KnownDir->second;
107 
108       // Search up the module stack until we find a module with an umbrella
109       // directory.
110       Module *UmbrellaModule = Result;
111       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
112         UmbrellaModule = UmbrellaModule->Parent;
113 
114       if (UmbrellaModule->InferSubmodules) {
115         // Infer submodules for each of the directories we found between
116         // the directory of the umbrella header and the directory where
117         // the actual header is located.
118         bool Explicit = UmbrellaModule->InferExplicitSubmodules;
119 
120         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
121           // Find or create the module that corresponds to this directory name.
122           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
123           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
124                                       Explicit).first;
125 
126           // Associate the module and the directory.
127           UmbrellaDirs[SkippedDirs[I-1]] = Result;
128 
129           // If inferred submodules export everything they import, add a
130           // wildcard to the set of exports.
131           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
132             Result->Exports.push_back(Module::ExportDecl(0, true));
133         }
134 
135         // Infer a submodule with the same name as this header file.
136         StringRef Name = llvm::sys::path::stem(File->getName());
137         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
138                                     Explicit).first;
139 
140         // If inferred submodules export everything they import, add a
141         // wildcard to the set of exports.
142         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
143           Result->Exports.push_back(Module::ExportDecl(0, true));
144       } else {
145         // Record each of the directories we stepped through as being part of
146         // the module we found, since the umbrella header covers them all.
147         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
148           UmbrellaDirs[SkippedDirs[I]] = Result;
149       }
150 
151       Headers[File] = Result;
152       return Result;
153     }
154 
155     SkippedDirs.push_back(Dir);
156 
157     // Retrieve our parent path.
158     DirName = llvm::sys::path::parent_path(DirName);
159     if (DirName.empty())
160       break;
161 
162     // Resolve the parent path to a directory entry.
163     Dir = SourceMgr->getFileManager().getDirectory(DirName);
164   } while (Dir);
165 
166   return 0;
167 }
168 
169 Module *ModuleMap::findModule(StringRef Name) {
170   llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
171   if (Known != Modules.end())
172     return Known->getValue();
173 
174   return 0;
175 }
176 
177 Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
178   for(; Context; Context = Context->Parent) {
179     if (Module *Sub = lookupModuleQualified(Name, Context))
180       return Sub;
181   }
182 
183   return findModule(Name);
184 }
185 
186 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
187   if (!Context)
188     return findModule(Name);
189 
190   llvm::StringMap<Module *>::iterator Sub = Context->SubModules.find(Name);
191   if (Sub != Context->SubModules.end())
192     return Sub->getValue();
193 
194   return 0;
195 }
196 
197 std::pair<Module *, bool>
198 ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
199                               bool IsExplicit) {
200   // Try to find an existing module with this name.
201   if (Module *Found = Parent? Parent->SubModules[Name] : Modules[Name])
202     return std::make_pair(Found, false);
203 
204   // Create a new module with this name.
205   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
206                               IsExplicit);
207   if (Parent)
208     Parent->SubModules[Name] = Result;
209   else
210     Modules[Name] = Result;
211   return std::make_pair(Result, true);
212 }
213 
214 Module *
215 ModuleMap::inferFrameworkModule(StringRef ModuleName,
216                                 const DirectoryEntry *FrameworkDir,
217                                 Module *Parent) {
218   // Check whether we've already found this module.
219   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
220     return Mod;
221 
222   FileManager &FileMgr = SourceMgr->getFileManager();
223 
224   // Look for an umbrella header.
225   llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
226   llvm::sys::path::append(UmbrellaName, "Headers");
227   llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
228   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
229 
230   // FIXME: If there's no umbrella header, we could probably scan the
231   // framework to load *everything*. But, it's not clear that this is a good
232   // idea.
233   if (!UmbrellaHeader)
234     return 0;
235 
236   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
237                               /*IsFramework=*/true, /*IsExplicit=*/false);
238 
239   if (Parent)
240     Parent->SubModules[ModuleName] = Result;
241   else
242     Modules[ModuleName] = Result;
243 
244   // umbrella header "umbrella-header-name"
245   Result->Umbrella = UmbrellaHeader;
246   Headers[UmbrellaHeader] = Result;
247   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
248 
249   // export *
250   Result->Exports.push_back(Module::ExportDecl(0, true));
251 
252   // module * { export * }
253   Result->InferSubmodules = true;
254   Result->InferExportWildcard = true;
255 
256   // Look for subframeworks.
257   llvm::error_code EC;
258   llvm::SmallString<128> SubframeworksDirName
259     = StringRef(FrameworkDir->getName());
260   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
261   llvm::SmallString<128> SubframeworksDirNameNative;
262   llvm::sys::path::native(SubframeworksDirName.str(),
263                           SubframeworksDirNameNative);
264   for (llvm::sys::fs::directory_iterator
265          Dir(SubframeworksDirNameNative.str(), EC), DirEnd;
266        Dir != DirEnd && !EC; Dir.increment(EC)) {
267     if (!StringRef(Dir->path()).endswith(".framework"))
268       continue;
269 
270     if (const DirectoryEntry *SubframeworkDir
271           = FileMgr.getDirectory(Dir->path())) {
272       // FIXME: Do we want to warn about subframeworks without umbrella headers?
273       inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
274                            Result);
275     }
276   }
277 
278   // Look for private headers.
279   llvm::SmallString<128> PrivateHeadersDirName(FrameworkDir->getName());
280   llvm::sys::path::append(PrivateHeadersDirName, "PrivateHeaders");
281   if (const DirectoryEntry *Dir = FileMgr.getDirectory(PrivateHeadersDirName)) {
282     Module *Private = findOrCreateModule("Private", Result,
283                                          /*IsFramework=*/false,
284                                          /*IsExplicit=*/true).first;
285     setUmbrellaDir(Private, Dir);
286     Private->InferSubmodules = true;
287     Private->InferExplicitSubmodules = true;
288     Private->InferExportWildcard = true;
289   }
290 
291   return Result;
292 }
293 
294 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
295   Headers[UmbrellaHeader] = Mod;
296   Mod->Umbrella = UmbrellaHeader;
297   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
298 }
299 
300 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
301   Mod->Umbrella = UmbrellaDir;
302   UmbrellaDirs[UmbrellaDir] = Mod;
303 }
304 
305 void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
306   Mod->Headers.push_back(Header);
307   Headers[Header] = Mod;
308 }
309 
310 const FileEntry *
311 ModuleMap::getContainingModuleMapFile(Module *Module) {
312   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
313     return 0;
314 
315   return SourceMgr->getFileEntryForID(
316            SourceMgr->getFileID(Module->DefinitionLoc));
317 }
318 
319 void ModuleMap::dump() {
320   llvm::errs() << "Modules:";
321   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
322                                         MEnd = Modules.end();
323        M != MEnd; ++M)
324     M->getValue()->print(llvm::errs(), 2);
325 
326   llvm::errs() << "Headers:";
327   for (llvm::DenseMap<const FileEntry *, Module *>::iterator
328             H = Headers.begin(),
329          HEnd = Headers.end();
330        H != HEnd; ++H) {
331     llvm::errs() << "  \"" << H->first->getName() << "\" -> "
332                  << H->second->getFullModuleName() << "\n";
333   }
334 }
335 
336 bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
337   bool HadError = false;
338   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
339     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
340                                               Complain);
341     if (Export.getPointer() || Export.getInt())
342       Mod->Exports.push_back(Export);
343     else
344       HadError = true;
345   }
346   Mod->UnresolvedExports.clear();
347   return HadError;
348 }
349 
350 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
351   if (Loc.isInvalid())
352     return 0;
353 
354   // Use the expansion location to determine which module we're in.
355   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
356   if (!ExpansionLoc.isFileID())
357     return 0;
358 
359 
360   const SourceManager &SrcMgr = Loc.getManager();
361   FileID ExpansionFileID = ExpansionLoc.getFileID();
362   const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID);
363   if (!ExpansionFile)
364     return 0;
365 
366   // Find the module that owns this header.
367   return findModuleForHeader(ExpansionFile);
368 }
369 
370 //----------------------------------------------------------------------------//
371 // Module map file parser
372 //----------------------------------------------------------------------------//
373 
374 namespace clang {
375   /// \brief A token in a module map file.
376   struct MMToken {
377     enum TokenKind {
378       EndOfFile,
379       HeaderKeyword,
380       Identifier,
381       ExplicitKeyword,
382       ExportKeyword,
383       FrameworkKeyword,
384       ModuleKeyword,
385       Period,
386       UmbrellaKeyword,
387       Star,
388       StringLiteral,
389       LBrace,
390       RBrace
391     } Kind;
392 
393     unsigned Location;
394     unsigned StringLength;
395     const char *StringData;
396 
397     void clear() {
398       Kind = EndOfFile;
399       Location = 0;
400       StringLength = 0;
401       StringData = 0;
402     }
403 
404     bool is(TokenKind K) const { return Kind == K; }
405 
406     SourceLocation getLocation() const {
407       return SourceLocation::getFromRawEncoding(Location);
408     }
409 
410     StringRef getString() const {
411       return StringRef(StringData, StringLength);
412     }
413   };
414 
415   class ModuleMapParser {
416     Lexer &L;
417     SourceManager &SourceMgr;
418     DiagnosticsEngine &Diags;
419     ModuleMap &Map;
420 
421     /// \brief The directory that this module map resides in.
422     const DirectoryEntry *Directory;
423 
424     /// \brief Whether an error occurred.
425     bool HadError;
426 
427     /// \brief Default target information, used only for string literal
428     /// parsing.
429     TargetInfo *Target;
430 
431     /// \brief Stores string data for the various string literals referenced
432     /// during parsing.
433     llvm::BumpPtrAllocator StringData;
434 
435     /// \brief The current token.
436     MMToken Tok;
437 
438     /// \brief The active module.
439     Module *ActiveModule;
440 
441     /// \brief Consume the current token and return its location.
442     SourceLocation consumeToken();
443 
444     /// \brief Skip tokens until we reach the a token with the given kind
445     /// (or the end of the file).
446     void skipUntil(MMToken::TokenKind K);
447 
448     typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
449       ModuleId;
450     bool parseModuleId(ModuleId &Id);
451     void parseModuleDecl();
452     void parseHeaderDecl(SourceLocation UmbrellaLoc);
453     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
454     void parseExportDecl();
455     void parseInferredSubmoduleDecl(bool Explicit);
456 
457     const DirectoryEntry *getOverriddenHeaderSearchDir();
458 
459   public:
460     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
461                              DiagnosticsEngine &Diags,
462                              ModuleMap &Map,
463                              const DirectoryEntry *Directory)
464       : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
465         Directory(Directory), HadError(false), ActiveModule(0)
466     {
467       TargetOptions TargetOpts;
468       TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
469       Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
470 
471       Tok.clear();
472       consumeToken();
473     }
474 
475     bool parseModuleMapFile();
476   };
477 }
478 
479 SourceLocation ModuleMapParser::consumeToken() {
480 retry:
481   SourceLocation Result = Tok.getLocation();
482   Tok.clear();
483 
484   Token LToken;
485   L.LexFromRawLexer(LToken);
486   Tok.Location = LToken.getLocation().getRawEncoding();
487   switch (LToken.getKind()) {
488   case tok::raw_identifier:
489     Tok.StringData = LToken.getRawIdentifierData();
490     Tok.StringLength = LToken.getLength();
491     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
492                  .Case("header", MMToken::HeaderKeyword)
493                  .Case("explicit", MMToken::ExplicitKeyword)
494                  .Case("export", MMToken::ExportKeyword)
495                  .Case("framework", MMToken::FrameworkKeyword)
496                  .Case("module", MMToken::ModuleKeyword)
497                  .Case("umbrella", MMToken::UmbrellaKeyword)
498                  .Default(MMToken::Identifier);
499     break;
500 
501   case tok::eof:
502     Tok.Kind = MMToken::EndOfFile;
503     break;
504 
505   case tok::l_brace:
506     Tok.Kind = MMToken::LBrace;
507     break;
508 
509   case tok::period:
510     Tok.Kind = MMToken::Period;
511     break;
512 
513   case tok::r_brace:
514     Tok.Kind = MMToken::RBrace;
515     break;
516 
517   case tok::star:
518     Tok.Kind = MMToken::Star;
519     break;
520 
521   case tok::string_literal: {
522     // Parse the string literal.
523     LangOptions LangOpts;
524     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
525     if (StringLiteral.hadError)
526       goto retry;
527 
528     // Copy the string literal into our string data allocator.
529     unsigned Length = StringLiteral.GetStringLength();
530     char *Saved = StringData.Allocate<char>(Length + 1);
531     memcpy(Saved, StringLiteral.GetString().data(), Length);
532     Saved[Length] = 0;
533 
534     // Form the token.
535     Tok.Kind = MMToken::StringLiteral;
536     Tok.StringData = Saved;
537     Tok.StringLength = Length;
538     break;
539   }
540 
541   case tok::comment:
542     goto retry;
543 
544   default:
545     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
546     HadError = true;
547     goto retry;
548   }
549 
550   return Result;
551 }
552 
553 void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
554   unsigned braceDepth = 0;
555   do {
556     switch (Tok.Kind) {
557     case MMToken::EndOfFile:
558       return;
559 
560     case MMToken::LBrace:
561       if (Tok.is(K) && braceDepth == 0)
562         return;
563 
564       ++braceDepth;
565       break;
566 
567     case MMToken::RBrace:
568       if (braceDepth > 0)
569         --braceDepth;
570       else if (Tok.is(K))
571         return;
572       break;
573 
574     default:
575       if (braceDepth == 0 && Tok.is(K))
576         return;
577       break;
578     }
579 
580    consumeToken();
581   } while (true);
582 }
583 
584 /// \brief Parse a module-id.
585 ///
586 ///   module-id:
587 ///     identifier
588 ///     identifier '.' module-id
589 ///
590 /// \returns true if an error occurred, false otherwise.
591 bool ModuleMapParser::parseModuleId(ModuleId &Id) {
592   Id.clear();
593   do {
594     if (Tok.is(MMToken::Identifier)) {
595       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
596       consumeToken();
597     } else {
598       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
599       return true;
600     }
601 
602     if (!Tok.is(MMToken::Period))
603       break;
604 
605     consumeToken();
606   } while (true);
607 
608   return false;
609 }
610 
611 /// \brief Parse a module declaration.
612 ///
613 ///   module-declaration:
614 ///     'explicit'[opt] 'framework'[opt] 'module' module-id { module-member* }
615 ///
616 ///   module-member:
617 ///     header-declaration
618 ///     submodule-declaration
619 ///     export-declaration
620 ///
621 ///   submodule-declaration:
622 ///     module-declaration
623 ///     inferred-submodule-declaration
624 void ModuleMapParser::parseModuleDecl() {
625   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
626          Tok.is(MMToken::FrameworkKeyword));
627   // Parse 'explicit' or 'framework' keyword, if present.
628   SourceLocation ExplicitLoc;
629   bool Explicit = false;
630   bool Framework = false;
631 
632   // Parse 'explicit' keyword, if present.
633   if (Tok.is(MMToken::ExplicitKeyword)) {
634     ExplicitLoc = consumeToken();
635     Explicit = true;
636   }
637 
638   // Parse 'framework' keyword, if present.
639   if (Tok.is(MMToken::FrameworkKeyword)) {
640     consumeToken();
641     Framework = true;
642   }
643 
644   // Parse 'module' keyword.
645   if (!Tok.is(MMToken::ModuleKeyword)) {
646     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
647     consumeToken();
648     HadError = true;
649     return;
650   }
651   consumeToken(); // 'module' keyword
652 
653   // If we have a wildcard for the module name, this is an inferred submodule.
654   // Parse it.
655   if (Tok.is(MMToken::Star))
656     return parseInferredSubmoduleDecl(Explicit);
657 
658   // Parse the module name.
659   ModuleId Id;
660   if (parseModuleId(Id)) {
661     HadError = true;
662     return;
663   }
664 
665   if (ActiveModule) {
666     if (Id.size() > 1) {
667       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
668         << SourceRange(Id.front().second, Id.back().second);
669 
670       HadError = true;
671       return;
672     }
673   } else if (Id.size() == 1 && Explicit) {
674     // Top-level modules can't be explicit.
675     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
676     Explicit = false;
677     ExplicitLoc = SourceLocation();
678     HadError = true;
679   }
680 
681   Module *PreviousActiveModule = ActiveModule;
682   if (Id.size() > 1) {
683     // This module map defines a submodule. Go find the module of which it
684     // is a submodule.
685     ActiveModule = 0;
686     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
687       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
688         ActiveModule = Next;
689         continue;
690       }
691 
692       if (ActiveModule) {
693         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
694           << Id[I].first << ActiveModule->getTopLevelModule();
695       } else {
696         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
697       }
698       HadError = true;
699       return;
700     }
701   }
702 
703   StringRef ModuleName = Id.back().first;
704   SourceLocation ModuleNameLoc = Id.back().second;
705 
706   // Parse the opening brace.
707   if (!Tok.is(MMToken::LBrace)) {
708     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
709       << ModuleName;
710     HadError = true;
711     return;
712   }
713   SourceLocation LBraceLoc = consumeToken();
714 
715   // Determine whether this (sub)module has already been defined.
716   llvm::StringMap<Module *> &ModuleSpace
717     = ActiveModule? ActiveModule->SubModules : Map.Modules;
718   llvm::StringMap<Module *>::iterator ExistingModule
719     = ModuleSpace.find(ModuleName);
720   if (ExistingModule != ModuleSpace.end()) {
721     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
722       << ModuleName;
723     Diags.Report(ExistingModule->getValue()->DefinitionLoc,
724                  diag::note_mmap_prev_definition);
725 
726     // Skip the module definition.
727     skipUntil(MMToken::RBrace);
728     if (Tok.is(MMToken::RBrace))
729       consumeToken();
730 
731     HadError = true;
732     return;
733   }
734 
735   // Start defining this module.
736   ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Framework,
737                             Explicit);
738   ModuleSpace[ModuleName] = ActiveModule;
739 
740   bool Done = false;
741   do {
742     switch (Tok.Kind) {
743     case MMToken::EndOfFile:
744     case MMToken::RBrace:
745       Done = true;
746       break;
747 
748     case MMToken::ExplicitKeyword:
749     case MMToken::FrameworkKeyword:
750     case MMToken::ModuleKeyword:
751       parseModuleDecl();
752       break;
753 
754     case MMToken::ExportKeyword:
755       parseExportDecl();
756       break;
757 
758     case MMToken::UmbrellaKeyword: {
759       SourceLocation UmbrellaLoc = consumeToken();
760       if (Tok.is(MMToken::HeaderKeyword))
761         parseHeaderDecl(UmbrellaLoc);
762       else
763         parseUmbrellaDirDecl(UmbrellaLoc);
764       break;
765     }
766 
767     case MMToken::HeaderKeyword:
768       parseHeaderDecl(SourceLocation());
769       break;
770 
771     default:
772       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
773       consumeToken();
774       break;
775     }
776   } while (!Done);
777 
778   if (Tok.is(MMToken::RBrace))
779     consumeToken();
780   else {
781     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
782     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
783     HadError = true;
784   }
785 
786   // We're done parsing this module. Pop back to the previous module.
787   ActiveModule = PreviousActiveModule;
788 }
789 
790 /// \brief Append to \p Paths the set of paths needed to get to the
791 /// subframework in which the given module lives.
792 void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) {
793   // Collect the framework names from the given module to the top-level module.
794   llvm::SmallVector<StringRef, 2> Paths;
795   for (; Mod; Mod = Mod->Parent) {
796     if (Mod->IsFramework)
797       Paths.push_back(Mod->Name);
798   }
799 
800   if (Paths.empty())
801     return;
802 
803   // Add Frameworks/Name.framework for each subframework.
804   for (unsigned I = Paths.size() - 1; I != 0; --I) {
805     llvm::sys::path::append(Path, "Frameworks");
806     llvm::sys::path::append(Path, Paths[I-1] + ".framework");
807   }
808 }
809 
810 /// \brief Parse a header declaration.
811 ///
812 ///   header-declaration:
813 ///     'umbrella'[opt] 'header' string-literal
814 void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) {
815   assert(Tok.is(MMToken::HeaderKeyword));
816   consumeToken();
817 
818   bool Umbrella = UmbrellaLoc.isValid();
819 
820   // Parse the header name.
821   if (!Tok.is(MMToken::StringLiteral)) {
822     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
823       << "header";
824     HadError = true;
825     return;
826   }
827   std::string FileName = Tok.getString();
828   SourceLocation FileNameLoc = consumeToken();
829 
830   // Check whether we already have an umbrella.
831   if (Umbrella && ActiveModule->Umbrella) {
832     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
833       << ActiveModule->getFullModuleName();
834     HadError = true;
835     return;
836   }
837 
838   // Look for this file.
839   const FileEntry *File = 0;
840   llvm::SmallString<128> PathName;
841   if (llvm::sys::path::is_absolute(FileName)) {
842     PathName = FileName;
843     File = SourceMgr.getFileManager().getFile(PathName);
844   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
845     PathName = Dir->getName();
846     llvm::sys::path::append(PathName, FileName);
847     File = SourceMgr.getFileManager().getFile(PathName);
848   } else {
849     // Search for the header file within the search directory.
850     PathName = Directory->getName();
851     unsigned PathLength = PathName.size();
852 
853     if (ActiveModule->isPartOfFramework()) {
854       appendSubframeworkPaths(ActiveModule, PathName);
855 
856       // Check whether this file is in the public headers.
857       llvm::sys::path::append(PathName, "Headers");
858       llvm::sys::path::append(PathName, FileName);
859       File = SourceMgr.getFileManager().getFile(PathName);
860 
861       if (!File) {
862         // Check whether this file is in the private headers.
863         PathName.resize(PathLength);
864         llvm::sys::path::append(PathName, "PrivateHeaders");
865         llvm::sys::path::append(PathName, FileName);
866         File = SourceMgr.getFileManager().getFile(PathName);
867       }
868     } else {
869       // Lookup for normal headers.
870       llvm::sys::path::append(PathName, FileName);
871       File = SourceMgr.getFileManager().getFile(PathName);
872     }
873   }
874 
875   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
876   // Come up with a lazy way to do this.
877   if (File) {
878     if (const Module *OwningModule = Map.Headers[File]) {
879       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
880         << FileName << OwningModule->getFullModuleName();
881       HadError = true;
882     } else if (Umbrella) {
883       const DirectoryEntry *UmbrellaDir = File->getDir();
884       if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
885         Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
886           << OwningModule->getFullModuleName();
887         HadError = true;
888       } else {
889         // Record this umbrella header.
890         Map.setUmbrellaHeader(ActiveModule, File);
891       }
892     } else {
893       // Record this header.
894       Map.addHeader(ActiveModule, File);
895     }
896   } else {
897     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
898       << Umbrella << FileName;
899     HadError = true;
900   }
901 }
902 
903 /// \brief Parse an umbrella directory declaration.
904 ///
905 ///   umbrella-dir-declaration:
906 ///     umbrella string-literal
907 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
908   // Parse the directory name.
909   if (!Tok.is(MMToken::StringLiteral)) {
910     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
911       << "umbrella";
912     HadError = true;
913     return;
914   }
915 
916   std::string DirName = Tok.getString();
917   SourceLocation DirNameLoc = consumeToken();
918 
919   // Check whether we already have an umbrella.
920   if (ActiveModule->Umbrella) {
921     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
922       << ActiveModule->getFullModuleName();
923     HadError = true;
924     return;
925   }
926 
927   // Look for this file.
928   const DirectoryEntry *Dir = 0;
929   if (llvm::sys::path::is_absolute(DirName))
930     Dir = SourceMgr.getFileManager().getDirectory(DirName);
931   else {
932     llvm::SmallString<128> PathName;
933     PathName = Directory->getName();
934     llvm::sys::path::append(PathName, DirName);
935     Dir = SourceMgr.getFileManager().getDirectory(PathName);
936   }
937 
938   if (!Dir) {
939     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
940       << DirName;
941     HadError = true;
942     return;
943   }
944 
945   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
946     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
947       << OwningModule->getFullModuleName();
948     HadError = true;
949     return;
950   }
951 
952   // Record this umbrella directory.
953   Map.setUmbrellaDir(ActiveModule, Dir);
954 }
955 
956 /// \brief Parse a module export declaration.
957 ///
958 ///   export-declaration:
959 ///     'export' wildcard-module-id
960 ///
961 ///   wildcard-module-id:
962 ///     identifier
963 ///     '*'
964 ///     identifier '.' wildcard-module-id
965 void ModuleMapParser::parseExportDecl() {
966   assert(Tok.is(MMToken::ExportKeyword));
967   SourceLocation ExportLoc = consumeToken();
968 
969   // Parse the module-id with an optional wildcard at the end.
970   ModuleId ParsedModuleId;
971   bool Wildcard = false;
972   do {
973     if (Tok.is(MMToken::Identifier)) {
974       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
975                                               Tok.getLocation()));
976       consumeToken();
977 
978       if (Tok.is(MMToken::Period)) {
979         consumeToken();
980         continue;
981       }
982 
983       break;
984     }
985 
986     if(Tok.is(MMToken::Star)) {
987       Wildcard = true;
988       consumeToken();
989       break;
990     }
991 
992     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
993     HadError = true;
994     return;
995   } while (true);
996 
997   Module::UnresolvedExportDecl Unresolved = {
998     ExportLoc, ParsedModuleId, Wildcard
999   };
1000   ActiveModule->UnresolvedExports.push_back(Unresolved);
1001 }
1002 
1003 void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
1004   assert(Tok.is(MMToken::Star));
1005   SourceLocation StarLoc = consumeToken();
1006   bool Failed = false;
1007 
1008   // Inferred modules must be submodules.
1009   if (!ActiveModule) {
1010     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
1011     Failed = true;
1012   }
1013 
1014   // Inferred modules must have umbrella directories.
1015   if (!Failed && !ActiveModule->getUmbrellaDir()) {
1016     Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
1017     Failed = true;
1018   }
1019 
1020   // Check for redefinition of an inferred module.
1021   if (!Failed && ActiveModule->InferSubmodules) {
1022     Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1023     if (ActiveModule->InferredSubmoduleLoc.isValid())
1024       Diags.Report(ActiveModule->InferredSubmoduleLoc,
1025                    diag::note_mmap_prev_definition);
1026     Failed = true;
1027   }
1028 
1029   // If there were any problems with this inferred submodule, skip its body.
1030   if (Failed) {
1031     if (Tok.is(MMToken::LBrace)) {
1032       consumeToken();
1033       skipUntil(MMToken::RBrace);
1034       if (Tok.is(MMToken::RBrace))
1035         consumeToken();
1036     }
1037     HadError = true;
1038     return;
1039   }
1040 
1041   // Note that we have an inferred submodule.
1042   ActiveModule->InferSubmodules = true;
1043   ActiveModule->InferredSubmoduleLoc = StarLoc;
1044   ActiveModule->InferExplicitSubmodules = Explicit;
1045 
1046   // Parse the opening brace.
1047   if (!Tok.is(MMToken::LBrace)) {
1048     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
1049     HadError = true;
1050     return;
1051   }
1052   SourceLocation LBraceLoc = consumeToken();
1053 
1054   // Parse the body of the inferred submodule.
1055   bool Done = false;
1056   do {
1057     switch (Tok.Kind) {
1058     case MMToken::EndOfFile:
1059     case MMToken::RBrace:
1060       Done = true;
1061       break;
1062 
1063     case MMToken::ExportKeyword: {
1064       consumeToken();
1065       if (Tok.is(MMToken::Star))
1066         ActiveModule->InferExportWildcard = true;
1067       else
1068         Diags.Report(Tok.getLocation(),
1069                      diag::err_mmap_expected_export_wildcard);
1070       consumeToken();
1071       break;
1072     }
1073 
1074     case MMToken::ExplicitKeyword:
1075     case MMToken::ModuleKeyword:
1076     case MMToken::HeaderKeyword:
1077     case MMToken::UmbrellaKeyword:
1078     default:
1079       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
1080       consumeToken();
1081       break;
1082     }
1083   } while (!Done);
1084 
1085   if (Tok.is(MMToken::RBrace))
1086     consumeToken();
1087   else {
1088     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1089     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1090     HadError = true;
1091   }
1092 }
1093 
1094 /// \brief If there is a specific header search directory due the presence
1095 /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
1096 const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
1097   for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
1098     // If we have an umbrella directory, use that.
1099     if (Mod->hasUmbrellaDir())
1100       return Mod->getUmbrellaDir();
1101 
1102     // If we have a framework directory, stop looking.
1103     if (Mod->IsFramework)
1104       return 0;
1105   }
1106 
1107   return 0;
1108 }
1109 
1110 /// \brief Parse a module map file.
1111 ///
1112 ///   module-map-file:
1113 ///     module-declaration*
1114 bool ModuleMapParser::parseModuleMapFile() {
1115   do {
1116     switch (Tok.Kind) {
1117     case MMToken::EndOfFile:
1118       return HadError;
1119 
1120     case MMToken::ExplicitKeyword:
1121     case MMToken::ModuleKeyword:
1122     case MMToken::FrameworkKeyword:
1123       parseModuleDecl();
1124       break;
1125 
1126     case MMToken::ExportKeyword:
1127     case MMToken::HeaderKeyword:
1128     case MMToken::Identifier:
1129     case MMToken::LBrace:
1130     case MMToken::Period:
1131     case MMToken::RBrace:
1132     case MMToken::Star:
1133     case MMToken::StringLiteral:
1134     case MMToken::UmbrellaKeyword:
1135       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1136       HadError = true;
1137       consumeToken();
1138       break;
1139     }
1140   } while (true);
1141 
1142   return HadError;
1143 }
1144 
1145 bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
1146   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1147   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1148   if (!Buffer)
1149     return true;
1150 
1151   // Parse this module map file.
1152   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts);
1153   Diags->getClient()->BeginSourceFile(LangOpts);
1154   ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir());
1155   bool Result = Parser.parseModuleMapFile();
1156   Diags->getClient()->EndSourceFile();
1157 
1158   return Result;
1159 }
1160