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