blob: 86ab9564a2350503ec26f44b3d95a82fe8050fc4 [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"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000018#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000019#include "llvm/Support/Path.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +000020#include "llvm/ADT/SmallString.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000021#include <cstdio>
Chris Lattner59a9ebd2006-10-18 05:34:33 +000022using namespace clang;
23
Douglas Gregor99734e72009-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 Gregor09b69892011-02-10 17:09:37 +000036ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
37
Chris Lattner5159f612010-11-23 08:35:12 +000038HeaderSearch::HeaderSearch(FileManager &FM)
39 : FileMgr(FM), FrameworkMap(64) {
Nico Weber3b1d1212011-05-24 04:31:14 +000040 AngledDirIdx = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000041 SystemDirIdx = 0;
42 NoCurDirSearch = false;
Mike Stump11289f42009-09-09 15:08:12 +000043
Douglas Gregor99734e72009-04-25 23:30:02 +000044 ExternalLookup = 0;
Douglas Gregor09b69892011-02-10 17:09:37 +000045 ExternalSource = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000046 NumIncluded = 0;
47 NumMultiIncludeFileOptzn = 0;
48 NumFrameworkLookups = NumSubFrameworkLookups = 0;
49}
50
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000051HeaderSearch::~HeaderSearch() {
52 // Delete headermaps.
53 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
54 delete HeaderMaps[i].second;
55}
Mike Stump11289f42009-09-09 15:08:12 +000056
Chris Lattner59a9ebd2006-10-18 05:34:33 +000057void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000058 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
59 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000060 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 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000067 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 Stump11289f42009-09-09 15:08:12 +000070
Chris Lattner23b7eb62007-06-15 23:05:46 +000071 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 Stump11289f42009-09-09 15:08:12 +000074
Chris Lattner23b7eb62007-06-15 23:05:46 +000075 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
76 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000077}
78
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000079/// CreateHeaderMap - This method returns a HeaderMap for the specified
80/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner4ffe46c2007-12-17 18:34:53 +000081const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000082 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerf62f7582007-12-17 07:52:39 +000083 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000084 if (!HeaderMaps.empty()) {
85 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerf62f7582007-12-17 07:52:39 +000086 // Pointer equality comparison of FileEntries works because they are
87 // already uniqued by inode.
Mike Stump11289f42009-09-09 15:08:12 +000088 if (HeaderMaps[i].first == FE)
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000089 return HeaderMaps[i].second;
90 }
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattner5159f612010-11-23 08:35:12 +000092 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000093 HeaderMaps.push_back(std::make_pair(FE, HM));
94 return HM;
95 }
Mike Stump11289f42009-09-09 15:08:12 +000096
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000097 return 0;
98}
99
Chris Lattnerf62f7582007-12-17 07:52:39 +0000100//===----------------------------------------------------------------------===//
101// File lookup within a DirectoryLookup scope
102//===----------------------------------------------------------------------===//
103
Chris Lattner8d720d02007-12-17 17:57:27 +0000104/// getName - Return the directory or filename corresponding to this lookup
105/// object.
106const 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 Lattnerf62f7582007-12-17 07:52:39 +0000116/// LookupFile - Lookup the specified file in this search path, returning it
117/// if it exists or returning null if not.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000118const FileEntry *DirectoryLookup::LookupFile(
119 llvm::StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000120 HeaderSearch &HS,
121 llvm::SmallVectorImpl<char> *SearchPath,
122 llvm::SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerf62f7582007-12-17 07:52:39 +0000123 llvm::SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000124 if (isNormalDir()) {
125 // Concatenate the requested file onto the directory.
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000126 TmpDir = getDir()->getName();
127 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000128 if (SearchPath != NULL) {
129 llvm::StringRef SearchPathRef(getDir()->getName());
130 SearchPath->clear();
131 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
132 }
133 if (RelativePath != NULL) {
134 RelativePath->clear();
135 RelativePath->append(Filename.begin(), Filename.end());
136 }
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000137 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattner712e3872007-12-17 08:13:48 +0000138 }
Mike Stump11289f42009-09-09 15:08:12 +0000139
Chris Lattner712e3872007-12-17 08:13:48 +0000140 if (isFramework())
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000141 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath);
Mike Stump11289f42009-09-09 15:08:12 +0000142
Chris Lattner44bd21b2007-12-17 08:17:39 +0000143 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000144 const FileEntry * const Result = getHeaderMap()->LookupFile(
145 Filename, HS.getFileMgr());
146 if (Result) {
147 if (SearchPath != NULL) {
Douglas Gregor68dac462011-04-29 00:45:09 +0000148 llvm::StringRef SearchPathRef(getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000149 SearchPath->clear();
150 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
151 }
152 if (RelativePath != NULL) {
153 RelativePath->clear();
154 RelativePath->append(Filename.begin(), Filename.end());
155 }
156 }
157 return Result;
Chris Lattnerf62f7582007-12-17 07:52:39 +0000158}
159
160
Chris Lattner712e3872007-12-17 08:13:48 +0000161/// DoFrameworkLookup - Do a lookup of the specified file in the current
162/// DirectoryLookup, which is a framework directory.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000163const FileEntry *DirectoryLookup::DoFrameworkLookup(
164 llvm::StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000165 HeaderSearch &HS,
166 llvm::SmallVectorImpl<char> *SearchPath,
167 llvm::SmallVectorImpl<char> *RelativePath) const {
Chris Lattner712e3872007-12-17 08:13:48 +0000168 FileManager &FileMgr = HS.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000169
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000170 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000171 size_t SlashPos = Filename.find('/');
172 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000173
Chris Lattner712e3872007-12-17 08:13:48 +0000174 // Find out if this is the home for the specified framework, by checking
175 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump11289f42009-09-09 15:08:12 +0000176 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000177 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner712e3872007-12-17 08:13:48 +0000179 // If it is known and in some other directory, fail.
180 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Chris Lattner5ed76da2006-10-22 07:24:13 +0000181 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattner712e3872007-12-17 08:13:48 +0000183 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000184
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000185 // FrameworkName = "/System/Library/Frameworks/"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000186 llvm::SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000187 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000188 if (FrameworkName.empty() || FrameworkName.back() != '/')
189 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000190
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000191 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000192 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump11289f42009-09-09 15:08:12 +0000193
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000194 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
195 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000196
Chris Lattner712e3872007-12-17 08:13:48 +0000197 // If the cache entry is still unresolved, query to see if the cache entry is
198 // still unresolved. If so, check its existence now.
199 if (FrameworkDirCache == 0) {
200 HS.IncrementFrameworkLookupCount();
Mike Stump11289f42009-09-09 15:08:12 +0000201
Chris Lattner5ed76da2006-10-22 07:24:13 +0000202 // If the framework dir doesn't exist, we fail.
Chris Lattner712e3872007-12-17 08:13:48 +0000203 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000204 bool Exists;
205 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000206 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000207
Chris Lattner5ed76da2006-10-22 07:24:13 +0000208 // Otherwise, if it does, remember that this is the right direntry for this
209 // framework.
Chris Lattner712e3872007-12-17 08:13:48 +0000210 FrameworkDirCache = getFrameworkDir();
Chris Lattner5ed76da2006-10-22 07:24:13 +0000211 }
Mike Stump11289f42009-09-09 15:08:12 +0000212
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000213 if (RelativePath != NULL) {
214 RelativePath->clear();
215 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
216 }
217
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000218 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000219 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000220
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000221 FrameworkName += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000222
223 if (SearchPath != NULL) {
224 SearchPath->clear();
225 // Without trailing '/'.
226 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
227 }
228
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000229 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000230 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
231 /*openFile=*/true)) {
Chris Lattner577377e2006-10-20 04:55:45 +0000232 return FE;
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000233 }
Mike Stump11289f42009-09-09 15:08:12 +0000234
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000235 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000236 const char *Private = "Private";
Mike Stump11289f42009-09-09 15:08:12 +0000237 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000238 Private+strlen(Private));
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000239 if (SearchPath != NULL)
240 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
241 Private+strlen(Private));
242
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000243 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000244}
245
Chris Lattnerf62f7582007-12-17 07:52:39 +0000246
Chris Lattner712e3872007-12-17 08:13:48 +0000247//===----------------------------------------------------------------------===//
248// Header File Location.
249//===----------------------------------------------------------------------===//
250
251
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000252/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
253/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor618e64a2010-08-08 07:49:23 +0000254/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
255/// non-null, indicates where the #including file is, in case a relative search
256/// is needed.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000257const FileEntry *HeaderSearch::LookupFile(
258 llvm::StringRef Filename,
259 bool isAngled,
260 const DirectoryLookup *FromDir,
261 const DirectoryLookup *&CurDir,
262 const FileEntry *CurFileEnt,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000263 llvm::SmallVectorImpl<char> *SearchPath,
264 llvm::SmallVectorImpl<char> *RelativePath) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000265 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000266 if (llvm::sys::path::is_absolute(Filename)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000267 CurDir = 0;
268
269 // If this was an #include_next "/absolute/file", fail.
270 if (FromDir) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000271
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000272 if (SearchPath != NULL)
273 SearchPath->clear();
274 if (RelativePath != NULL) {
275 RelativePath->clear();
276 RelativePath->append(Filename.begin(), Filename.end());
277 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000278 // Otherwise, just return the file.
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000279 return FileMgr.getFile(Filename, /*openFile=*/true);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000280 }
Mike Stump11289f42009-09-09 15:08:12 +0000281
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000282 // Step #0, unless disabled, check to see if the file is in the #includer's
Douglas Gregor618e64a2010-08-08 07:49:23 +0000283 // directory. This has to be based on CurFileEnt, not CurDir, because
284 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerf62f7582007-12-17 07:52:39 +0000285 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
286 // This search is not done for <> headers.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000287 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
288 llvm::SmallString<1024> TmpDir;
289 // Concatenate the requested file onto the directory.
290 // FIXME: Portability. Filename concatenation should be in sys::Path.
291 TmpDir += CurFileEnt->getDir()->getName();
292 TmpDir.push_back('/');
293 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000294 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000295 // Leave CurDir unset.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000296 // This file is a system header or C++ unfriendly if the old file is.
297 //
298 // Note that the temporary 'DirInfo' is required here, as either call to
299 // getFileInfo could resize the vector and we don't want to rely on order
300 // of evaluation.
301 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000302 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000303 if (SearchPath != NULL) {
304 llvm::StringRef SearchPathRef(CurFileEnt->getDir()->getName());
305 SearchPath->clear();
306 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
307 }
308 if (RelativePath != NULL) {
309 RelativePath->clear();
310 RelativePath->append(Filename.begin(), Filename.end());
311 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000312 return FE;
313 }
314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000316 CurDir = 0;
317
318 // If this is a system #include, ignore the user #include locs.
Nico Weber3b1d1212011-05-24 04:31:14 +0000319 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000320
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000321 // If this is a #include_next request, start searching after the directory the
322 // file was found in.
323 if (FromDir)
324 i = FromDir-&SearchDirs[0];
Mike Stump11289f42009-09-09 15:08:12 +0000325
Chris Lattnerd4275422007-07-22 07:28:00 +0000326 // Cache all of the lookups performed by this method. Many headers are
327 // multiply included, and the "pragma once" optimization prevents them from
328 // being relex/pp'd, but they would still have to search through a
329 // (potentially huge) series of SearchDirs to find it.
330 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000331 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattnerd4275422007-07-22 07:28:00 +0000332
333 // If the entry has been previously looked up, the first value will be
334 // non-zero. If the value is equal to i (the start point of our search), then
335 // this is a matching hit.
336 if (CacheLookup.first == i+1) {
337 // Skip querying potentially lots of directories for this lookup.
338 i = CacheLookup.second;
339 } else {
340 // Otherwise, this is the first query, or the previous query didn't match
341 // our search start. We will fill in our found location below, so prime the
342 // start point value.
343 CacheLookup.first = i+1;
344 }
Mike Stump11289f42009-09-09 15:08:12 +0000345
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000346 // Check each directory in sequence to see if it contains this file.
347 for (; i != SearchDirs.size(); ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000348 const FileEntry *FE =
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000349 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
Chris Lattner712e3872007-12-17 08:13:48 +0000350 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattner712e3872007-12-17 08:13:48 +0000352 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000353
Chris Lattner712e3872007-12-17 08:13:48 +0000354 // This file is a system header or C++ unfriendly if the dir is.
355 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000356
Chris Lattner712e3872007-12-17 08:13:48 +0000357 // Remember this location for the next lookup we do.
358 CacheLookup.second = i;
359 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000360 }
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattnerd4275422007-07-22 07:28:00 +0000362 // Otherwise, didn't find it. Remember we didn't find this.
363 CacheLookup.second = SearchDirs.size();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000364 return 0;
365}
366
Chris Lattner63dd32b2006-10-20 04:42:40 +0000367/// LookupSubframeworkHeader - Look up a subframework for the specified
368/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
369/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
370/// is a subframework within Carbon.framework. If so, return the FileEntry
371/// for the designated file, otherwise return null.
372const FileEntry *HeaderSearch::
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000373LookupSubframeworkHeader(llvm::StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000374 const FileEntry *ContextFileEnt,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000375 llvm::SmallVectorImpl<char> *SearchPath,
376 llvm::SmallVectorImpl<char> *RelativePath) {
Chris Lattner12261882008-02-01 05:34:02 +0000377 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000378
Chris Lattner63dd32b2006-10-20 04:42:40 +0000379 // Framework names must have a '/' in the filename. Find it.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000380 size_t SlashPos = Filename.find('/');
381 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattner63dd32b2006-10-20 04:42:40 +0000383 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000384 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000385
Chris Lattner63dd32b2006-10-20 04:42:40 +0000386 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000387 const char *FrameworkPos = strstr(ContextName, ".framework/");
388 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000389 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000390
391 llvm::SmallString<1024> FrameworkName(ContextName,
Chris Lattner23b7eb62007-06-15 23:05:46 +0000392 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000393
Chris Lattner63dd32b2006-10-20 04:42:40 +0000394 // Append Frameworks/HIToolbox.framework/
395 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000396 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000397 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000398
Chris Lattner23b7eb62007-06-15 23:05:46 +0000399 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner8afa6de2010-11-21 09:55:08 +0000400 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000401
Chris Lattner5ed76da2006-10-22 07:24:13 +0000402 // Some other location?
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000403 if (CacheLookup.getValue() &&
404 CacheLookup.getKeyLength() == FrameworkName.size() &&
405 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
406 CacheLookup.getKeyLength()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000407 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Chris Lattner5ed76da2006-10-22 07:24:13 +0000409 // Cache subframework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000410 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000411 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattner5ed76da2006-10-22 07:24:13 +0000413 // If the framework dir doesn't exist, we fail.
Chris Lattner5159f612010-11-23 08:35:12 +0000414 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000415 if (Dir == 0) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000416
Chris Lattner5ed76da2006-10-22 07:24:13 +0000417 // Otherwise, if it does, remember that this is the right direntry for this
418 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000419 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattner577377e2006-10-20 04:55:45 +0000422 const FileEntry *FE = 0;
423
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000424 if (RelativePath != NULL) {
425 RelativePath->clear();
426 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
427 }
428
Chris Lattner63dd32b2006-10-20 04:42:40 +0000429 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000430 llvm::SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000431 HeadersFilename += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000432 if (SearchPath != NULL) {
433 SearchPath->clear();
434 // Without trailing '/'.
435 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
436 }
437
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000438 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000439 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattner63dd32b2006-10-20 04:42:40 +0000441 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000442 HeadersFilename = FrameworkName;
443 HeadersFilename += "PrivateHeaders/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000444 if (SearchPath != NULL) {
445 SearchPath->clear();
446 // Without trailing '/'.
447 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
448 }
449
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000450 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000451 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000452 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Chris Lattner577377e2006-10-20 04:55:45 +0000455 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000456 //
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000457 // Note that the temporary 'DirInfo' is required here, as either call to
458 // getFileInfo could resize the vector and we don't want to rely on order
459 // of evaluation.
460 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
461 getFileInfo(FE).DirInfo = DirInfo;
Chris Lattner577377e2006-10-20 04:55:45 +0000462 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000463}
464
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000465//===----------------------------------------------------------------------===//
466// File Info Management.
467//===----------------------------------------------------------------------===//
468
469
Steve Naroff3fa455a2009-04-24 20:03:17 +0000470/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000471/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000472HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000473 if (FE->getUID() >= FileInfo.size())
474 FileInfo.resize(FE->getUID()+1);
Douglas Gregor09b69892011-02-10 17:09:37 +0000475
476 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
477 if (ExternalSource && !HFI.Resolved) {
478 HFI = ExternalSource->GetHeaderFileInfo(FE);
479 HFI.Resolved = true;
480 }
481 return HFI;
Mike Stump11289f42009-09-09 15:08:12 +0000482}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000483
Douglas Gregor37aa4932011-05-04 00:14:37 +0000484bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
485 // Check if we've ever seen this file as a header.
486 if (File->getUID() >= FileInfo.size())
487 return false;
488
489 // Resolve header file info from the external source, if needed.
490 HeaderFileInfo &HFI = FileInfo[File->getUID()];
491 if (ExternalSource && !HFI.Resolved) {
492 HFI = ExternalSource->GetHeaderFileInfo(File);
493 HFI.Resolved = true;
494 }
495
496 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
497}
498
Steve Naroff3fa455a2009-04-24 20:03:17 +0000499void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
500 if (UID >= FileInfo.size())
501 FileInfo.resize(UID+1);
Douglas Gregor09b69892011-02-10 17:09:37 +0000502 HFI.Resolved = true;
Steve Naroff3fa455a2009-04-24 20:03:17 +0000503 FileInfo[UID] = HFI;
504}
505
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000506/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
507/// #include, #include_next, or #import directive. Return false if #including
508/// the file will have no effect or true if we should include it.
509bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
510 ++NumIncluded; // Count # of attempted #includes.
511
512 // Get information about this file.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000513 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump11289f42009-09-09 15:08:12 +0000514
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000515 // If this is a #import directive, check that we have not already imported
516 // this header.
517 if (isImport) {
518 // If this has already been imported, don't import it again.
519 FileInfo.isImport = true;
Mike Stump11289f42009-09-09 15:08:12 +0000520
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000521 // Has this already been #import'ed or #include'd?
522 if (FileInfo.NumIncludes) return false;
523 } else {
524 // Otherwise, if this is a #include of a file that was previously #import'd
525 // or if this is the second #include of a #pragma once file, ignore it.
526 if (FileInfo.isImport)
527 return false;
528 }
Mike Stump11289f42009-09-09 15:08:12 +0000529
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000530 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
531 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump11289f42009-09-09 15:08:12 +0000532 if (const IdentifierInfo *ControllingMacro
Douglas Gregor99734e72009-04-25 23:30:02 +0000533 = FileInfo.getControllingMacro(ExternalLookup))
534 if (ControllingMacro->hasMacroDefinition()) {
535 ++NumMultiIncludeFileOptzn;
536 return false;
537 }
Mike Stump11289f42009-09-09 15:08:12 +0000538
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000539 // Increment the number of times this file has been included.
540 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +0000541
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000542 return true;
543}
544
545