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