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/Lex/Lexer.h"
16 #include "clang/Lex/LiteralSupport.h"
17 #include "clang/Lex/LexDiagnostic.h"
18 #include "clang/Basic/Diagnostic.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/Basic/TargetOptions.h"
22 #include "llvm/Support/Allocator.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Support/PathV2.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/StringSwitch.h"
29 using namespace clang;
32 ModuleMap::resolveExport(Module *Mod,
33 const Module::UnresolvedExportDecl &Unresolved,
35 // We may have just a wildcard.
36 if (Unresolved.Id.empty()) {
37 assert(Unresolved.Wildcard && "Invalid unresolved export");
38 return Module::ExportDecl(0, true);
41 // Find the starting module.
42 Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
45 Diags->Report(Unresolved.Id[0].second,
46 diag::err_mmap_missing_module_unqualified)
47 << Unresolved.Id[0].first << Mod->getFullModuleName();
49 return Module::ExportDecl();
52 // Dig into the module path.
53 for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
54 Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
58 Diags->Report(Unresolved.Id[I].second,
59 diag::err_mmap_missing_module_qualified)
60 << Unresolved.Id[I].first << Context->getFullModuleName()
61 << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
63 return Module::ExportDecl();
69 return Module::ExportDecl(Context, Unresolved.Wildcard);
72 ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
73 const LangOptions &LangOpts, const TargetInfo *Target)
74 : LangOpts(LangOpts), Target(Target), BuiltinIncludeDir(0)
76 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
77 Diags = IntrusiveRefCntPtr<DiagnosticsEngine>(
78 new DiagnosticsEngine(DiagIDs));
79 Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
80 SourceMgr = new SourceManager(*Diags, FileMgr);
83 ModuleMap::~ModuleMap() {
84 for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
93 void ModuleMap::setTarget(const TargetInfo &Target) {
94 assert((!this->Target || this->Target == &Target) &&
95 "Improper target override");
96 this->Target = &Target;
99 Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
100 llvm::DenseMap<const FileEntry *, Module *>::iterator Known
101 = Headers.find(File);
102 if (Known != Headers.end()) {
103 // If a header corresponds to an unavailable module, don't report
104 // that it maps to anything.
105 if (!Known->second->isAvailable())
108 return Known->second;
111 const DirectoryEntry *Dir = File->getDir();
112 llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
113 StringRef DirName = Dir->getName();
115 // Keep walking up the directory hierarchy, looking for a directory with
116 // an umbrella header.
118 llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
119 = UmbrellaDirs.find(Dir);
120 if (KnownDir != UmbrellaDirs.end()) {
121 Module *Result = KnownDir->second;
123 // Search up the module stack until we find a module with an umbrella
125 Module *UmbrellaModule = Result;
126 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
127 UmbrellaModule = UmbrellaModule->Parent;
129 if (UmbrellaModule->InferSubmodules) {
130 // Infer submodules for each of the directories we found between
131 // the directory of the umbrella header and the directory where
132 // the actual header is located.
133 bool Explicit = UmbrellaModule->InferExplicitSubmodules;
135 for (unsigned I = SkippedDirs.size(); I != 0; --I) {
136 // Find or create the module that corresponds to this directory name.
137 StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
138 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
141 // Associate the module and the directory.
142 UmbrellaDirs[SkippedDirs[I-1]] = Result;
144 // If inferred submodules export everything they import, add a
145 // wildcard to the set of exports.
146 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
147 Result->Exports.push_back(Module::ExportDecl(0, true));
150 // Infer a submodule with the same name as this header file.
151 StringRef Name = llvm::sys::path::stem(File->getName());
152 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
155 // If inferred submodules export everything they import, add a
156 // wildcard to the set of exports.
157 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
158 Result->Exports.push_back(Module::ExportDecl(0, true));
160 // Record each of the directories we stepped through as being part of
161 // the module we found, since the umbrella header covers them all.
162 for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
163 UmbrellaDirs[SkippedDirs[I]] = Result;
166 Headers[File] = Result;
168 // If a header corresponds to an unavailable module, don't report
169 // that it maps to anything.
170 if (!Result->isAvailable())
176 SkippedDirs.push_back(Dir);
178 // Retrieve our parent path.
179 DirName = llvm::sys::path::parent_path(DirName);
183 // Resolve the parent path to a directory entry.
184 Dir = SourceMgr->getFileManager().getDirectory(DirName);
190 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) {
191 llvm::DenseMap<const FileEntry *, Module *>::iterator Known
192 = Headers.find(Header);
193 if (Known != Headers.end())
194 return !Known->second->isAvailable();
196 const DirectoryEntry *Dir = Header->getDir();
197 llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
198 StringRef DirName = Dir->getName();
200 // Keep walking up the directory hierarchy, looking for a directory with
201 // an umbrella header.
203 llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
204 = UmbrellaDirs.find(Dir);
205 if (KnownDir != UmbrellaDirs.end()) {
206 Module *Found = KnownDir->second;
207 if (!Found->isAvailable())
210 // Search up the module stack until we find a module with an umbrella
212 Module *UmbrellaModule = Found;
213 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
214 UmbrellaModule = UmbrellaModule->Parent;
216 if (UmbrellaModule->InferSubmodules) {
217 for (unsigned I = SkippedDirs.size(); I != 0; --I) {
218 // Find or create the module that corresponds to this directory name.
219 StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
220 Found = lookupModuleQualified(Name, Found);
223 if (!Found->isAvailable())
227 // Infer a submodule with the same name as this header file.
228 StringRef Name = llvm::sys::path::stem(Header->getName());
229 Found = lookupModuleQualified(Name, Found);
234 return !Found->isAvailable();
237 SkippedDirs.push_back(Dir);
239 // Retrieve our parent path.
240 DirName = llvm::sys::path::parent_path(DirName);
244 // Resolve the parent path to a directory entry.
245 Dir = SourceMgr->getFileManager().getDirectory(DirName);
251 Module *ModuleMap::findModule(StringRef Name) {
252 llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
253 if (Known != Modules.end())
254 return Known->getValue();
259 Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
260 for(; Context; Context = Context->Parent) {
261 if (Module *Sub = lookupModuleQualified(Name, Context))
265 return findModule(Name);
268 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
270 return findModule(Name);
272 return Context->findSubmodule(Name);
275 std::pair<Module *, bool>
276 ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
278 // Try to find an existing module with this name.
279 if (Module *Sub = lookupModuleQualified(Name, Parent))
280 return std::make_pair(Sub, false);
282 // Create a new module with this name.
283 Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
286 Modules[Name] = Result;
287 return std::make_pair(Result, true);
291 ModuleMap::inferFrameworkModule(StringRef ModuleName,
292 const DirectoryEntry *FrameworkDir,
295 // Check whether we've already found this module.
296 if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
299 FileManager &FileMgr = SourceMgr->getFileManager();
301 // Look for an umbrella header.
302 SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
303 llvm::sys::path::append(UmbrellaName, "Headers");
304 llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
305 const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
307 // FIXME: If there's no umbrella header, we could probably scan the
308 // framework to load *everything*. But, it's not clear that this is a good
313 Module *Result = new Module(ModuleName, SourceLocation(), Parent,
314 /*IsFramework=*/true, /*IsExplicit=*/false);
316 Result->IsSystem = IsSystem;
319 Modules[ModuleName] = Result;
321 // umbrella header "umbrella-header-name"
322 Result->Umbrella = UmbrellaHeader;
323 Headers[UmbrellaHeader] = Result;
324 UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
327 Result->Exports.push_back(Module::ExportDecl(0, true));
329 // module * { export * }
330 Result->InferSubmodules = true;
331 Result->InferExportWildcard = true;
333 // Look for subframeworks.
335 SmallString<128> SubframeworksDirName
336 = StringRef(FrameworkDir->getName());
337 llvm::sys::path::append(SubframeworksDirName, "Frameworks");
338 SmallString<128> SubframeworksDirNameNative;
339 llvm::sys::path::native(SubframeworksDirName.str(),
340 SubframeworksDirNameNative);
341 for (llvm::sys::fs::directory_iterator
342 Dir(SubframeworksDirNameNative.str(), EC), DirEnd;
343 Dir != DirEnd && !EC; Dir.increment(EC)) {
344 if (!StringRef(Dir->path()).endswith(".framework"))
347 if (const DirectoryEntry *SubframeworkDir
348 = FileMgr.getDirectory(Dir->path())) {
349 // FIXME: Do we want to warn about subframeworks without umbrella headers?
350 inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
358 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
359 Headers[UmbrellaHeader] = Mod;
360 Mod->Umbrella = UmbrellaHeader;
361 UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
364 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
365 Mod->Umbrella = UmbrellaDir;
366 UmbrellaDirs[UmbrellaDir] = Mod;
369 void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
370 Mod->Headers.push_back(Header);
371 Headers[Header] = Mod;
375 ModuleMap::getContainingModuleMapFile(Module *Module) {
376 if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
379 return SourceMgr->getFileEntryForID(
380 SourceMgr->getFileID(Module->DefinitionLoc));
383 void ModuleMap::dump() {
384 llvm::errs() << "Modules:";
385 for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
386 MEnd = Modules.end();
388 M->getValue()->print(llvm::errs(), 2);
390 llvm::errs() << "Headers:";
391 for (llvm::DenseMap<const FileEntry *, Module *>::iterator
393 HEnd = Headers.end();
395 llvm::errs() << " \"" << H->first->getName() << "\" -> "
396 << H->second->getFullModuleName() << "\n";
400 bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
401 bool HadError = false;
402 for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
403 Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
405 if (Export.getPointer() || Export.getInt())
406 Mod->Exports.push_back(Export);
410 Mod->UnresolvedExports.clear();
414 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
418 // Use the expansion location to determine which module we're in.
419 FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
420 if (!ExpansionLoc.isFileID())
424 const SourceManager &SrcMgr = Loc.getManager();
425 FileID ExpansionFileID = ExpansionLoc.getFileID();
427 while (const FileEntry *ExpansionFile
428 = SrcMgr.getFileEntryForID(ExpansionFileID)) {
429 // Find the module that owns this header (if any).
430 if (Module *Mod = findModuleForHeader(ExpansionFile))
433 // No module owns this header, so look up the inclusion chain to see if
434 // any included header has an associated module.
435 SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
436 if (IncludeLoc.isInvalid())
439 ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
445 //----------------------------------------------------------------------------//
446 // Module map file parser
447 //----------------------------------------------------------------------------//
450 /// \brief A token in a module map file.
473 unsigned StringLength;
474 const char *StringData;
483 bool is(TokenKind K) const { return Kind == K; }
485 SourceLocation getLocation() const {
486 return SourceLocation::getFromRawEncoding(Location);
489 StringRef getString() const {
490 return StringRef(StringData, StringLength);
494 class ModuleMapParser {
496 SourceManager &SourceMgr;
497 DiagnosticsEngine &Diags;
500 /// \brief The directory that this module map resides in.
501 const DirectoryEntry *Directory;
503 /// \brief The directory containing Clang-supplied headers.
504 const DirectoryEntry *BuiltinIncludeDir;
506 /// \brief Whether an error occurred.
509 /// \brief Default target information, used only for string literal
511 OwningPtr<TargetInfo> Target;
513 /// \brief Stores string data for the various string literals referenced
515 llvm::BumpPtrAllocator StringData;
517 /// \brief The current token.
520 /// \brief The active module.
521 Module *ActiveModule;
523 /// \brief Consume the current token and return its location.
524 SourceLocation consumeToken();
526 /// \brief Skip tokens until we reach the a token with the given kind
527 /// (or the end of the file).
528 void skipUntil(MMToken::TokenKind K);
530 typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
532 bool parseModuleId(ModuleId &Id);
533 void parseModuleDecl();
534 void parseRequiresDecl();
535 void parseHeaderDecl(SourceLocation UmbrellaLoc);
536 void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
537 void parseExportDecl();
538 void parseInferredSubmoduleDecl(bool Explicit);
540 const DirectoryEntry *getOverriddenHeaderSearchDir();
543 explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
544 DiagnosticsEngine &Diags,
546 const DirectoryEntry *Directory,
547 const DirectoryEntry *BuiltinIncludeDir)
548 : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
549 Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir),
550 HadError(false), ActiveModule(0)
552 TargetOptions TargetOpts;
553 TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
554 Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
560 bool parseModuleMapFile();
564 SourceLocation ModuleMapParser::consumeToken() {
566 SourceLocation Result = Tok.getLocation();
570 L.LexFromRawLexer(LToken);
571 Tok.Location = LToken.getLocation().getRawEncoding();
572 switch (LToken.getKind()) {
573 case tok::raw_identifier:
574 Tok.StringData = LToken.getRawIdentifierData();
575 Tok.StringLength = LToken.getLength();
576 Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
577 .Case("header", MMToken::HeaderKeyword)
578 .Case("explicit", MMToken::ExplicitKeyword)
579 .Case("export", MMToken::ExportKeyword)
580 .Case("framework", MMToken::FrameworkKeyword)
581 .Case("module", MMToken::ModuleKeyword)
582 .Case("requires", MMToken::RequiresKeyword)
583 .Case("umbrella", MMToken::UmbrellaKeyword)
584 .Default(MMToken::Identifier);
588 Tok.Kind = MMToken::Comma;
592 Tok.Kind = MMToken::EndOfFile;
596 Tok.Kind = MMToken::LBrace;
600 Tok.Kind = MMToken::LSquare;
604 Tok.Kind = MMToken::Period;
608 Tok.Kind = MMToken::RBrace;
612 Tok.Kind = MMToken::RSquare;
616 Tok.Kind = MMToken::Star;
619 case tok::string_literal: {
620 if (LToken.hasUDSuffix()) {
621 Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
626 // Parse the string literal.
627 LangOptions LangOpts;
628 StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target);
629 if (StringLiteral.hadError)
632 // Copy the string literal into our string data allocator.
633 unsigned Length = StringLiteral.GetStringLength();
634 char *Saved = StringData.Allocate<char>(Length + 1);
635 memcpy(Saved, StringLiteral.GetString().data(), Length);
639 Tok.Kind = MMToken::StringLiteral;
640 Tok.StringData = Saved;
641 Tok.StringLength = Length;
649 Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
657 void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
658 unsigned braceDepth = 0;
659 unsigned squareDepth = 0;
662 case MMToken::EndOfFile:
665 case MMToken::LBrace:
666 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
672 case MMToken::LSquare:
673 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
679 case MMToken::RBrace:
686 case MMToken::RSquare:
694 if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
703 /// \brief Parse a module-id.
707 /// identifier '.' module-id
709 /// \returns true if an error occurred, false otherwise.
710 bool ModuleMapParser::parseModuleId(ModuleId &Id) {
713 if (Tok.is(MMToken::Identifier)) {
714 Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
717 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
721 if (!Tok.is(MMToken::Period))
731 /// \brief Enumerates the known attributes.
733 /// \brief An unknown attribute.
735 /// \brief The 'system' attribute.
740 /// \brief Parse a module declaration.
742 /// module-declaration:
743 /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
744 /// { module-member* }
747 /// attribute attributes
754 /// requires-declaration
755 /// header-declaration
756 /// submodule-declaration
757 /// export-declaration
759 /// submodule-declaration:
760 /// module-declaration
761 /// inferred-submodule-declaration
762 void ModuleMapParser::parseModuleDecl() {
763 assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
764 Tok.is(MMToken::FrameworkKeyword));
765 // Parse 'explicit' or 'framework' keyword, if present.
766 SourceLocation ExplicitLoc;
767 bool Explicit = false;
768 bool Framework = false;
770 // Parse 'explicit' keyword, if present.
771 if (Tok.is(MMToken::ExplicitKeyword)) {
772 ExplicitLoc = consumeToken();
776 // Parse 'framework' keyword, if present.
777 if (Tok.is(MMToken::FrameworkKeyword)) {
782 // Parse 'module' keyword.
783 if (!Tok.is(MMToken::ModuleKeyword)) {
784 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
789 consumeToken(); // 'module' keyword
791 // If we have a wildcard for the module name, this is an inferred submodule.
793 if (Tok.is(MMToken::Star))
794 return parseInferredSubmoduleDecl(Explicit);
796 // Parse the module name.
798 if (parseModuleId(Id)) {
805 Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
806 << SourceRange(Id.front().second, Id.back().second);
811 } else if (Id.size() == 1 && Explicit) {
812 // Top-level modules can't be explicit.
813 Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
815 ExplicitLoc = SourceLocation();
819 Module *PreviousActiveModule = ActiveModule;
821 // This module map defines a submodule. Go find the module of which it
824 for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
825 if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
831 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
832 << Id[I].first << ActiveModule->getTopLevelModule();
834 Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
841 StringRef ModuleName = Id.back().first;
842 SourceLocation ModuleNameLoc = Id.back().second;
844 // Parse the optional attribute list.
845 bool IsSystem = false;
846 while (Tok.is(MMToken::LSquare)) {
848 SourceLocation LSquareLoc = consumeToken();
850 // Check whether we have an attribute name here.
851 if (!Tok.is(MMToken::Identifier)) {
852 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
853 skipUntil(MMToken::RSquare);
854 if (Tok.is(MMToken::RSquare))
859 // Decode the attribute name.
860 AttributeKind Attribute
861 = llvm::StringSwitch<AttributeKind>(Tok.getString())
862 .Case("system", AT_system)
863 .Default(AT_unknown);
866 Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
877 if (!Tok.is(MMToken::RSquare)) {
878 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
879 Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
880 skipUntil(MMToken::RSquare);
883 if (Tok.is(MMToken::RSquare))
887 // Parse the opening brace.
888 if (!Tok.is(MMToken::LBrace)) {
889 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
894 SourceLocation LBraceLoc = consumeToken();
896 // Determine whether this (sub)module has already been defined.
897 if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
898 if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
899 // Skip the module definition.
900 skipUntil(MMToken::RBrace);
901 if (Tok.is(MMToken::RBrace))
904 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
905 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
911 Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
913 Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
915 // Skip the module definition.
916 skipUntil(MMToken::RBrace);
917 if (Tok.is(MMToken::RBrace))
924 // Start defining this module.
925 ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
927 ActiveModule->DefinitionLoc = ModuleNameLoc;
929 ActiveModule->IsSystem = true;
934 case MMToken::EndOfFile:
935 case MMToken::RBrace:
939 case MMToken::ExplicitKeyword:
940 case MMToken::FrameworkKeyword:
941 case MMToken::ModuleKeyword:
945 case MMToken::ExportKeyword:
949 case MMToken::RequiresKeyword:
953 case MMToken::UmbrellaKeyword: {
954 SourceLocation UmbrellaLoc = consumeToken();
955 if (Tok.is(MMToken::HeaderKeyword))
956 parseHeaderDecl(UmbrellaLoc);
958 parseUmbrellaDirDecl(UmbrellaLoc);
962 case MMToken::HeaderKeyword:
963 parseHeaderDecl(SourceLocation());
967 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
973 if (Tok.is(MMToken::RBrace))
976 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
977 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
981 // We're done parsing this module. Pop back to the previous module.
982 ActiveModule = PreviousActiveModule;
985 /// \brief Parse a requires declaration.
987 /// requires-declaration:
988 /// 'requires' feature-list
991 /// identifier ',' feature-list
993 void ModuleMapParser::parseRequiresDecl() {
994 assert(Tok.is(MMToken::RequiresKeyword));
996 // Parse 'requires' keyword.
999 // Parse the feature-list.
1001 if (!Tok.is(MMToken::Identifier)) {
1002 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
1007 // Consume the feature name.
1008 std::string Feature = Tok.getString();
1011 // Add this feature.
1012 ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target);
1014 if (!Tok.is(MMToken::Comma))
1017 // Consume the comma.
1022 /// \brief Append to \p Paths the set of paths needed to get to the
1023 /// subframework in which the given module lives.
1024 static void appendSubframeworkPaths(Module *Mod,
1025 llvm::SmallVectorImpl<char> &Path) {
1026 // Collect the framework names from the given module to the top-level module.
1027 llvm::SmallVector<StringRef, 2> Paths;
1028 for (; Mod; Mod = Mod->Parent) {
1029 if (Mod->IsFramework)
1030 Paths.push_back(Mod->Name);
1036 // Add Frameworks/Name.framework for each subframework.
1037 for (unsigned I = Paths.size() - 1; I != 0; --I) {
1038 llvm::sys::path::append(Path, "Frameworks");
1039 llvm::sys::path::append(Path, Paths[I-1] + ".framework");
1043 /// \brief Determine whether the given file name is the name of a builtin
1044 /// header, supplied by Clang to replace, override, or augment existing system
1046 static bool isBuiltinHeader(StringRef FileName) {
1047 return llvm::StringSwitch<bool>(FileName)
1048 .Case("float.h", true)
1049 .Case("iso646.h", true)
1050 .Case("limits.h", true)
1051 .Case("stdalign.h", true)
1052 .Case("stdarg.h", true)
1053 .Case("stdbool.h", true)
1054 .Case("stddef.h", true)
1055 .Case("stdint.h", true)
1056 .Case("tgmath.h", true)
1057 .Case("unwind.h", true)
1061 /// \brief Parse a header declaration.
1063 /// header-declaration:
1064 /// 'umbrella'[opt] 'header' string-literal
1065 void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) {
1066 assert(Tok.is(MMToken::HeaderKeyword));
1069 bool Umbrella = UmbrellaLoc.isValid();
1071 // Parse the header name.
1072 if (!Tok.is(MMToken::StringLiteral)) {
1073 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1078 std::string FileName = Tok.getString();
1079 SourceLocation FileNameLoc = consumeToken();
1081 // Check whether we already have an umbrella.
1082 if (Umbrella && ActiveModule->Umbrella) {
1083 Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
1084 << ActiveModule->getFullModuleName();
1089 // Look for this file.
1090 const FileEntry *File = 0;
1091 const FileEntry *BuiltinFile = 0;
1092 SmallString<128> PathName;
1093 if (llvm::sys::path::is_absolute(FileName)) {
1094 PathName = FileName;
1095 File = SourceMgr.getFileManager().getFile(PathName);
1096 } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
1097 PathName = Dir->getName();
1098 llvm::sys::path::append(PathName, FileName);
1099 File = SourceMgr.getFileManager().getFile(PathName);
1101 // Search for the header file within the search directory.
1102 PathName = Directory->getName();
1103 unsigned PathLength = PathName.size();
1105 if (ActiveModule->isPartOfFramework()) {
1106 appendSubframeworkPaths(ActiveModule, PathName);
1108 // Check whether this file is in the public headers.
1109 llvm::sys::path::append(PathName, "Headers");
1110 llvm::sys::path::append(PathName, FileName);
1111 File = SourceMgr.getFileManager().getFile(PathName);
1114 // Check whether this file is in the private headers.
1115 PathName.resize(PathLength);
1116 llvm::sys::path::append(PathName, "PrivateHeaders");
1117 llvm::sys::path::append(PathName, FileName);
1118 File = SourceMgr.getFileManager().getFile(PathName);
1121 // Lookup for normal headers.
1122 llvm::sys::path::append(PathName, FileName);
1123 File = SourceMgr.getFileManager().getFile(PathName);
1125 // If this is a system module with a top-level header, this header
1126 // may have a counterpart (or replacement) in the set of headers
1127 // supplied by Clang. Find that builtin header.
1128 if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir &&
1129 BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) {
1130 SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
1131 llvm::sys::path::append(BuiltinPathName, FileName);
1132 BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
1134 // If Clang supplies this header but the underlying system does not,
1135 // just silently swap in our builtin version. Otherwise, we'll end
1136 // up adding both (later).
1137 if (!File && BuiltinFile) {
1145 // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
1146 // Come up with a lazy way to do this.
1148 if (const Module *OwningModule = Map.Headers[File]) {
1149 Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
1150 << FileName << OwningModule->getFullModuleName();
1152 } else if (Umbrella) {
1153 const DirectoryEntry *UmbrellaDir = File->getDir();
1154 if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
1155 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1156 << OwningModule->getFullModuleName();
1159 // Record this umbrella header.
1160 Map.setUmbrellaHeader(ActiveModule, File);
1163 // Record this header.
1164 Map.addHeader(ActiveModule, File);
1166 // If there is a builtin counterpart to this file, add it now.
1168 Map.addHeader(ActiveModule, BuiltinFile);
1171 Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
1172 << Umbrella << FileName;
1177 /// \brief Parse an umbrella directory declaration.
1179 /// umbrella-dir-declaration:
1180 /// umbrella string-literal
1181 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1182 // Parse the directory name.
1183 if (!Tok.is(MMToken::StringLiteral)) {
1184 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1190 std::string DirName = Tok.getString();
1191 SourceLocation DirNameLoc = consumeToken();
1193 // Check whether we already have an umbrella.
1194 if (ActiveModule->Umbrella) {
1195 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1196 << ActiveModule->getFullModuleName();
1201 // Look for this file.
1202 const DirectoryEntry *Dir = 0;
1203 if (llvm::sys::path::is_absolute(DirName))
1204 Dir = SourceMgr.getFileManager().getDirectory(DirName);
1206 SmallString<128> PathName;
1207 PathName = Directory->getName();
1208 llvm::sys::path::append(PathName, DirName);
1209 Dir = SourceMgr.getFileManager().getDirectory(PathName);
1213 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1219 if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1220 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1221 << OwningModule->getFullModuleName();
1226 // Record this umbrella directory.
1227 Map.setUmbrellaDir(ActiveModule, Dir);
1230 /// \brief Parse a module export declaration.
1232 /// export-declaration:
1233 /// 'export' wildcard-module-id
1235 /// wildcard-module-id:
1238 /// identifier '.' wildcard-module-id
1239 void ModuleMapParser::parseExportDecl() {
1240 assert(Tok.is(MMToken::ExportKeyword));
1241 SourceLocation ExportLoc = consumeToken();
1243 // Parse the module-id with an optional wildcard at the end.
1244 ModuleId ParsedModuleId;
1245 bool Wildcard = false;
1247 if (Tok.is(MMToken::Identifier)) {
1248 ParsedModuleId.push_back(std::make_pair(Tok.getString(),
1249 Tok.getLocation()));
1252 if (Tok.is(MMToken::Period)) {
1260 if(Tok.is(MMToken::Star)) {
1266 Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
1271 Module::UnresolvedExportDecl Unresolved = {
1272 ExportLoc, ParsedModuleId, Wildcard
1274 ActiveModule->UnresolvedExports.push_back(Unresolved);
1277 void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
1278 assert(Tok.is(MMToken::Star));
1279 SourceLocation StarLoc = consumeToken();
1280 bool Failed = false;
1282 // Inferred modules must be submodules.
1283 if (!ActiveModule) {
1284 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
1288 // Inferred modules must have umbrella directories.
1289 if (!Failed && !ActiveModule->getUmbrellaDir()) {
1290 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
1294 // Check for redefinition of an inferred module.
1295 if (!Failed && ActiveModule->InferSubmodules) {
1296 Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1297 if (ActiveModule->InferredSubmoduleLoc.isValid())
1298 Diags.Report(ActiveModule->InferredSubmoduleLoc,
1299 diag::note_mmap_prev_definition);
1303 // If there were any problems with this inferred submodule, skip its body.
1305 if (Tok.is(MMToken::LBrace)) {
1307 skipUntil(MMToken::RBrace);
1308 if (Tok.is(MMToken::RBrace))
1315 // Note that we have an inferred submodule.
1316 ActiveModule->InferSubmodules = true;
1317 ActiveModule->InferredSubmoduleLoc = StarLoc;
1318 ActiveModule->InferExplicitSubmodules = Explicit;
1320 // Parse the opening brace.
1321 if (!Tok.is(MMToken::LBrace)) {
1322 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
1326 SourceLocation LBraceLoc = consumeToken();
1328 // Parse the body of the inferred submodule.
1332 case MMToken::EndOfFile:
1333 case MMToken::RBrace:
1337 case MMToken::ExportKeyword: {
1339 if (Tok.is(MMToken::Star))
1340 ActiveModule->InferExportWildcard = true;
1342 Diags.Report(Tok.getLocation(),
1343 diag::err_mmap_expected_export_wildcard);
1348 case MMToken::ExplicitKeyword:
1349 case MMToken::ModuleKeyword:
1350 case MMToken::HeaderKeyword:
1351 case MMToken::UmbrellaKeyword:
1353 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
1359 if (Tok.is(MMToken::RBrace))
1362 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1363 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1368 /// \brief If there is a specific header search directory due the presence
1369 /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
1370 const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
1371 for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
1372 // If we have an umbrella directory, use that.
1373 if (Mod->hasUmbrellaDir())
1374 return Mod->getUmbrellaDir();
1376 // If we have a framework directory, stop looking.
1377 if (Mod->IsFramework)
1384 /// \brief Parse a module map file.
1386 /// module-map-file:
1387 /// module-declaration*
1388 bool ModuleMapParser::parseModuleMapFile() {
1391 case MMToken::EndOfFile:
1394 case MMToken::ExplicitKeyword:
1395 case MMToken::ModuleKeyword:
1396 case MMToken::FrameworkKeyword:
1400 case MMToken::Comma:
1401 case MMToken::ExportKeyword:
1402 case MMToken::HeaderKeyword:
1403 case MMToken::Identifier:
1404 case MMToken::LBrace:
1405 case MMToken::LSquare:
1406 case MMToken::Period:
1407 case MMToken::RBrace:
1408 case MMToken::RSquare:
1409 case MMToken::RequiresKeyword:
1411 case MMToken::StringLiteral:
1412 case MMToken::UmbrellaKeyword:
1413 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1421 bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
1422 assert(Target != 0 && "Missing target information");
1423 FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1424 const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1428 // Parse this module map file.
1429 Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
1430 Diags->getClient()->BeginSourceFile(MMapLangOpts);
1431 ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir(),
1433 bool Result = Parser.parseModuleMapFile();
1434 Diags->getClient()->EndSourceFile();