Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- HeaderSearch.cpp - Resolve Header File Locations ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the DirectoryLookup and HeaderSearch interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 14 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 15 | #include "clang/Lex/HeaderMap.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
| 17 | #include "clang/Basic/IdentifierTable.h" |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | eabea45 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Capacity.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 22 | #include <cstdio> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 25 | const IdentifierInfo * |
| 26 | HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) { |
| 27 | if (ControllingMacro) |
| 28 | return ControllingMacro; |
| 29 | |
| 30 | if (!ControllingMacroID || !External) |
| 31 | return 0; |
| 32 | |
| 33 | ControllingMacro = External->GetIdentifier(ControllingMacroID); |
| 34 | return ControllingMacro; |
| 35 | } |
| 36 | |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 37 | ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {} |
| 38 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 39 | HeaderSearch::HeaderSearch(FileManager &FM) |
| 40 | : FileMgr(FM), FrameworkMap(64) { |
Nico Weber | 74a5fd8 | 2011-05-24 04:31:14 +0000 | [diff] [blame] | 41 | AngledDirIdx = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | SystemDirIdx = 0; |
| 43 | NoCurDirSearch = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 45 | ExternalLookup = 0; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 46 | ExternalSource = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | NumIncluded = 0; |
| 48 | NumMultiIncludeFileOptzn = 0; |
| 49 | NumFrameworkLookups = NumSubFrameworkLookups = 0; |
| 50 | } |
| 51 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 52 | HeaderSearch::~HeaderSearch() { |
| 53 | // Delete headermaps. |
| 54 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
| 55 | delete HeaderMaps[i].second; |
| 56 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | void HeaderSearch::PrintStats() { |
| 59 | fprintf(stderr, "\n*** HeaderSearch Stats:\n"); |
| 60 | fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size()); |
| 61 | unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; |
| 62 | for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { |
| 63 | NumOnceOnlyFiles += FileInfo[i].isImport; |
| 64 | if (MaxNumIncludes < FileInfo[i].NumIncludes) |
| 65 | MaxNumIncludes = FileInfo[i].NumIncludes; |
| 66 | NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; |
| 67 | } |
| 68 | fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles); |
| 69 | fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles); |
| 70 | fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded); |
| 73 | fprintf(stderr, " %d #includes skipped due to" |
| 74 | " the multi-include optimization.\n", NumMultiIncludeFileOptzn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups); |
| 77 | fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups); |
| 78 | } |
| 79 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 80 | /// CreateHeaderMap - This method returns a HeaderMap for the specified |
| 81 | /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. |
Chris Lattner | 1bfd4a6 | 2007-12-17 18:34:53 +0000 | [diff] [blame] | 82 | const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 83 | // We expect the number of headermaps to be small, and almost always empty. |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 84 | // If it ever grows, use of a linear search should be re-evaluated. |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 85 | if (!HeaderMaps.empty()) { |
| 86 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 87 | // Pointer equality comparison of FileEntries works because they are |
| 88 | // already uniqued by inode. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | if (HeaderMaps[i].first == FE) |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 90 | return HeaderMaps[i].second; |
| 91 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 93 | if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 94 | HeaderMaps.push_back(std::make_pair(FE, HM)); |
| 95 | return HM; |
| 96 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 101 | //===----------------------------------------------------------------------===// |
| 102 | // File lookup within a DirectoryLookup scope |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | |
Chris Lattner | 3af66a9 | 2007-12-17 17:57:27 +0000 | [diff] [blame] | 105 | /// getName - Return the directory or filename corresponding to this lookup |
| 106 | /// object. |
| 107 | const char *DirectoryLookup::getName() const { |
| 108 | if (isNormalDir()) |
| 109 | return getDir()->getName(); |
| 110 | if (isFramework()) |
| 111 | return getFrameworkDir()->getName(); |
| 112 | assert(isHeaderMap() && "Unknown DirectoryLookup"); |
| 113 | return getHeaderMap()->getFileName(); |
| 114 | } |
| 115 | |
| 116 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 117 | /// LookupFile - Lookup the specified file in this search path, returning it |
| 118 | /// if it exists or returning null if not. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 119 | const FileEntry *DirectoryLookup::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 120 | StringRef Filename, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 121 | HeaderSearch &HS, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 122 | SmallVectorImpl<char> *SearchPath, |
| 123 | SmallVectorImpl<char> *RelativePath) const { |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 124 | llvm::SmallString<1024> TmpDir; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 125 | if (isNormalDir()) { |
| 126 | // Concatenate the requested file onto the directory. |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 127 | TmpDir = getDir()->getName(); |
| 128 | llvm::sys::path::append(TmpDir, Filename); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 129 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 130 | StringRef SearchPathRef(getDir()->getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 131 | SearchPath->clear(); |
| 132 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 133 | } |
| 134 | if (RelativePath != NULL) { |
| 135 | RelativePath->clear(); |
| 136 | RelativePath->append(Filename.begin(), Filename.end()); |
| 137 | } |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 138 | return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 139 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 141 | if (isFramework()) |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 142 | return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Chris Lattner | b09e71f | 2007-12-17 08:17:39 +0000 | [diff] [blame] | 144 | assert(isHeaderMap() && "Unknown directory lookup"); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 145 | const FileEntry * const Result = getHeaderMap()->LookupFile( |
| 146 | Filename, HS.getFileMgr()); |
| 147 | if (Result) { |
| 148 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 149 | StringRef SearchPathRef(getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 150 | SearchPath->clear(); |
| 151 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 152 | } |
| 153 | if (RelativePath != NULL) { |
| 154 | RelativePath->clear(); |
| 155 | RelativePath->append(Filename.begin(), Filename.end()); |
| 156 | } |
| 157 | } |
| 158 | return Result; |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 162 | /// DoFrameworkLookup - Do a lookup of the specified file in the current |
| 163 | /// DirectoryLookup, which is a framework directory. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 164 | const FileEntry *DirectoryLookup::DoFrameworkLookup( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 165 | StringRef Filename, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 166 | HeaderSearch &HS, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 167 | SmallVectorImpl<char> *SearchPath, |
| 168 | SmallVectorImpl<char> *RelativePath) const { |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 169 | FileManager &FileMgr = HS.getFileMgr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | // Framework names must have a '/' in the filename. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 172 | size_t SlashPos = Filename.find('/'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 173 | if (SlashPos == StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 175 | // Find out if this is the home for the specified framework, by checking |
| 176 | // HeaderSearch. Possible answer are yes/no and unknown. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | const DirectoryEntry *&FrameworkDirCache = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 178 | HS.LookupFrameworkCache(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 180 | // If it is known and in some other directory, fail. |
| 181 | if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 184 | // Otherwise, construct the path to this framework dir. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | // FrameworkName = "/System/Library/Frameworks/" |
| 187 | llvm::SmallString<1024> FrameworkName; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 188 | FrameworkName += getFrameworkDir()->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | if (FrameworkName.empty() || FrameworkName.back() != '/') |
| 190 | FrameworkName.push_back('/'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | // FrameworkName = "/System/Library/Frameworks/Cocoa" |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 193 | FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" |
| 196 | FrameworkName += ".framework/"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 198 | // If the cache entry is still unresolved, query to see if the cache entry is |
| 199 | // still unresolved. If so, check its existence now. |
| 200 | if (FrameworkDirCache == 0) { |
| 201 | HS.IncrementFrameworkLookupCount(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 203 | // If the framework dir doesn't exist, we fail. |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 204 | // FIXME: It's probably more efficient to query this with FileMgr.getDir. |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 205 | bool Exists; |
| 206 | if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 207 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | // Otherwise, if it does, remember that this is the right direntry for this |
| 210 | // framework. |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 211 | FrameworkDirCache = getFrameworkDir(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 214 | if (RelativePath != NULL) { |
| 215 | RelativePath->clear(); |
| 216 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 217 | } |
| 218 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" |
| 220 | unsigned OrigSize = FrameworkName.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 222 | FrameworkName += "Headers/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 223 | |
| 224 | if (SearchPath != NULL) { |
| 225 | SearchPath->clear(); |
| 226 | // Without trailing '/'. |
| 227 | SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1); |
| 228 | } |
| 229 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 230 | FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 231 | if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(), |
| 232 | /*openFile=*/true)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | return FE; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 234 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" |
| 237 | const char *Private = "Private"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | FrameworkName.insert(FrameworkName.begin()+OrigSize, Private, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | Private+strlen(Private)); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 240 | if (SearchPath != NULL) |
| 241 | SearchPath->insert(SearchPath->begin()+OrigSize, Private, |
| 242 | Private+strlen(Private)); |
| 243 | |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 244 | return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 247 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 248 | //===----------------------------------------------------------------------===// |
| 249 | // Header File Location. |
| 250 | //===----------------------------------------------------------------------===// |
| 251 | |
| 252 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 254 | /// return null on failure. isAngled indicates whether the file reference is |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 255 | /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if |
| 256 | /// non-null, indicates where the #including file is, in case a relative search |
| 257 | /// is needed. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 258 | const FileEntry *HeaderSearch::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 259 | StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 260 | bool isAngled, |
| 261 | const DirectoryLookup *FromDir, |
| 262 | const DirectoryLookup *&CurDir, |
| 263 | const FileEntry *CurFileEnt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 264 | SmallVectorImpl<char> *SearchPath, |
| 265 | SmallVectorImpl<char> *RelativePath) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | // If 'Filename' is absolute, check to see if it exists and no searching. |
Michael J. Spencer | 256053b | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 267 | if (llvm::sys::path::is_absolute(Filename)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | CurDir = 0; |
| 269 | |
| 270 | // If this was an #include_next "/absolute/file", fail. |
| 271 | if (FromDir) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 273 | if (SearchPath != NULL) |
| 274 | SearchPath->clear(); |
| 275 | if (RelativePath != NULL) { |
| 276 | RelativePath->clear(); |
| 277 | RelativePath->append(Filename.begin(), Filename.end()); |
| 278 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | // Otherwise, just return the file. |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 280 | return FileMgr.getFile(Filename, /*openFile=*/true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 283 | // Unless disabled, check to see if the file is in the #includer's |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 284 | // directory. This has to be based on CurFileEnt, not CurDir, because |
| 285 | // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 286 | // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h". |
| 287 | // This search is not done for <> headers. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 288 | if (CurFileEnt && !isAngled && !NoCurDirSearch) { |
| 289 | llvm::SmallString<1024> TmpDir; |
| 290 | // Concatenate the requested file onto the directory. |
| 291 | // FIXME: Portability. Filename concatenation should be in sys::Path. |
| 292 | TmpDir += CurFileEnt->getDir()->getName(); |
| 293 | TmpDir.push_back('/'); |
| 294 | TmpDir.append(Filename.begin(), Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 295 | if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | // Leave CurDir unset. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 297 | // This file is a system header or C++ unfriendly if the old file is. |
| 298 | // |
| 299 | // Note that the temporary 'DirInfo' is required here, as either call to |
| 300 | // getFileInfo could resize the vector and we don't want to rely on order |
| 301 | // of evaluation. |
| 302 | unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo; |
Chris Lattner | c9dde4f | 2008-02-25 21:38:21 +0000 | [diff] [blame] | 303 | getFileInfo(FE).DirInfo = DirInfo; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 304 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 305 | StringRef SearchPathRef(CurFileEnt->getDir()->getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 306 | SearchPath->clear(); |
| 307 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 308 | } |
| 309 | if (RelativePath != NULL) { |
| 310 | RelativePath->clear(); |
| 311 | RelativePath->append(Filename.begin(), Filename.end()); |
| 312 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | return FE; |
| 314 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 316 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | CurDir = 0; |
| 318 | |
| 319 | // If this is a system #include, ignore the user #include locs. |
Nico Weber | 74a5fd8 | 2011-05-24 04:31:14 +0000 | [diff] [blame] | 320 | unsigned i = isAngled ? AngledDirIdx : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | // If this is a #include_next request, start searching after the directory the |
| 323 | // file was found in. |
| 324 | if (FromDir) |
| 325 | i = FromDir-&SearchDirs[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 327 | // Cache all of the lookups performed by this method. Many headers are |
| 328 | // multiply included, and the "pragma once" optimization prevents them from |
| 329 | // being relex/pp'd, but they would still have to search through a |
| 330 | // (potentially huge) series of SearchDirs to find it. |
| 331 | std::pair<unsigned, unsigned> &CacheLookup = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 332 | LookupFileCache.GetOrCreateValue(Filename).getValue(); |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 333 | |
| 334 | // If the entry has been previously looked up, the first value will be |
| 335 | // non-zero. If the value is equal to i (the start point of our search), then |
| 336 | // this is a matching hit. |
| 337 | if (CacheLookup.first == i+1) { |
| 338 | // Skip querying potentially lots of directories for this lookup. |
| 339 | i = CacheLookup.second; |
| 340 | } else { |
| 341 | // Otherwise, this is the first query, or the previous query didn't match |
| 342 | // our search start. We will fill in our found location below, so prime the |
| 343 | // start point value. |
| 344 | CacheLookup.first = i+1; |
| 345 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | // Check each directory in sequence to see if it contains this file. |
| 348 | for (; i != SearchDirs.size(); ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 349 | const FileEntry *FE = |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 350 | SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 351 | if (!FE) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 353 | CurDir = &SearchDirs[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 355 | // This file is a system header or C++ unfriendly if the dir is. |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 356 | HeaderFileInfo &HFI = getFileInfo(FE); |
| 357 | HFI.DirInfo = CurDir->getDirCharacteristic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 359 | // If this file is found in a header map and uses the framework style of |
| 360 | // includes, then this header is part of a framework we're building. |
| 361 | if (CurDir->isIndexHeaderMap()) { |
| 362 | size_t SlashPos = Filename.find('/'); |
| 363 | if (SlashPos != StringRef::npos) { |
| 364 | HFI.IndexHeaderMapHeader = 1; |
| 365 | HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(), |
| 366 | SlashPos)); |
| 367 | } |
| 368 | } |
| 369 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 370 | // Remember this location for the next lookup we do. |
| 371 | CacheLookup.second = i; |
| 372 | return FE; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 373 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Douglas Gregor | 2c7b780 | 2011-07-30 06:28:34 +0000 | [diff] [blame^] | 375 | // If we are including a file with a quoted include "foo.h" from inside |
| 376 | // a header in a framework that is currently being built, and we couldn't |
| 377 | // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where |
| 378 | // "Foo" is the name of the framework in which the including header was found. |
| 379 | if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) { |
| 380 | HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt); |
| 381 | if (IncludingHFI.IndexHeaderMapHeader) { |
| 382 | llvm::SmallString<128> ScratchFilename; |
| 383 | ScratchFilename += IncludingHFI.Framework; |
| 384 | ScratchFilename += '/'; |
| 385 | ScratchFilename += Filename; |
| 386 | |
| 387 | const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true, |
| 388 | FromDir, CurDir, CurFileEnt, |
| 389 | SearchPath, RelativePath); |
| 390 | std::pair<unsigned, unsigned> &CacheLookup |
| 391 | = LookupFileCache.GetOrCreateValue(Filename).getValue(); |
| 392 | CacheLookup.second |
| 393 | = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second; |
| 394 | return Result; |
| 395 | } |
| 396 | } |
| 397 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 398 | // Otherwise, didn't find it. Remember we didn't find this. |
| 399 | CacheLookup.second = SearchDirs.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | /// LookupSubframeworkHeader - Look up a subframework for the specified |
| 404 | /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from |
| 405 | /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox |
| 406 | /// is a subframework within Carbon.framework. If so, return the FileEntry |
| 407 | /// for the designated file, otherwise return null. |
| 408 | const FileEntry *HeaderSearch:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 409 | LookupSubframeworkHeader(StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 410 | const FileEntry *ContextFileEnt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 411 | SmallVectorImpl<char> *SearchPath, |
| 412 | SmallVectorImpl<char> *RelativePath) { |
Chris Lattner | 9415a0c | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 413 | assert(ContextFileEnt && "No context file?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 414 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 415 | // Framework names must have a '/' in the filename. Find it. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 416 | size_t SlashPos = Filename.find('/'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 417 | if (SlashPos == StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 419 | // Look up the base framework name of the ContextFileEnt. |
| 420 | const char *ContextName = ContextFileEnt->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | // If the context info wasn't a framework, couldn't be a subframework. |
| 423 | const char *FrameworkPos = strstr(ContextName, ".framework/"); |
| 424 | if (FrameworkPos == 0) |
| 425 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
| 427 | llvm::SmallString<1024> FrameworkName(ContextName, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 428 | FrameworkPos+strlen(".framework/")); |
| 429 | |
| 430 | // Append Frameworks/HIToolbox.framework/ |
| 431 | FrameworkName += "Frameworks/"; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 432 | FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 433 | FrameworkName += ".framework/"; |
| 434 | |
| 435 | llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup = |
Chris Lattner | 6538227 | 2010-11-21 09:55:08 +0000 | [diff] [blame] | 436 | FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 438 | // Some other location? |
| 439 | if (CacheLookup.getValue() && |
| 440 | CacheLookup.getKeyLength() == FrameworkName.size() && |
| 441 | memcmp(CacheLookup.getKeyData(), &FrameworkName[0], |
| 442 | CacheLookup.getKeyLength()) != 0) |
| 443 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 445 | // Cache subframework. |
| 446 | if (CacheLookup.getValue() == 0) { |
| 447 | ++NumSubFrameworkLookups; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 449 | // If the framework dir doesn't exist, we fail. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 450 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 451 | if (Dir == 0) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 453 | // Otherwise, if it does, remember that this is the right direntry for this |
| 454 | // framework. |
| 455 | CacheLookup.setValue(Dir); |
| 456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 458 | const FileEntry *FE = 0; |
| 459 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 460 | if (RelativePath != NULL) { |
| 461 | RelativePath->clear(); |
| 462 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 463 | } |
| 464 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 465 | // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" |
| 466 | llvm::SmallString<1024> HeadersFilename(FrameworkName); |
| 467 | HeadersFilename += "Headers/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 468 | if (SearchPath != NULL) { |
| 469 | SearchPath->clear(); |
| 470 | // Without trailing '/'. |
| 471 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 472 | } |
| 473 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 474 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 475 | if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 477 | // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" |
| 478 | HeadersFilename = FrameworkName; |
| 479 | HeadersFilename += "PrivateHeaders/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 480 | if (SearchPath != NULL) { |
| 481 | SearchPath->clear(); |
| 482 | // Without trailing '/'. |
| 483 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 484 | } |
| 485 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 486 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 487 | if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 488 | return 0; |
| 489 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 491 | // This file is a system header or C++ unfriendly if the old file is. |
Ted Kremenek | ca63fa0 | 2008-02-24 03:55:14 +0000 | [diff] [blame] | 492 | // |
Chris Lattner | c9dde4f | 2008-02-25 21:38:21 +0000 | [diff] [blame] | 493 | // Note that the temporary 'DirInfo' is required here, as either call to |
| 494 | // getFileInfo could resize the vector and we don't want to rely on order |
| 495 | // of evaluation. |
| 496 | unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo; |
| 497 | getFileInfo(FE).DirInfo = DirInfo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 498 | return FE; |
| 499 | } |
| 500 | |
| 501 | //===----------------------------------------------------------------------===// |
| 502 | // File Info Management. |
| 503 | //===----------------------------------------------------------------------===// |
| 504 | |
| 505 | |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 506 | /// getFileInfo - Return the HeaderFileInfo structure for the specified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 507 | /// FileEntry. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 508 | HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 509 | if (FE->getUID() >= FileInfo.size()) |
| 510 | FileInfo.resize(FE->getUID()+1); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 511 | |
| 512 | HeaderFileInfo &HFI = FileInfo[FE->getUID()]; |
| 513 | if (ExternalSource && !HFI.Resolved) { |
| 514 | HFI = ExternalSource->GetHeaderFileInfo(FE); |
| 515 | HFI.Resolved = true; |
| 516 | } |
| 517 | return HFI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 520 | bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) { |
| 521 | // Check if we've ever seen this file as a header. |
| 522 | if (File->getUID() >= FileInfo.size()) |
| 523 | return false; |
| 524 | |
| 525 | // Resolve header file info from the external source, if needed. |
| 526 | HeaderFileInfo &HFI = FileInfo[File->getUID()]; |
| 527 | if (ExternalSource && !HFI.Resolved) { |
| 528 | HFI = ExternalSource->GetHeaderFileInfo(File); |
| 529 | HFI.Resolved = true; |
| 530 | } |
| 531 | |
| 532 | return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID; |
| 533 | } |
| 534 | |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 535 | void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) { |
| 536 | if (UID >= FileInfo.size()) |
| 537 | FileInfo.resize(UID+1); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 538 | HFI.Resolved = true; |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 539 | FileInfo[UID] = HFI; |
| 540 | } |
| 541 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 542 | /// ShouldEnterIncludeFile - Mark the specified file as a target of of a |
| 543 | /// #include, #include_next, or #import directive. Return false if #including |
| 544 | /// the file will have no effect or true if we should include it. |
| 545 | bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ |
| 546 | ++NumIncluded; // Count # of attempted #includes. |
| 547 | |
| 548 | // Get information about this file. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 549 | HeaderFileInfo &FileInfo = getFileInfo(File); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 551 | // If this is a #import directive, check that we have not already imported |
| 552 | // this header. |
| 553 | if (isImport) { |
| 554 | // If this has already been imported, don't import it again. |
| 555 | FileInfo.isImport = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | // Has this already been #import'ed or #include'd? |
| 558 | if (FileInfo.NumIncludes) return false; |
| 559 | } else { |
| 560 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 561 | // or if this is the second #include of a #pragma once file, ignore it. |
| 562 | if (FileInfo.isImport) |
| 563 | return false; |
| 564 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 566 | // Next, check to see if the file is wrapped with #ifndef guards. If so, and |
| 567 | // if the macro that guards it is defined, we know the #include has no effect. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | if (const IdentifierInfo *ControllingMacro |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 569 | = FileInfo.getControllingMacro(ExternalLookup)) |
| 570 | if (ControllingMacro->hasMacroDefinition()) { |
| 571 | ++NumMultiIncludeFileOptzn; |
| 572 | return false; |
| 573 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 575 | // Increment the number of times this file has been included. |
| 576 | ++FileInfo.NumIncludes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 578 | return true; |
| 579 | } |
| 580 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 581 | size_t HeaderSearch::getTotalMemory() const { |
| 582 | return SearchDirs.capacity() |
Ted Kremenek | eabea45 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 583 | + llvm::capacity_in_bytes(FileInfo) |
| 584 | + llvm::capacity_in_bytes(HeaderMaps) |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 585 | + LookupFileCache.getAllocator().getTotalMemory() |
| 586 | + FrameworkMap.getAllocator().getTotalMemory(); |
| 587 | } |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 588 | |
| 589 | StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) { |
| 590 | return FrameworkNames.GetOrCreateValue(Framework).getKey(); |
| 591 | } |