blob: 4554ababf76d4aedd3d4af1bc43234e538abd29c [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/System/Path.h"
19#include "llvm/ADT/SmallString.h"
Chris Lattner3daed522009-03-02 22:20:04 +000020#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
Douglas Gregor8c5a7602009-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
Reid Spencer5f016e22007-07-11 17:01:13 +000035HeaderSearch::HeaderSearch(FileManager &FM) : FileMgr(FM), FrameworkMap(64) {
36 SystemDirIdx = 0;
37 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000038
Douglas Gregor8c5a7602009-04-25 23:30:02 +000039 ExternalLookup = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000040 NumIncluded = 0;
41 NumMultiIncludeFileOptzn = 0;
42 NumFrameworkLookups = NumSubFrameworkLookups = 0;
43}
44
Chris Lattner822da612007-12-17 06:36:45 +000045HeaderSearch::~HeaderSearch() {
46 // Delete headermaps.
47 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
48 delete HeaderMaps[i].second;
49}
Mike Stump1eb44332009-09-09 15:08:12 +000050
Reid Spencer5f016e22007-07-11 17:01:13 +000051void HeaderSearch::PrintStats() {
52 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
53 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
54 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
55 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
56 NumOnceOnlyFiles += FileInfo[i].isImport;
57 if (MaxNumIncludes < FileInfo[i].NumIncludes)
58 MaxNumIncludes = FileInfo[i].NumIncludes;
59 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
60 }
61 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
62 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
63 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
66 fprintf(stderr, " %d #includes skipped due to"
67 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000068
Reid Spencer5f016e22007-07-11 17:01:13 +000069 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
70 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
71}
72
Chris Lattner822da612007-12-17 06:36:45 +000073/// CreateHeaderMap - This method returns a HeaderMap for the specified
74/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000075const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000076 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000077 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000078 if (!HeaderMaps.empty()) {
79 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000080 // Pointer equality comparison of FileEntries works because they are
81 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +000082 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +000083 return HeaderMaps[i].second;
84 }
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner1bfd4a62007-12-17 18:34:53 +000086 if (const HeaderMap *HM = HeaderMap::Create(FE)) {
Chris Lattner822da612007-12-17 06:36:45 +000087 HeaderMaps.push_back(std::make_pair(FE, HM));
88 return HM;
89 }
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattner822da612007-12-17 06:36:45 +000091 return 0;
92}
93
Chris Lattnerdf772332007-12-17 07:52:39 +000094//===----------------------------------------------------------------------===//
95// File lookup within a DirectoryLookup scope
96//===----------------------------------------------------------------------===//
97
Chris Lattner3af66a92007-12-17 17:57:27 +000098/// getName - Return the directory or filename corresponding to this lookup
99/// object.
100const char *DirectoryLookup::getName() const {
101 if (isNormalDir())
102 return getDir()->getName();
103 if (isFramework())
104 return getFrameworkDir()->getName();
105 assert(isHeaderMap() && "Unknown DirectoryLookup");
106 return getHeaderMap()->getFileName();
107}
108
109
Chris Lattnerdf772332007-12-17 07:52:39 +0000110/// LookupFile - Lookup the specified file in this search path, returning it
111/// if it exists or returning null if not.
Chris Lattnera1394812010-01-10 01:35:12 +0000112const FileEntry *DirectoryLookup::LookupFile(llvm::StringRef Filename,
Chris Lattnerafded5b2007-12-17 08:13:48 +0000113 HeaderSearch &HS) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000114 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000115 if (isNormalDir()) {
116 // Concatenate the requested file onto the directory.
117 // FIXME: Portability. Filename concatenation should be in sys::Path.
118 TmpDir += getDir()->getName();
119 TmpDir.push_back('/');
Chris Lattnera1394812010-01-10 01:35:12 +0000120 TmpDir.append(Filename.begin(), Filename.end());
Chris Lattnerafded5b2007-12-17 08:13:48 +0000121 return HS.getFileMgr().getFile(TmpDir.begin(), TmpDir.end());
122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Chris Lattnerafded5b2007-12-17 08:13:48 +0000124 if (isFramework())
Chris Lattnera1394812010-01-10 01:35:12 +0000125 return DoFrameworkLookup(Filename, HS);
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000127 assert(isHeaderMap() && "Unknown directory lookup");
Chris Lattnera1394812010-01-10 01:35:12 +0000128 return getHeaderMap()->LookupFile(Filename, HS.getFileMgr());
Chris Lattnerdf772332007-12-17 07:52:39 +0000129}
130
131
Chris Lattnerafded5b2007-12-17 08:13:48 +0000132/// DoFrameworkLookup - Do a lookup of the specified file in the current
133/// DirectoryLookup, which is a framework directory.
Chris Lattnera1394812010-01-10 01:35:12 +0000134const FileEntry *DirectoryLookup::DoFrameworkLookup(llvm::StringRef Filename,
Chris Lattnerafded5b2007-12-17 08:13:48 +0000135 HeaderSearch &HS) const {
136 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000139 size_t SlashPos = Filename.find('/');
140 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattnerafded5b2007-12-17 08:13:48 +0000142 // Find out if this is the home for the specified framework, by checking
143 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000144 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000145 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Chris Lattnerafded5b2007-12-17 08:13:48 +0000147 // If it is known and in some other directory, fail.
148 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Chris Lattnerafded5b2007-12-17 08:13:48 +0000151 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 // FrameworkName = "/System/Library/Frameworks/"
154 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000155 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 if (FrameworkName.empty() || FrameworkName.back() != '/')
157 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000160 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
163 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Chris Lattnerafded5b2007-12-17 08:13:48 +0000165 // If the cache entry is still unresolved, query to see if the cache entry is
166 // still unresolved. If so, check its existence now.
167 if (FrameworkDirCache == 0) {
168 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000171 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Mike Stump1eb44332009-09-09 15:08:12 +0000172 if (!llvm::sys::Path(std::string(FrameworkName.begin(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 FrameworkName.end())).exists())
174 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 // Otherwise, if it does, remember that this is the right direntry for this
177 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000178 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
182 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 FrameworkName += "Headers/";
Chris Lattnera1394812010-01-10 01:35:12 +0000185 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.begin(),
187 FrameworkName.end())) {
188 return FE;
189 }
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
192 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000193 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 Private+strlen(Private));
195 return FileMgr.getFile(FrameworkName.begin(), FrameworkName.end());
196}
197
Chris Lattnerdf772332007-12-17 07:52:39 +0000198
Chris Lattnerafded5b2007-12-17 08:13:48 +0000199//===----------------------------------------------------------------------===//
200// Header File Location.
201//===----------------------------------------------------------------------===//
202
203
Reid Spencer5f016e22007-07-11 17:01:13 +0000204/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
205/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000206/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
207/// non-null, indicates where the #including file is, in case a relative search
208/// is needed.
Chris Lattnera1394812010-01-10 01:35:12 +0000209const FileEntry *HeaderSearch::LookupFile(llvm::StringRef Filename,
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 bool isAngled,
211 const DirectoryLookup *FromDir,
212 const DirectoryLookup *&CurDir,
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000213 const FileEntry *CurFileEnt) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 // If 'Filename' is absolute, check to see if it exists and no searching.
Chris Lattnera1394812010-01-10 01:35:12 +0000215 if (llvm::sys::Path::isAbsolute(Filename.begin(), Filename.size())) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 CurDir = 0;
217
218 // If this was an #include_next "/absolute/file", fail.
219 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 // Otherwise, just return the file.
Chris Lattnera1394812010-01-10 01:35:12 +0000222 return FileMgr.getFile(Filename);
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 }
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 // Step #0, unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000226 // directory. This has to be based on CurFileEnt, not CurDir, because
227 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000228 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
229 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000230 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
231 llvm::SmallString<1024> TmpDir;
232 // Concatenate the requested file onto the directory.
233 // FIXME: Portability. Filename concatenation should be in sys::Path.
234 TmpDir += CurFileEnt->getDir()->getName();
235 TmpDir.push_back('/');
236 TmpDir.append(Filename.begin(), Filename.end());
237 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str())) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000239 // This file is a system header or C++ unfriendly if the old file is.
240 //
241 // Note that the temporary 'DirInfo' is required here, as either call to
242 // getFileInfo could resize the vector and we don't want to rely on order
243 // of evaluation.
244 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000245 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 return FE;
247 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 }
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 CurDir = 0;
251
252 // If this is a system #include, ignore the user #include locs.
253 unsigned i = isAngled ? SystemDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // If this is a #include_next request, start searching after the directory the
256 // file was found in.
257 if (FromDir)
258 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Chris Lattner9960ae82007-07-22 07:28:00 +0000260 // Cache all of the lookups performed by this method. Many headers are
261 // multiply included, and the "pragma once" optimization prevents them from
262 // being relex/pp'd, but they would still have to search through a
263 // (potentially huge) series of SearchDirs to find it.
264 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000265 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000266
267 // If the entry has been previously looked up, the first value will be
268 // non-zero. If the value is equal to i (the start point of our search), then
269 // this is a matching hit.
270 if (CacheLookup.first == i+1) {
271 // Skip querying potentially lots of directories for this lookup.
272 i = CacheLookup.second;
273 } else {
274 // Otherwise, this is the first query, or the previous query didn't match
275 // our search start. We will fill in our found location below, so prime the
276 // start point value.
277 CacheLookup.first = i+1;
278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // Check each directory in sequence to see if it contains this file.
281 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000282 const FileEntry *FE =
Chris Lattnera1394812010-01-10 01:35:12 +0000283 SearchDirs[i].LookupFile(Filename, *this);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000284 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattnerafded5b2007-12-17 08:13:48 +0000286 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Chris Lattnerafded5b2007-12-17 08:13:48 +0000288 // This file is a system header or C++ unfriendly if the dir is.
289 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Chris Lattnerafded5b2007-12-17 08:13:48 +0000291 // Remember this location for the next lookup we do.
292 CacheLookup.second = i;
293 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Chris Lattner9960ae82007-07-22 07:28:00 +0000296 // Otherwise, didn't find it. Remember we didn't find this.
297 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 return 0;
299}
300
301/// LookupSubframeworkHeader - Look up a subframework for the specified
302/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
303/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
304/// is a subframework within Carbon.framework. If so, return the FileEntry
305/// for the designated file, otherwise return null.
306const FileEntry *HeaderSearch::
Chris Lattnera1394812010-01-10 01:35:12 +0000307LookupSubframeworkHeader(llvm::StringRef Filename,
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 const FileEntry *ContextFileEnt) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000309 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Reid Spencer5f016e22007-07-11 17:01:13 +0000311 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000312 size_t SlashPos = Filename.find('/');
313 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 // Look up the base framework name of the ContextFileEnt.
316 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 // If the context info wasn't a framework, couldn't be a subframework.
319 const char *FrameworkPos = strstr(ContextName, ".framework/");
320 if (FrameworkPos == 0)
321 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
323 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 FrameworkPos+strlen(".framework/"));
325
326 // Append Frameworks/HIToolbox.framework/
327 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000328 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 FrameworkName += ".framework/";
330
331 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000332 FrameworkMap.GetOrCreateValue(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 // Some other location?
335 if (CacheLookup.getValue() &&
336 CacheLookup.getKeyLength() == FrameworkName.size() &&
337 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
338 CacheLookup.getKeyLength()) != 0)
339 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // Cache subframework.
342 if (CacheLookup.getValue() == 0) {
343 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 // If the framework dir doesn't exist, we fail.
346 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
347 FrameworkName.end());
348 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 // Otherwise, if it does, remember that this is the right direntry for this
351 // framework.
352 CacheLookup.setValue(Dir);
353 }
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 const FileEntry *FE = 0;
356
357 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
358 llvm::SmallString<1024> HeadersFilename(FrameworkName);
359 HeadersFilename += "Headers/";
Chris Lattnera1394812010-01-10 01:35:12 +0000360 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
362 HeadersFilename.end()))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
365 HeadersFilename = FrameworkName;
366 HeadersFilename += "PrivateHeaders/";
Chris Lattnera1394812010-01-10 01:35:12 +0000367 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
369 return 0;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000373 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000374 // Note that the temporary 'DirInfo' is required here, as either call to
375 // getFileInfo could resize the vector and we don't want to rely on order
376 // of evaluation.
377 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
378 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 return FE;
380}
381
382//===----------------------------------------------------------------------===//
383// File Info Management.
384//===----------------------------------------------------------------------===//
385
386
Steve Naroff83d63c72009-04-24 20:03:17 +0000387/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000388/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000389HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 if (FE->getUID() >= FileInfo.size())
391 FileInfo.resize(FE->getUID()+1);
392 return FileInfo[FE->getUID()];
Mike Stump1eb44332009-09-09 15:08:12 +0000393}
Reid Spencer5f016e22007-07-11 17:01:13 +0000394
Steve Naroff83d63c72009-04-24 20:03:17 +0000395void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
396 if (UID >= FileInfo.size())
397 FileInfo.resize(UID+1);
398 FileInfo[UID] = HFI;
399}
400
Reid Spencer5f016e22007-07-11 17:01:13 +0000401/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
402/// #include, #include_next, or #import directive. Return false if #including
403/// the file will have no effect or true if we should include it.
404bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
405 ++NumIncluded; // Count # of attempted #includes.
406
407 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000408 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 // If this is a #import directive, check that we have not already imported
411 // this header.
412 if (isImport) {
413 // If this has already been imported, don't import it again.
414 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 // Has this already been #import'ed or #include'd?
417 if (FileInfo.NumIncludes) return false;
418 } else {
419 // Otherwise, if this is a #include of a file that was previously #import'd
420 // or if this is the second #include of a #pragma once file, ignore it.
421 if (FileInfo.isImport)
422 return false;
423 }
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
426 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000427 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000428 = FileInfo.getControllingMacro(ExternalLookup))
429 if (ControllingMacro->hasMacroDefinition()) {
430 ++NumMultiIncludeFileOptzn;
431 return false;
432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 // Increment the number of times this file has been included.
435 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 return true;
438}
439
440