HeaderSearch.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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/Lex/HeaderMap.h"
  15. #include "clang/Basic/FileManager.h"
  16. #include "clang/Basic/IdentifierTable.h"
  17. #include "llvm/System/Path.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include <cstdio>
  20. using namespace clang;
  21. const IdentifierInfo *
  22. HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
  23. if (ControllingMacro)
  24. return ControllingMacro;
  25. if (!ControllingMacroID || !External)
  26. return 0;
  27. ControllingMacro = External->GetIdentifier(ControllingMacroID);
  28. return ControllingMacro;
  29. }
  30. HeaderSearch::HeaderSearch(FileManager &FM, const FileSystemOptions &FSOpts)
  31. : FileMgr(FM), FileSystemOpts(FSOpts), FrameworkMap(64) {
  32. SystemDirIdx = 0;
  33. NoCurDirSearch = false;
  34. ExternalLookup = 0;
  35. NumIncluded = 0;
  36. NumMultiIncludeFileOptzn = 0;
  37. NumFrameworkLookups = NumSubFrameworkLookups = 0;
  38. }
  39. HeaderSearch::~HeaderSearch() {
  40. // Delete headermaps.
  41. for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
  42. delete HeaderMaps[i].second;
  43. }
  44. void HeaderSearch::PrintStats() {
  45. fprintf(stderr, "\n*** HeaderSearch Stats:\n");
  46. fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
  47. unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
  48. for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
  49. NumOnceOnlyFiles += FileInfo[i].isImport;
  50. if (MaxNumIncludes < FileInfo[i].NumIncludes)
  51. MaxNumIncludes = FileInfo[i].NumIncludes;
  52. NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
  53. }
  54. fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
  55. fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
  56. fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
  57. fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
  58. fprintf(stderr, " %d #includes skipped due to"
  59. " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
  60. fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
  61. fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
  62. }
  63. /// CreateHeaderMap - This method returns a HeaderMap for the specified
  64. /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
  65. const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
  66. // We expect the number of headermaps to be small, and almost always empty.
  67. // If it ever grows, use of a linear search should be re-evaluated.
  68. if (!HeaderMaps.empty()) {
  69. for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
  70. // Pointer equality comparison of FileEntries works because they are
  71. // already uniqued by inode.
  72. if (HeaderMaps[i].first == FE)
  73. return HeaderMaps[i].second;
  74. }
  75. if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr, FileSystemOpts)) {
  76. HeaderMaps.push_back(std::make_pair(FE, HM));
  77. return HM;
  78. }
  79. return 0;
  80. }
  81. //===----------------------------------------------------------------------===//
  82. // File lookup within a DirectoryLookup scope
  83. //===----------------------------------------------------------------------===//
  84. /// getName - Return the directory or filename corresponding to this lookup
  85. /// object.
  86. const char *DirectoryLookup::getName() const {
  87. if (isNormalDir())
  88. return getDir()->getName();
  89. if (isFramework())
  90. return getFrameworkDir()->getName();
  91. assert(isHeaderMap() && "Unknown DirectoryLookup");
  92. return getHeaderMap()->getFileName();
  93. }
  94. /// LookupFile - Lookup the specified file in this search path, returning it
  95. /// if it exists or returning null if not.
  96. const FileEntry *DirectoryLookup::LookupFile(llvm::StringRef Filename,
  97. HeaderSearch &HS) const {
  98. llvm::SmallString<1024> TmpDir;
  99. if (isNormalDir()) {
  100. // Concatenate the requested file onto the directory.
  101. // FIXME: Portability. Filename concatenation should be in sys::Path.
  102. TmpDir += getDir()->getName();
  103. TmpDir.push_back('/');
  104. TmpDir.append(Filename.begin(), Filename.end());
  105. return HS.getFileMgr().getFile(TmpDir.begin(), TmpDir.end(),
  106. HS.getFileSystemOpts());
  107. }
  108. if (isFramework())
  109. return DoFrameworkLookup(Filename, HS);
  110. assert(isHeaderMap() && "Unknown directory lookup");
  111. return getHeaderMap()->LookupFile(Filename, HS.getFileMgr(),
  112. HS.getFileSystemOpts());
  113. }
  114. /// DoFrameworkLookup - Do a lookup of the specified file in the current
  115. /// DirectoryLookup, which is a framework directory.
  116. const FileEntry *DirectoryLookup::DoFrameworkLookup(llvm::StringRef Filename,
  117. HeaderSearch &HS) const {
  118. FileManager &FileMgr = HS.getFileMgr();
  119. const FileSystemOptions &FileSystemOpts = HS.getFileSystemOpts();
  120. // Framework names must have a '/' in the filename.
  121. size_t SlashPos = Filename.find('/');
  122. if (SlashPos == llvm::StringRef::npos) return 0;
  123. // Find out if this is the home for the specified framework, by checking
  124. // HeaderSearch. Possible answer are yes/no and unknown.
  125. const DirectoryEntry *&FrameworkDirCache =
  126. HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
  127. // If it is known and in some other directory, fail.
  128. if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
  129. return 0;
  130. // Otherwise, construct the path to this framework dir.
  131. // FrameworkName = "/System/Library/Frameworks/"
  132. llvm::SmallString<1024> FrameworkName;
  133. FrameworkName += getFrameworkDir()->getName();
  134. if (FrameworkName.empty() || FrameworkName.back() != '/')
  135. FrameworkName.push_back('/');
  136. // FrameworkName = "/System/Library/Frameworks/Cocoa"
  137. FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
  138. // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
  139. FrameworkName += ".framework/";
  140. // If the cache entry is still unresolved, query to see if the cache entry is
  141. // still unresolved. If so, check its existence now.
  142. if (FrameworkDirCache == 0) {
  143. HS.IncrementFrameworkLookupCount();
  144. // If the framework dir doesn't exist, we fail.
  145. // FIXME: It's probably more efficient to query this with FileMgr.getDir.
  146. if (!llvm::sys::Path(std::string(FrameworkName.begin(),
  147. FrameworkName.end())).exists())
  148. return 0;
  149. // Otherwise, if it does, remember that this is the right direntry for this
  150. // framework.
  151. FrameworkDirCache = getFrameworkDir();
  152. }
  153. // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
  154. unsigned OrigSize = FrameworkName.size();
  155. FrameworkName += "Headers/";
  156. FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
  157. if (const FileEntry *FE = FileMgr.getFile(FrameworkName.begin(),
  158. FrameworkName.end(),
  159. FileSystemOpts)) {
  160. return FE;
  161. }
  162. // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
  163. const char *Private = "Private";
  164. FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
  165. Private+strlen(Private));
  166. return FileMgr.getFile(FrameworkName.begin(), FrameworkName.end(),
  167. FileSystemOpts);
  168. }
  169. //===----------------------------------------------------------------------===//
  170. // Header File Location.
  171. //===----------------------------------------------------------------------===//
  172. /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
  173. /// return null on failure. isAngled indicates whether the file reference is
  174. /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
  175. /// non-null, indicates where the #including file is, in case a relative search
  176. /// is needed.
  177. const FileEntry *HeaderSearch::LookupFile(llvm::StringRef Filename,
  178. bool isAngled,
  179. const DirectoryLookup *FromDir,
  180. const DirectoryLookup *&CurDir,
  181. const FileEntry *CurFileEnt) {
  182. // If 'Filename' is absolute, check to see if it exists and no searching.
  183. if (llvm::sys::Path::isAbsolute(Filename.begin(), Filename.size())) {
  184. CurDir = 0;
  185. // If this was an #include_next "/absolute/file", fail.
  186. if (FromDir) return 0;
  187. // Otherwise, just return the file.
  188. return FileMgr.getFile(Filename, FileSystemOpts);
  189. }
  190. // Step #0, unless disabled, check to see if the file is in the #includer's
  191. // directory. This has to be based on CurFileEnt, not CurDir, because
  192. // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
  193. // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
  194. // This search is not done for <> headers.
  195. if (CurFileEnt && !isAngled && !NoCurDirSearch) {
  196. llvm::SmallString<1024> TmpDir;
  197. // Concatenate the requested file onto the directory.
  198. // FIXME: Portability. Filename concatenation should be in sys::Path.
  199. TmpDir += CurFileEnt->getDir()->getName();
  200. TmpDir.push_back('/');
  201. TmpDir.append(Filename.begin(), Filename.end());
  202. if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(), FileSystemOpts)) {
  203. // Leave CurDir unset.
  204. // This file is a system header or C++ unfriendly if the old file is.
  205. //
  206. // Note that the temporary 'DirInfo' is required here, as either call to
  207. // getFileInfo could resize the vector and we don't want to rely on order
  208. // of evaluation.
  209. unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
  210. getFileInfo(FE).DirInfo = DirInfo;
  211. return FE;
  212. }
  213. }
  214. CurDir = 0;
  215. // If this is a system #include, ignore the user #include locs.
  216. unsigned i = isAngled ? SystemDirIdx : 0;
  217. // If this is a #include_next request, start searching after the directory the
  218. // file was found in.
  219. if (FromDir)
  220. i = FromDir-&SearchDirs[0];
  221. // Cache all of the lookups performed by this method. Many headers are
  222. // multiply included, and the "pragma once" optimization prevents them from
  223. // being relex/pp'd, but they would still have to search through a
  224. // (potentially huge) series of SearchDirs to find it.
  225. std::pair<unsigned, unsigned> &CacheLookup =
  226. LookupFileCache.GetOrCreateValue(Filename).getValue();
  227. // If the entry has been previously looked up, the first value will be
  228. // non-zero. If the value is equal to i (the start point of our search), then
  229. // this is a matching hit.
  230. if (CacheLookup.first == i+1) {
  231. // Skip querying potentially lots of directories for this lookup.
  232. i = CacheLookup.second;
  233. } else {
  234. // Otherwise, this is the first query, or the previous query didn't match
  235. // our search start. We will fill in our found location below, so prime the
  236. // start point value.
  237. CacheLookup.first = i+1;
  238. }
  239. // Check each directory in sequence to see if it contains this file.
  240. for (; i != SearchDirs.size(); ++i) {
  241. const FileEntry *FE =
  242. SearchDirs[i].LookupFile(Filename, *this);
  243. if (!FE) continue;
  244. CurDir = &SearchDirs[i];
  245. // This file is a system header or C++ unfriendly if the dir is.
  246. getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
  247. // Remember this location for the next lookup we do.
  248. CacheLookup.second = i;
  249. return FE;
  250. }
  251. // Otherwise, didn't find it. Remember we didn't find this.
  252. CacheLookup.second = SearchDirs.size();
  253. return 0;
  254. }
  255. /// LookupSubframeworkHeader - Look up a subframework for the specified
  256. /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
  257. /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
  258. /// is a subframework within Carbon.framework. If so, return the FileEntry
  259. /// for the designated file, otherwise return null.
  260. const FileEntry *HeaderSearch::
  261. LookupSubframeworkHeader(llvm::StringRef Filename,
  262. const FileEntry *ContextFileEnt) {
  263. assert(ContextFileEnt && "No context file?");
  264. // Framework names must have a '/' in the filename. Find it.
  265. size_t SlashPos = Filename.find('/');
  266. if (SlashPos == llvm::StringRef::npos) return 0;
  267. // Look up the base framework name of the ContextFileEnt.
  268. const char *ContextName = ContextFileEnt->getName();
  269. // If the context info wasn't a framework, couldn't be a subframework.
  270. const char *FrameworkPos = strstr(ContextName, ".framework/");
  271. if (FrameworkPos == 0)
  272. return 0;
  273. llvm::SmallString<1024> FrameworkName(ContextName,
  274. FrameworkPos+strlen(".framework/"));
  275. // Append Frameworks/HIToolbox.framework/
  276. FrameworkName += "Frameworks/";
  277. FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
  278. FrameworkName += ".framework/";
  279. llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
  280. FrameworkMap.GetOrCreateValue(Filename.begin(), Filename.begin()+SlashPos);
  281. // Some other location?
  282. if (CacheLookup.getValue() &&
  283. CacheLookup.getKeyLength() == FrameworkName.size() &&
  284. memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
  285. CacheLookup.getKeyLength()) != 0)
  286. return 0;
  287. // Cache subframework.
  288. if (CacheLookup.getValue() == 0) {
  289. ++NumSubFrameworkLookups;
  290. // If the framework dir doesn't exist, we fail.
  291. const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
  292. FrameworkName.end(),
  293. FileSystemOpts);
  294. if (Dir == 0) return 0;
  295. // Otherwise, if it does, remember that this is the right direntry for this
  296. // framework.
  297. CacheLookup.setValue(Dir);
  298. }
  299. const FileEntry *FE = 0;
  300. // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
  301. llvm::SmallString<1024> HeadersFilename(FrameworkName);
  302. HeadersFilename += "Headers/";
  303. HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
  304. if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
  305. HeadersFilename.end(), FileSystemOpts))) {
  306. // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
  307. HeadersFilename = FrameworkName;
  308. HeadersFilename += "PrivateHeaders/";
  309. HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
  310. if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end(),
  311. FileSystemOpts)))
  312. return 0;
  313. }
  314. // This file is a system header or C++ unfriendly if the old file is.
  315. //
  316. // Note that the temporary 'DirInfo' is required here, as either call to
  317. // getFileInfo could resize the vector and we don't want to rely on order
  318. // of evaluation.
  319. unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
  320. getFileInfo(FE).DirInfo = DirInfo;
  321. return FE;
  322. }
  323. //===----------------------------------------------------------------------===//
  324. // File Info Management.
  325. //===----------------------------------------------------------------------===//
  326. /// getFileInfo - Return the HeaderFileInfo structure for the specified
  327. /// FileEntry.
  328. HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
  329. if (FE->getUID() >= FileInfo.size())
  330. FileInfo.resize(FE->getUID()+1);
  331. return FileInfo[FE->getUID()];
  332. }
  333. void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
  334. if (UID >= FileInfo.size())
  335. FileInfo.resize(UID+1);
  336. FileInfo[UID] = HFI;
  337. }
  338. /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
  339. /// #include, #include_next, or #import directive. Return false if #including
  340. /// the file will have no effect or true if we should include it.
  341. bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
  342. ++NumIncluded; // Count # of attempted #includes.
  343. // Get information about this file.
  344. HeaderFileInfo &FileInfo = getFileInfo(File);
  345. // If this is a #import directive, check that we have not already imported
  346. // this header.
  347. if (isImport) {
  348. // If this has already been imported, don't import it again.
  349. FileInfo.isImport = true;
  350. // Has this already been #import'ed or #include'd?
  351. if (FileInfo.NumIncludes) return false;
  352. } else {
  353. // Otherwise, if this is a #include of a file that was previously #import'd
  354. // or if this is the second #include of a #pragma once file, ignore it.
  355. if (FileInfo.isImport)
  356. return false;
  357. }
  358. // Next, check to see if the file is wrapped with #ifndef guards. If so, and
  359. // if the macro that guards it is defined, we know the #include has no effect.
  360. if (const IdentifierInfo *ControllingMacro
  361. = FileInfo.getControllingMacro(ExternalLookup))
  362. if (ControllingMacro->hasMacroDefinition()) {
  363. ++NumMultiIncludeFileOptzn;
  364. return false;
  365. }
  366. // Increment the number of times this file has been included.
  367. ++FileInfo.NumIncludes;
  368. return true;
  369. }