HeaderSearch.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. //===- HeaderSearch.cpp - Resolve Header File Locations -------------------===//
  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 implements the DirectoryLookup and HeaderSearch interfaces.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Lex/HeaderSearch.h"
  13. #include "clang/Basic/Diagnostic.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/IdentifierTable.h"
  16. #include "clang/Basic/Module.h"
  17. #include "clang/Basic/SourceManager.h"
  18. #include "clang/Lex/DirectoryLookup.h"
  19. #include "clang/Lex/ExternalPreprocessorSource.h"
  20. #include "clang/Lex/HeaderMap.h"
  21. #include "clang/Lex/HeaderSearchOptions.h"
  22. #include "clang/Lex/LexDiagnostic.h"
  23. #include "clang/Lex/ModuleMap.h"
  24. #include "clang/Lex/Preprocessor.h"
  25. #include "llvm/ADT/APInt.h"
  26. #include "llvm/ADT/Hashing.h"
  27. #include "llvm/ADT/SmallString.h"
  28. #include "llvm/ADT/SmallVector.h"
  29. #include "llvm/ADT/Statistic.h"
  30. #include "llvm/ADT/StringRef.h"
  31. #include "llvm/ADT/STLExtras.h"
  32. #include "llvm/Support/Allocator.h"
  33. #include "llvm/Support/Capacity.h"
  34. #include "llvm/Support/Errc.h"
  35. #include "llvm/Support/ErrorHandling.h"
  36. #include "llvm/Support/FileSystem.h"
  37. #include "llvm/Support/Path.h"
  38. #include "llvm/Support/VirtualFileSystem.h"
  39. #include <algorithm>
  40. #include <cassert>
  41. #include <cstddef>
  42. #include <cstdio>
  43. #include <cstring>
  44. #include <string>
  45. #include <system_error>
  46. #include <utility>
  47. using namespace clang;
  48. #define DEBUG_TYPE "file-search"
  49. ALWAYS_ENABLED_STATISTIC(NumIncluded, "Number of attempted #includes.");
  50. ALWAYS_ENABLED_STATISTIC(
  51. NumMultiIncludeFileOptzn,
  52. "Number of #includes skipped due to the multi-include optimization.");
  53. ALWAYS_ENABLED_STATISTIC(NumFrameworkLookups, "Number of framework lookups.");
  54. ALWAYS_ENABLED_STATISTIC(NumSubFrameworkLookups,
  55. "Number of subframework lookups.");
  56. const IdentifierInfo *
  57. HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
  58. if (ControllingMacro) {
  59. if (ControllingMacro->isOutOfDate()) {
  60. assert(External && "We must have an external source if we have a "
  61. "controlling macro that is out of date.");
  62. External->updateOutOfDateIdentifier(
  63. *const_cast<IdentifierInfo *>(ControllingMacro));
  64. }
  65. return ControllingMacro;
  66. }
  67. if (!ControllingMacroID || !External)
  68. return nullptr;
  69. ControllingMacro = External->GetIdentifier(ControllingMacroID);
  70. return ControllingMacro;
  71. }
  72. ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() = default;
  73. HeaderSearch::HeaderSearch(std::shared_ptr<HeaderSearchOptions> HSOpts,
  74. SourceManager &SourceMgr, DiagnosticsEngine &Diags,
  75. const LangOptions &LangOpts,
  76. const TargetInfo *Target)
  77. : HSOpts(std::move(HSOpts)), Diags(Diags),
  78. FileMgr(SourceMgr.getFileManager()), FrameworkMap(64),
  79. ModMap(SourceMgr, Diags, LangOpts, Target, *this) {}
  80. void HeaderSearch::PrintStats() {
  81. llvm::errs() << "\n*** HeaderSearch Stats:\n"
  82. << FileInfo.size() << " files tracked.\n";
  83. unsigned NumOnceOnlyFiles = 0;
  84. for (unsigned i = 0, e = FileInfo.size(); i != e; ++i)
  85. NumOnceOnlyFiles += (FileInfo[i].isPragmaOnce || FileInfo[i].isImport);
  86. llvm::errs() << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
  87. llvm::errs() << " " << NumIncluded << " #include/#include_next/#import.\n"
  88. << " " << NumMultiIncludeFileOptzn
  89. << " #includes skipped due to the multi-include optimization.\n";
  90. llvm::errs() << NumFrameworkLookups << " framework lookups.\n"
  91. << NumSubFrameworkLookups << " subframework lookups.\n";
  92. }
  93. void HeaderSearch::SetSearchPaths(
  94. std::vector<DirectoryLookup> dirs, unsigned int angledDirIdx,
  95. unsigned int systemDirIdx, bool noCurDirSearch,
  96. llvm::DenseMap<unsigned int, unsigned int> searchDirToHSEntry) {
  97. assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
  98. "Directory indices are unordered");
  99. SearchDirs = std::move(dirs);
  100. SearchDirsUsage.assign(SearchDirs.size(), false);
  101. AngledDirIdx = angledDirIdx;
  102. SystemDirIdx = systemDirIdx;
  103. NoCurDirSearch = noCurDirSearch;
  104. SearchDirToHSEntry = std::move(searchDirToHSEntry);
  105. //LookupFileCache.clear();
  106. indexInitialHeaderMaps();
  107. }
  108. void HeaderSearch::AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
  109. unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
  110. SearchDirs.insert(SearchDirs.begin() + idx, dir);
  111. SearchDirsUsage.insert(SearchDirsUsage.begin() + idx, false);
  112. if (!isAngled)
  113. AngledDirIdx++;
  114. SystemDirIdx++;
  115. }
  116. std::vector<bool> HeaderSearch::computeUserEntryUsage() const {
  117. std::vector<bool> UserEntryUsage(HSOpts->UserEntries.size());
  118. for (unsigned I = 0, E = SearchDirsUsage.size(); I < E; ++I) {
  119. // Check whether this DirectoryLookup has been successfully used.
  120. if (SearchDirsUsage[I]) {
  121. auto UserEntryIdxIt = SearchDirToHSEntry.find(I);
  122. // Check whether this DirectoryLookup maps to a HeaderSearch::UserEntry.
  123. if (UserEntryIdxIt != SearchDirToHSEntry.end())
  124. UserEntryUsage[UserEntryIdxIt->second] = true;
  125. }
  126. }
  127. return UserEntryUsage;
  128. }
  129. /// CreateHeaderMap - This method returns a HeaderMap for the specified
  130. /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
  131. const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
  132. // We expect the number of headermaps to be small, and almost always empty.
  133. // If it ever grows, use of a linear search should be re-evaluated.
  134. if (!HeaderMaps.empty()) {
  135. for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
  136. // Pointer equality comparison of FileEntries works because they are
  137. // already uniqued by inode.
  138. if (HeaderMaps[i].first == FE)
  139. return HeaderMaps[i].second.get();
  140. }
  141. if (std::unique_ptr<HeaderMap> HM = HeaderMap::Create(FE, FileMgr)) {
  142. HeaderMaps.emplace_back(FE, std::move(HM));
  143. return HeaderMaps.back().second.get();
  144. }
  145. return nullptr;
  146. }
  147. /// Get filenames for all registered header maps.
  148. void HeaderSearch::getHeaderMapFileNames(
  149. SmallVectorImpl<std::string> &Names) const {
  150. for (auto &HM : HeaderMaps)
  151. Names.push_back(std::string(HM.first->getName()));
  152. }
  153. std::string HeaderSearch::getCachedModuleFileName(Module *Module) {
  154. OptionalFileEntryRef ModuleMap =
  155. getModuleMap().getModuleMapFileForUniquing(Module);
  156. // The ModuleMap maybe a nullptr, when we load a cached C++ module without
  157. // *.modulemap file. In this case, just return an empty string.
  158. if (!ModuleMap)
  159. return {};
  160. return getCachedModuleFileName(Module->Name, ModuleMap->getName());
  161. }
  162. std::string HeaderSearch::getPrebuiltModuleFileName(StringRef ModuleName,
  163. bool FileMapOnly) {
  164. // First check the module name to pcm file map.
  165. auto i(HSOpts->PrebuiltModuleFiles.find(ModuleName));
  166. if (i != HSOpts->PrebuiltModuleFiles.end())
  167. return i->second;
  168. if (FileMapOnly || HSOpts->PrebuiltModulePaths.empty())
  169. return {};
  170. // Then go through each prebuilt module directory and try to find the pcm
  171. // file.
  172. for (const std::string &Dir : HSOpts->PrebuiltModulePaths) {
  173. SmallString<256> Result(Dir);
  174. llvm::sys::fs::make_absolute(Result);
  175. if (ModuleName.contains(':'))
  176. // The separator of C++20 modules partitions (':') is not good for file
  177. // systems, here clang and gcc choose '-' by default since it is not a
  178. // valid character of C++ indentifiers. So we could avoid conflicts.
  179. llvm::sys::path::append(Result, ModuleName.split(':').first + "-" +
  180. ModuleName.split(':').second +
  181. ".pcm");
  182. else
  183. llvm::sys::path::append(Result, ModuleName + ".pcm");
  184. if (getFileMgr().getFile(Result.str()))
  185. return std::string(Result);
  186. }
  187. return {};
  188. }
  189. std::string HeaderSearch::getPrebuiltImplicitModuleFileName(Module *Module) {
  190. OptionalFileEntryRef ModuleMap =
  191. getModuleMap().getModuleMapFileForUniquing(Module);
  192. StringRef ModuleName = Module->Name;
  193. StringRef ModuleMapPath = ModuleMap->getName();
  194. StringRef ModuleCacheHash = HSOpts->DisableModuleHash ? "" : getModuleHash();
  195. for (const std::string &Dir : HSOpts->PrebuiltModulePaths) {
  196. SmallString<256> CachePath(Dir);
  197. llvm::sys::fs::make_absolute(CachePath);
  198. llvm::sys::path::append(CachePath, ModuleCacheHash);
  199. std::string FileName =
  200. getCachedModuleFileNameImpl(ModuleName, ModuleMapPath, CachePath);
  201. if (!FileName.empty() && getFileMgr().getFile(FileName))
  202. return FileName;
  203. }
  204. return {};
  205. }
  206. std::string HeaderSearch::getCachedModuleFileName(StringRef ModuleName,
  207. StringRef ModuleMapPath) {
  208. return getCachedModuleFileNameImpl(ModuleName, ModuleMapPath,
  209. getModuleCachePath());
  210. }
  211. std::string HeaderSearch::getCachedModuleFileNameImpl(StringRef ModuleName,
  212. StringRef ModuleMapPath,
  213. StringRef CachePath) {
  214. // If we don't have a module cache path or aren't supposed to use one, we
  215. // can't do anything.
  216. if (CachePath.empty())
  217. return {};
  218. SmallString<256> Result(CachePath);
  219. llvm::sys::fs::make_absolute(Result);
  220. if (HSOpts->DisableModuleHash) {
  221. llvm::sys::path::append(Result, ModuleName + ".pcm");
  222. } else {
  223. // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
  224. // ideally be globally unique to this particular module. Name collisions
  225. // in the hash are safe (because any translation unit can only import one
  226. // module with each name), but result in a loss of caching.
  227. //
  228. // To avoid false-negatives, we form as canonical a path as we can, and map
  229. // to lower-case in case we're on a case-insensitive file system.
  230. SmallString<128> CanonicalPath(ModuleMapPath);
  231. if (getModuleMap().canonicalizeModuleMapPath(CanonicalPath))
  232. return {};
  233. llvm::hash_code Hash = llvm::hash_combine(CanonicalPath.str().lower());
  234. SmallString<128> HashStr;
  235. llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
  236. llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
  237. }
  238. return Result.str().str();
  239. }
  240. Module *HeaderSearch::lookupModule(StringRef ModuleName,
  241. SourceLocation ImportLoc, bool AllowSearch,
  242. bool AllowExtraModuleMapSearch) {
  243. // Look in the module map to determine if there is a module by this name.
  244. Module *Module = ModMap.findModule(ModuleName);
  245. if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
  246. return Module;
  247. StringRef SearchName = ModuleName;
  248. Module = lookupModule(ModuleName, SearchName, ImportLoc,
  249. AllowExtraModuleMapSearch);
  250. // The facility for "private modules" -- adjacent, optional module maps named
  251. // module.private.modulemap that are supposed to define private submodules --
  252. // may have different flavors of names: FooPrivate, Foo_Private and Foo.Private.
  253. //
  254. // Foo.Private is now deprecated in favor of Foo_Private. Users of FooPrivate
  255. // should also rename to Foo_Private. Representing private as submodules
  256. // could force building unwanted dependencies into the parent module and cause
  257. // dependency cycles.
  258. if (!Module && SearchName.consume_back("_Private"))
  259. Module = lookupModule(ModuleName, SearchName, ImportLoc,
  260. AllowExtraModuleMapSearch);
  261. if (!Module && SearchName.consume_back("Private"))
  262. Module = lookupModule(ModuleName, SearchName, ImportLoc,
  263. AllowExtraModuleMapSearch);
  264. return Module;
  265. }
  266. Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
  267. SourceLocation ImportLoc,
  268. bool AllowExtraModuleMapSearch) {
  269. Module *Module = nullptr;
  270. // Look through the various header search paths to load any available module
  271. // maps, searching for a module map that describes this module.
  272. for (DirectoryLookup &Dir : search_dir_range()) {
  273. if (Dir.isFramework()) {
  274. // Search for or infer a module map for a framework. Here we use
  275. // SearchName rather than ModuleName, to permit finding private modules
  276. // named FooPrivate in buggy frameworks named Foo.
  277. SmallString<128> FrameworkDirName;
  278. FrameworkDirName += Dir.getFrameworkDir()->getName();
  279. llvm::sys::path::append(FrameworkDirName, SearchName + ".framework");
  280. if (auto FrameworkDir =
  281. FileMgr.getOptionalDirectoryRef(FrameworkDirName)) {
  282. bool IsSystem = Dir.getDirCharacteristic() != SrcMgr::C_User;
  283. Module = loadFrameworkModule(ModuleName, *FrameworkDir, IsSystem);
  284. if (Module)
  285. break;
  286. }
  287. }
  288. // FIXME: Figure out how header maps and module maps will work together.
  289. // Only deal with normal search directories.
  290. if (!Dir.isNormalDir())
  291. continue;
  292. bool IsSystem = Dir.isSystemHeaderDirectory();
  293. // Only returns std::nullopt if not a normal directory, which we just
  294. // checked
  295. DirectoryEntryRef NormalDir = *Dir.getDirRef();
  296. // Search for a module map file in this directory.
  297. if (loadModuleMapFile(NormalDir, IsSystem,
  298. /*IsFramework*/false) == LMM_NewlyLoaded) {
  299. // We just loaded a module map file; check whether the module is
  300. // available now.
  301. Module = ModMap.findModule(ModuleName);
  302. if (Module)
  303. break;
  304. }
  305. // Search for a module map in a subdirectory with the same name as the
  306. // module.
  307. SmallString<128> NestedModuleMapDirName;
  308. NestedModuleMapDirName = Dir.getDir()->getName();
  309. llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
  310. if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
  311. /*IsFramework*/false) == LMM_NewlyLoaded){
  312. // If we just loaded a module map file, look for the module again.
  313. Module = ModMap.findModule(ModuleName);
  314. if (Module)
  315. break;
  316. }
  317. // If we've already performed the exhaustive search for module maps in this
  318. // search directory, don't do it again.
  319. if (Dir.haveSearchedAllModuleMaps())
  320. continue;
  321. // Load all module maps in the immediate subdirectories of this search
  322. // directory if ModuleName was from @import.
  323. if (AllowExtraModuleMapSearch)
  324. loadSubdirectoryModuleMaps(Dir);
  325. // Look again for the module.
  326. Module = ModMap.findModule(ModuleName);
  327. if (Module)
  328. break;
  329. }
  330. return Module;
  331. }
  332. void HeaderSearch::indexInitialHeaderMaps() {
  333. llvm::StringMap<unsigned, llvm::BumpPtrAllocator> Index(SearchDirs.size());
  334. // Iterate over all filename keys and associate them with the index i.
  335. unsigned i = 0;
  336. for (; i != SearchDirs.size(); ++i) {
  337. auto &Dir = SearchDirs[i];
  338. // We're concerned with only the initial contiguous run of header
  339. // maps within SearchDirs, which can be 99% of SearchDirs when
  340. // SearchDirs.size() is ~10000.
  341. if (!Dir.isHeaderMap())
  342. break;
  343. // Give earlier keys precedence over identical later keys.
  344. auto Callback = [&](StringRef Filename) {
  345. Index.try_emplace(Filename.lower(), i);
  346. };
  347. Dir.getHeaderMap()->forEachKey(Callback);
  348. }
  349. SearchDirHeaderMapIndex = std::move(Index);
  350. FirstNonHeaderMapSearchDirIdx = i;
  351. }
  352. //===----------------------------------------------------------------------===//
  353. // File lookup within a DirectoryLookup scope
  354. //===----------------------------------------------------------------------===//
  355. /// getName - Return the directory or filename corresponding to this lookup
  356. /// object.
  357. StringRef DirectoryLookup::getName() const {
  358. // FIXME: Use the name from \c DirectoryEntryRef.
  359. if (isNormalDir())
  360. return getDir()->getName();
  361. if (isFramework())
  362. return getFrameworkDir()->getName();
  363. assert(isHeaderMap() && "Unknown DirectoryLookup");
  364. return getHeaderMap()->getFileName();
  365. }
  366. OptionalFileEntryRef HeaderSearch::getFileAndSuggestModule(
  367. StringRef FileName, SourceLocation IncludeLoc, const DirectoryEntry *Dir,
  368. bool IsSystemHeaderDir, Module *RequestingModule,
  369. ModuleMap::KnownHeader *SuggestedModule, bool OpenFile /*=true*/,
  370. bool CacheFailures /*=true*/) {
  371. // If we have a module map that might map this header, load it and
  372. // check whether we'll have a suggestion for a module.
  373. auto File = getFileMgr().getFileRef(FileName, OpenFile, CacheFailures);
  374. if (!File) {
  375. // For rare, surprising errors (e.g. "out of file handles"), diag the EC
  376. // message.
  377. std::error_code EC = llvm::errorToErrorCode(File.takeError());
  378. if (EC != llvm::errc::no_such_file_or_directory &&
  379. EC != llvm::errc::invalid_argument &&
  380. EC != llvm::errc::is_a_directory && EC != llvm::errc::not_a_directory) {
  381. Diags.Report(IncludeLoc, diag::err_cannot_open_file)
  382. << FileName << EC.message();
  383. }
  384. return std::nullopt;
  385. }
  386. // If there is a module that corresponds to this header, suggest it.
  387. if (!findUsableModuleForHeader(
  388. &File->getFileEntry(), Dir ? Dir : File->getFileEntry().getDir(),
  389. RequestingModule, SuggestedModule, IsSystemHeaderDir))
  390. return std::nullopt;
  391. return *File;
  392. }
  393. /// LookupFile - Lookup the specified file in this search path, returning it
  394. /// if it exists or returning null if not.
  395. OptionalFileEntryRef DirectoryLookup::LookupFile(
  396. StringRef &Filename, HeaderSearch &HS, SourceLocation IncludeLoc,
  397. SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
  398. Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
  399. bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound,
  400. bool &IsInHeaderMap, SmallVectorImpl<char> &MappedName,
  401. bool OpenFile) const {
  402. InUserSpecifiedSystemFramework = false;
  403. IsInHeaderMap = false;
  404. MappedName.clear();
  405. SmallString<1024> TmpDir;
  406. if (isNormalDir()) {
  407. // Concatenate the requested file onto the directory.
  408. TmpDir = getDirRef()->getName();
  409. llvm::sys::path::append(TmpDir, Filename);
  410. if (SearchPath) {
  411. StringRef SearchPathRef(getDirRef()->getName());
  412. SearchPath->clear();
  413. SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
  414. }
  415. if (RelativePath) {
  416. RelativePath->clear();
  417. RelativePath->append(Filename.begin(), Filename.end());
  418. }
  419. return HS.getFileAndSuggestModule(
  420. TmpDir, IncludeLoc, getDir(), isSystemHeaderDirectory(),
  421. RequestingModule, SuggestedModule, OpenFile);
  422. }
  423. if (isFramework())
  424. return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
  425. RequestingModule, SuggestedModule,
  426. InUserSpecifiedSystemFramework, IsFrameworkFound);
  427. assert(isHeaderMap() && "Unknown directory lookup");
  428. const HeaderMap *HM = getHeaderMap();
  429. SmallString<1024> Path;
  430. StringRef Dest = HM->lookupFilename(Filename, Path);
  431. if (Dest.empty())
  432. return std::nullopt;
  433. IsInHeaderMap = true;
  434. auto FixupSearchPath = [&]() {
  435. if (SearchPath) {
  436. StringRef SearchPathRef(getName());
  437. SearchPath->clear();
  438. SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
  439. }
  440. if (RelativePath) {
  441. RelativePath->clear();
  442. RelativePath->append(Filename.begin(), Filename.end());
  443. }
  444. };
  445. // Check if the headermap maps the filename to a framework include
  446. // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
  447. // framework include.
  448. if (llvm::sys::path::is_relative(Dest)) {
  449. MappedName.append(Dest.begin(), Dest.end());
  450. Filename = StringRef(MappedName.begin(), MappedName.size());
  451. Dest = HM->lookupFilename(Filename, Path);
  452. }
  453. if (auto Res = HS.getFileMgr().getOptionalFileRef(Dest, OpenFile)) {
  454. FixupSearchPath();
  455. return *Res;
  456. }
  457. // Header maps need to be marked as used whenever the filename matches.
  458. // The case where the target file **exists** is handled by callee of this
  459. // function as part of the regular logic that applies to include search paths.
  460. // The case where the target file **does not exist** is handled here:
  461. HS.noteLookupUsage(HS.searchDirIdx(*this), IncludeLoc);
  462. return std::nullopt;
  463. }
  464. /// Given a framework directory, find the top-most framework directory.
  465. ///
  466. /// \param FileMgr The file manager to use for directory lookups.
  467. /// \param DirName The name of the framework directory.
  468. /// \param SubmodulePath Will be populated with the submodule path from the
  469. /// returned top-level module to the originally named framework.
  470. static OptionalDirectoryEntryRef
  471. getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
  472. SmallVectorImpl<std::string> &SubmodulePath) {
  473. assert(llvm::sys::path::extension(DirName) == ".framework" &&
  474. "Not a framework directory");
  475. // Note: as an egregious but useful hack we use the real path here, because
  476. // frameworks moving between top-level frameworks to embedded frameworks tend
  477. // to be symlinked, and we base the logical structure of modules on the
  478. // physical layout. In particular, we need to deal with crazy includes like
  479. //
  480. // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
  481. //
  482. // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
  483. // which one should access with, e.g.,
  484. //
  485. // #include <Bar/Wibble.h>
  486. //
  487. // Similar issues occur when a top-level framework has moved into an
  488. // embedded framework.
  489. auto TopFrameworkDir = FileMgr.getOptionalDirectoryRef(DirName);
  490. if (TopFrameworkDir)
  491. DirName = FileMgr.getCanonicalName(*TopFrameworkDir);
  492. do {
  493. // Get the parent directory name.
  494. DirName = llvm::sys::path::parent_path(DirName);
  495. if (DirName.empty())
  496. break;
  497. // Determine whether this directory exists.
  498. auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
  499. if (!Dir)
  500. break;
  501. // If this is a framework directory, then we're a subframework of this
  502. // framework.
  503. if (llvm::sys::path::extension(DirName) == ".framework") {
  504. SubmodulePath.push_back(std::string(llvm::sys::path::stem(DirName)));
  505. TopFrameworkDir = *Dir;
  506. }
  507. } while (true);
  508. return TopFrameworkDir;
  509. }
  510. static bool needModuleLookup(Module *RequestingModule,
  511. bool HasSuggestedModule) {
  512. return HasSuggestedModule ||
  513. (RequestingModule && RequestingModule->NoUndeclaredIncludes);
  514. }
  515. /// DoFrameworkLookup - Do a lookup of the specified file in the current
  516. /// DirectoryLookup, which is a framework directory.
  517. OptionalFileEntryRef DirectoryLookup::DoFrameworkLookup(
  518. StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
  519. SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
  520. ModuleMap::KnownHeader *SuggestedModule,
  521. bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound) const {
  522. FileManager &FileMgr = HS.getFileMgr();
  523. // Framework names must have a '/' in the filename.
  524. size_t SlashPos = Filename.find('/');
  525. if (SlashPos == StringRef::npos)
  526. return std::nullopt;
  527. // Find out if this is the home for the specified framework, by checking
  528. // HeaderSearch. Possible answers are yes/no and unknown.
  529. FrameworkCacheEntry &CacheEntry =
  530. HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
  531. // If it is known and in some other directory, fail.
  532. if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDirRef())
  533. return std::nullopt;
  534. // Otherwise, construct the path to this framework dir.
  535. // FrameworkName = "/System/Library/Frameworks/"
  536. SmallString<1024> FrameworkName;
  537. FrameworkName += getFrameworkDirRef()->getName();
  538. if (FrameworkName.empty() || FrameworkName.back() != '/')
  539. FrameworkName.push_back('/');
  540. // FrameworkName = "/System/Library/Frameworks/Cocoa"
  541. StringRef ModuleName(Filename.begin(), SlashPos);
  542. FrameworkName += ModuleName;
  543. // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
  544. FrameworkName += ".framework/";
  545. // If the cache entry was unresolved, populate it now.
  546. if (!CacheEntry.Directory) {
  547. ++NumFrameworkLookups;
  548. // If the framework dir doesn't exist, we fail.
  549. auto Dir = FileMgr.getDirectory(FrameworkName);
  550. if (!Dir)
  551. return std::nullopt;
  552. // Otherwise, if it does, remember that this is the right direntry for this
  553. // framework.
  554. CacheEntry.Directory = getFrameworkDirRef();
  555. // If this is a user search directory, check if the framework has been
  556. // user-specified as a system framework.
  557. if (getDirCharacteristic() == SrcMgr::C_User) {
  558. SmallString<1024> SystemFrameworkMarker(FrameworkName);
  559. SystemFrameworkMarker += ".system_framework";
  560. if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
  561. CacheEntry.IsUserSpecifiedSystemFramework = true;
  562. }
  563. }
  564. }
  565. // Set out flags.
  566. InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
  567. IsFrameworkFound = CacheEntry.Directory.has_value();
  568. if (RelativePath) {
  569. RelativePath->clear();
  570. RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
  571. }
  572. // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
  573. unsigned OrigSize = FrameworkName.size();
  574. FrameworkName += "Headers/";
  575. if (SearchPath) {
  576. SearchPath->clear();
  577. // Without trailing '/'.
  578. SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
  579. }
  580. FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
  581. auto File =
  582. FileMgr.getOptionalFileRef(FrameworkName, /*OpenFile=*/!SuggestedModule);
  583. if (!File) {
  584. // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
  585. const char *Private = "Private";
  586. FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
  587. Private+strlen(Private));
  588. if (SearchPath)
  589. SearchPath->insert(SearchPath->begin()+OrigSize, Private,
  590. Private+strlen(Private));
  591. File = FileMgr.getOptionalFileRef(FrameworkName,
  592. /*OpenFile=*/!SuggestedModule);
  593. }
  594. // If we found the header and are allowed to suggest a module, do so now.
  595. if (File && needModuleLookup(RequestingModule, SuggestedModule)) {
  596. // Find the framework in which this header occurs.
  597. StringRef FrameworkPath = File->getFileEntry().getDir()->getName();
  598. bool FoundFramework = false;
  599. do {
  600. // Determine whether this directory exists.
  601. auto Dir = FileMgr.getDirectory(FrameworkPath);
  602. if (!Dir)
  603. break;
  604. // If this is a framework directory, then we're a subframework of this
  605. // framework.
  606. if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
  607. FoundFramework = true;
  608. break;
  609. }
  610. // Get the parent directory name.
  611. FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
  612. if (FrameworkPath.empty())
  613. break;
  614. } while (true);
  615. bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
  616. if (FoundFramework) {
  617. if (!HS.findUsableModuleForFrameworkHeader(
  618. &File->getFileEntry(), FrameworkPath, RequestingModule,
  619. SuggestedModule, IsSystem))
  620. return std::nullopt;
  621. } else {
  622. if (!HS.findUsableModuleForHeader(&File->getFileEntry(), getDir(),
  623. RequestingModule, SuggestedModule,
  624. IsSystem))
  625. return std::nullopt;
  626. }
  627. }
  628. if (File)
  629. return *File;
  630. return std::nullopt;
  631. }
  632. void HeaderSearch::cacheLookupSuccess(LookupFileCacheInfo &CacheLookup,
  633. ConstSearchDirIterator HitIt,
  634. SourceLocation Loc) {
  635. CacheLookup.HitIt = HitIt;
  636. noteLookupUsage(HitIt.Idx, Loc);
  637. }
  638. void HeaderSearch::noteLookupUsage(unsigned HitIdx, SourceLocation Loc) {
  639. SearchDirsUsage[HitIdx] = true;
  640. auto UserEntryIdxIt = SearchDirToHSEntry.find(HitIdx);
  641. if (UserEntryIdxIt != SearchDirToHSEntry.end())
  642. Diags.Report(Loc, diag::remark_pp_search_path_usage)
  643. << HSOpts->UserEntries[UserEntryIdxIt->second].Path;
  644. }
  645. void HeaderSearch::setTarget(const TargetInfo &Target) {
  646. ModMap.setTarget(Target);
  647. }
  648. //===----------------------------------------------------------------------===//
  649. // Header File Location.
  650. //===----------------------------------------------------------------------===//
  651. /// Return true with a diagnostic if the file that MSVC would have found
  652. /// fails to match the one that Clang would have found with MSVC header search
  653. /// disabled.
  654. static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
  655. const FileEntry *MSFE, const FileEntry *FE,
  656. SourceLocation IncludeLoc) {
  657. if (MSFE && FE != MSFE) {
  658. Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
  659. return true;
  660. }
  661. return false;
  662. }
  663. static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
  664. assert(!Str.empty());
  665. char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
  666. std::copy(Str.begin(), Str.end(), CopyStr);
  667. CopyStr[Str.size()] = '\0';
  668. return CopyStr;
  669. }
  670. static bool isFrameworkStylePath(StringRef Path, bool &IsPrivateHeader,
  671. SmallVectorImpl<char> &FrameworkName,
  672. SmallVectorImpl<char> &IncludeSpelling) {
  673. using namespace llvm::sys;
  674. path::const_iterator I = path::begin(Path);
  675. path::const_iterator E = path::end(Path);
  676. IsPrivateHeader = false;
  677. // Detect different types of framework style paths:
  678. //
  679. // ...Foo.framework/{Headers,PrivateHeaders}
  680. // ...Foo.framework/Versions/{A,Current}/{Headers,PrivateHeaders}
  681. // ...Foo.framework/Frameworks/Nested.framework/{Headers,PrivateHeaders}
  682. // ...<other variations with 'Versions' like in the above path>
  683. //
  684. // and some other variations among these lines.
  685. int FoundComp = 0;
  686. while (I != E) {
  687. if (*I == "Headers") {
  688. ++FoundComp;
  689. } else if (*I == "PrivateHeaders") {
  690. ++FoundComp;
  691. IsPrivateHeader = true;
  692. } else if (I->endswith(".framework")) {
  693. StringRef Name = I->drop_back(10); // Drop .framework
  694. // Need to reset the strings and counter to support nested frameworks.
  695. FrameworkName.clear();
  696. FrameworkName.append(Name.begin(), Name.end());
  697. IncludeSpelling.clear();
  698. IncludeSpelling.append(Name.begin(), Name.end());
  699. FoundComp = 1;
  700. } else if (FoundComp >= 2) {
  701. IncludeSpelling.push_back('/');
  702. IncludeSpelling.append(I->begin(), I->end());
  703. }
  704. ++I;
  705. }
  706. return !FrameworkName.empty() && FoundComp >= 2;
  707. }
  708. static void
  709. diagnoseFrameworkInclude(DiagnosticsEngine &Diags, SourceLocation IncludeLoc,
  710. StringRef Includer, StringRef IncludeFilename,
  711. const FileEntry *IncludeFE, bool isAngled = false,
  712. bool FoundByHeaderMap = false) {
  713. bool IsIncluderPrivateHeader = false;
  714. SmallString<128> FromFramework, ToFramework;
  715. SmallString<128> FromIncludeSpelling, ToIncludeSpelling;
  716. if (!isFrameworkStylePath(Includer, IsIncluderPrivateHeader, FromFramework,
  717. FromIncludeSpelling))
  718. return;
  719. bool IsIncludeePrivateHeader = false;
  720. bool IsIncludeeInFramework =
  721. isFrameworkStylePath(IncludeFE->getName(), IsIncludeePrivateHeader,
  722. ToFramework, ToIncludeSpelling);
  723. if (!isAngled && !FoundByHeaderMap) {
  724. SmallString<128> NewInclude("<");
  725. if (IsIncludeeInFramework) {
  726. NewInclude += ToIncludeSpelling;
  727. NewInclude += ">";
  728. } else {
  729. NewInclude += IncludeFilename;
  730. NewInclude += ">";
  731. }
  732. Diags.Report(IncludeLoc, diag::warn_quoted_include_in_framework_header)
  733. << IncludeFilename
  734. << FixItHint::CreateReplacement(IncludeLoc, NewInclude);
  735. }
  736. // Headers in Foo.framework/Headers should not include headers
  737. // from Foo.framework/PrivateHeaders, since this violates public/private
  738. // API boundaries and can cause modular dependency cycles.
  739. if (!IsIncluderPrivateHeader && IsIncludeeInFramework &&
  740. IsIncludeePrivateHeader && FromFramework == ToFramework)
  741. Diags.Report(IncludeLoc, diag::warn_framework_include_private_from_public)
  742. << IncludeFilename;
  743. }
  744. /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
  745. /// return null on failure. isAngled indicates whether the file reference is
  746. /// for system \#include's or not (i.e. using <> instead of ""). Includers, if
  747. /// non-empty, indicates where the \#including file(s) are, in case a relative
  748. /// search is needed. Microsoft mode will pass all \#including files.
  749. OptionalFileEntryRef HeaderSearch::LookupFile(
  750. StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
  751. ConstSearchDirIterator FromDir, ConstSearchDirIterator *CurDirArg,
  752. ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
  753. SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
  754. Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
  755. bool *IsMapped, bool *IsFrameworkFound, bool SkipCache,
  756. bool BuildSystemModule, bool OpenFile, bool CacheFailures) {
  757. ConstSearchDirIterator CurDirLocal = nullptr;
  758. ConstSearchDirIterator &CurDir = CurDirArg ? *CurDirArg : CurDirLocal;
  759. if (IsMapped)
  760. *IsMapped = false;
  761. if (IsFrameworkFound)
  762. *IsFrameworkFound = false;
  763. if (SuggestedModule)
  764. *SuggestedModule = ModuleMap::KnownHeader();
  765. // If 'Filename' is absolute, check to see if it exists and no searching.
  766. if (llvm::sys::path::is_absolute(Filename)) {
  767. CurDir = nullptr;
  768. // If this was an #include_next "/absolute/file", fail.
  769. if (FromDir)
  770. return std::nullopt;
  771. if (SearchPath)
  772. SearchPath->clear();
  773. if (RelativePath) {
  774. RelativePath->clear();
  775. RelativePath->append(Filename.begin(), Filename.end());
  776. }
  777. // Otherwise, just return the file.
  778. return getFileAndSuggestModule(Filename, IncludeLoc, nullptr,
  779. /*IsSystemHeaderDir*/ false,
  780. RequestingModule, SuggestedModule, OpenFile,
  781. CacheFailures);
  782. }
  783. // This is the header that MSVC's header search would have found.
  784. ModuleMap::KnownHeader MSSuggestedModule;
  785. OptionalFileEntryRef MSFE;
  786. // Unless disabled, check to see if the file is in the #includer's
  787. // directory. This cannot be based on CurDir, because each includer could be
  788. // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
  789. // include of "baz.h" should resolve to "whatever/foo/baz.h".
  790. // This search is not done for <> headers.
  791. if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
  792. SmallString<1024> TmpDir;
  793. bool First = true;
  794. for (const auto &IncluderAndDir : Includers) {
  795. const FileEntry *Includer = IncluderAndDir.first;
  796. // Concatenate the requested file onto the directory.
  797. // FIXME: Portability. Filename concatenation should be in sys::Path.
  798. TmpDir = IncluderAndDir.second->getName();
  799. TmpDir.push_back('/');
  800. TmpDir.append(Filename.begin(), Filename.end());
  801. // FIXME: We don't cache the result of getFileInfo across the call to
  802. // getFileAndSuggestModule, because it's a reference to an element of
  803. // a container that could be reallocated across this call.
  804. //
  805. // If we have no includer, that means we're processing a #include
  806. // from a module build. We should treat this as a system header if we're
  807. // building a [system] module.
  808. bool IncluderIsSystemHeader =
  809. Includer ? getFileInfo(Includer).DirInfo != SrcMgr::C_User :
  810. BuildSystemModule;
  811. if (OptionalFileEntryRef FE = getFileAndSuggestModule(
  812. TmpDir, IncludeLoc, IncluderAndDir.second, IncluderIsSystemHeader,
  813. RequestingModule, SuggestedModule)) {
  814. if (!Includer) {
  815. assert(First && "only first includer can have no file");
  816. return FE;
  817. }
  818. // Leave CurDir unset.
  819. // This file is a system header or C++ unfriendly if the old file is.
  820. //
  821. // Note that we only use one of FromHFI/ToHFI at once, due to potential
  822. // reallocation of the underlying vector potentially making the first
  823. // reference binding dangling.
  824. HeaderFileInfo &FromHFI = getFileInfo(Includer);
  825. unsigned DirInfo = FromHFI.DirInfo;
  826. bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
  827. StringRef Framework = FromHFI.Framework;
  828. HeaderFileInfo &ToHFI = getFileInfo(&FE->getFileEntry());
  829. ToHFI.DirInfo = DirInfo;
  830. ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
  831. ToHFI.Framework = Framework;
  832. if (SearchPath) {
  833. StringRef SearchPathRef(IncluderAndDir.second->getName());
  834. SearchPath->clear();
  835. SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
  836. }
  837. if (RelativePath) {
  838. RelativePath->clear();
  839. RelativePath->append(Filename.begin(), Filename.end());
  840. }
  841. if (First) {
  842. diagnoseFrameworkInclude(Diags, IncludeLoc,
  843. IncluderAndDir.second->getName(), Filename,
  844. &FE->getFileEntry());
  845. return FE;
  846. }
  847. // Otherwise, we found the path via MSVC header search rules. If
  848. // -Wmsvc-include is enabled, we have to keep searching to see if we
  849. // would've found this header in -I or -isystem directories.
  850. if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
  851. return FE;
  852. } else {
  853. MSFE = FE;
  854. if (SuggestedModule) {
  855. MSSuggestedModule = *SuggestedModule;
  856. *SuggestedModule = ModuleMap::KnownHeader();
  857. }
  858. break;
  859. }
  860. }
  861. First = false;
  862. }
  863. }
  864. CurDir = nullptr;
  865. // If this is a system #include, ignore the user #include locs.
  866. ConstSearchDirIterator It =
  867. isAngled ? angled_dir_begin() : search_dir_begin();
  868. // If this is a #include_next request, start searching after the directory the
  869. // file was found in.
  870. if (FromDir)
  871. It = FromDir;
  872. // Cache all of the lookups performed by this method. Many headers are
  873. // multiply included, and the "pragma once" optimization prevents them from
  874. // being relex/pp'd, but they would still have to search through a
  875. // (potentially huge) series of SearchDirs to find it.
  876. LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
  877. ConstSearchDirIterator NextIt = std::next(It);
  878. if (!SkipCache) {
  879. if (CacheLookup.StartIt == NextIt) {
  880. // HIT: Skip querying potentially lots of directories for this lookup.
  881. if (CacheLookup.HitIt)
  882. It = CacheLookup.HitIt;
  883. if (CacheLookup.MappedName) {
  884. Filename = CacheLookup.MappedName;
  885. if (IsMapped)
  886. *IsMapped = true;
  887. }
  888. } else {
  889. // MISS: This is the first query, or the previous query didn't match
  890. // our search start. We will fill in our found location below, so prime
  891. // the start point value.
  892. CacheLookup.reset(/*NewStartIt=*/NextIt);
  893. if (It == search_dir_begin() && FirstNonHeaderMapSearchDirIdx > 0) {
  894. // Handle cold misses of user includes in the presence of many header
  895. // maps. We avoid searching perhaps thousands of header maps by
  896. // jumping directly to the correct one or jumping beyond all of them.
  897. auto Iter = SearchDirHeaderMapIndex.find(Filename.lower());
  898. if (Iter == SearchDirHeaderMapIndex.end())
  899. // Not in index => Skip to first SearchDir after initial header maps
  900. It = search_dir_nth(FirstNonHeaderMapSearchDirIdx);
  901. else
  902. // In index => Start with a specific header map
  903. It = search_dir_nth(Iter->second);
  904. }
  905. }
  906. } else
  907. CacheLookup.reset(/*NewStartIt=*/NextIt);
  908. SmallString<64> MappedName;
  909. // Check each directory in sequence to see if it contains this file.
  910. for (; It != search_dir_end(); ++It) {
  911. bool InUserSpecifiedSystemFramework = false;
  912. bool IsInHeaderMap = false;
  913. bool IsFrameworkFoundInDir = false;
  914. OptionalFileEntryRef File = It->LookupFile(
  915. Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
  916. SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
  917. IsInHeaderMap, MappedName, OpenFile);
  918. if (!MappedName.empty()) {
  919. assert(IsInHeaderMap && "MappedName should come from a header map");
  920. CacheLookup.MappedName =
  921. copyString(MappedName, LookupFileCache.getAllocator());
  922. }
  923. if (IsMapped)
  924. // A filename is mapped when a header map remapped it to a relative path
  925. // used in subsequent header search or to an absolute path pointing to an
  926. // existing file.
  927. *IsMapped |= (!MappedName.empty() || (IsInHeaderMap && File));
  928. if (IsFrameworkFound)
  929. // Because we keep a filename remapped for subsequent search directory
  930. // lookups, ignore IsFrameworkFoundInDir after the first remapping and not
  931. // just for remapping in a current search directory.
  932. *IsFrameworkFound |= (IsFrameworkFoundInDir && !CacheLookup.MappedName);
  933. if (!File)
  934. continue;
  935. CurDir = It;
  936. const auto FE = &File->getFileEntry();
  937. IncludeNames[FE] = Filename;
  938. // This file is a system header or C++ unfriendly if the dir is.
  939. HeaderFileInfo &HFI = getFileInfo(FE);
  940. HFI.DirInfo = CurDir->getDirCharacteristic();
  941. // If the directory characteristic is User but this framework was
  942. // user-specified to be treated as a system framework, promote the
  943. // characteristic.
  944. if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
  945. HFI.DirInfo = SrcMgr::C_System;
  946. // If the filename matches a known system header prefix, override
  947. // whether the file is a system header.
  948. for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
  949. if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
  950. HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
  951. : SrcMgr::C_User;
  952. break;
  953. }
  954. }
  955. // Set the `Framework` info if this file is in a header map with framework
  956. // style include spelling or found in a framework dir. The header map case
  957. // is possible when building frameworks which use header maps.
  958. if (CurDir->isHeaderMap() && isAngled) {
  959. size_t SlashPos = Filename.find('/');
  960. if (SlashPos != StringRef::npos)
  961. HFI.Framework =
  962. getUniqueFrameworkName(StringRef(Filename.begin(), SlashPos));
  963. if (CurDir->isIndexHeaderMap())
  964. HFI.IndexHeaderMapHeader = 1;
  965. } else if (CurDir->isFramework()) {
  966. size_t SlashPos = Filename.find('/');
  967. if (SlashPos != StringRef::npos)
  968. HFI.Framework =
  969. getUniqueFrameworkName(StringRef(Filename.begin(), SlashPos));
  970. }
  971. if (checkMSVCHeaderSearch(Diags, MSFE ? &MSFE->getFileEntry() : nullptr,
  972. &File->getFileEntry(), IncludeLoc)) {
  973. if (SuggestedModule)
  974. *SuggestedModule = MSSuggestedModule;
  975. return MSFE;
  976. }
  977. bool FoundByHeaderMap = !IsMapped ? false : *IsMapped;
  978. if (!Includers.empty())
  979. diagnoseFrameworkInclude(
  980. Diags, IncludeLoc, Includers.front().second->getName(), Filename,
  981. &File->getFileEntry(), isAngled, FoundByHeaderMap);
  982. // Remember this location for the next lookup we do.
  983. cacheLookupSuccess(CacheLookup, It, IncludeLoc);
  984. return File;
  985. }
  986. // If we are including a file with a quoted include "foo.h" from inside
  987. // a header in a framework that is currently being built, and we couldn't
  988. // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
  989. // "Foo" is the name of the framework in which the including header was found.
  990. if (!Includers.empty() && Includers.front().first && !isAngled &&
  991. !Filename.contains('/')) {
  992. HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
  993. if (IncludingHFI.IndexHeaderMapHeader) {
  994. SmallString<128> ScratchFilename;
  995. ScratchFilename += IncludingHFI.Framework;
  996. ScratchFilename += '/';
  997. ScratchFilename += Filename;
  998. OptionalFileEntryRef File = LookupFile(
  999. ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, &CurDir,
  1000. Includers.front(), SearchPath, RelativePath, RequestingModule,
  1001. SuggestedModule, IsMapped, /*IsFrameworkFound=*/nullptr);
  1002. if (checkMSVCHeaderSearch(Diags, MSFE ? &MSFE->getFileEntry() : nullptr,
  1003. File ? &File->getFileEntry() : nullptr,
  1004. IncludeLoc)) {
  1005. if (SuggestedModule)
  1006. *SuggestedModule = MSSuggestedModule;
  1007. return MSFE;
  1008. }
  1009. cacheLookupSuccess(LookupFileCache[Filename],
  1010. LookupFileCache[ScratchFilename].HitIt, IncludeLoc);
  1011. // FIXME: SuggestedModule.
  1012. return File;
  1013. }
  1014. }
  1015. if (checkMSVCHeaderSearch(Diags, MSFE ? &MSFE->getFileEntry() : nullptr,
  1016. nullptr, IncludeLoc)) {
  1017. if (SuggestedModule)
  1018. *SuggestedModule = MSSuggestedModule;
  1019. return MSFE;
  1020. }
  1021. // Otherwise, didn't find it. Remember we didn't find this.
  1022. CacheLookup.HitIt = search_dir_end();
  1023. return std::nullopt;
  1024. }
  1025. /// LookupSubframeworkHeader - Look up a subframework for the specified
  1026. /// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
  1027. /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
  1028. /// is a subframework within Carbon.framework. If so, return the FileEntry
  1029. /// for the designated file, otherwise return null.
  1030. OptionalFileEntryRef HeaderSearch::LookupSubframeworkHeader(
  1031. StringRef Filename, const FileEntry *ContextFileEnt,
  1032. SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
  1033. Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
  1034. assert(ContextFileEnt && "No context file?");
  1035. // Framework names must have a '/' in the filename. Find it.
  1036. // FIXME: Should we permit '\' on Windows?
  1037. size_t SlashPos = Filename.find('/');
  1038. if (SlashPos == StringRef::npos)
  1039. return std::nullopt;
  1040. // Look up the base framework name of the ContextFileEnt.
  1041. StringRef ContextName = ContextFileEnt->getName();
  1042. // If the context info wasn't a framework, couldn't be a subframework.
  1043. const unsigned DotFrameworkLen = 10;
  1044. auto FrameworkPos = ContextName.find(".framework");
  1045. if (FrameworkPos == StringRef::npos ||
  1046. (ContextName[FrameworkPos + DotFrameworkLen] != '/' &&
  1047. ContextName[FrameworkPos + DotFrameworkLen] != '\\'))
  1048. return std::nullopt;
  1049. SmallString<1024> FrameworkName(ContextName.data(), ContextName.data() +
  1050. FrameworkPos +
  1051. DotFrameworkLen + 1);
  1052. // Append Frameworks/HIToolbox.framework/
  1053. FrameworkName += "Frameworks/";
  1054. FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
  1055. FrameworkName += ".framework/";
  1056. auto &CacheLookup =
  1057. *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
  1058. FrameworkCacheEntry())).first;
  1059. // Some other location?
  1060. if (CacheLookup.second.Directory &&
  1061. CacheLookup.first().size() == FrameworkName.size() &&
  1062. memcmp(CacheLookup.first().data(), &FrameworkName[0],
  1063. CacheLookup.first().size()) != 0)
  1064. return std::nullopt;
  1065. // Cache subframework.
  1066. if (!CacheLookup.second.Directory) {
  1067. ++NumSubFrameworkLookups;
  1068. // If the framework dir doesn't exist, we fail.
  1069. auto Dir = FileMgr.getOptionalDirectoryRef(FrameworkName);
  1070. if (!Dir)
  1071. return std::nullopt;
  1072. // Otherwise, if it does, remember that this is the right direntry for this
  1073. // framework.
  1074. CacheLookup.second.Directory = Dir;
  1075. }
  1076. if (RelativePath) {
  1077. RelativePath->clear();
  1078. RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
  1079. }
  1080. // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
  1081. SmallString<1024> HeadersFilename(FrameworkName);
  1082. HeadersFilename += "Headers/";
  1083. if (SearchPath) {
  1084. SearchPath->clear();
  1085. // Without trailing '/'.
  1086. SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
  1087. }
  1088. HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
  1089. auto File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
  1090. if (!File) {
  1091. // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
  1092. HeadersFilename = FrameworkName;
  1093. HeadersFilename += "PrivateHeaders/";
  1094. if (SearchPath) {
  1095. SearchPath->clear();
  1096. // Without trailing '/'.
  1097. SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
  1098. }
  1099. HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
  1100. File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
  1101. if (!File)
  1102. return std::nullopt;
  1103. }
  1104. // This file is a system header or C++ unfriendly if the old file is.
  1105. //
  1106. // Note that the temporary 'DirInfo' is required here, as either call to
  1107. // getFileInfo could resize the vector and we don't want to rely on order
  1108. // of evaluation.
  1109. unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
  1110. getFileInfo(&File->getFileEntry()).DirInfo = DirInfo;
  1111. FrameworkName.pop_back(); // remove the trailing '/'
  1112. if (!findUsableModuleForFrameworkHeader(&File->getFileEntry(), FrameworkName,
  1113. RequestingModule, SuggestedModule,
  1114. /*IsSystem*/ false))
  1115. return std::nullopt;
  1116. return *File;
  1117. }
  1118. //===----------------------------------------------------------------------===//
  1119. // File Info Management.
  1120. //===----------------------------------------------------------------------===//
  1121. /// Merge the header file info provided by \p OtherHFI into the current
  1122. /// header file info (\p HFI)
  1123. static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
  1124. const HeaderFileInfo &OtherHFI) {
  1125. assert(OtherHFI.External && "expected to merge external HFI");
  1126. HFI.isImport |= OtherHFI.isImport;
  1127. HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
  1128. HFI.isModuleHeader |= OtherHFI.isModuleHeader;
  1129. if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
  1130. HFI.ControllingMacro = OtherHFI.ControllingMacro;
  1131. HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
  1132. }
  1133. HFI.DirInfo = OtherHFI.DirInfo;
  1134. HFI.External = (!HFI.IsValid || HFI.External);
  1135. HFI.IsValid = true;
  1136. HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
  1137. if (HFI.Framework.empty())
  1138. HFI.Framework = OtherHFI.Framework;
  1139. }
  1140. /// getFileInfo - Return the HeaderFileInfo structure for the specified
  1141. /// FileEntry.
  1142. HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
  1143. if (FE->getUID() >= FileInfo.size())
  1144. FileInfo.resize(FE->getUID() + 1);
  1145. HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
  1146. // FIXME: Use a generation count to check whether this is really up to date.
  1147. if (ExternalSource && !HFI->Resolved) {
  1148. auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
  1149. if (ExternalHFI.IsValid) {
  1150. HFI->Resolved = true;
  1151. if (ExternalHFI.External)
  1152. mergeHeaderFileInfo(*HFI, ExternalHFI);
  1153. }
  1154. }
  1155. HFI->IsValid = true;
  1156. // We have local information about this header file, so it's no longer
  1157. // strictly external.
  1158. HFI->External = false;
  1159. return *HFI;
  1160. }
  1161. const HeaderFileInfo *
  1162. HeaderSearch::getExistingFileInfo(const FileEntry *FE,
  1163. bool WantExternal) const {
  1164. // If we have an external source, ensure we have the latest information.
  1165. // FIXME: Use a generation count to check whether this is really up to date.
  1166. HeaderFileInfo *HFI;
  1167. if (ExternalSource) {
  1168. if (FE->getUID() >= FileInfo.size()) {
  1169. if (!WantExternal)
  1170. return nullptr;
  1171. FileInfo.resize(FE->getUID() + 1);
  1172. }
  1173. HFI = &FileInfo[FE->getUID()];
  1174. if (!WantExternal && (!HFI->IsValid || HFI->External))
  1175. return nullptr;
  1176. if (!HFI->Resolved) {
  1177. auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
  1178. if (ExternalHFI.IsValid) {
  1179. HFI->Resolved = true;
  1180. if (ExternalHFI.External)
  1181. mergeHeaderFileInfo(*HFI, ExternalHFI);
  1182. }
  1183. }
  1184. } else if (FE->getUID() >= FileInfo.size()) {
  1185. return nullptr;
  1186. } else {
  1187. HFI = &FileInfo[FE->getUID()];
  1188. }
  1189. if (!HFI->IsValid || (HFI->External && !WantExternal))
  1190. return nullptr;
  1191. return HFI;
  1192. }
  1193. bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
  1194. // Check if we've entered this file and found an include guard or #pragma
  1195. // once. Note that we dor't check for #import, because that's not a property
  1196. // of the file itself.
  1197. if (auto *HFI = getExistingFileInfo(File))
  1198. return HFI->isPragmaOnce || HFI->ControllingMacro ||
  1199. HFI->ControllingMacroID;
  1200. return false;
  1201. }
  1202. void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
  1203. ModuleMap::ModuleHeaderRole Role,
  1204. bool isCompilingModuleHeader) {
  1205. bool isModularHeader = ModuleMap::isModular(Role);
  1206. // Don't mark the file info as non-external if there's nothing to change.
  1207. if (!isCompilingModuleHeader) {
  1208. if (!isModularHeader)
  1209. return;
  1210. auto *HFI = getExistingFileInfo(FE);
  1211. if (HFI && HFI->isModuleHeader)
  1212. return;
  1213. }
  1214. auto &HFI = getFileInfo(FE);
  1215. HFI.isModuleHeader |= isModularHeader;
  1216. HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
  1217. }
  1218. bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
  1219. const FileEntry *File, bool isImport,
  1220. bool ModulesEnabled, Module *M,
  1221. bool &IsFirstIncludeOfFile) {
  1222. ++NumIncluded; // Count # of attempted #includes.
  1223. IsFirstIncludeOfFile = false;
  1224. // Get information about this file.
  1225. HeaderFileInfo &FileInfo = getFileInfo(File);
  1226. // FIXME: this is a workaround for the lack of proper modules-aware support
  1227. // for #import / #pragma once
  1228. auto TryEnterImported = [&]() -> bool {
  1229. if (!ModulesEnabled)
  1230. return false;
  1231. // Ensure FileInfo bits are up to date.
  1232. ModMap.resolveHeaderDirectives(File);
  1233. // Modules with builtins are special; multiple modules use builtins as
  1234. // modular headers, example:
  1235. //
  1236. // module stddef { header "stddef.h" export * }
  1237. //
  1238. // After module map parsing, this expands to:
  1239. //
  1240. // module stddef {
  1241. // header "/path_to_builtin_dirs/stddef.h"
  1242. // textual "stddef.h"
  1243. // }
  1244. //
  1245. // It's common that libc++ and system modules will both define such
  1246. // submodules. Make sure cached results for a builtin header won't
  1247. // prevent other builtin modules from potentially entering the builtin
  1248. // header. Note that builtins are header guarded and the decision to
  1249. // actually enter them is postponed to the controlling macros logic below.
  1250. bool TryEnterHdr = false;
  1251. if (FileInfo.isCompilingModuleHeader && FileInfo.isModuleHeader)
  1252. TryEnterHdr = ModMap.isBuiltinHeader(File);
  1253. // Textual headers can be #imported from different modules. Since ObjC
  1254. // headers find in the wild might rely only on #import and do not contain
  1255. // controlling macros, be conservative and only try to enter textual headers
  1256. // if such macro is present.
  1257. if (!FileInfo.isModuleHeader &&
  1258. FileInfo.getControllingMacro(ExternalLookup))
  1259. TryEnterHdr = true;
  1260. return TryEnterHdr;
  1261. };
  1262. // If this is a #import directive, check that we have not already imported
  1263. // this header.
  1264. if (isImport) {
  1265. // If this has already been imported, don't import it again.
  1266. FileInfo.isImport = true;
  1267. // Has this already been #import'ed or #include'd?
  1268. if (PP.alreadyIncluded(File) && !TryEnterImported())
  1269. return false;
  1270. } else {
  1271. // Otherwise, if this is a #include of a file that was previously #import'd
  1272. // or if this is the second #include of a #pragma once file, ignore it.
  1273. if ((FileInfo.isPragmaOnce || FileInfo.isImport) && !TryEnterImported())
  1274. return false;
  1275. }
  1276. // Next, check to see if the file is wrapped with #ifndef guards. If so, and
  1277. // if the macro that guards it is defined, we know the #include has no effect.
  1278. if (const IdentifierInfo *ControllingMacro
  1279. = FileInfo.getControllingMacro(ExternalLookup)) {
  1280. // If the header corresponds to a module, check whether the macro is already
  1281. // defined in that module rather than checking in the current set of visible
  1282. // modules.
  1283. if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M)
  1284. : PP.isMacroDefined(ControllingMacro)) {
  1285. ++NumMultiIncludeFileOptzn;
  1286. return false;
  1287. }
  1288. }
  1289. IsFirstIncludeOfFile = PP.markIncluded(File);
  1290. return true;
  1291. }
  1292. size_t HeaderSearch::getTotalMemory() const {
  1293. return SearchDirs.capacity()
  1294. + llvm::capacity_in_bytes(FileInfo)
  1295. + llvm::capacity_in_bytes(HeaderMaps)
  1296. + LookupFileCache.getAllocator().getTotalMemory()
  1297. + FrameworkMap.getAllocator().getTotalMemory();
  1298. }
  1299. unsigned HeaderSearch::searchDirIdx(const DirectoryLookup &DL) const {
  1300. return &DL - &*SearchDirs.begin();
  1301. }
  1302. StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
  1303. return FrameworkNames.insert(Framework).first->first();
  1304. }
  1305. StringRef HeaderSearch::getIncludeNameForHeader(const FileEntry *File) const {
  1306. auto It = IncludeNames.find(File);
  1307. if (It == IncludeNames.end())
  1308. return {};
  1309. return It->second;
  1310. }
  1311. bool HeaderSearch::hasModuleMap(StringRef FileName,
  1312. const DirectoryEntry *Root,
  1313. bool IsSystem) {
  1314. if (!HSOpts->ImplicitModuleMaps)
  1315. return false;
  1316. SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
  1317. StringRef DirName = FileName;
  1318. do {
  1319. // Get the parent directory name.
  1320. DirName = llvm::sys::path::parent_path(DirName);
  1321. if (DirName.empty())
  1322. return false;
  1323. // Determine whether this directory exists.
  1324. auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
  1325. if (!Dir)
  1326. return false;
  1327. // Try to load the module map file in this directory.
  1328. switch (loadModuleMapFile(*Dir, IsSystem,
  1329. llvm::sys::path::extension(Dir->getName()) ==
  1330. ".framework")) {
  1331. case LMM_NewlyLoaded:
  1332. case LMM_AlreadyLoaded:
  1333. // Success. All of the directories we stepped through inherit this module
  1334. // map file.
  1335. for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
  1336. DirectoryHasModuleMap[FixUpDirectories[I]] = true;
  1337. return true;
  1338. case LMM_NoDirectory:
  1339. case LMM_InvalidModuleMap:
  1340. break;
  1341. }
  1342. // If we hit the top of our search, we're done.
  1343. if (*Dir == Root)
  1344. return false;
  1345. // Keep track of all of the directories we checked, so we can mark them as
  1346. // having module maps if we eventually do find a module map.
  1347. FixUpDirectories.push_back(*Dir);
  1348. } while (true);
  1349. }
  1350. ModuleMap::KnownHeader
  1351. HeaderSearch::findModuleForHeader(const FileEntry *File, bool AllowTextual,
  1352. bool AllowExcluded) const {
  1353. if (ExternalSource) {
  1354. // Make sure the external source has handled header info about this file,
  1355. // which includes whether the file is part of a module.
  1356. (void)getExistingFileInfo(File);
  1357. }
  1358. return ModMap.findModuleForHeader(File, AllowTextual, AllowExcluded);
  1359. }
  1360. ArrayRef<ModuleMap::KnownHeader>
  1361. HeaderSearch::findAllModulesForHeader(const FileEntry *File) const {
  1362. if (ExternalSource) {
  1363. // Make sure the external source has handled header info about this file,
  1364. // which includes whether the file is part of a module.
  1365. (void)getExistingFileInfo(File);
  1366. }
  1367. return ModMap.findAllModulesForHeader(File);
  1368. }
  1369. static bool suggestModule(HeaderSearch &HS, const FileEntry *File,
  1370. Module *RequestingModule,
  1371. ModuleMap::KnownHeader *SuggestedModule) {
  1372. ModuleMap::KnownHeader Module =
  1373. HS.findModuleForHeader(File, /*AllowTextual*/true);
  1374. // If this module specifies [no_undeclared_includes], we cannot find any
  1375. // file that's in a non-dependency module.
  1376. if (RequestingModule && Module && RequestingModule->NoUndeclaredIncludes) {
  1377. HS.getModuleMap().resolveUses(RequestingModule, /*Complain*/ false);
  1378. if (!RequestingModule->directlyUses(Module.getModule())) {
  1379. // Builtin headers are a special case. Multiple modules can use the same
  1380. // builtin as a modular header (see also comment in
  1381. // ShouldEnterIncludeFile()), so the builtin header may have been
  1382. // "claimed" by an unrelated module. This shouldn't prevent us from
  1383. // including the builtin header textually in this module.
  1384. if (HS.getModuleMap().isBuiltinHeader(File)) {
  1385. if (SuggestedModule)
  1386. *SuggestedModule = ModuleMap::KnownHeader();
  1387. return true;
  1388. }
  1389. // TODO: Add this module (or just its module map file) into something like
  1390. // `RequestingModule->AffectingClangModules`.
  1391. return false;
  1392. }
  1393. }
  1394. if (SuggestedModule)
  1395. *SuggestedModule = (Module.getRole() & ModuleMap::TextualHeader)
  1396. ? ModuleMap::KnownHeader()
  1397. : Module;
  1398. return true;
  1399. }
  1400. bool HeaderSearch::findUsableModuleForHeader(
  1401. const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
  1402. ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
  1403. if (File && needModuleLookup(RequestingModule, SuggestedModule)) {
  1404. // If there is a module that corresponds to this header, suggest it.
  1405. hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
  1406. return suggestModule(*this, File, RequestingModule, SuggestedModule);
  1407. }
  1408. return true;
  1409. }
  1410. bool HeaderSearch::findUsableModuleForFrameworkHeader(
  1411. const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
  1412. ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
  1413. // If we're supposed to suggest a module, look for one now.
  1414. if (needModuleLookup(RequestingModule, SuggestedModule)) {
  1415. // Find the top-level framework based on this framework.
  1416. SmallVector<std::string, 4> SubmodulePath;
  1417. OptionalDirectoryEntryRef TopFrameworkDir =
  1418. ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
  1419. assert(TopFrameworkDir && "Could not find the top-most framework dir");
  1420. // Determine the name of the top-level framework.
  1421. StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
  1422. // Load this framework module. If that succeeds, find the suggested module
  1423. // for this header, if any.
  1424. loadFrameworkModule(ModuleName, *TopFrameworkDir, IsSystemFramework);
  1425. // FIXME: This can find a module not part of ModuleName, which is
  1426. // important so that we're consistent about whether this header
  1427. // corresponds to a module. Possibly we should lock down framework modules
  1428. // so that this is not possible.
  1429. return suggestModule(*this, File, RequestingModule, SuggestedModule);
  1430. }
  1431. return true;
  1432. }
  1433. static const FileEntry *getPrivateModuleMap(const FileEntry *File,
  1434. FileManager &FileMgr) {
  1435. StringRef Filename = llvm::sys::path::filename(File->getName());
  1436. SmallString<128> PrivateFilename(File->getDir()->getName());
  1437. if (Filename == "module.map")
  1438. llvm::sys::path::append(PrivateFilename, "module_private.map");
  1439. else if (Filename == "module.modulemap")
  1440. llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
  1441. else
  1442. return nullptr;
  1443. if (auto File = FileMgr.getFile(PrivateFilename))
  1444. return *File;
  1445. return nullptr;
  1446. }
  1447. bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
  1448. FileID ID, unsigned *Offset,
  1449. StringRef OriginalModuleMapFile) {
  1450. // Find the directory for the module. For frameworks, that may require going
  1451. // up from the 'Modules' directory.
  1452. OptionalDirectoryEntryRef Dir;
  1453. if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) {
  1454. Dir = FileMgr.getOptionalDirectoryRef(".");
  1455. } else {
  1456. if (!OriginalModuleMapFile.empty()) {
  1457. // We're building a preprocessed module map. Find or invent the directory
  1458. // that it originally occupied.
  1459. Dir = FileMgr.getOptionalDirectoryRef(
  1460. llvm::sys::path::parent_path(OriginalModuleMapFile));
  1461. if (!Dir) {
  1462. auto FakeFile = FileMgr.getVirtualFileRef(OriginalModuleMapFile, 0, 0);
  1463. Dir = FakeFile.getDir();
  1464. }
  1465. } else {
  1466. // TODO: Replace with `Dir = File.getDir()` when `File` is switched to
  1467. // `FileEntryRef`.
  1468. Dir = FileMgr.getOptionalDirectoryRef(File->getDir()->getName());
  1469. }
  1470. assert(Dir && "parent must exist");
  1471. StringRef DirName(Dir->getName());
  1472. if (llvm::sys::path::filename(DirName) == "Modules") {
  1473. DirName = llvm::sys::path::parent_path(DirName);
  1474. if (DirName.endswith(".framework"))
  1475. if (auto MaybeDir = FileMgr.getOptionalDirectoryRef(DirName))
  1476. Dir = *MaybeDir;
  1477. // FIXME: This assert can fail if there's a race between the above check
  1478. // and the removal of the directory.
  1479. assert(Dir && "parent must exist");
  1480. }
  1481. }
  1482. assert(Dir && "module map home directory must exist");
  1483. switch (loadModuleMapFileImpl(File, IsSystem, *Dir, ID, Offset)) {
  1484. case LMM_AlreadyLoaded:
  1485. case LMM_NewlyLoaded:
  1486. return false;
  1487. case LMM_NoDirectory:
  1488. case LMM_InvalidModuleMap:
  1489. return true;
  1490. }
  1491. llvm_unreachable("Unknown load module map result");
  1492. }
  1493. HeaderSearch::LoadModuleMapResult
  1494. HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
  1495. DirectoryEntryRef Dir, FileID ID,
  1496. unsigned *Offset) {
  1497. assert(File && "expected FileEntry");
  1498. // Check whether we've already loaded this module map, and mark it as being
  1499. // loaded in case we recursively try to load it from itself.
  1500. auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
  1501. if (!AddResult.second)
  1502. return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
  1503. if (ModMap.parseModuleMapFile(File, IsSystem, Dir, ID, Offset)) {
  1504. LoadedModuleMaps[File] = false;
  1505. return LMM_InvalidModuleMap;
  1506. }
  1507. // Try to load a corresponding private module map.
  1508. if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
  1509. if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
  1510. LoadedModuleMaps[File] = false;
  1511. return LMM_InvalidModuleMap;
  1512. }
  1513. }
  1514. // This directory has a module map.
  1515. return LMM_NewlyLoaded;
  1516. }
  1517. const FileEntry *
  1518. HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
  1519. if (!HSOpts->ImplicitModuleMaps)
  1520. return nullptr;
  1521. // For frameworks, the preferred spelling is Modules/module.modulemap, but
  1522. // module.map at the framework root is also accepted.
  1523. SmallString<128> ModuleMapFileName(Dir->getName());
  1524. if (IsFramework)
  1525. llvm::sys::path::append(ModuleMapFileName, "Modules");
  1526. llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
  1527. if (auto F = FileMgr.getFile(ModuleMapFileName))
  1528. return *F;
  1529. // Continue to allow module.map
  1530. ModuleMapFileName = Dir->getName();
  1531. llvm::sys::path::append(ModuleMapFileName, "module.map");
  1532. if (auto F = FileMgr.getFile(ModuleMapFileName))
  1533. return *F;
  1534. // For frameworks, allow to have a private module map with a preferred
  1535. // spelling when a public module map is absent.
  1536. if (IsFramework) {
  1537. ModuleMapFileName = Dir->getName();
  1538. llvm::sys::path::append(ModuleMapFileName, "Modules",
  1539. "module.private.modulemap");
  1540. if (auto F = FileMgr.getFile(ModuleMapFileName))
  1541. return *F;
  1542. }
  1543. return nullptr;
  1544. }
  1545. Module *HeaderSearch::loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
  1546. bool IsSystem) {
  1547. if (Module *Module = ModMap.findModule(Name))
  1548. return Module;
  1549. // Try to load a module map file.
  1550. switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
  1551. case LMM_InvalidModuleMap:
  1552. // Try to infer a module map from the framework directory.
  1553. if (HSOpts->ImplicitModuleMaps)
  1554. ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
  1555. break;
  1556. case LMM_AlreadyLoaded:
  1557. case LMM_NoDirectory:
  1558. return nullptr;
  1559. case LMM_NewlyLoaded:
  1560. break;
  1561. }
  1562. return ModMap.findModule(Name);
  1563. }
  1564. HeaderSearch::LoadModuleMapResult
  1565. HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
  1566. bool IsFramework) {
  1567. if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName))
  1568. return loadModuleMapFile(*Dir, IsSystem, IsFramework);
  1569. return LMM_NoDirectory;
  1570. }
  1571. HeaderSearch::LoadModuleMapResult
  1572. HeaderSearch::loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
  1573. bool IsFramework) {
  1574. auto KnownDir = DirectoryHasModuleMap.find(Dir);
  1575. if (KnownDir != DirectoryHasModuleMap.end())
  1576. return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
  1577. if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
  1578. LoadModuleMapResult Result =
  1579. loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
  1580. // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
  1581. // E.g. Foo.framework/Modules/module.modulemap
  1582. // ^Dir ^ModuleMapFile
  1583. if (Result == LMM_NewlyLoaded)
  1584. DirectoryHasModuleMap[Dir] = true;
  1585. else if (Result == LMM_InvalidModuleMap)
  1586. DirectoryHasModuleMap[Dir] = false;
  1587. return Result;
  1588. }
  1589. return LMM_InvalidModuleMap;
  1590. }
  1591. void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
  1592. Modules.clear();
  1593. if (HSOpts->ImplicitModuleMaps) {
  1594. // Load module maps for each of the header search directories.
  1595. for (DirectoryLookup &DL : search_dir_range()) {
  1596. bool IsSystem = DL.isSystemHeaderDirectory();
  1597. if (DL.isFramework()) {
  1598. std::error_code EC;
  1599. SmallString<128> DirNative;
  1600. llvm::sys::path::native(DL.getFrameworkDir()->getName(), DirNative);
  1601. // Search each of the ".framework" directories to load them as modules.
  1602. llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
  1603. for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
  1604. DirEnd;
  1605. Dir != DirEnd && !EC; Dir.increment(EC)) {
  1606. if (llvm::sys::path::extension(Dir->path()) != ".framework")
  1607. continue;
  1608. auto FrameworkDir = FileMgr.getOptionalDirectoryRef(Dir->path());
  1609. if (!FrameworkDir)
  1610. continue;
  1611. // Load this framework module.
  1612. loadFrameworkModule(llvm::sys::path::stem(Dir->path()), *FrameworkDir,
  1613. IsSystem);
  1614. }
  1615. continue;
  1616. }
  1617. // FIXME: Deal with header maps.
  1618. if (DL.isHeaderMap())
  1619. continue;
  1620. // Try to load a module map file for the search directory.
  1621. loadModuleMapFile(*DL.getDirRef(), IsSystem, /*IsFramework*/ false);
  1622. // Try to load module map files for immediate subdirectories of this
  1623. // search directory.
  1624. loadSubdirectoryModuleMaps(DL);
  1625. }
  1626. }
  1627. // Populate the list of modules.
  1628. llvm::transform(ModMap.modules(), std::back_inserter(Modules),
  1629. [](const auto &NameAndMod) { return NameAndMod.second; });
  1630. }
  1631. void HeaderSearch::loadTopLevelSystemModules() {
  1632. if (!HSOpts->ImplicitModuleMaps)
  1633. return;
  1634. // Load module maps for each of the header search directories.
  1635. for (const DirectoryLookup &DL : search_dir_range()) {
  1636. // We only care about normal header directories.
  1637. if (!DL.isNormalDir())
  1638. continue;
  1639. // Try to load a module map file for the search directory.
  1640. loadModuleMapFile(*DL.getDirRef(), DL.isSystemHeaderDirectory(),
  1641. DL.isFramework());
  1642. }
  1643. }
  1644. void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
  1645. assert(HSOpts->ImplicitModuleMaps &&
  1646. "Should not be loading subdirectory module maps");
  1647. if (SearchDir.haveSearchedAllModuleMaps())
  1648. return;
  1649. std::error_code EC;
  1650. SmallString<128> Dir = SearchDir.getDir()->getName();
  1651. FileMgr.makeAbsolutePath(Dir);
  1652. SmallString<128> DirNative;
  1653. llvm::sys::path::native(Dir, DirNative);
  1654. llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
  1655. for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
  1656. Dir != DirEnd && !EC; Dir.increment(EC)) {
  1657. bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
  1658. if (IsFramework == SearchDir.isFramework())
  1659. loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
  1660. SearchDir.isFramework());
  1661. }
  1662. SearchDir.setSearchedAllModuleMaps(true);
  1663. }
  1664. std::string HeaderSearch::suggestPathToFileForDiagnostics(
  1665. const FileEntry *File, llvm::StringRef MainFile, bool *IsSystem) {
  1666. // FIXME: We assume that the path name currently cached in the FileEntry is
  1667. // the most appropriate one for this analysis (and that it's spelled the
  1668. // same way as the corresponding header search path).
  1669. return suggestPathToFileForDiagnostics(File->getName(), /*WorkingDir=*/"",
  1670. MainFile, IsSystem);
  1671. }
  1672. std::string HeaderSearch::suggestPathToFileForDiagnostics(
  1673. llvm::StringRef File, llvm::StringRef WorkingDir, llvm::StringRef MainFile,
  1674. bool *IsSystem) {
  1675. using namespace llvm::sys;
  1676. llvm::SmallString<32> FilePath = File;
  1677. // remove_dots switches to backslashes on windows as a side-effect!
  1678. // We always want to suggest forward slashes for includes.
  1679. // (not remove_dots(..., posix) as that misparses windows paths).
  1680. path::remove_dots(FilePath, /*remove_dot_dot=*/true);
  1681. path::native(FilePath, path::Style::posix);
  1682. File = FilePath;
  1683. unsigned BestPrefixLength = 0;
  1684. // Checks whether `Dir` is a strict path prefix of `File`. If so and that's
  1685. // the longest prefix we've seen so for it, returns true and updates the
  1686. // `BestPrefixLength` accordingly.
  1687. auto CheckDir = [&](llvm::SmallString<32> Dir) -> bool {
  1688. if (!WorkingDir.empty() && !path::is_absolute(Dir))
  1689. fs::make_absolute(WorkingDir, Dir);
  1690. path::remove_dots(Dir, /*remove_dot_dot=*/true);
  1691. for (auto NI = path::begin(File), NE = path::end(File),
  1692. DI = path::begin(Dir), DE = path::end(Dir);
  1693. NI != NE; ++NI, ++DI) {
  1694. if (DI == DE) {
  1695. // Dir is a prefix of File, up to choice of path separators.
  1696. unsigned PrefixLength = NI - path::begin(File);
  1697. if (PrefixLength > BestPrefixLength) {
  1698. BestPrefixLength = PrefixLength;
  1699. return true;
  1700. }
  1701. break;
  1702. }
  1703. // Consider all path separators equal.
  1704. if (NI->size() == 1 && DI->size() == 1 &&
  1705. path::is_separator(NI->front()) && path::is_separator(DI->front()))
  1706. continue;
  1707. // Special case Apple .sdk folders since the search path is typically a
  1708. // symlink like `iPhoneSimulator14.5.sdk` while the file is instead
  1709. // located in `iPhoneSimulator.sdk` (the real folder).
  1710. if (NI->endswith(".sdk") && DI->endswith(".sdk")) {
  1711. StringRef NBasename = path::stem(*NI);
  1712. StringRef DBasename = path::stem(*DI);
  1713. if (DBasename.startswith(NBasename))
  1714. continue;
  1715. }
  1716. if (*NI != *DI)
  1717. break;
  1718. }
  1719. return false;
  1720. };
  1721. bool BestPrefixIsFramework = false;
  1722. for (const DirectoryLookup &DL : search_dir_range()) {
  1723. if (DL.isNormalDir()) {
  1724. StringRef Dir = DL.getDir()->getName();
  1725. if (CheckDir(Dir)) {
  1726. if (IsSystem)
  1727. *IsSystem = BestPrefixLength && isSystem(DL.getDirCharacteristic());
  1728. BestPrefixIsFramework = false;
  1729. }
  1730. } else if (DL.isFramework()) {
  1731. StringRef Dir = DL.getFrameworkDir()->getName();
  1732. if (CheckDir(Dir)) {
  1733. if (IsSystem)
  1734. *IsSystem = BestPrefixLength && isSystem(DL.getDirCharacteristic());
  1735. BestPrefixIsFramework = true;
  1736. }
  1737. }
  1738. }
  1739. // Try to shorten include path using TUs directory, if we couldn't find any
  1740. // suitable prefix in include search paths.
  1741. if (!BestPrefixLength && CheckDir(path::parent_path(MainFile))) {
  1742. if (IsSystem)
  1743. *IsSystem = false;
  1744. BestPrefixIsFramework = false;
  1745. }
  1746. // Try resolving resulting filename via reverse search in header maps,
  1747. // key from header name is user preferred name for the include file.
  1748. StringRef Filename = File.drop_front(BestPrefixLength);
  1749. for (const DirectoryLookup &DL : search_dir_range()) {
  1750. if (!DL.isHeaderMap())
  1751. continue;
  1752. StringRef SpelledFilename =
  1753. DL.getHeaderMap()->reverseLookupFilename(Filename);
  1754. if (!SpelledFilename.empty()) {
  1755. Filename = SpelledFilename;
  1756. BestPrefixIsFramework = false;
  1757. break;
  1758. }
  1759. }
  1760. // If the best prefix is a framework path, we need to compute the proper
  1761. // include spelling for the framework header.
  1762. bool IsPrivateHeader;
  1763. SmallString<128> FrameworkName, IncludeSpelling;
  1764. if (BestPrefixIsFramework &&
  1765. isFrameworkStylePath(Filename, IsPrivateHeader, FrameworkName,
  1766. IncludeSpelling)) {
  1767. Filename = IncludeSpelling;
  1768. }
  1769. return path::convert_to_slash(Filename);
  1770. }