HeaderSearch.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Chris Lattner and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the DirectoryLookup and HeaderSearch interfaces.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/FileManager.h"
  14. #include "clang/Lex/HeaderSearch.h"
  15. #include "clang/Lex/IdentifierTable.h"
  16. #include "llvm/System/Path.h"
  17. #include "llvm/ADT/SmallString.h"
  18. using namespace clang;
  19. HeaderSearch::HeaderSearch(FileManager &FM) : FileMgr(FM), FrameworkMap(64) {
  20. SystemDirIdx = 0;
  21. NoCurDirSearch = false;
  22. NumIncluded = 0;
  23. NumMultiIncludeFileOptzn = 0;
  24. NumFrameworkLookups = NumSubFrameworkLookups = 0;
  25. }
  26. void HeaderSearch::PrintStats() {
  27. fprintf(stderr, "\n*** HeaderSearch Stats:\n");
  28. fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
  29. unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
  30. for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
  31. NumOnceOnlyFiles += FileInfo[i].isImport;
  32. if (MaxNumIncludes < FileInfo[i].NumIncludes)
  33. MaxNumIncludes = FileInfo[i].NumIncludes;
  34. NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
  35. }
  36. fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
  37. fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
  38. fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
  39. fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
  40. fprintf(stderr, " %d #includes skipped due to"
  41. " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
  42. fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
  43. fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
  44. }
  45. //===----------------------------------------------------------------------===//
  46. // Header File Location.
  47. //===----------------------------------------------------------------------===//
  48. const FileEntry *HeaderSearch::DoFrameworkLookup(const DirectoryEntry *Dir,
  49. const char *FilenameStart,
  50. const char *FilenameEnd) {
  51. // Framework names must have a '/' in the filename.
  52. const char *SlashPos = std::find(FilenameStart, FilenameEnd, '/');
  53. if (SlashPos == FilenameEnd) return 0;
  54. llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
  55. FrameworkMap.GetOrCreateValue(FilenameStart, SlashPos);
  56. // If it is some other directory, fail.
  57. if (CacheLookup.getValue() && CacheLookup.getValue() != Dir)
  58. return 0;
  59. // FrameworkName = "/System/Library/Frameworks/"
  60. llvm::SmallString<1024> FrameworkName;
  61. FrameworkName += Dir->getName();
  62. if (FrameworkName.empty() || FrameworkName.back() != '/')
  63. FrameworkName.push_back('/');
  64. // FrameworkName = "/System/Library/Frameworks/Cocoa"
  65. FrameworkName.append(FilenameStart, SlashPos);
  66. // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
  67. FrameworkName += ".framework/";
  68. if (CacheLookup.getValue() == 0) {
  69. ++NumFrameworkLookups;
  70. // If the framework dir doesn't exist, we fail.
  71. if (!llvm::sys::Path(std::string(FrameworkName.begin(),
  72. FrameworkName.end())).exists())
  73. return 0;
  74. // Otherwise, if it does, remember that this is the right direntry for this
  75. // framework.
  76. CacheLookup.setValue(Dir);
  77. }
  78. // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
  79. unsigned OrigSize = FrameworkName.size();
  80. FrameworkName += "Headers/";
  81. FrameworkName.append(SlashPos+1, FilenameEnd);
  82. if (const FileEntry *FE = FileMgr.getFile(FrameworkName.begin(),
  83. FrameworkName.end())) {
  84. return FE;
  85. }
  86. // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
  87. const char *Private = "Private";
  88. FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
  89. Private+strlen(Private));
  90. return FileMgr.getFile(FrameworkName.begin(), FrameworkName.end());
  91. }
  92. /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
  93. /// return null on failure. isAngled indicates whether the file reference is
  94. /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
  95. /// non-null, indicates where the #including file is, in case a relative search
  96. /// is needed.
  97. const FileEntry *HeaderSearch::LookupFile(const char *FilenameStart,
  98. const char *FilenameEnd,
  99. bool isAngled,
  100. const DirectoryLookup *FromDir,
  101. const DirectoryLookup *&CurDir,
  102. const FileEntry *CurFileEnt) {
  103. // If 'Filename' is absolute, check to see if it exists and no searching.
  104. // FIXME: Portability. This should be a sys::Path interface, this doesn't
  105. // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
  106. if (FilenameStart[0] == '/') {
  107. CurDir = 0;
  108. // If this was an #include_next "/absolute/file", fail.
  109. if (FromDir) return 0;
  110. // Otherwise, just return the file.
  111. return FileMgr.getFile(FilenameStart, FilenameEnd);
  112. }
  113. llvm::SmallString<1024> TmpDir;
  114. // Step #0, unless disabled, check to see if the file is in the #includer's
  115. // directory. This search is not done for <> headers.
  116. if (CurFileEnt && !isAngled && !NoCurDirSearch) {
  117. // Concatenate the requested file onto the directory.
  118. // FIXME: Portability. Filename concatenation should be in sys::Path.
  119. TmpDir += CurFileEnt->getDir()->getName();
  120. TmpDir.push_back('/');
  121. TmpDir.append(FilenameStart, FilenameEnd);
  122. if (const FileEntry *FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end())) {
  123. // Leave CurDir unset.
  124. // This file is a system header or C++ unfriendly if the old file is.
  125. getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo;
  126. return FE;
  127. }
  128. TmpDir.clear();
  129. }
  130. CurDir = 0;
  131. // If this is a system #include, ignore the user #include locs.
  132. unsigned i = isAngled ? SystemDirIdx : 0;
  133. // If this is a #include_next request, start searching after the directory the
  134. // file was found in.
  135. if (FromDir)
  136. i = FromDir-&SearchDirs[0];
  137. // Check each directory in sequence to see if it contains this file.
  138. for (; i != SearchDirs.size(); ++i) {
  139. const FileEntry *FE = 0;
  140. if (!SearchDirs[i].isFramework()) {
  141. // FIXME: Portability. Adding file to dir should be in sys::Path.
  142. // Concatenate the requested file onto the directory.
  143. TmpDir.clear();
  144. TmpDir += SearchDirs[i].getDir()->getName();
  145. TmpDir.push_back('/');
  146. TmpDir.append(FilenameStart, FilenameEnd);
  147. FE = FileMgr.getFile(TmpDir.begin(), TmpDir.end());
  148. } else {
  149. FE = DoFrameworkLookup(SearchDirs[i].getDir(), FilenameStart,FilenameEnd);
  150. }
  151. if (FE) {
  152. CurDir = &SearchDirs[i];
  153. // This file is a system header or C++ unfriendly if the dir is.
  154. getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
  155. return FE;
  156. }
  157. }
  158. // Otherwise, didn't find it.
  159. return 0;
  160. }
  161. /// LookupSubframeworkHeader - Look up a subframework for the specified
  162. /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
  163. /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
  164. /// is a subframework within Carbon.framework. If so, return the FileEntry
  165. /// for the designated file, otherwise return null.
  166. const FileEntry *HeaderSearch::
  167. LookupSubframeworkHeader(const char *FilenameStart,
  168. const char *FilenameEnd,
  169. const FileEntry *ContextFileEnt) {
  170. // Framework names must have a '/' in the filename. Find it.
  171. const char *SlashPos = std::find(FilenameStart, FilenameEnd, '/');
  172. if (SlashPos == FilenameEnd) return 0;
  173. // Look up the base framework name of the ContextFileEnt.
  174. const char *ContextName = ContextFileEnt->getName();
  175. // If the context info wasn't a framework, couldn't be a subframework.
  176. const char *FrameworkPos = strstr(ContextName, ".framework/");
  177. if (FrameworkPos == 0)
  178. return 0;
  179. llvm::SmallString<1024> FrameworkName(ContextName,
  180. FrameworkPos+strlen(".framework/"));
  181. // Append Frameworks/HIToolbox.framework/
  182. FrameworkName += "Frameworks/";
  183. FrameworkName.append(FilenameStart, SlashPos);
  184. FrameworkName += ".framework/";
  185. llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
  186. FrameworkMap.GetOrCreateValue(FilenameStart, SlashPos);
  187. // Some other location?
  188. if (CacheLookup.getValue() &&
  189. CacheLookup.getKeyLength() == FrameworkName.size() &&
  190. memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
  191. CacheLookup.getKeyLength()) != 0)
  192. return 0;
  193. // Cache subframework.
  194. if (CacheLookup.getValue() == 0) {
  195. ++NumSubFrameworkLookups;
  196. // If the framework dir doesn't exist, we fail.
  197. const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
  198. FrameworkName.end());
  199. if (Dir == 0) return 0;
  200. // Otherwise, if it does, remember that this is the right direntry for this
  201. // framework.
  202. CacheLookup.setValue(Dir);
  203. }
  204. const FileEntry *FE = 0;
  205. // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
  206. llvm::SmallString<1024> HeadersFilename(FrameworkName);
  207. HeadersFilename += "Headers/";
  208. HeadersFilename.append(SlashPos+1, FilenameEnd);
  209. if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
  210. HeadersFilename.end()))) {
  211. // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
  212. HeadersFilename = FrameworkName;
  213. HeadersFilename += "PrivateHeaders/";
  214. HeadersFilename.append(SlashPos+1, FilenameEnd);
  215. if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
  216. return 0;
  217. }
  218. // This file is a system header or C++ unfriendly if the old file is.
  219. getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo;
  220. return FE;
  221. }
  222. //===----------------------------------------------------------------------===//
  223. // File Info Management.
  224. //===----------------------------------------------------------------------===//
  225. /// getFileInfo - Return the PerFileInfo structure for the specified
  226. /// FileEntry.
  227. HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
  228. if (FE->getUID() >= FileInfo.size())
  229. FileInfo.resize(FE->getUID()+1);
  230. return FileInfo[FE->getUID()];
  231. }
  232. /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
  233. /// #include, #include_next, or #import directive. Return false if #including
  234. /// the file will have no effect or true if we should include it.
  235. bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
  236. ++NumIncluded; // Count # of attempted #includes.
  237. // Get information about this file.
  238. PerFileInfo &FileInfo = getFileInfo(File);
  239. // If this is a #import directive, check that we have not already imported
  240. // this header.
  241. if (isImport) {
  242. // If this has already been imported, don't import it again.
  243. FileInfo.isImport = true;
  244. // Has this already been #import'ed or #include'd?
  245. if (FileInfo.NumIncludes) return false;
  246. } else {
  247. // Otherwise, if this is a #include of a file that was previously #import'd
  248. // or if this is the second #include of a #pragma once file, ignore it.
  249. if (FileInfo.isImport)
  250. return false;
  251. }
  252. // Next, check to see if the file is wrapped with #ifndef guards. If so, and
  253. // if the macro that guards it is defined, we know the #include has no effect.
  254. if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
  255. ++NumMultiIncludeFileOptzn;
  256. return false;
  257. }
  258. // Increment the number of times this file has been included.
  259. ++FileInfo.NumIncludes;
  260. return true;
  261. }