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