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