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