ModuleMap.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the ModuleMap implementation, which describes the layout
  11. // of a module as it relates to headers.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Lex/ModuleMap.h"
  15. #include "clang/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/Host.h"
  24. #include "llvm/Support/PathV2.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include "llvm/ADT/StringRef.h"
  27. #include "llvm/ADT/StringSwitch.h"
  28. using namespace clang;
  29. Module::ExportDecl
  30. ModuleMap::resolveExport(Module *Mod,
  31. const Module::UnresolvedExportDecl &Unresolved,
  32. bool Complain) {
  33. // We may have just a wildcard.
  34. if (Unresolved.Id.empty()) {
  35. assert(Unresolved.Wildcard && "Invalid unresolved export");
  36. return Module::ExportDecl(0, true);
  37. }
  38. // Find the starting module.
  39. Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
  40. if (!Context) {
  41. if (Complain)
  42. Diags->Report(Unresolved.Id[0].second,
  43. diag::err_mmap_missing_module_unqualified)
  44. << Unresolved.Id[0].first << Mod->getFullModuleName();
  45. return Module::ExportDecl();
  46. }
  47. // Dig into the module path.
  48. for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
  49. Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
  50. Context);
  51. if (!Sub) {
  52. if (Complain)
  53. Diags->Report(Unresolved.Id[I].second,
  54. diag::err_mmap_missing_module_qualified)
  55. << Unresolved.Id[I].first << Context->getFullModuleName()
  56. << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
  57. return Module::ExportDecl();
  58. }
  59. Context = Sub;
  60. }
  61. return Module::ExportDecl(Context, Unresolved.Wildcard);
  62. }
  63. ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC) {
  64. llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
  65. Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>(
  66. new DiagnosticsEngine(DiagIDs));
  67. Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
  68. SourceMgr = new SourceManager(*Diags, FileMgr);
  69. }
  70. ModuleMap::~ModuleMap() {
  71. for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
  72. IEnd = Modules.end();
  73. I != IEnd; ++I) {
  74. delete I->getValue();
  75. }
  76. delete SourceMgr;
  77. }
  78. Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
  79. llvm::DenseMap<const FileEntry *, Module *>::iterator Known
  80. = Headers.find(File);
  81. if (Known != Headers.end())
  82. return Known->second;
  83. const DirectoryEntry *Dir = File->getDir();
  84. llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
  85. = UmbrellaDirs.find(Dir);
  86. if (KnownDir != UmbrellaDirs.end())
  87. return KnownDir->second;
  88. // Walk up the directory hierarchy looking for umbrella headers.
  89. llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
  90. StringRef DirName = Dir->getName();
  91. do {
  92. // Retrieve our parent path.
  93. DirName = llvm::sys::path::parent_path(DirName);
  94. if (DirName.empty())
  95. break;
  96. // Resolve the parent path to a directory entry.
  97. Dir = SourceMgr->getFileManager().getDirectory(DirName);
  98. if (!Dir)
  99. break;
  100. KnownDir = UmbrellaDirs.find(Dir);
  101. if (KnownDir != UmbrellaDirs.end()) {
  102. Module *Result = KnownDir->second;
  103. // Record each of the directories we stepped through as being part of
  104. // the module we found, since the umbrella header covers them all.
  105. for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
  106. UmbrellaDirs[SkippedDirs[I]] = Result;
  107. return Result;
  108. }
  109. SkippedDirs.push_back(Dir);
  110. } while (true);
  111. return 0;
  112. }
  113. Module *ModuleMap::findModule(StringRef Name) {
  114. llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
  115. if (Known != Modules.end())
  116. return Known->getValue();
  117. return 0;
  118. }
  119. Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
  120. for(; Context; Context = Context->Parent) {
  121. if (Module *Sub = lookupModuleQualified(Name, Context))
  122. return Sub;
  123. }
  124. return findModule(Name);
  125. }
  126. Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
  127. if (!Context)
  128. return findModule(Name);
  129. llvm::StringMap<Module *>::iterator Sub = Context->SubModules.find(Name);
  130. if (Sub != Context->SubModules.end())
  131. return Sub->getValue();
  132. return 0;
  133. }
  134. std::pair<Module *, bool>
  135. ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
  136. bool IsExplicit) {
  137. // Try to find an existing module with this name.
  138. if (Module *Found = Parent? Parent->SubModules[Name] : Modules[Name])
  139. return std::make_pair(Found, false);
  140. // Create a new module with this name.
  141. Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
  142. IsExplicit);
  143. if (Parent)
  144. Parent->SubModules[Name] = Result;
  145. else
  146. Modules[Name] = Result;
  147. return std::make_pair(Result, true);
  148. }
  149. Module *
  150. ModuleMap::inferFrameworkModule(StringRef ModuleName,
  151. const DirectoryEntry *FrameworkDir) {
  152. // Check whether we've already found this module.
  153. if (Module *Module = findModule(ModuleName))
  154. return Module;
  155. // Look for an umbrella header.
  156. llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
  157. llvm::sys::path::append(UmbrellaName, "Headers");
  158. llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
  159. const FileEntry *UmbrellaHeader
  160. = SourceMgr->getFileManager().getFile(UmbrellaName);
  161. // FIXME: If there's no umbrella header, we could probably scan the
  162. // framework to load *everything*. But, it's not clear that this is a good
  163. // idea.
  164. if (!UmbrellaHeader)
  165. return 0;
  166. Module *Result = new Module(ModuleName, SourceLocation(),
  167. /*IsFramework=*/true);
  168. Result->UmbrellaHeader = UmbrellaHeader;
  169. Headers[UmbrellaHeader] = Result;
  170. UmbrellaDirs[FrameworkDir] = Result;
  171. Modules[ModuleName] = Result;
  172. return Result;
  173. }
  174. const FileEntry *
  175. ModuleMap::getContainingModuleMapFile(Module *Module) {
  176. if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
  177. return 0;
  178. return SourceMgr->getFileEntryForID(
  179. SourceMgr->getFileID(Module->DefinitionLoc));
  180. }
  181. void ModuleMap::dump() {
  182. llvm::errs() << "Modules:";
  183. for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
  184. MEnd = Modules.end();
  185. M != MEnd; ++M)
  186. M->getValue()->print(llvm::errs(), 2);
  187. llvm::errs() << "Headers:";
  188. for (llvm::DenseMap<const FileEntry *, Module *>::iterator
  189. H = Headers.begin(),
  190. HEnd = Headers.end();
  191. H != HEnd; ++H) {
  192. llvm::errs() << " \"" << H->first->getName() << "\" -> "
  193. << H->second->getFullModuleName() << "\n";
  194. }
  195. }
  196. bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
  197. bool HadError = false;
  198. for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
  199. Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
  200. Complain);
  201. if (Export.getPointer() || Export.getInt())
  202. Mod->Exports.push_back(Export);
  203. else
  204. HadError = true;
  205. }
  206. Mod->UnresolvedExports.clear();
  207. return HadError;
  208. }
  209. Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
  210. if (Loc.isInvalid())
  211. return 0;
  212. // Use the expansion location to determine which module we're in.
  213. FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
  214. if (!ExpansionLoc.isFileID())
  215. return 0;
  216. const SourceManager &SrcMgr = Loc.getManager();
  217. FileID ExpansionFileID = ExpansionLoc.getFileID();
  218. const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID);
  219. if (!ExpansionFile)
  220. return 0;
  221. // Find the module that owns this header.
  222. return findModuleForHeader(ExpansionFile);
  223. }
  224. //----------------------------------------------------------------------------//
  225. // Module map file parser
  226. //----------------------------------------------------------------------------//
  227. namespace clang {
  228. /// \brief A token in a module map file.
  229. struct MMToken {
  230. enum TokenKind {
  231. EndOfFile,
  232. HeaderKeyword,
  233. Identifier,
  234. ExplicitKeyword,
  235. ExportKeyword,
  236. FrameworkKeyword,
  237. ModuleKeyword,
  238. Period,
  239. UmbrellaKeyword,
  240. Star,
  241. StringLiteral,
  242. LBrace,
  243. RBrace
  244. } Kind;
  245. unsigned Location;
  246. unsigned StringLength;
  247. const char *StringData;
  248. void clear() {
  249. Kind = EndOfFile;
  250. Location = 0;
  251. StringLength = 0;
  252. StringData = 0;
  253. }
  254. bool is(TokenKind K) const { return Kind == K; }
  255. SourceLocation getLocation() const {
  256. return SourceLocation::getFromRawEncoding(Location);
  257. }
  258. StringRef getString() const {
  259. return StringRef(StringData, StringLength);
  260. }
  261. };
  262. class ModuleMapParser {
  263. Lexer &L;
  264. SourceManager &SourceMgr;
  265. DiagnosticsEngine &Diags;
  266. ModuleMap &Map;
  267. /// \brief The directory that this module map resides in.
  268. const DirectoryEntry *Directory;
  269. /// \brief Whether an error occurred.
  270. bool HadError;
  271. /// \brief Default target information, used only for string literal
  272. /// parsing.
  273. TargetInfo *Target;
  274. /// \brief Stores string data for the various string literals referenced
  275. /// during parsing.
  276. llvm::BumpPtrAllocator StringData;
  277. /// \brief The current token.
  278. MMToken Tok;
  279. /// \brief The active module.
  280. Module *ActiveModule;
  281. /// \brief Consume the current token and return its location.
  282. SourceLocation consumeToken();
  283. /// \brief Skip tokens until we reach the a token with the given kind
  284. /// (or the end of the file).
  285. void skipUntil(MMToken::TokenKind K);
  286. void parseModuleDecl();
  287. void parseUmbrellaDecl();
  288. void parseHeaderDecl();
  289. void parseExportDecl();
  290. public:
  291. explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
  292. DiagnosticsEngine &Diags,
  293. ModuleMap &Map,
  294. const DirectoryEntry *Directory)
  295. : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
  296. Directory(Directory), HadError(false), ActiveModule(0)
  297. {
  298. TargetOptions TargetOpts;
  299. TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
  300. Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
  301. Tok.clear();
  302. consumeToken();
  303. }
  304. bool parseModuleMapFile();
  305. };
  306. }
  307. SourceLocation ModuleMapParser::consumeToken() {
  308. retry:
  309. SourceLocation Result = Tok.getLocation();
  310. Tok.clear();
  311. Token LToken;
  312. L.LexFromRawLexer(LToken);
  313. Tok.Location = LToken.getLocation().getRawEncoding();
  314. switch (LToken.getKind()) {
  315. case tok::raw_identifier:
  316. Tok.StringData = LToken.getRawIdentifierData();
  317. Tok.StringLength = LToken.getLength();
  318. Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
  319. .Case("header", MMToken::HeaderKeyword)
  320. .Case("explicit", MMToken::ExplicitKeyword)
  321. .Case("export", MMToken::ExportKeyword)
  322. .Case("framework", MMToken::FrameworkKeyword)
  323. .Case("module", MMToken::ModuleKeyword)
  324. .Case("umbrella", MMToken::UmbrellaKeyword)
  325. .Default(MMToken::Identifier);
  326. break;
  327. case tok::eof:
  328. Tok.Kind = MMToken::EndOfFile;
  329. break;
  330. case tok::l_brace:
  331. Tok.Kind = MMToken::LBrace;
  332. break;
  333. case tok::period:
  334. Tok.Kind = MMToken::Period;
  335. break;
  336. case tok::r_brace:
  337. Tok.Kind = MMToken::RBrace;
  338. break;
  339. case tok::star:
  340. Tok.Kind = MMToken::Star;
  341. break;
  342. case tok::string_literal: {
  343. // Parse the string literal.
  344. LangOptions LangOpts;
  345. StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
  346. if (StringLiteral.hadError)
  347. goto retry;
  348. // Copy the string literal into our string data allocator.
  349. unsigned Length = StringLiteral.GetStringLength();
  350. char *Saved = StringData.Allocate<char>(Length + 1);
  351. memcpy(Saved, StringLiteral.GetString().data(), Length);
  352. Saved[Length] = 0;
  353. // Form the token.
  354. Tok.Kind = MMToken::StringLiteral;
  355. Tok.StringData = Saved;
  356. Tok.StringLength = Length;
  357. break;
  358. }
  359. case tok::comment:
  360. goto retry;
  361. default:
  362. Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
  363. HadError = true;
  364. goto retry;
  365. }
  366. return Result;
  367. }
  368. void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
  369. unsigned braceDepth = 0;
  370. do {
  371. switch (Tok.Kind) {
  372. case MMToken::EndOfFile:
  373. return;
  374. case MMToken::LBrace:
  375. if (Tok.is(K) && braceDepth == 0)
  376. return;
  377. ++braceDepth;
  378. break;
  379. case MMToken::RBrace:
  380. if (braceDepth > 0)
  381. --braceDepth;
  382. else if (Tok.is(K))
  383. return;
  384. break;
  385. default:
  386. if (braceDepth == 0 && Tok.is(K))
  387. return;
  388. break;
  389. }
  390. consumeToken();
  391. } while (true);
  392. }
  393. /// \brief Parse a module declaration.
  394. ///
  395. /// module-declaration:
  396. /// 'framework'[opt] 'module' identifier { module-member* }
  397. ///
  398. /// module-member:
  399. /// umbrella-declaration
  400. /// header-declaration
  401. /// 'explicit'[opt] module-declaration
  402. /// export-declaration
  403. void ModuleMapParser::parseModuleDecl() {
  404. assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
  405. Tok.is(MMToken::FrameworkKeyword));
  406. // Parse 'framework' or 'explicit' keyword, if present.
  407. bool Framework = false;
  408. bool Explicit = false;
  409. if (Tok.is(MMToken::FrameworkKeyword)) {
  410. consumeToken();
  411. Framework = true;
  412. }
  413. // Parse 'explicit' keyword, if present.
  414. else if (Tok.is(MMToken::ExplicitKeyword)) {
  415. consumeToken();
  416. Explicit = true;
  417. }
  418. // Parse 'module' keyword.
  419. if (!Tok.is(MMToken::ModuleKeyword)) {
  420. Diags.Report(Tok.getLocation(),
  421. diag::err_mmap_expected_module_after_explicit);
  422. consumeToken();
  423. HadError = true;
  424. return;
  425. }
  426. consumeToken(); // 'module' keyword
  427. // Parse the module name.
  428. if (!Tok.is(MMToken::Identifier)) {
  429. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
  430. HadError = true;
  431. return;
  432. }
  433. StringRef ModuleName = Tok.getString();
  434. SourceLocation ModuleNameLoc = consumeToken();
  435. // Parse the opening brace.
  436. if (!Tok.is(MMToken::LBrace)) {
  437. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
  438. << ModuleName;
  439. HadError = true;
  440. return;
  441. }
  442. SourceLocation LBraceLoc = consumeToken();
  443. // Determine whether this (sub)module has already been defined.
  444. llvm::StringMap<Module *> &ModuleSpace
  445. = ActiveModule? ActiveModule->SubModules : Map.Modules;
  446. llvm::StringMap<Module *>::iterator ExistingModule
  447. = ModuleSpace.find(ModuleName);
  448. if (ExistingModule != ModuleSpace.end()) {
  449. Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
  450. << ModuleName;
  451. Diags.Report(ExistingModule->getValue()->DefinitionLoc,
  452. diag::note_mmap_prev_definition);
  453. // Skip the module definition.
  454. skipUntil(MMToken::RBrace);
  455. if (Tok.is(MMToken::RBrace))
  456. consumeToken();
  457. HadError = true;
  458. return;
  459. }
  460. // Start defining this module.
  461. ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Framework,
  462. Explicit);
  463. ModuleSpace[ModuleName] = ActiveModule;
  464. bool Done = false;
  465. do {
  466. switch (Tok.Kind) {
  467. case MMToken::EndOfFile:
  468. case MMToken::RBrace:
  469. Done = true;
  470. break;
  471. case MMToken::ExplicitKeyword:
  472. case MMToken::ModuleKeyword:
  473. parseModuleDecl();
  474. break;
  475. case MMToken::ExportKeyword:
  476. parseExportDecl();
  477. break;
  478. case MMToken::HeaderKeyword:
  479. parseHeaderDecl();
  480. break;
  481. case MMToken::UmbrellaKeyword:
  482. parseUmbrellaDecl();
  483. break;
  484. default:
  485. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
  486. consumeToken();
  487. break;
  488. }
  489. } while (!Done);
  490. if (Tok.is(MMToken::RBrace))
  491. consumeToken();
  492. else {
  493. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  494. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  495. HadError = true;
  496. }
  497. // We're done parsing this module. Pop back to our parent scope.
  498. ActiveModule = ActiveModule->Parent;
  499. }
  500. /// \brief Parse an umbrella header declaration.
  501. ///
  502. /// umbrella-declaration:
  503. /// 'umbrella' string-literal
  504. void ModuleMapParser::parseUmbrellaDecl() {
  505. assert(Tok.is(MMToken::UmbrellaKeyword));
  506. SourceLocation UmbrellaLoc = consumeToken();
  507. // Parse the header name.
  508. if (!Tok.is(MMToken::StringLiteral)) {
  509. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  510. << "umbrella";
  511. HadError = true;
  512. return;
  513. }
  514. StringRef FileName = Tok.getString();
  515. SourceLocation FileNameLoc = consumeToken();
  516. // Check whether we already have an umbrella header.
  517. if (ActiveModule->UmbrellaHeader) {
  518. Diags.Report(FileNameLoc, diag::err_mmap_umbrella_header_conflict)
  519. << ActiveModule->getFullModuleName()
  520. << ActiveModule->UmbrellaHeader->getName();
  521. HadError = true;
  522. return;
  523. }
  524. // Only top-level modules can have umbrella headers.
  525. if (ActiveModule->Parent) {
  526. Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_header_submodule)
  527. << ActiveModule->getFullModuleName();
  528. HadError = true;
  529. return;
  530. }
  531. // Look for this file.
  532. llvm::SmallString<128> PathName;
  533. const FileEntry *File = 0;
  534. if (llvm::sys::path::is_absolute(FileName)) {
  535. PathName = FileName;
  536. File = SourceMgr.getFileManager().getFile(PathName);
  537. } else {
  538. // Search for the header file within the search directory.
  539. PathName += Directory->getName();
  540. unsigned PathLength = PathName.size();
  541. if (ActiveModule->isPartOfFramework()) {
  542. // Check whether this file is in the public headers.
  543. llvm::sys::path::append(PathName, "Headers");
  544. llvm::sys::path::append(PathName, FileName);
  545. File = SourceMgr.getFileManager().getFile(PathName);
  546. if (!File) {
  547. // Check whether this file is in the private headers.
  548. PathName.resize(PathLength);
  549. llvm::sys::path::append(PathName, "PrivateHeaders");
  550. llvm::sys::path::append(PathName, FileName);
  551. File = SourceMgr.getFileManager().getFile(PathName);
  552. }
  553. // FIXME: Deal with subframeworks.
  554. } else {
  555. // Lookup for normal headers.
  556. llvm::sys::path::append(PathName, FileName);
  557. File = SourceMgr.getFileManager().getFile(PathName);
  558. }
  559. }
  560. // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
  561. // Come up with a lazy way to do this.
  562. if (File) {
  563. if (const Module *OwningModule = Map.Headers[File]) {
  564. Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
  565. << FileName << OwningModule->getFullModuleName();
  566. HadError = true;
  567. } else if ((OwningModule = Map.UmbrellaDirs[Directory])) {
  568. Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
  569. << OwningModule->getFullModuleName();
  570. HadError = true;
  571. } else {
  572. // Record this umbrella header.
  573. ActiveModule->UmbrellaHeader = File;
  574. Map.Headers[File] = ActiveModule;
  575. Map.UmbrellaDirs[Directory] = ActiveModule;
  576. }
  577. } else {
  578. Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
  579. << true << FileName;
  580. HadError = true;
  581. }
  582. }
  583. /// \brief Parse a header declaration.
  584. ///
  585. /// header-declaration:
  586. /// 'header' string-literal
  587. void ModuleMapParser::parseHeaderDecl() {
  588. assert(Tok.is(MMToken::HeaderKeyword));
  589. consumeToken();
  590. // Parse the header name.
  591. if (!Tok.is(MMToken::StringLiteral)) {
  592. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  593. << "header";
  594. HadError = true;
  595. return;
  596. }
  597. StringRef FileName = Tok.getString();
  598. SourceLocation FileNameLoc = consumeToken();
  599. // Look for this file.
  600. llvm::SmallString<128> PathName;
  601. if (llvm::sys::path::is_relative(FileName)) {
  602. // FIXME: Change this search to also look for private headers!
  603. PathName += Directory->getName();
  604. if (ActiveModule->isPartOfFramework())
  605. llvm::sys::path::append(PathName, "Headers");
  606. }
  607. llvm::sys::path::append(PathName, FileName);
  608. // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
  609. // Come up with a lazy way to do this.
  610. if (const FileEntry *File = SourceMgr.getFileManager().getFile(PathName)) {
  611. if (const Module *OwningModule = Map.Headers[File]) {
  612. Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
  613. << FileName << OwningModule->getFullModuleName();
  614. HadError = true;
  615. } else {
  616. // Record this file.
  617. ActiveModule->Headers.push_back(File);
  618. Map.Headers[File] = ActiveModule;
  619. }
  620. } else {
  621. Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
  622. << false << FileName;
  623. HadError = true;
  624. }
  625. }
  626. /// \brief Parse a module export declaration.
  627. ///
  628. /// export-declaration:
  629. /// 'export' wildcard-module-id
  630. ///
  631. /// wildcard-module-id:
  632. /// identifier
  633. /// '*'
  634. /// identifier '.' wildcard-module-id
  635. void ModuleMapParser::parseExportDecl() {
  636. assert(Tok.is(MMToken::ExportKeyword));
  637. SourceLocation ExportLoc = consumeToken();
  638. // Parse the module-id with an optional wildcard at the end.
  639. ModuleId ParsedModuleId;
  640. bool Wildcard = false;
  641. do {
  642. if (Tok.is(MMToken::Identifier)) {
  643. ParsedModuleId.push_back(std::make_pair(Tok.getString(),
  644. Tok.getLocation()));
  645. consumeToken();
  646. if (Tok.is(MMToken::Period)) {
  647. consumeToken();
  648. continue;
  649. }
  650. break;
  651. }
  652. if(Tok.is(MMToken::Star)) {
  653. Wildcard = true;
  654. consumeToken();
  655. break;
  656. }
  657. Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
  658. HadError = true;
  659. return;
  660. } while (true);
  661. Module::UnresolvedExportDecl Unresolved = {
  662. ExportLoc, ParsedModuleId, Wildcard
  663. };
  664. ActiveModule->UnresolvedExports.push_back(Unresolved);
  665. }
  666. /// \brief Parse a module map file.
  667. ///
  668. /// module-map-file:
  669. /// module-declaration*
  670. bool ModuleMapParser::parseModuleMapFile() {
  671. do {
  672. switch (Tok.Kind) {
  673. case MMToken::EndOfFile:
  674. return HadError;
  675. case MMToken::ModuleKeyword:
  676. case MMToken::FrameworkKeyword:
  677. parseModuleDecl();
  678. break;
  679. case MMToken::ExplicitKeyword:
  680. case MMToken::ExportKeyword:
  681. case MMToken::HeaderKeyword:
  682. case MMToken::Identifier:
  683. case MMToken::LBrace:
  684. case MMToken::Period:
  685. case MMToken::RBrace:
  686. case MMToken::Star:
  687. case MMToken::StringLiteral:
  688. case MMToken::UmbrellaKeyword:
  689. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
  690. HadError = true;
  691. consumeToken();
  692. break;
  693. }
  694. } while (true);
  695. return HadError;
  696. }
  697. bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
  698. FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
  699. const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
  700. if (!Buffer)
  701. return true;
  702. // Parse this module map file.
  703. Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts);
  704. Diags->getClient()->BeginSourceFile(LangOpts);
  705. ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir());
  706. bool Result = Parser.parseModuleMapFile();
  707. Diags->getClient()->EndSourceFile();
  708. return Result;
  709. }