blob: 931145a8d655e7084ca51453c32e26955df0dc9e [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"
Ted Kremenekeabea452011-07-27 18:41:18 +000021#include "llvm/Support/Capacity.h"
Chris Lattner3daed522009-03-02 22:20:04 +000022#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Douglas Gregor8c5a7602009-04-25 23:30:02 +000025const IdentifierInfo *
26HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
27 if (ControllingMacro)
28 return ControllingMacro;
29
30 if (!ControllingMacroID || !External)
31 return 0;
32
33 ControllingMacro = External->GetIdentifier(ControllingMacroID);
34 return ControllingMacro;
35}
36
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000037ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
38
Chris Lattner39b49bc2010-11-23 08:35:12 +000039HeaderSearch::HeaderSearch(FileManager &FM)
40 : FileMgr(FM), FrameworkMap(64) {
Nico Weber74a5fd82011-05-24 04:31:14 +000041 AngledDirIdx = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000042 SystemDirIdx = 0;
43 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Douglas Gregor8c5a7602009-04-25 23:30:02 +000045 ExternalLookup = 0;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000046 ExternalSource = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000047 NumIncluded = 0;
48 NumMultiIncludeFileOptzn = 0;
49 NumFrameworkLookups = NumSubFrameworkLookups = 0;
50}
51
Chris Lattner822da612007-12-17 06:36:45 +000052HeaderSearch::~HeaderSearch() {
53 // Delete headermaps.
54 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
55 delete HeaderMaps[i].second;
56}
Mike Stump1eb44332009-09-09 15:08:12 +000057
Reid Spencer5f016e22007-07-11 17:01:13 +000058void HeaderSearch::PrintStats() {
59 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
60 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
61 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
62 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
63 NumOnceOnlyFiles += FileInfo[i].isImport;
64 if (MaxNumIncludes < FileInfo[i].NumIncludes)
65 MaxNumIncludes = FileInfo[i].NumIncludes;
66 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
67 }
68 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
69 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
70 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000071
Reid Spencer5f016e22007-07-11 17:01:13 +000072 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
73 fprintf(stderr, " %d #includes skipped due to"
74 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Reid Spencer5f016e22007-07-11 17:01:13 +000076 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
77 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
78}
79
Chris Lattner822da612007-12-17 06:36:45 +000080/// CreateHeaderMap - This method returns a HeaderMap for the specified
81/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000082const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000083 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000084 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000085 if (!HeaderMaps.empty()) {
86 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000087 // Pointer equality comparison of FileEntries works because they are
88 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +000089 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +000090 return HeaderMaps[i].second;
91 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattner39b49bc2010-11-23 08:35:12 +000093 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattner822da612007-12-17 06:36:45 +000094 HeaderMaps.push_back(std::make_pair(FE, HM));
95 return HM;
96 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner822da612007-12-17 06:36:45 +000098 return 0;
99}
100
Douglas Gregor21cae202011-09-12 23:31:24 +0000101const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName,
Douglas Gregor6e975c42011-09-13 23:15:45 +0000102 std::string *ModuleFileName,
Douglas Gregor21cae202011-09-12 23:31:24 +0000103 std::string *UmbrellaHeader) {
Douglas Gregor9a6da692011-09-12 20:41:59 +0000104 // If we don't have a module cache path, we can't do anything.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000105 if (ModuleCachePath.empty()) {
106 if (ModuleFileName)
107 ModuleFileName->clear();
Douglas Gregor9a6da692011-09-12 20:41:59 +0000108 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000109 }
110
Douglas Gregor21cae202011-09-12 23:31:24 +0000111 // Try to find the module path.
Douglas Gregor9a6da692011-09-12 20:41:59 +0000112 llvm::SmallString<256> FileName(ModuleCachePath);
113 llvm::sys::path::append(FileName, ModuleName + ".pcm");
Douglas Gregor6e975c42011-09-13 23:15:45 +0000114 if (ModuleFileName)
115 *ModuleFileName = FileName.str();
116
117 if (const FileEntry *ModuleFile
118 = getFileMgr().getFile(FileName, /*OpenFile=*/false,
119 /*CacheFailure=*/false))
Douglas Gregor21cae202011-09-12 23:31:24 +0000120 return ModuleFile;
121
122 // We didn't find the module. If we're not supposed to look for an
123 // umbrella header, this is the end of the road.
124 if (!UmbrellaHeader)
125 return 0;
126
127 // Look in each of the framework directories for an umbrella header with
128 // the same name as the module.
129 // FIXME: We need a way for non-frameworks to provide umbrella headers.
130 llvm::SmallString<128> UmbrellaHeaderName;
131 UmbrellaHeaderName = ModuleName;
132 UmbrellaHeaderName += '/';
133 UmbrellaHeaderName += ModuleName;
134 UmbrellaHeaderName += ".h";
135 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
136 // Skip non-framework include paths
137 if (!SearchDirs[Idx].isFramework())
138 continue;
139
140 // Look for the umbrella header in this directory.
141 if (const FileEntry *HeaderFile
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000142 = SearchDirs[Idx].LookupFile(UmbrellaHeaderName, *this, 0, 0,
143 StringRef(), 0)) {
Douglas Gregor21cae202011-09-12 23:31:24 +0000144 *UmbrellaHeader = HeaderFile->getName();
145 return 0;
146 }
147 }
148
149 // We did not find an umbrella header. Clear out the UmbrellaHeader pointee
150 // so our caller knows that we failed.
151 UmbrellaHeader->clear();
152 return 0;
Douglas Gregor9a6da692011-09-12 20:41:59 +0000153}
154
Chris Lattnerdf772332007-12-17 07:52:39 +0000155//===----------------------------------------------------------------------===//
156// File lookup within a DirectoryLookup scope
157//===----------------------------------------------------------------------===//
158
Chris Lattner3af66a92007-12-17 17:57:27 +0000159/// getName - Return the directory or filename corresponding to this lookup
160/// object.
161const char *DirectoryLookup::getName() const {
162 if (isNormalDir())
163 return getDir()->getName();
164 if (isFramework())
165 return getFrameworkDir()->getName();
166 assert(isHeaderMap() && "Unknown DirectoryLookup");
167 return getHeaderMap()->getFileName();
168}
169
170
Chris Lattnerdf772332007-12-17 07:52:39 +0000171/// LookupFile - Lookup the specified file in this search path, returning it
172/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000173const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000174 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000175 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000176 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000177 SmallVectorImpl<char> *RelativePath,
178 StringRef BuildingModule,
179 StringRef *SuggestedModule) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000180 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000181 if (isNormalDir()) {
182 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000183 TmpDir = getDir()->getName();
184 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000185 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000186 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000187 SearchPath->clear();
188 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
189 }
190 if (RelativePath != NULL) {
191 RelativePath->clear();
192 RelativePath->append(Filename.begin(), Filename.end());
193 }
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000194 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Chris Lattnerafded5b2007-12-17 08:13:48 +0000197 if (isFramework())
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000198 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
199 BuildingModule, SuggestedModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000201 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000202 const FileEntry * const Result = getHeaderMap()->LookupFile(
203 Filename, HS.getFileMgr());
204 if (Result) {
205 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000206 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000207 SearchPath->clear();
208 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
209 }
210 if (RelativePath != NULL) {
211 RelativePath->clear();
212 RelativePath->append(Filename.begin(), Filename.end());
213 }
214 }
215 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000216}
217
218
Chris Lattnerafded5b2007-12-17 08:13:48 +0000219/// DoFrameworkLookup - Do a lookup of the specified file in the current
220/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000221const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000222 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000223 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000224 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000225 SmallVectorImpl<char> *RelativePath,
226 StringRef BuildingModule,
227 StringRef *SuggestedModule) const
228{
Chris Lattnerafded5b2007-12-17 08:13:48 +0000229 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000232 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000233 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Chris Lattnerafded5b2007-12-17 08:13:48 +0000235 // Find out if this is the home for the specified framework, by checking
236 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000237 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000238 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Chris Lattnerafded5b2007-12-17 08:13:48 +0000240 // If it is known and in some other directory, fail.
241 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Chris Lattnerafded5b2007-12-17 08:13:48 +0000244 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // FrameworkName = "/System/Library/Frameworks/"
247 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000248 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 if (FrameworkName.empty() || FrameworkName.back() != '/')
250 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000253 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
256 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattnerafded5b2007-12-17 08:13:48 +0000258 // If the cache entry is still unresolved, query to see if the cache entry is
259 // still unresolved. If so, check its existence now.
260 if (FrameworkDirCache == 0) {
261 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000264 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000265 bool Exists;
266 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // Otherwise, if it does, remember that this is the right direntry for this
270 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000271 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000272 }
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Manuel Klimek74124942011-04-26 21:50:03 +0000274 if (RelativePath != NULL) {
275 RelativePath->clear();
276 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
277 }
278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
280 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000283
284 if (SearchPath != NULL) {
285 SearchPath->clear();
286 // Without trailing '/'.
287 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
288 }
289
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000290 /// Determine whether this is the module we're building or not.
291 bool AutomaticImport = SuggestedModule &&
Douglas Gregor0fd787b2011-09-16 00:22:46 +0000292 (BuildingModule != StringRef(Filename.begin(), SlashPos)) &&
293 !Filename.substr(SlashPos + 1).startswith("..");
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000294
Chris Lattnera1394812010-01-10 01:35:12 +0000295 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000296 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000297 /*openFile=*/!AutomaticImport)) {
298 if (AutomaticImport)
299 *SuggestedModule = StringRef(Filename.begin(), SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
304 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000305 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000307 if (SearchPath != NULL)
308 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
309 Private+strlen(Private));
310
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000311 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
312 /*openFile=*/!AutomaticImport);
313 if (FE && AutomaticImport)
314 *SuggestedModule = StringRef(Filename.begin(), SlashPos);
315 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000316}
317
Chris Lattnerdf772332007-12-17 07:52:39 +0000318
Chris Lattnerafded5b2007-12-17 08:13:48 +0000319//===----------------------------------------------------------------------===//
320// Header File Location.
321//===----------------------------------------------------------------------===//
322
323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
325/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000326/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
327/// non-null, indicates where the #including file is, in case a relative search
328/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000329const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000330 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000331 bool isAngled,
332 const DirectoryLookup *FromDir,
333 const DirectoryLookup *&CurDir,
334 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000335 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000336 SmallVectorImpl<char> *RelativePath,
337 StringRef *SuggestedModule)
338{
339 if (SuggestedModule)
340 *SuggestedModule = StringRef();
341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000343 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 CurDir = 0;
345
346 // If this was an #include_next "/absolute/file", fail.
347 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Manuel Klimek74124942011-04-26 21:50:03 +0000349 if (SearchPath != NULL)
350 SearchPath->clear();
351 if (RelativePath != NULL) {
352 RelativePath->clear();
353 RelativePath->append(Filename.begin(), Filename.end());
354 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000356 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000359 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000360 // directory. This has to be based on CurFileEnt, not CurDir, because
361 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000362 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
363 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000364 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
365 llvm::SmallString<1024> TmpDir;
366 // Concatenate the requested file onto the directory.
367 // FIXME: Portability. Filename concatenation should be in sys::Path.
368 TmpDir += CurFileEnt->getDir()->getName();
369 TmpDir.push_back('/');
370 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000371 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000373 // This file is a system header or C++ unfriendly if the old file is.
374 //
375 // 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(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000379 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000380 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000381 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000382 SearchPath->clear();
383 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
384 }
385 if (RelativePath != NULL) {
386 RelativePath->clear();
387 RelativePath->append(Filename.begin(), Filename.end());
388 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 return FE;
390 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 CurDir = 0;
394
395 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000396 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 // If this is a #include_next request, start searching after the directory the
399 // file was found in.
400 if (FromDir)
401 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner9960ae82007-07-22 07:28:00 +0000403 // Cache all of the lookups performed by this method. Many headers are
404 // multiply included, and the "pragma once" optimization prevents them from
405 // being relex/pp'd, but they would still have to search through a
406 // (potentially huge) series of SearchDirs to find it.
407 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000408 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000409
410 // If the entry has been previously looked up, the first value will be
411 // non-zero. If the value is equal to i (the start point of our search), then
412 // this is a matching hit.
413 if (CacheLookup.first == i+1) {
414 // Skip querying potentially lots of directories for this lookup.
415 i = CacheLookup.second;
416 } else {
417 // Otherwise, this is the first query, or the previous query didn't match
418 // our search start. We will fill in our found location below, so prime the
419 // start point value.
420 CacheLookup.first = i+1;
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 // Check each directory in sequence to see if it contains this file.
424 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000425 const FileEntry *FE =
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000426 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
427 BuildingModule, SuggestedModule);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000428 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattnerafded5b2007-12-17 08:13:48 +0000430 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattnerafded5b2007-12-17 08:13:48 +0000432 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000433 HeaderFileInfo &HFI = getFileInfo(FE);
434 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000436 // If this file is found in a header map and uses the framework style of
437 // includes, then this header is part of a framework we're building.
438 if (CurDir->isIndexHeaderMap()) {
439 size_t SlashPos = Filename.find('/');
440 if (SlashPos != StringRef::npos) {
441 HFI.IndexHeaderMapHeader = 1;
442 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
443 SlashPos));
444 }
445 }
446
Chris Lattnerafded5b2007-12-17 08:13:48 +0000447 // Remember this location for the next lookup we do.
448 CacheLookup.second = i;
449 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 }
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000452 // If we are including a file with a quoted include "foo.h" from inside
453 // a header in a framework that is currently being built, and we couldn't
454 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
455 // "Foo" is the name of the framework in which the including header was found.
456 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
457 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
458 if (IncludingHFI.IndexHeaderMapHeader) {
459 llvm::SmallString<128> ScratchFilename;
460 ScratchFilename += IncludingHFI.Framework;
461 ScratchFilename += '/';
462 ScratchFilename += Filename;
463
464 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
465 FromDir, CurDir, CurFileEnt,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000466 SearchPath, RelativePath,
467 SuggestedModule);
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000468 std::pair<unsigned, unsigned> &CacheLookup
469 = LookupFileCache.GetOrCreateValue(Filename).getValue();
470 CacheLookup.second
471 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
472 return Result;
473 }
474 }
475
Chris Lattner9960ae82007-07-22 07:28:00 +0000476 // Otherwise, didn't find it. Remember we didn't find this.
477 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000478 return 0;
479}
480
481/// LookupSubframeworkHeader - Look up a subframework for the specified
482/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
483/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
484/// is a subframework within Carbon.framework. If so, return the FileEntry
485/// for the designated file, otherwise return null.
486const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000487LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000488 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000489 SmallVectorImpl<char> *SearchPath,
490 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000491 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000494 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000495 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 // Look up the base framework name of the ContextFileEnt.
498 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 // If the context info wasn't a framework, couldn't be a subframework.
501 const char *FrameworkPos = strstr(ContextName, ".framework/");
502 if (FrameworkPos == 0)
503 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
505 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 FrameworkPos+strlen(".framework/"));
507
508 // Append Frameworks/HIToolbox.framework/
509 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000510 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 FrameworkName += ".framework/";
512
513 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000514 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 // Some other location?
517 if (CacheLookup.getValue() &&
518 CacheLookup.getKeyLength() == FrameworkName.size() &&
519 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
520 CacheLookup.getKeyLength()) != 0)
521 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 // Cache subframework.
524 if (CacheLookup.getValue() == 0) {
525 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000528 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 // Otherwise, if it does, remember that this is the right direntry for this
532 // framework.
533 CacheLookup.setValue(Dir);
534 }
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 const FileEntry *FE = 0;
537
Manuel Klimek74124942011-04-26 21:50:03 +0000538 if (RelativePath != NULL) {
539 RelativePath->clear();
540 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
541 }
542
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
544 llvm::SmallString<1024> HeadersFilename(FrameworkName);
545 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000546 if (SearchPath != NULL) {
547 SearchPath->clear();
548 // Without trailing '/'.
549 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
550 }
551
Chris Lattnera1394812010-01-10 01:35:12 +0000552 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000553 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
556 HeadersFilename = FrameworkName;
557 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000558 if (SearchPath != NULL) {
559 SearchPath->clear();
560 // Without trailing '/'.
561 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
562 }
563
Chris Lattnera1394812010-01-10 01:35:12 +0000564 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000565 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 return 0;
567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000570 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000571 // Note that the temporary 'DirInfo' is required here, as either call to
572 // getFileInfo could resize the vector and we don't want to rely on order
573 // of evaluation.
574 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
575 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 return FE;
577}
578
579//===----------------------------------------------------------------------===//
580// File Info Management.
581//===----------------------------------------------------------------------===//
582
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000583/// \brief Merge the header file info provided by \p OtherHFI into the current
584/// header file info (\p HFI)
585static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
586 const HeaderFileInfo &OtherHFI) {
587 HFI.isImport |= OtherHFI.isImport;
588 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
589 HFI.NumIncludes += OtherHFI.NumIncludes;
590
591 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
592 HFI.ControllingMacro = OtherHFI.ControllingMacro;
593 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
594 }
595
596 if (OtherHFI.External) {
597 HFI.DirInfo = OtherHFI.DirInfo;
598 HFI.External = OtherHFI.External;
599 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
600 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000601
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000602 if (HFI.Framework.empty())
603 HFI.Framework = OtherHFI.Framework;
604
605 HFI.Resolved = true;
606}
607
Steve Naroff83d63c72009-04-24 20:03:17 +0000608/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000609/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000610HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 if (FE->getUID() >= FileInfo.size())
612 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000613
614 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000615 if (ExternalSource && !HFI.Resolved)
616 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000617 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000618}
Reid Spencer5f016e22007-07-11 17:01:13 +0000619
Douglas Gregordd3e5542011-05-04 00:14:37 +0000620bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
621 // Check if we've ever seen this file as a header.
622 if (File->getUID() >= FileInfo.size())
623 return false;
624
625 // Resolve header file info from the external source, if needed.
626 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000627 if (ExternalSource && !HFI.Resolved)
628 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregordd3e5542011-05-04 00:14:37 +0000629
630 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
631}
632
Steve Naroff83d63c72009-04-24 20:03:17 +0000633void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
634 if (UID >= FileInfo.size())
635 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000636 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000637 FileInfo[UID] = HFI;
638}
639
Reid Spencer5f016e22007-07-11 17:01:13 +0000640/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
641/// #include, #include_next, or #import directive. Return false if #including
642/// the file will have no effect or true if we should include it.
643bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
644 ++NumIncluded; // Count # of attempted #includes.
645
646 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000647 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 // If this is a #import directive, check that we have not already imported
650 // this header.
651 if (isImport) {
652 // If this has already been imported, don't import it again.
653 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 // Has this already been #import'ed or #include'd?
656 if (FileInfo.NumIncludes) return false;
657 } else {
658 // Otherwise, if this is a #include of a file that was previously #import'd
659 // or if this is the second #include of a #pragma once file, ignore it.
660 if (FileInfo.isImport)
661 return false;
662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Reid Spencer5f016e22007-07-11 17:01:13 +0000664 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
665 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000666 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000667 = FileInfo.getControllingMacro(ExternalLookup))
668 if (ControllingMacro->hasMacroDefinition()) {
669 ++NumMultiIncludeFileOptzn;
670 return false;
671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 // Increment the number of times this file has been included.
674 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 return true;
677}
678
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000679size_t HeaderSearch::getTotalMemory() const {
680 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000681 + llvm::capacity_in_bytes(FileInfo)
682 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000683 + LookupFileCache.getAllocator().getTotalMemory()
684 + FrameworkMap.getAllocator().getTotalMemory();
685}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000686
687StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
688 return FrameworkNames.GetOrCreateValue(Framework).getKey();
689}