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" |
Douglas Gregor | c042edd | 2012-10-24 16:19:39 +0000 | [diff] [blame] | 15 | #include "clang/Lex/HeaderSearchOptions.h" |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderMap.h" |
Chandler Carruth | cb381ea | 2011-12-09 01:33:57 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Lexer.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/IdentifierTable.h" |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Path.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | eabea45 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Capacity.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 25 | #include <cstdio> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 28 | const IdentifierInfo * |
| 29 | HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) { |
| 30 | if (ControllingMacro) |
| 31 | return ControllingMacro; |
| 32 | |
| 33 | if (!ControllingMacroID || !External) |
| 34 | return 0; |
| 35 | |
| 36 | ControllingMacro = External->GetIdentifier(ControllingMacroID); |
| 37 | return ControllingMacro; |
| 38 | } |
| 39 | |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 40 | ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {} |
| 41 | |
Douglas Gregor | c042edd | 2012-10-24 16:19:39 +0000 | [diff] [blame] | 42 | HeaderSearch::HeaderSearch(llvm::IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts, |
| 43 | FileManager &FM, DiagnosticsEngine &Diags, |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 44 | const LangOptions &LangOpts, |
| 45 | const TargetInfo *Target) |
Douglas Gregor | c042edd | 2012-10-24 16:19:39 +0000 | [diff] [blame] | 46 | : HSOpts(HSOpts), FileMgr(FM), FrameworkMap(64), |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 47 | ModMap(FileMgr, *Diags.getClient(), LangOpts, Target) |
Douglas Gregor | 8e23806 | 2011-11-11 00:35:06 +0000 | [diff] [blame] | 48 | { |
Nico Weber | 74a5fd8 | 2011-05-24 04:31:14 +0000 | [diff] [blame] | 49 | AngledDirIdx = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | SystemDirIdx = 0; |
| 51 | NoCurDirSearch = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 53 | ExternalLookup = 0; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 54 | ExternalSource = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | NumIncluded = 0; |
| 56 | NumMultiIncludeFileOptzn = 0; |
| 57 | NumFrameworkLookups = NumSubFrameworkLookups = 0; |
| 58 | } |
| 59 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 60 | HeaderSearch::~HeaderSearch() { |
| 61 | // Delete headermaps. |
| 62 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
| 63 | delete HeaderMaps[i].second; |
| 64 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | void HeaderSearch::PrintStats() { |
| 67 | fprintf(stderr, "\n*** HeaderSearch Stats:\n"); |
| 68 | fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size()); |
| 69 | unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; |
| 70 | for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { |
| 71 | NumOnceOnlyFiles += FileInfo[i].isImport; |
| 72 | if (MaxNumIncludes < FileInfo[i].NumIncludes) |
| 73 | MaxNumIncludes = FileInfo[i].NumIncludes; |
| 74 | NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; |
| 75 | } |
| 76 | fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles); |
| 77 | fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles); |
| 78 | fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded); |
| 81 | fprintf(stderr, " %d #includes skipped due to" |
| 82 | " the multi-include optimization.\n", NumMultiIncludeFileOptzn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 84 | fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups); |
| 85 | fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups); |
| 86 | } |
| 87 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 88 | /// CreateHeaderMap - This method returns a HeaderMap for the specified |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 89 | /// FileEntry, uniquing them through the 'HeaderMaps' datastructure. |
Chris Lattner | 1bfd4a6 | 2007-12-17 18:34:53 +0000 | [diff] [blame] | 90 | const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 91 | // 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] | 92 | // 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] | 93 | if (!HeaderMaps.empty()) { |
| 94 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 95 | // Pointer equality comparison of FileEntries works because they are |
| 96 | // already uniqued by inode. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | if (HeaderMaps[i].first == FE) |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 98 | return HeaderMaps[i].second; |
| 99 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 101 | if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 102 | HeaderMaps.push_back(std::make_pair(FE, HM)); |
| 103 | return HM; |
| 104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 109 | std::string HeaderSearch::getModuleFileName(Module *Module) { |
Douglas Gregor | 9a6da69 | 2011-09-12 20:41:59 +0000 | [diff] [blame] | 110 | // If we don't have a module cache path, we can't do anything. |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 111 | if (ModuleCachePath.empty()) |
| 112 | return std::string(); |
| 113 | |
| 114 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 115 | SmallString<256> Result(ModuleCachePath); |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 116 | llvm::sys::path::append(Result, Module->getTopLevelModule()->Name + ".pcm"); |
| 117 | return Result.str().str(); |
| 118 | } |
| 119 | |
| 120 | std::string HeaderSearch::getModuleFileName(StringRef ModuleName) { |
| 121 | // If we don't have a module cache path, we can't do anything. |
| 122 | if (ModuleCachePath.empty()) |
| 123 | return std::string(); |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 124 | |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 125 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 126 | SmallString<256> Result(ModuleCachePath); |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 127 | llvm::sys::path::append(Result, ModuleName + ".pcm"); |
| 128 | return Result.str().str(); |
| 129 | } |
| 130 | |
| 131 | Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) { |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 132 | // Look in the module map to determine if there is a module by this name. |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 133 | Module *Module = ModMap.findModule(ModuleName); |
| 134 | if (Module || !AllowSearch) |
| 135 | return Module; |
| 136 | |
| 137 | // Look through the various header search paths to load any avai;able module |
| 138 | // maps, searching for a module map that describes this module. |
| 139 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
| 140 | if (SearchDirs[Idx].isFramework()) { |
| 141 | // Search for or infer a module map for a framework. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 142 | SmallString<128> FrameworkDirName; |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 143 | FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName(); |
| 144 | llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework"); |
| 145 | if (const DirectoryEntry *FrameworkDir |
| 146 | = FileMgr.getDirectory(FrameworkDirName)) { |
| 147 | bool IsSystem |
| 148 | = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User; |
| 149 | Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem); |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 150 | if (Module) |
| 151 | break; |
| 152 | } |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // FIXME: Figure out how header maps and module maps will work together. |
| 156 | |
| 157 | // Only deal with normal search directories. |
| 158 | if (!SearchDirs[Idx].isNormalDir()) |
| 159 | continue; |
| 160 | |
| 161 | // Search for a module map file in this directory. |
| 162 | if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) { |
| 163 | // We just loaded a module map file; check whether the module is |
| 164 | // available now. |
| 165 | Module = ModMap.findModule(ModuleName); |
| 166 | if (Module) |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | // Search for a module map in a subdirectory with the same name as the |
| 171 | // module. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 172 | SmallString<128> NestedModuleMapDirName; |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 173 | NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName(); |
| 174 | llvm::sys::path::append(NestedModuleMapDirName, ModuleName); |
| 175 | if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) { |
| 176 | // If we just loaded a module map file, look for the module again. |
| 177 | Module = ModMap.findModule(ModuleName); |
| 178 | if (Module) |
| 179 | break; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 182 | |
| 183 | return Module; |
Douglas Gregor | 9a6da69 | 2011-09-12 20:41:59 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 186 | //===----------------------------------------------------------------------===// |
| 187 | // File lookup within a DirectoryLookup scope |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | |
Chris Lattner | 3af66a9 | 2007-12-17 17:57:27 +0000 | [diff] [blame] | 190 | /// getName - Return the directory or filename corresponding to this lookup |
| 191 | /// object. |
| 192 | const char *DirectoryLookup::getName() const { |
| 193 | if (isNormalDir()) |
| 194 | return getDir()->getName(); |
| 195 | if (isFramework()) |
| 196 | return getFrameworkDir()->getName(); |
| 197 | assert(isHeaderMap() && "Unknown DirectoryLookup"); |
| 198 | return getHeaderMap()->getFileName(); |
| 199 | } |
| 200 | |
| 201 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 202 | /// LookupFile - Lookup the specified file in this search path, returning it |
| 203 | /// if it exists or returning null if not. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 204 | const FileEntry *DirectoryLookup::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 205 | StringRef Filename, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 206 | HeaderSearch &HS, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 207 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 208 | SmallVectorImpl<char> *RelativePath, |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 209 | Module **SuggestedModule, |
| 210 | bool &InUserSpecifiedSystemFramework) const { |
| 211 | InUserSpecifiedSystemFramework = false; |
| 212 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 213 | SmallString<1024> TmpDir; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 214 | if (isNormalDir()) { |
| 215 | // Concatenate the requested file onto the directory. |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 216 | TmpDir = getDir()->getName(); |
| 217 | llvm::sys::path::append(TmpDir, Filename); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 218 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 219 | StringRef SearchPathRef(getDir()->getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 220 | SearchPath->clear(); |
| 221 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 222 | } |
| 223 | if (RelativePath != NULL) { |
| 224 | RelativePath->clear(); |
| 225 | RelativePath->append(Filename.begin(), Filename.end()); |
| 226 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 227 | |
| 228 | // If we have a module map that might map this header, load it and |
| 229 | // check whether we'll have a suggestion for a module. |
| 230 | if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) { |
| 231 | const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(), |
| 232 | /*openFile=*/false); |
| 233 | if (!File) |
| 234 | return File; |
| 235 | |
| 236 | // If there is a module that corresponds to this header, |
| 237 | // suggest it. |
Douglas Gregor | 5e3f922 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 238 | *SuggestedModule = HS.findModuleForHeader(File); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 239 | return File; |
| 240 | } |
| 241 | |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 242 | return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 243 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 245 | if (isFramework()) |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 246 | return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath, |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 247 | SuggestedModule, InUserSpecifiedSystemFramework); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Chris Lattner | b09e71f | 2007-12-17 08:17:39 +0000 | [diff] [blame] | 249 | assert(isHeaderMap() && "Unknown directory lookup"); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 250 | const FileEntry * const Result = getHeaderMap()->LookupFile( |
| 251 | Filename, HS.getFileMgr()); |
| 252 | if (Result) { |
| 253 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 254 | StringRef SearchPathRef(getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 255 | SearchPath->clear(); |
| 256 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 257 | } |
| 258 | if (RelativePath != NULL) { |
| 259 | RelativePath->clear(); |
| 260 | RelativePath->append(Filename.begin(), Filename.end()); |
| 261 | } |
| 262 | } |
| 263 | return Result; |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 267 | /// DoFrameworkLookup - Do a lookup of the specified file in the current |
| 268 | /// DirectoryLookup, which is a framework directory. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 269 | const FileEntry *DirectoryLookup::DoFrameworkLookup( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 270 | StringRef Filename, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 271 | HeaderSearch &HS, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 272 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 273 | SmallVectorImpl<char> *RelativePath, |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 274 | Module **SuggestedModule, |
| 275 | bool &InUserSpecifiedSystemFramework) const |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 276 | { |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 277 | FileManager &FileMgr = HS.getFileMgr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | // Framework names must have a '/' in the filename. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 280 | size_t SlashPos = Filename.find('/'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 281 | if (SlashPos == StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 283 | // Find out if this is the home for the specified framework, by checking |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 284 | // HeaderSearch. Possible answers are yes/no and unknown. |
| 285 | HeaderSearch::FrameworkCacheEntry &CacheEntry = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 286 | HS.LookupFrameworkCache(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 288 | // If it is known and in some other directory, fail. |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 289 | if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 292 | // Otherwise, construct the path to this framework dir. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | // FrameworkName = "/System/Library/Frameworks/" |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 295 | SmallString<1024> FrameworkName; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 296 | FrameworkName += getFrameworkDir()->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | if (FrameworkName.empty() || FrameworkName.back() != '/') |
| 298 | FrameworkName.push_back('/'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | // FrameworkName = "/System/Library/Frameworks/Cocoa" |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 301 | StringRef ModuleName(Filename.begin(), SlashPos); |
| 302 | FrameworkName += ModuleName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" |
| 305 | FrameworkName += ".framework/"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 307 | // If the cache entry was unresolved, populate it now. |
| 308 | if (CacheEntry.Directory == 0) { |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 309 | HS.IncrementFrameworkLookupCount(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | // If the framework dir doesn't exist, we fail. |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 312 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str()); |
| 313 | if (Dir == 0) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | // Otherwise, if it does, remember that this is the right direntry for this |
| 316 | // framework. |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 317 | CacheEntry.Directory = getFrameworkDir(); |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 318 | |
| 319 | // If this is a user search directory, check if the framework has been |
| 320 | // user-specified as a system framework. |
| 321 | if (getDirCharacteristic() == SrcMgr::C_User) { |
| 322 | SmallString<1024> SystemFrameworkMarker(FrameworkName); |
| 323 | SystemFrameworkMarker += ".system_framework"; |
| 324 | if (llvm::sys::fs::exists(SystemFrameworkMarker.str())) { |
| 325 | CacheEntry.IsUserSpecifiedSystemFramework = true; |
| 326 | } |
| 327 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 330 | // Set the 'user-specified system framework' flag. |
| 331 | InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework; |
| 332 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 333 | if (RelativePath != NULL) { |
| 334 | RelativePath->clear(); |
| 335 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 336 | } |
| 337 | |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 338 | // If we're allowed to look for modules, try to load or create the module |
| 339 | // corresponding to this framework. |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 340 | Module *Module = 0; |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 341 | if (SuggestedModule) { |
| 342 | if (const DirectoryEntry *FrameworkDir |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 343 | = FileMgr.getDirectory(FrameworkName)) { |
| 344 | bool IsSystem = getDirCharacteristic() != SrcMgr::C_User; |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 345 | Module = HS.loadFrameworkModule(ModuleName, FrameworkDir, IsSystem); |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 346 | } |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" |
| 350 | unsigned OrigSize = FrameworkName.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | FrameworkName += "Headers/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 353 | |
| 354 | if (SearchPath != NULL) { |
| 355 | SearchPath->clear(); |
| 356 | // Without trailing '/'. |
| 357 | SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1); |
| 358 | } |
| 359 | |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 360 | // Determine whether this is the module we're building or not. |
Douglas Gregor | 0983392 | 2011-12-06 17:31:28 +0000 | [diff] [blame] | 361 | bool AutomaticImport = Module; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 362 | FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 363 | if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(), |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 364 | /*openFile=*/!AutomaticImport)) { |
| 365 | if (AutomaticImport) |
Douglas Gregor | 0983392 | 2011-12-06 17:31:28 +0000 | [diff] [blame] | 366 | *SuggestedModule = HS.findModuleForHeader(FE); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | return FE; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 368 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 370 | // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" |
| 371 | const char *Private = "Private"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | FrameworkName.insert(FrameworkName.begin()+OrigSize, Private, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 373 | Private+strlen(Private)); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 374 | if (SearchPath != NULL) |
| 375 | SearchPath->insert(SearchPath->begin()+OrigSize, Private, |
| 376 | Private+strlen(Private)); |
| 377 | |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 378 | const FileEntry *FE = FileMgr.getFile(FrameworkName.str(), |
| 379 | /*openFile=*/!AutomaticImport); |
| 380 | if (FE && AutomaticImport) |
Douglas Gregor | 0983392 | 2011-12-06 17:31:28 +0000 | [diff] [blame] | 381 | *SuggestedModule = HS.findModuleForHeader(FE); |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 382 | return FE; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 385 | void HeaderSearch::setTarget(const TargetInfo &Target) { |
| 386 | ModMap.setTarget(Target); |
| 387 | } |
| 388 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 389 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 390 | //===----------------------------------------------------------------------===// |
| 391 | // Header File Location. |
| 392 | //===----------------------------------------------------------------------===// |
| 393 | |
| 394 | |
James Dennett | 853519c | 2012-06-20 00:56:32 +0000 | [diff] [blame] | 395 | /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 396 | /// return null on failure. isAngled indicates whether the file reference is |
James Dennett | 853519c | 2012-06-20 00:56:32 +0000 | [diff] [blame] | 397 | /// for system \#include's or not (i.e. using <> instead of ""). CurFileEnt, if |
| 398 | /// non-null, indicates where the \#including file is, in case a relative search |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 399 | /// is needed. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 400 | const FileEntry *HeaderSearch::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 401 | StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 402 | bool isAngled, |
| 403 | const DirectoryLookup *FromDir, |
| 404 | const DirectoryLookup *&CurDir, |
| 405 | const FileEntry *CurFileEnt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 406 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 407 | SmallVectorImpl<char> *RelativePath, |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 408 | Module **SuggestedModule, |
Douglas Gregor | 1c2e933 | 2011-11-20 17:46:46 +0000 | [diff] [blame] | 409 | bool SkipCache) |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 410 | { |
| 411 | if (SuggestedModule) |
Douglas Gregor | c69c42e | 2011-11-17 22:44:56 +0000 | [diff] [blame] | 412 | *SuggestedModule = 0; |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 413 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 414 | // 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] | 415 | if (llvm::sys::path::is_absolute(Filename)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | CurDir = 0; |
| 417 | |
| 418 | // If this was an #include_next "/absolute/file", fail. |
| 419 | if (FromDir) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 421 | if (SearchPath != NULL) |
| 422 | SearchPath->clear(); |
| 423 | if (RelativePath != NULL) { |
| 424 | RelativePath->clear(); |
| 425 | RelativePath->append(Filename.begin(), Filename.end()); |
| 426 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | // Otherwise, just return the file. |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 428 | return FileMgr.getFile(Filename, /*openFile=*/true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 429 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 431 | // 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] | 432 | // directory. This has to be based on CurFileEnt, not CurDir, because |
| 433 | // 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] | 434 | // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h". |
| 435 | // This search is not done for <> headers. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 436 | if (CurFileEnt && !isAngled && !NoCurDirSearch) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 437 | SmallString<1024> TmpDir; |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 438 | // Concatenate the requested file onto the directory. |
| 439 | // FIXME: Portability. Filename concatenation should be in sys::Path. |
| 440 | TmpDir += CurFileEnt->getDir()->getName(); |
| 441 | TmpDir.push_back('/'); |
| 442 | TmpDir.append(Filename.begin(), Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 443 | if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 444 | // Leave CurDir unset. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 445 | // This file is a system header or C++ unfriendly if the old file is. |
| 446 | // |
Douglas Gregor | 21efbb6 | 2012-08-13 15:47:39 +0000 | [diff] [blame] | 447 | // Note that we only use one of FromHFI/ToHFI at once, due to potential |
| 448 | // reallocation of the underlying vector potentially making the first |
| 449 | // reference binding dangling. |
| 450 | HeaderFileInfo &FromHFI = getFileInfo(CurFileEnt); |
| 451 | unsigned DirInfo = FromHFI.DirInfo; |
| 452 | bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader; |
| 453 | StringRef Framework = FromHFI.Framework; |
| 454 | |
| 455 | HeaderFileInfo &ToHFI = getFileInfo(FE); |
| 456 | ToHFI.DirInfo = DirInfo; |
| 457 | ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader; |
| 458 | ToHFI.Framework = Framework; |
| 459 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 460 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 461 | StringRef SearchPathRef(CurFileEnt->getDir()->getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 462 | SearchPath->clear(); |
| 463 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 464 | } |
| 465 | if (RelativePath != NULL) { |
| 466 | RelativePath->clear(); |
| 467 | RelativePath->append(Filename.begin(), Filename.end()); |
| 468 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 469 | return FE; |
| 470 | } |
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 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | CurDir = 0; |
| 474 | |
| 475 | // If this is a system #include, ignore the user #include locs. |
Nico Weber | 74a5fd8 | 2011-05-24 04:31:14 +0000 | [diff] [blame] | 476 | unsigned i = isAngled ? AngledDirIdx : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | // If this is a #include_next request, start searching after the directory the |
| 479 | // file was found in. |
| 480 | if (FromDir) |
| 481 | i = FromDir-&SearchDirs[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 483 | // Cache all of the lookups performed by this method. Many headers are |
| 484 | // multiply included, and the "pragma once" optimization prevents them from |
| 485 | // being relex/pp'd, but they would still have to search through a |
| 486 | // (potentially huge) series of SearchDirs to find it. |
| 487 | std::pair<unsigned, unsigned> &CacheLookup = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 488 | LookupFileCache.GetOrCreateValue(Filename).getValue(); |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 489 | |
| 490 | // If the entry has been previously looked up, the first value will be |
| 491 | // non-zero. If the value is equal to i (the start point of our search), then |
| 492 | // this is a matching hit. |
Douglas Gregor | 1c2e933 | 2011-11-20 17:46:46 +0000 | [diff] [blame] | 493 | if (!SkipCache && CacheLookup.first == i+1) { |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 494 | // Skip querying potentially lots of directories for this lookup. |
| 495 | i = CacheLookup.second; |
| 496 | } else { |
| 497 | // Otherwise, this is the first query, or the previous query didn't match |
| 498 | // our search start. We will fill in our found location below, so prime the |
| 499 | // start point value. |
| 500 | CacheLookup.first = i+1; |
| 501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 503 | // Check each directory in sequence to see if it contains this file. |
| 504 | for (; i != SearchDirs.size(); ++i) { |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 505 | bool InUserSpecifiedSystemFramework = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | const FileEntry *FE = |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 507 | SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath, |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 508 | SuggestedModule, InUserSpecifiedSystemFramework); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 509 | if (!FE) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 511 | CurDir = &SearchDirs[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 513 | // 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] | 514 | HeaderFileInfo &HFI = getFileInfo(FE); |
| 515 | HFI.DirInfo = CurDir->getDirCharacteristic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 517 | // If the directory characteristic is User but this framework was |
| 518 | // user-specified to be treated as a system framework, promote the |
| 519 | // characteristic. |
| 520 | if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework) |
| 521 | HFI.DirInfo = SrcMgr::C_System; |
| 522 | |
Richard Smith | f122a13 | 2012-06-13 20:27:03 +0000 | [diff] [blame] | 523 | // If the filename matches a known system header prefix, override |
| 524 | // whether the file is a system header. |
Richard Trieu | 4ef2f6a | 2012-06-13 20:52:36 +0000 | [diff] [blame] | 525 | for (unsigned j = SystemHeaderPrefixes.size(); j; --j) { |
| 526 | if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) { |
| 527 | HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System |
Richard Smith | f122a13 | 2012-06-13 20:27:03 +0000 | [diff] [blame] | 528 | : SrcMgr::C_User; |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 533 | // If this file is found in a header map and uses the framework style of |
| 534 | // includes, then this header is part of a framework we're building. |
| 535 | if (CurDir->isIndexHeaderMap()) { |
| 536 | size_t SlashPos = Filename.find('/'); |
| 537 | if (SlashPos != StringRef::npos) { |
| 538 | HFI.IndexHeaderMapHeader = 1; |
| 539 | HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(), |
| 540 | SlashPos)); |
| 541 | } |
| 542 | } |
| 543 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 544 | // Remember this location for the next lookup we do. |
| 545 | CacheLookup.second = i; |
| 546 | return FE; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 547 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | |
Douglas Gregor | 2c7b780 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 549 | // If we are including a file with a quoted include "foo.h" from inside |
| 550 | // a header in a framework that is currently being built, and we couldn't |
| 551 | // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where |
| 552 | // "Foo" is the name of the framework in which the including header was found. |
| 553 | if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) { |
| 554 | HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt); |
| 555 | if (IncludingHFI.IndexHeaderMapHeader) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 556 | SmallString<128> ScratchFilename; |
Douglas Gregor | 2c7b780 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 557 | ScratchFilename += IncludingHFI.Framework; |
| 558 | ScratchFilename += '/'; |
| 559 | ScratchFilename += Filename; |
| 560 | |
| 561 | const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true, |
| 562 | FromDir, CurDir, CurFileEnt, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 563 | SearchPath, RelativePath, |
| 564 | SuggestedModule); |
Douglas Gregor | 2c7b780 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 565 | std::pair<unsigned, unsigned> &CacheLookup |
| 566 | = LookupFileCache.GetOrCreateValue(Filename).getValue(); |
| 567 | CacheLookup.second |
| 568 | = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second; |
| 569 | return Result; |
| 570 | } |
| 571 | } |
| 572 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 573 | // Otherwise, didn't find it. Remember we didn't find this. |
| 574 | CacheLookup.second = SearchDirs.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /// LookupSubframeworkHeader - Look up a subframework for the specified |
James Dennett | 853519c | 2012-06-20 00:56:32 +0000 | [diff] [blame] | 579 | /// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 580 | /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox |
| 581 | /// is a subframework within Carbon.framework. If so, return the FileEntry |
| 582 | /// for the designated file, otherwise return null. |
| 583 | const FileEntry *HeaderSearch:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 584 | LookupSubframeworkHeader(StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 585 | const FileEntry *ContextFileEnt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 586 | SmallVectorImpl<char> *SearchPath, |
| 587 | SmallVectorImpl<char> *RelativePath) { |
Chris Lattner | 9415a0c | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 588 | assert(ContextFileEnt && "No context file?"); |
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 | // Framework names must have a '/' in the filename. Find it. |
Douglas Gregor | efda0e8 | 2011-12-09 16:48:01 +0000 | [diff] [blame] | 591 | // FIXME: Should we permit '\' on Windows? |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 592 | size_t SlashPos = Filename.find('/'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 593 | if (SlashPos == StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 595 | // Look up the base framework name of the ContextFileEnt. |
| 596 | const char *ContextName = ContextFileEnt->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 598 | // If the context info wasn't a framework, couldn't be a subframework. |
Douglas Gregor | efda0e8 | 2011-12-09 16:48:01 +0000 | [diff] [blame] | 599 | const unsigned DotFrameworkLen = 10; |
| 600 | const char *FrameworkPos = strstr(ContextName, ".framework"); |
| 601 | if (FrameworkPos == 0 || |
| 602 | (FrameworkPos[DotFrameworkLen] != '/' && |
| 603 | FrameworkPos[DotFrameworkLen] != '\\')) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 604 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 606 | SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 607 | |
| 608 | // Append Frameworks/HIToolbox.framework/ |
| 609 | FrameworkName += "Frameworks/"; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 610 | FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 611 | FrameworkName += ".framework/"; |
| 612 | |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 613 | llvm::StringMapEntry<FrameworkCacheEntry> &CacheLookup = |
Chris Lattner | 6538227 | 2010-11-21 09:55:08 +0000 | [diff] [blame] | 614 | FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | // Some other location? |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 617 | if (CacheLookup.getValue().Directory && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 618 | CacheLookup.getKeyLength() == FrameworkName.size() && |
| 619 | memcmp(CacheLookup.getKeyData(), &FrameworkName[0], |
| 620 | CacheLookup.getKeyLength()) != 0) |
| 621 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 623 | // Cache subframework. |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 624 | if (CacheLookup.getValue().Directory == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 625 | ++NumSubFrameworkLookups; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 627 | // If the framework dir doesn't exist, we fail. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 628 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 629 | if (Dir == 0) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 630 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 631 | // Otherwise, if it does, remember that this is the right direntry for this |
| 632 | // framework. |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 633 | CacheLookup.getValue().Directory = Dir; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 634 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 636 | const FileEntry *FE = 0; |
| 637 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 638 | if (RelativePath != NULL) { |
| 639 | RelativePath->clear(); |
| 640 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 641 | } |
| 642 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 643 | // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 644 | SmallString<1024> HeadersFilename(FrameworkName); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 645 | HeadersFilename += "Headers/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 646 | if (SearchPath != NULL) { |
| 647 | SearchPath->clear(); |
| 648 | // Without trailing '/'. |
| 649 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 650 | } |
| 651 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 652 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 653 | if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) { |
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 | // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" |
| 656 | HeadersFilename = FrameworkName; |
| 657 | HeadersFilename += "PrivateHeaders/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 658 | if (SearchPath != NULL) { |
| 659 | SearchPath->clear(); |
| 660 | // Without trailing '/'. |
| 661 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 662 | } |
| 663 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 664 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 665 | if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 666 | return 0; |
| 667 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 669 | // 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] | 670 | // |
Chris Lattner | c9dde4f | 2008-02-25 21:38:21 +0000 | [diff] [blame] | 671 | // Note that the temporary 'DirInfo' is required here, as either call to |
| 672 | // getFileInfo could resize the vector and we don't want to rely on order |
| 673 | // of evaluation. |
| 674 | unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo; |
| 675 | getFileInfo(FE).DirInfo = DirInfo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 676 | return FE; |
| 677 | } |
| 678 | |
Chandler Carruth | cb381ea | 2011-12-09 01:33:57 +0000 | [diff] [blame] | 679 | /// \brief Helper static function to normalize a path for injection into |
| 680 | /// a synthetic header. |
| 681 | /*static*/ std::string |
| 682 | HeaderSearch::NormalizeDashIncludePath(StringRef File, FileManager &FileMgr) { |
| 683 | // Implicit include paths should be resolved relative to the current |
| 684 | // working directory first, and then use the regular header search |
| 685 | // mechanism. The proper way to handle this is to have the |
| 686 | // predefines buffer located at the current working directory, but |
| 687 | // it has no file entry. For now, workaround this by using an |
| 688 | // absolute path if we find the file here, and otherwise letting |
| 689 | // header search handle it. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 690 | SmallString<128> Path(File); |
Chandler Carruth | cb381ea | 2011-12-09 01:33:57 +0000 | [diff] [blame] | 691 | llvm::sys::fs::make_absolute(Path); |
| 692 | bool exists; |
| 693 | if (llvm::sys::fs::exists(Path.str(), exists) || !exists) |
| 694 | Path = File; |
| 695 | else if (exists) |
| 696 | FileMgr.getFile(File); |
| 697 | |
| 698 | return Lexer::Stringify(Path.str()); |
| 699 | } |
| 700 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 701 | //===----------------------------------------------------------------------===// |
| 702 | // File Info Management. |
| 703 | //===----------------------------------------------------------------------===// |
| 704 | |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 705 | /// \brief Merge the header file info provided by \p OtherHFI into the current |
| 706 | /// header file info (\p HFI) |
| 707 | static void mergeHeaderFileInfo(HeaderFileInfo &HFI, |
| 708 | const HeaderFileInfo &OtherHFI) { |
| 709 | HFI.isImport |= OtherHFI.isImport; |
| 710 | HFI.isPragmaOnce |= OtherHFI.isPragmaOnce; |
| 711 | HFI.NumIncludes += OtherHFI.NumIncludes; |
| 712 | |
| 713 | if (!HFI.ControllingMacro && !HFI.ControllingMacroID) { |
| 714 | HFI.ControllingMacro = OtherHFI.ControllingMacro; |
| 715 | HFI.ControllingMacroID = OtherHFI.ControllingMacroID; |
| 716 | } |
| 717 | |
| 718 | if (OtherHFI.External) { |
| 719 | HFI.DirInfo = OtherHFI.DirInfo; |
| 720 | HFI.External = OtherHFI.External; |
| 721 | HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader; |
| 722 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 723 | |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 724 | if (HFI.Framework.empty()) |
| 725 | HFI.Framework = OtherHFI.Framework; |
| 726 | |
| 727 | HFI.Resolved = true; |
| 728 | } |
| 729 | |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 730 | /// getFileInfo - Return the HeaderFileInfo structure for the specified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 731 | /// FileEntry. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 732 | HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 733 | if (FE->getUID() >= FileInfo.size()) |
| 734 | FileInfo.resize(FE->getUID()+1); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 735 | |
| 736 | HeaderFileInfo &HFI = FileInfo[FE->getUID()]; |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 737 | if (ExternalSource && !HFI.Resolved) |
| 738 | mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE)); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 739 | return HFI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 742 | bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) { |
| 743 | // Check if we've ever seen this file as a header. |
| 744 | if (File->getUID() >= FileInfo.size()) |
| 745 | return false; |
| 746 | |
| 747 | // Resolve header file info from the external source, if needed. |
| 748 | HeaderFileInfo &HFI = FileInfo[File->getUID()]; |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 749 | if (ExternalSource && !HFI.Resolved) |
| 750 | mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File)); |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 751 | |
| 752 | return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID; |
| 753 | } |
| 754 | |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 755 | void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) { |
| 756 | if (UID >= FileInfo.size()) |
| 757 | FileInfo.resize(UID+1); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 758 | HFI.Resolved = true; |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 759 | FileInfo[UID] = HFI; |
| 760 | } |
| 761 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 762 | bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ |
| 763 | ++NumIncluded; // Count # of attempted #includes. |
| 764 | |
| 765 | // Get information about this file. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 766 | HeaderFileInfo &FileInfo = getFileInfo(File); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 768 | // If this is a #import directive, check that we have not already imported |
| 769 | // this header. |
| 770 | if (isImport) { |
| 771 | // If this has already been imported, don't import it again. |
| 772 | FileInfo.isImport = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 774 | // Has this already been #import'ed or #include'd? |
| 775 | if (FileInfo.NumIncludes) return false; |
| 776 | } else { |
| 777 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 778 | // or if this is the second #include of a #pragma once file, ignore it. |
| 779 | if (FileInfo.isImport) |
| 780 | return false; |
| 781 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 783 | // Next, check to see if the file is wrapped with #ifndef guards. If so, and |
| 784 | // 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] | 785 | if (const IdentifierInfo *ControllingMacro |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 786 | = FileInfo.getControllingMacro(ExternalLookup)) |
| 787 | if (ControllingMacro->hasMacroDefinition()) { |
| 788 | ++NumMultiIncludeFileOptzn; |
| 789 | return false; |
| 790 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 791 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 792 | // Increment the number of times this file has been included. |
| 793 | ++FileInfo.NumIncludes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | return true; |
| 796 | } |
| 797 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 798 | size_t HeaderSearch::getTotalMemory() const { |
| 799 | return SearchDirs.capacity() |
Ted Kremenek | eabea45 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 800 | + llvm::capacity_in_bytes(FileInfo) |
| 801 | + llvm::capacity_in_bytes(HeaderMaps) |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 802 | + LookupFileCache.getAllocator().getTotalMemory() |
| 803 | + FrameworkMap.getAllocator().getTotalMemory(); |
| 804 | } |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 805 | |
| 806 | StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) { |
| 807 | return FrameworkNames.GetOrCreateValue(Framework).getKey(); |
| 808 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 809 | |
| 810 | bool HeaderSearch::hasModuleMap(StringRef FileName, |
| 811 | const DirectoryEntry *Root) { |
| 812 | llvm::SmallVector<const DirectoryEntry *, 2> FixUpDirectories; |
| 813 | |
| 814 | StringRef DirName = FileName; |
| 815 | do { |
| 816 | // Get the parent directory name. |
| 817 | DirName = llvm::sys::path::parent_path(DirName); |
| 818 | if (DirName.empty()) |
| 819 | return false; |
| 820 | |
| 821 | // Determine whether this directory exists. |
| 822 | const DirectoryEntry *Dir = FileMgr.getDirectory(DirName); |
| 823 | if (!Dir) |
| 824 | return false; |
| 825 | |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 826 | // Try to load the module map file in this directory. |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 827 | switch (loadModuleMapFile(Dir)) { |
| 828 | case LMM_NewlyLoaded: |
| 829 | case LMM_AlreadyLoaded: |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 830 | // Success. All of the directories we stepped through inherit this module |
| 831 | // map file. |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 832 | for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I) |
| 833 | DirectoryHasModuleMap[FixUpDirectories[I]] = true; |
| 834 | |
| 835 | return true; |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 836 | |
| 837 | case LMM_NoDirectory: |
| 838 | case LMM_InvalidModuleMap: |
| 839 | break; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 840 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 841 | |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 842 | // If we hit the top of our search, we're done. |
| 843 | if (Dir == Root) |
| 844 | return false; |
| 845 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 846 | // Keep track of all of the directories we checked, so we can mark them as |
| 847 | // having module maps if we eventually do find a module map. |
| 848 | FixUpDirectories.push_back(Dir); |
| 849 | } while (true); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 852 | Module *HeaderSearch::findModuleForHeader(const FileEntry *File) { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 853 | if (Module *Mod = ModMap.findModuleForHeader(File)) |
| 854 | return Mod; |
Douglas Gregor | 65f3b5e | 2011-11-11 22:18:48 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | c69c42e | 2011-11-17 22:44:56 +0000 | [diff] [blame] | 856 | return 0; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 859 | bool HeaderSearch::loadModuleMapFile(const FileEntry *File) { |
| 860 | const DirectoryEntry *Dir = File->getDir(); |
| 861 | |
| 862 | llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir |
| 863 | = DirectoryHasModuleMap.find(Dir); |
| 864 | if (KnownDir != DirectoryHasModuleMap.end()) |
| 865 | return !KnownDir->second; |
| 866 | |
| 867 | bool Result = ModMap.parseModuleMapFile(File); |
Douglas Gregor | 4813442c | 2011-12-07 21:25:07 +0000 | [diff] [blame] | 868 | if (!Result && llvm::sys::path::filename(File->getName()) == "module.map") { |
| 869 | // If the file we loaded was a module.map, look for the corresponding |
| 870 | // module_private.map. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 871 | SmallString<128> PrivateFilename(Dir->getName()); |
Douglas Gregor | 4813442c | 2011-12-07 21:25:07 +0000 | [diff] [blame] | 872 | llvm::sys::path::append(PrivateFilename, "module_private.map"); |
| 873 | if (const FileEntry *PrivateFile = FileMgr.getFile(PrivateFilename)) |
| 874 | Result = ModMap.parseModuleMapFile(PrivateFile); |
| 875 | } |
| 876 | |
| 877 | DirectoryHasModuleMap[Dir] = !Result; |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 878 | return Result; |
| 879 | } |
| 880 | |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 881 | Module *HeaderSearch::loadFrameworkModule(StringRef Name, |
| 882 | const DirectoryEntry *Dir, |
| 883 | bool IsSystem) { |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 884 | if (Module *Module = ModMap.findModule(Name)) |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 885 | return Module; |
| 886 | |
| 887 | // Try to load a module map file. |
| 888 | switch (loadModuleMapFile(Dir)) { |
| 889 | case LMM_InvalidModuleMap: |
| 890 | break; |
| 891 | |
| 892 | case LMM_AlreadyLoaded: |
| 893 | case LMM_NoDirectory: |
| 894 | return 0; |
| 895 | |
| 896 | case LMM_NewlyLoaded: |
| 897 | return ModMap.findModule(Name); |
| 898 | } |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 899 | |
| 900 | // The top-level framework directory, from which we'll infer a framework |
| 901 | // module. |
| 902 | const DirectoryEntry *TopFrameworkDir = Dir; |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 903 | |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 904 | // The path from the module we're actually looking for back to the top-level |
| 905 | // framework name. |
| 906 | llvm::SmallVector<StringRef, 2> SubmodulePath; |
| 907 | SubmodulePath.push_back(Name); |
| 908 | |
| 909 | // Walk the directory structure to find any enclosing frameworks. |
Douglas Gregor | 156f0f0 | 2012-09-25 18:29:14 +0000 | [diff] [blame] | 910 | #ifdef LLVM_ON_UNIX |
| 911 | // Note: as an egregious but useful hack we use the real path here, because |
| 912 | // frameworks moving from top-level frameworks to embedded frameworks tend |
| 913 | // to be symlinked from the top-level location to the embedded location, |
| 914 | // and we need to resolve lookups as if we had found the embedded location. |
| 915 | char RealDirName[PATH_MAX]; |
| 916 | StringRef DirName; |
| 917 | if (realpath(Dir->getName(), RealDirName)) |
| 918 | DirName = RealDirName; |
| 919 | else |
| 920 | DirName = Dir->getName(); |
| 921 | #else |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 922 | StringRef DirName = Dir->getName(); |
Douglas Gregor | 156f0f0 | 2012-09-25 18:29:14 +0000 | [diff] [blame] | 923 | #endif |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 924 | do { |
| 925 | // Get the parent directory name. |
| 926 | DirName = llvm::sys::path::parent_path(DirName); |
| 927 | if (DirName.empty()) |
| 928 | break; |
| 929 | |
| 930 | // Determine whether this directory exists. |
| 931 | Dir = FileMgr.getDirectory(DirName); |
| 932 | if (!Dir) |
| 933 | break; |
| 934 | |
| 935 | // If this is a framework directory, then we're a subframework of this |
| 936 | // framework. |
| 937 | if (llvm::sys::path::extension(DirName) == ".framework") { |
| 938 | SubmodulePath.push_back(llvm::sys::path::stem(DirName)); |
| 939 | TopFrameworkDir = Dir; |
| 940 | } |
| 941 | } while (true); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 942 | |
| 943 | // Determine whether we're allowed to infer a module map. |
| 944 | bool canInfer = false; |
| 945 | if (llvm::sys::path::has_parent_path(TopFrameworkDir->getName())) { |
| 946 | // Figure out the parent path. |
| 947 | StringRef Parent = llvm::sys::path::parent_path(TopFrameworkDir->getName()); |
| 948 | if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { |
| 949 | // If there's a module map file in the parent directory, it can |
| 950 | // explicitly allow us to infer framework modules. |
| 951 | switch (loadModuleMapFile(ParentDir)) { |
| 952 | case LMM_AlreadyLoaded: |
| 953 | case LMM_NewlyLoaded: { |
| 954 | StringRef Name = llvm::sys::path::stem(TopFrameworkDir->getName()); |
| 955 | canInfer = ModMap.canInferFrameworkModule(ParentDir, Name, IsSystem); |
| 956 | break; |
| 957 | } |
| 958 | case LMM_InvalidModuleMap: |
| 959 | case LMM_NoDirectory: |
| 960 | break; |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | // If we're not allowed to infer a module map, we're done. |
| 966 | if (!canInfer) |
| 967 | return 0; |
| 968 | |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 969 | // Try to infer a module map from the top-level framework directory. |
| 970 | Module *Result = ModMap.inferFrameworkModule(SubmodulePath.back(), |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 971 | TopFrameworkDir, |
| 972 | IsSystem, |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 973 | /*Parent=*/0); |
| 974 | |
| 975 | // Follow the submodule path to find the requested (sub)framework module |
| 976 | // within the top-level framework module. |
| 977 | SubmodulePath.pop_back(); |
| 978 | while (!SubmodulePath.empty() && Result) { |
| 979 | Result = ModMap.lookupModuleQualified(SubmodulePath.back(), Result); |
| 980 | SubmodulePath.pop_back(); |
| 981 | } |
| 982 | return Result; |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 985 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 986 | HeaderSearch::LoadModuleMapResult |
| 987 | HeaderSearch::loadModuleMapFile(StringRef DirName) { |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 988 | if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName)) |
| 989 | return loadModuleMapFile(Dir); |
| 990 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 991 | return LMM_NoDirectory; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 994 | HeaderSearch::LoadModuleMapResult |
| 995 | HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) { |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 996 | llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir |
| 997 | = DirectoryHasModuleMap.find(Dir); |
| 998 | if (KnownDir != DirectoryHasModuleMap.end()) |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 999 | return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1000 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1001 | SmallString<128> ModuleMapFileName; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1002 | ModuleMapFileName += Dir->getName(); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1003 | unsigned ModuleMapDirNameLen = ModuleMapFileName.size(); |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1004 | llvm::sys::path::append(ModuleMapFileName, "module.map"); |
| 1005 | if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) { |
| 1006 | // We have found a module map file. Try to parse it. |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1007 | if (ModMap.parseModuleMapFile(ModuleMapFile)) { |
| 1008 | // No suitable module map. |
| 1009 | DirectoryHasModuleMap[Dir] = false; |
| 1010 | return LMM_InvalidModuleMap; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1011 | } |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1012 | |
| 1013 | // This directory has a module map. |
| 1014 | DirectoryHasModuleMap[Dir] = true; |
| 1015 | |
| 1016 | // Check whether there is a private module map that we need to load as well. |
| 1017 | ModuleMapFileName.erase(ModuleMapFileName.begin() + ModuleMapDirNameLen, |
| 1018 | ModuleMapFileName.end()); |
| 1019 | llvm::sys::path::append(ModuleMapFileName, "module_private.map"); |
| 1020 | if (const FileEntry *PrivateModuleMapFile |
| 1021 | = FileMgr.getFile(ModuleMapFileName)) { |
| 1022 | if (ModMap.parseModuleMapFile(PrivateModuleMapFile)) { |
| 1023 | // No suitable module map. |
| 1024 | DirectoryHasModuleMap[Dir] = false; |
| 1025 | return LMM_InvalidModuleMap; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | return LMM_NewlyLoaded; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | // No suitable module map. |
| 1033 | DirectoryHasModuleMap[Dir] = false; |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1034 | return LMM_InvalidModuleMap; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1035 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1036 | |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1037 | void HeaderSearch::collectAllModules(llvm::SmallVectorImpl<Module *> &Modules) { |
| 1038 | Modules.clear(); |
| 1039 | |
| 1040 | // Load module maps for each of the header search directories. |
| 1041 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
| 1042 | if (SearchDirs[Idx].isFramework()) { |
| 1043 | llvm::error_code EC; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1044 | SmallString<128> DirNative; |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1045 | llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(), |
| 1046 | DirNative); |
| 1047 | |
| 1048 | // Search each of the ".framework" directories to load them as modules. |
| 1049 | bool IsSystem = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User; |
| 1050 | for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd; |
| 1051 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 1052 | if (llvm::sys::path::extension(Dir->path()) != ".framework") |
| 1053 | continue; |
| 1054 | |
| 1055 | const DirectoryEntry *FrameworkDir = FileMgr.getDirectory(Dir->path()); |
| 1056 | if (!FrameworkDir) |
| 1057 | continue; |
| 1058 | |
| 1059 | // Load this framework module. |
| 1060 | loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir, |
| 1061 | IsSystem); |
| 1062 | } |
| 1063 | continue; |
| 1064 | } |
| 1065 | |
| 1066 | // FIXME: Deal with header maps. |
| 1067 | if (SearchDirs[Idx].isHeaderMap()) |
| 1068 | continue; |
| 1069 | |
| 1070 | // Try to load a module map file for the search directory. |
| 1071 | loadModuleMapFile(SearchDirs[Idx].getDir()); |
| 1072 | |
| 1073 | // Try to load module map files for immediate subdirectories of this search |
| 1074 | // directory. |
| 1075 | llvm::error_code EC; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1076 | SmallString<128> DirNative; |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1077 | llvm::sys::path::native(SearchDirs[Idx].getDir()->getName(), DirNative); |
| 1078 | for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd; |
| 1079 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 1080 | loadModuleMapFile(Dir->path()); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | // Populate the list of modules. |
| 1085 | for (ModuleMap::module_iterator M = ModMap.module_begin(), |
| 1086 | MEnd = ModMap.module_end(); |
| 1087 | M != MEnd; ++M) { |
| 1088 | Modules.push_back(M->getValue()); |
| 1089 | } |
| 1090 | } |