blob: b028e339ae9194d7c886affdec47356ff24cbb14 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DirectoryLookup and HeaderSearch interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer5f016e22007-07-11 17:01:13 +000014#include "clang/Lex/HeaderSearch.h"
Chris Lattner822da612007-12-17 06:36:45 +000015#include "clang/Lex/HeaderMap.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/IdentifierTable.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000018#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000019#include "llvm/Support/Path.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallString.h"
Chris Lattner3daed522009-03-02 22:20:04 +000021#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Douglas Gregor8c5a7602009-04-25 23:30:02 +000024const IdentifierInfo *
25HeaderFileInfo::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 Gregorcfbf1c72011-02-10 17:09:37 +000036ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
37
Chris Lattner39b49bc2010-11-23 08:35:12 +000038HeaderSearch::HeaderSearch(FileManager &FM)
39 : FileMgr(FM), FrameworkMap(64) {
Reid Spencer5f016e22007-07-11 17:01:13 +000040 SystemDirIdx = 0;
41 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000042
Douglas Gregor8c5a7602009-04-25 23:30:02 +000043 ExternalLookup = 0;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000044 ExternalSource = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000045 NumIncluded = 0;
46 NumMultiIncludeFileOptzn = 0;
47 NumFrameworkLookups = NumSubFrameworkLookups = 0;
48}
49
Chris Lattner822da612007-12-17 06:36:45 +000050HeaderSearch::~HeaderSearch() {
51 // Delete headermaps.
52 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
53 delete HeaderMaps[i].second;
54}
Mike Stump1eb44332009-09-09 15:08:12 +000055
Reid Spencer5f016e22007-07-11 17:01:13 +000056void HeaderSearch::PrintStats() {
57 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
58 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
59 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
60 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
61 NumOnceOnlyFiles += FileInfo[i].isImport;
62 if (MaxNumIncludes < FileInfo[i].NumIncludes)
63 MaxNumIncludes = FileInfo[i].NumIncludes;
64 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
65 }
66 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
67 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
68 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000069
Reid Spencer5f016e22007-07-11 17:01:13 +000070 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
71 fprintf(stderr, " %d #includes skipped due to"
72 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000073
Reid Spencer5f016e22007-07-11 17:01:13 +000074 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
75 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
76}
77
Chris Lattner822da612007-12-17 06:36:45 +000078/// CreateHeaderMap - This method returns a HeaderMap for the specified
79/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000080const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000081 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000082 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000083 if (!HeaderMaps.empty()) {
84 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000085 // Pointer equality comparison of FileEntries works because they are
86 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +000087 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +000088 return HeaderMaps[i].second;
89 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattner39b49bc2010-11-23 08:35:12 +000091 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattner822da612007-12-17 06:36:45 +000092 HeaderMaps.push_back(std::make_pair(FE, HM));
93 return HM;
94 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Chris Lattner822da612007-12-17 06:36:45 +000096 return 0;
97}
98
Chris Lattnerdf772332007-12-17 07:52:39 +000099//===----------------------------------------------------------------------===//
100// File lookup within a DirectoryLookup scope
101//===----------------------------------------------------------------------===//
102
Chris Lattner3af66a92007-12-17 17:57:27 +0000103/// getName - Return the directory or filename corresponding to this lookup
104/// object.
105const char *DirectoryLookup::getName() const {
106 if (isNormalDir())
107 return getDir()->getName();
108 if (isFramework())
109 return getFrameworkDir()->getName();
110 assert(isHeaderMap() && "Unknown DirectoryLookup");
111 return getHeaderMap()->getFileName();
112}
113
114
Chris Lattnerdf772332007-12-17 07:52:39 +0000115/// LookupFile - Lookup the specified file in this search path, returning it
116/// if it exists or returning null if not.
Chris Lattnera1394812010-01-10 01:35:12 +0000117const FileEntry *DirectoryLookup::LookupFile(llvm::StringRef Filename,
Chris Lattnerafded5b2007-12-17 08:13:48 +0000118 HeaderSearch &HS) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000119 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000120 if (isNormalDir()) {
121 // Concatenate the requested file onto the directory.
122 // FIXME: Portability. Filename concatenation should be in sys::Path.
123 TmpDir += getDir()->getName();
124 TmpDir.push_back('/');
Chris Lattnera1394812010-01-10 01:35:12 +0000125 TmpDir.append(Filename.begin(), Filename.end());
Chris Lattner39b49bc2010-11-23 08:35:12 +0000126 return HS.getFileMgr().getFile(TmpDir.str());
Chris Lattnerafded5b2007-12-17 08:13:48 +0000127 }
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Chris Lattnerafded5b2007-12-17 08:13:48 +0000129 if (isFramework())
Chris Lattnera1394812010-01-10 01:35:12 +0000130 return DoFrameworkLookup(Filename, HS);
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000132 assert(isHeaderMap() && "Unknown directory lookup");
Chris Lattner39b49bc2010-11-23 08:35:12 +0000133 return getHeaderMap()->LookupFile(Filename, HS.getFileMgr());
Chris Lattnerdf772332007-12-17 07:52:39 +0000134}
135
136
Chris Lattnerafded5b2007-12-17 08:13:48 +0000137/// DoFrameworkLookup - Do a lookup of the specified file in the current
138/// DirectoryLookup, which is a framework directory.
Chris Lattnera1394812010-01-10 01:35:12 +0000139const FileEntry *DirectoryLookup::DoFrameworkLookup(llvm::StringRef Filename,
Chris Lattnerafded5b2007-12-17 08:13:48 +0000140 HeaderSearch &HS) const {
141 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000144 size_t SlashPos = Filename.find('/');
145 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Chris Lattnerafded5b2007-12-17 08:13:48 +0000147 // Find out if this is the home for the specified framework, by checking
148 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000149 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000150 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Chris Lattnerafded5b2007-12-17 08:13:48 +0000152 // If it is known and in some other directory, fail.
153 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattnerafded5b2007-12-17 08:13:48 +0000156 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // FrameworkName = "/System/Library/Frameworks/"
159 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000160 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 if (FrameworkName.empty() || FrameworkName.back() != '/')
162 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000165 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
168 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Chris Lattnerafded5b2007-12-17 08:13:48 +0000170 // If the cache entry is still unresolved, query to see if the cache entry is
171 // still unresolved. If so, check its existence now.
172 if (FrameworkDirCache == 0) {
173 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000176 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000177 bool Exists;
178 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 // Otherwise, if it does, remember that this is the right direntry for this
182 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000183 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
187 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 FrameworkName += "Headers/";
Chris Lattnera1394812010-01-10 01:35:12 +0000190 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner39b49bc2010-11-23 08:35:12 +0000191 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 return FE;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
195 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000196 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 Private+strlen(Private));
Chris Lattner39b49bc2010-11-23 08:35:12 +0000198 return FileMgr.getFile(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000199}
200
Chris Lattnerdf772332007-12-17 07:52:39 +0000201
Chris Lattnerafded5b2007-12-17 08:13:48 +0000202//===----------------------------------------------------------------------===//
203// Header File Location.
204//===----------------------------------------------------------------------===//
205
206
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor10fe93d2010-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 Lattnera1394812010-01-10 01:35:12 +0000212const FileEntry *HeaderSearch::LookupFile(llvm::StringRef Filename,
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 bool isAngled,
214 const DirectoryLookup *FromDir,
215 const DirectoryLookup *&CurDir,
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000216 const FileEntry *CurFileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000218 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 CurDir = 0;
220
221 // If this was an #include_next "/absolute/file", fail.
222 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 // Otherwise, just return the file.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000225 return FileMgr.getFile(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 // Step #0, unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-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 Lattnerdf772332007-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 Gregor10fe93d2010-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());
Chris Lattner39b49bc2010-11-23 08:35:12 +0000240 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str())) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 // Leave CurDir unset.
Douglas Gregor10fe93d2010-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 Lattnerc9dde4f2008-02-25 21:38:21 +0000248 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 return FE;
250 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 }
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 CurDir = 0;
254
255 // If this is a system #include, ignore the user #include locs.
256 unsigned i = isAngled ? SystemDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000262
Chris Lattner9960ae82007-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 Lattnera1394812010-01-10 01:35:12 +0000268 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-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 Stump1eb44332009-09-09 15:08:12 +0000282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // Check each directory in sequence to see if it contains this file.
284 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000285 const FileEntry *FE =
Chris Lattnera1394812010-01-10 01:35:12 +0000286 SearchDirs[i].LookupFile(Filename, *this);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000287 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattnerafded5b2007-12-17 08:13:48 +0000289 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattnerafded5b2007-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 Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattnerafded5b2007-12-17 08:13:48 +0000294 // Remember this location for the next lookup we do.
295 CacheLookup.second = i;
296 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 }
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Chris Lattner9960ae82007-07-22 07:28:00 +0000299 // Otherwise, didn't find it. Remember we didn't find this.
300 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 return 0;
302}
303
304/// 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 Lattnera1394812010-01-10 01:35:12 +0000310LookupSubframeworkHeader(llvm::StringRef Filename,
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 const FileEntry *ContextFileEnt) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000312 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000315 size_t SlashPos = Filename.find('/');
316 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 // Look up the base framework name of the ContextFileEnt.
319 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 // If the context info wasn't a framework, couldn't be a subframework.
322 const char *FrameworkPos = strstr(ContextName, ".framework/");
323 if (FrameworkPos == 0)
324 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000325
326 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 FrameworkPos+strlen(".framework/"));
328
329 // Append Frameworks/HIToolbox.framework/
330 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000331 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 FrameworkName += ".framework/";
333
334 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000335 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 // Some other location?
338 if (CacheLookup.getValue() &&
339 CacheLookup.getKeyLength() == FrameworkName.size() &&
340 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
341 CacheLookup.getKeyLength()) != 0)
342 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 // Cache subframework.
345 if (CacheLookup.getValue() == 0) {
346 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000349 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 // Otherwise, if it does, remember that this is the right direntry for this
353 // framework.
354 CacheLookup.setValue(Dir);
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 const FileEntry *FE = 0;
358
359 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
360 llvm::SmallString<1024> HeadersFilename(FrameworkName);
361 HeadersFilename += "Headers/";
Chris Lattnera1394812010-01-10 01:35:12 +0000362 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner39b49bc2010-11-23 08:35:12 +0000363 if (!(FE = FileMgr.getFile(HeadersFilename.str()))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
366 HeadersFilename = FrameworkName;
367 HeadersFilename += "PrivateHeaders/";
Chris Lattnera1394812010-01-10 01:35:12 +0000368 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Chris Lattner39b49bc2010-11-23 08:35:12 +0000369 if (!(FE = FileMgr.getFile(HeadersFilename.str())))
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 return 0;
371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000374 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000375 // Note that the temporary 'DirInfo' is required here, as either call to
376 // getFileInfo could resize the vector and we don't want to rely on order
377 // of evaluation.
378 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
379 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 return FE;
381}
382
383//===----------------------------------------------------------------------===//
384// File Info Management.
385//===----------------------------------------------------------------------===//
386
387
Steve Naroff83d63c72009-04-24 20:03:17 +0000388/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000389/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000390HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 if (FE->getUID() >= FileInfo.size())
392 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000393
394 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
395 if (ExternalSource && !HFI.Resolved) {
396 HFI = ExternalSource->GetHeaderFileInfo(FE);
397 HFI.Resolved = true;
398 }
399 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000400}
Reid Spencer5f016e22007-07-11 17:01:13 +0000401
Steve Naroff83d63c72009-04-24 20:03:17 +0000402void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
403 if (UID >= FileInfo.size())
404 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000405 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000406 FileInfo[UID] = HFI;
407}
408
Reid Spencer5f016e22007-07-11 17:01:13 +0000409/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
410/// #include, #include_next, or #import directive. Return false if #including
411/// the file will have no effect or true if we should include it.
412bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
413 ++NumIncluded; // Count # of attempted #includes.
414
415 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000416 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 // If this is a #import directive, check that we have not already imported
419 // this header.
420 if (isImport) {
421 // If this has already been imported, don't import it again.
422 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 // Has this already been #import'ed or #include'd?
425 if (FileInfo.NumIncludes) return false;
426 } else {
427 // Otherwise, if this is a #include of a file that was previously #import'd
428 // or if this is the second #include of a #pragma once file, ignore it.
429 if (FileInfo.isImport)
430 return false;
431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
434 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000435 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000436 = FileInfo.getControllingMacro(ExternalLookup))
437 if (ControllingMacro->hasMacroDefinition()) {
438 ++NumMultiIncludeFileOptzn;
439 return false;
440 }
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 // Increment the number of times this file has been included.
443 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 return true;
446}
447
448