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