ModuleMap.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435
  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/Basic/CharInfo.h"
  16. #include "clang/Basic/Diagnostic.h"
  17. #include "clang/Basic/DiagnosticOptions.h"
  18. #include "clang/Basic/FileManager.h"
  19. #include "clang/Basic/TargetInfo.h"
  20. #include "clang/Basic/TargetOptions.h"
  21. #include "clang/Lex/HeaderSearch.h"
  22. #include "clang/Lex/HeaderSearchOptions.h"
  23. #include "clang/Lex/LexDiagnostic.h"
  24. #include "clang/Lex/Lexer.h"
  25. #include "clang/Lex/LiteralSupport.h"
  26. #include "llvm/ADT/StringRef.h"
  27. #include "llvm/ADT/StringSwitch.h"
  28. #include "llvm/Support/Allocator.h"
  29. #include "llvm/Support/FileSystem.h"
  30. #include "llvm/Support/Host.h"
  31. #include "llvm/Support/Path.h"
  32. #include "llvm/Support/raw_ostream.h"
  33. #include <stdlib.h>
  34. #if defined(LLVM_ON_UNIX)
  35. #include <limits.h>
  36. #endif
  37. using namespace clang;
  38. Module::ExportDecl
  39. ModuleMap::resolveExport(Module *Mod,
  40. const Module::UnresolvedExportDecl &Unresolved,
  41. bool Complain) const {
  42. // We may have just a wildcard.
  43. if (Unresolved.Id.empty()) {
  44. assert(Unresolved.Wildcard && "Invalid unresolved export");
  45. return Module::ExportDecl(nullptr, true);
  46. }
  47. // Resolve the module-id.
  48. Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain);
  49. if (!Context)
  50. return Module::ExportDecl();
  51. return Module::ExportDecl(Context, Unresolved.Wildcard);
  52. }
  53. Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod,
  54. bool Complain) const {
  55. // Find the starting module.
  56. Module *Context = lookupModuleUnqualified(Id[0].first, Mod);
  57. if (!Context) {
  58. if (Complain)
  59. Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified)
  60. << Id[0].first << Mod->getFullModuleName();
  61. return nullptr;
  62. }
  63. // Dig into the module path.
  64. for (unsigned I = 1, N = Id.size(); I != N; ++I) {
  65. Module *Sub = lookupModuleQualified(Id[I].first, Context);
  66. if (!Sub) {
  67. if (Complain)
  68. Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
  69. << Id[I].first << Context->getFullModuleName()
  70. << SourceRange(Id[0].second, Id[I-1].second);
  71. return nullptr;
  72. }
  73. Context = Sub;
  74. }
  75. return Context;
  76. }
  77. ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
  78. const LangOptions &LangOpts, const TargetInfo *Target,
  79. HeaderSearch &HeaderInfo)
  80. : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
  81. HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr),
  82. CompilingModule(nullptr), SourceModule(nullptr), NumCreatedModules(0) {
  83. MMapLangOpts.LineComment = true;
  84. }
  85. ModuleMap::~ModuleMap() {
  86. for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
  87. IEnd = Modules.end();
  88. I != IEnd; ++I) {
  89. delete I->getValue();
  90. }
  91. }
  92. void ModuleMap::setTarget(const TargetInfo &Target) {
  93. assert((!this->Target || this->Target == &Target) &&
  94. "Improper target override");
  95. this->Target = &Target;
  96. }
  97. /// \brief "Sanitize" a filename so that it can be used as an identifier.
  98. static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
  99. SmallVectorImpl<char> &Buffer) {
  100. if (Name.empty())
  101. return Name;
  102. if (!isValidIdentifier(Name)) {
  103. // If we don't already have something with the form of an identifier,
  104. // create a buffer with the sanitized name.
  105. Buffer.clear();
  106. if (isDigit(Name[0]))
  107. Buffer.push_back('_');
  108. Buffer.reserve(Buffer.size() + Name.size());
  109. for (unsigned I = 0, N = Name.size(); I != N; ++I) {
  110. if (isIdentifierBody(Name[I]))
  111. Buffer.push_back(Name[I]);
  112. else
  113. Buffer.push_back('_');
  114. }
  115. Name = StringRef(Buffer.data(), Buffer.size());
  116. }
  117. while (llvm::StringSwitch<bool>(Name)
  118. #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
  119. #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
  120. #include "clang/Basic/TokenKinds.def"
  121. .Default(false)) {
  122. if (Name.data() != Buffer.data())
  123. Buffer.append(Name.begin(), Name.end());
  124. Buffer.push_back('_');
  125. Name = StringRef(Buffer.data(), Buffer.size());
  126. }
  127. return Name;
  128. }
  129. /// \brief Determine whether the given file name is the name of a builtin
  130. /// header, supplied by Clang to replace, override, or augment existing system
  131. /// headers.
  132. static bool isBuiltinHeader(StringRef FileName) {
  133. return llvm::StringSwitch<bool>(FileName)
  134. .Case("float.h", true)
  135. .Case("iso646.h", true)
  136. .Case("limits.h", true)
  137. .Case("stdalign.h", true)
  138. .Case("stdarg.h", true)
  139. .Case("stdbool.h", true)
  140. .Case("stddef.h", true)
  141. .Case("stdint.h", true)
  142. .Case("tgmath.h", true)
  143. .Case("unwind.h", true)
  144. .Default(false);
  145. }
  146. ModuleMap::HeadersMap::iterator
  147. ModuleMap::findKnownHeader(const FileEntry *File) {
  148. HeadersMap::iterator Known = Headers.find(File);
  149. if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps &&
  150. Known == Headers.end() && File->getDir() == BuiltinIncludeDir &&
  151. isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
  152. HeaderInfo.loadTopLevelSystemModules();
  153. return Headers.find(File);
  154. }
  155. return Known;
  156. }
  157. ModuleMap::KnownHeader
  158. ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
  159. SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) {
  160. if (UmbrellaDirs.empty())
  161. return KnownHeader();
  162. const DirectoryEntry *Dir = File->getDir();
  163. assert(Dir && "file in no directory");
  164. // Note: as an egregious but useful hack we use the real path here, because
  165. // frameworks moving from top-level frameworks to embedded frameworks tend
  166. // to be symlinked from the top-level location to the embedded location,
  167. // and we need to resolve lookups as if we had found the embedded location.
  168. StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
  169. // Keep walking up the directory hierarchy, looking for a directory with
  170. // an umbrella header.
  171. do {
  172. auto KnownDir = UmbrellaDirs.find(Dir);
  173. if (KnownDir != UmbrellaDirs.end())
  174. return KnownHeader(KnownDir->second, NormalHeader);
  175. IntermediateDirs.push_back(Dir);
  176. // Retrieve our parent path.
  177. DirName = llvm::sys::path::parent_path(DirName);
  178. if (DirName.empty())
  179. break;
  180. // Resolve the parent path to a directory entry.
  181. Dir = SourceMgr.getFileManager().getDirectory(DirName);
  182. } while (Dir);
  183. return KnownHeader();
  184. }
  185. static bool violatesPrivateInclude(Module *RequestingModule,
  186. const FileEntry *IncFileEnt,
  187. ModuleMap::ModuleHeaderRole Role,
  188. Module *RequestedModule) {
  189. bool IsPrivateRole = Role & ModuleMap::PrivateHeader;
  190. #ifndef NDEBUG
  191. if (IsPrivateRole) {
  192. // Check for consistency between the module header role
  193. // as obtained from the lookup and as obtained from the module.
  194. // This check is not cheap, so enable it only for debugging.
  195. bool IsPrivate = false;
  196. SmallVectorImpl<Module::Header> *HeaderList[] = {
  197. &RequestedModule->Headers[Module::HK_Private],
  198. &RequestedModule->Headers[Module::HK_PrivateTextual]};
  199. for (auto *Hs : HeaderList)
  200. IsPrivate |=
  201. std::find_if(Hs->begin(), Hs->end(), [&](const Module::Header &H) {
  202. return H.Entry == IncFileEnt;
  203. }) != Hs->end();
  204. assert((!IsPrivateRole || IsPrivate) && "inconsistent headers and roles");
  205. }
  206. #endif
  207. return IsPrivateRole &&
  208. // FIXME: Should we map RequestingModule to its top-level module here
  209. // too? This check is redundant with the isSubModuleOf check in
  210. // diagnoseHeaderInclusion.
  211. RequestedModule->getTopLevelModule() != RequestingModule;
  212. }
  213. static Module *getTopLevelOrNull(Module *M) {
  214. return M ? M->getTopLevelModule() : nullptr;
  215. }
  216. void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule,
  217. SourceLocation FilenameLoc,
  218. StringRef Filename,
  219. const FileEntry *File) {
  220. // No errors for indirect modules. This may be a bit of a problem for modules
  221. // with no source files.
  222. if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule))
  223. return;
  224. if (RequestingModule)
  225. resolveUses(RequestingModule, /*Complain=*/false);
  226. bool Excluded = false;
  227. Module *Private = nullptr;
  228. Module *NotUsed = nullptr;
  229. HeadersMap::iterator Known = findKnownHeader(File);
  230. if (Known != Headers.end()) {
  231. for (const KnownHeader &Header : Known->second) {
  232. // If 'File' is part of 'RequestingModule' we can definitely include it.
  233. if (Header.getModule() &&
  234. Header.getModule()->isSubModuleOf(RequestingModule))
  235. return;
  236. // Remember private headers for later printing of a diagnostic.
  237. if (violatesPrivateInclude(RequestingModule, File, Header.getRole(),
  238. Header.getModule())) {
  239. Private = Header.getModule();
  240. continue;
  241. }
  242. // If uses need to be specified explicitly, we are only allowed to return
  243. // modules that are explicitly used by the requesting module.
  244. if (RequestingModule && LangOpts.ModulesDeclUse &&
  245. !RequestingModule->directlyUses(Header.getModule())) {
  246. NotUsed = Header.getModule();
  247. continue;
  248. }
  249. // We have found a module that we can happily use.
  250. return;
  251. }
  252. Excluded = true;
  253. }
  254. // We have found a header, but it is private.
  255. if (Private) {
  256. Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module)
  257. << Filename;
  258. return;
  259. }
  260. // We have found a module, but we don't use it.
  261. if (NotUsed) {
  262. Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
  263. << RequestingModule->getFullModuleName() << Filename;
  264. return;
  265. }
  266. if (Excluded || isHeaderInUmbrellaDirs(File))
  267. return;
  268. // At this point, only non-modular includes remain.
  269. if (LangOpts.ModulesStrictDeclUse) {
  270. Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
  271. << RequestingModule->getFullModuleName() << Filename;
  272. } else if (RequestingModule) {
  273. diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ?
  274. diag::warn_non_modular_include_in_framework_module :
  275. diag::warn_non_modular_include_in_module;
  276. Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName();
  277. }
  278. }
  279. static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New,
  280. const ModuleMap::KnownHeader &Old) {
  281. // Prefer a public header over a private header.
  282. if ((New.getRole() & ModuleMap::PrivateHeader) !=
  283. (Old.getRole() & ModuleMap::PrivateHeader))
  284. return !(New.getRole() & ModuleMap::PrivateHeader);
  285. // Prefer a non-textual header over a textual header.
  286. if ((New.getRole() & ModuleMap::TextualHeader) !=
  287. (Old.getRole() & ModuleMap::TextualHeader))
  288. return !(New.getRole() & ModuleMap::TextualHeader);
  289. // Don't have a reason to choose between these. Just keep the first one.
  290. return false;
  291. }
  292. ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) {
  293. auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader {
  294. if (R.getRole() & ModuleMap::TextualHeader)
  295. return ModuleMap::KnownHeader();
  296. return R;
  297. };
  298. HeadersMap::iterator Known = findKnownHeader(File);
  299. if (Known != Headers.end()) {
  300. ModuleMap::KnownHeader Result;
  301. // Iterate over all modules that 'File' is part of to find the best fit.
  302. for (KnownHeader &H : Known->second) {
  303. // Prefer a header from the current module over all others.
  304. if (H.getModule()->getTopLevelModule() == CompilingModule)
  305. return MakeResult(H);
  306. // Cannot use a module if it is unavailable.
  307. if (!H.getModule()->isAvailable())
  308. continue;
  309. if (!Result || isBetterKnownHeader(H, Result))
  310. Result = H;
  311. }
  312. return MakeResult(Result);
  313. }
  314. SmallVector<const DirectoryEntry *, 2> SkippedDirs;
  315. KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs);
  316. if (H) {
  317. Module *Result = H.getModule();
  318. // Search up the module stack until we find a module with an umbrella
  319. // directory.
  320. Module *UmbrellaModule = Result;
  321. while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
  322. UmbrellaModule = UmbrellaModule->Parent;
  323. if (UmbrellaModule->InferSubmodules) {
  324. const FileEntry *UmbrellaModuleMap =
  325. getModuleMapFileForUniquing(UmbrellaModule);
  326. // Infer submodules for each of the directories we found between
  327. // the directory of the umbrella header and the directory where
  328. // the actual header is located.
  329. bool Explicit = UmbrellaModule->InferExplicitSubmodules;
  330. for (unsigned I = SkippedDirs.size(); I != 0; --I) {
  331. // Find or create the module that corresponds to this directory name.
  332. SmallString<32> NameBuf;
  333. StringRef Name = sanitizeFilenameAsIdentifier(
  334. llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf);
  335. Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
  336. Explicit).first;
  337. InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
  338. Result->IsInferred = true;
  339. // Associate the module and the directory.
  340. UmbrellaDirs[SkippedDirs[I-1]] = Result;
  341. // If inferred submodules export everything they import, add a
  342. // wildcard to the set of exports.
  343. if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
  344. Result->Exports.push_back(Module::ExportDecl(nullptr, true));
  345. }
  346. // Infer a submodule with the same name as this header file.
  347. SmallString<32> NameBuf;
  348. StringRef Name = sanitizeFilenameAsIdentifier(
  349. llvm::sys::path::stem(File->getName()), NameBuf);
  350. Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
  351. Explicit).first;
  352. InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
  353. Result->IsInferred = true;
  354. Result->addTopHeader(File);
  355. // If inferred submodules export everything they import, add a
  356. // wildcard to the set of exports.
  357. if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
  358. Result->Exports.push_back(Module::ExportDecl(nullptr, true));
  359. } else {
  360. // Record each of the directories we stepped through as being part of
  361. // the module we found, since the umbrella header covers them all.
  362. for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
  363. UmbrellaDirs[SkippedDirs[I]] = Result;
  364. }
  365. Headers[File].push_back(KnownHeader(Result, NormalHeader));
  366. // If a header corresponds to an unavailable module, don't report
  367. // that it maps to anything.
  368. if (!Result->isAvailable())
  369. return KnownHeader();
  370. return MakeResult(Headers[File].back());
  371. }
  372. return KnownHeader();
  373. }
  374. bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
  375. return isHeaderUnavailableInModule(Header, nullptr);
  376. }
  377. bool
  378. ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header,
  379. const Module *RequestingModule) const {
  380. HeadersMap::const_iterator Known = Headers.find(Header);
  381. if (Known != Headers.end()) {
  382. for (SmallVectorImpl<KnownHeader>::const_iterator
  383. I = Known->second.begin(),
  384. E = Known->second.end();
  385. I != E; ++I) {
  386. if (I->isAvailable() && (!RequestingModule ||
  387. I->getModule()->isSubModuleOf(RequestingModule)))
  388. return false;
  389. }
  390. return true;
  391. }
  392. const DirectoryEntry *Dir = Header->getDir();
  393. SmallVector<const DirectoryEntry *, 2> SkippedDirs;
  394. StringRef DirName = Dir->getName();
  395. auto IsUnavailable = [&](const Module *M) {
  396. return !M->isAvailable() && (!RequestingModule ||
  397. M->isSubModuleOf(RequestingModule));
  398. };
  399. // Keep walking up the directory hierarchy, looking for a directory with
  400. // an umbrella header.
  401. do {
  402. llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
  403. = UmbrellaDirs.find(Dir);
  404. if (KnownDir != UmbrellaDirs.end()) {
  405. Module *Found = KnownDir->second;
  406. if (IsUnavailable(Found))
  407. return true;
  408. // Search up the module stack until we find a module with an umbrella
  409. // directory.
  410. Module *UmbrellaModule = Found;
  411. while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
  412. UmbrellaModule = UmbrellaModule->Parent;
  413. if (UmbrellaModule->InferSubmodules) {
  414. for (unsigned I = SkippedDirs.size(); I != 0; --I) {
  415. // Find or create the module that corresponds to this directory name.
  416. SmallString<32> NameBuf;
  417. StringRef Name = sanitizeFilenameAsIdentifier(
  418. llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
  419. NameBuf);
  420. Found = lookupModuleQualified(Name, Found);
  421. if (!Found)
  422. return false;
  423. if (IsUnavailable(Found))
  424. return true;
  425. }
  426. // Infer a submodule with the same name as this header file.
  427. SmallString<32> NameBuf;
  428. StringRef Name = sanitizeFilenameAsIdentifier(
  429. llvm::sys::path::stem(Header->getName()),
  430. NameBuf);
  431. Found = lookupModuleQualified(Name, Found);
  432. if (!Found)
  433. return false;
  434. }
  435. return IsUnavailable(Found);
  436. }
  437. SkippedDirs.push_back(Dir);
  438. // Retrieve our parent path.
  439. DirName = llvm::sys::path::parent_path(DirName);
  440. if (DirName.empty())
  441. break;
  442. // Resolve the parent path to a directory entry.
  443. Dir = SourceMgr.getFileManager().getDirectory(DirName);
  444. } while (Dir);
  445. return false;
  446. }
  447. Module *ModuleMap::findModule(StringRef Name) const {
  448. llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
  449. if (Known != Modules.end())
  450. return Known->getValue();
  451. return nullptr;
  452. }
  453. Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
  454. Module *Context) const {
  455. for(; Context; Context = Context->Parent) {
  456. if (Module *Sub = lookupModuleQualified(Name, Context))
  457. return Sub;
  458. }
  459. return findModule(Name);
  460. }
  461. Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
  462. if (!Context)
  463. return findModule(Name);
  464. return Context->findSubmodule(Name);
  465. }
  466. std::pair<Module *, bool>
  467. ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
  468. bool IsExplicit) {
  469. // Try to find an existing module with this name.
  470. if (Module *Sub = lookupModuleQualified(Name, Parent))
  471. return std::make_pair(Sub, false);
  472. // Create a new module with this name.
  473. Module *Result = new Module(Name, SourceLocation(), Parent,
  474. IsFramework, IsExplicit, NumCreatedModules++);
  475. if (LangOpts.CurrentModule == Name) {
  476. SourceModule = Result;
  477. SourceModuleName = Name;
  478. }
  479. if (!Parent) {
  480. Modules[Name] = Result;
  481. if (!LangOpts.CurrentModule.empty() && !CompilingModule &&
  482. Name == LangOpts.CurrentModule) {
  483. CompilingModule = Result;
  484. }
  485. }
  486. return std::make_pair(Result, true);
  487. }
  488. /// \brief For a framework module, infer the framework against which we
  489. /// should link.
  490. static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
  491. FileManager &FileMgr) {
  492. assert(Mod->IsFramework && "Can only infer linking for framework modules");
  493. assert(!Mod->isSubFramework() &&
  494. "Can only infer linking for top-level frameworks");
  495. SmallString<128> LibName;
  496. LibName += FrameworkDir->getName();
  497. llvm::sys::path::append(LibName, Mod->Name);
  498. if (FileMgr.getFile(LibName)) {
  499. Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
  500. /*IsFramework=*/true));
  501. }
  502. }
  503. Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
  504. bool IsSystem, Module *Parent) {
  505. Attributes Attrs;
  506. Attrs.IsSystem = IsSystem;
  507. return inferFrameworkModule(FrameworkDir, Attrs, Parent);
  508. }
  509. Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
  510. Attributes Attrs, Module *Parent) {
  511. // Note: as an egregious but useful hack we use the real path here, because
  512. // we might be looking at an embedded framework that symlinks out to a
  513. // top-level framework, and we need to infer as if we were naming the
  514. // top-level framework.
  515. StringRef FrameworkDirName =
  516. SourceMgr.getFileManager().getCanonicalName(FrameworkDir);
  517. // In case this is a case-insensitive filesystem, use the canonical
  518. // directory name as the ModuleName, since modules are case-sensitive.
  519. // FIXME: we should be able to give a fix-it hint for the correct spelling.
  520. SmallString<32> ModuleNameStorage;
  521. StringRef ModuleName = sanitizeFilenameAsIdentifier(
  522. llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage);
  523. // Check whether we've already found this module.
  524. if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
  525. return Mod;
  526. FileManager &FileMgr = SourceMgr.getFileManager();
  527. // If the framework has a parent path from which we're allowed to infer
  528. // a framework module, do so.
  529. const FileEntry *ModuleMapFile = nullptr;
  530. if (!Parent) {
  531. // Determine whether we're allowed to infer a module map.
  532. bool canInfer = false;
  533. if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
  534. // Figure out the parent path.
  535. StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
  536. if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
  537. // Check whether we have already looked into the parent directory
  538. // for a module map.
  539. llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
  540. inferred = InferredDirectories.find(ParentDir);
  541. if (inferred == InferredDirectories.end()) {
  542. // We haven't looked here before. Load a module map, if there is
  543. // one.
  544. bool IsFrameworkDir = Parent.endswith(".framework");
  545. if (const FileEntry *ModMapFile =
  546. HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) {
  547. parseModuleMapFile(ModMapFile, Attrs.IsSystem, ParentDir);
  548. inferred = InferredDirectories.find(ParentDir);
  549. }
  550. if (inferred == InferredDirectories.end())
  551. inferred = InferredDirectories.insert(
  552. std::make_pair(ParentDir, InferredDirectory())).first;
  553. }
  554. if (inferred->second.InferModules) {
  555. // We're allowed to infer for this directory, but make sure it's okay
  556. // to infer this particular module.
  557. StringRef Name = llvm::sys::path::stem(FrameworkDirName);
  558. canInfer = std::find(inferred->second.ExcludedModules.begin(),
  559. inferred->second.ExcludedModules.end(),
  560. Name) == inferred->second.ExcludedModules.end();
  561. Attrs.IsSystem |= inferred->second.Attrs.IsSystem;
  562. Attrs.IsExternC |= inferred->second.Attrs.IsExternC;
  563. Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive;
  564. ModuleMapFile = inferred->second.ModuleMapFile;
  565. }
  566. }
  567. }
  568. // If we're not allowed to infer a framework module, don't.
  569. if (!canInfer)
  570. return nullptr;
  571. } else
  572. ModuleMapFile = getModuleMapFileForUniquing(Parent);
  573. // Look for an umbrella header.
  574. SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
  575. llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
  576. const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
  577. // FIXME: If there's no umbrella header, we could probably scan the
  578. // framework to load *everything*. But, it's not clear that this is a good
  579. // idea.
  580. if (!UmbrellaHeader)
  581. return nullptr;
  582. Module *Result = new Module(ModuleName, SourceLocation(), Parent,
  583. /*IsFramework=*/true, /*IsExplicit=*/false,
  584. NumCreatedModules++);
  585. InferredModuleAllowedBy[Result] = ModuleMapFile;
  586. Result->IsInferred = true;
  587. if (LangOpts.CurrentModule == ModuleName) {
  588. SourceModule = Result;
  589. SourceModuleName = ModuleName;
  590. }
  591. Result->IsSystem |= Attrs.IsSystem;
  592. Result->IsExternC |= Attrs.IsExternC;
  593. Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive;
  594. Result->Directory = FrameworkDir;
  595. if (!Parent)
  596. Modules[ModuleName] = Result;
  597. // umbrella header "umbrella-header-name"
  598. //
  599. // The "Headers/" component of the name is implied because this is
  600. // a framework module.
  601. setUmbrellaHeader(Result, UmbrellaHeader, ModuleName + ".h");
  602. // export *
  603. Result->Exports.push_back(Module::ExportDecl(nullptr, true));
  604. // module * { export * }
  605. Result->InferSubmodules = true;
  606. Result->InferExportWildcard = true;
  607. // Look for subframeworks.
  608. std::error_code EC;
  609. SmallString<128> SubframeworksDirName
  610. = StringRef(FrameworkDir->getName());
  611. llvm::sys::path::append(SubframeworksDirName, "Frameworks");
  612. llvm::sys::path::native(SubframeworksDirName);
  613. for (llvm::sys::fs::directory_iterator Dir(SubframeworksDirName, EC), DirEnd;
  614. Dir != DirEnd && !EC; Dir.increment(EC)) {
  615. if (!StringRef(Dir->path()).endswith(".framework"))
  616. continue;
  617. if (const DirectoryEntry *SubframeworkDir
  618. = FileMgr.getDirectory(Dir->path())) {
  619. // Note: as an egregious but useful hack, we use the real path here and
  620. // check whether it is actually a subdirectory of the parent directory.
  621. // This will not be the case if the 'subframework' is actually a symlink
  622. // out to a top-level framework.
  623. StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
  624. bool FoundParent = false;
  625. do {
  626. // Get the parent directory name.
  627. SubframeworkDirName
  628. = llvm::sys::path::parent_path(SubframeworkDirName);
  629. if (SubframeworkDirName.empty())
  630. break;
  631. if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
  632. FoundParent = true;
  633. break;
  634. }
  635. } while (true);
  636. if (!FoundParent)
  637. continue;
  638. // FIXME: Do we want to warn about subframeworks without umbrella headers?
  639. inferFrameworkModule(SubframeworkDir, Attrs, Result);
  640. }
  641. }
  642. // If the module is a top-level framework, automatically link against the
  643. // framework.
  644. if (!Result->isSubFramework()) {
  645. inferFrameworkLink(Result, FrameworkDir, FileMgr);
  646. }
  647. return Result;
  648. }
  649. void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
  650. Twine NameAsWritten) {
  651. Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
  652. Mod->Umbrella = UmbrellaHeader;
  653. Mod->UmbrellaAsWritten = NameAsWritten.str();
  654. UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
  655. }
  656. void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
  657. Twine NameAsWritten) {
  658. Mod->Umbrella = UmbrellaDir;
  659. Mod->UmbrellaAsWritten = NameAsWritten.str();
  660. UmbrellaDirs[UmbrellaDir] = Mod;
  661. }
  662. static Module::HeaderKind headerRoleToKind(ModuleMap::ModuleHeaderRole Role) {
  663. switch ((int)Role) {
  664. default: llvm_unreachable("unknown header role");
  665. case ModuleMap::NormalHeader:
  666. return Module::HK_Normal;
  667. case ModuleMap::PrivateHeader:
  668. return Module::HK_Private;
  669. case ModuleMap::TextualHeader:
  670. return Module::HK_Textual;
  671. case ModuleMap::PrivateHeader | ModuleMap::TextualHeader:
  672. return Module::HK_PrivateTextual;
  673. }
  674. }
  675. void ModuleMap::addHeader(Module *Mod, Module::Header Header,
  676. ModuleHeaderRole Role) {
  677. if (!(Role & TextualHeader)) {
  678. bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
  679. HeaderInfo.MarkFileModuleHeader(Header.Entry, Role,
  680. isCompilingModuleHeader);
  681. }
  682. Headers[Header.Entry].push_back(KnownHeader(Mod, Role));
  683. Mod->Headers[headerRoleToKind(Role)].push_back(std::move(Header));
  684. }
  685. void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) {
  686. // Add this as a known header so we won't implicitly add it to any
  687. // umbrella directory module.
  688. // FIXME: Should we only exclude it from umbrella modules within the
  689. // specified module?
  690. (void) Headers[Header.Entry];
  691. Mod->Headers[Module::HK_Excluded].push_back(std::move(Header));
  692. }
  693. const FileEntry *
  694. ModuleMap::getContainingModuleMapFile(const Module *Module) const {
  695. if (Module->DefinitionLoc.isInvalid())
  696. return nullptr;
  697. return SourceMgr.getFileEntryForID(
  698. SourceMgr.getFileID(Module->DefinitionLoc));
  699. }
  700. const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const {
  701. if (M->IsInferred) {
  702. assert(InferredModuleAllowedBy.count(M) && "missing inferred module map");
  703. return InferredModuleAllowedBy.find(M)->second;
  704. }
  705. return getContainingModuleMapFile(M);
  706. }
  707. void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) {
  708. assert(M->IsInferred && "module not inferred");
  709. InferredModuleAllowedBy[M] = ModMap;
  710. }
  711. void ModuleMap::dump() {
  712. llvm::errs() << "Modules:";
  713. for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
  714. MEnd = Modules.end();
  715. M != MEnd; ++M)
  716. M->getValue()->print(llvm::errs(), 2);
  717. llvm::errs() << "Headers:";
  718. for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
  719. H != HEnd; ++H) {
  720. llvm::errs() << " \"" << H->first->getName() << "\" -> ";
  721. for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
  722. E = H->second.end();
  723. I != E; ++I) {
  724. if (I != H->second.begin())
  725. llvm::errs() << ",";
  726. llvm::errs() << I->getModule()->getFullModuleName();
  727. }
  728. llvm::errs() << "\n";
  729. }
  730. }
  731. bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
  732. auto Unresolved = std::move(Mod->UnresolvedExports);
  733. Mod->UnresolvedExports.clear();
  734. for (auto &UE : Unresolved) {
  735. Module::ExportDecl Export = resolveExport(Mod, UE, Complain);
  736. if (Export.getPointer() || Export.getInt())
  737. Mod->Exports.push_back(Export);
  738. else
  739. Mod->UnresolvedExports.push_back(UE);
  740. }
  741. return !Mod->UnresolvedExports.empty();
  742. }
  743. bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
  744. auto Unresolved = std::move(Mod->UnresolvedDirectUses);
  745. Mod->UnresolvedDirectUses.clear();
  746. for (auto &UDU : Unresolved) {
  747. Module *DirectUse = resolveModuleId(UDU, Mod, Complain);
  748. if (DirectUse)
  749. Mod->DirectUses.push_back(DirectUse);
  750. else
  751. Mod->UnresolvedDirectUses.push_back(UDU);
  752. }
  753. return !Mod->UnresolvedDirectUses.empty();
  754. }
  755. bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
  756. auto Unresolved = std::move(Mod->UnresolvedConflicts);
  757. Mod->UnresolvedConflicts.clear();
  758. for (auto &UC : Unresolved) {
  759. if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) {
  760. Module::Conflict Conflict;
  761. Conflict.Other = OtherMod;
  762. Conflict.Message = UC.Message;
  763. Mod->Conflicts.push_back(Conflict);
  764. } else
  765. Mod->UnresolvedConflicts.push_back(UC);
  766. }
  767. return !Mod->UnresolvedConflicts.empty();
  768. }
  769. Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
  770. if (Loc.isInvalid())
  771. return nullptr;
  772. // Use the expansion location to determine which module we're in.
  773. FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
  774. if (!ExpansionLoc.isFileID())
  775. return nullptr;
  776. const SourceManager &SrcMgr = Loc.getManager();
  777. FileID ExpansionFileID = ExpansionLoc.getFileID();
  778. while (const FileEntry *ExpansionFile
  779. = SrcMgr.getFileEntryForID(ExpansionFileID)) {
  780. // Find the module that owns this header (if any).
  781. if (Module *Mod = findModuleForHeader(ExpansionFile).getModule())
  782. return Mod;
  783. // No module owns this header, so look up the inclusion chain to see if
  784. // any included header has an associated module.
  785. SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
  786. if (IncludeLoc.isInvalid())
  787. return nullptr;
  788. ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
  789. }
  790. return nullptr;
  791. }
  792. //----------------------------------------------------------------------------//
  793. // Module map file parser
  794. //----------------------------------------------------------------------------//
  795. namespace clang {
  796. /// \brief A token in a module map file.
  797. struct MMToken {
  798. enum TokenKind {
  799. Comma,
  800. ConfigMacros,
  801. Conflict,
  802. EndOfFile,
  803. HeaderKeyword,
  804. Identifier,
  805. Exclaim,
  806. ExcludeKeyword,
  807. ExplicitKeyword,
  808. ExportKeyword,
  809. ExternKeyword,
  810. FrameworkKeyword,
  811. LinkKeyword,
  812. ModuleKeyword,
  813. Period,
  814. PrivateKeyword,
  815. UmbrellaKeyword,
  816. UseKeyword,
  817. RequiresKeyword,
  818. Star,
  819. StringLiteral,
  820. TextualKeyword,
  821. LBrace,
  822. RBrace,
  823. LSquare,
  824. RSquare
  825. } Kind;
  826. unsigned Location;
  827. unsigned StringLength;
  828. const char *StringData;
  829. void clear() {
  830. Kind = EndOfFile;
  831. Location = 0;
  832. StringLength = 0;
  833. StringData = nullptr;
  834. }
  835. bool is(TokenKind K) const { return Kind == K; }
  836. SourceLocation getLocation() const {
  837. return SourceLocation::getFromRawEncoding(Location);
  838. }
  839. StringRef getString() const {
  840. return StringRef(StringData, StringLength);
  841. }
  842. };
  843. class ModuleMapParser {
  844. Lexer &L;
  845. SourceManager &SourceMgr;
  846. /// \brief Default target information, used only for string literal
  847. /// parsing.
  848. const TargetInfo *Target;
  849. DiagnosticsEngine &Diags;
  850. ModuleMap &Map;
  851. /// \brief The current module map file.
  852. const FileEntry *ModuleMapFile;
  853. /// \brief The directory that file names in this module map file should
  854. /// be resolved relative to.
  855. const DirectoryEntry *Directory;
  856. /// \brief The directory containing Clang-supplied headers.
  857. const DirectoryEntry *BuiltinIncludeDir;
  858. /// \brief Whether this module map is in a system header directory.
  859. bool IsSystem;
  860. /// \brief Whether an error occurred.
  861. bool HadError;
  862. /// \brief Stores string data for the various string literals referenced
  863. /// during parsing.
  864. llvm::BumpPtrAllocator StringData;
  865. /// \brief The current token.
  866. MMToken Tok;
  867. /// \brief The active module.
  868. Module *ActiveModule;
  869. /// \brief Whether a module uses the 'requires excluded' hack to mark its
  870. /// contents as 'textual'.
  871. ///
  872. /// On older Darwin SDK versions, 'requires excluded' is used to mark the
  873. /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as
  874. /// non-modular headers. For backwards compatibility, we continue to
  875. /// support this idiom for just these modules, and map the headers to
  876. /// 'textual' to match the original intent.
  877. llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack;
  878. /// \brief Consume the current token and return its location.
  879. SourceLocation consumeToken();
  880. /// \brief Skip tokens until we reach the a token with the given kind
  881. /// (or the end of the file).
  882. void skipUntil(MMToken::TokenKind K);
  883. typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
  884. bool parseModuleId(ModuleId &Id);
  885. void parseModuleDecl();
  886. void parseExternModuleDecl();
  887. void parseRequiresDecl();
  888. void parseHeaderDecl(clang::MMToken::TokenKind,
  889. SourceLocation LeadingLoc);
  890. void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
  891. void parseExportDecl();
  892. void parseUseDecl();
  893. void parseLinkDecl();
  894. void parseConfigMacros();
  895. void parseConflict();
  896. void parseInferredModuleDecl(bool Framework, bool Explicit);
  897. typedef ModuleMap::Attributes Attributes;
  898. bool parseOptionalAttributes(Attributes &Attrs);
  899. public:
  900. explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
  901. const TargetInfo *Target,
  902. DiagnosticsEngine &Diags,
  903. ModuleMap &Map,
  904. const FileEntry *ModuleMapFile,
  905. const DirectoryEntry *Directory,
  906. const DirectoryEntry *BuiltinIncludeDir,
  907. bool IsSystem)
  908. : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
  909. ModuleMapFile(ModuleMapFile), Directory(Directory),
  910. BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem),
  911. HadError(false), ActiveModule(nullptr)
  912. {
  913. Tok.clear();
  914. consumeToken();
  915. }
  916. bool parseModuleMapFile();
  917. };
  918. }
  919. SourceLocation ModuleMapParser::consumeToken() {
  920. retry:
  921. SourceLocation Result = Tok.getLocation();
  922. Tok.clear();
  923. Token LToken;
  924. L.LexFromRawLexer(LToken);
  925. Tok.Location = LToken.getLocation().getRawEncoding();
  926. switch (LToken.getKind()) {
  927. case tok::raw_identifier: {
  928. StringRef RI = LToken.getRawIdentifier();
  929. Tok.StringData = RI.data();
  930. Tok.StringLength = RI.size();
  931. Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI)
  932. .Case("config_macros", MMToken::ConfigMacros)
  933. .Case("conflict", MMToken::Conflict)
  934. .Case("exclude", MMToken::ExcludeKeyword)
  935. .Case("explicit", MMToken::ExplicitKeyword)
  936. .Case("export", MMToken::ExportKeyword)
  937. .Case("extern", MMToken::ExternKeyword)
  938. .Case("framework", MMToken::FrameworkKeyword)
  939. .Case("header", MMToken::HeaderKeyword)
  940. .Case("link", MMToken::LinkKeyword)
  941. .Case("module", MMToken::ModuleKeyword)
  942. .Case("private", MMToken::PrivateKeyword)
  943. .Case("requires", MMToken::RequiresKeyword)
  944. .Case("textual", MMToken::TextualKeyword)
  945. .Case("umbrella", MMToken::UmbrellaKeyword)
  946. .Case("use", MMToken::UseKeyword)
  947. .Default(MMToken::Identifier);
  948. break;
  949. }
  950. case tok::comma:
  951. Tok.Kind = MMToken::Comma;
  952. break;
  953. case tok::eof:
  954. Tok.Kind = MMToken::EndOfFile;
  955. break;
  956. case tok::l_brace:
  957. Tok.Kind = MMToken::LBrace;
  958. break;
  959. case tok::l_square:
  960. Tok.Kind = MMToken::LSquare;
  961. break;
  962. case tok::period:
  963. Tok.Kind = MMToken::Period;
  964. break;
  965. case tok::r_brace:
  966. Tok.Kind = MMToken::RBrace;
  967. break;
  968. case tok::r_square:
  969. Tok.Kind = MMToken::RSquare;
  970. break;
  971. case tok::star:
  972. Tok.Kind = MMToken::Star;
  973. break;
  974. case tok::exclaim:
  975. Tok.Kind = MMToken::Exclaim;
  976. break;
  977. case tok::string_literal: {
  978. if (LToken.hasUDSuffix()) {
  979. Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
  980. HadError = true;
  981. goto retry;
  982. }
  983. // Parse the string literal.
  984. LangOptions LangOpts;
  985. StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target);
  986. if (StringLiteral.hadError)
  987. goto retry;
  988. // Copy the string literal into our string data allocator.
  989. unsigned Length = StringLiteral.GetStringLength();
  990. char *Saved = StringData.Allocate<char>(Length + 1);
  991. memcpy(Saved, StringLiteral.GetString().data(), Length);
  992. Saved[Length] = 0;
  993. // Form the token.
  994. Tok.Kind = MMToken::StringLiteral;
  995. Tok.StringData = Saved;
  996. Tok.StringLength = Length;
  997. break;
  998. }
  999. case tok::comment:
  1000. goto retry;
  1001. default:
  1002. Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
  1003. HadError = true;
  1004. goto retry;
  1005. }
  1006. return Result;
  1007. }
  1008. void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
  1009. unsigned braceDepth = 0;
  1010. unsigned squareDepth = 0;
  1011. do {
  1012. switch (Tok.Kind) {
  1013. case MMToken::EndOfFile:
  1014. return;
  1015. case MMToken::LBrace:
  1016. if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
  1017. return;
  1018. ++braceDepth;
  1019. break;
  1020. case MMToken::LSquare:
  1021. if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
  1022. return;
  1023. ++squareDepth;
  1024. break;
  1025. case MMToken::RBrace:
  1026. if (braceDepth > 0)
  1027. --braceDepth;
  1028. else if (Tok.is(K))
  1029. return;
  1030. break;
  1031. case MMToken::RSquare:
  1032. if (squareDepth > 0)
  1033. --squareDepth;
  1034. else if (Tok.is(K))
  1035. return;
  1036. break;
  1037. default:
  1038. if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
  1039. return;
  1040. break;
  1041. }
  1042. consumeToken();
  1043. } while (true);
  1044. }
  1045. /// \brief Parse a module-id.
  1046. ///
  1047. /// module-id:
  1048. /// identifier
  1049. /// identifier '.' module-id
  1050. ///
  1051. /// \returns true if an error occurred, false otherwise.
  1052. bool ModuleMapParser::parseModuleId(ModuleId &Id) {
  1053. Id.clear();
  1054. do {
  1055. if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) {
  1056. Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
  1057. consumeToken();
  1058. } else {
  1059. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
  1060. return true;
  1061. }
  1062. if (!Tok.is(MMToken::Period))
  1063. break;
  1064. consumeToken();
  1065. } while (true);
  1066. return false;
  1067. }
  1068. namespace {
  1069. /// \brief Enumerates the known attributes.
  1070. enum AttributeKind {
  1071. /// \brief An unknown attribute.
  1072. AT_unknown,
  1073. /// \brief The 'system' attribute.
  1074. AT_system,
  1075. /// \brief The 'extern_c' attribute.
  1076. AT_extern_c,
  1077. /// \brief The 'exhaustive' attribute.
  1078. AT_exhaustive
  1079. };
  1080. }
  1081. /// \brief Parse a module declaration.
  1082. ///
  1083. /// module-declaration:
  1084. /// 'extern' 'module' module-id string-literal
  1085. /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
  1086. /// { module-member* }
  1087. ///
  1088. /// module-member:
  1089. /// requires-declaration
  1090. /// header-declaration
  1091. /// submodule-declaration
  1092. /// export-declaration
  1093. /// link-declaration
  1094. ///
  1095. /// submodule-declaration:
  1096. /// module-declaration
  1097. /// inferred-submodule-declaration
  1098. void ModuleMapParser::parseModuleDecl() {
  1099. assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
  1100. Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
  1101. if (Tok.is(MMToken::ExternKeyword)) {
  1102. parseExternModuleDecl();
  1103. return;
  1104. }
  1105. // Parse 'explicit' or 'framework' keyword, if present.
  1106. SourceLocation ExplicitLoc;
  1107. bool Explicit = false;
  1108. bool Framework = false;
  1109. // Parse 'explicit' keyword, if present.
  1110. if (Tok.is(MMToken::ExplicitKeyword)) {
  1111. ExplicitLoc = consumeToken();
  1112. Explicit = true;
  1113. }
  1114. // Parse 'framework' keyword, if present.
  1115. if (Tok.is(MMToken::FrameworkKeyword)) {
  1116. consumeToken();
  1117. Framework = true;
  1118. }
  1119. // Parse 'module' keyword.
  1120. if (!Tok.is(MMToken::ModuleKeyword)) {
  1121. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
  1122. consumeToken();
  1123. HadError = true;
  1124. return;
  1125. }
  1126. consumeToken(); // 'module' keyword
  1127. // If we have a wildcard for the module name, this is an inferred submodule.
  1128. // Parse it.
  1129. if (Tok.is(MMToken::Star))
  1130. return parseInferredModuleDecl(Framework, Explicit);
  1131. // Parse the module name.
  1132. ModuleId Id;
  1133. if (parseModuleId(Id)) {
  1134. HadError = true;
  1135. return;
  1136. }
  1137. if (ActiveModule) {
  1138. if (Id.size() > 1) {
  1139. Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
  1140. << SourceRange(Id.front().second, Id.back().second);
  1141. HadError = true;
  1142. return;
  1143. }
  1144. } else if (Id.size() == 1 && Explicit) {
  1145. // Top-level modules can't be explicit.
  1146. Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
  1147. Explicit = false;
  1148. ExplicitLoc = SourceLocation();
  1149. HadError = true;
  1150. }
  1151. Module *PreviousActiveModule = ActiveModule;
  1152. if (Id.size() > 1) {
  1153. // This module map defines a submodule. Go find the module of which it
  1154. // is a submodule.
  1155. ActiveModule = nullptr;
  1156. const Module *TopLevelModule = nullptr;
  1157. for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
  1158. if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
  1159. if (I == 0)
  1160. TopLevelModule = Next;
  1161. ActiveModule = Next;
  1162. continue;
  1163. }
  1164. if (ActiveModule) {
  1165. Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
  1166. << Id[I].first
  1167. << ActiveModule->getTopLevelModule()->getFullModuleName();
  1168. } else {
  1169. Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
  1170. }
  1171. HadError = true;
  1172. return;
  1173. }
  1174. if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) {
  1175. assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) &&
  1176. "submodule defined in same file as 'module *' that allowed its "
  1177. "top-level module");
  1178. Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile);
  1179. }
  1180. }
  1181. StringRef ModuleName = Id.back().first;
  1182. SourceLocation ModuleNameLoc = Id.back().second;
  1183. // Parse the optional attribute list.
  1184. Attributes Attrs;
  1185. parseOptionalAttributes(Attrs);
  1186. // Parse the opening brace.
  1187. if (!Tok.is(MMToken::LBrace)) {
  1188. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
  1189. << ModuleName;
  1190. HadError = true;
  1191. return;
  1192. }
  1193. SourceLocation LBraceLoc = consumeToken();
  1194. // Determine whether this (sub)module has already been defined.
  1195. if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
  1196. if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
  1197. // Skip the module definition.
  1198. skipUntil(MMToken::RBrace);
  1199. if (Tok.is(MMToken::RBrace))
  1200. consumeToken();
  1201. else {
  1202. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  1203. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  1204. HadError = true;
  1205. }
  1206. return;
  1207. }
  1208. Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
  1209. << ModuleName;
  1210. Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
  1211. // Skip the module definition.
  1212. skipUntil(MMToken::RBrace);
  1213. if (Tok.is(MMToken::RBrace))
  1214. consumeToken();
  1215. HadError = true;
  1216. return;
  1217. }
  1218. // Start defining this module.
  1219. ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
  1220. Explicit).first;
  1221. ActiveModule->DefinitionLoc = ModuleNameLoc;
  1222. if (Attrs.IsSystem || IsSystem)
  1223. ActiveModule->IsSystem = true;
  1224. if (Attrs.IsExternC)
  1225. ActiveModule->IsExternC = true;
  1226. ActiveModule->Directory = Directory;
  1227. bool Done = false;
  1228. do {
  1229. switch (Tok.Kind) {
  1230. case MMToken::EndOfFile:
  1231. case MMToken::RBrace:
  1232. Done = true;
  1233. break;
  1234. case MMToken::ConfigMacros:
  1235. parseConfigMacros();
  1236. break;
  1237. case MMToken::Conflict:
  1238. parseConflict();
  1239. break;
  1240. case MMToken::ExplicitKeyword:
  1241. case MMToken::ExternKeyword:
  1242. case MMToken::FrameworkKeyword:
  1243. case MMToken::ModuleKeyword:
  1244. parseModuleDecl();
  1245. break;
  1246. case MMToken::ExportKeyword:
  1247. parseExportDecl();
  1248. break;
  1249. case MMToken::UseKeyword:
  1250. parseUseDecl();
  1251. break;
  1252. case MMToken::RequiresKeyword:
  1253. parseRequiresDecl();
  1254. break;
  1255. case MMToken::TextualKeyword:
  1256. parseHeaderDecl(MMToken::TextualKeyword, consumeToken());
  1257. break;
  1258. case MMToken::UmbrellaKeyword: {
  1259. SourceLocation UmbrellaLoc = consumeToken();
  1260. if (Tok.is(MMToken::HeaderKeyword))
  1261. parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
  1262. else
  1263. parseUmbrellaDirDecl(UmbrellaLoc);
  1264. break;
  1265. }
  1266. case MMToken::ExcludeKeyword:
  1267. parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken());
  1268. break;
  1269. case MMToken::PrivateKeyword:
  1270. parseHeaderDecl(MMToken::PrivateKeyword, consumeToken());
  1271. break;
  1272. case MMToken::HeaderKeyword:
  1273. parseHeaderDecl(MMToken::HeaderKeyword, consumeToken());
  1274. break;
  1275. case MMToken::LinkKeyword:
  1276. parseLinkDecl();
  1277. break;
  1278. default:
  1279. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
  1280. consumeToken();
  1281. break;
  1282. }
  1283. } while (!Done);
  1284. if (Tok.is(MMToken::RBrace))
  1285. consumeToken();
  1286. else {
  1287. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  1288. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  1289. HadError = true;
  1290. }
  1291. // If the active module is a top-level framework, and there are no link
  1292. // libraries, automatically link against the framework.
  1293. if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
  1294. ActiveModule->LinkLibraries.empty()) {
  1295. inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
  1296. }
  1297. // If the module meets all requirements but is still unavailable, mark the
  1298. // whole tree as unavailable to prevent it from building.
  1299. if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement &&
  1300. ActiveModule->Parent) {
  1301. ActiveModule->getTopLevelModule()->markUnavailable();
  1302. ActiveModule->getTopLevelModule()->MissingHeaders.append(
  1303. ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end());
  1304. }
  1305. // We're done parsing this module. Pop back to the previous module.
  1306. ActiveModule = PreviousActiveModule;
  1307. }
  1308. /// \brief Parse an extern module declaration.
  1309. ///
  1310. /// extern module-declaration:
  1311. /// 'extern' 'module' module-id string-literal
  1312. void ModuleMapParser::parseExternModuleDecl() {
  1313. assert(Tok.is(MMToken::ExternKeyword));
  1314. SourceLocation ExternLoc = consumeToken(); // 'extern' keyword
  1315. // Parse 'module' keyword.
  1316. if (!Tok.is(MMToken::ModuleKeyword)) {
  1317. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
  1318. consumeToken();
  1319. HadError = true;
  1320. return;
  1321. }
  1322. consumeToken(); // 'module' keyword
  1323. // Parse the module name.
  1324. ModuleId Id;
  1325. if (parseModuleId(Id)) {
  1326. HadError = true;
  1327. return;
  1328. }
  1329. // Parse the referenced module map file name.
  1330. if (!Tok.is(MMToken::StringLiteral)) {
  1331. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
  1332. HadError = true;
  1333. return;
  1334. }
  1335. std::string FileName = Tok.getString();
  1336. consumeToken(); // filename
  1337. StringRef FileNameRef = FileName;
  1338. SmallString<128> ModuleMapFileName;
  1339. if (llvm::sys::path::is_relative(FileNameRef)) {
  1340. ModuleMapFileName += Directory->getName();
  1341. llvm::sys::path::append(ModuleMapFileName, FileName);
  1342. FileNameRef = ModuleMapFileName;
  1343. }
  1344. if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef))
  1345. Map.parseModuleMapFile(
  1346. File, /*IsSystem=*/false,
  1347. Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd
  1348. ? Directory
  1349. : File->getDir(), ExternLoc);
  1350. }
  1351. /// Whether to add the requirement \p Feature to the module \p M.
  1352. ///
  1353. /// This preserves backwards compatibility for two hacks in the Darwin system
  1354. /// module map files:
  1355. ///
  1356. /// 1. The use of 'requires excluded' to make headers non-modular, which
  1357. /// should really be mapped to 'textual' now that we have this feature. We
  1358. /// drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to
  1359. /// true. Later, this bit will be used to map all the headers inside this
  1360. /// module to 'textual'.
  1361. ///
  1362. /// This affects Darwin.C.excluded (for assert.h) and Tcl.Private.
  1363. ///
  1364. /// 2. Removes a bogus cplusplus requirement from IOKit.avc. This requirement
  1365. /// was never correct and causes issues now that we check it, so drop it.
  1366. static bool shouldAddRequirement(Module *M, StringRef Feature,
  1367. bool &IsRequiresExcludedHack) {
  1368. static const StringRef DarwinCExcluded[] = {"Darwin", "C", "excluded"};
  1369. static const StringRef TclPrivate[] = {"Tcl", "Private"};
  1370. static const StringRef IOKitAVC[] = {"IOKit", "avc"};
  1371. if (Feature == "excluded" && (M->fullModuleNameIs(DarwinCExcluded) ||
  1372. M->fullModuleNameIs(TclPrivate))) {
  1373. IsRequiresExcludedHack = true;
  1374. return false;
  1375. } else if (Feature == "cplusplus" && M->fullModuleNameIs(IOKitAVC)) {
  1376. return false;
  1377. }
  1378. return true;
  1379. }
  1380. /// \brief Parse a requires declaration.
  1381. ///
  1382. /// requires-declaration:
  1383. /// 'requires' feature-list
  1384. ///
  1385. /// feature-list:
  1386. /// feature ',' feature-list
  1387. /// feature
  1388. ///
  1389. /// feature:
  1390. /// '!'[opt] identifier
  1391. void ModuleMapParser::parseRequiresDecl() {
  1392. assert(Tok.is(MMToken::RequiresKeyword));
  1393. // Parse 'requires' keyword.
  1394. consumeToken();
  1395. // Parse the feature-list.
  1396. do {
  1397. bool RequiredState = true;
  1398. if (Tok.is(MMToken::Exclaim)) {
  1399. RequiredState = false;
  1400. consumeToken();
  1401. }
  1402. if (!Tok.is(MMToken::Identifier)) {
  1403. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
  1404. HadError = true;
  1405. return;
  1406. }
  1407. // Consume the feature name.
  1408. std::string Feature = Tok.getString();
  1409. consumeToken();
  1410. bool IsRequiresExcludedHack = false;
  1411. bool ShouldAddRequirement =
  1412. shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack);
  1413. if (IsRequiresExcludedHack)
  1414. UsesRequiresExcludedHack.insert(ActiveModule);
  1415. if (ShouldAddRequirement) {
  1416. // Add this feature.
  1417. ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts,
  1418. *Map.Target);
  1419. }
  1420. if (!Tok.is(MMToken::Comma))
  1421. break;
  1422. // Consume the comma.
  1423. consumeToken();
  1424. } while (true);
  1425. }
  1426. /// \brief Append to \p Paths the set of paths needed to get to the
  1427. /// subframework in which the given module lives.
  1428. static void appendSubframeworkPaths(Module *Mod,
  1429. SmallVectorImpl<char> &Path) {
  1430. // Collect the framework names from the given module to the top-level module.
  1431. SmallVector<StringRef, 2> Paths;
  1432. for (; Mod; Mod = Mod->Parent) {
  1433. if (Mod->IsFramework)
  1434. Paths.push_back(Mod->Name);
  1435. }
  1436. if (Paths.empty())
  1437. return;
  1438. // Add Frameworks/Name.framework for each subframework.
  1439. for (unsigned I = Paths.size() - 1; I != 0; --I)
  1440. llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework");
  1441. }
  1442. /// \brief Parse a header declaration.
  1443. ///
  1444. /// header-declaration:
  1445. /// 'textual'[opt] 'header' string-literal
  1446. /// 'private' 'textual'[opt] 'header' string-literal
  1447. /// 'exclude' 'header' string-literal
  1448. /// 'umbrella' 'header' string-literal
  1449. ///
  1450. /// FIXME: Support 'private textual header'.
  1451. void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
  1452. SourceLocation LeadingLoc) {
  1453. // We've already consumed the first token.
  1454. ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
  1455. if (LeadingToken == MMToken::PrivateKeyword) {
  1456. Role = ModuleMap::PrivateHeader;
  1457. // 'private' may optionally be followed by 'textual'.
  1458. if (Tok.is(MMToken::TextualKeyword)) {
  1459. LeadingToken = Tok.Kind;
  1460. consumeToken();
  1461. }
  1462. }
  1463. if (LeadingToken == MMToken::TextualKeyword)
  1464. Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
  1465. if (UsesRequiresExcludedHack.count(ActiveModule)) {
  1466. // Mark this header 'textual' (see doc comment for
  1467. // Module::UsesRequiresExcludedHack).
  1468. Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
  1469. }
  1470. if (LeadingToken != MMToken::HeaderKeyword) {
  1471. if (!Tok.is(MMToken::HeaderKeyword)) {
  1472. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  1473. << (LeadingToken == MMToken::PrivateKeyword ? "private" :
  1474. LeadingToken == MMToken::ExcludeKeyword ? "exclude" :
  1475. LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella");
  1476. return;
  1477. }
  1478. consumeToken();
  1479. }
  1480. // Parse the header name.
  1481. if (!Tok.is(MMToken::StringLiteral)) {
  1482. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  1483. << "header";
  1484. HadError = true;
  1485. return;
  1486. }
  1487. Module::UnresolvedHeaderDirective Header;
  1488. Header.FileName = Tok.getString();
  1489. Header.FileNameLoc = consumeToken();
  1490. // Check whether we already have an umbrella.
  1491. if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) {
  1492. Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
  1493. << ActiveModule->getFullModuleName();
  1494. HadError = true;
  1495. return;
  1496. }
  1497. // Look for this file.
  1498. const FileEntry *File = nullptr;
  1499. const FileEntry *BuiltinFile = nullptr;
  1500. SmallString<128> RelativePathName;
  1501. if (llvm::sys::path::is_absolute(Header.FileName)) {
  1502. RelativePathName = Header.FileName;
  1503. File = SourceMgr.getFileManager().getFile(RelativePathName);
  1504. } else {
  1505. // Search for the header file within the search directory.
  1506. SmallString<128> FullPathName(Directory->getName());
  1507. unsigned FullPathLength = FullPathName.size();
  1508. if (ActiveModule->isPartOfFramework()) {
  1509. appendSubframeworkPaths(ActiveModule, RelativePathName);
  1510. // Check whether this file is in the public headers.
  1511. llvm::sys::path::append(RelativePathName, "Headers", Header.FileName);
  1512. llvm::sys::path::append(FullPathName, RelativePathName);
  1513. File = SourceMgr.getFileManager().getFile(FullPathName);
  1514. if (!File) {
  1515. // Check whether this file is in the private headers.
  1516. // FIXME: Should we retain the subframework paths here?
  1517. RelativePathName.clear();
  1518. FullPathName.resize(FullPathLength);
  1519. llvm::sys::path::append(RelativePathName, "PrivateHeaders",
  1520. Header.FileName);
  1521. llvm::sys::path::append(FullPathName, RelativePathName);
  1522. File = SourceMgr.getFileManager().getFile(FullPathName);
  1523. }
  1524. } else {
  1525. // Lookup for normal headers.
  1526. llvm::sys::path::append(RelativePathName, Header.FileName);
  1527. llvm::sys::path::append(FullPathName, RelativePathName);
  1528. File = SourceMgr.getFileManager().getFile(FullPathName);
  1529. // If this is a system module with a top-level header, this header
  1530. // may have a counterpart (or replacement) in the set of headers
  1531. // supplied by Clang. Find that builtin header.
  1532. if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword &&
  1533. BuiltinIncludeDir && BuiltinIncludeDir != Directory &&
  1534. isBuiltinHeader(Header.FileName)) {
  1535. SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
  1536. llvm::sys::path::append(BuiltinPathName, Header.FileName);
  1537. BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
  1538. // If Clang supplies this header but the underlying system does not,
  1539. // just silently swap in our builtin version. Otherwise, we'll end
  1540. // up adding both (later).
  1541. //
  1542. // For local visibility, entirely replace the system file with our
  1543. // one and textually include the system one. We need to pass macros
  1544. // from our header to the system one if we #include_next it.
  1545. //
  1546. // FIXME: Can we do this in all cases?
  1547. if (BuiltinFile && (!File || Map.LangOpts.ModulesLocalVisibility)) {
  1548. File = BuiltinFile;
  1549. RelativePathName = BuiltinPathName;
  1550. BuiltinFile = nullptr;
  1551. }
  1552. }
  1553. }
  1554. }
  1555. // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
  1556. // Come up with a lazy way to do this.
  1557. if (File) {
  1558. if (LeadingToken == MMToken::UmbrellaKeyword) {
  1559. const DirectoryEntry *UmbrellaDir = File->getDir();
  1560. if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
  1561. Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash)
  1562. << UmbrellaModule->getFullModuleName();
  1563. HadError = true;
  1564. } else {
  1565. // Record this umbrella header.
  1566. Map.setUmbrellaHeader(ActiveModule, File, RelativePathName.str());
  1567. }
  1568. } else if (LeadingToken == MMToken::ExcludeKeyword) {
  1569. Module::Header H = {RelativePathName.str(), File};
  1570. Map.excludeHeader(ActiveModule, H);
  1571. } else {
  1572. // If there is a builtin counterpart to this file, add it now, before
  1573. // the "real" header, so we build the built-in one first when building
  1574. // the module.
  1575. if (BuiltinFile) {
  1576. // FIXME: Taking the name from the FileEntry is unstable and can give
  1577. // different results depending on how we've previously named that file
  1578. // in this build.
  1579. Module::Header H = { BuiltinFile->getName(), BuiltinFile };
  1580. Map.addHeader(ActiveModule, H, Role);
  1581. }
  1582. // Record this header.
  1583. Module::Header H = { RelativePathName.str(), File };
  1584. Map.addHeader(ActiveModule, H, Role);
  1585. }
  1586. } else if (LeadingToken != MMToken::ExcludeKeyword) {
  1587. // Ignore excluded header files. They're optional anyway.
  1588. // If we find a module that has a missing header, we mark this module as
  1589. // unavailable and store the header directive for displaying diagnostics.
  1590. Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword;
  1591. ActiveModule->markUnavailable();
  1592. ActiveModule->MissingHeaders.push_back(Header);
  1593. }
  1594. }
  1595. static int compareModuleHeaders(const Module::Header *A,
  1596. const Module::Header *B) {
  1597. return A->NameAsWritten.compare(B->NameAsWritten);
  1598. }
  1599. /// \brief Parse an umbrella directory declaration.
  1600. ///
  1601. /// umbrella-dir-declaration:
  1602. /// umbrella string-literal
  1603. void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
  1604. // Parse the directory name.
  1605. if (!Tok.is(MMToken::StringLiteral)) {
  1606. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  1607. << "umbrella";
  1608. HadError = true;
  1609. return;
  1610. }
  1611. std::string DirName = Tok.getString();
  1612. SourceLocation DirNameLoc = consumeToken();
  1613. // Check whether we already have an umbrella.
  1614. if (ActiveModule->Umbrella) {
  1615. Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
  1616. << ActiveModule->getFullModuleName();
  1617. HadError = true;
  1618. return;
  1619. }
  1620. // Look for this file.
  1621. const DirectoryEntry *Dir = nullptr;
  1622. if (llvm::sys::path::is_absolute(DirName))
  1623. Dir = SourceMgr.getFileManager().getDirectory(DirName);
  1624. else {
  1625. SmallString<128> PathName;
  1626. PathName = Directory->getName();
  1627. llvm::sys::path::append(PathName, DirName);
  1628. Dir = SourceMgr.getFileManager().getDirectory(PathName);
  1629. }
  1630. if (!Dir) {
  1631. Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
  1632. << DirName;
  1633. HadError = true;
  1634. return;
  1635. }
  1636. if (UsesRequiresExcludedHack.count(ActiveModule)) {
  1637. // Mark this header 'textual' (see doc comment for
  1638. // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the
  1639. // directory is relatively expensive, in practice this only applies to the
  1640. // uncommonly used Tcl module on Darwin platforms.
  1641. std::error_code EC;
  1642. SmallVector<Module::Header, 6> Headers;
  1643. for (llvm::sys::fs::recursive_directory_iterator I(Dir->getName(), EC), E;
  1644. I != E && !EC; I.increment(EC)) {
  1645. if (const FileEntry *FE = SourceMgr.getFileManager().getFile(I->path())) {
  1646. Module::Header Header = {I->path(), FE};
  1647. Headers.push_back(std::move(Header));
  1648. }
  1649. }
  1650. // Sort header paths so that the pcm doesn't depend on iteration order.
  1651. llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders);
  1652. for (auto &Header : Headers)
  1653. Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader);
  1654. return;
  1655. }
  1656. if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
  1657. Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
  1658. << OwningModule->getFullModuleName();
  1659. HadError = true;
  1660. return;
  1661. }
  1662. // Record this umbrella directory.
  1663. Map.setUmbrellaDir(ActiveModule, Dir, DirName);
  1664. }
  1665. /// \brief Parse a module export declaration.
  1666. ///
  1667. /// export-declaration:
  1668. /// 'export' wildcard-module-id
  1669. ///
  1670. /// wildcard-module-id:
  1671. /// identifier
  1672. /// '*'
  1673. /// identifier '.' wildcard-module-id
  1674. void ModuleMapParser::parseExportDecl() {
  1675. assert(Tok.is(MMToken::ExportKeyword));
  1676. SourceLocation ExportLoc = consumeToken();
  1677. // Parse the module-id with an optional wildcard at the end.
  1678. ModuleId ParsedModuleId;
  1679. bool Wildcard = false;
  1680. do {
  1681. // FIXME: Support string-literal module names here.
  1682. if (Tok.is(MMToken::Identifier)) {
  1683. ParsedModuleId.push_back(std::make_pair(Tok.getString(),
  1684. Tok.getLocation()));
  1685. consumeToken();
  1686. if (Tok.is(MMToken::Period)) {
  1687. consumeToken();
  1688. continue;
  1689. }
  1690. break;
  1691. }
  1692. if(Tok.is(MMToken::Star)) {
  1693. Wildcard = true;
  1694. consumeToken();
  1695. break;
  1696. }
  1697. Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
  1698. HadError = true;
  1699. return;
  1700. } while (true);
  1701. Module::UnresolvedExportDecl Unresolved = {
  1702. ExportLoc, ParsedModuleId, Wildcard
  1703. };
  1704. ActiveModule->UnresolvedExports.push_back(Unresolved);
  1705. }
  1706. /// \brief Parse a module use declaration.
  1707. ///
  1708. /// use-declaration:
  1709. /// 'use' wildcard-module-id
  1710. void ModuleMapParser::parseUseDecl() {
  1711. assert(Tok.is(MMToken::UseKeyword));
  1712. auto KWLoc = consumeToken();
  1713. // Parse the module-id.
  1714. ModuleId ParsedModuleId;
  1715. parseModuleId(ParsedModuleId);
  1716. if (ActiveModule->Parent)
  1717. Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule);
  1718. else
  1719. ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
  1720. }
  1721. /// \brief Parse a link declaration.
  1722. ///
  1723. /// module-declaration:
  1724. /// 'link' 'framework'[opt] string-literal
  1725. void ModuleMapParser::parseLinkDecl() {
  1726. assert(Tok.is(MMToken::LinkKeyword));
  1727. SourceLocation LinkLoc = consumeToken();
  1728. // Parse the optional 'framework' keyword.
  1729. bool IsFramework = false;
  1730. if (Tok.is(MMToken::FrameworkKeyword)) {
  1731. consumeToken();
  1732. IsFramework = true;
  1733. }
  1734. // Parse the library name
  1735. if (!Tok.is(MMToken::StringLiteral)) {
  1736. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
  1737. << IsFramework << SourceRange(LinkLoc);
  1738. HadError = true;
  1739. return;
  1740. }
  1741. std::string LibraryName = Tok.getString();
  1742. consumeToken();
  1743. ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
  1744. IsFramework));
  1745. }
  1746. /// \brief Parse a configuration macro declaration.
  1747. ///
  1748. /// module-declaration:
  1749. /// 'config_macros' attributes[opt] config-macro-list?
  1750. ///
  1751. /// config-macro-list:
  1752. /// identifier (',' identifier)?
  1753. void ModuleMapParser::parseConfigMacros() {
  1754. assert(Tok.is(MMToken::ConfigMacros));
  1755. SourceLocation ConfigMacrosLoc = consumeToken();
  1756. // Only top-level modules can have configuration macros.
  1757. if (ActiveModule->Parent) {
  1758. Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
  1759. }
  1760. // Parse the optional attributes.
  1761. Attributes Attrs;
  1762. parseOptionalAttributes(Attrs);
  1763. if (Attrs.IsExhaustive && !ActiveModule->Parent) {
  1764. ActiveModule->ConfigMacrosExhaustive = true;
  1765. }
  1766. // If we don't have an identifier, we're done.
  1767. // FIXME: Support macros with the same name as a keyword here.
  1768. if (!Tok.is(MMToken::Identifier))
  1769. return;
  1770. // Consume the first identifier.
  1771. if (!ActiveModule->Parent) {
  1772. ActiveModule->ConfigMacros.push_back(Tok.getString().str());
  1773. }
  1774. consumeToken();
  1775. do {
  1776. // If there's a comma, consume it.
  1777. if (!Tok.is(MMToken::Comma))
  1778. break;
  1779. consumeToken();
  1780. // We expect to see a macro name here.
  1781. // FIXME: Support macros with the same name as a keyword here.
  1782. if (!Tok.is(MMToken::Identifier)) {
  1783. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
  1784. break;
  1785. }
  1786. // Consume the macro name.
  1787. if (!ActiveModule->Parent) {
  1788. ActiveModule->ConfigMacros.push_back(Tok.getString().str());
  1789. }
  1790. consumeToken();
  1791. } while (true);
  1792. }
  1793. /// \brief Format a module-id into a string.
  1794. static std::string formatModuleId(const ModuleId &Id) {
  1795. std::string result;
  1796. {
  1797. llvm::raw_string_ostream OS(result);
  1798. for (unsigned I = 0, N = Id.size(); I != N; ++I) {
  1799. if (I)
  1800. OS << ".";
  1801. OS << Id[I].first;
  1802. }
  1803. }
  1804. return result;
  1805. }
  1806. /// \brief Parse a conflict declaration.
  1807. ///
  1808. /// module-declaration:
  1809. /// 'conflict' module-id ',' string-literal
  1810. void ModuleMapParser::parseConflict() {
  1811. assert(Tok.is(MMToken::Conflict));
  1812. SourceLocation ConflictLoc = consumeToken();
  1813. Module::UnresolvedConflict Conflict;
  1814. // Parse the module-id.
  1815. if (parseModuleId(Conflict.Id))
  1816. return;
  1817. // Parse the ','.
  1818. if (!Tok.is(MMToken::Comma)) {
  1819. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
  1820. << SourceRange(ConflictLoc);
  1821. return;
  1822. }
  1823. consumeToken();
  1824. // Parse the message.
  1825. if (!Tok.is(MMToken::StringLiteral)) {
  1826. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
  1827. << formatModuleId(Conflict.Id);
  1828. return;
  1829. }
  1830. Conflict.Message = Tok.getString().str();
  1831. consumeToken();
  1832. // Add this unresolved conflict.
  1833. ActiveModule->UnresolvedConflicts.push_back(Conflict);
  1834. }
  1835. /// \brief Parse an inferred module declaration (wildcard modules).
  1836. ///
  1837. /// module-declaration:
  1838. /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
  1839. /// { inferred-module-member* }
  1840. ///
  1841. /// inferred-module-member:
  1842. /// 'export' '*'
  1843. /// 'exclude' identifier
  1844. void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
  1845. assert(Tok.is(MMToken::Star));
  1846. SourceLocation StarLoc = consumeToken();
  1847. bool Failed = false;
  1848. // Inferred modules must be submodules.
  1849. if (!ActiveModule && !Framework) {
  1850. Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
  1851. Failed = true;
  1852. }
  1853. if (ActiveModule) {
  1854. // Inferred modules must have umbrella directories.
  1855. if (!Failed && ActiveModule->IsAvailable &&
  1856. !ActiveModule->getUmbrellaDir()) {
  1857. Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
  1858. Failed = true;
  1859. }
  1860. // Check for redefinition of an inferred module.
  1861. if (!Failed && ActiveModule->InferSubmodules) {
  1862. Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
  1863. if (ActiveModule->InferredSubmoduleLoc.isValid())
  1864. Diags.Report(ActiveModule->InferredSubmoduleLoc,
  1865. diag::note_mmap_prev_definition);
  1866. Failed = true;
  1867. }
  1868. // Check for the 'framework' keyword, which is not permitted here.
  1869. if (Framework) {
  1870. Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
  1871. Framework = false;
  1872. }
  1873. } else if (Explicit) {
  1874. Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
  1875. Explicit = false;
  1876. }
  1877. // If there were any problems with this inferred submodule, skip its body.
  1878. if (Failed) {
  1879. if (Tok.is(MMToken::LBrace)) {
  1880. consumeToken();
  1881. skipUntil(MMToken::RBrace);
  1882. if (Tok.is(MMToken::RBrace))
  1883. consumeToken();
  1884. }
  1885. HadError = true;
  1886. return;
  1887. }
  1888. // Parse optional attributes.
  1889. Attributes Attrs;
  1890. parseOptionalAttributes(Attrs);
  1891. if (ActiveModule) {
  1892. // Note that we have an inferred submodule.
  1893. ActiveModule->InferSubmodules = true;
  1894. ActiveModule->InferredSubmoduleLoc = StarLoc;
  1895. ActiveModule->InferExplicitSubmodules = Explicit;
  1896. } else {
  1897. // We'll be inferring framework modules for this directory.
  1898. Map.InferredDirectories[Directory].InferModules = true;
  1899. Map.InferredDirectories[Directory].Attrs = Attrs;
  1900. Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile;
  1901. // FIXME: Handle the 'framework' keyword.
  1902. }
  1903. // Parse the opening brace.
  1904. if (!Tok.is(MMToken::LBrace)) {
  1905. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
  1906. HadError = true;
  1907. return;
  1908. }
  1909. SourceLocation LBraceLoc = consumeToken();
  1910. // Parse the body of the inferred submodule.
  1911. bool Done = false;
  1912. do {
  1913. switch (Tok.Kind) {
  1914. case MMToken::EndOfFile:
  1915. case MMToken::RBrace:
  1916. Done = true;
  1917. break;
  1918. case MMToken::ExcludeKeyword: {
  1919. if (ActiveModule) {
  1920. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
  1921. << (ActiveModule != nullptr);
  1922. consumeToken();
  1923. break;
  1924. }
  1925. consumeToken();
  1926. // FIXME: Support string-literal module names here.
  1927. if (!Tok.is(MMToken::Identifier)) {
  1928. Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
  1929. break;
  1930. }
  1931. Map.InferredDirectories[Directory].ExcludedModules
  1932. .push_back(Tok.getString());
  1933. consumeToken();
  1934. break;
  1935. }
  1936. case MMToken::ExportKeyword:
  1937. if (!ActiveModule) {
  1938. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
  1939. << (ActiveModule != nullptr);
  1940. consumeToken();
  1941. break;
  1942. }
  1943. consumeToken();
  1944. if (Tok.is(MMToken::Star))
  1945. ActiveModule->InferExportWildcard = true;
  1946. else
  1947. Diags.Report(Tok.getLocation(),
  1948. diag::err_mmap_expected_export_wildcard);
  1949. consumeToken();
  1950. break;
  1951. case MMToken::ExplicitKeyword:
  1952. case MMToken::ModuleKeyword:
  1953. case MMToken::HeaderKeyword:
  1954. case MMToken::PrivateKeyword:
  1955. case MMToken::UmbrellaKeyword:
  1956. default:
  1957. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
  1958. << (ActiveModule != nullptr);
  1959. consumeToken();
  1960. break;
  1961. }
  1962. } while (!Done);
  1963. if (Tok.is(MMToken::RBrace))
  1964. consumeToken();
  1965. else {
  1966. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  1967. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  1968. HadError = true;
  1969. }
  1970. }
  1971. /// \brief Parse optional attributes.
  1972. ///
  1973. /// attributes:
  1974. /// attribute attributes
  1975. /// attribute
  1976. ///
  1977. /// attribute:
  1978. /// [ identifier ]
  1979. ///
  1980. /// \param Attrs Will be filled in with the parsed attributes.
  1981. ///
  1982. /// \returns true if an error occurred, false otherwise.
  1983. bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
  1984. bool HadError = false;
  1985. while (Tok.is(MMToken::LSquare)) {
  1986. // Consume the '['.
  1987. SourceLocation LSquareLoc = consumeToken();
  1988. // Check whether we have an attribute name here.
  1989. if (!Tok.is(MMToken::Identifier)) {
  1990. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
  1991. skipUntil(MMToken::RSquare);
  1992. if (Tok.is(MMToken::RSquare))
  1993. consumeToken();
  1994. HadError = true;
  1995. }
  1996. // Decode the attribute name.
  1997. AttributeKind Attribute
  1998. = llvm::StringSwitch<AttributeKind>(Tok.getString())
  1999. .Case("exhaustive", AT_exhaustive)
  2000. .Case("extern_c", AT_extern_c)
  2001. .Case("system", AT_system)
  2002. .Default(AT_unknown);
  2003. switch (Attribute) {
  2004. case AT_unknown:
  2005. Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
  2006. << Tok.getString();
  2007. break;
  2008. case AT_system:
  2009. Attrs.IsSystem = true;
  2010. break;
  2011. case AT_extern_c:
  2012. Attrs.IsExternC = true;
  2013. break;
  2014. case AT_exhaustive:
  2015. Attrs.IsExhaustive = true;
  2016. break;
  2017. }
  2018. consumeToken();
  2019. // Consume the ']'.
  2020. if (!Tok.is(MMToken::RSquare)) {
  2021. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
  2022. Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
  2023. skipUntil(MMToken::RSquare);
  2024. HadError = true;
  2025. }
  2026. if (Tok.is(MMToken::RSquare))
  2027. consumeToken();
  2028. }
  2029. return HadError;
  2030. }
  2031. /// \brief Parse a module map file.
  2032. ///
  2033. /// module-map-file:
  2034. /// module-declaration*
  2035. bool ModuleMapParser::parseModuleMapFile() {
  2036. do {
  2037. switch (Tok.Kind) {
  2038. case MMToken::EndOfFile:
  2039. return HadError;
  2040. case MMToken::ExplicitKeyword:
  2041. case MMToken::ExternKeyword:
  2042. case MMToken::ModuleKeyword:
  2043. case MMToken::FrameworkKeyword:
  2044. parseModuleDecl();
  2045. break;
  2046. case MMToken::Comma:
  2047. case MMToken::ConfigMacros:
  2048. case MMToken::Conflict:
  2049. case MMToken::Exclaim:
  2050. case MMToken::ExcludeKeyword:
  2051. case MMToken::ExportKeyword:
  2052. case MMToken::HeaderKeyword:
  2053. case MMToken::Identifier:
  2054. case MMToken::LBrace:
  2055. case MMToken::LinkKeyword:
  2056. case MMToken::LSquare:
  2057. case MMToken::Period:
  2058. case MMToken::PrivateKeyword:
  2059. case MMToken::RBrace:
  2060. case MMToken::RSquare:
  2061. case MMToken::RequiresKeyword:
  2062. case MMToken::Star:
  2063. case MMToken::StringLiteral:
  2064. case MMToken::TextualKeyword:
  2065. case MMToken::UmbrellaKeyword:
  2066. case MMToken::UseKeyword:
  2067. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
  2068. HadError = true;
  2069. consumeToken();
  2070. break;
  2071. }
  2072. } while (true);
  2073. }
  2074. bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
  2075. const DirectoryEntry *Dir,
  2076. SourceLocation ExternModuleLoc) {
  2077. llvm::DenseMap<const FileEntry *, bool>::iterator Known
  2078. = ParsedModuleMap.find(File);
  2079. if (Known != ParsedModuleMap.end())
  2080. return Known->second;
  2081. assert(Target && "Missing target information");
  2082. auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
  2083. FileID ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter);
  2084. const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID);
  2085. if (!Buffer)
  2086. return ParsedModuleMap[File] = true;
  2087. // Parse this module map file.
  2088. Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts);
  2089. SourceLocation Start = L.getSourceLocation();
  2090. ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir,
  2091. BuiltinIncludeDir, IsSystem);
  2092. bool Result = Parser.parseModuleMapFile();
  2093. ParsedModuleMap[File] = Result;
  2094. // Notify callbacks that we parsed it.
  2095. for (const auto &Cb : Callbacks)
  2096. Cb->moduleMapFileRead(Start, *File, IsSystem);
  2097. return Result;
  2098. }