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