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