HeaderSearch.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. //===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the DirectoryLookup and HeaderSearch interfaces.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Lex/HeaderSearch.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/IdentifierTable.h"
  16. #include "clang/Frontend/PCHContainerOperations.h"
  17. #include "clang/Lex/ExternalPreprocessorSource.h"
  18. #include "clang/Lex/HeaderMap.h"
  19. #include "clang/Lex/HeaderSearchOptions.h"
  20. #include "clang/Lex/LexDiagnostic.h"
  21. #include "clang/Lex/Lexer.h"
  22. #include "clang/Lex/Preprocessor.h"
  23. #include "llvm/ADT/APInt.h"
  24. #include "llvm/ADT/Hashing.h"
  25. #include "llvm/ADT/SmallString.h"
  26. #include "llvm/Support/Capacity.h"
  27. #include "llvm/Support/FileSystem.h"
  28. #include "llvm/Support/Path.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. #include <cstdio>
  31. #if defined(LLVM_ON_UNIX)
  32. #include <limits.h>
  33. #endif
  34. using namespace clang;
  35. const IdentifierInfo *
  36. HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
  37. if (ControllingMacro) {
  38. if (ControllingMacro->isOutOfDate())
  39. External->updateOutOfDateIdentifier(
  40. *const_cast<IdentifierInfo *>(ControllingMacro));
  41. return ControllingMacro;
  42. }
  43. if (!ControllingMacroID || !External)
  44. return nullptr;
  45. ControllingMacro = External->GetIdentifier(ControllingMacroID);
  46. return ControllingMacro;
  47. }
  48. ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
  49. HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
  50. SourceManager &SourceMgr, DiagnosticsEngine &Diags,
  51. const LangOptions &LangOpts,
  52. const TargetInfo *Target)
  53. : HSOpts(HSOpts), Diags(Diags), FileMgr(SourceMgr.getFileManager()),
  54. FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this),
  55. LangOpts(LangOpts) {
  56. AngledDirIdx = 0;
  57. SystemDirIdx = 0;
  58. NoCurDirSearch = false;
  59. ExternalLookup = nullptr;
  60. ExternalSource = nullptr;
  61. NumIncluded = 0;
  62. NumMultiIncludeFileOptzn = 0;
  63. NumFrameworkLookups = NumSubFrameworkLookups = 0;
  64. }
  65. HeaderSearch::~HeaderSearch() {
  66. // Delete headermaps.
  67. for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
  68. delete HeaderMaps[i].second;
  69. }
  70. void HeaderSearch::PrintStats() {
  71. fprintf(stderr, "\n*** HeaderSearch Stats:\n");
  72. fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
  73. unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
  74. for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
  75. NumOnceOnlyFiles += FileInfo[i].isImport;
  76. if (MaxNumIncludes < FileInfo[i].NumIncludes)
  77. MaxNumIncludes = FileInfo[i].NumIncludes;
  78. NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
  79. }
  80. fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
  81. fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
  82. fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
  83. fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
  84. fprintf(stderr, " %d #includes skipped due to"
  85. " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
  86. fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
  87. fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
  88. }
  89. /// CreateHeaderMap - This method returns a HeaderMap for the specified
  90. /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
  91. const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
  92. // We expect the number of headermaps to be small, and almost always empty.
  93. // If it ever grows, use of a linear search should be re-evaluated.
  94. if (!HeaderMaps.empty()) {
  95. for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
  96. // Pointer equality comparison of FileEntries works because they are
  97. // already uniqued by inode.
  98. if (HeaderMaps[i].first == FE)
  99. return HeaderMaps[i].second;
  100. }
  101. if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
  102. HeaderMaps.push_back(std::make_pair(FE, HM));
  103. return HM;
  104. }
  105. return nullptr;
  106. }
  107. std::string HeaderSearch::getModuleFileName(Module *Module) {
  108. const FileEntry *ModuleMap =
  109. getModuleMap().getModuleMapFileForUniquing(Module);
  110. return getModuleFileName(Module->Name, ModuleMap->getName());
  111. }
  112. std::string HeaderSearch::getModuleFileName(StringRef ModuleName,
  113. StringRef ModuleMapPath) {
  114. // If we don't have a module cache path, we can't do anything.
  115. if (ModuleCachePath.empty())
  116. return std::string();
  117. SmallString<256> Result(ModuleCachePath);
  118. llvm::sys::fs::make_absolute(Result);
  119. if (HSOpts->DisableModuleHash) {
  120. llvm::sys::path::append(Result, ModuleName + ".pcm");
  121. } else {
  122. // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
  123. // ideally be globally unique to this particular module. Name collisions
  124. // in the hash are safe (because any translation unit can only import one
  125. // module with each name), but result in a loss of caching.
  126. //
  127. // To avoid false-negatives, we form as canonical a path as we can, and map
  128. // to lower-case in case we're on a case-insensitive file system.
  129. auto *Dir =
  130. FileMgr.getDirectory(llvm::sys::path::parent_path(ModuleMapPath));
  131. if (!Dir)
  132. return std::string();
  133. auto DirName = FileMgr.getCanonicalName(Dir);
  134. auto FileName = llvm::sys::path::filename(ModuleMapPath);
  135. llvm::hash_code Hash =
  136. llvm::hash_combine(DirName.lower(), FileName.lower(),
  137. HSOpts->ModuleFormat);
  138. SmallString<128> HashStr;
  139. llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
  140. llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
  141. }
  142. return Result.str().str();
  143. }
  144. Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
  145. // Look in the module map to determine if there is a module by this name.
  146. Module *Module = ModMap.findModule(ModuleName);
  147. if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
  148. return Module;
  149. // Look through the various header search paths to load any available module
  150. // maps, searching for a module map that describes this module.
  151. for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
  152. if (SearchDirs[Idx].isFramework()) {
  153. // Search for or infer a module map for a framework.
  154. SmallString<128> FrameworkDirName;
  155. FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
  156. llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
  157. if (const DirectoryEntry *FrameworkDir
  158. = FileMgr.getDirectory(FrameworkDirName)) {
  159. bool IsSystem
  160. = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
  161. Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
  162. if (Module)
  163. break;
  164. }
  165. }
  166. // FIXME: Figure out how header maps and module maps will work together.
  167. // Only deal with normal search directories.
  168. if (!SearchDirs[Idx].isNormalDir())
  169. continue;
  170. bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
  171. // Search for a module map file in this directory.
  172. if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
  173. /*IsFramework*/false) == LMM_NewlyLoaded) {
  174. // We just loaded a module map file; check whether the module is
  175. // available now.
  176. Module = ModMap.findModule(ModuleName);
  177. if (Module)
  178. break;
  179. }
  180. // Search for a module map in a subdirectory with the same name as the
  181. // module.
  182. SmallString<128> NestedModuleMapDirName;
  183. NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
  184. llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
  185. if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
  186. /*IsFramework*/false) == LMM_NewlyLoaded){
  187. // If we just loaded a module map file, look for the module again.
  188. Module = ModMap.findModule(ModuleName);
  189. if (Module)
  190. break;
  191. }
  192. // If we've already performed the exhaustive search for module maps in this
  193. // search directory, don't do it again.
  194. if (SearchDirs[Idx].haveSearchedAllModuleMaps())
  195. continue;
  196. // Load all module maps in the immediate subdirectories of this search
  197. // directory.
  198. loadSubdirectoryModuleMaps(SearchDirs[Idx]);
  199. // Look again for the module.
  200. Module = ModMap.findModule(ModuleName);
  201. if (Module)
  202. break;
  203. }
  204. return Module;
  205. }
  206. //===----------------------------------------------------------------------===//
  207. // File lookup within a DirectoryLookup scope
  208. //===----------------------------------------------------------------------===//
  209. /// getName - Return the directory or filename corresponding to this lookup
  210. /// object.
  211. const char *DirectoryLookup::getName() const {
  212. if (isNormalDir())
  213. return getDir()->getName();
  214. if (isFramework())
  215. return getFrameworkDir()->getName();
  216. assert(isHeaderMap() && "Unknown DirectoryLookup");
  217. return getHeaderMap()->getFileName();
  218. }
  219. static const FileEntry *
  220. getFileAndSuggestModule(HeaderSearch &HS, StringRef FileName,
  221. const DirectoryEntry *Dir, bool IsSystemHeaderDir,
  222. ModuleMap::KnownHeader *SuggestedModule) {
  223. // If we have a module map that might map this header, load it and
  224. // check whether we'll have a suggestion for a module.
  225. HS.hasModuleMap(FileName, Dir, IsSystemHeaderDir);
  226. if (SuggestedModule) {
  227. const FileEntry *File = HS.getFileMgr().getFile(FileName,
  228. /*OpenFile=*/false);
  229. if (File) {
  230. // If there is a module that corresponds to this header, suggest it.
  231. *SuggestedModule = HS.findModuleForHeader(File);
  232. // FIXME: This appears to be a no-op. We loaded the module map for this
  233. // directory at the start of this function.
  234. if (!SuggestedModule->getModule() &&
  235. HS.hasModuleMap(FileName, Dir, IsSystemHeaderDir))
  236. *SuggestedModule = HS.findModuleForHeader(File);
  237. }
  238. return File;
  239. }
  240. return HS.getFileMgr().getFile(FileName, /*openFile=*/true);
  241. }
  242. /// LookupFile - Lookup the specified file in this search path, returning it
  243. /// if it exists or returning null if not.
  244. const FileEntry *DirectoryLookup::LookupFile(
  245. StringRef &Filename,
  246. HeaderSearch &HS,
  247. SmallVectorImpl<char> *SearchPath,
  248. SmallVectorImpl<char> *RelativePath,
  249. ModuleMap::KnownHeader *SuggestedModule,
  250. bool &InUserSpecifiedSystemFramework,
  251. bool &HasBeenMapped,
  252. SmallVectorImpl<char> &MappedName) const {
  253. InUserSpecifiedSystemFramework = false;
  254. HasBeenMapped = false;
  255. SmallString<1024> TmpDir;
  256. if (isNormalDir()) {
  257. // Concatenate the requested file onto the directory.
  258. TmpDir = getDir()->getName();
  259. llvm::sys::path::append(TmpDir, Filename);
  260. if (SearchPath) {
  261. StringRef SearchPathRef(getDir()->getName());
  262. SearchPath->clear();
  263. SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
  264. }
  265. if (RelativePath) {
  266. RelativePath->clear();
  267. RelativePath->append(Filename.begin(), Filename.end());
  268. }
  269. return getFileAndSuggestModule(HS, TmpDir, getDir(),
  270. isSystemHeaderDirectory(),
  271. SuggestedModule);
  272. }
  273. if (isFramework())
  274. return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
  275. SuggestedModule, InUserSpecifiedSystemFramework);
  276. assert(isHeaderMap() && "Unknown directory lookup");
  277. const HeaderMap *HM = getHeaderMap();
  278. SmallString<1024> Path;
  279. StringRef Dest = HM->lookupFilename(Filename, Path);
  280. if (Dest.empty())
  281. return nullptr;
  282. const FileEntry *Result;
  283. // Check if the headermap maps the filename to a framework include
  284. // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
  285. // framework include.
  286. if (llvm::sys::path::is_relative(Dest)) {
  287. MappedName.clear();
  288. MappedName.append(Dest.begin(), Dest.end());
  289. Filename = StringRef(MappedName.begin(), MappedName.size());
  290. HasBeenMapped = true;
  291. Result = HM->LookupFile(Filename, HS.getFileMgr());
  292. } else {
  293. Result = HS.getFileMgr().getFile(Dest);
  294. }
  295. if (Result) {
  296. if (SearchPath) {
  297. StringRef SearchPathRef(getName());
  298. SearchPath->clear();
  299. SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
  300. }
  301. if (RelativePath) {
  302. RelativePath->clear();
  303. RelativePath->append(Filename.begin(), Filename.end());
  304. }
  305. }
  306. return Result;
  307. }
  308. /// \brief Given a framework directory, find the top-most framework directory.
  309. ///
  310. /// \param FileMgr The file manager to use for directory lookups.
  311. /// \param DirName The name of the framework directory.
  312. /// \param SubmodulePath Will be populated with the submodule path from the
  313. /// returned top-level module to the originally named framework.
  314. static const DirectoryEntry *
  315. getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
  316. SmallVectorImpl<std::string> &SubmodulePath) {
  317. assert(llvm::sys::path::extension(DirName) == ".framework" &&
  318. "Not a framework directory");
  319. // Note: as an egregious but useful hack we use the real path here, because
  320. // frameworks moving between top-level frameworks to embedded frameworks tend
  321. // to be symlinked, and we base the logical structure of modules on the
  322. // physical layout. In particular, we need to deal with crazy includes like
  323. //
  324. // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
  325. //
  326. // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
  327. // which one should access with, e.g.,
  328. //
  329. // #include <Bar/Wibble.h>
  330. //
  331. // Similar issues occur when a top-level framework has moved into an
  332. // embedded framework.
  333. const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
  334. DirName = FileMgr.getCanonicalName(TopFrameworkDir);
  335. do {
  336. // Get the parent directory name.
  337. DirName = llvm::sys::path::parent_path(DirName);
  338. if (DirName.empty())
  339. break;
  340. // Determine whether this directory exists.
  341. const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
  342. if (!Dir)
  343. break;
  344. // If this is a framework directory, then we're a subframework of this
  345. // framework.
  346. if (llvm::sys::path::extension(DirName) == ".framework") {
  347. SubmodulePath.push_back(llvm::sys::path::stem(DirName));
  348. TopFrameworkDir = Dir;
  349. }
  350. } while (true);
  351. return TopFrameworkDir;
  352. }
  353. /// DoFrameworkLookup - Do a lookup of the specified file in the current
  354. /// DirectoryLookup, which is a framework directory.
  355. const FileEntry *DirectoryLookup::DoFrameworkLookup(
  356. StringRef Filename,
  357. HeaderSearch &HS,
  358. SmallVectorImpl<char> *SearchPath,
  359. SmallVectorImpl<char> *RelativePath,
  360. ModuleMap::KnownHeader *SuggestedModule,
  361. bool &InUserSpecifiedSystemFramework) const
  362. {
  363. FileManager &FileMgr = HS.getFileMgr();
  364. // Framework names must have a '/' in the filename.
  365. size_t SlashPos = Filename.find('/');
  366. if (SlashPos == StringRef::npos) return nullptr;
  367. // Find out if this is the home for the specified framework, by checking
  368. // HeaderSearch. Possible answers are yes/no and unknown.
  369. HeaderSearch::FrameworkCacheEntry &CacheEntry =
  370. HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
  371. // If it is known and in some other directory, fail.
  372. if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
  373. return nullptr;
  374. // Otherwise, construct the path to this framework dir.
  375. // FrameworkName = "/System/Library/Frameworks/"
  376. SmallString<1024> FrameworkName;
  377. FrameworkName += getFrameworkDir()->getName();
  378. if (FrameworkName.empty() || FrameworkName.back() != '/')
  379. FrameworkName.push_back('/');
  380. // FrameworkName = "/System/Library/Frameworks/Cocoa"
  381. StringRef ModuleName(Filename.begin(), SlashPos);
  382. FrameworkName += ModuleName;
  383. // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
  384. FrameworkName += ".framework/";
  385. // If the cache entry was unresolved, populate it now.
  386. if (!CacheEntry.Directory) {
  387. HS.IncrementFrameworkLookupCount();
  388. // If the framework dir doesn't exist, we fail.
  389. const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
  390. if (!Dir) return nullptr;
  391. // Otherwise, if it does, remember that this is the right direntry for this
  392. // framework.
  393. CacheEntry.Directory = getFrameworkDir();
  394. // If this is a user search directory, check if the framework has been
  395. // user-specified as a system framework.
  396. if (getDirCharacteristic() == SrcMgr::C_User) {
  397. SmallString<1024> SystemFrameworkMarker(FrameworkName);
  398. SystemFrameworkMarker += ".system_framework";
  399. if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
  400. CacheEntry.IsUserSpecifiedSystemFramework = true;
  401. }
  402. }
  403. }
  404. // Set the 'user-specified system framework' flag.
  405. InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
  406. if (RelativePath) {
  407. RelativePath->clear();
  408. RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
  409. }
  410. // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
  411. unsigned OrigSize = FrameworkName.size();
  412. FrameworkName += "Headers/";
  413. if (SearchPath) {
  414. SearchPath->clear();
  415. // Without trailing '/'.
  416. SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
  417. }
  418. FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
  419. const FileEntry *FE = FileMgr.getFile(FrameworkName,
  420. /*openFile=*/!SuggestedModule);
  421. if (!FE) {
  422. // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
  423. const char *Private = "Private";
  424. FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
  425. Private+strlen(Private));
  426. if (SearchPath)
  427. SearchPath->insert(SearchPath->begin()+OrigSize, Private,
  428. Private+strlen(Private));
  429. FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
  430. }
  431. // If we found the header and are allowed to suggest a module, do so now.
  432. if (FE && SuggestedModule) {
  433. // Find the framework in which this header occurs.
  434. StringRef FrameworkPath = FE->getDir()->getName();
  435. bool FoundFramework = false;
  436. do {
  437. // Determine whether this directory exists.
  438. const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
  439. if (!Dir)
  440. break;
  441. // If this is a framework directory, then we're a subframework of this
  442. // framework.
  443. if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
  444. FoundFramework = true;
  445. break;
  446. }
  447. // Get the parent directory name.
  448. FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
  449. if (FrameworkPath.empty())
  450. break;
  451. } while (true);
  452. if (FoundFramework) {
  453. // Find the top-level framework based on this framework.
  454. SmallVector<std::string, 4> SubmodulePath;
  455. const DirectoryEntry *TopFrameworkDir
  456. = ::getTopFrameworkDir(FileMgr, FrameworkPath, SubmodulePath);
  457. // Determine the name of the top-level framework.
  458. StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
  459. // Load this framework module. If that succeeds, find the suggested module
  460. // for this header, if any.
  461. bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
  462. HS.loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem);
  463. // FIXME: This can find a module not part of ModuleName, which is
  464. // important so that we're consistent about whether this header
  465. // corresponds to a module. Possibly we should lock down framework modules
  466. // so that this is not possible.
  467. *SuggestedModule = HS.findModuleForHeader(FE);
  468. } else {
  469. *SuggestedModule = HS.findModuleForHeader(FE);
  470. }
  471. }
  472. return FE;
  473. }
  474. void HeaderSearch::setTarget(const TargetInfo &Target) {
  475. ModMap.setTarget(Target);
  476. }
  477. //===----------------------------------------------------------------------===//
  478. // Header File Location.
  479. //===----------------------------------------------------------------------===//
  480. /// \brief Return true with a diagnostic if the file that MSVC would have found
  481. /// fails to match the one that Clang would have found with MSVC header search
  482. /// disabled.
  483. static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
  484. const FileEntry *MSFE, const FileEntry *FE,
  485. SourceLocation IncludeLoc) {
  486. if (MSFE && FE != MSFE) {
  487. Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
  488. return true;
  489. }
  490. return false;
  491. }
  492. static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
  493. assert(!Str.empty());
  494. char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
  495. std::copy(Str.begin(), Str.end(), CopyStr);
  496. CopyStr[Str.size()] = '\0';
  497. return CopyStr;
  498. }
  499. /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
  500. /// return null on failure. isAngled indicates whether the file reference is
  501. /// for system \#include's or not (i.e. using <> instead of ""). Includers, if
  502. /// non-empty, indicates where the \#including file(s) are, in case a relative
  503. /// search is needed. Microsoft mode will pass all \#including files.
  504. const FileEntry *HeaderSearch::LookupFile(
  505. StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
  506. const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
  507. ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
  508. SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
  509. ModuleMap::KnownHeader *SuggestedModule, bool SkipCache) {
  510. if (SuggestedModule)
  511. *SuggestedModule = ModuleMap::KnownHeader();
  512. // If 'Filename' is absolute, check to see if it exists and no searching.
  513. if (llvm::sys::path::is_absolute(Filename)) {
  514. CurDir = nullptr;
  515. // If this was an #include_next "/absolute/file", fail.
  516. if (FromDir) return nullptr;
  517. if (SearchPath)
  518. SearchPath->clear();
  519. if (RelativePath) {
  520. RelativePath->clear();
  521. RelativePath->append(Filename.begin(), Filename.end());
  522. }
  523. // Otherwise, just return the file.
  524. const FileEntry *File = FileMgr.getFile(Filename, /*openFile=*/true);
  525. if (File && SuggestedModule) {
  526. // If there is a module that corresponds to this header, suggest it.
  527. hasModuleMap(Filename, File->getDir(), /*SystemHeaderDir*/false);
  528. *SuggestedModule = findModuleForHeader(File);
  529. }
  530. return File;
  531. }
  532. // This is the header that MSVC's header search would have found.
  533. const FileEntry *MSFE = nullptr;
  534. ModuleMap::KnownHeader MSSuggestedModule;
  535. // Unless disabled, check to see if the file is in the #includer's
  536. // directory. This cannot be based on CurDir, because each includer could be
  537. // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
  538. // include of "baz.h" should resolve to "whatever/foo/baz.h".
  539. // This search is not done for <> headers.
  540. if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
  541. SmallString<1024> TmpDir;
  542. bool First = true;
  543. for (const auto &IncluderAndDir : Includers) {
  544. const FileEntry *Includer = IncluderAndDir.first;
  545. // Concatenate the requested file onto the directory.
  546. // FIXME: Portability. Filename concatenation should be in sys::Path.
  547. TmpDir = IncluderAndDir.second->getName();
  548. TmpDir.push_back('/');
  549. TmpDir.append(Filename.begin(), Filename.end());
  550. // FIXME: We don't cache the result of getFileInfo across the call to
  551. // getFileAndSuggestModule, because it's a reference to an element of
  552. // a container that could be reallocated across this call.
  553. //
  554. // FIXME: If we have no includer, that means we're processing a #include
  555. // from a module build. We should treat this as a system header if we're
  556. // building a [system] module.
  557. bool IncluderIsSystemHeader =
  558. Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User;
  559. if (const FileEntry *FE = getFileAndSuggestModule(
  560. *this, TmpDir, IncluderAndDir.second,
  561. IncluderIsSystemHeader, SuggestedModule)) {
  562. if (!Includer) {
  563. assert(First && "only first includer can have no file");
  564. return FE;
  565. }
  566. // Leave CurDir unset.
  567. // This file is a system header or C++ unfriendly if the old file is.
  568. //
  569. // Note that we only use one of FromHFI/ToHFI at once, due to potential
  570. // reallocation of the underlying vector potentially making the first
  571. // reference binding dangling.
  572. HeaderFileInfo &FromHFI = getFileInfo(Includer);
  573. unsigned DirInfo = FromHFI.DirInfo;
  574. bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
  575. StringRef Framework = FromHFI.Framework;
  576. HeaderFileInfo &ToHFI = getFileInfo(FE);
  577. ToHFI.DirInfo = DirInfo;
  578. ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
  579. ToHFI.Framework = Framework;
  580. if (SearchPath) {
  581. StringRef SearchPathRef(IncluderAndDir.second->getName());
  582. SearchPath->clear();
  583. SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
  584. }
  585. if (RelativePath) {
  586. RelativePath->clear();
  587. RelativePath->append(Filename.begin(), Filename.end());
  588. }
  589. if (First)
  590. return FE;
  591. // Otherwise, we found the path via MSVC header search rules. If
  592. // -Wmsvc-include is enabled, we have to keep searching to see if we
  593. // would've found this header in -I or -isystem directories.
  594. if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
  595. return FE;
  596. } else {
  597. MSFE = FE;
  598. if (SuggestedModule) {
  599. MSSuggestedModule = *SuggestedModule;
  600. *SuggestedModule = ModuleMap::KnownHeader();
  601. }
  602. break;
  603. }
  604. }
  605. First = false;
  606. }
  607. }
  608. CurDir = nullptr;
  609. // If this is a system #include, ignore the user #include locs.
  610. unsigned i = isAngled ? AngledDirIdx : 0;
  611. // If this is a #include_next request, start searching after the directory the
  612. // file was found in.
  613. if (FromDir)
  614. i = FromDir-&SearchDirs[0];
  615. // Cache all of the lookups performed by this method. Many headers are
  616. // multiply included, and the "pragma once" optimization prevents them from
  617. // being relex/pp'd, but they would still have to search through a
  618. // (potentially huge) series of SearchDirs to find it.
  619. LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
  620. // If the entry has been previously looked up, the first value will be
  621. // non-zero. If the value is equal to i (the start point of our search), then
  622. // this is a matching hit.
  623. if (!SkipCache && CacheLookup.StartIdx == i+1) {
  624. // Skip querying potentially lots of directories for this lookup.
  625. i = CacheLookup.HitIdx;
  626. if (CacheLookup.MappedName)
  627. Filename = CacheLookup.MappedName;
  628. } else {
  629. // Otherwise, this is the first query, or the previous query didn't match
  630. // our search start. We will fill in our found location below, so prime the
  631. // start point value.
  632. CacheLookup.reset(/*StartIdx=*/i+1);
  633. }
  634. SmallString<64> MappedName;
  635. // Check each directory in sequence to see if it contains this file.
  636. for (; i != SearchDirs.size(); ++i) {
  637. bool InUserSpecifiedSystemFramework = false;
  638. bool HasBeenMapped = false;
  639. const FileEntry *FE =
  640. SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
  641. SuggestedModule, InUserSpecifiedSystemFramework,
  642. HasBeenMapped, MappedName);
  643. if (HasBeenMapped) {
  644. CacheLookup.MappedName =
  645. copyString(Filename, LookupFileCache.getAllocator());
  646. }
  647. if (!FE) continue;
  648. CurDir = &SearchDirs[i];
  649. // This file is a system header or C++ unfriendly if the dir is.
  650. HeaderFileInfo &HFI = getFileInfo(FE);
  651. HFI.DirInfo = CurDir->getDirCharacteristic();
  652. // If the directory characteristic is User but this framework was
  653. // user-specified to be treated as a system framework, promote the
  654. // characteristic.
  655. if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
  656. HFI.DirInfo = SrcMgr::C_System;
  657. // If the filename matches a known system header prefix, override
  658. // whether the file is a system header.
  659. for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
  660. if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
  661. HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
  662. : SrcMgr::C_User;
  663. break;
  664. }
  665. }
  666. // If this file is found in a header map and uses the framework style of
  667. // includes, then this header is part of a framework we're building.
  668. if (CurDir->isIndexHeaderMap()) {
  669. size_t SlashPos = Filename.find('/');
  670. if (SlashPos != StringRef::npos) {
  671. HFI.IndexHeaderMapHeader = 1;
  672. HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
  673. SlashPos));
  674. }
  675. }
  676. if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
  677. if (SuggestedModule)
  678. *SuggestedModule = MSSuggestedModule;
  679. return MSFE;
  680. }
  681. // Remember this location for the next lookup we do.
  682. CacheLookup.HitIdx = i;
  683. return FE;
  684. }
  685. // If we are including a file with a quoted include "foo.h" from inside
  686. // a header in a framework that is currently being built, and we couldn't
  687. // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
  688. // "Foo" is the name of the framework in which the including header was found.
  689. if (!Includers.empty() && Includers.front().first && !isAngled &&
  690. Filename.find('/') == StringRef::npos) {
  691. HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
  692. if (IncludingHFI.IndexHeaderMapHeader) {
  693. SmallString<128> ScratchFilename;
  694. ScratchFilename += IncludingHFI.Framework;
  695. ScratchFilename += '/';
  696. ScratchFilename += Filename;
  697. const FileEntry *FE = LookupFile(
  698. ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, CurDir,
  699. Includers.front(), SearchPath, RelativePath, SuggestedModule);
  700. if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
  701. if (SuggestedModule)
  702. *SuggestedModule = MSSuggestedModule;
  703. return MSFE;
  704. }
  705. LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
  706. CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
  707. // FIXME: SuggestedModule.
  708. return FE;
  709. }
  710. }
  711. if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
  712. if (SuggestedModule)
  713. *SuggestedModule = MSSuggestedModule;
  714. return MSFE;
  715. }
  716. // Otherwise, didn't find it. Remember we didn't find this.
  717. CacheLookup.HitIdx = SearchDirs.size();
  718. return nullptr;
  719. }
  720. /// LookupSubframeworkHeader - Look up a subframework for the specified
  721. /// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
  722. /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
  723. /// is a subframework within Carbon.framework. If so, return the FileEntry
  724. /// for the designated file, otherwise return null.
  725. const FileEntry *HeaderSearch::
  726. LookupSubframeworkHeader(StringRef Filename,
  727. const FileEntry *ContextFileEnt,
  728. SmallVectorImpl<char> *SearchPath,
  729. SmallVectorImpl<char> *RelativePath,
  730. ModuleMap::KnownHeader *SuggestedModule) {
  731. assert(ContextFileEnt && "No context file?");
  732. // Framework names must have a '/' in the filename. Find it.
  733. // FIXME: Should we permit '\' on Windows?
  734. size_t SlashPos = Filename.find('/');
  735. if (SlashPos == StringRef::npos) return nullptr;
  736. // Look up the base framework name of the ContextFileEnt.
  737. const char *ContextName = ContextFileEnt->getName();
  738. // If the context info wasn't a framework, couldn't be a subframework.
  739. const unsigned DotFrameworkLen = 10;
  740. const char *FrameworkPos = strstr(ContextName, ".framework");
  741. if (FrameworkPos == nullptr ||
  742. (FrameworkPos[DotFrameworkLen] != '/' &&
  743. FrameworkPos[DotFrameworkLen] != '\\'))
  744. return nullptr;
  745. SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
  746. // Append Frameworks/HIToolbox.framework/
  747. FrameworkName += "Frameworks/";
  748. FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
  749. FrameworkName += ".framework/";
  750. auto &CacheLookup =
  751. *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
  752. FrameworkCacheEntry())).first;
  753. // Some other location?
  754. if (CacheLookup.second.Directory &&
  755. CacheLookup.first().size() == FrameworkName.size() &&
  756. memcmp(CacheLookup.first().data(), &FrameworkName[0],
  757. CacheLookup.first().size()) != 0)
  758. return nullptr;
  759. // Cache subframework.
  760. if (!CacheLookup.second.Directory) {
  761. ++NumSubFrameworkLookups;
  762. // If the framework dir doesn't exist, we fail.
  763. const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
  764. if (!Dir) return nullptr;
  765. // Otherwise, if it does, remember that this is the right direntry for this
  766. // framework.
  767. CacheLookup.second.Directory = Dir;
  768. }
  769. const FileEntry *FE = nullptr;
  770. if (RelativePath) {
  771. RelativePath->clear();
  772. RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
  773. }
  774. // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
  775. SmallString<1024> HeadersFilename(FrameworkName);
  776. HeadersFilename += "Headers/";
  777. if (SearchPath) {
  778. SearchPath->clear();
  779. // Without trailing '/'.
  780. SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
  781. }
  782. HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
  783. if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
  784. // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
  785. HeadersFilename = FrameworkName;
  786. HeadersFilename += "PrivateHeaders/";
  787. if (SearchPath) {
  788. SearchPath->clear();
  789. // Without trailing '/'.
  790. SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
  791. }
  792. HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
  793. if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true)))
  794. return nullptr;
  795. }
  796. // This file is a system header or C++ unfriendly if the old file is.
  797. //
  798. // Note that the temporary 'DirInfo' is required here, as either call to
  799. // getFileInfo could resize the vector and we don't want to rely on order
  800. // of evaluation.
  801. unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
  802. getFileInfo(FE).DirInfo = DirInfo;
  803. // If we're supposed to suggest a module, look for one now.
  804. if (SuggestedModule) {
  805. // Find the top-level framework based on this framework.
  806. FrameworkName.pop_back(); // remove the trailing '/'
  807. SmallVector<std::string, 4> SubmodulePath;
  808. const DirectoryEntry *TopFrameworkDir
  809. = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
  810. // Determine the name of the top-level framework.
  811. StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
  812. // Load this framework module. If that succeeds, find the suggested module
  813. // for this header, if any.
  814. bool IsSystem = false;
  815. if (loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
  816. *SuggestedModule = findModuleForHeader(FE);
  817. }
  818. }
  819. return FE;
  820. }
  821. //===----------------------------------------------------------------------===//
  822. // File Info Management.
  823. //===----------------------------------------------------------------------===//
  824. /// \brief Merge the header file info provided by \p OtherHFI into the current
  825. /// header file info (\p HFI)
  826. static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
  827. const HeaderFileInfo &OtherHFI) {
  828. HFI.isImport |= OtherHFI.isImport;
  829. HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
  830. HFI.isModuleHeader |= OtherHFI.isModuleHeader;
  831. HFI.NumIncludes += OtherHFI.NumIncludes;
  832. if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
  833. HFI.ControllingMacro = OtherHFI.ControllingMacro;
  834. HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
  835. }
  836. if (OtherHFI.External) {
  837. HFI.DirInfo = OtherHFI.DirInfo;
  838. HFI.External = OtherHFI.External;
  839. HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
  840. }
  841. if (HFI.Framework.empty())
  842. HFI.Framework = OtherHFI.Framework;
  843. HFI.Resolved = true;
  844. }
  845. /// getFileInfo - Return the HeaderFileInfo structure for the specified
  846. /// FileEntry.
  847. HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
  848. if (FE->getUID() >= FileInfo.size())
  849. FileInfo.resize(FE->getUID()+1);
  850. HeaderFileInfo &HFI = FileInfo[FE->getUID()];
  851. if (ExternalSource && !HFI.Resolved)
  852. mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
  853. HFI.IsValid = 1;
  854. return HFI;
  855. }
  856. bool HeaderSearch::tryGetFileInfo(const FileEntry *FE,
  857. HeaderFileInfo &Result) const {
  858. if (FE->getUID() >= FileInfo.size())
  859. return false;
  860. const HeaderFileInfo &HFI = FileInfo[FE->getUID()];
  861. if (HFI.IsValid) {
  862. Result = HFI;
  863. return true;
  864. }
  865. return false;
  866. }
  867. bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
  868. // Check if we've ever seen this file as a header.
  869. if (File->getUID() >= FileInfo.size())
  870. return false;
  871. // Resolve header file info from the external source, if needed.
  872. HeaderFileInfo &HFI = FileInfo[File->getUID()];
  873. if (ExternalSource && !HFI.Resolved)
  874. mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
  875. return HFI.isPragmaOnce || HFI.isImport ||
  876. HFI.ControllingMacro || HFI.ControllingMacroID;
  877. }
  878. void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
  879. ModuleMap::ModuleHeaderRole Role,
  880. bool isCompilingModuleHeader) {
  881. if (FE->getUID() >= FileInfo.size())
  882. FileInfo.resize(FE->getUID()+1);
  883. HeaderFileInfo &HFI = FileInfo[FE->getUID()];
  884. HFI.isModuleHeader = true;
  885. HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
  886. HFI.setHeaderRole(Role);
  887. }
  888. bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
  889. const FileEntry *File,
  890. bool isImport, Module *M) {
  891. ++NumIncluded; // Count # of attempted #includes.
  892. // Get information about this file.
  893. HeaderFileInfo &FileInfo = getFileInfo(File);
  894. // If this is a #import directive, check that we have not already imported
  895. // this header.
  896. if (isImport) {
  897. // If this has already been imported, don't import it again.
  898. FileInfo.isImport = true;
  899. // Has this already been #import'ed or #include'd?
  900. if (FileInfo.NumIncludes) return false;
  901. } else {
  902. // Otherwise, if this is a #include of a file that was previously #import'd
  903. // or if this is the second #include of a #pragma once file, ignore it.
  904. if (FileInfo.isImport)
  905. return false;
  906. }
  907. // Next, check to see if the file is wrapped with #ifndef guards. If so, and
  908. // if the macro that guards it is defined, we know the #include has no effect.
  909. if (const IdentifierInfo *ControllingMacro
  910. = FileInfo.getControllingMacro(ExternalLookup)) {
  911. // If the header corresponds to a module, check whether the macro is already
  912. // defined in that module rather than checking in the current set of visible
  913. // modules.
  914. if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M)
  915. : PP.isMacroDefined(ControllingMacro)) {
  916. ++NumMultiIncludeFileOptzn;
  917. return false;
  918. }
  919. }
  920. // Increment the number of times this file has been included.
  921. ++FileInfo.NumIncludes;
  922. return true;
  923. }
  924. size_t HeaderSearch::getTotalMemory() const {
  925. return SearchDirs.capacity()
  926. + llvm::capacity_in_bytes(FileInfo)
  927. + llvm::capacity_in_bytes(HeaderMaps)
  928. + LookupFileCache.getAllocator().getTotalMemory()
  929. + FrameworkMap.getAllocator().getTotalMemory();
  930. }
  931. StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
  932. return FrameworkNames.insert(Framework).first->first();
  933. }
  934. bool HeaderSearch::hasModuleMap(StringRef FileName,
  935. const DirectoryEntry *Root,
  936. bool IsSystem) {
  937. if (!HSOpts->ImplicitModuleMaps)
  938. return false;
  939. SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
  940. StringRef DirName = FileName;
  941. do {
  942. // Get the parent directory name.
  943. DirName = llvm::sys::path::parent_path(DirName);
  944. if (DirName.empty())
  945. return false;
  946. // Determine whether this directory exists.
  947. const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
  948. if (!Dir)
  949. return false;
  950. // Try to load the module map file in this directory.
  951. switch (loadModuleMapFile(Dir, IsSystem,
  952. llvm::sys::path::extension(Dir->getName()) ==
  953. ".framework")) {
  954. case LMM_NewlyLoaded:
  955. case LMM_AlreadyLoaded:
  956. // Success. All of the directories we stepped through inherit this module
  957. // map file.
  958. for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
  959. DirectoryHasModuleMap[FixUpDirectories[I]] = true;
  960. return true;
  961. case LMM_NoDirectory:
  962. case LMM_InvalidModuleMap:
  963. break;
  964. }
  965. // If we hit the top of our search, we're done.
  966. if (Dir == Root)
  967. return false;
  968. // Keep track of all of the directories we checked, so we can mark them as
  969. // having module maps if we eventually do find a module map.
  970. FixUpDirectories.push_back(Dir);
  971. } while (true);
  972. }
  973. ModuleMap::KnownHeader
  974. HeaderSearch::findModuleForHeader(const FileEntry *File) const {
  975. if (ExternalSource) {
  976. // Make sure the external source has handled header info about this file,
  977. // which includes whether the file is part of a module.
  978. (void)getFileInfo(File);
  979. }
  980. return ModMap.findModuleForHeader(File);
  981. }
  982. static const FileEntry *getPrivateModuleMap(const FileEntry *File,
  983. FileManager &FileMgr) {
  984. StringRef Filename = llvm::sys::path::filename(File->getName());
  985. SmallString<128> PrivateFilename(File->getDir()->getName());
  986. if (Filename == "module.map")
  987. llvm::sys::path::append(PrivateFilename, "module_private.map");
  988. else if (Filename == "module.modulemap")
  989. llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
  990. else
  991. return nullptr;
  992. return FileMgr.getFile(PrivateFilename);
  993. }
  994. bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
  995. // Find the directory for the module. For frameworks, that may require going
  996. // up from the 'Modules' directory.
  997. const DirectoryEntry *Dir = nullptr;
  998. if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
  999. Dir = FileMgr.getDirectory(".");
  1000. else {
  1001. Dir = File->getDir();
  1002. StringRef DirName(Dir->getName());
  1003. if (llvm::sys::path::filename(DirName) == "Modules") {
  1004. DirName = llvm::sys::path::parent_path(DirName);
  1005. if (DirName.endswith(".framework"))
  1006. Dir = FileMgr.getDirectory(DirName);
  1007. // FIXME: This assert can fail if there's a race between the above check
  1008. // and the removal of the directory.
  1009. assert(Dir && "parent must exist");
  1010. }
  1011. }
  1012. switch (loadModuleMapFileImpl(File, IsSystem, Dir)) {
  1013. case LMM_AlreadyLoaded:
  1014. case LMM_NewlyLoaded:
  1015. return false;
  1016. case LMM_NoDirectory:
  1017. case LMM_InvalidModuleMap:
  1018. return true;
  1019. }
  1020. llvm_unreachable("Unknown load module map result");
  1021. }
  1022. HeaderSearch::LoadModuleMapResult
  1023. HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
  1024. const DirectoryEntry *Dir) {
  1025. assert(File && "expected FileEntry");
  1026. // Check whether we've already loaded this module map, and mark it as being
  1027. // loaded in case we recursively try to load it from itself.
  1028. auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
  1029. if (!AddResult.second)
  1030. return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
  1031. if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) {
  1032. LoadedModuleMaps[File] = false;
  1033. return LMM_InvalidModuleMap;
  1034. }
  1035. // Try to load a corresponding private module map.
  1036. if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
  1037. if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
  1038. LoadedModuleMaps[File] = false;
  1039. return LMM_InvalidModuleMap;
  1040. }
  1041. }
  1042. // This directory has a module map.
  1043. return LMM_NewlyLoaded;
  1044. }
  1045. const FileEntry *
  1046. HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
  1047. if (!HSOpts->ImplicitModuleMaps)
  1048. return nullptr;
  1049. // For frameworks, the preferred spelling is Modules/module.modulemap, but
  1050. // module.map at the framework root is also accepted.
  1051. SmallString<128> ModuleMapFileName(Dir->getName());
  1052. if (IsFramework)
  1053. llvm::sys::path::append(ModuleMapFileName, "Modules");
  1054. llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
  1055. if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
  1056. return F;
  1057. // Continue to allow module.map
  1058. ModuleMapFileName = Dir->getName();
  1059. llvm::sys::path::append(ModuleMapFileName, "module.map");
  1060. return FileMgr.getFile(ModuleMapFileName);
  1061. }
  1062. Module *HeaderSearch::loadFrameworkModule(StringRef Name,
  1063. const DirectoryEntry *Dir,
  1064. bool IsSystem) {
  1065. if (Module *Module = ModMap.findModule(Name))
  1066. return Module;
  1067. // Try to load a module map file.
  1068. switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
  1069. case LMM_InvalidModuleMap:
  1070. // Try to infer a module map from the framework directory.
  1071. if (HSOpts->ImplicitModuleMaps)
  1072. ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
  1073. break;
  1074. case LMM_AlreadyLoaded:
  1075. case LMM_NoDirectory:
  1076. return nullptr;
  1077. case LMM_NewlyLoaded:
  1078. break;
  1079. }
  1080. return ModMap.findModule(Name);
  1081. }
  1082. HeaderSearch::LoadModuleMapResult
  1083. HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
  1084. bool IsFramework) {
  1085. if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
  1086. return loadModuleMapFile(Dir, IsSystem, IsFramework);
  1087. return LMM_NoDirectory;
  1088. }
  1089. HeaderSearch::LoadModuleMapResult
  1090. HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
  1091. bool IsFramework) {
  1092. auto KnownDir = DirectoryHasModuleMap.find(Dir);
  1093. if (KnownDir != DirectoryHasModuleMap.end())
  1094. return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
  1095. if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
  1096. LoadModuleMapResult Result =
  1097. loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
  1098. // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
  1099. // E.g. Foo.framework/Modules/module.modulemap
  1100. // ^Dir ^ModuleMapFile
  1101. if (Result == LMM_NewlyLoaded)
  1102. DirectoryHasModuleMap[Dir] = true;
  1103. else if (Result == LMM_InvalidModuleMap)
  1104. DirectoryHasModuleMap[Dir] = false;
  1105. return Result;
  1106. }
  1107. return LMM_InvalidModuleMap;
  1108. }
  1109. void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
  1110. Modules.clear();
  1111. if (HSOpts->ImplicitModuleMaps) {
  1112. // Load module maps for each of the header search directories.
  1113. for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
  1114. bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
  1115. if (SearchDirs[Idx].isFramework()) {
  1116. std::error_code EC;
  1117. SmallString<128> DirNative;
  1118. llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
  1119. DirNative);
  1120. // Search each of the ".framework" directories to load them as modules.
  1121. for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
  1122. Dir != DirEnd && !EC; Dir.increment(EC)) {
  1123. if (llvm::sys::path::extension(Dir->path()) != ".framework")
  1124. continue;
  1125. const DirectoryEntry *FrameworkDir =
  1126. FileMgr.getDirectory(Dir->path());
  1127. if (!FrameworkDir)
  1128. continue;
  1129. // Load this framework module.
  1130. loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
  1131. IsSystem);
  1132. }
  1133. continue;
  1134. }
  1135. // FIXME: Deal with header maps.
  1136. if (SearchDirs[Idx].isHeaderMap())
  1137. continue;
  1138. // Try to load a module map file for the search directory.
  1139. loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
  1140. /*IsFramework*/ false);
  1141. // Try to load module map files for immediate subdirectories of this
  1142. // search directory.
  1143. loadSubdirectoryModuleMaps(SearchDirs[Idx]);
  1144. }
  1145. }
  1146. // Populate the list of modules.
  1147. for (ModuleMap::module_iterator M = ModMap.module_begin(),
  1148. MEnd = ModMap.module_end();
  1149. M != MEnd; ++M) {
  1150. Modules.push_back(M->getValue());
  1151. }
  1152. }
  1153. void HeaderSearch::loadTopLevelSystemModules() {
  1154. if (!HSOpts->ImplicitModuleMaps)
  1155. return;
  1156. // Load module maps for each of the header search directories.
  1157. for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
  1158. // We only care about normal header directories.
  1159. if (!SearchDirs[Idx].isNormalDir()) {
  1160. continue;
  1161. }
  1162. // Try to load a module map file for the search directory.
  1163. loadModuleMapFile(SearchDirs[Idx].getDir(),
  1164. SearchDirs[Idx].isSystemHeaderDirectory(),
  1165. SearchDirs[Idx].isFramework());
  1166. }
  1167. }
  1168. void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
  1169. assert(HSOpts->ImplicitModuleMaps &&
  1170. "Should not be loading subdirectory module maps");
  1171. if (SearchDir.haveSearchedAllModuleMaps())
  1172. return;
  1173. std::error_code EC;
  1174. SmallString<128> DirNative;
  1175. llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
  1176. for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
  1177. Dir != DirEnd && !EC; Dir.increment(EC)) {
  1178. bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
  1179. if (IsFramework == SearchDir.isFramework())
  1180. loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
  1181. SearchDir.isFramework());
  1182. }
  1183. SearchDir.setSearchedAllModuleMaps(true);
  1184. }