xref: /llvm-project/clang/lib/Lex/ModuleMap.cpp (revision feb54b6ded123f8118fdc20620d3f657dfeab485)
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/Basic/CharInfo.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/DiagnosticOptions.h"
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "clang/Basic/TargetOptions.h"
21 #include "clang/Lex/HeaderSearch.h"
22 #include "clang/Lex/LexDiagnostic.h"
23 #include "clang/Lex/Lexer.h"
24 #include "clang/Lex/LiteralSupport.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/Host.h"
30 #include "llvm/Support/Path.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <stdlib.h>
33 #if defined(LLVM_ON_UNIX)
34 #include <limits.h>
35 #endif
36 using namespace clang;
37 
38 Module::ExportDecl
39 ModuleMap::resolveExport(Module *Mod,
40                          const Module::UnresolvedExportDecl &Unresolved,
41                          bool Complain) const {
42   // We may have just a wildcard.
43   if (Unresolved.Id.empty()) {
44     assert(Unresolved.Wildcard && "Invalid unresolved export");
45     return Module::ExportDecl(nullptr, true);
46   }
47 
48   // Resolve the module-id.
49   Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain);
50   if (!Context)
51     return Module::ExportDecl();
52 
53   return Module::ExportDecl(Context, Unresolved.Wildcard);
54 }
55 
56 Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod,
57                                    bool Complain) const {
58   // Find the starting module.
59   Module *Context = lookupModuleUnqualified(Id[0].first, Mod);
60   if (!Context) {
61     if (Complain)
62       Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified)
63       << Id[0].first << Mod->getFullModuleName();
64 
65     return nullptr;
66   }
67 
68   // Dig into the module path.
69   for (unsigned I = 1, N = Id.size(); I != N; ++I) {
70     Module *Sub = lookupModuleQualified(Id[I].first, Context);
71     if (!Sub) {
72       if (Complain)
73         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
74         << Id[I].first << Context->getFullModuleName()
75         << SourceRange(Id[0].second, Id[I-1].second);
76 
77       return nullptr;
78     }
79 
80     Context = Sub;
81   }
82 
83   return Context;
84 }
85 
86 ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
87                      const LangOptions &LangOpts, const TargetInfo *Target,
88                      HeaderSearch &HeaderInfo)
89     : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
90       HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr),
91       CompilingModule(nullptr), SourceModule(nullptr) {}
92 
93 ModuleMap::~ModuleMap() {
94   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
95                                         IEnd = Modules.end();
96        I != IEnd; ++I) {
97     delete I->getValue();
98   }
99 }
100 
101 void ModuleMap::setTarget(const TargetInfo &Target) {
102   assert((!this->Target || this->Target == &Target) &&
103          "Improper target override");
104   this->Target = &Target;
105 }
106 
107 /// \brief "Sanitize" a filename so that it can be used as an identifier.
108 static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
109                                               SmallVectorImpl<char> &Buffer) {
110   if (Name.empty())
111     return Name;
112 
113   if (!isValidIdentifier(Name)) {
114     // If we don't already have something with the form of an identifier,
115     // create a buffer with the sanitized name.
116     Buffer.clear();
117     if (isDigit(Name[0]))
118       Buffer.push_back('_');
119     Buffer.reserve(Buffer.size() + Name.size());
120     for (unsigned I = 0, N = Name.size(); I != N; ++I) {
121       if (isIdentifierBody(Name[I]))
122         Buffer.push_back(Name[I]);
123       else
124         Buffer.push_back('_');
125     }
126 
127     Name = StringRef(Buffer.data(), Buffer.size());
128   }
129 
130   while (llvm::StringSwitch<bool>(Name)
131 #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
132 #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
133 #include "clang/Basic/TokenKinds.def"
134            .Default(false)) {
135     if (Name.data() != Buffer.data())
136       Buffer.append(Name.begin(), Name.end());
137     Buffer.push_back('_');
138     Name = StringRef(Buffer.data(), Buffer.size());
139   }
140 
141   return Name;
142 }
143 
144 /// \brief Determine whether the given file name is the name of a builtin
145 /// header, supplied by Clang to replace, override, or augment existing system
146 /// headers.
147 static bool isBuiltinHeader(StringRef FileName) {
148   return llvm::StringSwitch<bool>(FileName)
149            .Case("float.h", true)
150            .Case("iso646.h", true)
151            .Case("limits.h", true)
152            .Case("stdalign.h", true)
153            .Case("stdarg.h", true)
154            .Case("stdbool.h", true)
155            .Case("stddef.h", true)
156            .Case("stdint.h", true)
157            .Case("tgmath.h", true)
158            .Case("unwind.h", true)
159            .Default(false);
160 }
161 
162 ModuleMap::HeadersMap::iterator
163 ModuleMap::findKnownHeader(const FileEntry *File) {
164   HeadersMap::iterator Known = Headers.find(File);
165   if (Known == Headers.end() && File->getDir() == BuiltinIncludeDir &&
166       isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
167     HeaderInfo.loadTopLevelSystemModules();
168     return Headers.find(File);
169   }
170   return Known;
171 }
172 
173 ModuleMap::KnownHeader
174 ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
175                     SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) {
176   const DirectoryEntry *Dir = File->getDir();
177   assert(Dir && "file in no directory");
178 
179   // Note: as an egregious but useful hack we use the real path here, because
180   // frameworks moving from top-level frameworks to embedded frameworks tend
181   // to be symlinked from the top-level location to the embedded location,
182   // and we need to resolve lookups as if we had found the embedded location.
183   StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
184 
185   // Keep walking up the directory hierarchy, looking for a directory with
186   // an umbrella header.
187   do {
188     auto KnownDir = UmbrellaDirs.find(Dir);
189     if (KnownDir != UmbrellaDirs.end())
190       return KnownHeader(KnownDir->second, NormalHeader);
191 
192     IntermediateDirs.push_back(Dir);
193 
194     // Retrieve our parent path.
195     DirName = llvm::sys::path::parent_path(DirName);
196     if (DirName.empty())
197       break;
198 
199     // Resolve the parent path to a directory entry.
200     Dir = SourceMgr.getFileManager().getDirectory(DirName);
201   } while (Dir);
202   return KnownHeader();
203 }
204 
205 // Returns 'true' if 'RequestingModule directly uses 'RequestedModule'.
206 static bool directlyUses(const Module *RequestingModule,
207                          const Module *RequestedModule) {
208   return std::find(RequestingModule->DirectUses.begin(),
209                    RequestingModule->DirectUses.end(),
210                    RequestedModule) != RequestingModule->DirectUses.end();
211 }
212 
213 static bool violatesPrivateInclude(Module *RequestingModule,
214                                    const FileEntry *IncFileEnt,
215                                    ModuleMap::ModuleHeaderRole Role,
216                                    Module *RequestedModule) {
217   #ifndef NDEBUG
218   // Check for consistency between the module header role
219   // as obtained from the lookup and as obtained from the module.
220   // This check is not cheap, so enable it only for debugging.
221   SmallVectorImpl<const FileEntry *> &PvtHdrs
222       = RequestedModule->PrivateHeaders;
223   SmallVectorImpl<const FileEntry *>::iterator Look
224       = std::find(PvtHdrs.begin(), PvtHdrs.end(), IncFileEnt);
225   bool IsPrivate = Look != PvtHdrs.end();
226   assert((IsPrivate && Role == ModuleMap::PrivateHeader)
227                || (!IsPrivate && Role != ModuleMap::PrivateHeader));
228   #endif
229   return Role == ModuleMap::PrivateHeader &&
230          RequestedModule->getTopLevelModule() != RequestingModule;
231 }
232 
233 static Module *getTopLevelOrNull(Module *M) {
234   return M ? M->getTopLevelModule() : nullptr;
235 }
236 
237 void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule,
238                                         SourceLocation FilenameLoc,
239                                         StringRef Filename,
240                                         const FileEntry *File) {
241   // No errors for indirect modules. This may be a bit of a problem for modules
242   // with no source files.
243   if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule))
244     return;
245 
246   if (RequestingModule)
247     resolveUses(RequestingModule, /*Complain=*/false);
248 
249   bool Excluded = false;
250   Module *Private = nullptr;
251   Module *NotUsed = nullptr;
252 
253   HeadersMap::iterator Known = findKnownHeader(File);
254   if (Known != Headers.end()) {
255     for (const KnownHeader &Header : Known->second) {
256       // If 'File' is part of 'RequestingModule' we can definitely include it.
257       if (Header.getModule() == RequestingModule)
258         return;
259 
260       // Remember private headers for later printing of a diagnostic.
261       if (violatesPrivateInclude(RequestingModule, File, Header.getRole(),
262                                  Header.getModule())) {
263         Private = Header.getModule();
264         continue;
265       }
266 
267       // If uses need to be specified explicitly, we are only allowed to return
268       // modules that are explicitly used by the requesting module.
269       if (RequestingModule && LangOpts.ModulesDeclUse &&
270           !directlyUses(RequestingModule, Header.getModule())) {
271         NotUsed = Header.getModule();
272         continue;
273       }
274 
275       // We have found a module that we can happily use.
276       return;
277     }
278 
279     Excluded = true;
280   }
281 
282   // We have found a header, but it is private.
283   if (Private) {
284     Diags.Report(FilenameLoc, diag::error_use_of_private_header_outside_module)
285         << Filename;
286     return;
287   }
288 
289   // We have found a module, but we don't use it.
290   if (NotUsed) {
291     Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module)
292         << RequestingModule->getFullModuleName() << Filename;
293     return;
294   }
295 
296   if (Excluded || isHeaderInUmbrellaDirs(File))
297     return;
298 
299   // At this point, only non-modular includes remain.
300 
301   if (LangOpts.ModulesStrictDeclUse) {
302     Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module)
303         << RequestingModule->getFullModuleName() << Filename;
304   } else if (RequestingModule) {
305     diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ?
306         diag::warn_non_modular_include_in_framework_module :
307         diag::warn_non_modular_include_in_module;
308     Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName();
309   }
310 }
311 
312 ModuleMap::KnownHeader
313 ModuleMap::findModuleForHeader(const FileEntry *File,
314                                Module *RequestingModule,
315                                bool IncludeTextualHeaders) {
316   HeadersMap::iterator Known = findKnownHeader(File);
317 
318   auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader {
319     if (!IncludeTextualHeaders && R.getRole() == ModuleMap::TextualHeader)
320       return ModuleMap::KnownHeader();
321     return R;
322   };
323 
324   if (Known != Headers.end()) {
325     ModuleMap::KnownHeader Result = KnownHeader();
326 
327     // Iterate over all modules that 'File' is part of to find the best fit.
328     for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(),
329                                                 E = Known->second.end();
330          I != E; ++I) {
331       // Cannot use a module if it is unavailable.
332       if (!I->getModule()->isAvailable())
333         continue;
334 
335       // If 'File' is part of 'RequestingModule', 'RequestingModule' is the
336       // module we are looking for.
337       if (I->getModule() == RequestingModule)
338         return MakeResult(*I);
339 
340       // If uses need to be specified explicitly, we are only allowed to return
341       // modules that are explicitly used by the requesting module.
342       if (RequestingModule && LangOpts.ModulesDeclUse &&
343           !directlyUses(RequestingModule, I->getModule()))
344         continue;
345 
346       Result = *I;
347       // If 'File' is a public header of this module, this is as good as we
348       // are going to get.
349       // FIXME: If we have a RequestingModule, we should prefer the header from
350       // that module.
351       if (I->getRole() == ModuleMap::NormalHeader ||
352           I->getRole() == ModuleMap::TextualHeader)
353         break;
354     }
355     return MakeResult(Result);
356   }
357 
358   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
359   KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs);
360   if (H) {
361     Module *Result = H.getModule();
362 
363     // Search up the module stack until we find a module with an umbrella
364     // directory.
365     Module *UmbrellaModule = Result;
366     while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
367       UmbrellaModule = UmbrellaModule->Parent;
368 
369     if (UmbrellaModule->InferSubmodules) {
370       const FileEntry *UmbrellaModuleMap =
371           getModuleMapFileForUniquing(UmbrellaModule);
372 
373       // Infer submodules for each of the directories we found between
374       // the directory of the umbrella header and the directory where
375       // the actual header is located.
376       bool Explicit = UmbrellaModule->InferExplicitSubmodules;
377 
378       for (unsigned I = SkippedDirs.size(); I != 0; --I) {
379         // Find or create the module that corresponds to this directory name.
380         SmallString<32> NameBuf;
381         StringRef Name = sanitizeFilenameAsIdentifier(
382             llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf);
383         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
384                                     Explicit).first;
385         InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
386         Result->IsInferred = true;
387 
388         // Associate the module and the directory.
389         UmbrellaDirs[SkippedDirs[I-1]] = Result;
390 
391         // If inferred submodules export everything they import, add a
392         // wildcard to the set of exports.
393         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
394           Result->Exports.push_back(Module::ExportDecl(nullptr, true));
395       }
396 
397       // Infer a submodule with the same name as this header file.
398       SmallString<32> NameBuf;
399       StringRef Name = sanitizeFilenameAsIdentifier(
400                          llvm::sys::path::stem(File->getName()), NameBuf);
401       Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
402                                   Explicit).first;
403       InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
404       Result->IsInferred = true;
405       Result->addTopHeader(File);
406 
407       // If inferred submodules export everything they import, add a
408       // wildcard to the set of exports.
409       if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
410         Result->Exports.push_back(Module::ExportDecl(nullptr, true));
411     } else {
412       // Record each of the directories we stepped through as being part of
413       // the module we found, since the umbrella header covers them all.
414       for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
415         UmbrellaDirs[SkippedDirs[I]] = Result;
416     }
417 
418     Headers[File].push_back(KnownHeader(Result, NormalHeader));
419 
420     // If a header corresponds to an unavailable module, don't report
421     // that it maps to anything.
422     if (!Result->isAvailable())
423       return KnownHeader();
424 
425     return MakeResult(Headers[File].back());
426   }
427 
428   return KnownHeader();
429 }
430 
431 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
432   return isHeaderUnavailableInModule(Header, nullptr);
433 }
434 
435 bool
436 ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header,
437                                        const Module *RequestingModule) const {
438   HeadersMap::const_iterator Known = Headers.find(Header);
439   if (Known != Headers.end()) {
440     for (SmallVectorImpl<KnownHeader>::const_iterator
441              I = Known->second.begin(),
442              E = Known->second.end();
443          I != E; ++I) {
444       if (I->isAvailable() && (!RequestingModule ||
445                                I->getModule()->isSubModuleOf(RequestingModule)))
446         return false;
447     }
448     return true;
449   }
450 
451   const DirectoryEntry *Dir = Header->getDir();
452   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
453   StringRef DirName = Dir->getName();
454 
455   auto IsUnavailable = [&](const Module *M) {
456     return !M->isAvailable() && (!RequestingModule ||
457                                  M->isSubModuleOf(RequestingModule));
458   };
459 
460   // Keep walking up the directory hierarchy, looking for a directory with
461   // an umbrella header.
462   do {
463     llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
464       = UmbrellaDirs.find(Dir);
465     if (KnownDir != UmbrellaDirs.end()) {
466       Module *Found = KnownDir->second;
467       if (IsUnavailable(Found))
468         return true;
469 
470       // Search up the module stack until we find a module with an umbrella
471       // directory.
472       Module *UmbrellaModule = Found;
473       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
474         UmbrellaModule = UmbrellaModule->Parent;
475 
476       if (UmbrellaModule->InferSubmodules) {
477         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
478           // Find or create the module that corresponds to this directory name.
479           SmallString<32> NameBuf;
480           StringRef Name = sanitizeFilenameAsIdentifier(
481                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
482                              NameBuf);
483           Found = lookupModuleQualified(Name, Found);
484           if (!Found)
485             return false;
486           if (IsUnavailable(Found))
487             return true;
488         }
489 
490         // Infer a submodule with the same name as this header file.
491         SmallString<32> NameBuf;
492         StringRef Name = sanitizeFilenameAsIdentifier(
493                            llvm::sys::path::stem(Header->getName()),
494                            NameBuf);
495         Found = lookupModuleQualified(Name, Found);
496         if (!Found)
497           return false;
498       }
499 
500       return IsUnavailable(Found);
501     }
502 
503     SkippedDirs.push_back(Dir);
504 
505     // Retrieve our parent path.
506     DirName = llvm::sys::path::parent_path(DirName);
507     if (DirName.empty())
508       break;
509 
510     // Resolve the parent path to a directory entry.
511     Dir = SourceMgr.getFileManager().getDirectory(DirName);
512   } while (Dir);
513 
514   return false;
515 }
516 
517 Module *ModuleMap::findModule(StringRef Name) const {
518   llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
519   if (Known != Modules.end())
520     return Known->getValue();
521 
522   return nullptr;
523 }
524 
525 Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
526                                            Module *Context) const {
527   for(; Context; Context = Context->Parent) {
528     if (Module *Sub = lookupModuleQualified(Name, Context))
529       return Sub;
530   }
531 
532   return findModule(Name);
533 }
534 
535 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
536   if (!Context)
537     return findModule(Name);
538 
539   return Context->findSubmodule(Name);
540 }
541 
542 std::pair<Module *, bool>
543 ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
544                               bool IsExplicit) {
545   // Try to find an existing module with this name.
546   if (Module *Sub = lookupModuleQualified(Name, Parent))
547     return std::make_pair(Sub, false);
548 
549   // Create a new module with this name.
550   Module *Result = new Module(Name, SourceLocation(), Parent,
551                               IsFramework, IsExplicit);
552   if (LangOpts.CurrentModule == Name) {
553     SourceModule = Result;
554     SourceModuleName = Name;
555   }
556   if (!Parent) {
557     Modules[Name] = Result;
558     if (!LangOpts.CurrentModule.empty() && !CompilingModule &&
559         Name == LangOpts.CurrentModule) {
560       CompilingModule = Result;
561     }
562   }
563   return std::make_pair(Result, true);
564 }
565 
566 bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir,
567                                         StringRef Name, bool &IsSystem) const {
568   // Check whether we have already looked into the parent directory
569   // for a module map.
570   llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
571     inferred = InferredDirectories.find(ParentDir);
572   if (inferred == InferredDirectories.end())
573     return false;
574 
575   if (!inferred->second.InferModules)
576     return false;
577 
578   // We're allowed to infer for this directory, but make sure it's okay
579   // to infer this particular module.
580   bool canInfer = std::find(inferred->second.ExcludedModules.begin(),
581                             inferred->second.ExcludedModules.end(),
582                             Name) == inferred->second.ExcludedModules.end();
583 
584   if (canInfer && inferred->second.InferSystemModules)
585     IsSystem = true;
586 
587   return canInfer;
588 }
589 
590 /// \brief For a framework module, infer the framework against which we
591 /// should link.
592 static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
593                                FileManager &FileMgr) {
594   assert(Mod->IsFramework && "Can only infer linking for framework modules");
595   assert(!Mod->isSubFramework() &&
596          "Can only infer linking for top-level frameworks");
597 
598   SmallString<128> LibName;
599   LibName += FrameworkDir->getName();
600   llvm::sys::path::append(LibName, Mod->Name);
601   if (FileMgr.getFile(LibName)) {
602     Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
603                                                      /*IsFramework=*/true));
604   }
605 }
606 
607 Module *
608 ModuleMap::inferFrameworkModule(StringRef ModuleName,
609                                 const DirectoryEntry *FrameworkDir,
610                                 bool IsSystem,
611                                 Module *Parent) {
612   // Check whether we've already found this module.
613   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
614     return Mod;
615 
616   FileManager &FileMgr = SourceMgr.getFileManager();
617 
618   // If the framework has a parent path from which we're allowed to infer
619   // a framework module, do so.
620   const FileEntry *ModuleMapFile = nullptr;
621   if (!Parent) {
622     // Determine whether we're allowed to infer a module map.
623 
624     // Note: as an egregious but useful hack we use the real path here, because
625     // we might be looking at an embedded framework that symlinks out to a
626     // top-level framework, and we need to infer as if we were naming the
627     // top-level framework.
628     StringRef FrameworkDirName
629       = SourceMgr.getFileManager().getCanonicalName(FrameworkDir);
630 
631     // In case this is a case-insensitive filesystem, make sure the canonical
632     // directory name matches ModuleName exactly. Modules are case-sensitive.
633     // FIXME: we should be able to give a fix-it hint for the correct spelling.
634     if (llvm::sys::path::stem(FrameworkDirName) != ModuleName)
635       return nullptr;
636 
637     bool canInfer = false;
638     if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
639       // Figure out the parent path.
640       StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
641       if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
642         // Check whether we have already looked into the parent directory
643         // for a module map.
644         llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
645           inferred = InferredDirectories.find(ParentDir);
646         if (inferred == InferredDirectories.end()) {
647           // We haven't looked here before. Load a module map, if there is
648           // one.
649           bool IsFrameworkDir = Parent.endswith(".framework");
650           if (const FileEntry *ModMapFile =
651                 HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) {
652             parseModuleMapFile(ModMapFile, IsSystem);
653             inferred = InferredDirectories.find(ParentDir);
654           }
655 
656           if (inferred == InferredDirectories.end())
657             inferred = InferredDirectories.insert(
658                          std::make_pair(ParentDir, InferredDirectory())).first;
659         }
660 
661         if (inferred->second.InferModules) {
662           // We're allowed to infer for this directory, but make sure it's okay
663           // to infer this particular module.
664           StringRef Name = llvm::sys::path::stem(FrameworkDirName);
665           canInfer = std::find(inferred->second.ExcludedModules.begin(),
666                                inferred->second.ExcludedModules.end(),
667                                Name) == inferred->second.ExcludedModules.end();
668 
669           if (inferred->second.InferSystemModules)
670             IsSystem = true;
671           ModuleMapFile = inferred->second.ModuleMapFile;
672         }
673       }
674     }
675 
676     // If we're not allowed to infer a framework module, don't.
677     if (!canInfer)
678       return nullptr;
679   } else
680     ModuleMapFile = getModuleMapFileForUniquing(Parent);
681 
682 
683   // Look for an umbrella header.
684   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
685   llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
686   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
687 
688   // FIXME: If there's no umbrella header, we could probably scan the
689   // framework to load *everything*. But, it's not clear that this is a good
690   // idea.
691   if (!UmbrellaHeader)
692     return nullptr;
693 
694   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
695                               /*IsFramework=*/true, /*IsExplicit=*/false);
696   InferredModuleAllowedBy[Result] = ModuleMapFile;
697   Result->IsInferred = true;
698   if (LangOpts.CurrentModule == ModuleName) {
699     SourceModule = Result;
700     SourceModuleName = ModuleName;
701   }
702   if (IsSystem)
703     Result->IsSystem = IsSystem;
704 
705   if (!Parent)
706     Modules[ModuleName] = Result;
707 
708   // umbrella header "umbrella-header-name"
709   Result->Umbrella = UmbrellaHeader;
710   Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader));
711   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
712 
713   // export *
714   Result->Exports.push_back(Module::ExportDecl(nullptr, true));
715 
716   // module * { export * }
717   Result->InferSubmodules = true;
718   Result->InferExportWildcard = true;
719 
720   // Look for subframeworks.
721   std::error_code EC;
722   SmallString<128> SubframeworksDirName
723     = StringRef(FrameworkDir->getName());
724   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
725   llvm::sys::path::native(SubframeworksDirName);
726   for (llvm::sys::fs::directory_iterator
727          Dir(SubframeworksDirName.str(), EC), DirEnd;
728        Dir != DirEnd && !EC; Dir.increment(EC)) {
729     if (!StringRef(Dir->path()).endswith(".framework"))
730       continue;
731 
732     if (const DirectoryEntry *SubframeworkDir
733           = FileMgr.getDirectory(Dir->path())) {
734       // Note: as an egregious but useful hack, we use the real path here and
735       // check whether it is actually a subdirectory of the parent directory.
736       // This will not be the case if the 'subframework' is actually a symlink
737       // out to a top-level framework.
738       StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
739       bool FoundParent = false;
740       do {
741         // Get the parent directory name.
742         SubframeworkDirName
743           = llvm::sys::path::parent_path(SubframeworkDirName);
744         if (SubframeworkDirName.empty())
745           break;
746 
747         if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
748           FoundParent = true;
749           break;
750         }
751       } while (true);
752 
753       if (!FoundParent)
754         continue;
755 
756       // FIXME: Do we want to warn about subframeworks without umbrella headers?
757       SmallString<32> NameBuf;
758       inferFrameworkModule(sanitizeFilenameAsIdentifier(
759                              llvm::sys::path::stem(Dir->path()), NameBuf),
760                            SubframeworkDir, IsSystem, Result);
761     }
762   }
763 
764   // If the module is a top-level framework, automatically link against the
765   // framework.
766   if (!Result->isSubFramework()) {
767     inferFrameworkLink(Result, FrameworkDir, FileMgr);
768   }
769 
770   return Result;
771 }
772 
773 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
774   Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
775   Mod->Umbrella = UmbrellaHeader;
776   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
777 }
778 
779 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
780   Mod->Umbrella = UmbrellaDir;
781   UmbrellaDirs[UmbrellaDir] = Mod;
782 }
783 
784 void ModuleMap::addHeader(Module *Mod, const FileEntry *Header,
785                           ModuleHeaderRole Role) {
786   if (Role == TextualHeader) {
787     Mod->TextualHeaders.push_back(Header);
788   } else {
789     if (Role == PrivateHeader)
790       Mod->PrivateHeaders.push_back(Header);
791     else
792       Mod->NormalHeaders.push_back(Header);
793     bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
794     HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader);
795   }
796   Headers[Header].push_back(KnownHeader(Mod, Role));
797 }
798 
799 void ModuleMap::excludeHeader(Module *Mod, const FileEntry *Header) {
800   Mod->ExcludedHeaders.push_back(Header);
801 
802   // Add this as a known header so we won't implicitly add it to any
803   // umbrella directory module.
804   // FIXME: Should we only exclude it from umbrella modules within the
805   // specified module?
806   (void) Headers[Header];
807 }
808 
809 const FileEntry *
810 ModuleMap::getContainingModuleMapFile(const Module *Module) const {
811   if (Module->DefinitionLoc.isInvalid())
812     return nullptr;
813 
814   return SourceMgr.getFileEntryForID(
815            SourceMgr.getFileID(Module->DefinitionLoc));
816 }
817 
818 const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const {
819   if (M->IsInferred) {
820     assert(InferredModuleAllowedBy.count(M) && "missing inferred module map");
821     return InferredModuleAllowedBy.find(M)->second;
822   }
823   return getContainingModuleMapFile(M);
824 }
825 
826 void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) {
827   assert(M->IsInferred && "module not inferred");
828   InferredModuleAllowedBy[M] = ModMap;
829 }
830 
831 void ModuleMap::dump() {
832   llvm::errs() << "Modules:";
833   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
834                                         MEnd = Modules.end();
835        M != MEnd; ++M)
836     M->getValue()->print(llvm::errs(), 2);
837 
838   llvm::errs() << "Headers:";
839   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
840        H != HEnd; ++H) {
841     llvm::errs() << "  \"" << H->first->getName() << "\" -> ";
842     for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
843                                                       E = H->second.end();
844          I != E; ++I) {
845       if (I != H->second.begin())
846         llvm::errs() << ",";
847       llvm::errs() << I->getModule()->getFullModuleName();
848     }
849     llvm::errs() << "\n";
850   }
851 }
852 
853 bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
854   bool HadError = false;
855   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
856     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
857                                               Complain);
858     if (Export.getPointer() || Export.getInt())
859       Mod->Exports.push_back(Export);
860     else
861       HadError = true;
862   }
863   Mod->UnresolvedExports.clear();
864   return HadError;
865 }
866 
867 bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
868   bool HadError = false;
869   for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) {
870     Module *DirectUse =
871         resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain);
872     if (DirectUse)
873       Mod->DirectUses.push_back(DirectUse);
874     else
875       HadError = true;
876   }
877   Mod->UnresolvedDirectUses.clear();
878   return HadError;
879 }
880 
881 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
882   bool HadError = false;
883   for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) {
884     Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id,
885                                        Mod, Complain);
886     if (!OtherMod) {
887       HadError = true;
888       continue;
889     }
890 
891     Module::Conflict Conflict;
892     Conflict.Other = OtherMod;
893     Conflict.Message = Mod->UnresolvedConflicts[I].Message;
894     Mod->Conflicts.push_back(Conflict);
895   }
896   Mod->UnresolvedConflicts.clear();
897   return HadError;
898 }
899 
900 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
901   if (Loc.isInvalid())
902     return nullptr;
903 
904   // Use the expansion location to determine which module we're in.
905   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
906   if (!ExpansionLoc.isFileID())
907     return nullptr;
908 
909   const SourceManager &SrcMgr = Loc.getManager();
910   FileID ExpansionFileID = ExpansionLoc.getFileID();
911 
912   while (const FileEntry *ExpansionFile
913            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
914     // Find the module that owns this header (if any).
915     if (Module *Mod = findModuleForHeader(ExpansionFile).getModule())
916       return Mod;
917 
918     // No module owns this header, so look up the inclusion chain to see if
919     // any included header has an associated module.
920     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
921     if (IncludeLoc.isInvalid())
922       return nullptr;
923 
924     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
925   }
926 
927   return nullptr;
928 }
929 
930 //----------------------------------------------------------------------------//
931 // Module map file parser
932 //----------------------------------------------------------------------------//
933 
934 namespace clang {
935   /// \brief A token in a module map file.
936   struct MMToken {
937     enum TokenKind {
938       Comma,
939       ConfigMacros,
940       Conflict,
941       EndOfFile,
942       HeaderKeyword,
943       Identifier,
944       Exclaim,
945       ExcludeKeyword,
946       ExplicitKeyword,
947       ExportKeyword,
948       ExternKeyword,
949       FrameworkKeyword,
950       LinkKeyword,
951       ModuleKeyword,
952       Period,
953       PrivateKeyword,
954       UmbrellaKeyword,
955       UseKeyword,
956       RequiresKeyword,
957       Star,
958       StringLiteral,
959       TextualKeyword,
960       LBrace,
961       RBrace,
962       LSquare,
963       RSquare
964     } Kind;
965 
966     unsigned Location;
967     unsigned StringLength;
968     const char *StringData;
969 
970     void clear() {
971       Kind = EndOfFile;
972       Location = 0;
973       StringLength = 0;
974       StringData = nullptr;
975     }
976 
977     bool is(TokenKind K) const { return Kind == K; }
978 
979     SourceLocation getLocation() const {
980       return SourceLocation::getFromRawEncoding(Location);
981     }
982 
983     StringRef getString() const {
984       return StringRef(StringData, StringLength);
985     }
986   };
987 
988   /// \brief The set of attributes that can be attached to a module.
989   struct Attributes {
990     Attributes() : IsSystem(), IsExternC(), IsExhaustive() { }
991 
992     /// \brief Whether this is a system module.
993     unsigned IsSystem : 1;
994 
995     /// \brief Whether this is an extern "C" module.
996     unsigned IsExternC : 1;
997 
998     /// \brief Whether this is an exhaustive set of configuration macros.
999     unsigned IsExhaustive : 1;
1000   };
1001 
1002 
1003   class ModuleMapParser {
1004     Lexer &L;
1005     SourceManager &SourceMgr;
1006 
1007     /// \brief Default target information, used only for string literal
1008     /// parsing.
1009     const TargetInfo *Target;
1010 
1011     DiagnosticsEngine &Diags;
1012     ModuleMap &Map;
1013 
1014     /// \brief The current module map file.
1015     const FileEntry *ModuleMapFile;
1016 
1017     /// \brief The directory that this module map resides in.
1018     const DirectoryEntry *Directory;
1019 
1020     /// \brief The directory containing Clang-supplied headers.
1021     const DirectoryEntry *BuiltinIncludeDir;
1022 
1023     /// \brief Whether this module map is in a system header directory.
1024     bool IsSystem;
1025 
1026     /// \brief Whether an error occurred.
1027     bool HadError;
1028 
1029     /// \brief Stores string data for the various string literals referenced
1030     /// during parsing.
1031     llvm::BumpPtrAllocator StringData;
1032 
1033     /// \brief The current token.
1034     MMToken Tok;
1035 
1036     /// \brief The active module.
1037     Module *ActiveModule;
1038 
1039     /// \brief Consume the current token and return its location.
1040     SourceLocation consumeToken();
1041 
1042     /// \brief Skip tokens until we reach the a token with the given kind
1043     /// (or the end of the file).
1044     void skipUntil(MMToken::TokenKind K);
1045 
1046     typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
1047     bool parseModuleId(ModuleId &Id);
1048     void parseModuleDecl();
1049     void parseExternModuleDecl();
1050     void parseRequiresDecl();
1051     void parseHeaderDecl(clang::MMToken::TokenKind,
1052                          SourceLocation LeadingLoc);
1053     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
1054     void parseExportDecl();
1055     void parseUseDecl();
1056     void parseLinkDecl();
1057     void parseConfigMacros();
1058     void parseConflict();
1059     void parseInferredModuleDecl(bool Framework, bool Explicit);
1060     bool parseOptionalAttributes(Attributes &Attrs);
1061 
1062   public:
1063     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
1064                              const TargetInfo *Target,
1065                              DiagnosticsEngine &Diags,
1066                              ModuleMap &Map,
1067                              const FileEntry *ModuleMapFile,
1068                              const DirectoryEntry *Directory,
1069                              const DirectoryEntry *BuiltinIncludeDir,
1070                              bool IsSystem)
1071       : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
1072         ModuleMapFile(ModuleMapFile), Directory(Directory),
1073         BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem),
1074         HadError(false), ActiveModule(nullptr)
1075     {
1076       Tok.clear();
1077       consumeToken();
1078     }
1079 
1080     bool parseModuleMapFile();
1081   };
1082 }
1083 
1084 SourceLocation ModuleMapParser::consumeToken() {
1085 retry:
1086   SourceLocation Result = Tok.getLocation();
1087   Tok.clear();
1088 
1089   Token LToken;
1090   L.LexFromRawLexer(LToken);
1091   Tok.Location = LToken.getLocation().getRawEncoding();
1092   switch (LToken.getKind()) {
1093   case tok::raw_identifier: {
1094     StringRef RI = LToken.getRawIdentifier();
1095     Tok.StringData = RI.data();
1096     Tok.StringLength = RI.size();
1097     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI)
1098                  .Case("config_macros", MMToken::ConfigMacros)
1099                  .Case("conflict", MMToken::Conflict)
1100                  .Case("exclude", MMToken::ExcludeKeyword)
1101                  .Case("explicit", MMToken::ExplicitKeyword)
1102                  .Case("export", MMToken::ExportKeyword)
1103                  .Case("extern", MMToken::ExternKeyword)
1104                  .Case("framework", MMToken::FrameworkKeyword)
1105                  .Case("header", MMToken::HeaderKeyword)
1106                  .Case("link", MMToken::LinkKeyword)
1107                  .Case("module", MMToken::ModuleKeyword)
1108                  .Case("private", MMToken::PrivateKeyword)
1109                  .Case("requires", MMToken::RequiresKeyword)
1110                  .Case("textual", MMToken::TextualKeyword)
1111                  .Case("umbrella", MMToken::UmbrellaKeyword)
1112                  .Case("use", MMToken::UseKeyword)
1113                  .Default(MMToken::Identifier);
1114     break;
1115   }
1116 
1117   case tok::comma:
1118     Tok.Kind = MMToken::Comma;
1119     break;
1120 
1121   case tok::eof:
1122     Tok.Kind = MMToken::EndOfFile;
1123     break;
1124 
1125   case tok::l_brace:
1126     Tok.Kind = MMToken::LBrace;
1127     break;
1128 
1129   case tok::l_square:
1130     Tok.Kind = MMToken::LSquare;
1131     break;
1132 
1133   case tok::period:
1134     Tok.Kind = MMToken::Period;
1135     break;
1136 
1137   case tok::r_brace:
1138     Tok.Kind = MMToken::RBrace;
1139     break;
1140 
1141   case tok::r_square:
1142     Tok.Kind = MMToken::RSquare;
1143     break;
1144 
1145   case tok::star:
1146     Tok.Kind = MMToken::Star;
1147     break;
1148 
1149   case tok::exclaim:
1150     Tok.Kind = MMToken::Exclaim;
1151     break;
1152 
1153   case tok::string_literal: {
1154     if (LToken.hasUDSuffix()) {
1155       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
1156       HadError = true;
1157       goto retry;
1158     }
1159 
1160     // Parse the string literal.
1161     LangOptions LangOpts;
1162     StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target);
1163     if (StringLiteral.hadError)
1164       goto retry;
1165 
1166     // Copy the string literal into our string data allocator.
1167     unsigned Length = StringLiteral.GetStringLength();
1168     char *Saved = StringData.Allocate<char>(Length + 1);
1169     memcpy(Saved, StringLiteral.GetString().data(), Length);
1170     Saved[Length] = 0;
1171 
1172     // Form the token.
1173     Tok.Kind = MMToken::StringLiteral;
1174     Tok.StringData = Saved;
1175     Tok.StringLength = Length;
1176     break;
1177   }
1178 
1179   case tok::comment:
1180     goto retry;
1181 
1182   default:
1183     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
1184     HadError = true;
1185     goto retry;
1186   }
1187 
1188   return Result;
1189 }
1190 
1191 void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
1192   unsigned braceDepth = 0;
1193   unsigned squareDepth = 0;
1194   do {
1195     switch (Tok.Kind) {
1196     case MMToken::EndOfFile:
1197       return;
1198 
1199     case MMToken::LBrace:
1200       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1201         return;
1202 
1203       ++braceDepth;
1204       break;
1205 
1206     case MMToken::LSquare:
1207       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1208         return;
1209 
1210       ++squareDepth;
1211       break;
1212 
1213     case MMToken::RBrace:
1214       if (braceDepth > 0)
1215         --braceDepth;
1216       else if (Tok.is(K))
1217         return;
1218       break;
1219 
1220     case MMToken::RSquare:
1221       if (squareDepth > 0)
1222         --squareDepth;
1223       else if (Tok.is(K))
1224         return;
1225       break;
1226 
1227     default:
1228       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
1229         return;
1230       break;
1231     }
1232 
1233    consumeToken();
1234   } while (true);
1235 }
1236 
1237 /// \brief Parse a module-id.
1238 ///
1239 ///   module-id:
1240 ///     identifier
1241 ///     identifier '.' module-id
1242 ///
1243 /// \returns true if an error occurred, false otherwise.
1244 bool ModuleMapParser::parseModuleId(ModuleId &Id) {
1245   Id.clear();
1246   do {
1247     if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) {
1248       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
1249       consumeToken();
1250     } else {
1251       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
1252       return true;
1253     }
1254 
1255     if (!Tok.is(MMToken::Period))
1256       break;
1257 
1258     consumeToken();
1259   } while (true);
1260 
1261   return false;
1262 }
1263 
1264 namespace {
1265   /// \brief Enumerates the known attributes.
1266   enum AttributeKind {
1267     /// \brief An unknown attribute.
1268     AT_unknown,
1269     /// \brief The 'system' attribute.
1270     AT_system,
1271     /// \brief The 'extern_c' attribute.
1272     AT_extern_c,
1273     /// \brief The 'exhaustive' attribute.
1274     AT_exhaustive
1275   };
1276 }
1277 
1278 /// \brief Parse a module declaration.
1279 ///
1280 ///   module-declaration:
1281 ///     'extern' 'module' module-id string-literal
1282 ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
1283 ///       { module-member* }
1284 ///
1285 ///   module-member:
1286 ///     requires-declaration
1287 ///     header-declaration
1288 ///     submodule-declaration
1289 ///     export-declaration
1290 ///     link-declaration
1291 ///
1292 ///   submodule-declaration:
1293 ///     module-declaration
1294 ///     inferred-submodule-declaration
1295 void ModuleMapParser::parseModuleDecl() {
1296   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
1297          Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
1298   if (Tok.is(MMToken::ExternKeyword)) {
1299     parseExternModuleDecl();
1300     return;
1301   }
1302 
1303   // Parse 'explicit' or 'framework' keyword, if present.
1304   SourceLocation ExplicitLoc;
1305   bool Explicit = false;
1306   bool Framework = false;
1307 
1308   // Parse 'explicit' keyword, if present.
1309   if (Tok.is(MMToken::ExplicitKeyword)) {
1310     ExplicitLoc = consumeToken();
1311     Explicit = true;
1312   }
1313 
1314   // Parse 'framework' keyword, if present.
1315   if (Tok.is(MMToken::FrameworkKeyword)) {
1316     consumeToken();
1317     Framework = true;
1318   }
1319 
1320   // Parse 'module' keyword.
1321   if (!Tok.is(MMToken::ModuleKeyword)) {
1322     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1323     consumeToken();
1324     HadError = true;
1325     return;
1326   }
1327   consumeToken(); // 'module' keyword
1328 
1329   // If we have a wildcard for the module name, this is an inferred submodule.
1330   // Parse it.
1331   if (Tok.is(MMToken::Star))
1332     return parseInferredModuleDecl(Framework, Explicit);
1333 
1334   // Parse the module name.
1335   ModuleId Id;
1336   if (parseModuleId(Id)) {
1337     HadError = true;
1338     return;
1339   }
1340 
1341   if (ActiveModule) {
1342     if (Id.size() > 1) {
1343       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
1344         << SourceRange(Id.front().second, Id.back().second);
1345 
1346       HadError = true;
1347       return;
1348     }
1349   } else if (Id.size() == 1 && Explicit) {
1350     // Top-level modules can't be explicit.
1351     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1352     Explicit = false;
1353     ExplicitLoc = SourceLocation();
1354     HadError = true;
1355   }
1356 
1357   Module *PreviousActiveModule = ActiveModule;
1358   if (Id.size() > 1) {
1359     // This module map defines a submodule. Go find the module of which it
1360     // is a submodule.
1361     ActiveModule = nullptr;
1362     const Module *TopLevelModule = nullptr;
1363     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1364       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1365         if (I == 0)
1366           TopLevelModule = Next;
1367         ActiveModule = Next;
1368         continue;
1369       }
1370 
1371       if (ActiveModule) {
1372         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
1373           << Id[I].first
1374           << ActiveModule->getTopLevelModule()->getFullModuleName();
1375       } else {
1376         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
1377       }
1378       HadError = true;
1379       return;
1380     }
1381 
1382     if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) {
1383       assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) &&
1384              "submodule defined in same file as 'module *' that allowed its "
1385              "top-level module");
1386       Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile);
1387     }
1388   }
1389 
1390   StringRef ModuleName = Id.back().first;
1391   SourceLocation ModuleNameLoc = Id.back().second;
1392 
1393   // Parse the optional attribute list.
1394   Attributes Attrs;
1395   parseOptionalAttributes(Attrs);
1396 
1397   // Parse the opening brace.
1398   if (!Tok.is(MMToken::LBrace)) {
1399     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1400       << ModuleName;
1401     HadError = true;
1402     return;
1403   }
1404   SourceLocation LBraceLoc = consumeToken();
1405 
1406   // Determine whether this (sub)module has already been defined.
1407   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
1408     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
1409       // Skip the module definition.
1410       skipUntil(MMToken::RBrace);
1411       if (Tok.is(MMToken::RBrace))
1412         consumeToken();
1413       else {
1414         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1415         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1416         HadError = true;
1417       }
1418       return;
1419     }
1420 
1421     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
1422       << ModuleName;
1423     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
1424 
1425     // Skip the module definition.
1426     skipUntil(MMToken::RBrace);
1427     if (Tok.is(MMToken::RBrace))
1428       consumeToken();
1429 
1430     HadError = true;
1431     return;
1432   }
1433 
1434   // Start defining this module.
1435   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
1436                                         Explicit).first;
1437   ActiveModule->DefinitionLoc = ModuleNameLoc;
1438   if (Attrs.IsSystem || IsSystem)
1439     ActiveModule->IsSystem = true;
1440   if (Attrs.IsExternC)
1441     ActiveModule->IsExternC = true;
1442 
1443   bool Done = false;
1444   do {
1445     switch (Tok.Kind) {
1446     case MMToken::EndOfFile:
1447     case MMToken::RBrace:
1448       Done = true;
1449       break;
1450 
1451     case MMToken::ConfigMacros:
1452       parseConfigMacros();
1453       break;
1454 
1455     case MMToken::Conflict:
1456       parseConflict();
1457       break;
1458 
1459     case MMToken::ExplicitKeyword:
1460     case MMToken::ExternKeyword:
1461     case MMToken::FrameworkKeyword:
1462     case MMToken::ModuleKeyword:
1463       parseModuleDecl();
1464       break;
1465 
1466     case MMToken::ExportKeyword:
1467       parseExportDecl();
1468       break;
1469 
1470     case MMToken::UseKeyword:
1471       parseUseDecl();
1472       break;
1473 
1474     case MMToken::RequiresKeyword:
1475       parseRequiresDecl();
1476       break;
1477 
1478     case MMToken::TextualKeyword: {
1479       SourceLocation TextualLoc = consumeToken();
1480       if (Tok.is(MMToken::HeaderKeyword)) {
1481         parseHeaderDecl(MMToken::TextualKeyword, TextualLoc);
1482       } else {
1483         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1484           << "textual";
1485       }
1486       break;
1487     }
1488 
1489     case MMToken::UmbrellaKeyword: {
1490       SourceLocation UmbrellaLoc = consumeToken();
1491       if (Tok.is(MMToken::HeaderKeyword))
1492         parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
1493       else
1494         parseUmbrellaDirDecl(UmbrellaLoc);
1495       break;
1496     }
1497 
1498     case MMToken::ExcludeKeyword: {
1499       SourceLocation ExcludeLoc = consumeToken();
1500       if (Tok.is(MMToken::HeaderKeyword)) {
1501         parseHeaderDecl(MMToken::ExcludeKeyword, ExcludeLoc);
1502       } else {
1503         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1504           << "exclude";
1505       }
1506       break;
1507     }
1508 
1509     case MMToken::PrivateKeyword: {
1510       SourceLocation PrivateLoc = consumeToken();
1511       if (Tok.is(MMToken::HeaderKeyword)) {
1512         parseHeaderDecl(MMToken::PrivateKeyword, PrivateLoc);
1513       } else {
1514         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1515           << "private";
1516       }
1517       break;
1518     }
1519 
1520     case MMToken::HeaderKeyword:
1521       parseHeaderDecl(MMToken::HeaderKeyword, SourceLocation());
1522       break;
1523 
1524     case MMToken::LinkKeyword:
1525       parseLinkDecl();
1526       break;
1527 
1528     default:
1529       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
1530       consumeToken();
1531       break;
1532     }
1533   } while (!Done);
1534 
1535   if (Tok.is(MMToken::RBrace))
1536     consumeToken();
1537   else {
1538     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1539     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1540     HadError = true;
1541   }
1542 
1543   // If the active module is a top-level framework, and there are no link
1544   // libraries, automatically link against the framework.
1545   if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
1546       ActiveModule->LinkLibraries.empty()) {
1547     inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
1548   }
1549 
1550   // If the module meets all requirements but is still unavailable, mark the
1551   // whole tree as unavailable to prevent it from building.
1552   if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement &&
1553       ActiveModule->Parent) {
1554     ActiveModule->getTopLevelModule()->markUnavailable();
1555     ActiveModule->getTopLevelModule()->MissingHeaders.append(
1556       ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end());
1557   }
1558 
1559   // We're done parsing this module. Pop back to the previous module.
1560   ActiveModule = PreviousActiveModule;
1561 }
1562 
1563 /// \brief Parse an extern module declaration.
1564 ///
1565 ///   extern module-declaration:
1566 ///     'extern' 'module' module-id string-literal
1567 void ModuleMapParser::parseExternModuleDecl() {
1568   assert(Tok.is(MMToken::ExternKeyword));
1569   consumeToken(); // 'extern' keyword
1570 
1571   // Parse 'module' keyword.
1572   if (!Tok.is(MMToken::ModuleKeyword)) {
1573     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1574     consumeToken();
1575     HadError = true;
1576     return;
1577   }
1578   consumeToken(); // 'module' keyword
1579 
1580   // Parse the module name.
1581   ModuleId Id;
1582   if (parseModuleId(Id)) {
1583     HadError = true;
1584     return;
1585   }
1586 
1587   // Parse the referenced module map file name.
1588   if (!Tok.is(MMToken::StringLiteral)) {
1589     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
1590     HadError = true;
1591     return;
1592   }
1593   std::string FileName = Tok.getString();
1594   consumeToken(); // filename
1595 
1596   StringRef FileNameRef = FileName;
1597   SmallString<128> ModuleMapFileName;
1598   if (llvm::sys::path::is_relative(FileNameRef)) {
1599     ModuleMapFileName += Directory->getName();
1600     llvm::sys::path::append(ModuleMapFileName, FileName);
1601     FileNameRef = ModuleMapFileName.str();
1602   }
1603   if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef))
1604     Map.parseModuleMapFile(File, /*IsSystem=*/false);
1605 }
1606 
1607 /// \brief Parse a requires declaration.
1608 ///
1609 ///   requires-declaration:
1610 ///     'requires' feature-list
1611 ///
1612 ///   feature-list:
1613 ///     feature ',' feature-list
1614 ///     feature
1615 ///
1616 ///   feature:
1617 ///     '!'[opt] identifier
1618 void ModuleMapParser::parseRequiresDecl() {
1619   assert(Tok.is(MMToken::RequiresKeyword));
1620 
1621   // Parse 'requires' keyword.
1622   consumeToken();
1623 
1624   // Parse the feature-list.
1625   do {
1626     bool RequiredState = true;
1627     if (Tok.is(MMToken::Exclaim)) {
1628       RequiredState = false;
1629       consumeToken();
1630     }
1631 
1632     if (!Tok.is(MMToken::Identifier)) {
1633       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
1634       HadError = true;
1635       return;
1636     }
1637 
1638     // Consume the feature name.
1639     std::string Feature = Tok.getString();
1640     consumeToken();
1641 
1642     // Add this feature.
1643     ActiveModule->addRequirement(Feature, RequiredState,
1644                                  Map.LangOpts, *Map.Target);
1645 
1646     if (!Tok.is(MMToken::Comma))
1647       break;
1648 
1649     // Consume the comma.
1650     consumeToken();
1651   } while (true);
1652 }
1653 
1654 /// \brief Append to \p Paths the set of paths needed to get to the
1655 /// subframework in which the given module lives.
1656 static void appendSubframeworkPaths(Module *Mod,
1657                                     SmallVectorImpl<char> &Path) {
1658   // Collect the framework names from the given module to the top-level module.
1659   SmallVector<StringRef, 2> Paths;
1660   for (; Mod; Mod = Mod->Parent) {
1661     if (Mod->IsFramework)
1662       Paths.push_back(Mod->Name);
1663   }
1664 
1665   if (Paths.empty())
1666     return;
1667 
1668   // Add Frameworks/Name.framework for each subframework.
1669   for (unsigned I = Paths.size() - 1; I != 0; --I)
1670     llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework");
1671 }
1672 
1673 /// \brief Parse a header declaration.
1674 ///
1675 ///   header-declaration:
1676 ///     'exclude'[opt] 'header' string-literal
1677 ///     'private'[opt] 'header' string-literal
1678 ///     'textual'[opt] 'header' string-literal
1679 ///     'umbrella'[opt] 'header' string-literal
1680 ///
1681 /// FIXME: Support 'private textual header'.
1682 void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
1683                                       SourceLocation LeadingLoc) {
1684   assert(Tok.is(MMToken::HeaderKeyword));
1685   consumeToken();
1686 
1687   // Parse the header name.
1688   if (!Tok.is(MMToken::StringLiteral)) {
1689     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1690       << "header";
1691     HadError = true;
1692     return;
1693   }
1694   Module::HeaderDirective Header;
1695   Header.FileName = Tok.getString();
1696   Header.FileNameLoc = consumeToken();
1697 
1698   // Check whether we already have an umbrella.
1699   if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) {
1700     Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
1701       << ActiveModule->getFullModuleName();
1702     HadError = true;
1703     return;
1704   }
1705 
1706   // Look for this file.
1707   const FileEntry *File = nullptr;
1708   const FileEntry *BuiltinFile = nullptr;
1709   SmallString<128> PathName;
1710   if (llvm::sys::path::is_absolute(Header.FileName)) {
1711     PathName = Header.FileName;
1712     File = SourceMgr.getFileManager().getFile(PathName);
1713   } else {
1714     // Search for the header file within the search directory.
1715     PathName = Directory->getName();
1716     unsigned PathLength = PathName.size();
1717 
1718     if (ActiveModule->isPartOfFramework()) {
1719       appendSubframeworkPaths(ActiveModule, PathName);
1720 
1721       // Check whether this file is in the public headers.
1722       llvm::sys::path::append(PathName, "Headers", Header.FileName);
1723       File = SourceMgr.getFileManager().getFile(PathName);
1724 
1725       if (!File) {
1726         // Check whether this file is in the private headers.
1727         PathName.resize(PathLength);
1728         llvm::sys::path::append(PathName, "PrivateHeaders", Header.FileName);
1729         File = SourceMgr.getFileManager().getFile(PathName);
1730       }
1731     } else {
1732       // Lookup for normal headers.
1733       llvm::sys::path::append(PathName, Header.FileName);
1734       File = SourceMgr.getFileManager().getFile(PathName);
1735 
1736       // If this is a system module with a top-level header, this header
1737       // may have a counterpart (or replacement) in the set of headers
1738       // supplied by Clang. Find that builtin header.
1739       if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword &&
1740           BuiltinIncludeDir && BuiltinIncludeDir != Directory &&
1741           isBuiltinHeader(Header.FileName)) {
1742         SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
1743         llvm::sys::path::append(BuiltinPathName, Header.FileName);
1744         BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
1745 
1746         // If Clang supplies this header but the underlying system does not,
1747         // just silently swap in our builtin version. Otherwise, we'll end
1748         // up adding both (later).
1749         if (!File && BuiltinFile) {
1750           File = BuiltinFile;
1751           BuiltinFile = nullptr;
1752         }
1753       }
1754     }
1755   }
1756 
1757   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
1758   // Come up with a lazy way to do this.
1759   if (File) {
1760     if (LeadingToken == MMToken::UmbrellaKeyword) {
1761       const DirectoryEntry *UmbrellaDir = File->getDir();
1762       if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
1763         Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash)
1764           << UmbrellaModule->getFullModuleName();
1765         HadError = true;
1766       } else {
1767         // Record this umbrella header.
1768         Map.setUmbrellaHeader(ActiveModule, File);
1769       }
1770     } else if (LeadingToken == MMToken::ExcludeKeyword) {
1771       Map.excludeHeader(ActiveModule, File);
1772     } else {
1773       // Record this header.
1774       ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
1775       if (LeadingToken == MMToken::PrivateKeyword)
1776         Role = ModuleMap::PrivateHeader;
1777       else if (LeadingToken == MMToken::TextualKeyword)
1778         Role = ModuleMap::TextualHeader;
1779       else
1780         assert(LeadingToken == MMToken::HeaderKeyword);
1781 
1782       // If there is a builtin counterpart to this file, add it now, before
1783       // the "real" header, so we build the built-in one first when building
1784       // the module.
1785       if (BuiltinFile)
1786         Map.addHeader(ActiveModule, BuiltinFile, Role);
1787 
1788       Map.addHeader(ActiveModule, File, Role);
1789     }
1790   } else if (LeadingToken != MMToken::ExcludeKeyword) {
1791     // Ignore excluded header files. They're optional anyway.
1792 
1793     // If we find a module that has a missing header, we mark this module as
1794     // unavailable and store the header directive for displaying diagnostics.
1795     Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword;
1796     ActiveModule->markUnavailable();
1797     ActiveModule->MissingHeaders.push_back(Header);
1798   }
1799 }
1800 
1801 /// \brief Parse an umbrella directory declaration.
1802 ///
1803 ///   umbrella-dir-declaration:
1804 ///     umbrella string-literal
1805 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1806   // Parse the directory name.
1807   if (!Tok.is(MMToken::StringLiteral)) {
1808     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1809       << "umbrella";
1810     HadError = true;
1811     return;
1812   }
1813 
1814   std::string DirName = Tok.getString();
1815   SourceLocation DirNameLoc = consumeToken();
1816 
1817   // Check whether we already have an umbrella.
1818   if (ActiveModule->Umbrella) {
1819     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1820       << ActiveModule->getFullModuleName();
1821     HadError = true;
1822     return;
1823   }
1824 
1825   // Look for this file.
1826   const DirectoryEntry *Dir = nullptr;
1827   if (llvm::sys::path::is_absolute(DirName))
1828     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1829   else {
1830     SmallString<128> PathName;
1831     PathName = Directory->getName();
1832     llvm::sys::path::append(PathName, DirName);
1833     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1834   }
1835 
1836   if (!Dir) {
1837     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1838       << DirName;
1839     HadError = true;
1840     return;
1841   }
1842 
1843   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1844     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1845       << OwningModule->getFullModuleName();
1846     HadError = true;
1847     return;
1848   }
1849 
1850   // Record this umbrella directory.
1851   Map.setUmbrellaDir(ActiveModule, Dir);
1852 }
1853 
1854 /// \brief Parse a module export declaration.
1855 ///
1856 ///   export-declaration:
1857 ///     'export' wildcard-module-id
1858 ///
1859 ///   wildcard-module-id:
1860 ///     identifier
1861 ///     '*'
1862 ///     identifier '.' wildcard-module-id
1863 void ModuleMapParser::parseExportDecl() {
1864   assert(Tok.is(MMToken::ExportKeyword));
1865   SourceLocation ExportLoc = consumeToken();
1866 
1867   // Parse the module-id with an optional wildcard at the end.
1868   ModuleId ParsedModuleId;
1869   bool Wildcard = false;
1870   do {
1871     // FIXME: Support string-literal module names here.
1872     if (Tok.is(MMToken::Identifier)) {
1873       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
1874                                               Tok.getLocation()));
1875       consumeToken();
1876 
1877       if (Tok.is(MMToken::Period)) {
1878         consumeToken();
1879         continue;
1880       }
1881 
1882       break;
1883     }
1884 
1885     if(Tok.is(MMToken::Star)) {
1886       Wildcard = true;
1887       consumeToken();
1888       break;
1889     }
1890 
1891     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
1892     HadError = true;
1893     return;
1894   } while (true);
1895 
1896   Module::UnresolvedExportDecl Unresolved = {
1897     ExportLoc, ParsedModuleId, Wildcard
1898   };
1899   ActiveModule->UnresolvedExports.push_back(Unresolved);
1900 }
1901 
1902 /// \brief Parse a module uses declaration.
1903 ///
1904 ///   uses-declaration:
1905 ///     'uses' wildcard-module-id
1906 void ModuleMapParser::parseUseDecl() {
1907   assert(Tok.is(MMToken::UseKeyword));
1908   consumeToken();
1909   // Parse the module-id.
1910   ModuleId ParsedModuleId;
1911   parseModuleId(ParsedModuleId);
1912 
1913   ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
1914 }
1915 
1916 /// \brief Parse a link declaration.
1917 ///
1918 ///   module-declaration:
1919 ///     'link' 'framework'[opt] string-literal
1920 void ModuleMapParser::parseLinkDecl() {
1921   assert(Tok.is(MMToken::LinkKeyword));
1922   SourceLocation LinkLoc = consumeToken();
1923 
1924   // Parse the optional 'framework' keyword.
1925   bool IsFramework = false;
1926   if (Tok.is(MMToken::FrameworkKeyword)) {
1927     consumeToken();
1928     IsFramework = true;
1929   }
1930 
1931   // Parse the library name
1932   if (!Tok.is(MMToken::StringLiteral)) {
1933     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
1934       << IsFramework << SourceRange(LinkLoc);
1935     HadError = true;
1936     return;
1937   }
1938 
1939   std::string LibraryName = Tok.getString();
1940   consumeToken();
1941   ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
1942                                                             IsFramework));
1943 }
1944 
1945 /// \brief Parse a configuration macro declaration.
1946 ///
1947 ///   module-declaration:
1948 ///     'config_macros' attributes[opt] config-macro-list?
1949 ///
1950 ///   config-macro-list:
1951 ///     identifier (',' identifier)?
1952 void ModuleMapParser::parseConfigMacros() {
1953   assert(Tok.is(MMToken::ConfigMacros));
1954   SourceLocation ConfigMacrosLoc = consumeToken();
1955 
1956   // Only top-level modules can have configuration macros.
1957   if (ActiveModule->Parent) {
1958     Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
1959   }
1960 
1961   // Parse the optional attributes.
1962   Attributes Attrs;
1963   parseOptionalAttributes(Attrs);
1964   if (Attrs.IsExhaustive && !ActiveModule->Parent) {
1965     ActiveModule->ConfigMacrosExhaustive = true;
1966   }
1967 
1968   // If we don't have an identifier, we're done.
1969   // FIXME: Support macros with the same name as a keyword here.
1970   if (!Tok.is(MMToken::Identifier))
1971     return;
1972 
1973   // Consume the first identifier.
1974   if (!ActiveModule->Parent) {
1975     ActiveModule->ConfigMacros.push_back(Tok.getString().str());
1976   }
1977   consumeToken();
1978 
1979   do {
1980     // If there's a comma, consume it.
1981     if (!Tok.is(MMToken::Comma))
1982       break;
1983     consumeToken();
1984 
1985     // We expect to see a macro name here.
1986     // FIXME: Support macros with the same name as a keyword here.
1987     if (!Tok.is(MMToken::Identifier)) {
1988       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
1989       break;
1990     }
1991 
1992     // Consume the macro name.
1993     if (!ActiveModule->Parent) {
1994       ActiveModule->ConfigMacros.push_back(Tok.getString().str());
1995     }
1996     consumeToken();
1997   } while (true);
1998 }
1999 
2000 /// \brief Format a module-id into a string.
2001 static std::string formatModuleId(const ModuleId &Id) {
2002   std::string result;
2003   {
2004     llvm::raw_string_ostream OS(result);
2005 
2006     for (unsigned I = 0, N = Id.size(); I != N; ++I) {
2007       if (I)
2008         OS << ".";
2009       OS << Id[I].first;
2010     }
2011   }
2012 
2013   return result;
2014 }
2015 
2016 /// \brief Parse a conflict declaration.
2017 ///
2018 ///   module-declaration:
2019 ///     'conflict' module-id ',' string-literal
2020 void ModuleMapParser::parseConflict() {
2021   assert(Tok.is(MMToken::Conflict));
2022   SourceLocation ConflictLoc = consumeToken();
2023   Module::UnresolvedConflict Conflict;
2024 
2025   // Parse the module-id.
2026   if (parseModuleId(Conflict.Id))
2027     return;
2028 
2029   // Parse the ','.
2030   if (!Tok.is(MMToken::Comma)) {
2031     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
2032       << SourceRange(ConflictLoc);
2033     return;
2034   }
2035   consumeToken();
2036 
2037   // Parse the message.
2038   if (!Tok.is(MMToken::StringLiteral)) {
2039     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
2040       << formatModuleId(Conflict.Id);
2041     return;
2042   }
2043   Conflict.Message = Tok.getString().str();
2044   consumeToken();
2045 
2046   // Add this unresolved conflict.
2047   ActiveModule->UnresolvedConflicts.push_back(Conflict);
2048 }
2049 
2050 /// \brief Parse an inferred module declaration (wildcard modules).
2051 ///
2052 ///   module-declaration:
2053 ///     'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
2054 ///       { inferred-module-member* }
2055 ///
2056 ///   inferred-module-member:
2057 ///     'export' '*'
2058 ///     'exclude' identifier
2059 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
2060   assert(Tok.is(MMToken::Star));
2061   SourceLocation StarLoc = consumeToken();
2062   bool Failed = false;
2063 
2064   // Inferred modules must be submodules.
2065   if (!ActiveModule && !Framework) {
2066     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
2067     Failed = true;
2068   }
2069 
2070   if (ActiveModule) {
2071     // Inferred modules must have umbrella directories.
2072     if (!Failed && ActiveModule->IsAvailable &&
2073         !ActiveModule->getUmbrellaDir()) {
2074       Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
2075       Failed = true;
2076     }
2077 
2078     // Check for redefinition of an inferred module.
2079     if (!Failed && ActiveModule->InferSubmodules) {
2080       Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
2081       if (ActiveModule->InferredSubmoduleLoc.isValid())
2082         Diags.Report(ActiveModule->InferredSubmoduleLoc,
2083                      diag::note_mmap_prev_definition);
2084       Failed = true;
2085     }
2086 
2087     // Check for the 'framework' keyword, which is not permitted here.
2088     if (Framework) {
2089       Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
2090       Framework = false;
2091     }
2092   } else if (Explicit) {
2093     Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
2094     Explicit = false;
2095   }
2096 
2097   // If there were any problems with this inferred submodule, skip its body.
2098   if (Failed) {
2099     if (Tok.is(MMToken::LBrace)) {
2100       consumeToken();
2101       skipUntil(MMToken::RBrace);
2102       if (Tok.is(MMToken::RBrace))
2103         consumeToken();
2104     }
2105     HadError = true;
2106     return;
2107   }
2108 
2109   // Parse optional attributes.
2110   Attributes Attrs;
2111   parseOptionalAttributes(Attrs);
2112 
2113   if (ActiveModule) {
2114     // Note that we have an inferred submodule.
2115     ActiveModule->InferSubmodules = true;
2116     ActiveModule->InferredSubmoduleLoc = StarLoc;
2117     ActiveModule->InferExplicitSubmodules = Explicit;
2118   } else {
2119     // We'll be inferring framework modules for this directory.
2120     Map.InferredDirectories[Directory].InferModules = true;
2121     Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem;
2122     Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile;
2123     // FIXME: Handle the 'framework' keyword.
2124   }
2125 
2126   // Parse the opening brace.
2127   if (!Tok.is(MMToken::LBrace)) {
2128     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
2129     HadError = true;
2130     return;
2131   }
2132   SourceLocation LBraceLoc = consumeToken();
2133 
2134   // Parse the body of the inferred submodule.
2135   bool Done = false;
2136   do {
2137     switch (Tok.Kind) {
2138     case MMToken::EndOfFile:
2139     case MMToken::RBrace:
2140       Done = true;
2141       break;
2142 
2143     case MMToken::ExcludeKeyword: {
2144       if (ActiveModule) {
2145         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2146           << (ActiveModule != nullptr);
2147         consumeToken();
2148         break;
2149       }
2150 
2151       consumeToken();
2152       // FIXME: Support string-literal module names here.
2153       if (!Tok.is(MMToken::Identifier)) {
2154         Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
2155         break;
2156       }
2157 
2158       Map.InferredDirectories[Directory].ExcludedModules
2159         .push_back(Tok.getString());
2160       consumeToken();
2161       break;
2162     }
2163 
2164     case MMToken::ExportKeyword:
2165       if (!ActiveModule) {
2166         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2167           << (ActiveModule != nullptr);
2168         consumeToken();
2169         break;
2170       }
2171 
2172       consumeToken();
2173       if (Tok.is(MMToken::Star))
2174         ActiveModule->InferExportWildcard = true;
2175       else
2176         Diags.Report(Tok.getLocation(),
2177                      diag::err_mmap_expected_export_wildcard);
2178       consumeToken();
2179       break;
2180 
2181     case MMToken::ExplicitKeyword:
2182     case MMToken::ModuleKeyword:
2183     case MMToken::HeaderKeyword:
2184     case MMToken::PrivateKeyword:
2185     case MMToken::UmbrellaKeyword:
2186     default:
2187       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2188           << (ActiveModule != nullptr);
2189       consumeToken();
2190       break;
2191     }
2192   } while (!Done);
2193 
2194   if (Tok.is(MMToken::RBrace))
2195     consumeToken();
2196   else {
2197     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
2198     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
2199     HadError = true;
2200   }
2201 }
2202 
2203 /// \brief Parse optional attributes.
2204 ///
2205 ///   attributes:
2206 ///     attribute attributes
2207 ///     attribute
2208 ///
2209 ///   attribute:
2210 ///     [ identifier ]
2211 ///
2212 /// \param Attrs Will be filled in with the parsed attributes.
2213 ///
2214 /// \returns true if an error occurred, false otherwise.
2215 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
2216   bool HadError = false;
2217 
2218   while (Tok.is(MMToken::LSquare)) {
2219     // Consume the '['.
2220     SourceLocation LSquareLoc = consumeToken();
2221 
2222     // Check whether we have an attribute name here.
2223     if (!Tok.is(MMToken::Identifier)) {
2224       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
2225       skipUntil(MMToken::RSquare);
2226       if (Tok.is(MMToken::RSquare))
2227         consumeToken();
2228       HadError = true;
2229     }
2230 
2231     // Decode the attribute name.
2232     AttributeKind Attribute
2233       = llvm::StringSwitch<AttributeKind>(Tok.getString())
2234           .Case("exhaustive", AT_exhaustive)
2235           .Case("extern_c", AT_extern_c)
2236           .Case("system", AT_system)
2237           .Default(AT_unknown);
2238     switch (Attribute) {
2239     case AT_unknown:
2240       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
2241         << Tok.getString();
2242       break;
2243 
2244     case AT_system:
2245       Attrs.IsSystem = true;
2246       break;
2247 
2248     case AT_extern_c:
2249       Attrs.IsExternC = true;
2250       break;
2251 
2252     case AT_exhaustive:
2253       Attrs.IsExhaustive = true;
2254       break;
2255     }
2256     consumeToken();
2257 
2258     // Consume the ']'.
2259     if (!Tok.is(MMToken::RSquare)) {
2260       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
2261       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
2262       skipUntil(MMToken::RSquare);
2263       HadError = true;
2264     }
2265 
2266     if (Tok.is(MMToken::RSquare))
2267       consumeToken();
2268   }
2269 
2270   return HadError;
2271 }
2272 
2273 /// \brief Parse a module map file.
2274 ///
2275 ///   module-map-file:
2276 ///     module-declaration*
2277 bool ModuleMapParser::parseModuleMapFile() {
2278   do {
2279     switch (Tok.Kind) {
2280     case MMToken::EndOfFile:
2281       return HadError;
2282 
2283     case MMToken::ExplicitKeyword:
2284     case MMToken::ExternKeyword:
2285     case MMToken::ModuleKeyword:
2286     case MMToken::FrameworkKeyword:
2287       parseModuleDecl();
2288       break;
2289 
2290     case MMToken::Comma:
2291     case MMToken::ConfigMacros:
2292     case MMToken::Conflict:
2293     case MMToken::Exclaim:
2294     case MMToken::ExcludeKeyword:
2295     case MMToken::ExportKeyword:
2296     case MMToken::HeaderKeyword:
2297     case MMToken::Identifier:
2298     case MMToken::LBrace:
2299     case MMToken::LinkKeyword:
2300     case MMToken::LSquare:
2301     case MMToken::Period:
2302     case MMToken::PrivateKeyword:
2303     case MMToken::RBrace:
2304     case MMToken::RSquare:
2305     case MMToken::RequiresKeyword:
2306     case MMToken::Star:
2307     case MMToken::StringLiteral:
2308     case MMToken::TextualKeyword:
2309     case MMToken::UmbrellaKeyword:
2310     case MMToken::UseKeyword:
2311       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
2312       HadError = true;
2313       consumeToken();
2314       break;
2315     }
2316   } while (true);
2317 }
2318 
2319 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) {
2320   llvm::DenseMap<const FileEntry *, bool>::iterator Known
2321     = ParsedModuleMap.find(File);
2322   if (Known != ParsedModuleMap.end())
2323     return Known->second;
2324 
2325   assert(Target && "Missing target information");
2326   auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
2327   FileID ID = SourceMgr.createFileID(File, SourceLocation(), FileCharacter);
2328   const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID);
2329   if (!Buffer)
2330     return ParsedModuleMap[File] = true;
2331 
2332   // Find the directory for the module. For frameworks, that may require going
2333   // up from the 'Modules' directory.
2334   const DirectoryEntry *Dir = File->getDir();
2335   StringRef DirName(Dir->getName());
2336   if (llvm::sys::path::filename(DirName) == "Modules") {
2337     DirName = llvm::sys::path::parent_path(DirName);
2338     if (DirName.endswith(".framework"))
2339       Dir = SourceMgr.getFileManager().getDirectory(DirName);
2340     assert(Dir && "parent must exist");
2341   }
2342 
2343   // Parse this module map file.
2344   Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts);
2345   ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir,
2346                          BuiltinIncludeDir, IsSystem);
2347   bool Result = Parser.parseModuleMapFile();
2348   ParsedModuleMap[File] = Result;
2349   return Result;
2350 }
2351