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