ModuleMap.cpp 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098
  1. //===- ModuleMap.cpp - Describe the layout of modules ---------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the ModuleMap implementation, which describes the layout
  10. // of a module as it relates to headers.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Lex/ModuleMap.h"
  14. #include "clang/Basic/CharInfo.h"
  15. #include "clang/Basic/Diagnostic.h"
  16. #include "clang/Basic/FileManager.h"
  17. #include "clang/Basic/LLVM.h"
  18. #include "clang/Basic/LangOptions.h"
  19. #include "clang/Basic/Module.h"
  20. #include "clang/Basic/SourceLocation.h"
  21. #include "clang/Basic/SourceManager.h"
  22. #include "clang/Basic/TargetInfo.h"
  23. #include "clang/Lex/HeaderSearch.h"
  24. #include "clang/Lex/HeaderSearchOptions.h"
  25. #include "clang/Lex/LexDiagnostic.h"
  26. #include "clang/Lex/Lexer.h"
  27. #include "clang/Lex/LiteralSupport.h"
  28. #include "clang/Lex/Token.h"
  29. #include "llvm/ADT/DenseMap.h"
  30. #include "llvm/ADT/STLExtras.h"
  31. #include "llvm/ADT/SmallPtrSet.h"
  32. #include "llvm/ADT/SmallString.h"
  33. #include "llvm/ADT/SmallVector.h"
  34. #include "llvm/ADT/StringMap.h"
  35. #include "llvm/ADT/StringRef.h"
  36. #include "llvm/ADT/StringSwitch.h"
  37. #include "llvm/Support/Allocator.h"
  38. #include "llvm/Support/Compiler.h"
  39. #include "llvm/Support/ErrorHandling.h"
  40. #include "llvm/Support/MemoryBuffer.h"
  41. #include "llvm/Support/Path.h"
  42. #include "llvm/Support/VirtualFileSystem.h"
  43. #include "llvm/Support/raw_ostream.h"
  44. #include <algorithm>
  45. #include <cassert>
  46. #include <cstdint>
  47. #include <cstring>
  48. #include <optional>
  49. #include <string>
  50. #include <system_error>
  51. #include <utility>
  52. using namespace clang;
  53. void ModuleMapCallbacks::anchor() {}
  54. void ModuleMap::resolveLinkAsDependencies(Module *Mod) {
  55. auto PendingLinkAs = PendingLinkAsModule.find(Mod->Name);
  56. if (PendingLinkAs != PendingLinkAsModule.end()) {
  57. for (auto &Name : PendingLinkAs->second) {
  58. auto *M = findModule(Name.getKey());
  59. if (M)
  60. M->UseExportAsModuleLinkName = true;
  61. }
  62. }
  63. }
  64. void ModuleMap::addLinkAsDependency(Module *Mod) {
  65. if (findModule(Mod->ExportAsModule))
  66. Mod->UseExportAsModuleLinkName = true;
  67. else
  68. PendingLinkAsModule[Mod->ExportAsModule].insert(Mod->Name);
  69. }
  70. Module::HeaderKind ModuleMap::headerRoleToKind(ModuleHeaderRole Role) {
  71. switch ((int)Role) {
  72. case NormalHeader:
  73. return Module::HK_Normal;
  74. case PrivateHeader:
  75. return Module::HK_Private;
  76. case TextualHeader:
  77. return Module::HK_Textual;
  78. case PrivateHeader | TextualHeader:
  79. return Module::HK_PrivateTextual;
  80. case ExcludedHeader:
  81. return Module::HK_Excluded;
  82. }
  83. llvm_unreachable("unknown header role");
  84. }
  85. ModuleMap::ModuleHeaderRole
  86. ModuleMap::headerKindToRole(Module::HeaderKind Kind) {
  87. switch ((int)Kind) {
  88. case Module::HK_Normal:
  89. return NormalHeader;
  90. case Module::HK_Private:
  91. return PrivateHeader;
  92. case Module::HK_Textual:
  93. return TextualHeader;
  94. case Module::HK_PrivateTextual:
  95. return ModuleHeaderRole(PrivateHeader | TextualHeader);
  96. case Module::HK_Excluded:
  97. return ExcludedHeader;
  98. }
  99. llvm_unreachable("unknown header kind");
  100. }
  101. bool ModuleMap::isModular(ModuleHeaderRole Role) {
  102. return !(Role & (ModuleMap::TextualHeader | ModuleMap::ExcludedHeader));
  103. }
  104. Module::ExportDecl
  105. ModuleMap::resolveExport(Module *Mod,
  106. const Module::UnresolvedExportDecl &Unresolved,
  107. bool Complain) const {
  108. // We may have just a wildcard.
  109. if (Unresolved.Id.empty()) {
  110. assert(Unresolved.Wildcard && "Invalid unresolved export");
  111. return Module::ExportDecl(nullptr, true);
  112. }
  113. // Resolve the module-id.
  114. Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain);
  115. if (!Context)
  116. return {};
  117. return Module::ExportDecl(Context, Unresolved.Wildcard);
  118. }
  119. Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod,
  120. bool Complain) const {
  121. // Find the starting module.
  122. Module *Context = lookupModuleUnqualified(Id[0].first, Mod);
  123. if (!Context) {
  124. if (Complain)
  125. Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified)
  126. << Id[0].first << Mod->getFullModuleName();
  127. return nullptr;
  128. }
  129. // Dig into the module path.
  130. for (unsigned I = 1, N = Id.size(); I != N; ++I) {
  131. Module *Sub = lookupModuleQualified(Id[I].first, Context);
  132. if (!Sub) {
  133. if (Complain)
  134. Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
  135. << Id[I].first << Context->getFullModuleName()
  136. << SourceRange(Id[0].second, Id[I-1].second);
  137. return nullptr;
  138. }
  139. Context = Sub;
  140. }
  141. return Context;
  142. }
  143. /// Append to \p Paths the set of paths needed to get to the
  144. /// subframework in which the given module lives.
  145. static void appendSubframeworkPaths(Module *Mod,
  146. SmallVectorImpl<char> &Path) {
  147. // Collect the framework names from the given module to the top-level module.
  148. SmallVector<StringRef, 2> Paths;
  149. for (; Mod; Mod = Mod->Parent) {
  150. if (Mod->IsFramework)
  151. Paths.push_back(Mod->Name);
  152. }
  153. if (Paths.empty())
  154. return;
  155. // Add Frameworks/Name.framework for each subframework.
  156. for (StringRef Framework : llvm::drop_begin(llvm::reverse(Paths)))
  157. llvm::sys::path::append(Path, "Frameworks", Framework + ".framework");
  158. }
  159. OptionalFileEntryRef ModuleMap::findHeader(
  160. Module *M, const Module::UnresolvedHeaderDirective &Header,
  161. SmallVectorImpl<char> &RelativePathName, bool &NeedsFramework) {
  162. // Search for the header file within the module's home directory.
  163. auto *Directory = M->Directory;
  164. SmallString<128> FullPathName(Directory->getName());
  165. auto GetFile = [&](StringRef Filename) -> OptionalFileEntryRef {
  166. auto File =
  167. expectedToOptional(SourceMgr.getFileManager().getFileRef(Filename));
  168. if (!File || (Header.Size && File->getSize() != *Header.Size) ||
  169. (Header.ModTime && File->getModificationTime() != *Header.ModTime))
  170. return std::nullopt;
  171. return *File;
  172. };
  173. auto GetFrameworkFile = [&]() -> OptionalFileEntryRef {
  174. unsigned FullPathLength = FullPathName.size();
  175. appendSubframeworkPaths(M, RelativePathName);
  176. unsigned RelativePathLength = RelativePathName.size();
  177. // Check whether this file is in the public headers.
  178. llvm::sys::path::append(RelativePathName, "Headers", Header.FileName);
  179. llvm::sys::path::append(FullPathName, RelativePathName);
  180. if (auto File = GetFile(FullPathName))
  181. return File;
  182. // Check whether this file is in the private headers.
  183. // Ideally, private modules in the form 'FrameworkName.Private' should
  184. // be defined as 'module FrameworkName.Private', and not as
  185. // 'framework module FrameworkName.Private', since a 'Private.Framework'
  186. // does not usually exist. However, since both are currently widely used
  187. // for private modules, make sure we find the right path in both cases.
  188. if (M->IsFramework && M->Name == "Private")
  189. RelativePathName.clear();
  190. else
  191. RelativePathName.resize(RelativePathLength);
  192. FullPathName.resize(FullPathLength);
  193. llvm::sys::path::append(RelativePathName, "PrivateHeaders",
  194. Header.FileName);
  195. llvm::sys::path::append(FullPathName, RelativePathName);
  196. return GetFile(FullPathName);
  197. };
  198. if (llvm::sys::path::is_absolute(Header.FileName)) {
  199. RelativePathName.clear();
  200. RelativePathName.append(Header.FileName.begin(), Header.FileName.end());
  201. return GetFile(Header.FileName);
  202. }
  203. if (M->isPartOfFramework())
  204. return GetFrameworkFile();
  205. // Lookup for normal headers.
  206. llvm::sys::path::append(RelativePathName, Header.FileName);
  207. llvm::sys::path::append(FullPathName, RelativePathName);
  208. auto NormalHdrFile = GetFile(FullPathName);
  209. if (!NormalHdrFile && Directory->getName().endswith(".framework")) {
  210. // The lack of 'framework' keyword in a module declaration it's a simple
  211. // mistake we can diagnose when the header exists within the proper
  212. // framework style path.
  213. FullPathName.assign(Directory->getName());
  214. RelativePathName.clear();
  215. if (GetFrameworkFile()) {
  216. Diags.Report(Header.FileNameLoc,
  217. diag::warn_mmap_incomplete_framework_module_declaration)
  218. << Header.FileName << M->getFullModuleName();
  219. NeedsFramework = true;
  220. }
  221. return std::nullopt;
  222. }
  223. return NormalHdrFile;
  224. }
  225. void ModuleMap::resolveHeader(Module *Mod,
  226. const Module::UnresolvedHeaderDirective &Header,
  227. bool &NeedsFramework) {
  228. SmallString<128> RelativePathName;
  229. if (OptionalFileEntryRef File =
  230. findHeader(Mod, Header, RelativePathName, NeedsFramework)) {
  231. if (Header.IsUmbrella) {
  232. const DirectoryEntry *UmbrellaDir = &File->getDir().getDirEntry();
  233. if (Module *UmbrellaMod = UmbrellaDirs[UmbrellaDir])
  234. Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
  235. << UmbrellaMod->getFullModuleName();
  236. else
  237. // Record this umbrella header.
  238. setUmbrellaHeader(Mod, *File, Header.FileName, RelativePathName.str());
  239. } else {
  240. Module::Header H = {Header.FileName, std::string(RelativePathName.str()),
  241. *File};
  242. addHeader(Mod, H, headerKindToRole(Header.Kind));
  243. }
  244. } else if (Header.HasBuiltinHeader && !Header.Size && !Header.ModTime) {
  245. // There's a builtin header but no corresponding on-disk header. Assume
  246. // this was supposed to modularize the builtin header alone.
  247. } else if (Header.Kind == Module::HK_Excluded) {
  248. // Ignore missing excluded header files. They're optional anyway.
  249. } else {
  250. // If we find a module that has a missing header, we mark this module as
  251. // unavailable and store the header directive for displaying diagnostics.
  252. Mod->MissingHeaders.push_back(Header);
  253. // A missing header with stat information doesn't make the module
  254. // unavailable; this keeps our behavior consistent as headers are lazily
  255. // resolved. (Such a module still can't be built though, except from
  256. // preprocessed source.)
  257. if (!Header.Size && !Header.ModTime)
  258. Mod->markUnavailable(/*Unimportable=*/false);
  259. }
  260. }
  261. bool ModuleMap::resolveAsBuiltinHeader(
  262. Module *Mod, const Module::UnresolvedHeaderDirective &Header) {
  263. if (Header.Kind == Module::HK_Excluded ||
  264. llvm::sys::path::is_absolute(Header.FileName) ||
  265. Mod->isPartOfFramework() || !Mod->IsSystem || Header.IsUmbrella ||
  266. !BuiltinIncludeDir || BuiltinIncludeDir == Mod->Directory ||
  267. !isBuiltinHeader(Header.FileName))
  268. return false;
  269. // This is a system module with a top-level header. This header
  270. // may have a counterpart (or replacement) in the set of headers
  271. // supplied by Clang. Find that builtin header.
  272. SmallString<128> Path;
  273. llvm::sys::path::append(Path, BuiltinIncludeDir->getName(), Header.FileName);
  274. auto File = SourceMgr.getFileManager().getOptionalFileRef(Path);
  275. if (!File)
  276. return false;
  277. auto Role = headerKindToRole(Header.Kind);
  278. Module::Header H = {Header.FileName, std::string(Path.str()), *File};
  279. addHeader(Mod, H, Role);
  280. return true;
  281. }
  282. ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
  283. const LangOptions &LangOpts, const TargetInfo *Target,
  284. HeaderSearch &HeaderInfo)
  285. : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
  286. HeaderInfo(HeaderInfo) {
  287. MMapLangOpts.LineComment = true;
  288. }
  289. ModuleMap::~ModuleMap() {
  290. for (auto &M : Modules)
  291. delete M.getValue();
  292. for (auto *M : ShadowModules)
  293. delete M;
  294. }
  295. void ModuleMap::setTarget(const TargetInfo &Target) {
  296. assert((!this->Target || this->Target == &Target) &&
  297. "Improper target override");
  298. this->Target = &Target;
  299. }
  300. /// "Sanitize" a filename so that it can be used as an identifier.
  301. static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
  302. SmallVectorImpl<char> &Buffer) {
  303. if (Name.empty())
  304. return Name;
  305. if (!isValidAsciiIdentifier(Name)) {
  306. // If we don't already have something with the form of an identifier,
  307. // create a buffer with the sanitized name.
  308. Buffer.clear();
  309. if (isDigit(Name[0]))
  310. Buffer.push_back('_');
  311. Buffer.reserve(Buffer.size() + Name.size());
  312. for (unsigned I = 0, N = Name.size(); I != N; ++I) {
  313. if (isAsciiIdentifierContinue(Name[I]))
  314. Buffer.push_back(Name[I]);
  315. else
  316. Buffer.push_back('_');
  317. }
  318. Name = StringRef(Buffer.data(), Buffer.size());
  319. }
  320. while (llvm::StringSwitch<bool>(Name)
  321. #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
  322. #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
  323. #include "clang/Basic/TokenKinds.def"
  324. .Default(false)) {
  325. if (Name.data() != Buffer.data())
  326. Buffer.append(Name.begin(), Name.end());
  327. Buffer.push_back('_');
  328. Name = StringRef(Buffer.data(), Buffer.size());
  329. }
  330. return Name;
  331. }
  332. /// Determine whether the given file name is the name of a builtin
  333. /// header, supplied by Clang to replace, override, or augment existing system
  334. /// headers.
  335. bool ModuleMap::isBuiltinHeader(StringRef FileName) {
  336. return llvm::StringSwitch<bool>(FileName)
  337. .Case("float.h", true)
  338. .Case("iso646.h", true)
  339. .Case("limits.h", true)
  340. .Case("stdalign.h", true)
  341. .Case("stdarg.h", true)
  342. .Case("stdatomic.h", true)
  343. .Case("stdbool.h", true)
  344. .Case("stddef.h", true)
  345. .Case("stdint.h", true)
  346. .Case("tgmath.h", true)
  347. .Case("unwind.h", true)
  348. .Default(false);
  349. }
  350. bool ModuleMap::isBuiltinHeader(const FileEntry *File) {
  351. return File->getDir() == BuiltinIncludeDir &&
  352. ModuleMap::isBuiltinHeader(llvm::sys::path::filename(File->getName()));
  353. }
  354. ModuleMap::HeadersMap::iterator
  355. ModuleMap::findKnownHeader(const FileEntry *File) {
  356. resolveHeaderDirectives(File);
  357. HeadersMap::iterator Known = Headers.find(File);
  358. if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps &&
  359. Known == Headers.end() && ModuleMap::isBuiltinHeader(File)) {
  360. HeaderInfo.loadTopLevelSystemModules();
  361. return Headers.find(File);
  362. }
  363. return Known;
  364. }
  365. ModuleMap::KnownHeader
  366. ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
  367. SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) {
  368. if (UmbrellaDirs.empty())
  369. return {};
  370. const DirectoryEntry *Dir = File->getDir();
  371. assert(Dir && "file in no directory");
  372. // Note: as an egregious but useful hack we use the real path here, because
  373. // frameworks moving from top-level frameworks to embedded frameworks tend
  374. // to be symlinked from the top-level location to the embedded location,
  375. // and we need to resolve lookups as if we had found the embedded location.
  376. StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
  377. // Keep walking up the directory hierarchy, looking for a directory with
  378. // an umbrella header.
  379. do {
  380. auto KnownDir = UmbrellaDirs.find(Dir);
  381. if (KnownDir != UmbrellaDirs.end())
  382. return KnownHeader(KnownDir->second, NormalHeader);
  383. IntermediateDirs.push_back(Dir);
  384. // Retrieve our parent path.
  385. DirName = llvm::sys::path::parent_path(DirName);
  386. if (DirName.empty())
  387. break;
  388. // Resolve the parent path to a directory entry.
  389. if (auto DirEntry = SourceMgr.getFileManager().getDirectory(DirName))
  390. Dir = *DirEntry;
  391. else
  392. Dir = nullptr;
  393. } while (Dir);
  394. return {};
  395. }
  396. static bool violatesPrivateInclude(Module *RequestingModule,
  397. const FileEntry *IncFileEnt,
  398. ModuleMap::KnownHeader Header) {
  399. #ifndef NDEBUG
  400. if (Header.getRole() & ModuleMap::PrivateHeader) {
  401. // Check for consistency between the module header role
  402. // as obtained from the lookup and as obtained from the module.
  403. // This check is not cheap, so enable it only for debugging.
  404. bool IsPrivate = false;
  405. SmallVectorImpl<Module::Header> *HeaderList[] = {
  406. &Header.getModule()->Headers[Module::HK_Private],
  407. &Header.getModule()->Headers[Module::HK_PrivateTextual]};
  408. for (auto *Hs : HeaderList)
  409. IsPrivate |= llvm::any_of(
  410. *Hs, [&](const Module::Header &H) { return H.Entry == IncFileEnt; });
  411. assert(IsPrivate && "inconsistent headers and roles");
  412. }
  413. #endif
  414. return !Header.isAccessibleFrom(RequestingModule);
  415. }
  416. static Module *getTopLevelOrNull(Module *M) {
  417. return M ? M->getTopLevelModule() : nullptr;
  418. }
  419. void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule,
  420. bool RequestingModuleIsModuleInterface,
  421. SourceLocation FilenameLoc,
  422. StringRef Filename, FileEntryRef File) {
  423. // No errors for indirect modules. This may be a bit of a problem for modules
  424. // with no source files.
  425. if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule))
  426. return;
  427. if (RequestingModule) {
  428. resolveUses(RequestingModule, /*Complain=*/false);
  429. resolveHeaderDirectives(RequestingModule, /*File=*/std::nullopt);
  430. }
  431. bool Excluded = false;
  432. Module *Private = nullptr;
  433. Module *NotUsed = nullptr;
  434. HeadersMap::iterator Known = findKnownHeader(File);
  435. if (Known != Headers.end()) {
  436. for (const KnownHeader &Header : Known->second) {
  437. // Excluded headers don't really belong to a module.
  438. if (Header.getRole() == ModuleMap::ExcludedHeader) {
  439. Excluded = true;
  440. continue;
  441. }
  442. // Remember private headers for later printing of a diagnostic.
  443. if (violatesPrivateInclude(RequestingModule, File, Header)) {
  444. Private = Header.getModule();
  445. continue;
  446. }
  447. // If uses need to be specified explicitly, we are only allowed to return
  448. // modules that are explicitly used by the requesting module.
  449. if (RequestingModule && LangOpts.ModulesDeclUse &&
  450. !RequestingModule->directlyUses(Header.getModule())) {
  451. NotUsed = Header.getModule();
  452. continue;
  453. }
  454. // We have found a module that we can happily use.
  455. return;
  456. }
  457. Excluded = true;
  458. }
  459. // We have found a header, but it is private.
  460. if (Private) {
  461. Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module)
  462. << Filename;
  463. return;
  464. }
  465. // We have found a module, but we don't use it.
  466. if (NotUsed) {
  467. Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
  468. << RequestingModule->getTopLevelModule()->Name << Filename;
  469. return;
  470. }
  471. if (Excluded || isHeaderInUmbrellaDirs(File))
  472. return;
  473. // At this point, only non-modular includes remain.
  474. if (RequestingModule && LangOpts.ModulesStrictDeclUse) {
  475. Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
  476. << RequestingModule->getTopLevelModule()->Name << Filename;
  477. } else if (RequestingModule && RequestingModuleIsModuleInterface &&
  478. LangOpts.isCompilingModule()) {
  479. // Do not diagnose when we are not compiling a module.
  480. diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ?
  481. diag::warn_non_modular_include_in_framework_module :
  482. diag::warn_non_modular_include_in_module;
  483. Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName()
  484. << File.getName();
  485. }
  486. }
  487. static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New,
  488. const ModuleMap::KnownHeader &Old) {
  489. // Prefer available modules.
  490. // FIXME: Considering whether the module is available rather than merely
  491. // importable is non-hermetic and can result in surprising behavior for
  492. // prebuilt modules. Consider only checking for importability here.
  493. if (New.getModule()->isAvailable() && !Old.getModule()->isAvailable())
  494. return true;
  495. // Prefer a public header over a private header.
  496. if ((New.getRole() & ModuleMap::PrivateHeader) !=
  497. (Old.getRole() & ModuleMap::PrivateHeader))
  498. return !(New.getRole() & ModuleMap::PrivateHeader);
  499. // Prefer a non-textual header over a textual header.
  500. if ((New.getRole() & ModuleMap::TextualHeader) !=
  501. (Old.getRole() & ModuleMap::TextualHeader))
  502. return !(New.getRole() & ModuleMap::TextualHeader);
  503. // Prefer a non-excluded header over an excluded header.
  504. if ((New.getRole() == ModuleMap::ExcludedHeader) !=
  505. (Old.getRole() == ModuleMap::ExcludedHeader))
  506. return New.getRole() != ModuleMap::ExcludedHeader;
  507. // Don't have a reason to choose between these. Just keep the first one.
  508. return false;
  509. }
  510. ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File,
  511. bool AllowTextual,
  512. bool AllowExcluded) {
  513. auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader {
  514. if (!AllowTextual && R.getRole() & ModuleMap::TextualHeader)
  515. return {};
  516. return R;
  517. };
  518. HeadersMap::iterator Known = findKnownHeader(File);
  519. if (Known != Headers.end()) {
  520. ModuleMap::KnownHeader Result;
  521. // Iterate over all modules that 'File' is part of to find the best fit.
  522. for (KnownHeader &H : Known->second) {
  523. // Cannot use a module if the header is excluded in it.
  524. if (!AllowExcluded && H.getRole() == ModuleMap::ExcludedHeader)
  525. continue;
  526. // Prefer a header from the source module over all others.
  527. if (H.getModule()->getTopLevelModule() == SourceModule)
  528. return MakeResult(H);
  529. if (!Result || isBetterKnownHeader(H, Result))
  530. Result = H;
  531. }
  532. return MakeResult(Result);
  533. }
  534. return MakeResult(findOrCreateModuleForHeaderInUmbrellaDir(File));
  535. }
  536. ModuleMap::KnownHeader
  537. ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) {
  538. assert(!Headers.count(File) && "already have a module for this header");
  539. SmallVector<const DirectoryEntry *, 2> SkippedDirs;
  540. KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs);
  541. if (H) {
  542. Module *Result = H.getModule();
  543. // Search up the module stack until we find a module with an umbrella
  544. // directory.
  545. Module *UmbrellaModule = Result;
  546. while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
  547. UmbrellaModule = UmbrellaModule->Parent;
  548. if (UmbrellaModule->InferSubmodules) {
  549. OptionalFileEntryRefDegradesToFileEntryPtr UmbrellaModuleMap =
  550. getModuleMapFileForUniquing(UmbrellaModule);
  551. // Infer submodules for each of the directories we found between
  552. // the directory of the umbrella header and the directory where
  553. // the actual header is located.
  554. bool Explicit = UmbrellaModule->InferExplicitSubmodules;
  555. for (const DirectoryEntry *SkippedDir : llvm::reverse(SkippedDirs)) {
  556. // Find or create the module that corresponds to this directory name.
  557. SmallString<32> NameBuf;
  558. StringRef Name = sanitizeFilenameAsIdentifier(
  559. llvm::sys::path::stem(SkippedDir->getName()), NameBuf);
  560. Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
  561. Explicit).first;
  562. InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
  563. Result->IsInferred = true;
  564. // Associate the module and the directory.
  565. UmbrellaDirs[SkippedDir] = Result;
  566. // If inferred submodules export everything they import, add a
  567. // wildcard to the set of exports.
  568. if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
  569. Result->Exports.push_back(Module::ExportDecl(nullptr, true));
  570. }
  571. // Infer a submodule with the same name as this header file.
  572. SmallString<32> NameBuf;
  573. StringRef Name = sanitizeFilenameAsIdentifier(
  574. llvm::sys::path::stem(File->getName()), NameBuf);
  575. Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
  576. Explicit).first;
  577. InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
  578. Result->IsInferred = true;
  579. Result->addTopHeader(File);
  580. // If inferred submodules export everything they import, add a
  581. // wildcard to the set of exports.
  582. if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
  583. Result->Exports.push_back(Module::ExportDecl(nullptr, true));
  584. } else {
  585. // Record each of the directories we stepped through as being part of
  586. // the module we found, since the umbrella header covers them all.
  587. for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
  588. UmbrellaDirs[SkippedDirs[I]] = Result;
  589. }
  590. KnownHeader Header(Result, NormalHeader);
  591. Headers[File].push_back(Header);
  592. return Header;
  593. }
  594. return {};
  595. }
  596. ArrayRef<ModuleMap::KnownHeader>
  597. ModuleMap::findAllModulesForHeader(const FileEntry *File) {
  598. HeadersMap::iterator Known = findKnownHeader(File);
  599. if (Known != Headers.end())
  600. return Known->second;
  601. if (findOrCreateModuleForHeaderInUmbrellaDir(File))
  602. return Headers.find(File)->second;
  603. return std::nullopt;
  604. }
  605. ArrayRef<ModuleMap::KnownHeader>
  606. ModuleMap::findResolvedModulesForHeader(const FileEntry *File) const {
  607. // FIXME: Is this necessary?
  608. resolveHeaderDirectives(File);
  609. auto It = Headers.find(File);
  610. if (It == Headers.end())
  611. return std::nullopt;
  612. return It->second;
  613. }
  614. bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
  615. return isHeaderUnavailableInModule(Header, nullptr);
  616. }
  617. bool
  618. ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header,
  619. const Module *RequestingModule) const {
  620. resolveHeaderDirectives(Header);
  621. HeadersMap::const_iterator Known = Headers.find(Header);
  622. if (Known != Headers.end()) {
  623. for (SmallVectorImpl<KnownHeader>::const_iterator
  624. I = Known->second.begin(),
  625. E = Known->second.end();
  626. I != E; ++I) {
  627. if (I->getRole() == ModuleMap::ExcludedHeader)
  628. continue;
  629. if (I->isAvailable() &&
  630. (!RequestingModule ||
  631. I->getModule()->isSubModuleOf(RequestingModule))) {
  632. // When no requesting module is available, the caller is looking if a
  633. // header is part a module by only looking into the module map. This is
  634. // done by warn_uncovered_module_header checks; don't consider textual
  635. // headers part of it in this mode, otherwise we get misleading warnings
  636. // that a umbrella header is not including a textual header.
  637. if (!RequestingModule && I->getRole() == ModuleMap::TextualHeader)
  638. continue;
  639. return false;
  640. }
  641. }
  642. return true;
  643. }
  644. const DirectoryEntry *Dir = Header->getDir();
  645. SmallVector<const DirectoryEntry *, 2> SkippedDirs;
  646. StringRef DirName = Dir->getName();
  647. auto IsUnavailable = [&](const Module *M) {
  648. return !M->isAvailable() && (!RequestingModule ||
  649. M->isSubModuleOf(RequestingModule));
  650. };
  651. // Keep walking up the directory hierarchy, looking for a directory with
  652. // an umbrella header.
  653. do {
  654. llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
  655. = UmbrellaDirs.find(Dir);
  656. if (KnownDir != UmbrellaDirs.end()) {
  657. Module *Found = KnownDir->second;
  658. if (IsUnavailable(Found))
  659. return true;
  660. // Search up the module stack until we find a module with an umbrella
  661. // directory.
  662. Module *UmbrellaModule = Found;
  663. while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
  664. UmbrellaModule = UmbrellaModule->Parent;
  665. if (UmbrellaModule->InferSubmodules) {
  666. for (const DirectoryEntry *SkippedDir : llvm::reverse(SkippedDirs)) {
  667. // Find or create the module that corresponds to this directory name.
  668. SmallString<32> NameBuf;
  669. StringRef Name = sanitizeFilenameAsIdentifier(
  670. llvm::sys::path::stem(SkippedDir->getName()), NameBuf);
  671. Found = lookupModuleQualified(Name, Found);
  672. if (!Found)
  673. return false;
  674. if (IsUnavailable(Found))
  675. return true;
  676. }
  677. // Infer a submodule with the same name as this header file.
  678. SmallString<32> NameBuf;
  679. StringRef Name = sanitizeFilenameAsIdentifier(
  680. llvm::sys::path::stem(Header->getName()),
  681. NameBuf);
  682. Found = lookupModuleQualified(Name, Found);
  683. if (!Found)
  684. return false;
  685. }
  686. return IsUnavailable(Found);
  687. }
  688. SkippedDirs.push_back(Dir);
  689. // Retrieve our parent path.
  690. DirName = llvm::sys::path::parent_path(DirName);
  691. if (DirName.empty())
  692. break;
  693. // Resolve the parent path to a directory entry.
  694. if (auto DirEntry = SourceMgr.getFileManager().getDirectory(DirName))
  695. Dir = *DirEntry;
  696. else
  697. Dir = nullptr;
  698. } while (Dir);
  699. return false;
  700. }
  701. Module *ModuleMap::findModule(StringRef Name) const {
  702. llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
  703. if (Known != Modules.end())
  704. return Known->getValue();
  705. return nullptr;
  706. }
  707. Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
  708. Module *Context) const {
  709. for(; Context; Context = Context->Parent) {
  710. if (Module *Sub = lookupModuleQualified(Name, Context))
  711. return Sub;
  712. }
  713. return findModule(Name);
  714. }
  715. Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
  716. if (!Context)
  717. return findModule(Name);
  718. return Context->findSubmodule(Name);
  719. }
  720. std::pair<Module *, bool> ModuleMap::findOrCreateModule(StringRef Name,
  721. Module *Parent,
  722. bool IsFramework,
  723. bool IsExplicit) {
  724. // Try to find an existing module with this name.
  725. if (Module *Sub = lookupModuleQualified(Name, Parent))
  726. return std::make_pair(Sub, false);
  727. // Create a new module with this name.
  728. Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
  729. IsExplicit, NumCreatedModules++);
  730. if (!Parent) {
  731. if (LangOpts.CurrentModule == Name)
  732. SourceModule = Result;
  733. Modules[Name] = Result;
  734. ModuleScopeIDs[Result] = CurrentModuleScopeID;
  735. }
  736. return std::make_pair(Result, true);
  737. }
  738. Module *ModuleMap::createGlobalModuleFragmentForModuleUnit(SourceLocation Loc,
  739. Module *Parent) {
  740. auto *Result = new Module("<global>", Loc, Parent, /*IsFramework*/ false,
  741. /*IsExplicit*/ true, NumCreatedModules++);
  742. Result->Kind = Module::GlobalModuleFragment;
  743. // If the created module isn't owned by a parent, send it to PendingSubmodules
  744. // to wait for its parent.
  745. if (!Result->Parent)
  746. PendingSubmodules.emplace_back(Result);
  747. return Result;
  748. }
  749. Module *
  750. ModuleMap::createPrivateModuleFragmentForInterfaceUnit(Module *Parent,
  751. SourceLocation Loc) {
  752. auto *Result =
  753. new Module("<private>", Loc, Parent, /*IsFramework*/ false,
  754. /*IsExplicit*/ true, NumCreatedModules++);
  755. Result->Kind = Module::PrivateModuleFragment;
  756. return Result;
  757. }
  758. Module *ModuleMap::createModuleForInterfaceUnit(SourceLocation Loc,
  759. StringRef Name) {
  760. assert(LangOpts.CurrentModule == Name && "module name mismatch");
  761. assert(!Modules[Name] && "redefining existing module");
  762. auto *Result =
  763. new Module(Name, Loc, nullptr, /*IsFramework*/ false,
  764. /*IsExplicit*/ false, NumCreatedModules++);
  765. Result->Kind = Module::ModuleInterfaceUnit;
  766. Modules[Name] = SourceModule = Result;
  767. // Reparent the current global module fragment as a submodule of this module.
  768. for (auto &Submodule : PendingSubmodules) {
  769. Submodule->setParent(Result);
  770. Submodule.release(); // now owned by parent
  771. }
  772. PendingSubmodules.clear();
  773. // Mark the main source file as being within the newly-created module so that
  774. // declarations and macros are properly visibility-restricted to it.
  775. auto *MainFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
  776. assert(MainFile && "no input file for module interface");
  777. Headers[MainFile].push_back(KnownHeader(Result, PrivateHeader));
  778. return Result;
  779. }
  780. Module *ModuleMap::createHeaderUnit(SourceLocation Loc, StringRef Name,
  781. Module::Header H) {
  782. assert(LangOpts.CurrentModule == Name && "module name mismatch");
  783. assert(!Modules[Name] && "redefining existing module");
  784. auto *Result = new Module(Name, Loc, nullptr, /*IsFramework*/ false,
  785. /*IsExplicit*/ false, NumCreatedModules++);
  786. Result->Kind = Module::ModuleHeaderUnit;
  787. Modules[Name] = SourceModule = Result;
  788. addHeader(Result, H, NormalHeader);
  789. return Result;
  790. }
  791. /// For a framework module, infer the framework against which we
  792. /// should link.
  793. static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
  794. FileManager &FileMgr) {
  795. assert(Mod->IsFramework && "Can only infer linking for framework modules");
  796. assert(!Mod->isSubFramework() &&
  797. "Can only infer linking for top-level frameworks");
  798. SmallString<128> LibName;
  799. LibName += FrameworkDir->getName();
  800. llvm::sys::path::append(LibName, Mod->Name);
  801. // The library name of a framework has more than one possible extension since
  802. // the introduction of the text-based dynamic library format. We need to check
  803. // for both before we give up.
  804. for (const char *extension : {"", ".tbd"}) {
  805. llvm::sys::path::replace_extension(LibName, extension);
  806. if (FileMgr.getFile(LibName)) {
  807. Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
  808. /*IsFramework=*/true));
  809. return;
  810. }
  811. }
  812. }
  813. Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
  814. bool IsSystem, Module *Parent) {
  815. Attributes Attrs;
  816. Attrs.IsSystem = IsSystem;
  817. return inferFrameworkModule(FrameworkDir, Attrs, Parent);
  818. }
  819. Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
  820. Attributes Attrs, Module *Parent) {
  821. // Note: as an egregious but useful hack we use the real path here, because
  822. // we might be looking at an embedded framework that symlinks out to a
  823. // top-level framework, and we need to infer as if we were naming the
  824. // top-level framework.
  825. StringRef FrameworkDirName =
  826. SourceMgr.getFileManager().getCanonicalName(FrameworkDir);
  827. // In case this is a case-insensitive filesystem, use the canonical
  828. // directory name as the ModuleName, since modules are case-sensitive.
  829. // FIXME: we should be able to give a fix-it hint for the correct spelling.
  830. SmallString<32> ModuleNameStorage;
  831. StringRef ModuleName = sanitizeFilenameAsIdentifier(
  832. llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage);
  833. // Check whether we've already found this module.
  834. if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
  835. return Mod;
  836. FileManager &FileMgr = SourceMgr.getFileManager();
  837. // If the framework has a parent path from which we're allowed to infer
  838. // a framework module, do so.
  839. const FileEntry *ModuleMapFile = nullptr;
  840. if (!Parent) {
  841. // Determine whether we're allowed to infer a module map.
  842. bool canInfer = false;
  843. if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
  844. // Figure out the parent path.
  845. StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
  846. if (auto ParentDir = FileMgr.getDirectory(Parent)) {
  847. // Check whether we have already looked into the parent directory
  848. // for a module map.
  849. llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
  850. inferred = InferredDirectories.find(*ParentDir);
  851. if (inferred == InferredDirectories.end()) {
  852. // We haven't looked here before. Load a module map, if there is
  853. // one.
  854. bool IsFrameworkDir = Parent.endswith(".framework");
  855. if (const FileEntry *ModMapFile =
  856. HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) {
  857. parseModuleMapFile(ModMapFile, Attrs.IsSystem, *ParentDir);
  858. inferred = InferredDirectories.find(*ParentDir);
  859. }
  860. if (inferred == InferredDirectories.end())
  861. inferred = InferredDirectories.insert(
  862. std::make_pair(*ParentDir, InferredDirectory())).first;
  863. }
  864. if (inferred->second.InferModules) {
  865. // We're allowed to infer for this directory, but make sure it's okay
  866. // to infer this particular module.
  867. StringRef Name = llvm::sys::path::stem(FrameworkDirName);
  868. canInfer =
  869. !llvm::is_contained(inferred->second.ExcludedModules, Name);
  870. Attrs.IsSystem |= inferred->second.Attrs.IsSystem;
  871. Attrs.IsExternC |= inferred->second.Attrs.IsExternC;
  872. Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive;
  873. Attrs.NoUndeclaredIncludes |=
  874. inferred->second.Attrs.NoUndeclaredIncludes;
  875. ModuleMapFile = inferred->second.ModuleMapFile;
  876. }
  877. }
  878. }
  879. // If we're not allowed to infer a framework module, don't.
  880. if (!canInfer)
  881. return nullptr;
  882. } else {
  883. OptionalFileEntryRefDegradesToFileEntryPtr ModuleMapRef =
  884. getModuleMapFileForUniquing(Parent);
  885. ModuleMapFile = ModuleMapRef;
  886. }
  887. // Look for an umbrella header.
  888. SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
  889. llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
  890. auto UmbrellaHeader = FileMgr.getOptionalFileRef(UmbrellaName);
  891. // FIXME: If there's no umbrella header, we could probably scan the
  892. // framework to load *everything*. But, it's not clear that this is a good
  893. // idea.
  894. if (!UmbrellaHeader)
  895. return nullptr;
  896. Module *Result = new Module(ModuleName, SourceLocation(), Parent,
  897. /*IsFramework=*/true, /*IsExplicit=*/false,
  898. NumCreatedModules++);
  899. InferredModuleAllowedBy[Result] = ModuleMapFile;
  900. Result->IsInferred = true;
  901. if (!Parent) {
  902. if (LangOpts.CurrentModule == ModuleName)
  903. SourceModule = Result;
  904. Modules[ModuleName] = Result;
  905. ModuleScopeIDs[Result] = CurrentModuleScopeID;
  906. }
  907. Result->IsSystem |= Attrs.IsSystem;
  908. Result->IsExternC |= Attrs.IsExternC;
  909. Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive;
  910. Result->NoUndeclaredIncludes |= Attrs.NoUndeclaredIncludes;
  911. Result->Directory = FrameworkDir;
  912. // Chop off the first framework bit, as that is implied.
  913. StringRef RelativePath = UmbrellaName.str().substr(
  914. Result->getTopLevelModule()->Directory->getName().size());
  915. RelativePath = llvm::sys::path::relative_path(RelativePath);
  916. // umbrella header "umbrella-header-name"
  917. setUmbrellaHeader(Result, *UmbrellaHeader, ModuleName + ".h", RelativePath);
  918. // export *
  919. Result->Exports.push_back(Module::ExportDecl(nullptr, true));
  920. // module * { export * }
  921. Result->InferSubmodules = true;
  922. Result->InferExportWildcard = true;
  923. // Look for subframeworks.
  924. std::error_code EC;
  925. SmallString<128> SubframeworksDirName
  926. = StringRef(FrameworkDir->getName());
  927. llvm::sys::path::append(SubframeworksDirName, "Frameworks");
  928. llvm::sys::path::native(SubframeworksDirName);
  929. llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
  930. for (llvm::vfs::directory_iterator
  931. Dir = FS.dir_begin(SubframeworksDirName, EC),
  932. DirEnd;
  933. Dir != DirEnd && !EC; Dir.increment(EC)) {
  934. if (!StringRef(Dir->path()).endswith(".framework"))
  935. continue;
  936. if (auto SubframeworkDir =
  937. FileMgr.getDirectory(Dir->path())) {
  938. // Note: as an egregious but useful hack, we use the real path here and
  939. // check whether it is actually a subdirectory of the parent directory.
  940. // This will not be the case if the 'subframework' is actually a symlink
  941. // out to a top-level framework.
  942. StringRef SubframeworkDirName =
  943. FileMgr.getCanonicalName(*SubframeworkDir);
  944. bool FoundParent = false;
  945. do {
  946. // Get the parent directory name.
  947. SubframeworkDirName
  948. = llvm::sys::path::parent_path(SubframeworkDirName);
  949. if (SubframeworkDirName.empty())
  950. break;
  951. if (auto SubDir = FileMgr.getDirectory(SubframeworkDirName)) {
  952. if (*SubDir == FrameworkDir) {
  953. FoundParent = true;
  954. break;
  955. }
  956. }
  957. } while (true);
  958. if (!FoundParent)
  959. continue;
  960. // FIXME: Do we want to warn about subframeworks without umbrella headers?
  961. inferFrameworkModule(*SubframeworkDir, Attrs, Result);
  962. }
  963. }
  964. // If the module is a top-level framework, automatically link against the
  965. // framework.
  966. if (!Result->isSubFramework()) {
  967. inferFrameworkLink(Result, FrameworkDir, FileMgr);
  968. }
  969. return Result;
  970. }
  971. Module *ModuleMap::createShadowedModule(StringRef Name, bool IsFramework,
  972. Module *ShadowingModule) {
  973. // Create a new module with this name.
  974. Module *Result =
  975. new Module(Name, SourceLocation(), /*Parent=*/nullptr, IsFramework,
  976. /*IsExplicit=*/false, NumCreatedModules++);
  977. Result->ShadowingModule = ShadowingModule;
  978. Result->markUnavailable(/*Unimportable*/true);
  979. ModuleScopeIDs[Result] = CurrentModuleScopeID;
  980. ShadowModules.push_back(Result);
  981. return Result;
  982. }
  983. void ModuleMap::setUmbrellaHeader(
  984. Module *Mod, FileEntryRef UmbrellaHeader, const Twine &NameAsWritten,
  985. const Twine &PathRelativeToRootModuleDirectory) {
  986. Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
  987. Mod->Umbrella = &UmbrellaHeader.getMapEntry();
  988. Mod->UmbrellaAsWritten = NameAsWritten.str();
  989. Mod->UmbrellaRelativeToRootModuleDirectory =
  990. PathRelativeToRootModuleDirectory.str();
  991. UmbrellaDirs[UmbrellaHeader.getDir()] = Mod;
  992. // Notify callbacks that we just added a new header.
  993. for (const auto &Cb : Callbacks)
  994. Cb->moduleMapAddUmbrellaHeader(&SourceMgr.getFileManager(), UmbrellaHeader);
  995. }
  996. void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
  997. const Twine &NameAsWritten,
  998. const Twine &PathRelativeToRootModuleDirectory) {
  999. Mod->Umbrella = UmbrellaDir;
  1000. Mod->UmbrellaAsWritten = NameAsWritten.str();
  1001. Mod->UmbrellaRelativeToRootModuleDirectory =
  1002. PathRelativeToRootModuleDirectory.str();
  1003. UmbrellaDirs[UmbrellaDir] = Mod;
  1004. }
  1005. void ModuleMap::addUnresolvedHeader(Module *Mod,
  1006. Module::UnresolvedHeaderDirective Header,
  1007. bool &NeedsFramework) {
  1008. // If there is a builtin counterpart to this file, add it now so it can
  1009. // wrap the system header.
  1010. if (resolveAsBuiltinHeader(Mod, Header)) {
  1011. // If we have both a builtin and system version of the file, the
  1012. // builtin version may want to inject macros into the system header, so
  1013. // force the system header to be treated as a textual header in this
  1014. // case.
  1015. Header.Kind = headerRoleToKind(ModuleMap::ModuleHeaderRole(
  1016. headerKindToRole(Header.Kind) | ModuleMap::TextualHeader));
  1017. Header.HasBuiltinHeader = true;
  1018. }
  1019. // If possible, don't stat the header until we need to. This requires the
  1020. // user to have provided us with some stat information about the file.
  1021. // FIXME: Add support for lazily stat'ing umbrella headers and excluded
  1022. // headers.
  1023. if ((Header.Size || Header.ModTime) && !Header.IsUmbrella &&
  1024. Header.Kind != Module::HK_Excluded) {
  1025. // We expect more variation in mtime than size, so if we're given both,
  1026. // use the mtime as the key.
  1027. if (Header.ModTime)
  1028. LazyHeadersByModTime[*Header.ModTime].push_back(Mod);
  1029. else
  1030. LazyHeadersBySize[*Header.Size].push_back(Mod);
  1031. Mod->UnresolvedHeaders.push_back(Header);
  1032. return;
  1033. }
  1034. // We don't have stat information or can't defer looking this file up.
  1035. // Perform the lookup now.
  1036. resolveHeader(Mod, Header, NeedsFramework);
  1037. }
  1038. void ModuleMap::resolveHeaderDirectives(const FileEntry *File) const {
  1039. auto BySize = LazyHeadersBySize.find(File->getSize());
  1040. if (BySize != LazyHeadersBySize.end()) {
  1041. for (auto *M : BySize->second)
  1042. resolveHeaderDirectives(M, File);
  1043. LazyHeadersBySize.erase(BySize);
  1044. }
  1045. auto ByModTime = LazyHeadersByModTime.find(File->getModificationTime());
  1046. if (ByModTime != LazyHeadersByModTime.end()) {
  1047. for (auto *M : ByModTime->second)
  1048. resolveHeaderDirectives(M, File);
  1049. LazyHeadersByModTime.erase(ByModTime);
  1050. }
  1051. }
  1052. void ModuleMap::resolveHeaderDirectives(
  1053. Module *Mod, std::optional<const FileEntry *> File) const {
  1054. bool NeedsFramework = false;
  1055. SmallVector<Module::UnresolvedHeaderDirective, 1> NewHeaders;
  1056. const auto Size = File ? (*File)->getSize() : 0;
  1057. const auto ModTime = File ? (*File)->getModificationTime() : 0;
  1058. for (auto &Header : Mod->UnresolvedHeaders) {
  1059. if (File && ((Header.ModTime && Header.ModTime != ModTime) ||
  1060. (Header.Size && Header.Size != Size)))
  1061. NewHeaders.push_back(Header);
  1062. else
  1063. // This operation is logically const; we're just changing how we represent
  1064. // the header information for this file.
  1065. const_cast<ModuleMap *>(this)->resolveHeader(Mod, Header, NeedsFramework);
  1066. }
  1067. Mod->UnresolvedHeaders.swap(NewHeaders);
  1068. }
  1069. void ModuleMap::addHeader(Module *Mod, Module::Header Header,
  1070. ModuleHeaderRole Role, bool Imported) {
  1071. KnownHeader KH(Mod, Role);
  1072. // Only add each header to the headers list once.
  1073. // FIXME: Should we diagnose if a header is listed twice in the
  1074. // same module definition?
  1075. auto &HeaderList = Headers[Header.Entry];
  1076. if (llvm::is_contained(HeaderList, KH))
  1077. return;
  1078. HeaderList.push_back(KH);
  1079. Mod->Headers[headerRoleToKind(Role)].push_back(Header);
  1080. bool isCompilingModuleHeader =
  1081. LangOpts.isCompilingModule() && Mod->getTopLevelModule() == SourceModule;
  1082. if (!Imported || isCompilingModuleHeader) {
  1083. // When we import HeaderFileInfo, the external source is expected to
  1084. // set the isModuleHeader flag itself.
  1085. HeaderInfo.MarkFileModuleHeader(Header.Entry, Role,
  1086. isCompilingModuleHeader);
  1087. }
  1088. // Notify callbacks that we just added a new header.
  1089. for (const auto &Cb : Callbacks)
  1090. Cb->moduleMapAddHeader(Header.Entry->getName());
  1091. }
  1092. OptionalFileEntryRef
  1093. ModuleMap::getContainingModuleMapFile(const Module *Module) const {
  1094. if (Module->DefinitionLoc.isInvalid())
  1095. return std::nullopt;
  1096. return SourceMgr.getFileEntryRefForID(
  1097. SourceMgr.getFileID(Module->DefinitionLoc));
  1098. }
  1099. OptionalFileEntryRef
  1100. ModuleMap::getModuleMapFileForUniquing(const Module *M) const {
  1101. if (M->IsInferred) {
  1102. assert(InferredModuleAllowedBy.count(M) && "missing inferred module map");
  1103. // FIXME: Update InferredModuleAllowedBy to use FileEntryRef.
  1104. return InferredModuleAllowedBy.find(M)->second->getLastRef();
  1105. }
  1106. return getContainingModuleMapFile(M);
  1107. }
  1108. void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) {
  1109. assert(M->IsInferred && "module not inferred");
  1110. InferredModuleAllowedBy[M] = ModMap;
  1111. }
  1112. std::error_code
  1113. ModuleMap::canonicalizeModuleMapPath(SmallVectorImpl<char> &Path) {
  1114. StringRef Dir = llvm::sys::path::parent_path({Path.data(), Path.size()});
  1115. // Do not canonicalize within the framework; the module map parser expects
  1116. // Modules/ not Versions/A/Modules.
  1117. if (llvm::sys::path::filename(Dir) == "Modules") {
  1118. StringRef Parent = llvm::sys::path::parent_path(Dir);
  1119. if (Parent.endswith(".framework"))
  1120. Dir = Parent;
  1121. }
  1122. FileManager &FM = SourceMgr.getFileManager();
  1123. auto DirEntry = FM.getDirectory(Dir.empty() ? "." : Dir);
  1124. if (!DirEntry)
  1125. return DirEntry.getError();
  1126. // Canonicalize the directory.
  1127. StringRef CanonicalDir = FM.getCanonicalName(*DirEntry);
  1128. if (CanonicalDir != Dir) {
  1129. auto CanonicalDirEntry = FM.getDirectory(CanonicalDir);
  1130. // Only use the canonicalized path if it resolves to the same entry as the
  1131. // original. This is not true if there's a VFS overlay on top of a FS where
  1132. // the directory is a symlink. The overlay would not remap the target path
  1133. // of the symlink to the same directory entry in that case.
  1134. if (CanonicalDirEntry && *CanonicalDirEntry == *DirEntry) {
  1135. bool Done = llvm::sys::path::replace_path_prefix(Path, Dir, CanonicalDir);
  1136. (void)Done;
  1137. assert(Done && "Path should always start with Dir");
  1138. }
  1139. }
  1140. // In theory, the filename component should also be canonicalized if it
  1141. // on a case-insensitive filesystem. However, the extra canonicalization is
  1142. // expensive and if clang looked up the filename it will always be lowercase.
  1143. // Remove ., remove redundant separators, and switch to native separators.
  1144. // This is needed for separators between CanonicalDir and the filename.
  1145. llvm::sys::path::remove_dots(Path);
  1146. return std::error_code();
  1147. }
  1148. void ModuleMap::addAdditionalModuleMapFile(const Module *M,
  1149. const FileEntry *ModuleMap) {
  1150. AdditionalModMaps[M].insert(ModuleMap);
  1151. }
  1152. LLVM_DUMP_METHOD void ModuleMap::dump() {
  1153. llvm::errs() << "Modules:";
  1154. for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
  1155. MEnd = Modules.end();
  1156. M != MEnd; ++M)
  1157. M->getValue()->print(llvm::errs(), 2);
  1158. llvm::errs() << "Headers:";
  1159. for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
  1160. H != HEnd; ++H) {
  1161. llvm::errs() << " \"" << H->first->getName() << "\" -> ";
  1162. for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
  1163. E = H->second.end();
  1164. I != E; ++I) {
  1165. if (I != H->second.begin())
  1166. llvm::errs() << ",";
  1167. llvm::errs() << I->getModule()->getFullModuleName();
  1168. }
  1169. llvm::errs() << "\n";
  1170. }
  1171. }
  1172. bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
  1173. auto Unresolved = std::move(Mod->UnresolvedExports);
  1174. Mod->UnresolvedExports.clear();
  1175. for (auto &UE : Unresolved) {
  1176. Module::ExportDecl Export = resolveExport(Mod, UE, Complain);
  1177. if (Export.getPointer() || Export.getInt())
  1178. Mod->Exports.push_back(Export);
  1179. else
  1180. Mod->UnresolvedExports.push_back(UE);
  1181. }
  1182. return !Mod->UnresolvedExports.empty();
  1183. }
  1184. bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
  1185. auto Unresolved = std::move(Mod->UnresolvedDirectUses);
  1186. Mod->UnresolvedDirectUses.clear();
  1187. for (auto &UDU : Unresolved) {
  1188. Module *DirectUse = resolveModuleId(UDU, Mod, Complain);
  1189. if (DirectUse)
  1190. Mod->DirectUses.push_back(DirectUse);
  1191. else
  1192. Mod->UnresolvedDirectUses.push_back(UDU);
  1193. }
  1194. return !Mod->UnresolvedDirectUses.empty();
  1195. }
  1196. bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
  1197. auto Unresolved = std::move(Mod->UnresolvedConflicts);
  1198. Mod->UnresolvedConflicts.clear();
  1199. for (auto &UC : Unresolved) {
  1200. if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) {
  1201. Module::Conflict Conflict;
  1202. Conflict.Other = OtherMod;
  1203. Conflict.Message = UC.Message;
  1204. Mod->Conflicts.push_back(Conflict);
  1205. } else
  1206. Mod->UnresolvedConflicts.push_back(UC);
  1207. }
  1208. return !Mod->UnresolvedConflicts.empty();
  1209. }
  1210. //----------------------------------------------------------------------------//
  1211. // Module map file parser
  1212. //----------------------------------------------------------------------------//
  1213. namespace clang {
  1214. /// A token in a module map file.
  1215. struct MMToken {
  1216. enum TokenKind {
  1217. Comma,
  1218. ConfigMacros,
  1219. Conflict,
  1220. EndOfFile,
  1221. HeaderKeyword,
  1222. Identifier,
  1223. Exclaim,
  1224. ExcludeKeyword,
  1225. ExplicitKeyword,
  1226. ExportKeyword,
  1227. ExportAsKeyword,
  1228. ExternKeyword,
  1229. FrameworkKeyword,
  1230. LinkKeyword,
  1231. ModuleKeyword,
  1232. Period,
  1233. PrivateKeyword,
  1234. UmbrellaKeyword,
  1235. UseKeyword,
  1236. RequiresKeyword,
  1237. Star,
  1238. StringLiteral,
  1239. IntegerLiteral,
  1240. TextualKeyword,
  1241. LBrace,
  1242. RBrace,
  1243. LSquare,
  1244. RSquare
  1245. } Kind;
  1246. SourceLocation::UIntTy Location;
  1247. unsigned StringLength;
  1248. union {
  1249. // If Kind != IntegerLiteral.
  1250. const char *StringData;
  1251. // If Kind == IntegerLiteral.
  1252. uint64_t IntegerValue;
  1253. };
  1254. void clear() {
  1255. Kind = EndOfFile;
  1256. Location = 0;
  1257. StringLength = 0;
  1258. StringData = nullptr;
  1259. }
  1260. bool is(TokenKind K) const { return Kind == K; }
  1261. SourceLocation getLocation() const {
  1262. return SourceLocation::getFromRawEncoding(Location);
  1263. }
  1264. uint64_t getInteger() const {
  1265. return Kind == IntegerLiteral ? IntegerValue : 0;
  1266. }
  1267. StringRef getString() const {
  1268. return Kind == IntegerLiteral ? StringRef()
  1269. : StringRef(StringData, StringLength);
  1270. }
  1271. };
  1272. class ModuleMapParser {
  1273. Lexer &L;
  1274. SourceManager &SourceMgr;
  1275. /// Default target information, used only for string literal
  1276. /// parsing.
  1277. const TargetInfo *Target;
  1278. DiagnosticsEngine &Diags;
  1279. ModuleMap &Map;
  1280. /// The current module map file.
  1281. const FileEntry *ModuleMapFile;
  1282. /// Source location of most recent parsed module declaration
  1283. SourceLocation CurrModuleDeclLoc;
  1284. /// The directory that file names in this module map file should
  1285. /// be resolved relative to.
  1286. const DirectoryEntry *Directory;
  1287. /// Whether this module map is in a system header directory.
  1288. bool IsSystem;
  1289. /// Whether an error occurred.
  1290. bool HadError = false;
  1291. /// Stores string data for the various string literals referenced
  1292. /// during parsing.
  1293. llvm::BumpPtrAllocator StringData;
  1294. /// The current token.
  1295. MMToken Tok;
  1296. /// The active module.
  1297. Module *ActiveModule = nullptr;
  1298. /// Whether a module uses the 'requires excluded' hack to mark its
  1299. /// contents as 'textual'.
  1300. ///
  1301. /// On older Darwin SDK versions, 'requires excluded' is used to mark the
  1302. /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as
  1303. /// non-modular headers. For backwards compatibility, we continue to
  1304. /// support this idiom for just these modules, and map the headers to
  1305. /// 'textual' to match the original intent.
  1306. llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack;
  1307. /// Consume the current token and return its location.
  1308. SourceLocation consumeToken();
  1309. /// Skip tokens until we reach the a token with the given kind
  1310. /// (or the end of the file).
  1311. void skipUntil(MMToken::TokenKind K);
  1312. using ModuleId = SmallVector<std::pair<std::string, SourceLocation>, 2>;
  1313. bool parseModuleId(ModuleId &Id);
  1314. void parseModuleDecl();
  1315. void parseExternModuleDecl();
  1316. void parseRequiresDecl();
  1317. void parseHeaderDecl(MMToken::TokenKind, SourceLocation LeadingLoc);
  1318. void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
  1319. void parseExportDecl();
  1320. void parseExportAsDecl();
  1321. void parseUseDecl();
  1322. void parseLinkDecl();
  1323. void parseConfigMacros();
  1324. void parseConflict();
  1325. void parseInferredModuleDecl(bool Framework, bool Explicit);
  1326. /// Private modules are canonicalized as Foo_Private. Clang provides extra
  1327. /// module map search logic to find the appropriate private module when PCH
  1328. /// is used with implicit module maps. Warn when private modules are written
  1329. /// in other ways (FooPrivate and Foo.Private), providing notes and fixits.
  1330. void diagnosePrivateModules(SourceLocation ExplicitLoc,
  1331. SourceLocation FrameworkLoc);
  1332. using Attributes = ModuleMap::Attributes;
  1333. bool parseOptionalAttributes(Attributes &Attrs);
  1334. public:
  1335. explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
  1336. const TargetInfo *Target, DiagnosticsEngine &Diags,
  1337. ModuleMap &Map, const FileEntry *ModuleMapFile,
  1338. const DirectoryEntry *Directory, bool IsSystem)
  1339. : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
  1340. ModuleMapFile(ModuleMapFile), Directory(Directory),
  1341. IsSystem(IsSystem) {
  1342. Tok.clear();
  1343. consumeToken();
  1344. }
  1345. bool parseModuleMapFile();
  1346. bool terminatedByDirective() { return false; }
  1347. SourceLocation getLocation() { return Tok.getLocation(); }
  1348. };
  1349. } // namespace clang
  1350. SourceLocation ModuleMapParser::consumeToken() {
  1351. SourceLocation Result = Tok.getLocation();
  1352. retry:
  1353. Tok.clear();
  1354. Token LToken;
  1355. L.LexFromRawLexer(LToken);
  1356. Tok.Location = LToken.getLocation().getRawEncoding();
  1357. switch (LToken.getKind()) {
  1358. case tok::raw_identifier: {
  1359. StringRef RI = LToken.getRawIdentifier();
  1360. Tok.StringData = RI.data();
  1361. Tok.StringLength = RI.size();
  1362. Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI)
  1363. .Case("config_macros", MMToken::ConfigMacros)
  1364. .Case("conflict", MMToken::Conflict)
  1365. .Case("exclude", MMToken::ExcludeKeyword)
  1366. .Case("explicit", MMToken::ExplicitKeyword)
  1367. .Case("export", MMToken::ExportKeyword)
  1368. .Case("export_as", MMToken::ExportAsKeyword)
  1369. .Case("extern", MMToken::ExternKeyword)
  1370. .Case("framework", MMToken::FrameworkKeyword)
  1371. .Case("header", MMToken::HeaderKeyword)
  1372. .Case("link", MMToken::LinkKeyword)
  1373. .Case("module", MMToken::ModuleKeyword)
  1374. .Case("private", MMToken::PrivateKeyword)
  1375. .Case("requires", MMToken::RequiresKeyword)
  1376. .Case("textual", MMToken::TextualKeyword)
  1377. .Case("umbrella", MMToken::UmbrellaKeyword)
  1378. .Case("use", MMToken::UseKeyword)
  1379. .Default(MMToken::Identifier);
  1380. break;
  1381. }
  1382. case tok::comma:
  1383. Tok.Kind = MMToken::Comma;
  1384. break;
  1385. case tok::eof:
  1386. Tok.Kind = MMToken::EndOfFile;
  1387. break;
  1388. case tok::l_brace:
  1389. Tok.Kind = MMToken::LBrace;
  1390. break;
  1391. case tok::l_square:
  1392. Tok.Kind = MMToken::LSquare;
  1393. break;
  1394. case tok::period:
  1395. Tok.Kind = MMToken::Period;
  1396. break;
  1397. case tok::r_brace:
  1398. Tok.Kind = MMToken::RBrace;
  1399. break;
  1400. case tok::r_square:
  1401. Tok.Kind = MMToken::RSquare;
  1402. break;
  1403. case tok::star:
  1404. Tok.Kind = MMToken::Star;
  1405. break;
  1406. case tok::exclaim:
  1407. Tok.Kind = MMToken::Exclaim;
  1408. break;
  1409. case tok::string_literal: {
  1410. if (LToken.hasUDSuffix()) {
  1411. Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
  1412. HadError = true;
  1413. goto retry;
  1414. }
  1415. // Parse the string literal.
  1416. LangOptions LangOpts;
  1417. StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target);
  1418. if (StringLiteral.hadError)
  1419. goto retry;
  1420. // Copy the string literal into our string data allocator.
  1421. unsigned Length = StringLiteral.GetStringLength();
  1422. char *Saved = StringData.Allocate<char>(Length + 1);
  1423. memcpy(Saved, StringLiteral.GetString().data(), Length);
  1424. Saved[Length] = 0;
  1425. // Form the token.
  1426. Tok.Kind = MMToken::StringLiteral;
  1427. Tok.StringData = Saved;
  1428. Tok.StringLength = Length;
  1429. break;
  1430. }
  1431. case tok::numeric_constant: {
  1432. // We don't support any suffixes or other complications.
  1433. SmallString<32> SpellingBuffer;
  1434. SpellingBuffer.resize(LToken.getLength() + 1);
  1435. const char *Start = SpellingBuffer.data();
  1436. unsigned Length =
  1437. Lexer::getSpelling(LToken, Start, SourceMgr, Map.LangOpts);
  1438. uint64_t Value;
  1439. if (StringRef(Start, Length).getAsInteger(0, Value)) {
  1440. Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token);
  1441. HadError = true;
  1442. goto retry;
  1443. }
  1444. Tok.Kind = MMToken::IntegerLiteral;
  1445. Tok.IntegerValue = Value;
  1446. break;
  1447. }
  1448. case tok::comment:
  1449. goto retry;
  1450. case tok::hash:
  1451. // A module map can be terminated prematurely by
  1452. // #pragma clang module contents
  1453. // When building the module, we'll treat the rest of the file as the
  1454. // contents of the module.
  1455. {
  1456. auto NextIsIdent = [&](StringRef Str) -> bool {
  1457. L.LexFromRawLexer(LToken);
  1458. return !LToken.isAtStartOfLine() && LToken.is(tok::raw_identifier) &&
  1459. LToken.getRawIdentifier() == Str;
  1460. };
  1461. if (NextIsIdent("pragma") && NextIsIdent("clang") &&
  1462. NextIsIdent("module") && NextIsIdent("contents")) {
  1463. Tok.Kind = MMToken::EndOfFile;
  1464. break;
  1465. }
  1466. }
  1467. [[fallthrough]];
  1468. default:
  1469. Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token);
  1470. HadError = true;
  1471. goto retry;
  1472. }
  1473. return Result;
  1474. }
  1475. void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
  1476. unsigned braceDepth = 0;
  1477. unsigned squareDepth = 0;
  1478. do {
  1479. switch (Tok.Kind) {
  1480. case MMToken::EndOfFile:
  1481. return;
  1482. case MMToken::LBrace:
  1483. if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
  1484. return;
  1485. ++braceDepth;
  1486. break;
  1487. case MMToken::LSquare:
  1488. if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
  1489. return;
  1490. ++squareDepth;
  1491. break;
  1492. case MMToken::RBrace:
  1493. if (braceDepth > 0)
  1494. --braceDepth;
  1495. else if (Tok.is(K))
  1496. return;
  1497. break;
  1498. case MMToken::RSquare:
  1499. if (squareDepth > 0)
  1500. --squareDepth;
  1501. else if (Tok.is(K))
  1502. return;
  1503. break;
  1504. default:
  1505. if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
  1506. return;
  1507. break;
  1508. }
  1509. consumeToken();
  1510. } while (true);
  1511. }
  1512. /// Parse a module-id.
  1513. ///
  1514. /// module-id:
  1515. /// identifier
  1516. /// identifier '.' module-id
  1517. ///
  1518. /// \returns true if an error occurred, false otherwise.
  1519. bool ModuleMapParser::parseModuleId(ModuleId &Id) {
  1520. Id.clear();
  1521. do {
  1522. if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) {
  1523. Id.push_back(
  1524. std::make_pair(std::string(Tok.getString()), Tok.getLocation()));
  1525. consumeToken();
  1526. } else {
  1527. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
  1528. return true;
  1529. }
  1530. if (!Tok.is(MMToken::Period))
  1531. break;
  1532. consumeToken();
  1533. } while (true);
  1534. return false;
  1535. }
  1536. namespace {
  1537. /// Enumerates the known attributes.
  1538. enum AttributeKind {
  1539. /// An unknown attribute.
  1540. AT_unknown,
  1541. /// The 'system' attribute.
  1542. AT_system,
  1543. /// The 'extern_c' attribute.
  1544. AT_extern_c,
  1545. /// The 'exhaustive' attribute.
  1546. AT_exhaustive,
  1547. /// The 'no_undeclared_includes' attribute.
  1548. AT_no_undeclared_includes
  1549. };
  1550. } // namespace
  1551. /// Private modules are canonicalized as Foo_Private. Clang provides extra
  1552. /// module map search logic to find the appropriate private module when PCH
  1553. /// is used with implicit module maps. Warn when private modules are written
  1554. /// in other ways (FooPrivate and Foo.Private), providing notes and fixits.
  1555. void ModuleMapParser::diagnosePrivateModules(SourceLocation ExplicitLoc,
  1556. SourceLocation FrameworkLoc) {
  1557. auto GenNoteAndFixIt = [&](StringRef BadName, StringRef Canonical,
  1558. const Module *M, SourceRange ReplLoc) {
  1559. auto D = Diags.Report(ActiveModule->DefinitionLoc,
  1560. diag::note_mmap_rename_top_level_private_module);
  1561. D << BadName << M->Name;
  1562. D << FixItHint::CreateReplacement(ReplLoc, Canonical);
  1563. };
  1564. for (auto E = Map.module_begin(); E != Map.module_end(); ++E) {
  1565. auto const *M = E->getValue();
  1566. if (M->Directory != ActiveModule->Directory)
  1567. continue;
  1568. SmallString<128> FullName(ActiveModule->getFullModuleName());
  1569. if (!FullName.startswith(M->Name) && !FullName.endswith("Private"))
  1570. continue;
  1571. SmallString<128> FixedPrivModDecl;
  1572. SmallString<128> Canonical(M->Name);
  1573. Canonical.append("_Private");
  1574. // Foo.Private -> Foo_Private
  1575. if (ActiveModule->Parent && ActiveModule->Name == "Private" && !M->Parent &&
  1576. M->Name == ActiveModule->Parent->Name) {
  1577. Diags.Report(ActiveModule->DefinitionLoc,
  1578. diag::warn_mmap_mismatched_private_submodule)
  1579. << FullName;
  1580. SourceLocation FixItInitBegin = CurrModuleDeclLoc;
  1581. if (FrameworkLoc.isValid())
  1582. FixItInitBegin = FrameworkLoc;
  1583. if (ExplicitLoc.isValid())
  1584. FixItInitBegin = ExplicitLoc;
  1585. if (FrameworkLoc.isValid() || ActiveModule->Parent->IsFramework)
  1586. FixedPrivModDecl.append("framework ");
  1587. FixedPrivModDecl.append("module ");
  1588. FixedPrivModDecl.append(Canonical);
  1589. GenNoteAndFixIt(FullName, FixedPrivModDecl, M,
  1590. SourceRange(FixItInitBegin, ActiveModule->DefinitionLoc));
  1591. continue;
  1592. }
  1593. // FooPrivate and whatnots -> Foo_Private
  1594. if (!ActiveModule->Parent && !M->Parent && M->Name != ActiveModule->Name &&
  1595. ActiveModule->Name != Canonical) {
  1596. Diags.Report(ActiveModule->DefinitionLoc,
  1597. diag::warn_mmap_mismatched_private_module_name)
  1598. << ActiveModule->Name;
  1599. GenNoteAndFixIt(ActiveModule->Name, Canonical, M,
  1600. SourceRange(ActiveModule->DefinitionLoc));
  1601. }
  1602. }
  1603. }
  1604. /// Parse a module declaration.
  1605. ///
  1606. /// module-declaration:
  1607. /// 'extern' 'module' module-id string-literal
  1608. /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
  1609. /// { module-member* }
  1610. ///
  1611. /// module-member:
  1612. /// requires-declaration
  1613. /// header-declaration
  1614. /// submodule-declaration
  1615. /// export-declaration
  1616. /// export-as-declaration
  1617. /// link-declaration
  1618. ///
  1619. /// submodule-declaration:
  1620. /// module-declaration
  1621. /// inferred-submodule-declaration
  1622. void ModuleMapParser::parseModuleDecl() {
  1623. assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
  1624. Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
  1625. if (Tok.is(MMToken::ExternKeyword)) {
  1626. parseExternModuleDecl();
  1627. return;
  1628. }
  1629. // Parse 'explicit' or 'framework' keyword, if present.
  1630. SourceLocation ExplicitLoc;
  1631. SourceLocation FrameworkLoc;
  1632. bool Explicit = false;
  1633. bool Framework = false;
  1634. // Parse 'explicit' keyword, if present.
  1635. if (Tok.is(MMToken::ExplicitKeyword)) {
  1636. ExplicitLoc = consumeToken();
  1637. Explicit = true;
  1638. }
  1639. // Parse 'framework' keyword, if present.
  1640. if (Tok.is(MMToken::FrameworkKeyword)) {
  1641. FrameworkLoc = consumeToken();
  1642. Framework = true;
  1643. }
  1644. // Parse 'module' keyword.
  1645. if (!Tok.is(MMToken::ModuleKeyword)) {
  1646. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
  1647. consumeToken();
  1648. HadError = true;
  1649. return;
  1650. }
  1651. CurrModuleDeclLoc = consumeToken(); // 'module' keyword
  1652. // If we have a wildcard for the module name, this is an inferred submodule.
  1653. // Parse it.
  1654. if (Tok.is(MMToken::Star))
  1655. return parseInferredModuleDecl(Framework, Explicit);
  1656. // Parse the module name.
  1657. ModuleId Id;
  1658. if (parseModuleId(Id)) {
  1659. HadError = true;
  1660. return;
  1661. }
  1662. if (ActiveModule) {
  1663. if (Id.size() > 1) {
  1664. Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
  1665. << SourceRange(Id.front().second, Id.back().second);
  1666. HadError = true;
  1667. return;
  1668. }
  1669. } else if (Id.size() == 1 && Explicit) {
  1670. // Top-level modules can't be explicit.
  1671. Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
  1672. Explicit = false;
  1673. ExplicitLoc = SourceLocation();
  1674. HadError = true;
  1675. }
  1676. Module *PreviousActiveModule = ActiveModule;
  1677. if (Id.size() > 1) {
  1678. // This module map defines a submodule. Go find the module of which it
  1679. // is a submodule.
  1680. ActiveModule = nullptr;
  1681. const Module *TopLevelModule = nullptr;
  1682. for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
  1683. if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
  1684. if (I == 0)
  1685. TopLevelModule = Next;
  1686. ActiveModule = Next;
  1687. continue;
  1688. }
  1689. Diags.Report(Id[I].second, diag::err_mmap_missing_parent_module)
  1690. << Id[I].first << (ActiveModule != nullptr)
  1691. << (ActiveModule
  1692. ? ActiveModule->getTopLevelModule()->getFullModuleName()
  1693. : "");
  1694. HadError = true;
  1695. }
  1696. if (TopLevelModule &&
  1697. ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) {
  1698. assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) &&
  1699. "submodule defined in same file as 'module *' that allowed its "
  1700. "top-level module");
  1701. Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile);
  1702. }
  1703. }
  1704. StringRef ModuleName = Id.back().first;
  1705. SourceLocation ModuleNameLoc = Id.back().second;
  1706. // Parse the optional attribute list.
  1707. Attributes Attrs;
  1708. if (parseOptionalAttributes(Attrs))
  1709. return;
  1710. // Parse the opening brace.
  1711. if (!Tok.is(MMToken::LBrace)) {
  1712. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
  1713. << ModuleName;
  1714. HadError = true;
  1715. return;
  1716. }
  1717. SourceLocation LBraceLoc = consumeToken();
  1718. // Determine whether this (sub)module has already been defined.
  1719. Module *ShadowingModule = nullptr;
  1720. if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
  1721. // We might see a (re)definition of a module that we already have a
  1722. // definition for in two cases:
  1723. // - If we loaded one definition from an AST file and we've just found a
  1724. // corresponding definition in a module map file, or
  1725. bool LoadedFromASTFile = Existing->DefinitionLoc.isInvalid();
  1726. // - If we're building a (preprocessed) module and we've just loaded the
  1727. // module map file from which it was created.
  1728. bool ParsedAsMainInput =
  1729. Map.LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap &&
  1730. Map.LangOpts.CurrentModule == ModuleName &&
  1731. SourceMgr.getDecomposedLoc(ModuleNameLoc).first !=
  1732. SourceMgr.getDecomposedLoc(Existing->DefinitionLoc).first;
  1733. if (!ActiveModule && (LoadedFromASTFile || ParsedAsMainInput)) {
  1734. // Skip the module definition.
  1735. skipUntil(MMToken::RBrace);
  1736. if (Tok.is(MMToken::RBrace))
  1737. consumeToken();
  1738. else {
  1739. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  1740. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  1741. HadError = true;
  1742. }
  1743. return;
  1744. }
  1745. if (!Existing->Parent && Map.mayShadowNewModule(Existing)) {
  1746. ShadowingModule = Existing;
  1747. } else {
  1748. // This is not a shawdowed module decl, it is an illegal redefinition.
  1749. Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
  1750. << ModuleName;
  1751. Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
  1752. // Skip the module definition.
  1753. skipUntil(MMToken::RBrace);
  1754. if (Tok.is(MMToken::RBrace))
  1755. consumeToken();
  1756. HadError = true;
  1757. return;
  1758. }
  1759. }
  1760. // Start defining this module.
  1761. if (ShadowingModule) {
  1762. ActiveModule =
  1763. Map.createShadowedModule(ModuleName, Framework, ShadowingModule);
  1764. } else {
  1765. ActiveModule =
  1766. Map.findOrCreateModule(ModuleName, ActiveModule, Framework, Explicit)
  1767. .first;
  1768. }
  1769. ActiveModule->DefinitionLoc = ModuleNameLoc;
  1770. if (Attrs.IsSystem || IsSystem)
  1771. ActiveModule->IsSystem = true;
  1772. if (Attrs.IsExternC)
  1773. ActiveModule->IsExternC = true;
  1774. if (Attrs.NoUndeclaredIncludes)
  1775. ActiveModule->NoUndeclaredIncludes = true;
  1776. ActiveModule->Directory = Directory;
  1777. StringRef MapFileName(ModuleMapFile->getName());
  1778. if (MapFileName.endswith("module.private.modulemap") ||
  1779. MapFileName.endswith("module_private.map")) {
  1780. ActiveModule->ModuleMapIsPrivate = true;
  1781. }
  1782. // Private modules named as FooPrivate, Foo.Private or similar are likely a
  1783. // user error; provide warnings, notes and fixits to direct users to use
  1784. // Foo_Private instead.
  1785. SourceLocation StartLoc =
  1786. SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
  1787. if (Map.HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps &&
  1788. !Diags.isIgnored(diag::warn_mmap_mismatched_private_submodule,
  1789. StartLoc) &&
  1790. !Diags.isIgnored(diag::warn_mmap_mismatched_private_module_name,
  1791. StartLoc) &&
  1792. ActiveModule->ModuleMapIsPrivate)
  1793. diagnosePrivateModules(ExplicitLoc, FrameworkLoc);
  1794. bool Done = false;
  1795. do {
  1796. switch (Tok.Kind) {
  1797. case MMToken::EndOfFile:
  1798. case MMToken::RBrace:
  1799. Done = true;
  1800. break;
  1801. case MMToken::ConfigMacros:
  1802. parseConfigMacros();
  1803. break;
  1804. case MMToken::Conflict:
  1805. parseConflict();
  1806. break;
  1807. case MMToken::ExplicitKeyword:
  1808. case MMToken::ExternKeyword:
  1809. case MMToken::FrameworkKeyword:
  1810. case MMToken::ModuleKeyword:
  1811. parseModuleDecl();
  1812. break;
  1813. case MMToken::ExportKeyword:
  1814. parseExportDecl();
  1815. break;
  1816. case MMToken::ExportAsKeyword:
  1817. parseExportAsDecl();
  1818. break;
  1819. case MMToken::UseKeyword:
  1820. parseUseDecl();
  1821. break;
  1822. case MMToken::RequiresKeyword:
  1823. parseRequiresDecl();
  1824. break;
  1825. case MMToken::TextualKeyword:
  1826. parseHeaderDecl(MMToken::TextualKeyword, consumeToken());
  1827. break;
  1828. case MMToken::UmbrellaKeyword: {
  1829. SourceLocation UmbrellaLoc = consumeToken();
  1830. if (Tok.is(MMToken::HeaderKeyword))
  1831. parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
  1832. else
  1833. parseUmbrellaDirDecl(UmbrellaLoc);
  1834. break;
  1835. }
  1836. case MMToken::ExcludeKeyword:
  1837. parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken());
  1838. break;
  1839. case MMToken::PrivateKeyword:
  1840. parseHeaderDecl(MMToken::PrivateKeyword, consumeToken());
  1841. break;
  1842. case MMToken::HeaderKeyword:
  1843. parseHeaderDecl(MMToken::HeaderKeyword, consumeToken());
  1844. break;
  1845. case MMToken::LinkKeyword:
  1846. parseLinkDecl();
  1847. break;
  1848. default:
  1849. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
  1850. consumeToken();
  1851. break;
  1852. }
  1853. } while (!Done);
  1854. if (Tok.is(MMToken::RBrace))
  1855. consumeToken();
  1856. else {
  1857. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  1858. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  1859. HadError = true;
  1860. }
  1861. // If the active module is a top-level framework, and there are no link
  1862. // libraries, automatically link against the framework.
  1863. if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
  1864. ActiveModule->LinkLibraries.empty()) {
  1865. inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
  1866. }
  1867. // If the module meets all requirements but is still unavailable, mark the
  1868. // whole tree as unavailable to prevent it from building.
  1869. if (!ActiveModule->IsAvailable && !ActiveModule->IsUnimportable &&
  1870. ActiveModule->Parent) {
  1871. ActiveModule->getTopLevelModule()->markUnavailable(/*Unimportable=*/false);
  1872. ActiveModule->getTopLevelModule()->MissingHeaders.append(
  1873. ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end());
  1874. }
  1875. // We're done parsing this module. Pop back to the previous module.
  1876. ActiveModule = PreviousActiveModule;
  1877. }
  1878. /// Parse an extern module declaration.
  1879. ///
  1880. /// extern module-declaration:
  1881. /// 'extern' 'module' module-id string-literal
  1882. void ModuleMapParser::parseExternModuleDecl() {
  1883. assert(Tok.is(MMToken::ExternKeyword));
  1884. SourceLocation ExternLoc = consumeToken(); // 'extern' keyword
  1885. // Parse 'module' keyword.
  1886. if (!Tok.is(MMToken::ModuleKeyword)) {
  1887. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
  1888. consumeToken();
  1889. HadError = true;
  1890. return;
  1891. }
  1892. consumeToken(); // 'module' keyword
  1893. // Parse the module name.
  1894. ModuleId Id;
  1895. if (parseModuleId(Id)) {
  1896. HadError = true;
  1897. return;
  1898. }
  1899. // Parse the referenced module map file name.
  1900. if (!Tok.is(MMToken::StringLiteral)) {
  1901. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
  1902. HadError = true;
  1903. return;
  1904. }
  1905. std::string FileName = std::string(Tok.getString());
  1906. consumeToken(); // filename
  1907. StringRef FileNameRef = FileName;
  1908. SmallString<128> ModuleMapFileName;
  1909. if (llvm::sys::path::is_relative(FileNameRef)) {
  1910. ModuleMapFileName += Directory->getName();
  1911. llvm::sys::path::append(ModuleMapFileName, FileName);
  1912. FileNameRef = ModuleMapFileName;
  1913. }
  1914. if (auto File = SourceMgr.getFileManager().getFile(FileNameRef))
  1915. Map.parseModuleMapFile(
  1916. *File, IsSystem,
  1917. Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd
  1918. ? Directory
  1919. : (*File)->getDir(),
  1920. FileID(), nullptr, ExternLoc);
  1921. }
  1922. /// Whether to add the requirement \p Feature to the module \p M.
  1923. ///
  1924. /// This preserves backwards compatibility for two hacks in the Darwin system
  1925. /// module map files:
  1926. ///
  1927. /// 1. The use of 'requires excluded' to make headers non-modular, which
  1928. /// should really be mapped to 'textual' now that we have this feature. We
  1929. /// drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to
  1930. /// true. Later, this bit will be used to map all the headers inside this
  1931. /// module to 'textual'.
  1932. ///
  1933. /// This affects Darwin.C.excluded (for assert.h) and Tcl.Private.
  1934. ///
  1935. /// 2. Removes a bogus cplusplus requirement from IOKit.avc. This requirement
  1936. /// was never correct and causes issues now that we check it, so drop it.
  1937. static bool shouldAddRequirement(Module *M, StringRef Feature,
  1938. bool &IsRequiresExcludedHack) {
  1939. if (Feature == "excluded" &&
  1940. (M->fullModuleNameIs({"Darwin", "C", "excluded"}) ||
  1941. M->fullModuleNameIs({"Tcl", "Private"}))) {
  1942. IsRequiresExcludedHack = true;
  1943. return false;
  1944. } else if (Feature == "cplusplus" && M->fullModuleNameIs({"IOKit", "avc"})) {
  1945. return false;
  1946. }
  1947. return true;
  1948. }
  1949. /// Parse a requires declaration.
  1950. ///
  1951. /// requires-declaration:
  1952. /// 'requires' feature-list
  1953. ///
  1954. /// feature-list:
  1955. /// feature ',' feature-list
  1956. /// feature
  1957. ///
  1958. /// feature:
  1959. /// '!'[opt] identifier
  1960. void ModuleMapParser::parseRequiresDecl() {
  1961. assert(Tok.is(MMToken::RequiresKeyword));
  1962. // Parse 'requires' keyword.
  1963. consumeToken();
  1964. // Parse the feature-list.
  1965. do {
  1966. bool RequiredState = true;
  1967. if (Tok.is(MMToken::Exclaim)) {
  1968. RequiredState = false;
  1969. consumeToken();
  1970. }
  1971. if (!Tok.is(MMToken::Identifier)) {
  1972. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
  1973. HadError = true;
  1974. return;
  1975. }
  1976. // Consume the feature name.
  1977. std::string Feature = std::string(Tok.getString());
  1978. consumeToken();
  1979. bool IsRequiresExcludedHack = false;
  1980. bool ShouldAddRequirement =
  1981. shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack);
  1982. if (IsRequiresExcludedHack)
  1983. UsesRequiresExcludedHack.insert(ActiveModule);
  1984. if (ShouldAddRequirement) {
  1985. // Add this feature.
  1986. ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts,
  1987. *Map.Target);
  1988. }
  1989. if (!Tok.is(MMToken::Comma))
  1990. break;
  1991. // Consume the comma.
  1992. consumeToken();
  1993. } while (true);
  1994. }
  1995. /// Parse a header declaration.
  1996. ///
  1997. /// header-declaration:
  1998. /// 'textual'[opt] 'header' string-literal
  1999. /// 'private' 'textual'[opt] 'header' string-literal
  2000. /// 'exclude' 'header' string-literal
  2001. /// 'umbrella' 'header' string-literal
  2002. ///
  2003. /// FIXME: Support 'private textual header'.
  2004. void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
  2005. SourceLocation LeadingLoc) {
  2006. // We've already consumed the first token.
  2007. ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
  2008. if (LeadingToken == MMToken::PrivateKeyword) {
  2009. Role = ModuleMap::PrivateHeader;
  2010. // 'private' may optionally be followed by 'textual'.
  2011. if (Tok.is(MMToken::TextualKeyword)) {
  2012. LeadingToken = Tok.Kind;
  2013. consumeToken();
  2014. }
  2015. } else if (LeadingToken == MMToken::ExcludeKeyword) {
  2016. Role = ModuleMap::ExcludedHeader;
  2017. }
  2018. if (LeadingToken == MMToken::TextualKeyword)
  2019. Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
  2020. if (UsesRequiresExcludedHack.count(ActiveModule)) {
  2021. // Mark this header 'textual' (see doc comment for
  2022. // Module::UsesRequiresExcludedHack).
  2023. Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
  2024. }
  2025. if (LeadingToken != MMToken::HeaderKeyword) {
  2026. if (!Tok.is(MMToken::HeaderKeyword)) {
  2027. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  2028. << (LeadingToken == MMToken::PrivateKeyword ? "private" :
  2029. LeadingToken == MMToken::ExcludeKeyword ? "exclude" :
  2030. LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella");
  2031. return;
  2032. }
  2033. consumeToken();
  2034. }
  2035. // Parse the header name.
  2036. if (!Tok.is(MMToken::StringLiteral)) {
  2037. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  2038. << "header";
  2039. HadError = true;
  2040. return;
  2041. }
  2042. Module::UnresolvedHeaderDirective Header;
  2043. Header.FileName = std::string(Tok.getString());
  2044. Header.FileNameLoc = consumeToken();
  2045. Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword;
  2046. Header.Kind = Map.headerRoleToKind(Role);
  2047. // Check whether we already have an umbrella.
  2048. if (Header.IsUmbrella && ActiveModule->Umbrella) {
  2049. Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
  2050. << ActiveModule->getFullModuleName();
  2051. HadError = true;
  2052. return;
  2053. }
  2054. // If we were given stat information, parse it so we can skip looking for
  2055. // the file.
  2056. if (Tok.is(MMToken::LBrace)) {
  2057. SourceLocation LBraceLoc = consumeToken();
  2058. while (!Tok.is(MMToken::RBrace) && !Tok.is(MMToken::EndOfFile)) {
  2059. enum Attribute { Size, ModTime, Unknown };
  2060. StringRef Str = Tok.getString();
  2061. SourceLocation Loc = consumeToken();
  2062. switch (llvm::StringSwitch<Attribute>(Str)
  2063. .Case("size", Size)
  2064. .Case("mtime", ModTime)
  2065. .Default(Unknown)) {
  2066. case Size:
  2067. if (Header.Size)
  2068. Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str;
  2069. if (!Tok.is(MMToken::IntegerLiteral)) {
  2070. Diags.Report(Tok.getLocation(),
  2071. diag::err_mmap_invalid_header_attribute_value) << Str;
  2072. skipUntil(MMToken::RBrace);
  2073. break;
  2074. }
  2075. Header.Size = Tok.getInteger();
  2076. consumeToken();
  2077. break;
  2078. case ModTime:
  2079. if (Header.ModTime)
  2080. Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str;
  2081. if (!Tok.is(MMToken::IntegerLiteral)) {
  2082. Diags.Report(Tok.getLocation(),
  2083. diag::err_mmap_invalid_header_attribute_value) << Str;
  2084. skipUntil(MMToken::RBrace);
  2085. break;
  2086. }
  2087. Header.ModTime = Tok.getInteger();
  2088. consumeToken();
  2089. break;
  2090. case Unknown:
  2091. Diags.Report(Loc, diag::err_mmap_expected_header_attribute);
  2092. skipUntil(MMToken::RBrace);
  2093. break;
  2094. }
  2095. }
  2096. if (Tok.is(MMToken::RBrace))
  2097. consumeToken();
  2098. else {
  2099. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  2100. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  2101. HadError = true;
  2102. }
  2103. }
  2104. bool NeedsFramework = false;
  2105. Map.addUnresolvedHeader(ActiveModule, std::move(Header), NeedsFramework);
  2106. if (NeedsFramework && ActiveModule)
  2107. Diags.Report(CurrModuleDeclLoc, diag::note_mmap_add_framework_keyword)
  2108. << ActiveModule->getFullModuleName()
  2109. << FixItHint::CreateReplacement(CurrModuleDeclLoc, "framework module");
  2110. }
  2111. static int compareModuleHeaders(const Module::Header *A,
  2112. const Module::Header *B) {
  2113. return A->NameAsWritten.compare(B->NameAsWritten);
  2114. }
  2115. /// Parse an umbrella directory declaration.
  2116. ///
  2117. /// umbrella-dir-declaration:
  2118. /// umbrella string-literal
  2119. void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
  2120. // Parse the directory name.
  2121. if (!Tok.is(MMToken::StringLiteral)) {
  2122. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
  2123. << "umbrella";
  2124. HadError = true;
  2125. return;
  2126. }
  2127. std::string DirName = std::string(Tok.getString());
  2128. std::string DirNameAsWritten = DirName;
  2129. SourceLocation DirNameLoc = consumeToken();
  2130. // Check whether we already have an umbrella.
  2131. if (ActiveModule->Umbrella) {
  2132. Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
  2133. << ActiveModule->getFullModuleName();
  2134. HadError = true;
  2135. return;
  2136. }
  2137. // Look for this file.
  2138. const DirectoryEntry *Dir = nullptr;
  2139. if (llvm::sys::path::is_absolute(DirName)) {
  2140. if (auto D = SourceMgr.getFileManager().getDirectory(DirName))
  2141. Dir = *D;
  2142. } else {
  2143. SmallString<128> PathName;
  2144. PathName = Directory->getName();
  2145. llvm::sys::path::append(PathName, DirName);
  2146. if (auto D = SourceMgr.getFileManager().getDirectory(PathName))
  2147. Dir = *D;
  2148. }
  2149. if (!Dir) {
  2150. Diags.Report(DirNameLoc, diag::warn_mmap_umbrella_dir_not_found)
  2151. << DirName;
  2152. return;
  2153. }
  2154. if (UsesRequiresExcludedHack.count(ActiveModule)) {
  2155. // Mark this header 'textual' (see doc comment for
  2156. // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the
  2157. // directory is relatively expensive, in practice this only applies to the
  2158. // uncommonly used Tcl module on Darwin platforms.
  2159. std::error_code EC;
  2160. SmallVector<Module::Header, 6> Headers;
  2161. llvm::vfs::FileSystem &FS =
  2162. SourceMgr.getFileManager().getVirtualFileSystem();
  2163. for (llvm::vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E;
  2164. I != E && !EC; I.increment(EC)) {
  2165. if (auto FE = SourceMgr.getFileManager().getOptionalFileRef(I->path())) {
  2166. Module::Header Header = {"", std::string(I->path()), FE};
  2167. Headers.push_back(std::move(Header));
  2168. }
  2169. }
  2170. // Sort header paths so that the pcm doesn't depend on iteration order.
  2171. llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders);
  2172. for (auto &Header : Headers)
  2173. Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader);
  2174. return;
  2175. }
  2176. if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
  2177. Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
  2178. << OwningModule->getFullModuleName();
  2179. HadError = true;
  2180. return;
  2181. }
  2182. // Record this umbrella directory.
  2183. Map.setUmbrellaDir(ActiveModule, Dir, DirNameAsWritten, DirName);
  2184. }
  2185. /// Parse a module export declaration.
  2186. ///
  2187. /// export-declaration:
  2188. /// 'export' wildcard-module-id
  2189. ///
  2190. /// wildcard-module-id:
  2191. /// identifier
  2192. /// '*'
  2193. /// identifier '.' wildcard-module-id
  2194. void ModuleMapParser::parseExportDecl() {
  2195. assert(Tok.is(MMToken::ExportKeyword));
  2196. SourceLocation ExportLoc = consumeToken();
  2197. // Parse the module-id with an optional wildcard at the end.
  2198. ModuleId ParsedModuleId;
  2199. bool Wildcard = false;
  2200. do {
  2201. // FIXME: Support string-literal module names here.
  2202. if (Tok.is(MMToken::Identifier)) {
  2203. ParsedModuleId.push_back(
  2204. std::make_pair(std::string(Tok.getString()), Tok.getLocation()));
  2205. consumeToken();
  2206. if (Tok.is(MMToken::Period)) {
  2207. consumeToken();
  2208. continue;
  2209. }
  2210. break;
  2211. }
  2212. if(Tok.is(MMToken::Star)) {
  2213. Wildcard = true;
  2214. consumeToken();
  2215. break;
  2216. }
  2217. Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
  2218. HadError = true;
  2219. return;
  2220. } while (true);
  2221. Module::UnresolvedExportDecl Unresolved = {
  2222. ExportLoc, ParsedModuleId, Wildcard
  2223. };
  2224. ActiveModule->UnresolvedExports.push_back(Unresolved);
  2225. }
  2226. /// Parse a module export_as declaration.
  2227. ///
  2228. /// export-as-declaration:
  2229. /// 'export_as' identifier
  2230. void ModuleMapParser::parseExportAsDecl() {
  2231. assert(Tok.is(MMToken::ExportAsKeyword));
  2232. consumeToken();
  2233. if (!Tok.is(MMToken::Identifier)) {
  2234. Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
  2235. HadError = true;
  2236. return;
  2237. }
  2238. if (ActiveModule->Parent) {
  2239. Diags.Report(Tok.getLocation(), diag::err_mmap_submodule_export_as);
  2240. consumeToken();
  2241. return;
  2242. }
  2243. if (!ActiveModule->ExportAsModule.empty()) {
  2244. if (ActiveModule->ExportAsModule == Tok.getString()) {
  2245. Diags.Report(Tok.getLocation(), diag::warn_mmap_redundant_export_as)
  2246. << ActiveModule->Name << Tok.getString();
  2247. } else {
  2248. Diags.Report(Tok.getLocation(), diag::err_mmap_conflicting_export_as)
  2249. << ActiveModule->Name << ActiveModule->ExportAsModule
  2250. << Tok.getString();
  2251. }
  2252. }
  2253. ActiveModule->ExportAsModule = std::string(Tok.getString());
  2254. Map.addLinkAsDependency(ActiveModule);
  2255. consumeToken();
  2256. }
  2257. /// Parse a module use declaration.
  2258. ///
  2259. /// use-declaration:
  2260. /// 'use' wildcard-module-id
  2261. void ModuleMapParser::parseUseDecl() {
  2262. assert(Tok.is(MMToken::UseKeyword));
  2263. auto KWLoc = consumeToken();
  2264. // Parse the module-id.
  2265. ModuleId ParsedModuleId;
  2266. parseModuleId(ParsedModuleId);
  2267. if (ActiveModule->Parent)
  2268. Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule);
  2269. else
  2270. ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
  2271. }
  2272. /// Parse a link declaration.
  2273. ///
  2274. /// module-declaration:
  2275. /// 'link' 'framework'[opt] string-literal
  2276. void ModuleMapParser::parseLinkDecl() {
  2277. assert(Tok.is(MMToken::LinkKeyword));
  2278. SourceLocation LinkLoc = consumeToken();
  2279. // Parse the optional 'framework' keyword.
  2280. bool IsFramework = false;
  2281. if (Tok.is(MMToken::FrameworkKeyword)) {
  2282. consumeToken();
  2283. IsFramework = true;
  2284. }
  2285. // Parse the library name
  2286. if (!Tok.is(MMToken::StringLiteral)) {
  2287. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
  2288. << IsFramework << SourceRange(LinkLoc);
  2289. HadError = true;
  2290. return;
  2291. }
  2292. std::string LibraryName = std::string(Tok.getString());
  2293. consumeToken();
  2294. ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
  2295. IsFramework));
  2296. }
  2297. /// Parse a configuration macro declaration.
  2298. ///
  2299. /// module-declaration:
  2300. /// 'config_macros' attributes[opt] config-macro-list?
  2301. ///
  2302. /// config-macro-list:
  2303. /// identifier (',' identifier)?
  2304. void ModuleMapParser::parseConfigMacros() {
  2305. assert(Tok.is(MMToken::ConfigMacros));
  2306. SourceLocation ConfigMacrosLoc = consumeToken();
  2307. // Only top-level modules can have configuration macros.
  2308. if (ActiveModule->Parent) {
  2309. Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
  2310. }
  2311. // Parse the optional attributes.
  2312. Attributes Attrs;
  2313. if (parseOptionalAttributes(Attrs))
  2314. return;
  2315. if (Attrs.IsExhaustive && !ActiveModule->Parent) {
  2316. ActiveModule->ConfigMacrosExhaustive = true;
  2317. }
  2318. // If we don't have an identifier, we're done.
  2319. // FIXME: Support macros with the same name as a keyword here.
  2320. if (!Tok.is(MMToken::Identifier))
  2321. return;
  2322. // Consume the first identifier.
  2323. if (!ActiveModule->Parent) {
  2324. ActiveModule->ConfigMacros.push_back(Tok.getString().str());
  2325. }
  2326. consumeToken();
  2327. do {
  2328. // If there's a comma, consume it.
  2329. if (!Tok.is(MMToken::Comma))
  2330. break;
  2331. consumeToken();
  2332. // We expect to see a macro name here.
  2333. // FIXME: Support macros with the same name as a keyword here.
  2334. if (!Tok.is(MMToken::Identifier)) {
  2335. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
  2336. break;
  2337. }
  2338. // Consume the macro name.
  2339. if (!ActiveModule->Parent) {
  2340. ActiveModule->ConfigMacros.push_back(Tok.getString().str());
  2341. }
  2342. consumeToken();
  2343. } while (true);
  2344. }
  2345. /// Format a module-id into a string.
  2346. static std::string formatModuleId(const ModuleId &Id) {
  2347. std::string result;
  2348. {
  2349. llvm::raw_string_ostream OS(result);
  2350. for (unsigned I = 0, N = Id.size(); I != N; ++I) {
  2351. if (I)
  2352. OS << ".";
  2353. OS << Id[I].first;
  2354. }
  2355. }
  2356. return result;
  2357. }
  2358. /// Parse a conflict declaration.
  2359. ///
  2360. /// module-declaration:
  2361. /// 'conflict' module-id ',' string-literal
  2362. void ModuleMapParser::parseConflict() {
  2363. assert(Tok.is(MMToken::Conflict));
  2364. SourceLocation ConflictLoc = consumeToken();
  2365. Module::UnresolvedConflict Conflict;
  2366. // Parse the module-id.
  2367. if (parseModuleId(Conflict.Id))
  2368. return;
  2369. // Parse the ','.
  2370. if (!Tok.is(MMToken::Comma)) {
  2371. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
  2372. << SourceRange(ConflictLoc);
  2373. return;
  2374. }
  2375. consumeToken();
  2376. // Parse the message.
  2377. if (!Tok.is(MMToken::StringLiteral)) {
  2378. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
  2379. << formatModuleId(Conflict.Id);
  2380. return;
  2381. }
  2382. Conflict.Message = Tok.getString().str();
  2383. consumeToken();
  2384. // Add this unresolved conflict.
  2385. ActiveModule->UnresolvedConflicts.push_back(Conflict);
  2386. }
  2387. /// Parse an inferred module declaration (wildcard modules).
  2388. ///
  2389. /// module-declaration:
  2390. /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
  2391. /// { inferred-module-member* }
  2392. ///
  2393. /// inferred-module-member:
  2394. /// 'export' '*'
  2395. /// 'exclude' identifier
  2396. void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
  2397. assert(Tok.is(MMToken::Star));
  2398. SourceLocation StarLoc = consumeToken();
  2399. bool Failed = false;
  2400. // Inferred modules must be submodules.
  2401. if (!ActiveModule && !Framework) {
  2402. Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
  2403. Failed = true;
  2404. }
  2405. if (ActiveModule) {
  2406. // Inferred modules must have umbrella directories.
  2407. if (!Failed && ActiveModule->IsAvailable &&
  2408. !ActiveModule->getUmbrellaDir()) {
  2409. Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
  2410. Failed = true;
  2411. }
  2412. // Check for redefinition of an inferred module.
  2413. if (!Failed && ActiveModule->InferSubmodules) {
  2414. Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
  2415. if (ActiveModule->InferredSubmoduleLoc.isValid())
  2416. Diags.Report(ActiveModule->InferredSubmoduleLoc,
  2417. diag::note_mmap_prev_definition);
  2418. Failed = true;
  2419. }
  2420. // Check for the 'framework' keyword, which is not permitted here.
  2421. if (Framework) {
  2422. Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
  2423. Framework = false;
  2424. }
  2425. } else if (Explicit) {
  2426. Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
  2427. Explicit = false;
  2428. }
  2429. // If there were any problems with this inferred submodule, skip its body.
  2430. if (Failed) {
  2431. if (Tok.is(MMToken::LBrace)) {
  2432. consumeToken();
  2433. skipUntil(MMToken::RBrace);
  2434. if (Tok.is(MMToken::RBrace))
  2435. consumeToken();
  2436. }
  2437. HadError = true;
  2438. return;
  2439. }
  2440. // Parse optional attributes.
  2441. Attributes Attrs;
  2442. if (parseOptionalAttributes(Attrs))
  2443. return;
  2444. if (ActiveModule) {
  2445. // Note that we have an inferred submodule.
  2446. ActiveModule->InferSubmodules = true;
  2447. ActiveModule->InferredSubmoduleLoc = StarLoc;
  2448. ActiveModule->InferExplicitSubmodules = Explicit;
  2449. } else {
  2450. // We'll be inferring framework modules for this directory.
  2451. Map.InferredDirectories[Directory].InferModules = true;
  2452. Map.InferredDirectories[Directory].Attrs = Attrs;
  2453. Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile;
  2454. // FIXME: Handle the 'framework' keyword.
  2455. }
  2456. // Parse the opening brace.
  2457. if (!Tok.is(MMToken::LBrace)) {
  2458. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
  2459. HadError = true;
  2460. return;
  2461. }
  2462. SourceLocation LBraceLoc = consumeToken();
  2463. // Parse the body of the inferred submodule.
  2464. bool Done = false;
  2465. do {
  2466. switch (Tok.Kind) {
  2467. case MMToken::EndOfFile:
  2468. case MMToken::RBrace:
  2469. Done = true;
  2470. break;
  2471. case MMToken::ExcludeKeyword:
  2472. if (ActiveModule) {
  2473. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
  2474. << (ActiveModule != nullptr);
  2475. consumeToken();
  2476. break;
  2477. }
  2478. consumeToken();
  2479. // FIXME: Support string-literal module names here.
  2480. if (!Tok.is(MMToken::Identifier)) {
  2481. Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
  2482. break;
  2483. }
  2484. Map.InferredDirectories[Directory].ExcludedModules.push_back(
  2485. std::string(Tok.getString()));
  2486. consumeToken();
  2487. break;
  2488. case MMToken::ExportKeyword:
  2489. if (!ActiveModule) {
  2490. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
  2491. << (ActiveModule != nullptr);
  2492. consumeToken();
  2493. break;
  2494. }
  2495. consumeToken();
  2496. if (Tok.is(MMToken::Star))
  2497. ActiveModule->InferExportWildcard = true;
  2498. else
  2499. Diags.Report(Tok.getLocation(),
  2500. diag::err_mmap_expected_export_wildcard);
  2501. consumeToken();
  2502. break;
  2503. case MMToken::ExplicitKeyword:
  2504. case MMToken::ModuleKeyword:
  2505. case MMToken::HeaderKeyword:
  2506. case MMToken::PrivateKeyword:
  2507. case MMToken::UmbrellaKeyword:
  2508. default:
  2509. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
  2510. << (ActiveModule != nullptr);
  2511. consumeToken();
  2512. break;
  2513. }
  2514. } while (!Done);
  2515. if (Tok.is(MMToken::RBrace))
  2516. consumeToken();
  2517. else {
  2518. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
  2519. Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
  2520. HadError = true;
  2521. }
  2522. }
  2523. /// Parse optional attributes.
  2524. ///
  2525. /// attributes:
  2526. /// attribute attributes
  2527. /// attribute
  2528. ///
  2529. /// attribute:
  2530. /// [ identifier ]
  2531. ///
  2532. /// \param Attrs Will be filled in with the parsed attributes.
  2533. ///
  2534. /// \returns true if an error occurred, false otherwise.
  2535. bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
  2536. bool HadError = false;
  2537. while (Tok.is(MMToken::LSquare)) {
  2538. // Consume the '['.
  2539. SourceLocation LSquareLoc = consumeToken();
  2540. // Check whether we have an attribute name here.
  2541. if (!Tok.is(MMToken::Identifier)) {
  2542. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
  2543. skipUntil(MMToken::RSquare);
  2544. if (Tok.is(MMToken::RSquare))
  2545. consumeToken();
  2546. HadError = true;
  2547. }
  2548. // Decode the attribute name.
  2549. AttributeKind Attribute
  2550. = llvm::StringSwitch<AttributeKind>(Tok.getString())
  2551. .Case("exhaustive", AT_exhaustive)
  2552. .Case("extern_c", AT_extern_c)
  2553. .Case("no_undeclared_includes", AT_no_undeclared_includes)
  2554. .Case("system", AT_system)
  2555. .Default(AT_unknown);
  2556. switch (Attribute) {
  2557. case AT_unknown:
  2558. Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
  2559. << Tok.getString();
  2560. break;
  2561. case AT_system:
  2562. Attrs.IsSystem = true;
  2563. break;
  2564. case AT_extern_c:
  2565. Attrs.IsExternC = true;
  2566. break;
  2567. case AT_exhaustive:
  2568. Attrs.IsExhaustive = true;
  2569. break;
  2570. case AT_no_undeclared_includes:
  2571. Attrs.NoUndeclaredIncludes = true;
  2572. break;
  2573. }
  2574. consumeToken();
  2575. // Consume the ']'.
  2576. if (!Tok.is(MMToken::RSquare)) {
  2577. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
  2578. Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
  2579. skipUntil(MMToken::RSquare);
  2580. HadError = true;
  2581. }
  2582. if (Tok.is(MMToken::RSquare))
  2583. consumeToken();
  2584. }
  2585. return HadError;
  2586. }
  2587. /// Parse a module map file.
  2588. ///
  2589. /// module-map-file:
  2590. /// module-declaration*
  2591. bool ModuleMapParser::parseModuleMapFile() {
  2592. do {
  2593. switch (Tok.Kind) {
  2594. case MMToken::EndOfFile:
  2595. return HadError;
  2596. case MMToken::ExplicitKeyword:
  2597. case MMToken::ExternKeyword:
  2598. case MMToken::ModuleKeyword:
  2599. case MMToken::FrameworkKeyword:
  2600. parseModuleDecl();
  2601. break;
  2602. case MMToken::Comma:
  2603. case MMToken::ConfigMacros:
  2604. case MMToken::Conflict:
  2605. case MMToken::Exclaim:
  2606. case MMToken::ExcludeKeyword:
  2607. case MMToken::ExportKeyword:
  2608. case MMToken::ExportAsKeyword:
  2609. case MMToken::HeaderKeyword:
  2610. case MMToken::Identifier:
  2611. case MMToken::LBrace:
  2612. case MMToken::LinkKeyword:
  2613. case MMToken::LSquare:
  2614. case MMToken::Period:
  2615. case MMToken::PrivateKeyword:
  2616. case MMToken::RBrace:
  2617. case MMToken::RSquare:
  2618. case MMToken::RequiresKeyword:
  2619. case MMToken::Star:
  2620. case MMToken::StringLiteral:
  2621. case MMToken::IntegerLiteral:
  2622. case MMToken::TextualKeyword:
  2623. case MMToken::UmbrellaKeyword:
  2624. case MMToken::UseKeyword:
  2625. Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
  2626. HadError = true;
  2627. consumeToken();
  2628. break;
  2629. }
  2630. } while (true);
  2631. }
  2632. bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
  2633. const DirectoryEntry *Dir, FileID ID,
  2634. unsigned *Offset,
  2635. SourceLocation ExternModuleLoc) {
  2636. assert(Target && "Missing target information");
  2637. llvm::DenseMap<const FileEntry *, bool>::iterator Known
  2638. = ParsedModuleMap.find(File);
  2639. if (Known != ParsedModuleMap.end())
  2640. return Known->second;
  2641. // If the module map file wasn't already entered, do so now.
  2642. if (ID.isInvalid()) {
  2643. auto FileCharacter =
  2644. IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap;
  2645. ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter);
  2646. }
  2647. assert(Target && "Missing target information");
  2648. std::optional<llvm::MemoryBufferRef> Buffer = SourceMgr.getBufferOrNone(ID);
  2649. if (!Buffer)
  2650. return ParsedModuleMap[File] = true;
  2651. assert((!Offset || *Offset <= Buffer->getBufferSize()) &&
  2652. "invalid buffer offset");
  2653. // Parse this module map file.
  2654. Lexer L(SourceMgr.getLocForStartOfFile(ID), MMapLangOpts,
  2655. Buffer->getBufferStart(),
  2656. Buffer->getBufferStart() + (Offset ? *Offset : 0),
  2657. Buffer->getBufferEnd());
  2658. SourceLocation Start = L.getSourceLocation();
  2659. ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir,
  2660. IsSystem);
  2661. bool Result = Parser.parseModuleMapFile();
  2662. ParsedModuleMap[File] = Result;
  2663. if (Offset) {
  2664. auto Loc = SourceMgr.getDecomposedLoc(Parser.getLocation());
  2665. assert(Loc.first == ID && "stopped in a different file?");
  2666. *Offset = Loc.second;
  2667. }
  2668. // Notify callbacks that we parsed it.
  2669. for (const auto &Cb : Callbacks)
  2670. Cb->moduleMapFileRead(Start, *File, IsSystem);
  2671. return Result;
  2672. }