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