ModuleMap.cpp 73 KB

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