1 //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ModuleMap implementation, which describes the layout
11 // of a module as it relates to headers.
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"
34 #if defined(LLVM_ON_UNIX)
37 using namespace clang;
39 Module::HeaderKind ModuleMap::headerRoleToKind(ModuleHeaderRole Role) {
41 default: llvm_unreachable("unknown header role");
43 return Module::HK_Normal;
45 return Module::HK_Private;
47 return Module::HK_Textual;
48 case PrivateHeader | TextualHeader:
49 return Module::HK_PrivateTextual;
53 ModuleMap::ModuleHeaderRole
54 ModuleMap::headerKindToRole(Module::HeaderKind Kind) {
56 case Module::HK_Normal:
58 case Module::HK_Private:
60 case Module::HK_Textual:
62 case Module::HK_PrivateTextual:
63 return ModuleHeaderRole(PrivateHeader | TextualHeader);
64 case Module::HK_Excluded:
65 llvm_unreachable("unexpected header kind");
67 llvm_unreachable("unknown header kind");
71 ModuleMap::resolveExport(Module *Mod,
72 const Module::UnresolvedExportDecl &Unresolved,
73 bool Complain) const {
74 // We may have just a wildcard.
75 if (Unresolved.Id.empty()) {
76 assert(Unresolved.Wildcard && "Invalid unresolved export");
77 return Module::ExportDecl(nullptr, true);
80 // Resolve the module-id.
81 Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain);
83 return Module::ExportDecl();
85 return Module::ExportDecl(Context, Unresolved.Wildcard);
88 Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod,
89 bool Complain) const {
90 // Find the starting module.
91 Module *Context = lookupModuleUnqualified(Id[0].first, Mod);
94 Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified)
95 << Id[0].first << Mod->getFullModuleName();
100 // Dig into the module path.
101 for (unsigned I = 1, N = Id.size(); I != N; ++I) {
102 Module *Sub = lookupModuleQualified(Id[I].first, Context);
105 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
106 << Id[I].first << Context->getFullModuleName()
107 << SourceRange(Id[0].second, Id[I-1].second);
118 /// \brief Append to \p Paths the set of paths needed to get to the
119 /// subframework in which the given module lives.
120 static void appendSubframeworkPaths(Module *Mod,
121 SmallVectorImpl<char> &Path) {
122 // Collect the framework names from the given module to the top-level module.
123 SmallVector<StringRef, 2> Paths;
124 for (; Mod; Mod = Mod->Parent) {
125 if (Mod->IsFramework)
126 Paths.push_back(Mod->Name);
132 // Add Frameworks/Name.framework for each subframework.
133 for (unsigned I = Paths.size() - 1; I != 0; --I)
134 llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework");
138 ModuleMap::findHeader(Module *M,
139 const Module::UnresolvedHeaderDirective &Header,
140 SmallVectorImpl<char> &RelativePathName) {
141 auto GetFile = [&](StringRef Filename) -> const FileEntry * {
142 auto *File = SourceMgr.getFileManager().getFile(Filename);
144 (Header.Size && File->getSize() != *Header.Size) ||
145 (Header.ModTime && File->getModificationTime() != *Header.ModTime))
150 if (llvm::sys::path::is_absolute(Header.FileName)) {
151 RelativePathName.clear();
152 RelativePathName.append(Header.FileName.begin(), Header.FileName.end());
153 return GetFile(Header.FileName);
156 // Search for the header file within the module's home directory.
157 auto *Directory = M->Directory;
158 SmallString<128> FullPathName(Directory->getName());
159 unsigned FullPathLength = FullPathName.size();
161 if (M->isPartOfFramework()) {
162 appendSubframeworkPaths(M, RelativePathName);
163 unsigned RelativePathLength = RelativePathName.size();
165 // Check whether this file is in the public headers.
166 llvm::sys::path::append(RelativePathName, "Headers", Header.FileName);
167 llvm::sys::path::append(FullPathName, RelativePathName);
168 if (auto *File = GetFile(FullPathName))
171 // Check whether this file is in the private headers.
172 // Ideally, private modules in the form 'FrameworkName.Private' should
173 // be defined as 'module FrameworkName.Private', and not as
174 // 'framework module FrameworkName.Private', since a 'Private.Framework'
175 // does not usually exist. However, since both are currently widely used
176 // for private modules, make sure we find the right path in both cases.
177 if (M->IsFramework && M->Name == "Private")
178 RelativePathName.clear();
180 RelativePathName.resize(RelativePathLength);
181 FullPathName.resize(FullPathLength);
182 llvm::sys::path::append(RelativePathName, "PrivateHeaders",
184 llvm::sys::path::append(FullPathName, RelativePathName);
185 return GetFile(FullPathName);
188 // Lookup for normal headers.
189 llvm::sys::path::append(RelativePathName, Header.FileName);
190 llvm::sys::path::append(FullPathName, RelativePathName);
191 return GetFile(FullPathName);
194 void ModuleMap::resolveHeader(Module *Mod,
195 const Module::UnresolvedHeaderDirective &Header) {
196 SmallString<128> RelativePathName;
197 if (const FileEntry *File = findHeader(Mod, Header, RelativePathName)) {
198 if (Header.IsUmbrella) {
199 const DirectoryEntry *UmbrellaDir = File->getDir();
200 if (Module *UmbrellaMod = UmbrellaDirs[UmbrellaDir])
201 Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
202 << UmbrellaMod->getFullModuleName();
204 // Record this umbrella header.
205 setUmbrellaHeader(Mod, File, RelativePathName.str());
207 Module::Header H = {RelativePathName.str(), File};
208 if (Header.Kind == Module::HK_Excluded)
209 excludeHeader(Mod, H);
211 addHeader(Mod, H, headerKindToRole(Header.Kind));
213 } else if (Header.HasBuiltinHeader && !Header.Size && !Header.ModTime) {
214 // There's a builtin header but no corresponding on-disk header. Assume
215 // this was supposed to modularize the builtin header alone.
216 } else if (Header.Kind == Module::HK_Excluded) {
217 // Ignore missing excluded header files. They're optional anyway.
219 // If we find a module that has a missing header, we mark this module as
220 // unavailable and store the header directive for displaying diagnostics.
221 Mod->MissingHeaders.push_back(Header);
222 // A missing header with stat information doesn't make the module
223 // unavailable; this keeps our behavior consistent as headers are lazily
224 // resolved. (Such a module still can't be built though, except from
225 // preprocessed source.)
226 if (!Header.Size && !Header.ModTime)
227 Mod->markUnavailable();
231 bool ModuleMap::resolveAsBuiltinHeader(
232 Module *Mod, const Module::UnresolvedHeaderDirective &Header) {
233 if (Header.Kind == Module::HK_Excluded ||
234 llvm::sys::path::is_absolute(Header.FileName) ||
235 Mod->isPartOfFramework() || !Mod->IsSystem || Header.IsUmbrella ||
236 !BuiltinIncludeDir || BuiltinIncludeDir == Mod->Directory ||
237 !isBuiltinHeader(Header.FileName))
240 // This is a system module with a top-level header. This header
241 // may have a counterpart (or replacement) in the set of headers
242 // supplied by Clang. Find that builtin header.
243 SmallString<128> Path;
244 llvm::sys::path::append(Path, BuiltinIncludeDir->getName(), Header.FileName);
245 auto *File = SourceMgr.getFileManager().getFile(Path);
249 auto Role = headerKindToRole(Header.Kind);
250 Module::Header H = {Path.str(), File};
251 addHeader(Mod, H, Role);
255 ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
256 const LangOptions &LangOpts, const TargetInfo *Target,
257 HeaderSearch &HeaderInfo)
258 : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
259 HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr),
260 SourceModule(nullptr), NumCreatedModules(0) {
261 MMapLangOpts.LineComment = true;
264 ModuleMap::~ModuleMap() {
265 for (auto &M : Modules)
269 void ModuleMap::setTarget(const TargetInfo &Target) {
270 assert((!this->Target || this->Target == &Target) &&
271 "Improper target override");
272 this->Target = &Target;
275 /// \brief "Sanitize" a filename so that it can be used as an identifier.
276 static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
277 SmallVectorImpl<char> &Buffer) {
281 if (!isValidIdentifier(Name)) {
282 // If we don't already have something with the form of an identifier,
283 // create a buffer with the sanitized name.
285 if (isDigit(Name[0]))
286 Buffer.push_back('_');
287 Buffer.reserve(Buffer.size() + Name.size());
288 for (unsigned I = 0, N = Name.size(); I != N; ++I) {
289 if (isIdentifierBody(Name[I]))
290 Buffer.push_back(Name[I]);
292 Buffer.push_back('_');
295 Name = StringRef(Buffer.data(), Buffer.size());
298 while (llvm::StringSwitch<bool>(Name)
299 #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
300 #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
301 #include "clang/Basic/TokenKinds.def"
303 if (Name.data() != Buffer.data())
304 Buffer.append(Name.begin(), Name.end());
305 Buffer.push_back('_');
306 Name = StringRef(Buffer.data(), Buffer.size());
312 /// \brief Determine whether the given file name is the name of a builtin
313 /// header, supplied by Clang to replace, override, or augment existing system
315 bool ModuleMap::isBuiltinHeader(StringRef FileName) {
316 return llvm::StringSwitch<bool>(FileName)
317 .Case("float.h", true)
318 .Case("iso646.h", true)
319 .Case("limits.h", true)
320 .Case("stdalign.h", true)
321 .Case("stdarg.h", true)
322 .Case("stdatomic.h", true)
323 .Case("stdbool.h", true)
324 .Case("stddef.h", true)
325 .Case("stdint.h", true)
326 .Case("tgmath.h", true)
327 .Case("unwind.h", true)
331 ModuleMap::HeadersMap::iterator
332 ModuleMap::findKnownHeader(const FileEntry *File) {
333 resolveHeaderDirectives(File);
334 HeadersMap::iterator Known = Headers.find(File);
335 if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps &&
336 Known == Headers.end() && File->getDir() == BuiltinIncludeDir &&
337 ModuleMap::isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
338 HeaderInfo.loadTopLevelSystemModules();
339 return Headers.find(File);
344 ModuleMap::KnownHeader
345 ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
346 SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) {
347 if (UmbrellaDirs.empty())
348 return KnownHeader();
350 const DirectoryEntry *Dir = File->getDir();
351 assert(Dir && "file in no directory");
353 // Note: as an egregious but useful hack we use the real path here, because
354 // frameworks moving from top-level frameworks to embedded frameworks tend
355 // to be symlinked from the top-level location to the embedded location,
356 // and we need to resolve lookups as if we had found the embedded location.
357 StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
359 // Keep walking up the directory hierarchy, looking for a directory with
360 // an umbrella header.
362 auto KnownDir = UmbrellaDirs.find(Dir);
363 if (KnownDir != UmbrellaDirs.end())
364 return KnownHeader(KnownDir->second, NormalHeader);
366 IntermediateDirs.push_back(Dir);
368 // Retrieve our parent path.
369 DirName = llvm::sys::path::parent_path(DirName);
373 // Resolve the parent path to a directory entry.
374 Dir = SourceMgr.getFileManager().getDirectory(DirName);
376 return KnownHeader();
379 static bool violatesPrivateInclude(Module *RequestingModule,
380 const FileEntry *IncFileEnt,
381 ModuleMap::KnownHeader Header) {
383 if (Header.getRole() & ModuleMap::PrivateHeader) {
384 // Check for consistency between the module header role
385 // as obtained from the lookup and as obtained from the module.
386 // This check is not cheap, so enable it only for debugging.
387 bool IsPrivate = false;
388 SmallVectorImpl<Module::Header> *HeaderList[] = {
389 &Header.getModule()->Headers[Module::HK_Private],
390 &Header.getModule()->Headers[Module::HK_PrivateTextual]};
391 for (auto *Hs : HeaderList)
393 std::find_if(Hs->begin(), Hs->end(), [&](const Module::Header &H) {
394 return H.Entry == IncFileEnt;
396 assert(IsPrivate && "inconsistent headers and roles");
399 return !Header.isAccessibleFrom(RequestingModule);
402 static Module *getTopLevelOrNull(Module *M) {
403 return M ? M->getTopLevelModule() : nullptr;
406 void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule,
407 bool RequestingModuleIsModuleInterface,
408 SourceLocation FilenameLoc,
410 const FileEntry *File) {
411 // No errors for indirect modules. This may be a bit of a problem for modules
412 // with no source files.
413 if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule))
416 if (RequestingModule) {
417 resolveUses(RequestingModule, /*Complain=*/false);
418 resolveHeaderDirectives(RequestingModule);
421 bool Excluded = false;
422 Module *Private = nullptr;
423 Module *NotUsed = nullptr;
425 HeadersMap::iterator Known = findKnownHeader(File);
426 if (Known != Headers.end()) {
427 for (const KnownHeader &Header : Known->second) {
428 // Remember private headers for later printing of a diagnostic.
429 if (violatesPrivateInclude(RequestingModule, File, Header)) {
430 Private = Header.getModule();
434 // If uses need to be specified explicitly, we are only allowed to return
435 // modules that are explicitly used by the requesting module.
436 if (RequestingModule && LangOpts.ModulesDeclUse &&
437 !RequestingModule->directlyUses(Header.getModule())) {
438 NotUsed = Header.getModule();
442 // We have found a module that we can happily use.
449 // We have found a header, but it is private.
451 Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module)
456 // We have found a module, but we don't use it.
458 Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
459 << RequestingModule->getFullModuleName() << Filename;
463 if (Excluded || isHeaderInUmbrellaDirs(File))
466 // At this point, only non-modular includes remain.
468 if (LangOpts.ModulesStrictDeclUse) {
469 Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
470 << RequestingModule->getFullModuleName() << Filename;
471 } else if (RequestingModule && RequestingModuleIsModuleInterface &&
472 LangOpts.isCompilingModule()) {
473 // Do not diagnose when we are not compiling a module.
474 diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ?
475 diag::warn_non_modular_include_in_framework_module :
476 diag::warn_non_modular_include_in_module;
477 Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName()
482 static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New,
483 const ModuleMap::KnownHeader &Old) {
484 // Prefer available modules.
485 if (New.getModule()->isAvailable() && !Old.getModule()->isAvailable())
488 // Prefer a public header over a private header.
489 if ((New.getRole() & ModuleMap::PrivateHeader) !=
490 (Old.getRole() & ModuleMap::PrivateHeader))
491 return !(New.getRole() & ModuleMap::PrivateHeader);
493 // Prefer a non-textual header over a textual header.
494 if ((New.getRole() & ModuleMap::TextualHeader) !=
495 (Old.getRole() & ModuleMap::TextualHeader))
496 return !(New.getRole() & ModuleMap::TextualHeader);
498 // Don't have a reason to choose between these. Just keep the first one.
502 ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File,
504 auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader {
505 if (!AllowTextual && R.getRole() & ModuleMap::TextualHeader)
506 return ModuleMap::KnownHeader();
510 HeadersMap::iterator Known = findKnownHeader(File);
511 if (Known != Headers.end()) {
512 ModuleMap::KnownHeader Result;
513 // Iterate over all modules that 'File' is part of to find the best fit.
514 for (KnownHeader &H : Known->second) {
515 // Prefer a header from the source module over all others.
516 if (H.getModule()->getTopLevelModule() == SourceModule)
517 return MakeResult(H);
518 if (!Result || isBetterKnownHeader(H, Result))
521 return MakeResult(Result);
524 return MakeResult(findOrCreateModuleForHeaderInUmbrellaDir(File));
527 ModuleMap::KnownHeader
528 ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) {
529 assert(!Headers.count(File) && "already have a module for this header");
531 SmallVector<const DirectoryEntry *, 2> SkippedDirs;
532 KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs);
534 Module *Result = H.getModule();
536 // Search up the module stack until we find a module with an umbrella
538 Module *UmbrellaModule = Result;
539 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
540 UmbrellaModule = UmbrellaModule->Parent;
542 if (UmbrellaModule->InferSubmodules) {
543 const FileEntry *UmbrellaModuleMap =
544 getModuleMapFileForUniquing(UmbrellaModule);
546 // Infer submodules for each of the directories we found between
547 // the directory of the umbrella header and the directory where
548 // the actual header is located.
549 bool Explicit = UmbrellaModule->InferExplicitSubmodules;
551 for (unsigned I = SkippedDirs.size(); I != 0; --I) {
552 // Find or create the module that corresponds to this directory name.
553 SmallString<32> NameBuf;
554 StringRef Name = sanitizeFilenameAsIdentifier(
555 llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf);
556 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
558 InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
559 Result->IsInferred = true;
561 // Associate the module and the directory.
562 UmbrellaDirs[SkippedDirs[I-1]] = Result;
564 // If inferred submodules export everything they import, add a
565 // wildcard to the set of exports.
566 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
567 Result->Exports.push_back(Module::ExportDecl(nullptr, true));
570 // Infer a submodule with the same name as this header file.
571 SmallString<32> NameBuf;
572 StringRef Name = sanitizeFilenameAsIdentifier(
573 llvm::sys::path::stem(File->getName()), NameBuf);
574 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
576 InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
577 Result->IsInferred = true;
578 Result->addTopHeader(File);
580 // If inferred submodules export everything they import, add a
581 // wildcard to the set of exports.
582 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
583 Result->Exports.push_back(Module::ExportDecl(nullptr, true));
585 // Record each of the directories we stepped through as being part of
586 // the module we found, since the umbrella header covers them all.
587 for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
588 UmbrellaDirs[SkippedDirs[I]] = Result;
591 KnownHeader Header(Result, NormalHeader);
592 Headers[File].push_back(Header);
596 return KnownHeader();
599 ArrayRef<ModuleMap::KnownHeader>
600 ModuleMap::findAllModulesForHeader(const FileEntry *File) const {
601 resolveHeaderDirectives(File);
602 auto It = Headers.find(File);
603 if (It == Headers.end())
608 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
609 return isHeaderUnavailableInModule(Header, nullptr);
613 ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header,
614 const Module *RequestingModule) const {
615 resolveHeaderDirectives(Header);
616 HeadersMap::const_iterator Known = Headers.find(Header);
617 if (Known != Headers.end()) {
618 for (SmallVectorImpl<KnownHeader>::const_iterator
619 I = Known->second.begin(),
620 E = Known->second.end();
623 if (I->isAvailable() &&
624 (!RequestingModule ||
625 I->getModule()->isSubModuleOf(RequestingModule))) {
626 // When no requesting module is available, the caller is looking if a
627 // header is part a module by only looking into the module map. This is
628 // done by warn_uncovered_module_header checks; don't consider textual
629 // headers part of it in this mode, otherwise we get misleading warnings
630 // that a umbrella header is not including a textual header.
631 if (!RequestingModule && I->getRole() == ModuleMap::TextualHeader)
639 const DirectoryEntry *Dir = Header->getDir();
640 SmallVector<const DirectoryEntry *, 2> SkippedDirs;
641 StringRef DirName = Dir->getName();
643 auto IsUnavailable = [&](const Module *M) {
644 return !M->isAvailable() && (!RequestingModule ||
645 M->isSubModuleOf(RequestingModule));
648 // Keep walking up the directory hierarchy, looking for a directory with
649 // an umbrella header.
651 llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
652 = UmbrellaDirs.find(Dir);
653 if (KnownDir != UmbrellaDirs.end()) {
654 Module *Found = KnownDir->second;
655 if (IsUnavailable(Found))
658 // Search up the module stack until we find a module with an umbrella
660 Module *UmbrellaModule = Found;
661 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
662 UmbrellaModule = UmbrellaModule->Parent;
664 if (UmbrellaModule->InferSubmodules) {
665 for (unsigned I = SkippedDirs.size(); I != 0; --I) {
666 // Find or create the module that corresponds to this directory name.
667 SmallString<32> NameBuf;
668 StringRef Name = sanitizeFilenameAsIdentifier(
669 llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
671 Found = lookupModuleQualified(Name, Found);
674 if (IsUnavailable(Found))
678 // Infer a submodule with the same name as this header file.
679 SmallString<32> NameBuf;
680 StringRef Name = sanitizeFilenameAsIdentifier(
681 llvm::sys::path::stem(Header->getName()),
683 Found = lookupModuleQualified(Name, Found);
688 return IsUnavailable(Found);
691 SkippedDirs.push_back(Dir);
693 // Retrieve our parent path.
694 DirName = llvm::sys::path::parent_path(DirName);
698 // Resolve the parent path to a directory entry.
699 Dir = SourceMgr.getFileManager().getDirectory(DirName);
705 Module *ModuleMap::findModule(StringRef Name) const {
706 llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
707 if (Known != Modules.end())
708 return Known->getValue();
713 Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
714 Module *Context) const {
715 for(; Context; Context = Context->Parent) {
716 if (Module *Sub = lookupModuleQualified(Name, Context))
720 return findModule(Name);
723 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
725 return findModule(Name);
727 return Context->findSubmodule(Name);
730 std::pair<Module *, bool> ModuleMap::findOrCreateModule(StringRef Name,
734 // Try to find an existing module with this name.
735 if (Module *Sub = lookupModuleQualified(Name, Parent))
736 return std::make_pair(Sub, false);
738 // Create a new module with this name.
739 Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
740 IsExplicit, NumCreatedModules++);
742 if (LangOpts.CurrentModule == Name)
743 SourceModule = Result;
744 Modules[Name] = Result;
746 return std::make_pair(Result, true);
749 Module *ModuleMap::createModuleForInterfaceUnit(SourceLocation Loc,
751 assert(LangOpts.CurrentModule == Name && "module name mismatch");
752 assert(!Modules[Name] && "redefining existing module");
755 new Module(Name, Loc, nullptr, /*IsFramework*/ false,
756 /*IsExplicit*/ false, NumCreatedModules++);
757 Result->Kind = Module::ModuleInterfaceUnit;
758 Modules[Name] = SourceModule = Result;
760 // Mark the main source file as being within the newly-created module so that
761 // declarations and macros are properly visibility-restricted to it.
762 auto *MainFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
763 assert(MainFile && "no input file for module interface");
764 Headers[MainFile].push_back(KnownHeader(Result, PrivateHeader));
769 /// \brief For a framework module, infer the framework against which we
771 static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
772 FileManager &FileMgr) {
773 assert(Mod->IsFramework && "Can only infer linking for framework modules");
774 assert(!Mod->isSubFramework() &&
775 "Can only infer linking for top-level frameworks");
777 SmallString<128> LibName;
778 LibName += FrameworkDir->getName();
779 llvm::sys::path::append(LibName, Mod->Name);
781 // The library name of a framework has more than one possible extension since
782 // the introduction of the text-based dynamic library format. We need to check
783 // for both before we give up.
784 for (const char *extension : {"", ".tbd"}) {
785 llvm::sys::path::replace_extension(LibName, extension);
786 if (FileMgr.getFile(LibName)) {
787 Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
788 /*IsFramework=*/true));
794 Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
795 bool IsSystem, Module *Parent) {
797 Attrs.IsSystem = IsSystem;
798 return inferFrameworkModule(FrameworkDir, Attrs, Parent);
801 Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
802 Attributes Attrs, Module *Parent) {
803 // Note: as an egregious but useful hack we use the real path here, because
804 // we might be looking at an embedded framework that symlinks out to a
805 // top-level framework, and we need to infer as if we were naming the
806 // top-level framework.
807 StringRef FrameworkDirName =
808 SourceMgr.getFileManager().getCanonicalName(FrameworkDir);
810 // In case this is a case-insensitive filesystem, use the canonical
811 // directory name as the ModuleName, since modules are case-sensitive.
812 // FIXME: we should be able to give a fix-it hint for the correct spelling.
813 SmallString<32> ModuleNameStorage;
814 StringRef ModuleName = sanitizeFilenameAsIdentifier(
815 llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage);
817 // Check whether we've already found this module.
818 if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
821 FileManager &FileMgr = SourceMgr.getFileManager();
823 // If the framework has a parent path from which we're allowed to infer
824 // a framework module, do so.
825 const FileEntry *ModuleMapFile = nullptr;
827 // Determine whether we're allowed to infer a module map.
828 bool canInfer = false;
829 if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
830 // Figure out the parent path.
831 StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
832 if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
833 // Check whether we have already looked into the parent directory
835 llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
836 inferred = InferredDirectories.find(ParentDir);
837 if (inferred == InferredDirectories.end()) {
838 // We haven't looked here before. Load a module map, if there is
840 bool IsFrameworkDir = Parent.endswith(".framework");
841 if (const FileEntry *ModMapFile =
842 HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) {
843 parseModuleMapFile(ModMapFile, Attrs.IsSystem, ParentDir);
844 inferred = InferredDirectories.find(ParentDir);
847 if (inferred == InferredDirectories.end())
848 inferred = InferredDirectories.insert(
849 std::make_pair(ParentDir, InferredDirectory())).first;
852 if (inferred->second.InferModules) {
853 // We're allowed to infer for this directory, but make sure it's okay
854 // to infer this particular module.
855 StringRef Name = llvm::sys::path::stem(FrameworkDirName);
856 canInfer = std::find(inferred->second.ExcludedModules.begin(),
857 inferred->second.ExcludedModules.end(),
858 Name) == inferred->second.ExcludedModules.end();
860 Attrs.IsSystem |= inferred->second.Attrs.IsSystem;
861 Attrs.IsExternC |= inferred->second.Attrs.IsExternC;
862 Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive;
863 Attrs.NoUndeclaredIncludes |=
864 inferred->second.Attrs.NoUndeclaredIncludes;
865 ModuleMapFile = inferred->second.ModuleMapFile;
870 // If we're not allowed to infer a framework module, don't.
874 ModuleMapFile = getModuleMapFileForUniquing(Parent);
877 // Look for an umbrella header.
878 SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
879 llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
880 const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
882 // FIXME: If there's no umbrella header, we could probably scan the
883 // framework to load *everything*. But, it's not clear that this is a good
888 Module *Result = new Module(ModuleName, SourceLocation(), Parent,
889 /*IsFramework=*/true, /*IsExplicit=*/false,
890 NumCreatedModules++);
891 InferredModuleAllowedBy[Result] = ModuleMapFile;
892 Result->IsInferred = true;
894 if (LangOpts.CurrentModule == ModuleName)
895 SourceModule = Result;
896 Modules[ModuleName] = Result;
899 Result->IsSystem |= Attrs.IsSystem;
900 Result->IsExternC |= Attrs.IsExternC;
901 Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive;
902 Result->NoUndeclaredIncludes |= Attrs.NoUndeclaredIncludes;
903 Result->Directory = FrameworkDir;
905 // umbrella header "umbrella-header-name"
907 // The "Headers/" component of the name is implied because this is
908 // a framework module.
909 setUmbrellaHeader(Result, UmbrellaHeader, ModuleName + ".h");
912 Result->Exports.push_back(Module::ExportDecl(nullptr, true));
914 // module * { export * }
915 Result->InferSubmodules = true;
916 Result->InferExportWildcard = true;
918 // Look for subframeworks.
920 SmallString<128> SubframeworksDirName
921 = StringRef(FrameworkDir->getName());
922 llvm::sys::path::append(SubframeworksDirName, "Frameworks");
923 llvm::sys::path::native(SubframeworksDirName);
924 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
925 for (vfs::directory_iterator Dir = FS.dir_begin(SubframeworksDirName, EC),
927 Dir != DirEnd && !EC; Dir.increment(EC)) {
928 if (!StringRef(Dir->getName()).endswith(".framework"))
931 if (const DirectoryEntry *SubframeworkDir =
932 FileMgr.getDirectory(Dir->getName())) {
933 // Note: as an egregious but useful hack, we use the real path here and
934 // check whether it is actually a subdirectory of the parent directory.
935 // This will not be the case if the 'subframework' is actually a symlink
936 // out to a top-level framework.
937 StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
938 bool FoundParent = false;
940 // Get the parent directory name.
942 = llvm::sys::path::parent_path(SubframeworkDirName);
943 if (SubframeworkDirName.empty())
946 if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
955 // FIXME: Do we want to warn about subframeworks without umbrella headers?
956 inferFrameworkModule(SubframeworkDir, Attrs, Result);
960 // If the module is a top-level framework, automatically link against the
962 if (!Result->isSubFramework()) {
963 inferFrameworkLink(Result, FrameworkDir, FileMgr);
969 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
970 Twine NameAsWritten) {
971 Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
972 Mod->Umbrella = UmbrellaHeader;
973 Mod->UmbrellaAsWritten = NameAsWritten.str();
974 UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
976 // Notify callbacks that we just added a new header.
977 for (const auto &Cb : Callbacks)
978 Cb->moduleMapAddUmbrellaHeader(&SourceMgr.getFileManager(), UmbrellaHeader);
981 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
982 Twine NameAsWritten) {
983 Mod->Umbrella = UmbrellaDir;
984 Mod->UmbrellaAsWritten = NameAsWritten.str();
985 UmbrellaDirs[UmbrellaDir] = Mod;
988 void ModuleMap::addUnresolvedHeader(Module *Mod,
989 Module::UnresolvedHeaderDirective Header) {
990 // If there is a builtin counterpart to this file, add it now so it can
991 // wrap the system header.
992 if (resolveAsBuiltinHeader(Mod, Header)) {
993 // If we have both a builtin and system version of the file, the
994 // builtin version may want to inject macros into the system header, so
995 // force the system header to be treated as a textual header in this
997 Header.Kind = headerRoleToKind(ModuleMap::ModuleHeaderRole(
998 headerKindToRole(Header.Kind) | ModuleMap::TextualHeader));
999 Header.HasBuiltinHeader = true;
1002 // If possible, don't stat the header until we need to. This requires the
1003 // user to have provided us with some stat information about the file.
1004 // FIXME: Add support for lazily stat'ing umbrella headers and excluded
1006 if ((Header.Size || Header.ModTime) && !Header.IsUmbrella &&
1007 Header.Kind != Module::HK_Excluded) {
1008 // We expect more variation in mtime than size, so if we're given both,
1009 // use the mtime as the key.
1011 LazyHeadersByModTime[*Header.ModTime].push_back(Mod);
1013 LazyHeadersBySize[*Header.Size].push_back(Mod);
1014 Mod->UnresolvedHeaders.push_back(Header);
1018 // We don't have stat information or can't defer looking this file up.
1019 // Perform the lookup now.
1020 resolveHeader(Mod, Header);
1023 void ModuleMap::resolveHeaderDirectives(const FileEntry *File) const {
1024 auto BySize = LazyHeadersBySize.find(File->getSize());
1025 if (BySize != LazyHeadersBySize.end()) {
1026 for (auto *M : BySize->second)
1027 resolveHeaderDirectives(M);
1028 LazyHeadersBySize.erase(BySize);
1031 auto ByModTime = LazyHeadersByModTime.find(File->getModificationTime());
1032 if (ByModTime != LazyHeadersByModTime.end()) {
1033 for (auto *M : ByModTime->second)
1034 resolveHeaderDirectives(M);
1035 LazyHeadersByModTime.erase(ByModTime);
1039 void ModuleMap::resolveHeaderDirectives(Module *Mod) const {
1040 for (auto &Header : Mod->UnresolvedHeaders)
1041 // This operation is logically const; we're just changing how we represent
1042 // the header information for this file.
1043 const_cast<ModuleMap*>(this)->resolveHeader(Mod, Header);
1044 Mod->UnresolvedHeaders.clear();
1047 void ModuleMap::addHeader(Module *Mod, Module::Header Header,
1048 ModuleHeaderRole Role, bool Imported) {
1049 KnownHeader KH(Mod, Role);
1051 // Only add each header to the headers list once.
1052 // FIXME: Should we diagnose if a header is listed twice in the
1053 // same module definition?
1054 auto &HeaderList = Headers[Header.Entry];
1055 for (auto H : HeaderList)
1059 HeaderList.push_back(KH);
1060 Mod->Headers[headerRoleToKind(Role)].push_back(Header);
1062 bool isCompilingModuleHeader =
1063 LangOpts.isCompilingModule() && Mod->getTopLevelModule() == SourceModule;
1064 if (!Imported || isCompilingModuleHeader) {
1065 // When we import HeaderFileInfo, the external source is expected to
1066 // set the isModuleHeader flag itself.
1067 HeaderInfo.MarkFileModuleHeader(Header.Entry, Role,
1068 isCompilingModuleHeader);
1071 // Notify callbacks that we just added a new header.
1072 for (const auto &Cb : Callbacks)
1073 Cb->moduleMapAddHeader(Header.Entry->getName());
1076 void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) {
1077 // Add this as a known header so we won't implicitly add it to any
1078 // umbrella directory module.
1079 // FIXME: Should we only exclude it from umbrella modules within the
1080 // specified module?
1081 (void) Headers[Header.Entry];
1083 Mod->Headers[Module::HK_Excluded].push_back(std::move(Header));
1087 ModuleMap::getContainingModuleMapFile(const Module *Module) const {
1088 if (Module->DefinitionLoc.isInvalid())
1091 return SourceMgr.getFileEntryForID(
1092 SourceMgr.getFileID(Module->DefinitionLoc));
1095 const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const {
1096 if (M->IsInferred) {
1097 assert(InferredModuleAllowedBy.count(M) && "missing inferred module map");
1098 return InferredModuleAllowedBy.find(M)->second;
1100 return getContainingModuleMapFile(M);
1103 void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) {
1104 assert(M->IsInferred && "module not inferred");
1105 InferredModuleAllowedBy[M] = ModMap;
1108 LLVM_DUMP_METHOD void ModuleMap::dump() {
1109 llvm::errs() << "Modules:";
1110 for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
1111 MEnd = Modules.end();
1113 M->getValue()->print(llvm::errs(), 2);
1115 llvm::errs() << "Headers:";
1116 for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
1118 llvm::errs() << " \"" << H->first->getName() << "\" -> ";
1119 for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
1120 E = H->second.end();
1122 if (I != H->second.begin())
1123 llvm::errs() << ",";
1124 llvm::errs() << I->getModule()->getFullModuleName();
1126 llvm::errs() << "\n";
1130 bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
1131 auto Unresolved = std::move(Mod->UnresolvedExports);
1132 Mod->UnresolvedExports.clear();
1133 for (auto &UE : Unresolved) {
1134 Module::ExportDecl Export = resolveExport(Mod, UE, Complain);
1135 if (Export.getPointer() || Export.getInt())
1136 Mod->Exports.push_back(Export);
1138 Mod->UnresolvedExports.push_back(UE);
1140 return !Mod->UnresolvedExports.empty();
1143 bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
1144 auto Unresolved = std::move(Mod->UnresolvedDirectUses);
1145 Mod->UnresolvedDirectUses.clear();
1146 for (auto &UDU : Unresolved) {
1147 Module *DirectUse = resolveModuleId(UDU, Mod, Complain);
1149 Mod->DirectUses.push_back(DirectUse);
1151 Mod->UnresolvedDirectUses.push_back(UDU);
1153 return !Mod->UnresolvedDirectUses.empty();
1156 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
1157 auto Unresolved = std::move(Mod->UnresolvedConflicts);
1158 Mod->UnresolvedConflicts.clear();
1159 for (auto &UC : Unresolved) {
1160 if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) {
1161 Module::Conflict Conflict;
1162 Conflict.Other = OtherMod;
1163 Conflict.Message = UC.Message;
1164 Mod->Conflicts.push_back(Conflict);
1166 Mod->UnresolvedConflicts.push_back(UC);
1168 return !Mod->UnresolvedConflicts.empty();
1171 //----------------------------------------------------------------------------//
1172 // Module map file parser
1173 //----------------------------------------------------------------------------//
1176 /// \brief A token in a module map file.
1209 unsigned StringLength;
1211 // If Kind != IntegerLiteral.
1212 const char *StringData;
1213 // If Kind == IntegerLiteral.
1214 uint64_t IntegerValue;
1221 StringData = nullptr;
1224 bool is(TokenKind K) const { return Kind == K; }
1226 SourceLocation getLocation() const {
1227 return SourceLocation::getFromRawEncoding(Location);
1230 uint64_t getInteger() const {
1231 return Kind == IntegerLiteral ? IntegerValue : 0;
1234 StringRef getString() const {
1235 return Kind == IntegerLiteral ? StringRef()
1236 : StringRef(StringData, StringLength);
1240 class ModuleMapParser {
1242 SourceManager &SourceMgr;
1244 /// \brief Default target information, used only for string literal
1246 const TargetInfo *Target;
1248 DiagnosticsEngine &Diags;
1251 /// \brief The current module map file.
1252 const FileEntry *ModuleMapFile;
1254 /// \brief The directory that file names in this module map file should
1255 /// be resolved relative to.
1256 const DirectoryEntry *Directory;
1258 /// \brief Whether this module map is in a system header directory.
1261 /// \brief Whether an error occurred.
1264 /// \brief Stores string data for the various string literals referenced
1266 llvm::BumpPtrAllocator StringData;
1268 /// \brief The current token.
1271 /// \brief The active module.
1272 Module *ActiveModule;
1274 /// \brief Whether a module uses the 'requires excluded' hack to mark its
1275 /// contents as 'textual'.
1277 /// On older Darwin SDK versions, 'requires excluded' is used to mark the
1278 /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as
1279 /// non-modular headers. For backwards compatibility, we continue to
1280 /// support this idiom for just these modules, and map the headers to
1281 /// 'textual' to match the original intent.
1282 llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack;
1284 /// \brief Consume the current token and return its location.
1285 SourceLocation consumeToken();
1287 /// \brief Skip tokens until we reach the a token with the given kind
1288 /// (or the end of the file).
1289 void skipUntil(MMToken::TokenKind K);
1291 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
1292 bool parseModuleId(ModuleId &Id);
1293 void parseModuleDecl();
1294 void parseExternModuleDecl();
1295 void parseRequiresDecl();
1296 void parseHeaderDecl(clang::MMToken::TokenKind,
1297 SourceLocation LeadingLoc);
1298 void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
1299 void parseExportDecl();
1300 void parseUseDecl();
1301 void parseLinkDecl();
1302 void parseConfigMacros();
1303 void parseConflict();
1304 void parseInferredModuleDecl(bool Framework, bool Explicit);
1306 typedef ModuleMap::Attributes Attributes;
1307 bool parseOptionalAttributes(Attributes &Attrs);
1310 explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
1311 const TargetInfo *Target,
1312 DiagnosticsEngine &Diags,
1314 const FileEntry *ModuleMapFile,
1315 const DirectoryEntry *Directory,
1317 : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
1318 ModuleMapFile(ModuleMapFile), Directory(Directory),
1319 IsSystem(IsSystem), HadError(false), ActiveModule(nullptr)
1325 bool parseModuleMapFile();
1327 bool terminatedByDirective() { return false; }
1328 SourceLocation getLocation() { return Tok.getLocation(); }
1332 SourceLocation ModuleMapParser::consumeToken() {
1333 SourceLocation Result = Tok.getLocation();
1338 L.LexFromRawLexer(LToken);
1339 Tok.Location = LToken.getLocation().getRawEncoding();
1340 switch (LToken.getKind()) {
1341 case tok::raw_identifier: {
1342 StringRef RI = LToken.getRawIdentifier();
1343 Tok.StringData = RI.data();
1344 Tok.StringLength = RI.size();
1345 Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI)
1346 .Case("config_macros", MMToken::ConfigMacros)
1347 .Case("conflict", MMToken::Conflict)
1348 .Case("exclude", MMToken::ExcludeKeyword)
1349 .Case("explicit", MMToken::ExplicitKeyword)
1350 .Case("export", MMToken::ExportKeyword)
1351 .Case("extern", MMToken::ExternKeyword)
1352 .Case("framework", MMToken::FrameworkKeyword)
1353 .Case("header", MMToken::HeaderKeyword)
1354 .Case("link", MMToken::LinkKeyword)
1355 .Case("module", MMToken::ModuleKeyword)
1356 .Case("private", MMToken::PrivateKeyword)
1357 .Case("requires", MMToken::RequiresKeyword)
1358 .Case("textual", MMToken::TextualKeyword)
1359 .Case("umbrella", MMToken::UmbrellaKeyword)
1360 .Case("use", MMToken::UseKeyword)
1361 .Default(MMToken::Identifier);
1366 Tok.Kind = MMToken::Comma;
1370 Tok.Kind = MMToken::EndOfFile;
1374 Tok.Kind = MMToken::LBrace;
1378 Tok.Kind = MMToken::LSquare;
1382 Tok.Kind = MMToken::Period;
1386 Tok.Kind = MMToken::RBrace;
1390 Tok.Kind = MMToken::RSquare;
1394 Tok.Kind = MMToken::Star;
1398 Tok.Kind = MMToken::Exclaim;
1401 case tok::string_literal: {
1402 if (LToken.hasUDSuffix()) {
1403 Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
1408 // Parse the string literal.
1409 LangOptions LangOpts;
1410 StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target);
1411 if (StringLiteral.hadError)
1414 // Copy the string literal into our string data allocator.
1415 unsigned Length = StringLiteral.GetStringLength();
1416 char *Saved = StringData.Allocate<char>(Length + 1);
1417 memcpy(Saved, StringLiteral.GetString().data(), Length);
1421 Tok.Kind = MMToken::StringLiteral;
1422 Tok.StringData = Saved;
1423 Tok.StringLength = Length;
1427 case tok::numeric_constant: {
1428 // We don't support any suffixes or other complications.
1429 SmallString<32> SpellingBuffer;
1430 SpellingBuffer.resize(LToken.getLength() + 1);
1431 const char *Start = SpellingBuffer.data();
1433 Lexer::getSpelling(LToken, Start, SourceMgr, L.getLangOpts());
1435 if (StringRef(Start, Length).getAsInteger(0, Value)) {
1436 Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token);
1441 Tok.Kind = MMToken::IntegerLiteral;
1442 Tok.IntegerValue = Value;
1450 // A module map can be terminated prematurely by
1451 // #pragma clang module contents
1452 // When building the module, we'll treat the rest of the file as the
1453 // contents of the module.
1455 auto NextIsIdent = [&](StringRef Str) -> bool {
1456 L.LexFromRawLexer(LToken);
1457 return !LToken.isAtStartOfLine() && LToken.is(tok::raw_identifier) &&
1458 LToken.getRawIdentifier() == Str;
1460 if (NextIsIdent("pragma") && NextIsIdent("clang") &&
1461 NextIsIdent("module") && NextIsIdent("contents")) {
1462 Tok.Kind = MMToken::EndOfFile;
1469 Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token);
1477 void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
1478 unsigned braceDepth = 0;
1479 unsigned squareDepth = 0;
1482 case MMToken::EndOfFile:
1485 case MMToken::LBrace:
1486 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1492 case MMToken::LSquare:
1493 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1499 case MMToken::RBrace:
1506 case MMToken::RSquare:
1507 if (squareDepth > 0)
1514 if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
1523 /// \brief Parse a module-id.
1527 /// identifier '.' module-id
1529 /// \returns true if an error occurred, false otherwise.
1530 bool ModuleMapParser::parseModuleId(ModuleId &Id) {
1533 if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) {
1534 Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
1537 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
1541 if (!Tok.is(MMToken::Period))
1551 /// \brief Enumerates the known attributes.
1552 enum AttributeKind {
1553 /// \brief An unknown attribute.
1555 /// \brief The 'system' attribute.
1557 /// \brief The 'extern_c' attribute.
1559 /// \brief The 'exhaustive' attribute.
1561 /// \brief The 'no_undeclared_includes' attribute.
1562 AT_no_undeclared_includes
1566 /// \brief Parse a module declaration.
1568 /// module-declaration:
1569 /// 'extern' 'module' module-id string-literal
1570 /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
1571 /// { module-member* }
1574 /// requires-declaration
1575 /// header-declaration
1576 /// submodule-declaration
1577 /// export-declaration
1578 /// link-declaration
1580 /// submodule-declaration:
1581 /// module-declaration
1582 /// inferred-submodule-declaration
1583 void ModuleMapParser::parseModuleDecl() {
1584 assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
1585 Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
1586 if (Tok.is(MMToken::ExternKeyword)) {
1587 parseExternModuleDecl();
1591 // Parse 'explicit' or 'framework' keyword, if present.
1592 SourceLocation ExplicitLoc;
1593 bool Explicit = false;
1594 bool Framework = false;
1596 // Parse 'explicit' keyword, if present.
1597 if (Tok.is(MMToken::ExplicitKeyword)) {
1598 ExplicitLoc = consumeToken();
1602 // Parse 'framework' keyword, if present.
1603 if (Tok.is(MMToken::FrameworkKeyword)) {
1608 // Parse 'module' keyword.
1609 if (!Tok.is(MMToken::ModuleKeyword)) {
1610 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1615 consumeToken(); // 'module' keyword
1617 // If we have a wildcard for the module name, this is an inferred submodule.
1619 if (Tok.is(MMToken::Star))
1620 return parseInferredModuleDecl(Framework, Explicit);
1622 // Parse the module name.
1624 if (parseModuleId(Id)) {
1630 if (Id.size() > 1) {
1631 Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
1632 << SourceRange(Id.front().second, Id.back().second);
1637 } else if (Id.size() == 1 && Explicit) {
1638 // Top-level modules can't be explicit.
1639 Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1641 ExplicitLoc = SourceLocation();
1645 Module *PreviousActiveModule = ActiveModule;
1646 if (Id.size() > 1) {
1647 // This module map defines a submodule. Go find the module of which it
1649 ActiveModule = nullptr;
1650 const Module *TopLevelModule = nullptr;
1651 for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1652 if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1654 TopLevelModule = Next;
1655 ActiveModule = Next;
1660 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
1662 << ActiveModule->getTopLevelModule()->getFullModuleName();
1664 Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
1670 if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) {
1671 assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) &&
1672 "submodule defined in same file as 'module *' that allowed its "
1673 "top-level module");
1674 Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile);
1678 StringRef ModuleName = Id.back().first;
1679 SourceLocation ModuleNameLoc = Id.back().second;
1681 // Parse the optional attribute list.
1683 if (parseOptionalAttributes(Attrs))
1687 // Parse the opening brace.
1688 if (!Tok.is(MMToken::LBrace)) {
1689 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1694 SourceLocation LBraceLoc = consumeToken();
1696 // Determine whether this (sub)module has already been defined.
1697 if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
1698 // We might see a (re)definition of a module that we already have a
1699 // definition for in two cases:
1700 // - If we loaded one definition from an AST file and we've just found a
1701 // corresponding definition in a module map file, or
1702 bool LoadedFromASTFile = Existing->DefinitionLoc.isInvalid();
1703 // - If we're building a (preprocessed) module and we've just loaded the
1704 // module map file from which it was created.
1705 bool ParsedAsMainInput =
1706 Map.LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap &&
1707 Map.LangOpts.CurrentModule == ModuleName &&
1708 SourceMgr.getDecomposedLoc(ModuleNameLoc).first !=
1709 SourceMgr.getDecomposedLoc(Existing->DefinitionLoc).first;
1710 if (!ActiveModule && (LoadedFromASTFile || ParsedAsMainInput)) {
1711 // Skip the module definition.
1712 skipUntil(MMToken::RBrace);
1713 if (Tok.is(MMToken::RBrace))
1716 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1717 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1723 Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
1725 Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
1727 // Skip the module definition.
1728 skipUntil(MMToken::RBrace);
1729 if (Tok.is(MMToken::RBrace))
1736 // Start defining this module.
1737 ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
1739 ActiveModule->DefinitionLoc = ModuleNameLoc;
1740 if (Attrs.IsSystem || IsSystem)
1741 ActiveModule->IsSystem = true;
1742 if (Attrs.IsExternC)
1743 ActiveModule->IsExternC = true;
1744 if (Attrs.NoUndeclaredIncludes ||
1745 (!ActiveModule->Parent && ModuleName == "Darwin"))
1746 ActiveModule->NoUndeclaredIncludes = true;
1747 ActiveModule->Directory = Directory;
1749 if (!ActiveModule->Parent) {
1750 StringRef MapFileName(ModuleMapFile->getName());
1751 if (MapFileName.endswith("module.private.modulemap") ||
1752 MapFileName.endswith("module_private.map")) {
1753 // Adding a top-level module from a private modulemap is likely a
1754 // user error; we check to see if there's another top-level module
1755 // defined in the non-private map in the same dir, and if so emit a
1757 for (auto E = Map.module_begin(); E != Map.module_end(); ++E) {
1758 auto const *M = E->getValue();
1760 M->Directory == ActiveModule->Directory &&
1761 M->Name != ActiveModule->Name) {
1762 Diags.Report(ActiveModule->DefinitionLoc,
1763 diag::warn_mmap_mismatched_top_level_private)
1764 << ActiveModule->Name << M->Name;
1765 // The pattern we're defending against here is typically due to
1766 // a module named FooPrivate which is supposed to be a submodule
1767 // called Foo.Private. Emit a fixit in that case.
1769 Diags.Report(ActiveModule->DefinitionLoc,
1770 diag::note_mmap_rename_top_level_private_as_submodule);
1771 D << ActiveModule->Name << M->Name;
1772 StringRef Bad(ActiveModule->Name);
1773 if (Bad.consume_back("Private")) {
1774 SmallString<128> Fixed = Bad;
1775 Fixed.append(".Private");
1776 D << FixItHint::CreateReplacement(ActiveModule->DefinitionLoc,
1788 case MMToken::EndOfFile:
1789 case MMToken::RBrace:
1793 case MMToken::ConfigMacros:
1794 parseConfigMacros();
1797 case MMToken::Conflict:
1801 case MMToken::ExplicitKeyword:
1802 case MMToken::ExternKeyword:
1803 case MMToken::FrameworkKeyword:
1804 case MMToken::ModuleKeyword:
1808 case MMToken::ExportKeyword:
1812 case MMToken::UseKeyword:
1816 case MMToken::RequiresKeyword:
1817 parseRequiresDecl();
1820 case MMToken::TextualKeyword:
1821 parseHeaderDecl(MMToken::TextualKeyword, consumeToken());
1824 case MMToken::UmbrellaKeyword: {
1825 SourceLocation UmbrellaLoc = consumeToken();
1826 if (Tok.is(MMToken::HeaderKeyword))
1827 parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
1829 parseUmbrellaDirDecl(UmbrellaLoc);
1833 case MMToken::ExcludeKeyword:
1834 parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken());
1837 case MMToken::PrivateKeyword:
1838 parseHeaderDecl(MMToken::PrivateKeyword, consumeToken());
1841 case MMToken::HeaderKeyword:
1842 parseHeaderDecl(MMToken::HeaderKeyword, consumeToken());
1845 case MMToken::LinkKeyword:
1850 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
1856 if (Tok.is(MMToken::RBrace))
1859 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1860 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1864 // If the active module is a top-level framework, and there are no link
1865 // libraries, automatically link against the framework.
1866 if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
1867 ActiveModule->LinkLibraries.empty()) {
1868 inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
1871 // If the module meets all requirements but is still unavailable, mark the
1872 // whole tree as unavailable to prevent it from building.
1873 if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement &&
1874 ActiveModule->Parent) {
1875 ActiveModule->getTopLevelModule()->markUnavailable();
1876 ActiveModule->getTopLevelModule()->MissingHeaders.append(
1877 ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end());
1880 // We're done parsing this module. Pop back to the previous module.
1881 ActiveModule = PreviousActiveModule;
1884 /// \brief Parse an extern module declaration.
1886 /// extern module-declaration:
1887 /// 'extern' 'module' module-id string-literal
1888 void ModuleMapParser::parseExternModuleDecl() {
1889 assert(Tok.is(MMToken::ExternKeyword));
1890 SourceLocation ExternLoc = consumeToken(); // 'extern' keyword
1892 // Parse 'module' keyword.
1893 if (!Tok.is(MMToken::ModuleKeyword)) {
1894 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1899 consumeToken(); // 'module' keyword
1901 // Parse the module name.
1903 if (parseModuleId(Id)) {
1908 // Parse the referenced module map file name.
1909 if (!Tok.is(MMToken::StringLiteral)) {
1910 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
1914 std::string FileName = Tok.getString();
1915 consumeToken(); // filename
1917 StringRef FileNameRef = FileName;
1918 SmallString<128> ModuleMapFileName;
1919 if (llvm::sys::path::is_relative(FileNameRef)) {
1920 ModuleMapFileName += Directory->getName();
1921 llvm::sys::path::append(ModuleMapFileName, FileName);
1922 FileNameRef = ModuleMapFileName;
1924 if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef))
1925 Map.parseModuleMapFile(
1926 File, /*IsSystem=*/false,
1927 Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd
1930 FileID(), nullptr, ExternLoc);
1933 /// Whether to add the requirement \p Feature to the module \p M.
1935 /// This preserves backwards compatibility for two hacks in the Darwin system
1936 /// module map files:
1938 /// 1. The use of 'requires excluded' to make headers non-modular, which
1939 /// should really be mapped to 'textual' now that we have this feature. We
1940 /// drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to
1941 /// true. Later, this bit will be used to map all the headers inside this
1942 /// module to 'textual'.
1944 /// This affects Darwin.C.excluded (for assert.h) and Tcl.Private.
1946 /// 2. Removes a bogus cplusplus requirement from IOKit.avc. This requirement
1947 /// was never correct and causes issues now that we check it, so drop it.
1948 static bool shouldAddRequirement(Module *M, StringRef Feature,
1949 bool &IsRequiresExcludedHack) {
1950 if (Feature == "excluded" &&
1951 (M->fullModuleNameIs({"Darwin", "C", "excluded"}) ||
1952 M->fullModuleNameIs({"Tcl", "Private"}))) {
1953 IsRequiresExcludedHack = true;
1955 } else if (Feature == "cplusplus" && M->fullModuleNameIs({"IOKit", "avc"})) {
1962 /// \brief Parse a requires declaration.
1964 /// requires-declaration:
1965 /// 'requires' feature-list
1968 /// feature ',' feature-list
1972 /// '!'[opt] identifier
1973 void ModuleMapParser::parseRequiresDecl() {
1974 assert(Tok.is(MMToken::RequiresKeyword));
1976 // Parse 'requires' keyword.
1979 // Parse the feature-list.
1981 bool RequiredState = true;
1982 if (Tok.is(MMToken::Exclaim)) {
1983 RequiredState = false;
1987 if (!Tok.is(MMToken::Identifier)) {
1988 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
1993 // Consume the feature name.
1994 std::string Feature = Tok.getString();
1997 bool IsRequiresExcludedHack = false;
1998 bool ShouldAddRequirement =
1999 shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack);
2001 if (IsRequiresExcludedHack)
2002 UsesRequiresExcludedHack.insert(ActiveModule);
2004 if (ShouldAddRequirement) {
2005 // Add this feature.
2006 ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts,
2010 if (!Tok.is(MMToken::Comma))
2013 // Consume the comma.
2018 /// \brief Parse a header declaration.
2020 /// header-declaration:
2021 /// 'textual'[opt] 'header' string-literal
2022 /// 'private' 'textual'[opt] 'header' string-literal
2023 /// 'exclude' 'header' string-literal
2024 /// 'umbrella' 'header' string-literal
2026 /// FIXME: Support 'private textual header'.
2027 void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
2028 SourceLocation LeadingLoc) {
2029 // We've already consumed the first token.
2030 ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
2031 if (LeadingToken == MMToken::PrivateKeyword) {
2032 Role = ModuleMap::PrivateHeader;
2033 // 'private' may optionally be followed by 'textual'.
2034 if (Tok.is(MMToken::TextualKeyword)) {
2035 LeadingToken = Tok.Kind;
2040 if (LeadingToken == MMToken::TextualKeyword)
2041 Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
2043 if (UsesRequiresExcludedHack.count(ActiveModule)) {
2044 // Mark this header 'textual' (see doc comment for
2045 // Module::UsesRequiresExcludedHack).
2046 Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
2049 if (LeadingToken != MMToken::HeaderKeyword) {
2050 if (!Tok.is(MMToken::HeaderKeyword)) {
2051 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
2052 << (LeadingToken == MMToken::PrivateKeyword ? "private" :
2053 LeadingToken == MMToken::ExcludeKeyword ? "exclude" :
2054 LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella");
2060 // Parse the header name.
2061 if (!Tok.is(MMToken::StringLiteral)) {
2062 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
2067 Module::UnresolvedHeaderDirective Header;
2068 Header.FileName = Tok.getString();
2069 Header.FileNameLoc = consumeToken();
2070 Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword;
2072 (LeadingToken == MMToken::ExcludeKeyword ? Module::HK_Excluded
2073 : Map.headerRoleToKind(Role));
2075 // Check whether we already have an umbrella.
2076 if (Header.IsUmbrella && ActiveModule->Umbrella) {
2077 Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
2078 << ActiveModule->getFullModuleName();
2083 // If we were given stat information, parse it so we can skip looking for
2085 if (Tok.is(MMToken::LBrace)) {
2086 SourceLocation LBraceLoc = consumeToken();
2088 while (!Tok.is(MMToken::RBrace) && !Tok.is(MMToken::EndOfFile)) {
2089 enum Attribute { Size, ModTime, Unknown };
2090 StringRef Str = Tok.getString();
2091 SourceLocation Loc = consumeToken();
2092 switch (llvm::StringSwitch<Attribute>(Str)
2094 .Case("mtime", ModTime)
2095 .Default(Unknown)) {
2098 Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str;
2099 if (!Tok.is(MMToken::IntegerLiteral)) {
2100 Diags.Report(Tok.getLocation(),
2101 diag::err_mmap_invalid_header_attribute_value) << Str;
2102 skipUntil(MMToken::RBrace);
2105 Header.Size = Tok.getInteger();
2111 Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str;
2112 if (!Tok.is(MMToken::IntegerLiteral)) {
2113 Diags.Report(Tok.getLocation(),
2114 diag::err_mmap_invalid_header_attribute_value) << Str;
2115 skipUntil(MMToken::RBrace);
2118 Header.ModTime = Tok.getInteger();
2123 Diags.Report(Loc, diag::err_mmap_expected_header_attribute);
2124 skipUntil(MMToken::RBrace);
2129 if (Tok.is(MMToken::RBrace))
2132 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
2133 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
2138 Map.addUnresolvedHeader(ActiveModule, std::move(Header));
2141 static int compareModuleHeaders(const Module::Header *A,
2142 const Module::Header *B) {
2143 return A->NameAsWritten.compare(B->NameAsWritten);
2146 /// \brief Parse an umbrella directory declaration.
2148 /// umbrella-dir-declaration:
2149 /// umbrella string-literal
2150 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
2151 // Parse the directory name.
2152 if (!Tok.is(MMToken::StringLiteral)) {
2153 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
2159 std::string DirName = Tok.getString();
2160 SourceLocation DirNameLoc = consumeToken();
2162 // Check whether we already have an umbrella.
2163 if (ActiveModule->Umbrella) {
2164 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
2165 << ActiveModule->getFullModuleName();
2170 // Look for this file.
2171 const DirectoryEntry *Dir = nullptr;
2172 if (llvm::sys::path::is_absolute(DirName))
2173 Dir = SourceMgr.getFileManager().getDirectory(DirName);
2175 SmallString<128> PathName;
2176 PathName = Directory->getName();
2177 llvm::sys::path::append(PathName, DirName);
2178 Dir = SourceMgr.getFileManager().getDirectory(PathName);
2182 Diags.Report(DirNameLoc, diag::warn_mmap_umbrella_dir_not_found)
2187 if (UsesRequiresExcludedHack.count(ActiveModule)) {
2188 // Mark this header 'textual' (see doc comment for
2189 // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the
2190 // directory is relatively expensive, in practice this only applies to the
2191 // uncommonly used Tcl module on Darwin platforms.
2193 SmallVector<Module::Header, 6> Headers;
2194 vfs::FileSystem &FS = *SourceMgr.getFileManager().getVirtualFileSystem();
2195 for (vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E;
2196 I != E && !EC; I.increment(EC)) {
2197 if (const FileEntry *FE =
2198 SourceMgr.getFileManager().getFile(I->getName())) {
2200 Module::Header Header = {I->getName(), FE};
2201 Headers.push_back(std::move(Header));
2205 // Sort header paths so that the pcm doesn't depend on iteration order.
2206 llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders);
2208 for (auto &Header : Headers)
2209 Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader);
2213 if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
2214 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
2215 << OwningModule->getFullModuleName();
2220 // Record this umbrella directory.
2221 Map.setUmbrellaDir(ActiveModule, Dir, DirName);
2224 /// \brief Parse a module export declaration.
2226 /// export-declaration:
2227 /// 'export' wildcard-module-id
2229 /// wildcard-module-id:
2232 /// identifier '.' wildcard-module-id
2233 void ModuleMapParser::parseExportDecl() {
2234 assert(Tok.is(MMToken::ExportKeyword));
2235 SourceLocation ExportLoc = consumeToken();
2237 // Parse the module-id with an optional wildcard at the end.
2238 ModuleId ParsedModuleId;
2239 bool Wildcard = false;
2241 // FIXME: Support string-literal module names here.
2242 if (Tok.is(MMToken::Identifier)) {
2243 ParsedModuleId.push_back(std::make_pair(Tok.getString(),
2244 Tok.getLocation()));
2247 if (Tok.is(MMToken::Period)) {
2255 if(Tok.is(MMToken::Star)) {
2261 Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
2266 Module::UnresolvedExportDecl Unresolved = {
2267 ExportLoc, ParsedModuleId, Wildcard
2269 ActiveModule->UnresolvedExports.push_back(Unresolved);
2272 /// \brief Parse a module use declaration.
2274 /// use-declaration:
2275 /// 'use' wildcard-module-id
2276 void ModuleMapParser::parseUseDecl() {
2277 assert(Tok.is(MMToken::UseKeyword));
2278 auto KWLoc = consumeToken();
2279 // Parse the module-id.
2280 ModuleId ParsedModuleId;
2281 parseModuleId(ParsedModuleId);
2283 if (ActiveModule->Parent)
2284 Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule);
2286 ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
2289 /// \brief Parse a link declaration.
2291 /// module-declaration:
2292 /// 'link' 'framework'[opt] string-literal
2293 void ModuleMapParser::parseLinkDecl() {
2294 assert(Tok.is(MMToken::LinkKeyword));
2295 SourceLocation LinkLoc = consumeToken();
2297 // Parse the optional 'framework' keyword.
2298 bool IsFramework = false;
2299 if (Tok.is(MMToken::FrameworkKeyword)) {
2304 // Parse the library name
2305 if (!Tok.is(MMToken::StringLiteral)) {
2306 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
2307 << IsFramework << SourceRange(LinkLoc);
2312 std::string LibraryName = Tok.getString();
2314 ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
2318 /// \brief Parse a configuration macro declaration.
2320 /// module-declaration:
2321 /// 'config_macros' attributes[opt] config-macro-list?
2323 /// config-macro-list:
2324 /// identifier (',' identifier)?
2325 void ModuleMapParser::parseConfigMacros() {
2326 assert(Tok.is(MMToken::ConfigMacros));
2327 SourceLocation ConfigMacrosLoc = consumeToken();
2329 // Only top-level modules can have configuration macros.
2330 if (ActiveModule->Parent) {
2331 Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
2334 // Parse the optional attributes.
2336 if (parseOptionalAttributes(Attrs))
2339 if (Attrs.IsExhaustive && !ActiveModule->Parent) {
2340 ActiveModule->ConfigMacrosExhaustive = true;
2343 // If we don't have an identifier, we're done.
2344 // FIXME: Support macros with the same name as a keyword here.
2345 if (!Tok.is(MMToken::Identifier))
2348 // Consume the first identifier.
2349 if (!ActiveModule->Parent) {
2350 ActiveModule->ConfigMacros.push_back(Tok.getString().str());
2355 // If there's a comma, consume it.
2356 if (!Tok.is(MMToken::Comma))
2360 // We expect to see a macro name here.
2361 // FIXME: Support macros with the same name as a keyword here.
2362 if (!Tok.is(MMToken::Identifier)) {
2363 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
2367 // Consume the macro name.
2368 if (!ActiveModule->Parent) {
2369 ActiveModule->ConfigMacros.push_back(Tok.getString().str());
2375 /// \brief Format a module-id into a string.
2376 static std::string formatModuleId(const ModuleId &Id) {
2379 llvm::raw_string_ostream OS(result);
2381 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
2391 /// \brief Parse a conflict declaration.
2393 /// module-declaration:
2394 /// 'conflict' module-id ',' string-literal
2395 void ModuleMapParser::parseConflict() {
2396 assert(Tok.is(MMToken::Conflict));
2397 SourceLocation ConflictLoc = consumeToken();
2398 Module::UnresolvedConflict Conflict;
2400 // Parse the module-id.
2401 if (parseModuleId(Conflict.Id))
2405 if (!Tok.is(MMToken::Comma)) {
2406 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
2407 << SourceRange(ConflictLoc);
2412 // Parse the message.
2413 if (!Tok.is(MMToken::StringLiteral)) {
2414 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
2415 << formatModuleId(Conflict.Id);
2418 Conflict.Message = Tok.getString().str();
2421 // Add this unresolved conflict.
2422 ActiveModule->UnresolvedConflicts.push_back(Conflict);
2425 /// \brief Parse an inferred module declaration (wildcard modules).
2427 /// module-declaration:
2428 /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
2429 /// { inferred-module-member* }
2431 /// inferred-module-member:
2433 /// 'exclude' identifier
2434 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
2435 assert(Tok.is(MMToken::Star));
2436 SourceLocation StarLoc = consumeToken();
2437 bool Failed = false;
2439 // Inferred modules must be submodules.
2440 if (!ActiveModule && !Framework) {
2441 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
2446 // Inferred modules must have umbrella directories.
2447 if (!Failed && ActiveModule->IsAvailable &&
2448 !ActiveModule->getUmbrellaDir()) {
2449 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
2453 // Check for redefinition of an inferred module.
2454 if (!Failed && ActiveModule->InferSubmodules) {
2455 Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
2456 if (ActiveModule->InferredSubmoduleLoc.isValid())
2457 Diags.Report(ActiveModule->InferredSubmoduleLoc,
2458 diag::note_mmap_prev_definition);
2462 // Check for the 'framework' keyword, which is not permitted here.
2464 Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
2467 } else if (Explicit) {
2468 Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
2472 // If there were any problems with this inferred submodule, skip its body.
2474 if (Tok.is(MMToken::LBrace)) {
2476 skipUntil(MMToken::RBrace);
2477 if (Tok.is(MMToken::RBrace))
2484 // Parse optional attributes.
2486 if (parseOptionalAttributes(Attrs))
2490 // Note that we have an inferred submodule.
2491 ActiveModule->InferSubmodules = true;
2492 ActiveModule->InferredSubmoduleLoc = StarLoc;
2493 ActiveModule->InferExplicitSubmodules = Explicit;
2495 // We'll be inferring framework modules for this directory.
2496 Map.InferredDirectories[Directory].InferModules = true;
2497 Map.InferredDirectories[Directory].Attrs = Attrs;
2498 Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile;
2499 // FIXME: Handle the 'framework' keyword.
2502 // Parse the opening brace.
2503 if (!Tok.is(MMToken::LBrace)) {
2504 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
2508 SourceLocation LBraceLoc = consumeToken();
2510 // Parse the body of the inferred submodule.
2514 case MMToken::EndOfFile:
2515 case MMToken::RBrace:
2519 case MMToken::ExcludeKeyword: {
2521 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2522 << (ActiveModule != nullptr);
2528 // FIXME: Support string-literal module names here.
2529 if (!Tok.is(MMToken::Identifier)) {
2530 Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
2534 Map.InferredDirectories[Directory].ExcludedModules
2535 .push_back(Tok.getString());
2540 case MMToken::ExportKeyword:
2541 if (!ActiveModule) {
2542 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2543 << (ActiveModule != nullptr);
2549 if (Tok.is(MMToken::Star))
2550 ActiveModule->InferExportWildcard = true;
2552 Diags.Report(Tok.getLocation(),
2553 diag::err_mmap_expected_export_wildcard);
2557 case MMToken::ExplicitKeyword:
2558 case MMToken::ModuleKeyword:
2559 case MMToken::HeaderKeyword:
2560 case MMToken::PrivateKeyword:
2561 case MMToken::UmbrellaKeyword:
2563 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2564 << (ActiveModule != nullptr);
2570 if (Tok.is(MMToken::RBrace))
2573 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
2574 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
2579 /// \brief Parse optional attributes.
2582 /// attribute attributes
2588 /// \param Attrs Will be filled in with the parsed attributes.
2590 /// \returns true if an error occurred, false otherwise.
2591 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
2592 bool HadError = false;
2594 while (Tok.is(MMToken::LSquare)) {
2596 SourceLocation LSquareLoc = consumeToken();
2598 // Check whether we have an attribute name here.
2599 if (!Tok.is(MMToken::Identifier)) {
2600 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
2601 skipUntil(MMToken::RSquare);
2602 if (Tok.is(MMToken::RSquare))
2607 // Decode the attribute name.
2608 AttributeKind Attribute
2609 = llvm::StringSwitch<AttributeKind>(Tok.getString())
2610 .Case("exhaustive", AT_exhaustive)
2611 .Case("extern_c", AT_extern_c)
2612 .Case("no_undeclared_includes", AT_no_undeclared_includes)
2613 .Case("system", AT_system)
2614 .Default(AT_unknown);
2615 switch (Attribute) {
2617 Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
2622 Attrs.IsSystem = true;
2626 Attrs.IsExternC = true;
2630 Attrs.IsExhaustive = true;
2633 case AT_no_undeclared_includes:
2634 Attrs.NoUndeclaredIncludes = true;
2640 if (!Tok.is(MMToken::RSquare)) {
2641 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
2642 Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
2643 skipUntil(MMToken::RSquare);
2647 if (Tok.is(MMToken::RSquare))
2654 /// \brief Parse a module map file.
2656 /// module-map-file:
2657 /// module-declaration*
2658 bool ModuleMapParser::parseModuleMapFile() {
2661 case MMToken::EndOfFile:
2664 case MMToken::ExplicitKeyword:
2665 case MMToken::ExternKeyword:
2666 case MMToken::ModuleKeyword:
2667 case MMToken::FrameworkKeyword:
2671 case MMToken::Comma:
2672 case MMToken::ConfigMacros:
2673 case MMToken::Conflict:
2674 case MMToken::Exclaim:
2675 case MMToken::ExcludeKeyword:
2676 case MMToken::ExportKeyword:
2677 case MMToken::HeaderKeyword:
2678 case MMToken::Identifier:
2679 case MMToken::LBrace:
2680 case MMToken::LinkKeyword:
2681 case MMToken::LSquare:
2682 case MMToken::Period:
2683 case MMToken::PrivateKeyword:
2684 case MMToken::RBrace:
2685 case MMToken::RSquare:
2686 case MMToken::RequiresKeyword:
2688 case MMToken::StringLiteral:
2689 case MMToken::IntegerLiteral:
2690 case MMToken::TextualKeyword:
2691 case MMToken::UmbrellaKeyword:
2692 case MMToken::UseKeyword:
2693 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
2701 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
2702 const DirectoryEntry *Dir, FileID ID,
2704 SourceLocation ExternModuleLoc) {
2705 assert(Target && "Missing target information");
2706 llvm::DenseMap<const FileEntry *, bool>::iterator Known
2707 = ParsedModuleMap.find(File);
2708 if (Known != ParsedModuleMap.end())
2709 return Known->second;
2711 // If the module map file wasn't already entered, do so now.
2712 if (ID.isInvalid()) {
2713 auto FileCharacter =
2714 IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap;
2715 ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter);
2718 assert(Target && "Missing target information");
2719 const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID);
2721 return ParsedModuleMap[File] = true;
2722 assert((!Offset || *Offset <= Buffer->getBufferSize()) &&
2723 "invalid buffer offset");
2725 // Parse this module map file.
2726 Lexer L(SourceMgr.getLocForStartOfFile(ID), MMapLangOpts,
2727 Buffer->getBufferStart(),
2728 Buffer->getBufferStart() + (Offset ? *Offset : 0),
2729 Buffer->getBufferEnd());
2730 SourceLocation Start = L.getSourceLocation();
2731 ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir,
2733 bool Result = Parser.parseModuleMapFile();
2734 ParsedModuleMap[File] = Result;
2737 auto Loc = SourceMgr.getDecomposedLoc(Parser.getLocation());
2738 assert(Loc.first == ID && "stopped in a different file?");
2739 *Offset = Loc.second;
2742 // Notify callbacks that we parsed it.
2743 for (const auto &Cb : Callbacks)
2744 Cb->moduleMapFileRead(Start, *File, IsSystem);