ModuleMap.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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/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;
  30. Module::ExportDecl
  31. ModuleMap::resolveExport(Module *Mod,
  32. const Module::UnresolvedExportDecl &Unresolved,
  33. bool Complain) {
  34. // We may have just a wildcard.
  35. if (Unresolved.Id.empty()) {
  36. assert(Unresolved.Wildcard && "Invalid unresolved export");
  37. return Module::ExportDecl(0, true);
  38. }
  39. // Find the starting module.
  40. Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
  41. if (!Context) {
  42. if (Complain)
  43. Diags->Report(Unresolved.Id[0].second,
  44. diag::err_mmap_missing_module_unqualified)
  45. << Unresolved.Id[0].first << Mod->getFullModuleName();
  46. return Module::ExportDecl();
  47. }
  48. // Dig into the module path.
  49. for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
  50. Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
  51. Context);
  52. if (!Sub) {
  53. if (Complain)
  54. Diags->Report(Unresolved.Id[I].second,
  55. diag::err_mmap_missing_module_qualified)
  56. << Unresolved.Id[I].first << Context->getFullModuleName()
  57. << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
  58. return Module::ExportDecl();
  59. }
  60. Context = Sub;
  61. }
  62. return Module::ExportDecl(Context, Unresolved.Wildcard);
  63. }
  64. ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
  65. const LangOptions &LangOpts, const TargetInfo *Target)
  66. : LangOpts(LangOpts), Target(Target), BuiltinIncludeDir(0)
  67. {
  68. llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
  69. Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>(
  70. new DiagnosticsEngine(DiagIDs));
  71. Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
  72. SourceMgr = new SourceManager(*Diags, FileMgr);
  73. }
  74. ModuleMap::~ModuleMap() {
  75. for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
  76. IEnd = Modules.end();
  77. I != IEnd; ++I) {
  78. delete I->getValue();
  79. }
  80. delete SourceMgr;
  81. }
  82. void ModuleMap::setTarget(const TargetInfo &Target) {
  83. assert((!this->Target || this->Target == &Target) &&
  84. "Improper target override");
  85. this->Target = &Target;
  86. }
  87. Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
  88. llvm::DenseMap<const FileEntry *, Module *>::iterator Known
  89. = Headers.find(File);
  90. if (Known != Headers.end()) {
  91. // If a header corresponds to an unavailable module, don't report
  92. // that it maps to anything.
  93. if (!Known->second->isAvailable())
  94. return 0;
  95. return Known->second;
  96. }
  97. const DirectoryEntry *Dir = File->getDir();
  98. llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
  99. StringRef DirName = Dir->getName();
  100. // Keep walking up the directory hierarchy, looking for a directory with
  101. // an umbrella header.
  102. do {
  103. llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
  104. = UmbrellaDirs.find(Dir);
  105. if (KnownDir != UmbrellaDirs.end()) {
  106. Module *Result = KnownDir->second;
  107. // Search up the module stack until we find a module with an umbrella
  108. // directory.
  109. Module *UmbrellaModule = Result;
  110. while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
  111. UmbrellaModule = UmbrellaModule->Parent;
  112. if (UmbrellaModule->InferSubmodules) {
  113. // Infer submodules for each of the directories we found between
  114. // the directory of the umbrella header and the directory where
  115. // the actual header is located.
  116. bool Explicit = UmbrellaModule->InferExplicitSubmodules;
  117. for (unsigned I = SkippedDirs.size(); I != 0; --I) {
  118. // Find or create the module that corresponds to this directory name.
  119. StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
  120. Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
  121. Explicit).first;
  122. // Associate the module and the directory.
  123. UmbrellaDirs[SkippedDirs[I-1]] = Result;
  124. // If inferred submodules export everything they import, add a
  125. // wildcard to the set of exports.
  126. if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
  127. Result->Exports.push_back(Module::ExportDecl(0, true));
  128. }
  129. // Infer a submodule with the same name as this header file.
  130. StringRef Name = llvm::sys::path::stem(File->getName());
  131. Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
  132. Explicit).first;
  133. // If inferred submodules export everything they import, add a
  134. // wildcard to the set of exports.
  135. if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
  136. Result->Exports.push_back(Module::ExportDecl(0, true));
  137. } else {
  138. // Record each of the directories we stepped through as being part of
  139. // the module we found, since the umbrella header covers them all.
  140. for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
  141. UmbrellaDirs[SkippedDirs[I]] = Result;
  142. }
  143. Headers[File] = Result;
  144. // If a header corresponds to an unavailable module, don't report
  145. // that it maps to anything.
  146. if (!Result->isAvailable())
  147. return 0;
  148. return Result;
  149. }
  150. SkippedDirs.push_back(Dir);
  151. // Retrieve our parent path.
  152. DirName = llvm::sys::path::parent_path(DirName);
  153. if (DirName.empty())
  154. break;
  155. // Resolve the parent path to a directory entry.
  156. Dir = SourceMgr->getFileManager().getDirectory(DirName);
  157. } while (Dir);
  158. return 0;
  159. }
  160. bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) {
  161. llvm::DenseMap<const FileEntry *, Module *>::iterator Known
  162. = Headers.find(Header);
  163. if (Known != Headers.end())
  164. return !Known->second->isAvailable();
  165. const DirectoryEntry *Dir = Header->getDir();
  166. llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
  167. StringRef DirName = Dir->getName();
  168. // Keep walking up the directory hierarchy, looking for a directory with
  169. // an umbrella header.
  170. do {
  171. llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
  172. = UmbrellaDirs.find(Dir);
  173. if (KnownDir != UmbrellaDirs.end()) {
  174. Module *Found = KnownDir->second;
  175. if (!Found->isAvailable())
  176. return true;
  177. // Search up the module stack until we find a module with an umbrella
  178. // directory.
  179. Module *UmbrellaModule = Found;
  180. while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
  181. UmbrellaModule = UmbrellaModule->Parent;
  182. if (UmbrellaModule->InferSubmodules) {
  183. for (unsigned I = SkippedDirs.size(); I != 0; --I) {
  184. // Find or create the module that corresponds to this directory name.
  185. StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
  186. Found = lookupModuleQualified(Name, Found);
  187. if (!Found)
  188. return false;
  189. if (!Found->isAvailable())
  190. return true;
  191. }
  192. // Infer a submodule with the same name as this header file.
  193. StringRef Name = llvm::sys::path::stem(Header->getName());
  194. Found = lookupModuleQualified(Name, Found);
  195. if (!Found)
  196. return false;
  197. }
  198. return !Found->isAvailable();
  199. }
  200. SkippedDirs.push_back(Dir);
  201. // Retrieve our parent path.
  202. DirName = llvm::sys::path::parent_path(DirName);
  203. if (DirName.empty())
  204. break;
  205. // Resolve the parent path to a directory entry.
  206. Dir = SourceMgr->getFileManager().getDirectory(DirName);
  207. } while (Dir);
  208. return false;
  209. }
  210. Module *ModuleMap::findModule(StringRef Name) {
  211. llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
  212. if (Known != Modules.end())
  213. return Known->getValue();
  214. return 0;
  215. }
  216. Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
  217. for(; Context; Context = Context->Parent) {
  218. if (Module *Sub = lookupModuleQualified(Name, Context))
  219. return Sub;
  220. }
  221. return findModule(Name);
  222. }
  223. Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
  224. if (!Context)
  225. return findModule(Name);
  226. return Context->findSubmodule(Name);
  227. }
  228. std::pair<Module *, bool>
  229. ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
  230. bool IsExplicit) {
  231. // Try to find an existing module with this name.
  232. if (Module *Sub = lookupModuleQualified(Name, Parent))
  233. return std::make_pair(Sub, false);
  234. // Create a new module with this name.
  235. Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
  236. IsExplicit);
  237. if (!Parent)
  238. Modules[Name] = Result;
  239. return std::make_pair(Result, true);
  240. }
  241. Module *
  242. ModuleMap::inferFrameworkModule(StringRef ModuleName,
  243. const DirectoryEntry *FrameworkDir,
  244. bool IsSystem,
  245. Module *Parent) {
  246. // Check whether we've already found this module.
  247. if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
  248. return Mod;
  249. FileManager &FileMgr = SourceMgr->getFileManager();
  250. // Look for an umbrella header.
  251. SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
  252. llvm::sys::path::append(UmbrellaName, "Headers");
  253. llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
  254. const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
  255. // FIXME: If there's no umbrella header, we could probably scan the
  256. // framework to load *everything*. But, it's not clear that this is a good
  257. // idea.
  258. if (!UmbrellaHeader)
  259. return 0;
  260. Module *Result = new Module(ModuleName, SourceLocation(), Parent,
  261. /*IsFramework=*/true, /*IsExplicit=*/false);
  262. if (IsSystem)
  263. Result->IsSystem = IsSystem;
  264. if (!Parent)
  265. Modules[ModuleName] = Result;
  266. // umbrella header "umbrella-header-name"
  267. Result->Umbrella = UmbrellaHeader;
  268. Headers[UmbrellaHeader] = Result;
  269. UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
  270. // export *
  271. Result->Exports.push_back(Module::ExportDecl(0, true));
  272. // module * { export * }
  273. Result->InferSubmodules = true;
  274. Result->InferExportWildcard = true;
  275. // Look for subframeworks.
  276. llvm::error_code EC;
  277. SmallString<128> SubframeworksDirName
  278. = StringRef(FrameworkDir->getName());
  279. llvm::sys::path::append(SubframeworksDirName, "Frameworks");
  280. SmallString<128> SubframeworksDirNameNative;
  281. llvm::sys::path::native(SubframeworksDirName.str(),
  282. SubframeworksDirNameNative);
  283. for (llvm::sys::fs::directory_iterator
  284. Dir(SubframeworksDirNameNative.str(), EC), DirEnd;
  285. Dir != DirEnd && !EC; Dir.increment(EC)) {
  286. if (!StringRef(Dir->path()).endswith(".framework"))
  287. continue;
  288. if (const DirectoryEntry *SubframeworkDir
  289. = FileMgr.getDirectory(Dir->path())) {
  290. // FIXME: Do we want to warn about subframeworks without umbrella headers?
  291. inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
  292. IsSystem, Result);
  293. }
  294. }
  295. return Result;
  296. }
  297. void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
  298. Headers[UmbrellaHeader] = Mod;
  299. Mod->Umbrella = UmbrellaHeader;
  300. UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
  301. }
  302. void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
  303. Mod->Umbrella = UmbrellaDir;
  304. UmbrellaDirs[UmbrellaDir] = Mod;
  305. }
  306. void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
  307. Mod->Headers.push_back(Header);
  308. Headers[Header] = Mod;
  309. }
  310. const FileEntry *
  311. ModuleMap::getContainingModuleMapFile(Module *Module) {
  312. if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
  313. return 0;
  314. return SourceMgr->getFileEntryForID(
  315. SourceMgr->getFileID(Module->DefinitionLoc));
  316. }
  317. void ModuleMap::dump() {
  318. llvm::errs() << "Modules:";
  319. for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
  320. MEnd = Modules.end();
  321. M != MEnd; ++M)
  322. M->getValue()->print(llvm::errs(), 2);
  323. llvm::errs() << "Headers:";
  324. for (llvm::DenseMap<const FileEntry *, Module *>::iterator
  325. H = Headers.begin(),
  326. HEnd = Headers.end();
  327. H != HEnd; ++H) {
  328. llvm::errs() << " \"" << H->first->getName() << "\" -> "
  329. << H->second->getFullModuleName() << "\n";
  330. }
  331. }
  332. bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
  333. bool HadError = false;
  334. for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
  335. Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
  336. Complain);
  337. if (Export.getPointer() || Export.getInt())
  338. Mod->Exports.push_back(Export);
  339. else
  340. HadError = true;
  341. }
  342. Mod->UnresolvedExports.clear();
  343. return HadError;
  344. }
  345. Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
  346. if (Loc.isInvalid())
  347. return 0;
  348. // Use the expansion location to determine which module we're in.
  349. FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
  350. if (!ExpansionLoc.isFileID())
  351. return 0;
  352. const SourceManager &SrcMgr = Loc.getManager();
  353. FileID ExpansionFileID = ExpansionLoc.getFileID();
  354. while (const FileEntry *ExpansionFile
  355. = SrcMgr.getFileEntryForID(ExpansionFileID)) {
  356. // Find the module that owns this header (if any).
  357. if (Module *Mod = findModuleForHeader(ExpansionFile))
  358. return Mod;
  359. // No module owns this header, so look up the inclusion chain to see if
  360. // any included header has an associated module.
  361. SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
  362. if (IncludeLoc.isInvalid())
  363. return 0;
  364. ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
  365. }
  366. return 0;
  367. }
  368. //----------------------------------------------------------------------------//
  369. // Module map file parser
  370. //----------------------------------------------------------------------------//
  371. namespace clang {
  372. /// \brief A token in a module map file.
  373. struct MMToken {
  374. enum TokenKind {
  375. Comma,
  376. EndOfFile,
  377. HeaderKeyword,
  378. Identifier,
  379. ExplicitKeyword,
  380. ExportKeyword,
  381. FrameworkKeyword,
  382. ModuleKeyword,
  383. Period,
  384. UmbrellaKeyword,
  385. RequiresKeyword,
  386. Star,
  387. StringLiteral,
  388. LBrace,
  389. RBrace,
  390. LSquare,
  391. RSquare
  392. } Kind;
  393. unsigned Location;
  394. unsigned StringLength;
  395. const char *StringData;
  396. void clear() {
  397. Kind = EndOfFile;
  398. Location = 0;
  399. StringLength = 0;
  400. StringData = 0;
  401. }
  402. bool is(TokenKind K) const { return Kind == K; }
  403. SourceLocation getLocation() const {
  404. return SourceLocation::getFromRawEncoding(Location);
  405. }
  406. StringRef getString() const {
  407. return StringRef(StringData, StringLength);
  408. }
  409. };
  410. class ModuleMapParser {
  411. Lexer &L;
  412. SourceManager &SourceMgr;
  413. DiagnosticsEngine &Diags;
  414. ModuleMap &Map;
  415. /// \brief The directory that this module map resides in.
  416. const DirectoryEntry *Directory;
  417. /// \brief The directory containing Clang-supplied headers.
  418. const DirectoryEntry *BuiltinIncludeDir;
  419. /// \brief Whether an error occurred.
  420. bool HadError;
  421. /// \brief Default target information, used only for string literal
  422. /// parsing.
  423. TargetInfo *Target;
  424. /// \brief Stores string data for the various string literals referenced
  425. /// during parsing.
  426. llvm::BumpPtrAllocator StringData;
  427. /// \brief The current token.
  428. MMToken Tok;
  429. /// \brief The active module.
  430. Module *ActiveModule;
  431. /// \brief Consume the current token and return its location.
  432. SourceLocation consumeToken();
  433. /// \brief Skip tokens until we reach the a token with the given kind
  434. /// (or the end of the file).
  435. void skipUntil(MMToken::TokenKind K);
  436. typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
  437. ModuleId;
  438. bool parseModuleId(ModuleId &Id);
  439. void parseModuleDecl();
  440. void parseRequiresDecl();
  441. void parseHeaderDecl(SourceLocation UmbrellaLoc);
  442. void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
  443. void parseExportDecl();
  444. void parseInferredSubmoduleDecl(bool Explicit);
  445. const DirectoryEntry *getOverriddenHeaderSearchDir();
  446. public:
  447. explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
  448. DiagnosticsEngine &Diags,
  449. ModuleMap &Map,
  450. const DirectoryEntry *Directory,
  451. const DirectoryEntry *BuiltinIncludeDir)
  452. : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
  453. Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir),
  454. HadError(false), ActiveModule(0)
  455. {
  456. TargetOptions TargetOpts;
  457. TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
  458. Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
  459. Tok.clear();
  460. consumeToken();
  461. }
  462. bool parseModuleMapFile();
  463. };
  464. }
  465. SourceLocation ModuleMapParser::consumeToken() {
  466. retry:
  467. SourceLocation Result = Tok.getLocation();
  468. Tok.clear();
  469. Token LToken;
  470. L.LexFromRawLexer(LToken);
  471. Tok.Location = LToken.getLocation().getRawEncoding();
  472. switch (LToken.getKind()) {
  473. case tok::raw_identifier:
  474. Tok.StringData = LToken.getRawIdentifierData();
  475. Tok.StringLength = LToken.getLength();
  476. Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
  477. .Case("header", MMToken::HeaderKeyword)
  478. .Case("explicit", MMToken::ExplicitKeyword)
  479. .Case("export", MMToken::ExportKeyword)
  480. .Case("framework", MMToken::FrameworkKeyword)
  481. .Case("module", MMToken::ModuleKeyword)
  482. .Case("requires", MMToken::RequiresKeyword)
  483. .Case("umbrella", MMToken::UmbrellaKeyword)
  484. .Default(MMToken::Identifier);
  485. break;
  486. case tok::comma:
  487. Tok.Kind = MMToken::Comma;
  488. break;
  489. case tok::eof:
  490. Tok.Kind = MMToken::EndOfFile;
  491. break;
  492. case tok::l_brace:
  493. Tok.Kind = MMToken::LBrace;
  494. break;
  495. case tok::l_square:
  496. Tok.Kind = MMToken::LSquare;
  497. break;
  498. case tok::period:
  499. Tok.Kind = MMToken::Period;
  500. break;
  501. case tok::r_brace:
  502. Tok.Kind = MMToken::RBrace;
  503. break;
  504. case tok::r_square:
  505. Tok.Kind = MMToken::RSquare;
  506. break;
  507. case tok::star:
  508. Tok.Kind = MMToken::Star;
  509. break;
  510. case tok::string_literal: {
  511. // Parse the string literal.
  512. LangOptions LangOpts;
  513. StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
  514. if (StringLiteral.hadError)
  515. goto retry;
  516. // Copy the string literal into our string data allocator.
  517. unsigned Length = StringLiteral.GetStringLength();
  518. char *Saved = StringData.Allocate<char>(Length + 1);
  519. memcpy(Saved, StringLiteral.GetString().data(), Length);
  520. Saved[Length] = 0;
  521. // Form the token.
  522. Tok.Kind = MMToken::StringLiteral;
  523. Tok.StringData = Saved;
  524. Tok.StringLength = Length;
  525. break;
  526. }
  527. case tok::comment:
  528. goto retry;
  529. default:
  530. Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
  531. HadError = true;
  532. goto retry;
  533. }
  534. return Result;
  535. }
  536. void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
  537. unsigned braceDepth = 0;
  538. unsigned squareDepth = 0;
  539. do {
  540. switch (Tok.Kind) {
  541. case MMToken::EndOfFile:
  542. return;
  543. case MMToken::LBrace:
  544. if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
  545. return;
  546. ++braceDepth;
  547. break;
  548. case MMToken::LSquare:
  549. if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
  550. return;
  551. ++squareDepth;
  552. break;
  553. case MMToken::RBrace:
  554. if (braceDepth > 0)
  555. --braceDepth;
  556. else if (Tok.is(K))
  557. return;
  558. break;
  559. case MMToken::RSquare:
  560. if (squareDepth > 0)
  561. --squareDepth;
  562. else if (Tok.is(K))
  563. return;
  564. break;
  565. default:
  566. if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
  567. return;
  568. break;
  569. }
  570. consumeToken();
  571. } while (true);
  572. }
  573. /// \brief Parse a module-id.
  574. ///
  575. /// module-id:
  576. /// identifier
  577. /// identifier '.' module-id
  578. ///
  579. /// \returns true if an error occurred, false otherwise.
  580. bool ModuleMapParser::parseModuleId(ModuleId &Id) {
  581. Id.clear();
  582. do {
  583. if (Tok.is(MMToken::Identifier)) {
  584. Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
  585. consumeToken();
  586. } else {
  587. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
  588. return true;
  589. }
  590. if (!Tok.is(MMToken::Period))
  591. break;
  592. consumeToken();
  593. } while (true);
  594. return false;
  595. }
  596. namespace {
  597. /// \brief Enumerates the known attributes.
  598. enum AttributeKind {
  599. /// \brief An unknown attribute.
  600. AT_unknown,
  601. /// \brief The 'system' attribute.
  602. AT_system
  603. };
  604. }
  605. /// \brief Parse a module declaration.
  606. ///
  607. /// module-declaration:
  608. /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
  609. /// { module-member* }
  610. ///
  611. /// attributes:
  612. /// attribute attributes
  613. /// attribute
  614. ///
  615. /// attribute:
  616. /// [ identifier ]
  617. ///
  618. /// module-member:
  619. /// requires-declaration
  620. /// header-declaration
  621. /// submodule-declaration
  622. /// export-declaration
  623. ///
  624. /// submodule-declaration:
  625. /// module-declaration
  626. /// inferred-submodule-declaration
  627. void ModuleMapParser::parseModuleDecl() {
  628. assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
  629. Tok.is(MMToken::FrameworkKeyword));
  630. // Parse 'explicit' or 'framework' keyword, if present.
  631. SourceLocation ExplicitLoc;
  632. bool Explicit = false;
  633. bool Framework = false;
  634. // Parse 'explicit' keyword, if present.
  635. if (Tok.is(MMToken::ExplicitKeyword)) {
  636. ExplicitLoc = consumeToken();
  637. Explicit = true;
  638. }
  639. // Parse 'framework' keyword, if present.
  640. if (Tok.is(MMToken::FrameworkKeyword)) {
  641. consumeToken();
  642. Framework = true;
  643. }
  644. // Parse 'module' keyword.
  645. if (!Tok.is(MMToken::ModuleKeyword)) {
  646. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
  647. consumeToken();
  648. HadError = true;
  649. return;
  650. }
  651. consumeToken(); // 'module' keyword
  652. // If we have a wildcard for the module name, this is an inferred submodule.
  653. // Parse it.
  654. if (Tok.is(MMToken::Star))
  655. return parseInferredSubmoduleDecl(Explicit);
  656. // Parse the module name.
  657. ModuleId Id;
  658. if (parseModuleId(Id)) {
  659. HadError = true;
  660. return;
  661. }
  662. if (ActiveModule) {
  663. if (Id.size() > 1) {
  664. Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
  665. << SourceRange(Id.front().second, Id.back().second);
  666. HadError = true;
  667. return;
  668. }
  669. } else if (Id.size() == 1 && Explicit) {
  670. // Top-level modules can't be explicit.
  671. Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
  672. Explicit = false;
  673. ExplicitLoc = SourceLocation();
  674. HadError = true;
  675. }
  676. Module *PreviousActiveModule = ActiveModule;
  677. if (Id.size() > 1) {
  678. // This module map defines a submodule. Go find the module of which it
  679. // is a submodule.
  680. ActiveModule = 0;
  681. for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
  682. if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
  683. ActiveModule = Next;
  684. continue;
  685. }
  686. if (ActiveModule) {
  687. Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
  688. << Id[I].first << ActiveModule->getTopLevelModule();
  689. } else {
  690. Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
  691. }
  692. HadError = true;
  693. return;
  694. }
  695. }
  696. StringRef ModuleName = Id.back().first;
  697. SourceLocation ModuleNameLoc = Id.back().second;
  698. // Parse the optional attribute list.
  699. bool IsSystem = false;
  700. while (Tok.is(MMToken::LSquare)) {
  701. // Consume the '['.
  702. SourceLocation LSquareLoc = consumeToken();
  703. // Check whether we have an attribute name here.
  704. if (!Tok.is(MMToken::Identifier)) {
  705. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
  706. skipUntil(MMToken::RSquare);
  707. if (Tok.is(MMToken::RSquare))
  708. consumeToken();
  709. continue;
  710. }
  711. // Decode the attribute name.
  712. AttributeKind Attribute
  713. = llvm::StringSwitch<AttributeKind>(Tok.getString())
  714. .Case("system", AT_system)
  715. .Default(AT_unknown);
  716. switch (Attribute) {
  717. case AT_unknown:
  718. Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
  719. << Tok.getString();
  720. break;
  721. case AT_system:
  722. IsSystem = true;
  723. break;
  724. }
  725. consumeToken();
  726. // Consume the ']'.
  727. if (!Tok.is(MMToken::RSquare)) {
  728. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
  729. Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
  730. skipUntil(MMToken::RSquare);
  731. }
  732. if (Tok.is(MMToken::RSquare))
  733. consumeToken();
  734. }
  735. // Parse the opening brace.
  736. if (!Tok.is(MMToken::LBrace)) {
  737. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
  738. << ModuleName;
  739. HadError = true;
  740. return;
  741. }
  742. SourceLocation LBraceLoc = consumeToken();
  743. // Determine whether this (sub)module has already been defined.
  744. if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
  745. if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
  746. // Skip the module definition.
  747. skipUntil(MMToken::RBrace);
  748. if (Tok.is(MMToken::RBrace))
  749. consumeToken();
  750. else {
  751. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  752. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  753. HadError = true;
  754. }
  755. return;
  756. }
  757. Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
  758. << ModuleName;
  759. Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
  760. // Skip the module definition.
  761. skipUntil(MMToken::RBrace);
  762. if (Tok.is(MMToken::RBrace))
  763. consumeToken();
  764. HadError = true;
  765. return;
  766. }
  767. // Start defining this module.
  768. ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
  769. Explicit).first;
  770. ActiveModule->DefinitionLoc = ModuleNameLoc;
  771. if (IsSystem)
  772. ActiveModule->IsSystem = true;
  773. bool Done = false;
  774. do {
  775. switch (Tok.Kind) {
  776. case MMToken::EndOfFile:
  777. case MMToken::RBrace:
  778. Done = true;
  779. break;
  780. case MMToken::ExplicitKeyword:
  781. case MMToken::FrameworkKeyword:
  782. case MMToken::ModuleKeyword:
  783. parseModuleDecl();
  784. break;
  785. case MMToken::ExportKeyword:
  786. parseExportDecl();
  787. break;
  788. case MMToken::RequiresKeyword:
  789. parseRequiresDecl();
  790. break;
  791. case MMToken::UmbrellaKeyword: {
  792. SourceLocation UmbrellaLoc = consumeToken();
  793. if (Tok.is(MMToken::HeaderKeyword))
  794. parseHeaderDecl(UmbrellaLoc);
  795. else
  796. parseUmbrellaDirDecl(UmbrellaLoc);
  797. break;
  798. }
  799. case MMToken::HeaderKeyword:
  800. parseHeaderDecl(SourceLocation());
  801. break;
  802. default:
  803. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
  804. consumeToken();
  805. break;
  806. }
  807. } while (!Done);
  808. if (Tok.is(MMToken::RBrace))
  809. consumeToken();
  810. else {
  811. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  812. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  813. HadError = true;
  814. }
  815. // We're done parsing this module. Pop back to the previous module.
  816. ActiveModule = PreviousActiveModule;
  817. }
  818. /// \brief Parse a requires declaration.
  819. ///
  820. /// requires-declaration:
  821. /// 'requires' feature-list
  822. ///
  823. /// feature-list:
  824. /// identifier ',' feature-list
  825. /// identifier
  826. void ModuleMapParser::parseRequiresDecl() {
  827. assert(Tok.is(MMToken::RequiresKeyword));
  828. // Parse 'requires' keyword.
  829. consumeToken();
  830. // Parse the feature-list.
  831. do {
  832. if (!Tok.is(MMToken::Identifier)) {
  833. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
  834. HadError = true;
  835. return;
  836. }
  837. // Consume the feature name.
  838. std::string Feature = Tok.getString();
  839. consumeToken();
  840. // Add this feature.
  841. ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target);
  842. if (!Tok.is(MMToken::Comma))
  843. break;
  844. // Consume the comma.
  845. consumeToken();
  846. } while (true);
  847. }
  848. /// \brief Append to \p Paths the set of paths needed to get to the
  849. /// subframework in which the given module lives.
  850. void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) {
  851. // Collect the framework names from the given module to the top-level module.
  852. llvm::SmallVector<StringRef, 2> Paths;
  853. for (; Mod; Mod = Mod->Parent) {
  854. if (Mod->IsFramework)
  855. Paths.push_back(Mod->Name);
  856. }
  857. if (Paths.empty())
  858. return;
  859. // Add Frameworks/Name.framework for each subframework.
  860. for (unsigned I = Paths.size() - 1; I != 0; --I) {
  861. llvm::sys::path::append(Path, "Frameworks");
  862. llvm::sys::path::append(Path, Paths[I-1] + ".framework");
  863. }
  864. }
  865. /// \brief Determine whether the given file name is the name of a builtin
  866. /// header, supplied by Clang to replace, override, or augment existing system
  867. /// headers.
  868. static bool isBuiltinHeader(StringRef FileName) {
  869. return llvm::StringSwitch<bool>(FileName)
  870. .Case("float.h", true)
  871. .Case("iso646.h", true)
  872. .Case("limits.h", true)
  873. .Case("stdalign.h", true)
  874. .Case("stdarg.h", true)
  875. .Case("stdbool.h", true)
  876. .Case("stddef.h", true)
  877. .Case("stdint.h", true)
  878. .Case("tgmath.h", true)
  879. .Case("unwind.h", true)
  880. .Default(false);
  881. }
  882. /// \brief Parse a header declaration.
  883. ///
  884. /// header-declaration:
  885. /// 'umbrella'[opt] 'header' string-literal
  886. void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) {
  887. assert(Tok.is(MMToken::HeaderKeyword));
  888. consumeToken();
  889. bool Umbrella = UmbrellaLoc.isValid();
  890. // Parse the header name.
  891. if (!Tok.is(MMToken::StringLiteral)) {
  892. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  893. << "header";
  894. HadError = true;
  895. return;
  896. }
  897. std::string FileName = Tok.getString();
  898. SourceLocation FileNameLoc = consumeToken();
  899. // Check whether we already have an umbrella.
  900. if (Umbrella && ActiveModule->Umbrella) {
  901. Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
  902. << ActiveModule->getFullModuleName();
  903. HadError = true;
  904. return;
  905. }
  906. // Look for this file.
  907. const FileEntry *File = 0;
  908. const FileEntry *BuiltinFile = 0;
  909. SmallString<128> PathName;
  910. if (llvm::sys::path::is_absolute(FileName)) {
  911. PathName = FileName;
  912. File = SourceMgr.getFileManager().getFile(PathName);
  913. } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
  914. PathName = Dir->getName();
  915. llvm::sys::path::append(PathName, FileName);
  916. File = SourceMgr.getFileManager().getFile(PathName);
  917. } else {
  918. // Search for the header file within the search directory.
  919. PathName = Directory->getName();
  920. unsigned PathLength = PathName.size();
  921. if (ActiveModule->isPartOfFramework()) {
  922. appendSubframeworkPaths(ActiveModule, PathName);
  923. // Check whether this file is in the public headers.
  924. llvm::sys::path::append(PathName, "Headers");
  925. llvm::sys::path::append(PathName, FileName);
  926. File = SourceMgr.getFileManager().getFile(PathName);
  927. if (!File) {
  928. // Check whether this file is in the private headers.
  929. PathName.resize(PathLength);
  930. llvm::sys::path::append(PathName, "PrivateHeaders");
  931. llvm::sys::path::append(PathName, FileName);
  932. File = SourceMgr.getFileManager().getFile(PathName);
  933. }
  934. } else {
  935. // Lookup for normal headers.
  936. llvm::sys::path::append(PathName, FileName);
  937. File = SourceMgr.getFileManager().getFile(PathName);
  938. // If this is a system module with a top-level header, this header
  939. // may have a counterpart (or replacement) in the set of headers
  940. // supplied by Clang. Find that builtin header.
  941. if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir &&
  942. BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) {
  943. SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
  944. llvm::sys::path::append(BuiltinPathName, FileName);
  945. BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
  946. // If Clang supplies this header but the underlying system does not,
  947. // just silently swap in our builtin version. Otherwise, we'll end
  948. // up adding both (later).
  949. if (!File && BuiltinFile) {
  950. File = BuiltinFile;
  951. BuiltinFile = 0;
  952. }
  953. }
  954. }
  955. }
  956. // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
  957. // Come up with a lazy way to do this.
  958. if (File) {
  959. if (const Module *OwningModule = Map.Headers[File]) {
  960. Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
  961. << FileName << OwningModule->getFullModuleName();
  962. HadError = true;
  963. } else if (Umbrella) {
  964. const DirectoryEntry *UmbrellaDir = File->getDir();
  965. if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
  966. Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
  967. << OwningModule->getFullModuleName();
  968. HadError = true;
  969. } else {
  970. // Record this umbrella header.
  971. Map.setUmbrellaHeader(ActiveModule, File);
  972. }
  973. } else {
  974. // Record this header.
  975. Map.addHeader(ActiveModule, File);
  976. // If there is a builtin counterpart to this file, add it now.
  977. if (BuiltinFile)
  978. Map.addHeader(ActiveModule, BuiltinFile);
  979. }
  980. } else {
  981. Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
  982. << Umbrella << FileName;
  983. HadError = true;
  984. }
  985. }
  986. /// \brief Parse an umbrella directory declaration.
  987. ///
  988. /// umbrella-dir-declaration:
  989. /// umbrella string-literal
  990. void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
  991. // Parse the directory name.
  992. if (!Tok.is(MMToken::StringLiteral)) {
  993. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  994. << "umbrella";
  995. HadError = true;
  996. return;
  997. }
  998. std::string DirName = Tok.getString();
  999. SourceLocation DirNameLoc = consumeToken();
  1000. // Check whether we already have an umbrella.
  1001. if (ActiveModule->Umbrella) {
  1002. Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
  1003. << ActiveModule->getFullModuleName();
  1004. HadError = true;
  1005. return;
  1006. }
  1007. // Look for this file.
  1008. const DirectoryEntry *Dir = 0;
  1009. if (llvm::sys::path::is_absolute(DirName))
  1010. Dir = SourceMgr.getFileManager().getDirectory(DirName);
  1011. else {
  1012. SmallString<128> PathName;
  1013. PathName = Directory->getName();
  1014. llvm::sys::path::append(PathName, DirName);
  1015. Dir = SourceMgr.getFileManager().getDirectory(PathName);
  1016. }
  1017. if (!Dir) {
  1018. Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
  1019. << DirName;
  1020. HadError = true;
  1021. return;
  1022. }
  1023. if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
  1024. Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
  1025. << OwningModule->getFullModuleName();
  1026. HadError = true;
  1027. return;
  1028. }
  1029. // Record this umbrella directory.
  1030. Map.setUmbrellaDir(ActiveModule, Dir);
  1031. }
  1032. /// \brief Parse a module export declaration.
  1033. ///
  1034. /// export-declaration:
  1035. /// 'export' wildcard-module-id
  1036. ///
  1037. /// wildcard-module-id:
  1038. /// identifier
  1039. /// '*'
  1040. /// identifier '.' wildcard-module-id
  1041. void ModuleMapParser::parseExportDecl() {
  1042. assert(Tok.is(MMToken::ExportKeyword));
  1043. SourceLocation ExportLoc = consumeToken();
  1044. // Parse the module-id with an optional wildcard at the end.
  1045. ModuleId ParsedModuleId;
  1046. bool Wildcard = false;
  1047. do {
  1048. if (Tok.is(MMToken::Identifier)) {
  1049. ParsedModuleId.push_back(std::make_pair(Tok.getString(),
  1050. Tok.getLocation()));
  1051. consumeToken();
  1052. if (Tok.is(MMToken::Period)) {
  1053. consumeToken();
  1054. continue;
  1055. }
  1056. break;
  1057. }
  1058. if(Tok.is(MMToken::Star)) {
  1059. Wildcard = true;
  1060. consumeToken();
  1061. break;
  1062. }
  1063. Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
  1064. HadError = true;
  1065. return;
  1066. } while (true);
  1067. Module::UnresolvedExportDecl Unresolved = {
  1068. ExportLoc, ParsedModuleId, Wildcard
  1069. };
  1070. ActiveModule->UnresolvedExports.push_back(Unresolved);
  1071. }
  1072. void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
  1073. assert(Tok.is(MMToken::Star));
  1074. SourceLocation StarLoc = consumeToken();
  1075. bool Failed = false;
  1076. // Inferred modules must be submodules.
  1077. if (!ActiveModule) {
  1078. Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
  1079. Failed = true;
  1080. }
  1081. // Inferred modules must have umbrella directories.
  1082. if (!Failed && !ActiveModule->getUmbrellaDir()) {
  1083. Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
  1084. Failed = true;
  1085. }
  1086. // Check for redefinition of an inferred module.
  1087. if (!Failed && ActiveModule->InferSubmodules) {
  1088. Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
  1089. if (ActiveModule->InferredSubmoduleLoc.isValid())
  1090. Diags.Report(ActiveModule->InferredSubmoduleLoc,
  1091. diag::note_mmap_prev_definition);
  1092. Failed = true;
  1093. }
  1094. // If there were any problems with this inferred submodule, skip its body.
  1095. if (Failed) {
  1096. if (Tok.is(MMToken::LBrace)) {
  1097. consumeToken();
  1098. skipUntil(MMToken::RBrace);
  1099. if (Tok.is(MMToken::RBrace))
  1100. consumeToken();
  1101. }
  1102. HadError = true;
  1103. return;
  1104. }
  1105. // Note that we have an inferred submodule.
  1106. ActiveModule->InferSubmodules = true;
  1107. ActiveModule->InferredSubmoduleLoc = StarLoc;
  1108. ActiveModule->InferExplicitSubmodules = Explicit;
  1109. // Parse the opening brace.
  1110. if (!Tok.is(MMToken::LBrace)) {
  1111. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
  1112. HadError = true;
  1113. return;
  1114. }
  1115. SourceLocation LBraceLoc = consumeToken();
  1116. // Parse the body of the inferred submodule.
  1117. bool Done = false;
  1118. do {
  1119. switch (Tok.Kind) {
  1120. case MMToken::EndOfFile:
  1121. case MMToken::RBrace:
  1122. Done = true;
  1123. break;
  1124. case MMToken::ExportKeyword: {
  1125. consumeToken();
  1126. if (Tok.is(MMToken::Star))
  1127. ActiveModule->InferExportWildcard = true;
  1128. else
  1129. Diags.Report(Tok.getLocation(),
  1130. diag::err_mmap_expected_export_wildcard);
  1131. consumeToken();
  1132. break;
  1133. }
  1134. case MMToken::ExplicitKeyword:
  1135. case MMToken::ModuleKeyword:
  1136. case MMToken::HeaderKeyword:
  1137. case MMToken::UmbrellaKeyword:
  1138. default:
  1139. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
  1140. consumeToken();
  1141. break;
  1142. }
  1143. } while (!Done);
  1144. if (Tok.is(MMToken::RBrace))
  1145. consumeToken();
  1146. else {
  1147. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  1148. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  1149. HadError = true;
  1150. }
  1151. }
  1152. /// \brief If there is a specific header search directory due the presence
  1153. /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
  1154. const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
  1155. for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
  1156. // If we have an umbrella directory, use that.
  1157. if (Mod->hasUmbrellaDir())
  1158. return Mod->getUmbrellaDir();
  1159. // If we have a framework directory, stop looking.
  1160. if (Mod->IsFramework)
  1161. return 0;
  1162. }
  1163. return 0;
  1164. }
  1165. /// \brief Parse a module map file.
  1166. ///
  1167. /// module-map-file:
  1168. /// module-declaration*
  1169. bool ModuleMapParser::parseModuleMapFile() {
  1170. do {
  1171. switch (Tok.Kind) {
  1172. case MMToken::EndOfFile:
  1173. return HadError;
  1174. case MMToken::ExplicitKeyword:
  1175. case MMToken::ModuleKeyword:
  1176. case MMToken::FrameworkKeyword:
  1177. parseModuleDecl();
  1178. break;
  1179. case MMToken::Comma:
  1180. case MMToken::ExportKeyword:
  1181. case MMToken::HeaderKeyword:
  1182. case MMToken::Identifier:
  1183. case MMToken::LBrace:
  1184. case MMToken::LSquare:
  1185. case MMToken::Period:
  1186. case MMToken::RBrace:
  1187. case MMToken::RSquare:
  1188. case MMToken::RequiresKeyword:
  1189. case MMToken::Star:
  1190. case MMToken::StringLiteral:
  1191. case MMToken::UmbrellaKeyword:
  1192. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
  1193. HadError = true;
  1194. consumeToken();
  1195. break;
  1196. }
  1197. } while (true);
  1198. }
  1199. bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
  1200. assert(Target != 0 && "Missing target information");
  1201. FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
  1202. const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
  1203. if (!Buffer)
  1204. return true;
  1205. // Parse this module map file.
  1206. Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
  1207. Diags->getClient()->BeginSourceFile(MMapLangOpts);
  1208. ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir(),
  1209. BuiltinIncludeDir);
  1210. bool Result = Parser.parseModuleMapFile();
  1211. Diags->getClient()->EndSourceFile();
  1212. return Result;
  1213. }