xref: /llvm-project/clang/lib/Lex/ModuleMap.cpp (revision ec87a50a3e9175441b5e4b38cc477881c078edf8)
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) {}
93 
94 ModuleMap::~ModuleMap() {
95   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
96                                         IEnd = Modules.end();
97        I != IEnd; ++I) {
98     delete I->getValue();
99   }
100 }
101 
102 void ModuleMap::setTarget(const TargetInfo &Target) {
103   assert((!this->Target || this->Target == &Target) &&
104          "Improper target override");
105   this->Target = &Target;
106 }
107 
108 /// \brief "Sanitize" a filename so that it can be used as an identifier.
109 static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
110                                               SmallVectorImpl<char> &Buffer) {
111   if (Name.empty())
112     return Name;
113 
114   if (!isValidIdentifier(Name)) {
115     // If we don't already have something with the form of an identifier,
116     // create a buffer with the sanitized name.
117     Buffer.clear();
118     if (isDigit(Name[0]))
119       Buffer.push_back('_');
120     Buffer.reserve(Buffer.size() + Name.size());
121     for (unsigned I = 0, N = Name.size(); I != N; ++I) {
122       if (isIdentifierBody(Name[I]))
123         Buffer.push_back(Name[I]);
124       else
125         Buffer.push_back('_');
126     }
127 
128     Name = StringRef(Buffer.data(), Buffer.size());
129   }
130 
131   while (llvm::StringSwitch<bool>(Name)
132 #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
133 #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
134 #include "clang/Basic/TokenKinds.def"
135            .Default(false)) {
136     if (Name.data() != Buffer.data())
137       Buffer.append(Name.begin(), Name.end());
138     Buffer.push_back('_');
139     Name = StringRef(Buffer.data(), Buffer.size());
140   }
141 
142   return Name;
143 }
144 
145 /// \brief Determine whether the given file name is the name of a builtin
146 /// header, supplied by Clang to replace, override, or augment existing system
147 /// headers.
148 static bool isBuiltinHeader(StringRef FileName) {
149   return llvm::StringSwitch<bool>(FileName)
150            .Case("float.h", true)
151            .Case("iso646.h", true)
152            .Case("limits.h", true)
153            .Case("stdalign.h", true)
154            .Case("stdarg.h", true)
155            .Case("stdbool.h", true)
156            .Case("stddef.h", true)
157            .Case("stdint.h", true)
158            .Case("tgmath.h", true)
159            .Case("unwind.h", true)
160            .Default(false);
161 }
162 
163 ModuleMap::HeadersMap::iterator
164 ModuleMap::findKnownHeader(const FileEntry *File) {
165   HeadersMap::iterator Known = Headers.find(File);
166   if (Known == Headers.end() && File->getDir() == BuiltinIncludeDir &&
167       isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
168     HeaderInfo.loadTopLevelSystemModules();
169     return Headers.find(File);
170   }
171   return Known;
172 }
173 
174 ModuleMap::KnownHeader
175 ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
176                     SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) {
177   const DirectoryEntry *Dir = File->getDir();
178   assert(Dir && "file in no directory");
179 
180   // Note: as an egregious but useful hack we use the real path here, because
181   // frameworks moving from top-level frameworks to embedded frameworks tend
182   // to be symlinked from the top-level location to the embedded location,
183   // and we need to resolve lookups as if we had found the embedded location.
184   StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
185 
186   // Keep walking up the directory hierarchy, looking for a directory with
187   // an umbrella header.
188   do {
189     auto KnownDir = UmbrellaDirs.find(Dir);
190     if (KnownDir != UmbrellaDirs.end())
191       return KnownHeader(KnownDir->second, NormalHeader);
192 
193     IntermediateDirs.push_back(Dir);
194 
195     // Retrieve our parent path.
196     DirName = llvm::sys::path::parent_path(DirName);
197     if (DirName.empty())
198       break;
199 
200     // Resolve the parent path to a directory entry.
201     Dir = SourceMgr.getFileManager().getDirectory(DirName);
202   } while (Dir);
203   return KnownHeader();
204 }
205 
206 // Returns true if RequestingModule directly uses RequestedModule.
207 static bool directlyUses(const Module *RequestingModule,
208                          const Module *RequestedModule) {
209   return std::find(RequestingModule->DirectUses.begin(),
210                    RequestingModule->DirectUses.end(),
211                    RequestedModule) != RequestingModule->DirectUses.end();
212 }
213 
214 static bool violatesPrivateInclude(Module *RequestingModule,
215                                    const FileEntry *IncFileEnt,
216                                    ModuleMap::ModuleHeaderRole Role,
217                                    Module *RequestedModule) {
218   bool IsPrivateRole = Role & ModuleMap::PrivateHeader;
219 #ifndef NDEBUG
220   // Check for consistency between the module header role
221   // as obtained from the lookup and as obtained from the module.
222   // This check is not cheap, so enable it only for debugging.
223   bool IsPrivate = false;
224   SmallVectorImpl<Module::Header> *HeaderList[] =
225       {&RequestedModule->Headers[Module::HK_Private],
226        &RequestedModule->Headers[Module::HK_PrivateTextual]};
227   for (auto *Hdrs : HeaderList)
228     IsPrivate |=
229         std::find_if(Hdrs->begin(), Hdrs->end(), [&](const Module::Header &H) {
230           return H.Entry == IncFileEnt;
231         }) != Hdrs->end();
232   assert(IsPrivate == IsPrivateRole && "inconsistent headers and roles");
233 #endif
234   return IsPrivateRole &&
235          RequestedModule->getTopLevelModule() != RequestingModule;
236 }
237 
238 static Module *getTopLevelOrNull(Module *M) {
239   return M ? M->getTopLevelModule() : nullptr;
240 }
241 
242 void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule,
243                                         SourceLocation FilenameLoc,
244                                         StringRef Filename,
245                                         const FileEntry *File) {
246   // No errors for indirect modules. This may be a bit of a problem for modules
247   // with no source files.
248   if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule))
249     return;
250 
251   if (RequestingModule)
252     resolveUses(RequestingModule, /*Complain=*/false);
253 
254   bool Excluded = false;
255   Module *Private = nullptr;
256   Module *NotUsed = nullptr;
257 
258   HeadersMap::iterator Known = findKnownHeader(File);
259   if (Known != Headers.end()) {
260     for (const KnownHeader &Header : Known->second) {
261       // If 'File' is part of 'RequestingModule' we can definitely include it.
262       if (Header.getModule() == 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           !directlyUses(RequestingModule, 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::error_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::error_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::error_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           !directlyUses(RequestingModule, 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);
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   InferredModuleAllowedBy[Result] = ModuleMapFile;
698   Result->IsInferred = true;
699   if (LangOpts.CurrentModule == ModuleName) {
700     SourceModule = Result;
701     SourceModuleName = ModuleName;
702   }
703 
704   Result->IsSystem |= Attrs.IsSystem;
705   Result->IsExternC |= Attrs.IsExternC;
706   Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive;
707 
708   if (!Parent)
709     Modules[ModuleName] = Result;
710 
711   // umbrella header "umbrella-header-name"
712   Result->Umbrella = UmbrellaHeader;
713   Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader));
714   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
715 
716   // export *
717   Result->Exports.push_back(Module::ExportDecl(nullptr, true));
718 
719   // module * { export * }
720   Result->InferSubmodules = true;
721   Result->InferExportWildcard = true;
722 
723   // Look for subframeworks.
724   std::error_code EC;
725   SmallString<128> SubframeworksDirName
726     = StringRef(FrameworkDir->getName());
727   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
728   llvm::sys::path::native(SubframeworksDirName);
729   for (llvm::sys::fs::directory_iterator
730          Dir(SubframeworksDirName.str(), 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.str();
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.str());
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.str());
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.str());
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 uses declaration.
1916 ///
1917 ///   uses-declaration:
1918 ///     'uses' wildcard-module-id
1919 void ModuleMapParser::parseUseDecl() {
1920   assert(Tok.is(MMToken::UseKeyword));
1921   consumeToken();
1922   // Parse the module-id.
1923   ModuleId ParsedModuleId;
1924   parseModuleId(ParsedModuleId);
1925 
1926   ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
1927 }
1928 
1929 /// \brief Parse a link declaration.
1930 ///
1931 ///   module-declaration:
1932 ///     'link' 'framework'[opt] string-literal
1933 void ModuleMapParser::parseLinkDecl() {
1934   assert(Tok.is(MMToken::LinkKeyword));
1935   SourceLocation LinkLoc = consumeToken();
1936 
1937   // Parse the optional 'framework' keyword.
1938   bool IsFramework = false;
1939   if (Tok.is(MMToken::FrameworkKeyword)) {
1940     consumeToken();
1941     IsFramework = true;
1942   }
1943 
1944   // Parse the library name
1945   if (!Tok.is(MMToken::StringLiteral)) {
1946     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
1947       << IsFramework << SourceRange(LinkLoc);
1948     HadError = true;
1949     return;
1950   }
1951 
1952   std::string LibraryName = Tok.getString();
1953   consumeToken();
1954   ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
1955                                                             IsFramework));
1956 }
1957 
1958 /// \brief Parse a configuration macro declaration.
1959 ///
1960 ///   module-declaration:
1961 ///     'config_macros' attributes[opt] config-macro-list?
1962 ///
1963 ///   config-macro-list:
1964 ///     identifier (',' identifier)?
1965 void ModuleMapParser::parseConfigMacros() {
1966   assert(Tok.is(MMToken::ConfigMacros));
1967   SourceLocation ConfigMacrosLoc = consumeToken();
1968 
1969   // Only top-level modules can have configuration macros.
1970   if (ActiveModule->Parent) {
1971     Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
1972   }
1973 
1974   // Parse the optional attributes.
1975   Attributes Attrs;
1976   parseOptionalAttributes(Attrs);
1977   if (Attrs.IsExhaustive && !ActiveModule->Parent) {
1978     ActiveModule->ConfigMacrosExhaustive = true;
1979   }
1980 
1981   // If we don't have an identifier, we're done.
1982   // FIXME: Support macros with the same name as a keyword here.
1983   if (!Tok.is(MMToken::Identifier))
1984     return;
1985 
1986   // Consume the first identifier.
1987   if (!ActiveModule->Parent) {
1988     ActiveModule->ConfigMacros.push_back(Tok.getString().str());
1989   }
1990   consumeToken();
1991 
1992   do {
1993     // If there's a comma, consume it.
1994     if (!Tok.is(MMToken::Comma))
1995       break;
1996     consumeToken();
1997 
1998     // We expect to see a macro name here.
1999     // FIXME: Support macros with the same name as a keyword here.
2000     if (!Tok.is(MMToken::Identifier)) {
2001       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
2002       break;
2003     }
2004 
2005     // Consume the macro name.
2006     if (!ActiveModule->Parent) {
2007       ActiveModule->ConfigMacros.push_back(Tok.getString().str());
2008     }
2009     consumeToken();
2010   } while (true);
2011 }
2012 
2013 /// \brief Format a module-id into a string.
2014 static std::string formatModuleId(const ModuleId &Id) {
2015   std::string result;
2016   {
2017     llvm::raw_string_ostream OS(result);
2018 
2019     for (unsigned I = 0, N = Id.size(); I != N; ++I) {
2020       if (I)
2021         OS << ".";
2022       OS << Id[I].first;
2023     }
2024   }
2025 
2026   return result;
2027 }
2028 
2029 /// \brief Parse a conflict declaration.
2030 ///
2031 ///   module-declaration:
2032 ///     'conflict' module-id ',' string-literal
2033 void ModuleMapParser::parseConflict() {
2034   assert(Tok.is(MMToken::Conflict));
2035   SourceLocation ConflictLoc = consumeToken();
2036   Module::UnresolvedConflict Conflict;
2037 
2038   // Parse the module-id.
2039   if (parseModuleId(Conflict.Id))
2040     return;
2041 
2042   // Parse the ','.
2043   if (!Tok.is(MMToken::Comma)) {
2044     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
2045       << SourceRange(ConflictLoc);
2046     return;
2047   }
2048   consumeToken();
2049 
2050   // Parse the message.
2051   if (!Tok.is(MMToken::StringLiteral)) {
2052     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
2053       << formatModuleId(Conflict.Id);
2054     return;
2055   }
2056   Conflict.Message = Tok.getString().str();
2057   consumeToken();
2058 
2059   // Add this unresolved conflict.
2060   ActiveModule->UnresolvedConflicts.push_back(Conflict);
2061 }
2062 
2063 /// \brief Parse an inferred module declaration (wildcard modules).
2064 ///
2065 ///   module-declaration:
2066 ///     'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
2067 ///       { inferred-module-member* }
2068 ///
2069 ///   inferred-module-member:
2070 ///     'export' '*'
2071 ///     'exclude' identifier
2072 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
2073   assert(Tok.is(MMToken::Star));
2074   SourceLocation StarLoc = consumeToken();
2075   bool Failed = false;
2076 
2077   // Inferred modules must be submodules.
2078   if (!ActiveModule && !Framework) {
2079     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
2080     Failed = true;
2081   }
2082 
2083   if (ActiveModule) {
2084     // Inferred modules must have umbrella directories.
2085     if (!Failed && ActiveModule->IsAvailable &&
2086         !ActiveModule->getUmbrellaDir()) {
2087       Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
2088       Failed = true;
2089     }
2090 
2091     // Check for redefinition of an inferred module.
2092     if (!Failed && ActiveModule->InferSubmodules) {
2093       Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
2094       if (ActiveModule->InferredSubmoduleLoc.isValid())
2095         Diags.Report(ActiveModule->InferredSubmoduleLoc,
2096                      diag::note_mmap_prev_definition);
2097       Failed = true;
2098     }
2099 
2100     // Check for the 'framework' keyword, which is not permitted here.
2101     if (Framework) {
2102       Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
2103       Framework = false;
2104     }
2105   } else if (Explicit) {
2106     Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
2107     Explicit = false;
2108   }
2109 
2110   // If there were any problems with this inferred submodule, skip its body.
2111   if (Failed) {
2112     if (Tok.is(MMToken::LBrace)) {
2113       consumeToken();
2114       skipUntil(MMToken::RBrace);
2115       if (Tok.is(MMToken::RBrace))
2116         consumeToken();
2117     }
2118     HadError = true;
2119     return;
2120   }
2121 
2122   // Parse optional attributes.
2123   Attributes Attrs;
2124   parseOptionalAttributes(Attrs);
2125 
2126   if (ActiveModule) {
2127     // Note that we have an inferred submodule.
2128     ActiveModule->InferSubmodules = true;
2129     ActiveModule->InferredSubmoduleLoc = StarLoc;
2130     ActiveModule->InferExplicitSubmodules = Explicit;
2131   } else {
2132     // We'll be inferring framework modules for this directory.
2133     Map.InferredDirectories[Directory].InferModules = true;
2134     Map.InferredDirectories[Directory].Attrs = Attrs;
2135     Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile;
2136     // FIXME: Handle the 'framework' keyword.
2137   }
2138 
2139   // Parse the opening brace.
2140   if (!Tok.is(MMToken::LBrace)) {
2141     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
2142     HadError = true;
2143     return;
2144   }
2145   SourceLocation LBraceLoc = consumeToken();
2146 
2147   // Parse the body of the inferred submodule.
2148   bool Done = false;
2149   do {
2150     switch (Tok.Kind) {
2151     case MMToken::EndOfFile:
2152     case MMToken::RBrace:
2153       Done = true;
2154       break;
2155 
2156     case MMToken::ExcludeKeyword: {
2157       if (ActiveModule) {
2158         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2159           << (ActiveModule != nullptr);
2160         consumeToken();
2161         break;
2162       }
2163 
2164       consumeToken();
2165       // FIXME: Support string-literal module names here.
2166       if (!Tok.is(MMToken::Identifier)) {
2167         Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
2168         break;
2169       }
2170 
2171       Map.InferredDirectories[Directory].ExcludedModules
2172         .push_back(Tok.getString());
2173       consumeToken();
2174       break;
2175     }
2176 
2177     case MMToken::ExportKeyword:
2178       if (!ActiveModule) {
2179         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2180           << (ActiveModule != nullptr);
2181         consumeToken();
2182         break;
2183       }
2184 
2185       consumeToken();
2186       if (Tok.is(MMToken::Star))
2187         ActiveModule->InferExportWildcard = true;
2188       else
2189         Diags.Report(Tok.getLocation(),
2190                      diag::err_mmap_expected_export_wildcard);
2191       consumeToken();
2192       break;
2193 
2194     case MMToken::ExplicitKeyword:
2195     case MMToken::ModuleKeyword:
2196     case MMToken::HeaderKeyword:
2197     case MMToken::PrivateKeyword:
2198     case MMToken::UmbrellaKeyword:
2199     default:
2200       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2201           << (ActiveModule != nullptr);
2202       consumeToken();
2203       break;
2204     }
2205   } while (!Done);
2206 
2207   if (Tok.is(MMToken::RBrace))
2208     consumeToken();
2209   else {
2210     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
2211     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
2212     HadError = true;
2213   }
2214 }
2215 
2216 /// \brief Parse optional attributes.
2217 ///
2218 ///   attributes:
2219 ///     attribute attributes
2220 ///     attribute
2221 ///
2222 ///   attribute:
2223 ///     [ identifier ]
2224 ///
2225 /// \param Attrs Will be filled in with the parsed attributes.
2226 ///
2227 /// \returns true if an error occurred, false otherwise.
2228 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
2229   bool HadError = false;
2230 
2231   while (Tok.is(MMToken::LSquare)) {
2232     // Consume the '['.
2233     SourceLocation LSquareLoc = consumeToken();
2234 
2235     // Check whether we have an attribute name here.
2236     if (!Tok.is(MMToken::Identifier)) {
2237       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
2238       skipUntil(MMToken::RSquare);
2239       if (Tok.is(MMToken::RSquare))
2240         consumeToken();
2241       HadError = true;
2242     }
2243 
2244     // Decode the attribute name.
2245     AttributeKind Attribute
2246       = llvm::StringSwitch<AttributeKind>(Tok.getString())
2247           .Case("exhaustive", AT_exhaustive)
2248           .Case("extern_c", AT_extern_c)
2249           .Case("system", AT_system)
2250           .Default(AT_unknown);
2251     switch (Attribute) {
2252     case AT_unknown:
2253       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
2254         << Tok.getString();
2255       break;
2256 
2257     case AT_system:
2258       Attrs.IsSystem = true;
2259       break;
2260 
2261     case AT_extern_c:
2262       Attrs.IsExternC = true;
2263       break;
2264 
2265     case AT_exhaustive:
2266       Attrs.IsExhaustive = true;
2267       break;
2268     }
2269     consumeToken();
2270 
2271     // Consume the ']'.
2272     if (!Tok.is(MMToken::RSquare)) {
2273       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
2274       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
2275       skipUntil(MMToken::RSquare);
2276       HadError = true;
2277     }
2278 
2279     if (Tok.is(MMToken::RSquare))
2280       consumeToken();
2281   }
2282 
2283   return HadError;
2284 }
2285 
2286 /// \brief Parse a module map file.
2287 ///
2288 ///   module-map-file:
2289 ///     module-declaration*
2290 bool ModuleMapParser::parseModuleMapFile() {
2291   do {
2292     switch (Tok.Kind) {
2293     case MMToken::EndOfFile:
2294       return HadError;
2295 
2296     case MMToken::ExplicitKeyword:
2297     case MMToken::ExternKeyword:
2298     case MMToken::ModuleKeyword:
2299     case MMToken::FrameworkKeyword:
2300       parseModuleDecl();
2301       break;
2302 
2303     case MMToken::Comma:
2304     case MMToken::ConfigMacros:
2305     case MMToken::Conflict:
2306     case MMToken::Exclaim:
2307     case MMToken::ExcludeKeyword:
2308     case MMToken::ExportKeyword:
2309     case MMToken::HeaderKeyword:
2310     case MMToken::Identifier:
2311     case MMToken::LBrace:
2312     case MMToken::LinkKeyword:
2313     case MMToken::LSquare:
2314     case MMToken::Period:
2315     case MMToken::PrivateKeyword:
2316     case MMToken::RBrace:
2317     case MMToken::RSquare:
2318     case MMToken::RequiresKeyword:
2319     case MMToken::Star:
2320     case MMToken::StringLiteral:
2321     case MMToken::TextualKeyword:
2322     case MMToken::UmbrellaKeyword:
2323     case MMToken::UseKeyword:
2324       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
2325       HadError = true;
2326       consumeToken();
2327       break;
2328     }
2329   } while (true);
2330 }
2331 
2332 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
2333                                    const DirectoryEntry *Dir) {
2334   llvm::DenseMap<const FileEntry *, bool>::iterator Known
2335     = ParsedModuleMap.find(File);
2336   if (Known != ParsedModuleMap.end())
2337     return Known->second;
2338 
2339   assert(Target && "Missing target information");
2340   auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
2341   FileID ID = SourceMgr.createFileID(File, SourceLocation(), FileCharacter);
2342   const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID);
2343   if (!Buffer)
2344     return ParsedModuleMap[File] = true;
2345 
2346   // Parse this module map file.
2347   Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts);
2348   ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir,
2349                          BuiltinIncludeDir, IsSystem);
2350   bool Result = Parser.parseModuleMapFile();
2351   ParsedModuleMap[File] = Result;
2352   return Result;
2353 }
2354