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