ModuleMap.cpp 70 KB

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