1718292f2SDouglas Gregor //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===// 2718292f2SDouglas Gregor // 3718292f2SDouglas Gregor // The LLVM Compiler Infrastructure 4718292f2SDouglas Gregor // 5718292f2SDouglas Gregor // This file is distributed under the University of Illinois Open Source 6718292f2SDouglas Gregor // License. See LICENSE.TXT for details. 7718292f2SDouglas Gregor // 8718292f2SDouglas Gregor //===----------------------------------------------------------------------===// 9718292f2SDouglas Gregor // 10718292f2SDouglas Gregor // This file defines the ModuleMap implementation, which describes the layout 11718292f2SDouglas Gregor // of a module as it relates to headers. 12718292f2SDouglas Gregor // 13718292f2SDouglas Gregor //===----------------------------------------------------------------------===// 14718292f2SDouglas Gregor #include "clang/Lex/ModuleMap.h" 15718292f2SDouglas Gregor #include "clang/Lex/Lexer.h" 16718292f2SDouglas Gregor #include "clang/Lex/LiteralSupport.h" 17718292f2SDouglas Gregor #include "clang/Lex/LexDiagnostic.h" 18718292f2SDouglas Gregor #include "clang/Basic/Diagnostic.h" 19718292f2SDouglas Gregor #include "clang/Basic/FileManager.h" 20718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h" 21718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h" 22718292f2SDouglas Gregor #include "llvm/Support/Allocator.h" 23718292f2SDouglas Gregor #include "llvm/Support/Host.h" 245257fc63SDouglas Gregor #include "llvm/Support/PathV2.h" 25718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h" 26718292f2SDouglas Gregor #include "llvm/ADT/StringRef.h" 27718292f2SDouglas Gregor #include "llvm/ADT/StringSwitch.h" 28718292f2SDouglas Gregor using namespace clang; 29718292f2SDouglas Gregor 30718292f2SDouglas Gregor //----------------------------------------------------------------------------// 31718292f2SDouglas Gregor // Module 32718292f2SDouglas Gregor //----------------------------------------------------------------------------// 33718292f2SDouglas Gregor 34718292f2SDouglas Gregor std::string ModuleMap::Module::getFullModuleName() const { 35718292f2SDouglas Gregor llvm::SmallVector<StringRef, 2> Names; 36718292f2SDouglas Gregor 37718292f2SDouglas Gregor // Build up the set of module names (from innermost to outermost). 38718292f2SDouglas Gregor for (const Module *M = this; M; M = M->Parent) 39718292f2SDouglas Gregor Names.push_back(M->Name); 40718292f2SDouglas Gregor 41718292f2SDouglas Gregor std::string Result; 42718292f2SDouglas Gregor for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(), 43718292f2SDouglas Gregor IEnd = Names.rend(); 44718292f2SDouglas Gregor I != IEnd; ++I) { 45718292f2SDouglas Gregor if (!Result.empty()) 46718292f2SDouglas Gregor Result += '.'; 47718292f2SDouglas Gregor 48718292f2SDouglas Gregor Result += *I; 49718292f2SDouglas Gregor } 50718292f2SDouglas Gregor 51718292f2SDouglas Gregor return Result; 52718292f2SDouglas Gregor } 53718292f2SDouglas Gregor 54ab0c8a84SDouglas Gregor StringRef ModuleMap::Module::getTopLevelModuleName() const { 55ab0c8a84SDouglas Gregor const Module *Top = this; 56ab0c8a84SDouglas Gregor while (Top->Parent) 57ab0c8a84SDouglas Gregor Top = Top->Parent; 58ab0c8a84SDouglas Gregor 59ab0c8a84SDouglas Gregor return Top->Name; 60ab0c8a84SDouglas Gregor } 61ab0c8a84SDouglas Gregor 62718292f2SDouglas Gregor //----------------------------------------------------------------------------// 63718292f2SDouglas Gregor // Module map 64718292f2SDouglas Gregor //----------------------------------------------------------------------------// 65718292f2SDouglas Gregor 66718292f2SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC) { 67718292f2SDouglas Gregor llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 68718292f2SDouglas Gregor Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>( 69718292f2SDouglas Gregor new DiagnosticsEngine(DiagIDs)); 70718292f2SDouglas Gregor Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true); 71718292f2SDouglas Gregor SourceMgr = new SourceManager(*Diags, FileMgr); 72718292f2SDouglas Gregor } 73718292f2SDouglas Gregor 74718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 75718292f2SDouglas Gregor delete SourceMgr; 76718292f2SDouglas Gregor } 77718292f2SDouglas Gregor 78ab0c8a84SDouglas Gregor ModuleMap::Module *ModuleMap::findModuleForHeader(const FileEntry *File) { 79ab0c8a84SDouglas Gregor llvm::DenseMap<const FileEntry *, Module *>::iterator Known 80ab0c8a84SDouglas Gregor = Headers.find(File); 81ab0c8a84SDouglas Gregor if (Known != Headers.end()) 82ab0c8a84SDouglas Gregor return Known->second; 83ab0c8a84SDouglas Gregor 84b65dbfffSDouglas Gregor const DirectoryEntry *Dir = File->getDir(); 85b65dbfffSDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 86b65dbfffSDouglas Gregor = UmbrellaDirs.find(Dir); 87b65dbfffSDouglas Gregor if (KnownDir != UmbrellaDirs.end()) 88b65dbfffSDouglas Gregor return KnownDir->second; 89b65dbfffSDouglas Gregor 90b65dbfffSDouglas Gregor // Walk up the directory hierarchy looking for umbrella headers. 91b65dbfffSDouglas Gregor llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs; 92b65dbfffSDouglas Gregor StringRef DirName = Dir->getName(); 93b65dbfffSDouglas Gregor do { 94b65dbfffSDouglas Gregor // Retrieve our parent path. 95b65dbfffSDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 96b65dbfffSDouglas Gregor if (DirName.empty()) 97b65dbfffSDouglas Gregor break; 98b65dbfffSDouglas Gregor 99b65dbfffSDouglas Gregor // Resolve the parent path to a directory entry. 100b65dbfffSDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 101b65dbfffSDouglas Gregor if (!Dir) 102b65dbfffSDouglas Gregor break; 103b65dbfffSDouglas Gregor 104b65dbfffSDouglas Gregor KnownDir = UmbrellaDirs.find(Dir); 105b65dbfffSDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 106b65dbfffSDouglas Gregor Module *Result = KnownDir->second; 107b65dbfffSDouglas Gregor 108b65dbfffSDouglas Gregor // Record each of the directories we stepped through as being part of 109b65dbfffSDouglas Gregor // the module we found, since the umbrella header covers them all. 110b65dbfffSDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 111b65dbfffSDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 112b65dbfffSDouglas Gregor 113b65dbfffSDouglas Gregor return Result; 114b65dbfffSDouglas Gregor } 115b65dbfffSDouglas Gregor 116b65dbfffSDouglas Gregor SkippedDirs.push_back(Dir); 117b65dbfffSDouglas Gregor } while (true); 118b65dbfffSDouglas Gregor 119ab0c8a84SDouglas Gregor return 0; 120ab0c8a84SDouglas Gregor } 121ab0c8a84SDouglas Gregor 12288bdfb0eSDouglas Gregor ModuleMap::Module *ModuleMap::findModule(StringRef Name) { 12388bdfb0eSDouglas Gregor llvm::StringMap<Module *>::iterator Known = Modules.find(Name); 12488bdfb0eSDouglas Gregor if (Known != Modules.end()) 12588bdfb0eSDouglas Gregor return Known->getValue(); 12688bdfb0eSDouglas Gregor 12788bdfb0eSDouglas Gregor return 0; 12888bdfb0eSDouglas Gregor } 12988bdfb0eSDouglas Gregor 130*56c64013SDouglas Gregor ModuleMap::Module * 131*56c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 132*56c64013SDouglas Gregor const DirectoryEntry *FrameworkDir) { 133*56c64013SDouglas Gregor // Check whether we've already found this module. 134*56c64013SDouglas Gregor if (Module *Module = findModule(ModuleName)) 135*56c64013SDouglas Gregor return Module; 136*56c64013SDouglas Gregor 137*56c64013SDouglas Gregor // Look for an umbrella header. 138*56c64013SDouglas Gregor llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 139*56c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, "Headers"); 140*56c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, ModuleName + ".h"); 141*56c64013SDouglas Gregor const FileEntry *UmbrellaHeader 142*56c64013SDouglas Gregor = SourceMgr->getFileManager().getFile(UmbrellaName); 143*56c64013SDouglas Gregor 144*56c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 145*56c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 146*56c64013SDouglas Gregor // idea. 147*56c64013SDouglas Gregor if (!UmbrellaHeader) 148*56c64013SDouglas Gregor return 0; 149*56c64013SDouglas Gregor 150*56c64013SDouglas Gregor Module *Result = new Module(ModuleName, SourceLocation()); 151*56c64013SDouglas Gregor Result->UmbrellaHeader = UmbrellaHeader; 152*56c64013SDouglas Gregor Headers[UmbrellaHeader] = Result; 153*56c64013SDouglas Gregor UmbrellaDirs[FrameworkDir] = Result; 154*56c64013SDouglas Gregor Modules[ModuleName] = Result; 155*56c64013SDouglas Gregor return Result; 156*56c64013SDouglas Gregor } 157*56c64013SDouglas Gregor 158718292f2SDouglas Gregor static void indent(llvm::raw_ostream &OS, unsigned Spaces) { 159718292f2SDouglas Gregor OS << std::string(' ', Spaces); 160718292f2SDouglas Gregor } 161718292f2SDouglas Gregor 162718292f2SDouglas Gregor static void dumpModule(llvm::raw_ostream &OS, ModuleMap::Module *M, 163718292f2SDouglas Gregor unsigned Indent) { 164718292f2SDouglas Gregor indent(OS, Indent); 165718292f2SDouglas Gregor if (M->IsExplicit) 166718292f2SDouglas Gregor OS << "explicit "; 167718292f2SDouglas Gregor OS << M->Name << " {\n"; 168718292f2SDouglas Gregor 169718292f2SDouglas Gregor if (M->UmbrellaHeader) { 170718292f2SDouglas Gregor indent(OS, Indent + 2); 171718292f2SDouglas Gregor OS << "umbrella \"" << M->UmbrellaHeader->getName() << "\"\n"; 172718292f2SDouglas Gregor } 173718292f2SDouglas Gregor 174718292f2SDouglas Gregor for (unsigned I = 0, N = M->Headers.size(); I != N; ++I) { 175718292f2SDouglas Gregor indent(OS, Indent + 2); 176718292f2SDouglas Gregor OS << "header \"" << M->Headers[I]->getName() << "\"\n"; 177718292f2SDouglas Gregor } 178718292f2SDouglas Gregor 179718292f2SDouglas Gregor for (llvm::StringMap<ModuleMap::Module *>::iterator 180718292f2SDouglas Gregor MI = M->SubModules.begin(), 181718292f2SDouglas Gregor MIEnd = M->SubModules.end(); 182718292f2SDouglas Gregor MI != MIEnd; ++MI) 183718292f2SDouglas Gregor dumpModule(llvm::errs(), MI->getValue(), Indent + 2); 184718292f2SDouglas Gregor 185718292f2SDouglas Gregor indent(OS, Indent); 186718292f2SDouglas Gregor OS << "}\n"; 187718292f2SDouglas Gregor } 188718292f2SDouglas Gregor 189718292f2SDouglas Gregor void ModuleMap::dump() { 190718292f2SDouglas Gregor llvm::errs() << "Modules:"; 191718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 192718292f2SDouglas Gregor MEnd = Modules.end(); 193718292f2SDouglas Gregor M != MEnd; ++M) 194718292f2SDouglas Gregor dumpModule(llvm::errs(), M->getValue(), 2); 195718292f2SDouglas Gregor 196718292f2SDouglas Gregor llvm::errs() << "Headers:"; 197718292f2SDouglas Gregor for (llvm::DenseMap<const FileEntry *, Module *>::iterator 198718292f2SDouglas Gregor H = Headers.begin(), 199718292f2SDouglas Gregor HEnd = Headers.end(); 200718292f2SDouglas Gregor H != HEnd; ++H) { 201718292f2SDouglas Gregor llvm::errs() << " \"" << H->first->getName() << "\" -> " 202718292f2SDouglas Gregor << H->second->getFullModuleName() << "\n"; 203718292f2SDouglas Gregor } 204718292f2SDouglas Gregor } 205718292f2SDouglas Gregor 206718292f2SDouglas Gregor //----------------------------------------------------------------------------// 207718292f2SDouglas Gregor // Module map file parser 208718292f2SDouglas Gregor //----------------------------------------------------------------------------// 209718292f2SDouglas Gregor 210718292f2SDouglas Gregor namespace clang { 211718292f2SDouglas Gregor /// \brief A token in a module map file. 212718292f2SDouglas Gregor struct MMToken { 213718292f2SDouglas Gregor enum TokenKind { 214718292f2SDouglas Gregor EndOfFile, 215718292f2SDouglas Gregor HeaderKeyword, 216718292f2SDouglas Gregor Identifier, 217718292f2SDouglas Gregor ExplicitKeyword, 218718292f2SDouglas Gregor ModuleKeyword, 219718292f2SDouglas Gregor UmbrellaKeyword, 220718292f2SDouglas Gregor StringLiteral, 221718292f2SDouglas Gregor LBrace, 222718292f2SDouglas Gregor RBrace 223718292f2SDouglas Gregor } Kind; 224718292f2SDouglas Gregor 225718292f2SDouglas Gregor unsigned Location; 226718292f2SDouglas Gregor unsigned StringLength; 227718292f2SDouglas Gregor const char *StringData; 228718292f2SDouglas Gregor 229718292f2SDouglas Gregor void clear() { 230718292f2SDouglas Gregor Kind = EndOfFile; 231718292f2SDouglas Gregor Location = 0; 232718292f2SDouglas Gregor StringLength = 0; 233718292f2SDouglas Gregor StringData = 0; 234718292f2SDouglas Gregor } 235718292f2SDouglas Gregor 236718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 237718292f2SDouglas Gregor 238718292f2SDouglas Gregor SourceLocation getLocation() const { 239718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 240718292f2SDouglas Gregor } 241718292f2SDouglas Gregor 242718292f2SDouglas Gregor StringRef getString() const { 243718292f2SDouglas Gregor return StringRef(StringData, StringLength); 244718292f2SDouglas Gregor } 245718292f2SDouglas Gregor }; 246718292f2SDouglas Gregor 247718292f2SDouglas Gregor class ModuleMapParser { 248718292f2SDouglas Gregor Lexer &L; 249718292f2SDouglas Gregor SourceManager &SourceMgr; 250718292f2SDouglas Gregor DiagnosticsEngine &Diags; 251718292f2SDouglas Gregor ModuleMap ⤅ 252718292f2SDouglas Gregor 2535257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 2545257fc63SDouglas Gregor const DirectoryEntry *Directory; 2555257fc63SDouglas Gregor 256718292f2SDouglas Gregor /// \brief Whether an error occurred. 257718292f2SDouglas Gregor bool HadError; 258718292f2SDouglas Gregor 259718292f2SDouglas Gregor /// \brief Default target information, used only for string literal 260718292f2SDouglas Gregor /// parsing. 261718292f2SDouglas Gregor TargetInfo *Target; 262718292f2SDouglas Gregor 263718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 264718292f2SDouglas Gregor /// during parsing. 265718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 266718292f2SDouglas Gregor 267718292f2SDouglas Gregor /// \brief The current token. 268718292f2SDouglas Gregor MMToken Tok; 269718292f2SDouglas Gregor 270718292f2SDouglas Gregor /// \brief The active module. 271718292f2SDouglas Gregor ModuleMap::Module *ActiveModule; 272718292f2SDouglas Gregor 273718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 274718292f2SDouglas Gregor SourceLocation consumeToken(); 275718292f2SDouglas Gregor 276718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 277718292f2SDouglas Gregor /// (or the end of the file). 278718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 279718292f2SDouglas Gregor 280718292f2SDouglas Gregor void parseModuleDecl(); 281718292f2SDouglas Gregor void parseUmbrellaDecl(); 282718292f2SDouglas Gregor void parseHeaderDecl(); 283718292f2SDouglas Gregor 284718292f2SDouglas Gregor public: 285718292f2SDouglas Gregor typedef ModuleMap::Module Module; 286718292f2SDouglas Gregor 287718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 288718292f2SDouglas Gregor DiagnosticsEngine &Diags, 2895257fc63SDouglas Gregor ModuleMap &Map, 2905257fc63SDouglas Gregor const DirectoryEntry *Directory) 2915257fc63SDouglas Gregor : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map), 2925257fc63SDouglas Gregor Directory(Directory), HadError(false), ActiveModule(0) 293718292f2SDouglas Gregor { 294718292f2SDouglas Gregor TargetOptions TargetOpts; 295718292f2SDouglas Gregor TargetOpts.Triple = llvm::sys::getDefaultTargetTriple(); 296718292f2SDouglas Gregor Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); 297718292f2SDouglas Gregor 298718292f2SDouglas Gregor Tok.clear(); 299718292f2SDouglas Gregor consumeToken(); 300718292f2SDouglas Gregor } 301718292f2SDouglas Gregor 302718292f2SDouglas Gregor bool parseModuleMapFile(); 303718292f2SDouglas Gregor }; 304718292f2SDouglas Gregor } 305718292f2SDouglas Gregor 306718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 307718292f2SDouglas Gregor retry: 308718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 309718292f2SDouglas Gregor Tok.clear(); 310718292f2SDouglas Gregor 311718292f2SDouglas Gregor Token LToken; 312718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 313718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 314718292f2SDouglas Gregor switch (LToken.getKind()) { 315718292f2SDouglas Gregor case tok::raw_identifier: 316718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 317718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 318718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 319718292f2SDouglas Gregor .Case("header", MMToken::HeaderKeyword) 320718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 321718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 322718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 323718292f2SDouglas Gregor .Default(MMToken::Identifier); 324718292f2SDouglas Gregor break; 325718292f2SDouglas Gregor 326718292f2SDouglas Gregor case tok::eof: 327718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 328718292f2SDouglas Gregor break; 329718292f2SDouglas Gregor 330718292f2SDouglas Gregor case tok::l_brace: 331718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 332718292f2SDouglas Gregor break; 333718292f2SDouglas Gregor 334718292f2SDouglas Gregor case tok::r_brace: 335718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 336718292f2SDouglas Gregor break; 337718292f2SDouglas Gregor 338718292f2SDouglas Gregor case tok::string_literal: { 339718292f2SDouglas Gregor // Parse the string literal. 340718292f2SDouglas Gregor LangOptions LangOpts; 341718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 342718292f2SDouglas Gregor if (StringLiteral.hadError) 343718292f2SDouglas Gregor goto retry; 344718292f2SDouglas Gregor 345718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 346718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 347718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 348718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 349718292f2SDouglas Gregor Saved[Length] = 0; 350718292f2SDouglas Gregor 351718292f2SDouglas Gregor // Form the token. 352718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 353718292f2SDouglas Gregor Tok.StringData = Saved; 354718292f2SDouglas Gregor Tok.StringLength = Length; 355718292f2SDouglas Gregor break; 356718292f2SDouglas Gregor } 357718292f2SDouglas Gregor 358718292f2SDouglas Gregor case tok::comment: 359718292f2SDouglas Gregor goto retry; 360718292f2SDouglas Gregor 361718292f2SDouglas Gregor default: 362718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 363718292f2SDouglas Gregor HadError = true; 364718292f2SDouglas Gregor goto retry; 365718292f2SDouglas Gregor } 366718292f2SDouglas Gregor 367718292f2SDouglas Gregor return Result; 368718292f2SDouglas Gregor } 369718292f2SDouglas Gregor 370718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 371718292f2SDouglas Gregor unsigned braceDepth = 0; 372718292f2SDouglas Gregor do { 373718292f2SDouglas Gregor switch (Tok.Kind) { 374718292f2SDouglas Gregor case MMToken::EndOfFile: 375718292f2SDouglas Gregor return; 376718292f2SDouglas Gregor 377718292f2SDouglas Gregor case MMToken::LBrace: 378718292f2SDouglas Gregor if (Tok.is(K) && braceDepth == 0) 379718292f2SDouglas Gregor return; 380718292f2SDouglas Gregor 381718292f2SDouglas Gregor ++braceDepth; 382718292f2SDouglas Gregor break; 383718292f2SDouglas Gregor 384718292f2SDouglas Gregor case MMToken::RBrace: 385718292f2SDouglas Gregor if (braceDepth > 0) 386718292f2SDouglas Gregor --braceDepth; 387718292f2SDouglas Gregor else if (Tok.is(K)) 388718292f2SDouglas Gregor return; 389718292f2SDouglas Gregor break; 390718292f2SDouglas Gregor 391718292f2SDouglas Gregor default: 392718292f2SDouglas Gregor if (braceDepth == 0 && Tok.is(K)) 393718292f2SDouglas Gregor return; 394718292f2SDouglas Gregor break; 395718292f2SDouglas Gregor } 396718292f2SDouglas Gregor 397718292f2SDouglas Gregor consumeToken(); 398718292f2SDouglas Gregor } while (true); 399718292f2SDouglas Gregor } 400718292f2SDouglas Gregor 401718292f2SDouglas Gregor /// \brief Parse a module declaration. 402718292f2SDouglas Gregor /// 403718292f2SDouglas Gregor /// module-declaration: 404718292f2SDouglas Gregor /// 'module' identifier { module-member* } 405718292f2SDouglas Gregor /// 406718292f2SDouglas Gregor /// module-member: 407718292f2SDouglas Gregor /// umbrella-declaration 408718292f2SDouglas Gregor /// header-declaration 409718292f2SDouglas Gregor /// 'explicit'[opt] module-declaration 410718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 411718292f2SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword)); 412718292f2SDouglas Gregor 413718292f2SDouglas Gregor // Parse 'explicit' keyword, if present. 414718292f2SDouglas Gregor bool Explicit = false; 415718292f2SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 416718292f2SDouglas Gregor consumeToken(); 417718292f2SDouglas Gregor Explicit = true; 418718292f2SDouglas Gregor } 419718292f2SDouglas Gregor 420718292f2SDouglas Gregor // Parse 'module' keyword. 421718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 422718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), 423718292f2SDouglas Gregor diag::err_mmap_expected_module_after_explicit); 424718292f2SDouglas Gregor consumeToken(); 425718292f2SDouglas Gregor HadError = true; 426718292f2SDouglas Gregor return; 427718292f2SDouglas Gregor } 428718292f2SDouglas Gregor consumeToken(); // 'module' keyword 429718292f2SDouglas Gregor 430718292f2SDouglas Gregor // Parse the module name. 431718292f2SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 432718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 433718292f2SDouglas Gregor HadError = true; 434718292f2SDouglas Gregor return; 435718292f2SDouglas Gregor } 436718292f2SDouglas Gregor StringRef ModuleName = Tok.getString(); 437718292f2SDouglas Gregor SourceLocation ModuleNameLoc = consumeToken(); 438718292f2SDouglas Gregor 439718292f2SDouglas Gregor // Parse the opening brace. 440718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 441718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 442718292f2SDouglas Gregor << ModuleName; 443718292f2SDouglas Gregor HadError = true; 444718292f2SDouglas Gregor return; 445718292f2SDouglas Gregor } 446718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 447718292f2SDouglas Gregor 448718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 449718292f2SDouglas Gregor llvm::StringMap<Module *> &ModuleSpace 450718292f2SDouglas Gregor = ActiveModule? ActiveModule->SubModules : Map.Modules; 451718292f2SDouglas Gregor llvm::StringMap<Module *>::iterator ExistingModule 452718292f2SDouglas Gregor = ModuleSpace.find(ModuleName); 453718292f2SDouglas Gregor if (ExistingModule != ModuleSpace.end()) { 454718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 455718292f2SDouglas Gregor << ModuleName; 456718292f2SDouglas Gregor Diags.Report(ExistingModule->getValue()->DefinitionLoc, 457718292f2SDouglas Gregor diag::note_mmap_prev_definition); 458718292f2SDouglas Gregor 459718292f2SDouglas Gregor // Skip the module definition. 460718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 461718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 462718292f2SDouglas Gregor consumeToken(); 463718292f2SDouglas Gregor 464718292f2SDouglas Gregor HadError = true; 465718292f2SDouglas Gregor return; 466718292f2SDouglas Gregor } 467718292f2SDouglas Gregor 468718292f2SDouglas Gregor // Start defining this module. 469718292f2SDouglas Gregor ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Explicit); 470718292f2SDouglas Gregor ModuleSpace[ModuleName] = ActiveModule; 471718292f2SDouglas Gregor 472718292f2SDouglas Gregor bool Done = false; 473718292f2SDouglas Gregor do { 474718292f2SDouglas Gregor switch (Tok.Kind) { 475718292f2SDouglas Gregor case MMToken::EndOfFile: 476718292f2SDouglas Gregor case MMToken::RBrace: 477718292f2SDouglas Gregor Done = true; 478718292f2SDouglas Gregor break; 479718292f2SDouglas Gregor 480718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 481718292f2SDouglas Gregor case MMToken::ModuleKeyword: 482718292f2SDouglas Gregor parseModuleDecl(); 483718292f2SDouglas Gregor break; 484718292f2SDouglas Gregor 485718292f2SDouglas Gregor case MMToken::HeaderKeyword: 486718292f2SDouglas Gregor parseHeaderDecl(); 487718292f2SDouglas Gregor break; 488718292f2SDouglas Gregor 489718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 490718292f2SDouglas Gregor parseUmbrellaDecl(); 491718292f2SDouglas Gregor break; 492718292f2SDouglas Gregor 493718292f2SDouglas Gregor default: 494718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 495718292f2SDouglas Gregor consumeToken(); 496718292f2SDouglas Gregor break; 497718292f2SDouglas Gregor } 498718292f2SDouglas Gregor } while (!Done); 499718292f2SDouglas Gregor 500718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 501718292f2SDouglas Gregor consumeToken(); 502718292f2SDouglas Gregor else { 503718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 504718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 505718292f2SDouglas Gregor HadError = true; 506718292f2SDouglas Gregor } 507718292f2SDouglas Gregor 508718292f2SDouglas Gregor // We're done parsing this module. Pop back to our parent scope. 509718292f2SDouglas Gregor ActiveModule = ActiveModule->Parent; 510718292f2SDouglas Gregor } 511718292f2SDouglas Gregor 512718292f2SDouglas Gregor /// \brief Parse an umbrella header declaration. 513718292f2SDouglas Gregor /// 514718292f2SDouglas Gregor /// umbrella-declaration: 515718292f2SDouglas Gregor /// 'umbrella' string-literal 516718292f2SDouglas Gregor void ModuleMapParser::parseUmbrellaDecl() { 517718292f2SDouglas Gregor assert(Tok.is(MMToken::UmbrellaKeyword)); 518718292f2SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 519718292f2SDouglas Gregor 520718292f2SDouglas Gregor // Parse the header name. 521718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 522718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 523718292f2SDouglas Gregor << "umbrella"; 524718292f2SDouglas Gregor HadError = true; 525718292f2SDouglas Gregor return; 526718292f2SDouglas Gregor } 527718292f2SDouglas Gregor StringRef FileName = Tok.getString(); 528718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 529718292f2SDouglas Gregor 5305257fc63SDouglas Gregor // Check whether we already have an umbrella header. 5315257fc63SDouglas Gregor if (ActiveModule->UmbrellaHeader) { 5325257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_umbrella_header_conflict) 5335257fc63SDouglas Gregor << ActiveModule->getFullModuleName() 5345257fc63SDouglas Gregor << ActiveModule->UmbrellaHeader->getName(); 5355257fc63SDouglas Gregor HadError = true; 5365257fc63SDouglas Gregor return; 5375257fc63SDouglas Gregor } 5385257fc63SDouglas Gregor 5395257fc63SDouglas Gregor // Only top-level modules can have umbrella headers. 5405257fc63SDouglas Gregor if (ActiveModule->Parent) { 5415257fc63SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_header_submodule) 5425257fc63SDouglas Gregor << ActiveModule->getFullModuleName(); 5435257fc63SDouglas Gregor HadError = true; 5445257fc63SDouglas Gregor return; 5455257fc63SDouglas Gregor } 5465257fc63SDouglas Gregor 5475257fc63SDouglas Gregor // Look for this file. 5485257fc63SDouglas Gregor llvm::SmallString<128> PathName; 5495257fc63SDouglas Gregor PathName += Directory->getName(); 5505257fc63SDouglas Gregor llvm::sys::path::append(PathName, FileName); 5515257fc63SDouglas Gregor 5525257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 5535257fc63SDouglas Gregor // Come up with a lazy way to do this. 5545257fc63SDouglas Gregor if (const FileEntry *File = SourceMgr.getFileManager().getFile(PathName)) { 5555257fc63SDouglas Gregor if (const Module *OwningModule = Map.Headers[File]) { 5565257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 5575257fc63SDouglas Gregor << FileName << OwningModule->getFullModuleName(); 5585257fc63SDouglas Gregor HadError = true; 559b65dbfffSDouglas Gregor } else if ((OwningModule = Map.UmbrellaDirs[Directory])) { 560b65dbfffSDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 561b65dbfffSDouglas Gregor << OwningModule->getFullModuleName(); 562b65dbfffSDouglas Gregor HadError = true; 5635257fc63SDouglas Gregor } else { 5645257fc63SDouglas Gregor // Record this umbrella header. 5655257fc63SDouglas Gregor ActiveModule->UmbrellaHeader = File; 5665257fc63SDouglas Gregor Map.Headers[File] = ActiveModule; 567b65dbfffSDouglas Gregor Map.UmbrellaDirs[Directory] = ActiveModule; 5685257fc63SDouglas Gregor } 5695257fc63SDouglas Gregor } else { 5705257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 5715257fc63SDouglas Gregor << true << FileName; 5725257fc63SDouglas Gregor HadError = true; 5735257fc63SDouglas Gregor } 574718292f2SDouglas Gregor } 575718292f2SDouglas Gregor 576718292f2SDouglas Gregor /// \brief Parse a header declaration. 577718292f2SDouglas Gregor /// 578718292f2SDouglas Gregor /// header-declaration: 579718292f2SDouglas Gregor /// 'header' string-literal 580718292f2SDouglas Gregor void ModuleMapParser::parseHeaderDecl() { 581718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 5821871ed3dSBenjamin Kramer consumeToken(); 583718292f2SDouglas Gregor 584718292f2SDouglas Gregor // Parse the header name. 585718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 586718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 587718292f2SDouglas Gregor << "header"; 588718292f2SDouglas Gregor HadError = true; 589718292f2SDouglas Gregor return; 590718292f2SDouglas Gregor } 591718292f2SDouglas Gregor StringRef FileName = Tok.getString(); 592718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 593718292f2SDouglas Gregor 5945257fc63SDouglas Gregor // Look for this file. 5955257fc63SDouglas Gregor llvm::SmallString<128> PathName; 5965257fc63SDouglas Gregor PathName += Directory->getName(); 5975257fc63SDouglas Gregor llvm::sys::path::append(PathName, FileName); 5985257fc63SDouglas Gregor 5995257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 6005257fc63SDouglas Gregor // Come up with a lazy way to do this. 6015257fc63SDouglas Gregor if (const FileEntry *File = SourceMgr.getFileManager().getFile(PathName)) { 6025257fc63SDouglas Gregor if (const Module *OwningModule = Map.Headers[File]) { 6035257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 6045257fc63SDouglas Gregor << FileName << OwningModule->getFullModuleName(); 6055257fc63SDouglas Gregor HadError = true; 6065257fc63SDouglas Gregor } else { 6075257fc63SDouglas Gregor // Record this file. 6085257fc63SDouglas Gregor ActiveModule->Headers.push_back(File); 6095257fc63SDouglas Gregor Map.Headers[File] = ActiveModule; 6105257fc63SDouglas Gregor } 6115257fc63SDouglas Gregor } else { 6125257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 6135257fc63SDouglas Gregor << false << FileName; 6145257fc63SDouglas Gregor HadError = true; 6155257fc63SDouglas Gregor } 616718292f2SDouglas Gregor } 617718292f2SDouglas Gregor 618718292f2SDouglas Gregor /// \brief Parse a module map file. 619718292f2SDouglas Gregor /// 620718292f2SDouglas Gregor /// module-map-file: 621718292f2SDouglas Gregor /// module-declaration* 622718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 623718292f2SDouglas Gregor do { 624718292f2SDouglas Gregor switch (Tok.Kind) { 625718292f2SDouglas Gregor case MMToken::EndOfFile: 626718292f2SDouglas Gregor return HadError; 627718292f2SDouglas Gregor 628718292f2SDouglas Gregor case MMToken::ModuleKeyword: 629718292f2SDouglas Gregor parseModuleDecl(); 630718292f2SDouglas Gregor break; 631718292f2SDouglas Gregor 632718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 633718292f2SDouglas Gregor case MMToken::HeaderKeyword: 634718292f2SDouglas Gregor case MMToken::Identifier: 635718292f2SDouglas Gregor case MMToken::LBrace: 636718292f2SDouglas Gregor case MMToken::RBrace: 637718292f2SDouglas Gregor case MMToken::StringLiteral: 638718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 639718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 640718292f2SDouglas Gregor HadError = true; 641718292f2SDouglas Gregor consumeToken(); 642718292f2SDouglas Gregor break; 643718292f2SDouglas Gregor } 644718292f2SDouglas Gregor } while (true); 645718292f2SDouglas Gregor 646718292f2SDouglas Gregor return HadError; 647718292f2SDouglas Gregor } 648718292f2SDouglas Gregor 649718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 650718292f2SDouglas Gregor FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 651718292f2SDouglas Gregor const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 652718292f2SDouglas Gregor if (!Buffer) 653718292f2SDouglas Gregor return true; 654718292f2SDouglas Gregor 655718292f2SDouglas Gregor // Parse this module map file. 656718292f2SDouglas Gregor Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts); 657718292f2SDouglas Gregor Diags->getClient()->BeginSourceFile(LangOpts); 6585257fc63SDouglas Gregor ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir()); 659718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 660718292f2SDouglas Gregor Diags->getClient()->EndSourceFile(); 661718292f2SDouglas Gregor 662718292f2SDouglas Gregor return Result; 663718292f2SDouglas Gregor } 664