ModuleMap.cpp 74 KB

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