blob: 1fe0240f4364e6ea372c37e000ad3ebb53a82921 [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) {
Chris Lattner641a0be2006-10-20 06:23:14 +000040 SystemDirIdx = 0;
41 NoCurDirSearch = false;
Mike Stump11289f42009-09-09 15:08:12 +000042
Douglas Gregor99734e72009-04-25 23:30:02 +000043 ExternalLookup = 0;
Douglas Gregor09b69892011-02-10 17:09:37 +000044 ExternalSource = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000045 NumIncluded = 0;
46 NumMultiIncludeFileOptzn = 0;
47 NumFrameworkLookups = NumSubFrameworkLookups = 0;
48}
49
Chris Lattnerc4ba38e2007-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 Stump11289f42009-09-09 15:08:12 +000055
Chris Lattner59a9ebd2006-10-18 05:34:33 +000056void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000057 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
58 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000059 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 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000066 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 Stump11289f42009-09-09 15:08:12 +000069
Chris Lattner23b7eb62007-06-15 23:05:46 +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 Stump11289f42009-09-09 15:08:12 +000073
Chris Lattner23b7eb62007-06-15 23:05:46 +000074 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
75 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000076}
77
Chris Lattnerc4ba38e2007-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 Lattner4ffe46c2007-12-17 18:34:53 +000080const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000081 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerf62f7582007-12-17 07:52:39 +000082 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000083 if (!HeaderMaps.empty()) {
84 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerf62f7582007-12-17 07:52:39 +000085 // Pointer equality comparison of FileEntries works because they are
86 // already uniqued by inode.
Mike Stump11289f42009-09-09 15:08:12 +000087 if (HeaderMaps[i].first == FE)
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000088 return HeaderMaps[i].second;
89 }
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattner5159f612010-11-23 08:35:12 +000091 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000092 HeaderMaps.push_back(std::make_pair(FE, HM));
93 return HM;
94 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000096 return 0;
97}
98
Chris Lattnerf62f7582007-12-17 07:52:39 +000099//===----------------------------------------------------------------------===//
100// File lookup within a DirectoryLookup scope
101//===----------------------------------------------------------------------===//
102
Chris Lattner8d720d02007-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 Lattnerf62f7582007-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.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000117const FileEntry *DirectoryLookup::LookupFile(
118 llvm::StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000119 HeaderSearch &HS,
120 llvm::SmallVectorImpl<char> *SearchPath,
121 llvm::SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerf62f7582007-12-17 07:52:39 +0000122 llvm::SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000123 if (isNormalDir()) {
124 // Concatenate the requested file onto the directory.
125 // FIXME: Portability. Filename concatenation should be in sys::Path.
126 TmpDir += getDir()->getName();
127 TmpDir.push_back('/');
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000128 TmpDir.append(Filename.begin(), Filename.end());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000129 if (SearchPath != NULL) {
130 llvm::StringRef SearchPathRef(getDir()->getName());
131 SearchPath->clear();
132 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
133 }
134 if (RelativePath != NULL) {
135 RelativePath->clear();
136 RelativePath->append(Filename.begin(), Filename.end());
137 }
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000138 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattner712e3872007-12-17 08:13:48 +0000139 }
Mike Stump11289f42009-09-09 15:08:12 +0000140
Chris Lattner712e3872007-12-17 08:13:48 +0000141 if (isFramework())
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000142 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath);
Mike Stump11289f42009-09-09 15:08:12 +0000143
Chris Lattner44bd21b2007-12-17 08:17:39 +0000144 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000145 const FileEntry * const Result = getHeaderMap()->LookupFile(
146 Filename, HS.getFileMgr());
147 if (Result) {
148 if (SearchPath != NULL) {
149 llvm::StringRef SearchPathRef(getDir()->getName());
150 SearchPath->clear();
151 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
152 }
153 if (RelativePath != NULL) {
154 RelativePath->clear();
155 RelativePath->append(Filename.begin(), Filename.end());
156 }
157 }
158 return Result;
Chris Lattnerf62f7582007-12-17 07:52:39 +0000159}
160
161
Chris Lattner712e3872007-12-17 08:13:48 +0000162/// DoFrameworkLookup - Do a lookup of the specified file in the current
163/// DirectoryLookup, which is a framework directory.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000164const FileEntry *DirectoryLookup::DoFrameworkLookup(
165 llvm::StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000166 HeaderSearch &HS,
167 llvm::SmallVectorImpl<char> *SearchPath,
168 llvm::SmallVectorImpl<char> *RelativePath) const {
Chris Lattner712e3872007-12-17 08:13:48 +0000169 FileManager &FileMgr = HS.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000170
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000171 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000172 size_t SlashPos = Filename.find('/');
173 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000174
Chris Lattner712e3872007-12-17 08:13:48 +0000175 // Find out if this is the home for the specified framework, by checking
176 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump11289f42009-09-09 15:08:12 +0000177 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000178 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000179
Chris Lattner712e3872007-12-17 08:13:48 +0000180 // If it is known and in some other directory, fail.
181 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Chris Lattner5ed76da2006-10-22 07:24:13 +0000182 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattner712e3872007-12-17 08:13:48 +0000184 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000186 // FrameworkName = "/System/Library/Frameworks/"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000187 llvm::SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000188 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000189 if (FrameworkName.empty() || FrameworkName.back() != '/')
190 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000191
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000192 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000193 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump11289f42009-09-09 15:08:12 +0000194
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000195 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
196 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000197
Chris Lattner712e3872007-12-17 08:13:48 +0000198 // If the cache entry is still unresolved, query to see if the cache entry is
199 // still unresolved. If so, check its existence now.
200 if (FrameworkDirCache == 0) {
201 HS.IncrementFrameworkLookupCount();
Mike Stump11289f42009-09-09 15:08:12 +0000202
Chris Lattner5ed76da2006-10-22 07:24:13 +0000203 // If the framework dir doesn't exist, we fail.
Chris Lattner712e3872007-12-17 08:13:48 +0000204 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000205 bool Exists;
206 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000207 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Chris Lattner5ed76da2006-10-22 07:24:13 +0000209 // Otherwise, if it does, remember that this is the right direntry for this
210 // framework.
Chris Lattner712e3872007-12-17 08:13:48 +0000211 FrameworkDirCache = getFrameworkDir();
Chris Lattner5ed76da2006-10-22 07:24:13 +0000212 }
Mike Stump11289f42009-09-09 15:08:12 +0000213
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000214 if (RelativePath != NULL) {
215 RelativePath->clear();
216 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
217 }
218
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000219 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000220 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000221
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000222 FrameworkName += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000223
224 if (SearchPath != NULL) {
225 SearchPath->clear();
226 // Without trailing '/'.
227 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
228 }
229
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000230 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000231 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
232 /*openFile=*/true)) {
Chris Lattner577377e2006-10-20 04:55:45 +0000233 return FE;
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000234 }
Mike Stump11289f42009-09-09 15:08:12 +0000235
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000236 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000237 const char *Private = "Private";
Mike Stump11289f42009-09-09 15:08:12 +0000238 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000239 Private+strlen(Private));
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000240 if (SearchPath != NULL)
241 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
242 Private+strlen(Private));
243
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000244 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000245}
246
Chris Lattnerf62f7582007-12-17 07:52:39 +0000247
Chris Lattner712e3872007-12-17 08:13:48 +0000248//===----------------------------------------------------------------------===//
249// Header File Location.
250//===----------------------------------------------------------------------===//
251
252
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000253/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
254/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor618e64a2010-08-08 07:49:23 +0000255/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
256/// non-null, indicates where the #including file is, in case a relative search
257/// is needed.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000258const FileEntry *HeaderSearch::LookupFile(
259 llvm::StringRef Filename,
260 bool isAngled,
261 const DirectoryLookup *FromDir,
262 const DirectoryLookup *&CurDir,
263 const FileEntry *CurFileEnt,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000264 llvm::SmallVectorImpl<char> *SearchPath,
265 llvm::SmallVectorImpl<char> *RelativePath) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000266 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000267 if (llvm::sys::path::is_absolute(Filename)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000268 CurDir = 0;
269
270 // If this was an #include_next "/absolute/file", fail.
271 if (FromDir) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000272
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000273 if (SearchPath != NULL)
274 SearchPath->clear();
275 if (RelativePath != NULL) {
276 RelativePath->clear();
277 RelativePath->append(Filename.begin(), Filename.end());
278 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000279 // Otherwise, just return the file.
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000280 return FileMgr.getFile(Filename, /*openFile=*/true);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000281 }
Mike Stump11289f42009-09-09 15:08:12 +0000282
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000283 // Step #0, unless disabled, check to see if the file is in the #includer's
Douglas Gregor618e64a2010-08-08 07:49:23 +0000284 // directory. This has to be based on CurFileEnt, not CurDir, because
285 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerf62f7582007-12-17 07:52:39 +0000286 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
287 // This search is not done for <> headers.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000288 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
289 llvm::SmallString<1024> TmpDir;
290 // Concatenate the requested file onto the directory.
291 // FIXME: Portability. Filename concatenation should be in sys::Path.
292 TmpDir += CurFileEnt->getDir()->getName();
293 TmpDir.push_back('/');
294 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000295 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000296 // Leave CurDir unset.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000297 // This file is a system header or C++ unfriendly if the old file is.
298 //
299 // Note that the temporary 'DirInfo' is required here, as either call to
300 // getFileInfo could resize the vector and we don't want to rely on order
301 // of evaluation.
302 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000303 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000304 if (SearchPath != NULL) {
305 llvm::StringRef SearchPathRef(CurFileEnt->getDir()->getName());
306 SearchPath->clear();
307 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
308 }
309 if (RelativePath != NULL) {
310 RelativePath->clear();
311 RelativePath->append(Filename.begin(), Filename.end());
312 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000313 return FE;
314 }
315 }
Mike Stump11289f42009-09-09 15:08:12 +0000316
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000317 CurDir = 0;
318
319 // If this is a system #include, ignore the user #include locs.
320 unsigned i = isAngled ? SystemDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000321
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000322 // If this is a #include_next request, start searching after the directory the
323 // file was found in.
324 if (FromDir)
325 i = FromDir-&SearchDirs[0];
Mike Stump11289f42009-09-09 15:08:12 +0000326
Chris Lattnerd4275422007-07-22 07:28:00 +0000327 // Cache all of the lookups performed by this method. Many headers are
328 // multiply included, and the "pragma once" optimization prevents them from
329 // being relex/pp'd, but they would still have to search through a
330 // (potentially huge) series of SearchDirs to find it.
331 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000332 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattnerd4275422007-07-22 07:28:00 +0000333
334 // If the entry has been previously looked up, the first value will be
335 // non-zero. If the value is equal to i (the start point of our search), then
336 // this is a matching hit.
337 if (CacheLookup.first == i+1) {
338 // Skip querying potentially lots of directories for this lookup.
339 i = CacheLookup.second;
340 } else {
341 // Otherwise, this is the first query, or the previous query didn't match
342 // our search start. We will fill in our found location below, so prime the
343 // start point value.
344 CacheLookup.first = i+1;
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000347 // Check each directory in sequence to see if it contains this file.
348 for (; i != SearchDirs.size(); ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000349 const FileEntry *FE =
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000350 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
Chris Lattner712e3872007-12-17 08:13:48 +0000351 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner712e3872007-12-17 08:13:48 +0000353 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattner712e3872007-12-17 08:13:48 +0000355 // This file is a system header or C++ unfriendly if the dir is.
356 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000357
Chris Lattner712e3872007-12-17 08:13:48 +0000358 // Remember this location for the next lookup we do.
359 CacheLookup.second = i;
360 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000361 }
Mike Stump11289f42009-09-09 15:08:12 +0000362
Chris Lattnerd4275422007-07-22 07:28:00 +0000363 // Otherwise, didn't find it. Remember we didn't find this.
364 CacheLookup.second = SearchDirs.size();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000365 return 0;
366}
367
Chris Lattner63dd32b2006-10-20 04:42:40 +0000368/// LookupSubframeworkHeader - Look up a subframework for the specified
369/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
370/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
371/// is a subframework within Carbon.framework. If so, return the FileEntry
372/// for the designated file, otherwise return null.
373const FileEntry *HeaderSearch::
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000374LookupSubframeworkHeader(llvm::StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000375 const FileEntry *ContextFileEnt,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000376 llvm::SmallVectorImpl<char> *SearchPath,
377 llvm::SmallVectorImpl<char> *RelativePath) {
Chris Lattner12261882008-02-01 05:34:02 +0000378 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000379
Chris Lattner63dd32b2006-10-20 04:42:40 +0000380 // Framework names must have a '/' in the filename. Find it.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000381 size_t SlashPos = Filename.find('/');
382 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000383
Chris Lattner63dd32b2006-10-20 04:42:40 +0000384 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000385 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattner63dd32b2006-10-20 04:42:40 +0000387 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000388 const char *FrameworkPos = strstr(ContextName, ".framework/");
389 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000390 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000391
392 llvm::SmallString<1024> FrameworkName(ContextName,
Chris Lattner23b7eb62007-06-15 23:05:46 +0000393 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000394
Chris Lattner63dd32b2006-10-20 04:42:40 +0000395 // Append Frameworks/HIToolbox.framework/
396 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000397 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000398 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000399
Chris Lattner23b7eb62007-06-15 23:05:46 +0000400 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner8afa6de2010-11-21 09:55:08 +0000401 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000402
Chris Lattner5ed76da2006-10-22 07:24:13 +0000403 // Some other location?
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000404 if (CacheLookup.getValue() &&
405 CacheLookup.getKeyLength() == FrameworkName.size() &&
406 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
407 CacheLookup.getKeyLength()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000408 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattner5ed76da2006-10-22 07:24:13 +0000410 // Cache subframework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000411 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000412 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattner5ed76da2006-10-22 07:24:13 +0000414 // If the framework dir doesn't exist, we fail.
Chris Lattner5159f612010-11-23 08:35:12 +0000415 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000416 if (Dir == 0) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattner5ed76da2006-10-22 07:24:13 +0000418 // Otherwise, if it does, remember that this is the right direntry for this
419 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000420 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000421 }
Mike Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattner577377e2006-10-20 04:55:45 +0000423 const FileEntry *FE = 0;
424
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000425 if (RelativePath != NULL) {
426 RelativePath->clear();
427 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
428 }
429
Chris Lattner63dd32b2006-10-20 04:42:40 +0000430 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000431 llvm::SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000432 HeadersFilename += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000433 if (SearchPath != NULL) {
434 SearchPath->clear();
435 // Without trailing '/'.
436 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
437 }
438
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000439 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000440 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump11289f42009-09-09 15:08:12 +0000441
Chris Lattner63dd32b2006-10-20 04:42:40 +0000442 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000443 HeadersFilename = FrameworkName;
444 HeadersFilename += "PrivateHeaders/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000445 if (SearchPath != NULL) {
446 SearchPath->clear();
447 // Without trailing '/'.
448 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
449 }
450
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000451 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000452 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000453 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000454 }
Mike Stump11289f42009-09-09 15:08:12 +0000455
Chris Lattner577377e2006-10-20 04:55:45 +0000456 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000457 //
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000458 // Note that the temporary 'DirInfo' is required here, as either call to
459 // getFileInfo could resize the vector and we don't want to rely on order
460 // of evaluation.
461 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
462 getFileInfo(FE).DirInfo = DirInfo;
Chris Lattner577377e2006-10-20 04:55:45 +0000463 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000464}
465
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000466//===----------------------------------------------------------------------===//
467// File Info Management.
468//===----------------------------------------------------------------------===//
469
470
Steve Naroff3fa455a2009-04-24 20:03:17 +0000471/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000472/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000473HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000474 if (FE->getUID() >= FileInfo.size())
475 FileInfo.resize(FE->getUID()+1);
Douglas Gregor09b69892011-02-10 17:09:37 +0000476
477 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
478 if (ExternalSource && !HFI.Resolved) {
479 HFI = ExternalSource->GetHeaderFileInfo(FE);
480 HFI.Resolved = true;
481 }
482 return HFI;
Mike Stump11289f42009-09-09 15:08:12 +0000483}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000484
Steve Naroff3fa455a2009-04-24 20:03:17 +0000485void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
486 if (UID >= FileInfo.size())
487 FileInfo.resize(UID+1);
Douglas Gregor09b69892011-02-10 17:09:37 +0000488 HFI.Resolved = true;
Steve Naroff3fa455a2009-04-24 20:03:17 +0000489 FileInfo[UID] = HFI;
490}
491
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000492/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
493/// #include, #include_next, or #import directive. Return false if #including
494/// the file will have no effect or true if we should include it.
495bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
496 ++NumIncluded; // Count # of attempted #includes.
497
498 // Get information about this file.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000499 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump11289f42009-09-09 15:08:12 +0000500
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000501 // If this is a #import directive, check that we have not already imported
502 // this header.
503 if (isImport) {
504 // If this has already been imported, don't import it again.
505 FileInfo.isImport = true;
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000507 // Has this already been #import'ed or #include'd?
508 if (FileInfo.NumIncludes) return false;
509 } else {
510 // Otherwise, if this is a #include of a file that was previously #import'd
511 // or if this is the second #include of a #pragma once file, ignore it.
512 if (FileInfo.isImport)
513 return false;
514 }
Mike Stump11289f42009-09-09 15:08:12 +0000515
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000516 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
517 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump11289f42009-09-09 15:08:12 +0000518 if (const IdentifierInfo *ControllingMacro
Douglas Gregor99734e72009-04-25 23:30:02 +0000519 = FileInfo.getControllingMacro(ExternalLookup))
520 if (ControllingMacro->hasMacroDefinition()) {
521 ++NumMultiIncludeFileOptzn;
522 return false;
523 }
Mike Stump11289f42009-09-09 15:08:12 +0000524
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000525 // Increment the number of times this file has been included.
526 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +0000527
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000528 return true;
529}
530
531