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