blob: ee6c4c6a8c06fed478f9597b63996ba4a4b0b105 [file] [log] [blame]
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner59a9ebd2006-10-18 05:34:33 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DirectoryLookup and HeaderSearch interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner59a9ebd2006-10-18 05:34:33 +000014#include "clang/Lex/HeaderSearch.h"
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000015#include "clang/Lex/HeaderMap.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/IdentifierTable.h"
Chris Lattner59a9ebd2006-10-18 05:34:33 +000018#include "llvm/System/Path.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +000019#include "llvm/ADT/SmallString.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000020#include <cstdio>
Chris Lattner59a9ebd2006-10-18 05:34:33 +000021using namespace clang;
22
Douglas Gregor99734e72009-04-25 23:30:02 +000023const IdentifierInfo *
24HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
25 if (ControllingMacro)
26 return ControllingMacro;
27
28 if (!ControllingMacroID || !External)
29 return 0;
30
31 ControllingMacro = External->GetIdentifier(ControllingMacroID);
32 return ControllingMacro;
33}
34
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000035HeaderSearch::HeaderSearch(FileManager &FM, const FileSystemOptions &FSOpts)
36 : FileMgr(FM), FileSystemOpts(FSOpts), FrameworkMap(64) {
Chris Lattner641a0be2006-10-20 06:23:14 +000037 SystemDirIdx = 0;
38 NoCurDirSearch = false;
Mike Stump11289f42009-09-09 15:08:12 +000039
Douglas Gregor99734e72009-04-25 23:30:02 +000040 ExternalLookup = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000041 NumIncluded = 0;
42 NumMultiIncludeFileOptzn = 0;
43 NumFrameworkLookups = NumSubFrameworkLookups = 0;
44}
45
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000046HeaderSearch::~HeaderSearch() {
47 // Delete headermaps.
48 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
49 delete HeaderMaps[i].second;
50}
Mike Stump11289f42009-09-09 15:08:12 +000051
Chris Lattner59a9ebd2006-10-18 05:34:33 +000052void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000053 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
54 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000055 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
56 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
57 NumOnceOnlyFiles += FileInfo[i].isImport;
58 if (MaxNumIncludes < FileInfo[i].NumIncludes)
59 MaxNumIncludes = FileInfo[i].NumIncludes;
60 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
61 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000062 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
63 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
64 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump11289f42009-09-09 15:08:12 +000065
Chris Lattner23b7eb62007-06-15 23:05:46 +000066 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
67 fprintf(stderr, " %d #includes skipped due to"
68 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump11289f42009-09-09 15:08:12 +000069
Chris Lattner23b7eb62007-06-15 23:05:46 +000070 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
71 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000072}
73
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000074/// CreateHeaderMap - This method returns a HeaderMap for the specified
75/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner4ffe46c2007-12-17 18:34:53 +000076const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000077 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerf62f7582007-12-17 07:52:39 +000078 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000079 if (!HeaderMaps.empty()) {
80 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerf62f7582007-12-17 07:52:39 +000081 // Pointer equality comparison of FileEntries works because they are
82 // already uniqued by inode.
Mike Stump11289f42009-09-09 15:08:12 +000083 if (HeaderMaps[i].first == FE)
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000084 return HeaderMaps[i].second;
85 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000087 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr, FileSystemOpts)) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000088 HeaderMaps.push_back(std::make_pair(FE, HM));
89 return HM;
90 }
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000092 return 0;
93}
94
Chris Lattnerf62f7582007-12-17 07:52:39 +000095//===----------------------------------------------------------------------===//
96// File lookup within a DirectoryLookup scope
97//===----------------------------------------------------------------------===//
98
Chris Lattner8d720d02007-12-17 17:57:27 +000099/// getName - Return the directory or filename corresponding to this lookup
100/// object.
101const char *DirectoryLookup::getName() const {
102 if (isNormalDir())
103 return getDir()->getName();
104 if (isFramework())
105 return getFrameworkDir()->getName();
106 assert(isHeaderMap() && "Unknown DirectoryLookup");
107 return getHeaderMap()->getFileName();
108}
109
110
Chris Lattnerf62f7582007-12-17 07:52:39 +0000111/// LookupFile - Lookup the specified file in this search path, returning it
112/// if it exists or returning null if not.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000113const FileEntry *DirectoryLookup::LookupFile(llvm::StringRef Filename,
Chris Lattner712e3872007-12-17 08:13:48 +0000114 HeaderSearch &HS) const {
Chris Lattnerf62f7582007-12-17 07:52:39 +0000115 llvm::SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000116 if (isNormalDir()) {
117 // Concatenate the requested file onto the directory.
118 // FIXME: Portability. Filename concatenation should be in sys::Path.
119 TmpDir += getDir()->getName();
120 TmpDir.push_back('/');
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000121 TmpDir.append(Filename.begin(), Filename.end());
Chris Lattner8afa6de2010-11-21 09:55:08 +0000122 return HS.getFileMgr().getFile(TmpDir.str(), HS.getFileSystemOpts());
Chris Lattner712e3872007-12-17 08:13:48 +0000123 }
Mike Stump11289f42009-09-09 15:08:12 +0000124
Chris Lattner712e3872007-12-17 08:13:48 +0000125 if (isFramework())
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000126 return DoFrameworkLookup(Filename, HS);
Mike Stump11289f42009-09-09 15:08:12 +0000127
Chris Lattner44bd21b2007-12-17 08:17:39 +0000128 assert(isHeaderMap() && "Unknown directory lookup");
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000129 return getHeaderMap()->LookupFile(Filename, HS.getFileMgr(),
130 HS.getFileSystemOpts());
Chris Lattnerf62f7582007-12-17 07:52:39 +0000131}
132
133
Chris Lattner712e3872007-12-17 08:13:48 +0000134/// DoFrameworkLookup - Do a lookup of the specified file in the current
135/// DirectoryLookup, which is a framework directory.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000136const FileEntry *DirectoryLookup::DoFrameworkLookup(llvm::StringRef Filename,
Chris Lattner712e3872007-12-17 08:13:48 +0000137 HeaderSearch &HS) const {
138 FileManager &FileMgr = HS.getFileMgr();
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000139 const FileSystemOptions &FileSystemOpts = HS.getFileSystemOpts();
Mike Stump11289f42009-09-09 15:08:12 +0000140
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000141 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000142 size_t SlashPos = Filename.find('/');
143 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000144
Chris Lattner712e3872007-12-17 08:13:48 +0000145 // Find out if this is the home for the specified framework, by checking
146 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump11289f42009-09-09 15:08:12 +0000147 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000148 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000149
Chris Lattner712e3872007-12-17 08:13:48 +0000150 // If it is known and in some other directory, fail.
151 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Chris Lattner5ed76da2006-10-22 07:24:13 +0000152 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000153
Chris Lattner712e3872007-12-17 08:13:48 +0000154 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000156 // FrameworkName = "/System/Library/Frameworks/"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000157 llvm::SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000158 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000159 if (FrameworkName.empty() || FrameworkName.back() != '/')
160 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000161
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000162 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000163 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000165 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
166 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattner712e3872007-12-17 08:13:48 +0000168 // If the cache entry is still unresolved, query to see if the cache entry is
169 // still unresolved. If so, check its existence now.
170 if (FrameworkDirCache == 0) {
171 HS.IncrementFrameworkLookupCount();
Mike Stump11289f42009-09-09 15:08:12 +0000172
Chris Lattner5ed76da2006-10-22 07:24:13 +0000173 // If the framework dir doesn't exist, we fail.
Chris Lattner712e3872007-12-17 08:13:48 +0000174 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Mike Stump11289f42009-09-09 15:08:12 +0000175 if (!llvm::sys::Path(std::string(FrameworkName.begin(),
Chris Lattner23b7eb62007-06-15 23:05:46 +0000176 FrameworkName.end())).exists())
Chris Lattner5ed76da2006-10-22 07:24:13 +0000177 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner5ed76da2006-10-22 07:24:13 +0000179 // Otherwise, if it does, remember that this is the right direntry for this
180 // framework.
Chris Lattner712e3872007-12-17 08:13:48 +0000181 FrameworkDirCache = getFrameworkDir();
Chris Lattner5ed76da2006-10-22 07:24:13 +0000182 }
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000184 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000185 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000186
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000187 FrameworkName += "Headers/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000188 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner8afa6de2010-11-21 09:55:08 +0000189 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000190 FileSystemOpts)) {
Chris Lattner577377e2006-10-20 04:55:45 +0000191 return FE;
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000192 }
Mike Stump11289f42009-09-09 15:08:12 +0000193
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000194 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000195 const char *Private = "Private";
Mike Stump11289f42009-09-09 15:08:12 +0000196 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000197 Private+strlen(Private));
Chris Lattner8afa6de2010-11-21 09:55:08 +0000198 return FileMgr.getFile(FrameworkName.str(), FileSystemOpts);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000199}
200
Chris Lattnerf62f7582007-12-17 07:52:39 +0000201
Chris Lattner712e3872007-12-17 08:13:48 +0000202//===----------------------------------------------------------------------===//
203// Header File Location.
204//===----------------------------------------------------------------------===//
205
206
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000207/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
208/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor618e64a2010-08-08 07:49:23 +0000209/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
210/// non-null, indicates where the #including file is, in case a relative search
211/// is needed.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000212const FileEntry *HeaderSearch::LookupFile(llvm::StringRef Filename,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000213 bool isAngled,
214 const DirectoryLookup *FromDir,
215 const DirectoryLookup *&CurDir,
Douglas Gregor618e64a2010-08-08 07:49:23 +0000216 const FileEntry *CurFileEnt) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000217 // If 'Filename' is absolute, check to see if it exists and no searching.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000218 if (llvm::sys::Path::isAbsolute(Filename.begin(), Filename.size())) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000219 CurDir = 0;
220
221 // If this was an #include_next "/absolute/file", fail.
222 if (FromDir) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000223
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000224 // Otherwise, just return the file.
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000225 return FileMgr.getFile(Filename, FileSystemOpts);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000226 }
Mike Stump11289f42009-09-09 15:08:12 +0000227
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000228 // Step #0, unless disabled, check to see if the file is in the #includer's
Douglas Gregor618e64a2010-08-08 07:49:23 +0000229 // directory. This has to be based on CurFileEnt, not CurDir, because
230 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerf62f7582007-12-17 07:52:39 +0000231 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
232 // This search is not done for <> headers.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000233 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
234 llvm::SmallString<1024> TmpDir;
235 // Concatenate the requested file onto the directory.
236 // FIXME: Portability. Filename concatenation should be in sys::Path.
237 TmpDir += CurFileEnt->getDir()->getName();
238 TmpDir.push_back('/');
239 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000240 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(), FileSystemOpts)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000241 // Leave CurDir unset.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000242 // This file is a system header or C++ unfriendly if the old file is.
243 //
244 // Note that the temporary 'DirInfo' is required here, as either call to
245 // getFileInfo could resize the vector and we don't want to rely on order
246 // of evaluation.
247 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000248 getFileInfo(FE).DirInfo = DirInfo;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000249 return FE;
250 }
251 }
Mike Stump11289f42009-09-09 15:08:12 +0000252
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000253 CurDir = 0;
254
255 // If this is a system #include, ignore the user #include locs.
256 unsigned i = isAngled ? SystemDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000257
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000258 // If this is a #include_next request, start searching after the directory the
259 // file was found in.
260 if (FromDir)
261 i = FromDir-&SearchDirs[0];
Mike Stump11289f42009-09-09 15:08:12 +0000262
Chris Lattnerd4275422007-07-22 07:28:00 +0000263 // Cache all of the lookups performed by this method. Many headers are
264 // multiply included, and the "pragma once" optimization prevents them from
265 // being relex/pp'd, but they would still have to search through a
266 // (potentially huge) series of SearchDirs to find it.
267 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000268 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattnerd4275422007-07-22 07:28:00 +0000269
270 // If the entry has been previously looked up, the first value will be
271 // non-zero. If the value is equal to i (the start point of our search), then
272 // this is a matching hit.
273 if (CacheLookup.first == i+1) {
274 // Skip querying potentially lots of directories for this lookup.
275 i = CacheLookup.second;
276 } else {
277 // Otherwise, this is the first query, or the previous query didn't match
278 // our search start. We will fill in our found location below, so prime the
279 // start point value.
280 CacheLookup.first = i+1;
281 }
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000283 // Check each directory in sequence to see if it contains this file.
284 for (; i != SearchDirs.size(); ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000285 const FileEntry *FE =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000286 SearchDirs[i].LookupFile(Filename, *this);
Chris Lattner712e3872007-12-17 08:13:48 +0000287 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner712e3872007-12-17 08:13:48 +0000289 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000290
Chris Lattner712e3872007-12-17 08:13:48 +0000291 // This file is a system header or C++ unfriendly if the dir is.
292 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattner712e3872007-12-17 08:13:48 +0000294 // Remember this location for the next lookup we do.
295 CacheLookup.second = i;
296 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000297 }
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattnerd4275422007-07-22 07:28:00 +0000299 // Otherwise, didn't find it. Remember we didn't find this.
300 CacheLookup.second = SearchDirs.size();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000301 return 0;
302}
303
Chris Lattner63dd32b2006-10-20 04:42:40 +0000304/// LookupSubframeworkHeader - Look up a subframework for the specified
305/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
306/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
307/// is a subframework within Carbon.framework. If so, return the FileEntry
308/// for the designated file, otherwise return null.
309const FileEntry *HeaderSearch::
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000310LookupSubframeworkHeader(llvm::StringRef Filename,
Chris Lattner63dd32b2006-10-20 04:42:40 +0000311 const FileEntry *ContextFileEnt) {
Chris Lattner12261882008-02-01 05:34:02 +0000312 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000313
Chris Lattner63dd32b2006-10-20 04:42:40 +0000314 // Framework names must have a '/' in the filename. Find it.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000315 size_t SlashPos = Filename.find('/');
316 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000317
Chris Lattner63dd32b2006-10-20 04:42:40 +0000318 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000319 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000320
Chris Lattner63dd32b2006-10-20 04:42:40 +0000321 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000322 const char *FrameworkPos = strstr(ContextName, ".framework/");
323 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000324 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000325
326 llvm::SmallString<1024> FrameworkName(ContextName,
Chris Lattner23b7eb62007-06-15 23:05:46 +0000327 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000328
Chris Lattner63dd32b2006-10-20 04:42:40 +0000329 // Append Frameworks/HIToolbox.framework/
330 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000331 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000332 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000333
Chris Lattner23b7eb62007-06-15 23:05:46 +0000334 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner8afa6de2010-11-21 09:55:08 +0000335 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000336
Chris Lattner5ed76da2006-10-22 07:24:13 +0000337 // Some other location?
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000338 if (CacheLookup.getValue() &&
339 CacheLookup.getKeyLength() == FrameworkName.size() &&
340 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
341 CacheLookup.getKeyLength()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000342 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattner5ed76da2006-10-22 07:24:13 +0000344 // Cache subframework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000345 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000346 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000347
Chris Lattner5ed76da2006-10-22 07:24:13 +0000348 // If the framework dir doesn't exist, we fail.
Chris Lattner8afa6de2010-11-21 09:55:08 +0000349 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str(),
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000350 FileSystemOpts);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000351 if (Dir == 0) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner5ed76da2006-10-22 07:24:13 +0000353 // Otherwise, if it does, remember that this is the right direntry for this
354 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000355 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Chris Lattner577377e2006-10-20 04:55:45 +0000358 const FileEntry *FE = 0;
359
Chris Lattner63dd32b2006-10-20 04:42:40 +0000360 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000361 llvm::SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000362 HeadersFilename += "Headers/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000363 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner8afa6de2010-11-21 09:55:08 +0000364 if (!(FE = FileMgr.getFile(HeadersFilename.str(), FileSystemOpts))) {
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner63dd32b2006-10-20 04:42:40 +0000366 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000367 HeadersFilename = FrameworkName;
368 HeadersFilename += "PrivateHeaders/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000369 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner8afa6de2010-11-21 09:55:08 +0000370 if (!(FE = FileMgr.getFile(HeadersFilename.str(), FileSystemOpts)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000371 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000372 }
Mike Stump11289f42009-09-09 15:08:12 +0000373
Chris Lattner577377e2006-10-20 04:55:45 +0000374 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000375 //
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000376 // Note that the temporary 'DirInfo' is required here, as either call to
377 // getFileInfo could resize the vector and we don't want to rely on order
378 // of evaluation.
379 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
380 getFileInfo(FE).DirInfo = DirInfo;
Chris Lattner577377e2006-10-20 04:55:45 +0000381 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000382}
383
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000384//===----------------------------------------------------------------------===//
385// File Info Management.
386//===----------------------------------------------------------------------===//
387
388
Steve Naroff3fa455a2009-04-24 20:03:17 +0000389/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000390/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000391HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000392 if (FE->getUID() >= FileInfo.size())
393 FileInfo.resize(FE->getUID()+1);
394 return FileInfo[FE->getUID()];
Mike Stump11289f42009-09-09 15:08:12 +0000395}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000396
Steve Naroff3fa455a2009-04-24 20:03:17 +0000397void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
398 if (UID >= FileInfo.size())
399 FileInfo.resize(UID+1);
400 FileInfo[UID] = HFI;
401}
402
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000403/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
404/// #include, #include_next, or #import directive. Return false if #including
405/// the file will have no effect or true if we should include it.
406bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
407 ++NumIncluded; // Count # of attempted #includes.
408
409 // Get information about this file.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000410 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000412 // If this is a #import directive, check that we have not already imported
413 // this header.
414 if (isImport) {
415 // If this has already been imported, don't import it again.
416 FileInfo.isImport = true;
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000418 // Has this already been #import'ed or #include'd?
419 if (FileInfo.NumIncludes) return false;
420 } else {
421 // Otherwise, if this is a #include of a file that was previously #import'd
422 // or if this is the second #include of a #pragma once file, ignore it.
423 if (FileInfo.isImport)
424 return false;
425 }
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000427 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
428 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump11289f42009-09-09 15:08:12 +0000429 if (const IdentifierInfo *ControllingMacro
Douglas Gregor99734e72009-04-25 23:30:02 +0000430 = FileInfo.getControllingMacro(ExternalLookup))
431 if (ControllingMacro->hasMacroDefinition()) {
432 ++NumMultiIncludeFileOptzn;
433 return false;
434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000436 // Increment the number of times this file has been included.
437 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +0000438
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000439 return true;
440}
441
442