blob: 8a73fb956585dd7a2717cbf6e3bcc5274a3423ce [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"
Chandler Carruthcb381ea2011-12-09 01:33:57 +000016#include "clang/Lex/Lexer.h"
Douglas Gregora30cfe52011-11-11 19:10:28 +000017#include "clang/Basic/Diagnostic.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000018#include "clang/Basic/FileManager.h"
19#include "clang/Basic/IdentifierTable.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000020#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000021#include "llvm/Support/Path.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallString.h"
Ted Kremenekeabea452011-07-27 18:41:18 +000023#include "llvm/Support/Capacity.h"
Chris Lattner3daed522009-03-02 22:20:04 +000024#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Douglas Gregor8c5a7602009-04-25 23:30:02 +000027const IdentifierInfo *
28HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
29 if (ControllingMacro)
30 return ControllingMacro;
31
32 if (!ControllingMacroID || !External)
33 return 0;
34
35 ControllingMacro = External->GetIdentifier(ControllingMacroID);
36 return ControllingMacro;
37}
38
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000039ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
40
Douglas Gregor8e238062011-11-11 00:35:06 +000041HeaderSearch::HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags)
Douglas Gregora30cfe52011-11-11 19:10:28 +000042 : FileMgr(FM), Diags(Diags), FrameworkMap(64),
43 ModMap(FileMgr, *Diags.getClient())
Douglas Gregor8e238062011-11-11 00:35:06 +000044{
Nico Weber74a5fd82011-05-24 04:31:14 +000045 AngledDirIdx = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000046 SystemDirIdx = 0;
47 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000048
Douglas Gregor8c5a7602009-04-25 23:30:02 +000049 ExternalLookup = 0;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000050 ExternalSource = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000051 NumIncluded = 0;
52 NumMultiIncludeFileOptzn = 0;
53 NumFrameworkLookups = NumSubFrameworkLookups = 0;
54}
55
Chris Lattner822da612007-12-17 06:36:45 +000056HeaderSearch::~HeaderSearch() {
57 // Delete headermaps.
58 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
59 delete HeaderMaps[i].second;
60}
Mike Stump1eb44332009-09-09 15:08:12 +000061
Reid Spencer5f016e22007-07-11 17:01:13 +000062void HeaderSearch::PrintStats() {
63 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
64 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
65 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
66 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
67 NumOnceOnlyFiles += FileInfo[i].isImport;
68 if (MaxNumIncludes < FileInfo[i].NumIncludes)
69 MaxNumIncludes = FileInfo[i].NumIncludes;
70 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
71 }
72 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
73 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
74 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Reid Spencer5f016e22007-07-11 17:01:13 +000076 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
77 fprintf(stderr, " %d #includes skipped due to"
78 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Reid Spencer5f016e22007-07-11 17:01:13 +000080 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
81 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
82}
83
Chris Lattner822da612007-12-17 06:36:45 +000084/// CreateHeaderMap - This method returns a HeaderMap for the specified
85/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000086const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000087 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000088 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000089 if (!HeaderMaps.empty()) {
90 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000091 // Pointer equality comparison of FileEntries works because they are
92 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +000093 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +000094 return HeaderMaps[i].second;
95 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattner39b49bc2010-11-23 08:35:12 +000097 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattner822da612007-12-17 06:36:45 +000098 HeaderMaps.push_back(std::make_pair(FE, HM));
99 return HM;
100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Chris Lattner822da612007-12-17 06:36:45 +0000102 return 0;
103}
104
Douglas Gregor21cae202011-09-12 23:31:24 +0000105const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000106 Module *&Module,
Douglas Gregora4d36a62011-11-28 23:16:06 +0000107 std::string *ModuleFileName) {
108 Module = 0;
109
Douglas Gregor9a6da692011-09-12 20:41:59 +0000110 // If we don't have a module cache path, we can't do anything.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000111 if (ModuleCachePath.empty()) {
112 if (ModuleFileName)
113 ModuleFileName->clear();
Douglas Gregor9a6da692011-09-12 20:41:59 +0000114 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000115 }
116
Douglas Gregor21cae202011-09-12 23:31:24 +0000117 // Try to find the module path.
Douglas Gregor9a6da692011-09-12 20:41:59 +0000118 llvm::SmallString<256> FileName(ModuleCachePath);
119 llvm::sys::path::append(FileName, ModuleName + ".pcm");
Douglas Gregor6e975c42011-09-13 23:15:45 +0000120 if (ModuleFileName)
121 *ModuleFileName = FileName.str();
Douglas Gregora4d36a62011-11-28 23:16:06 +0000122
Douglas Gregorcf70d782011-11-12 00:05:07 +0000123 // Look in the module map to determine if there is a module by this name.
Douglas Gregora4d36a62011-11-28 23:16:06 +0000124 Module = ModMap.findModule(ModuleName);
Douglas Gregorcf70d782011-11-12 00:05:07 +0000125 if (!Module) {
126 // Look through the various header search paths to load any avaiable module
127 // maps, searching for a module map that describes this module.
128 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregora4d36a62011-11-28 23:16:06 +0000129 if (SearchDirs[Idx].isFramework()) {
130 // Search for or infer a module map for a framework.
131 llvm::SmallString<128> FrameworkDirName;
132 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
133 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
134 if (const DirectoryEntry *FrameworkDir
135 = FileMgr.getDirectory(FrameworkDirName)) {
136 Module = getFrameworkModule(ModuleName, FrameworkDir);
137 if (Module)
138 break;
139 }
140 }
141
142 // FIXME: Figure out how header maps and module maps will work together.
143
144 // Only deal with normal search directories.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000145 if (!SearchDirs[Idx].isNormalDir())
146 continue;
147
Douglas Gregor26697972011-11-12 00:22:19 +0000148 // Search for a module map file in this directory.
149 if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) {
150 // We just loaded a module map file; check whether the module is
151 // available now.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000152 Module = ModMap.findModule(ModuleName);
153 if (Module)
154 break;
155 }
Douglas Gregor26697972011-11-12 00:22:19 +0000156
Douglas Gregorcf70d782011-11-12 00:05:07 +0000157 // Search for a module map in a subdirectory with the same name as the
158 // module.
159 llvm::SmallString<128> NestedModuleMapDirName;
160 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
161 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
Douglas Gregor26697972011-11-12 00:22:19 +0000162 if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) {
163 // If we just loaded a module map file, look for the module again.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000164 Module = ModMap.findModule(ModuleName);
165 if (Module)
166 break;
167 }
168 }
169 }
170
Douglas Gregora4d36a62011-11-28 23:16:06 +0000171 // Look for the module file in the module cache.
172 // FIXME: If we didn't find a description of the module itself, should we
173 // even try to find the module in the cache?
174 return getFileMgr().getFile(FileName, /*OpenFile=*/false,
175 /*CacheFailure=*/false);
Douglas Gregor9a6da692011-09-12 20:41:59 +0000176}
177
Chris Lattnerdf772332007-12-17 07:52:39 +0000178//===----------------------------------------------------------------------===//
179// File lookup within a DirectoryLookup scope
180//===----------------------------------------------------------------------===//
181
Chris Lattner3af66a92007-12-17 17:57:27 +0000182/// getName - Return the directory or filename corresponding to this lookup
183/// object.
184const char *DirectoryLookup::getName() const {
185 if (isNormalDir())
186 return getDir()->getName();
187 if (isFramework())
188 return getFrameworkDir()->getName();
189 assert(isHeaderMap() && "Unknown DirectoryLookup");
190 return getHeaderMap()->getFileName();
191}
192
193
Chris Lattnerdf772332007-12-17 07:52:39 +0000194/// LookupFile - Lookup the specified file in this search path, returning it
195/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000196const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000197 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000198 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000200 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000201 Module **SuggestedModule) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000202 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000203 if (isNormalDir()) {
204 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000205 TmpDir = getDir()->getName();
206 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000207 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000209 SearchPath->clear();
210 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
211 }
212 if (RelativePath != NULL) {
213 RelativePath->clear();
214 RelativePath->append(Filename.begin(), Filename.end());
215 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000216
217 // If we have a module map that might map this header, load it and
218 // check whether we'll have a suggestion for a module.
219 if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) {
220 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
221 /*openFile=*/false);
222 if (!File)
223 return File;
224
225 // If there is a module that corresponds to this header,
226 // suggest it.
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000227 *SuggestedModule = HS.findModuleForHeader(File);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000228 return File;
229 }
230
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000231 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000232 }
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Chris Lattnerafded5b2007-12-17 08:13:48 +0000234 if (isFramework())
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000235 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000236 SuggestedModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000238 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000239 const FileEntry * const Result = getHeaderMap()->LookupFile(
240 Filename, HS.getFileMgr());
241 if (Result) {
242 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000243 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000244 SearchPath->clear();
245 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
246 }
247 if (RelativePath != NULL) {
248 RelativePath->clear();
249 RelativePath->append(Filename.begin(), Filename.end());
250 }
251 }
252 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000253}
254
255
Chris Lattnerafded5b2007-12-17 08:13:48 +0000256/// DoFrameworkLookup - Do a lookup of the specified file in the current
257/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000258const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000259 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000260 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000261 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000262 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000263 Module **SuggestedModule) const
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000264{
Chris Lattnerafded5b2007-12-17 08:13:48 +0000265 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000268 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000269 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattnerafded5b2007-12-17 08:13:48 +0000271 // Find out if this is the home for the specified framework, by checking
272 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000273 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000274 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattnerafded5b2007-12-17 08:13:48 +0000276 // If it is known and in some other directory, fail.
277 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattnerafded5b2007-12-17 08:13:48 +0000280 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 // FrameworkName = "/System/Library/Frameworks/"
283 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000284 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 if (FrameworkName.empty() || FrameworkName.back() != '/')
286 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Reid Spencer5f016e22007-07-11 17:01:13 +0000288 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000289 StringRef ModuleName(Filename.begin(), SlashPos);
290 FrameworkName += ModuleName;
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
293 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattnerafded5b2007-12-17 08:13:48 +0000295 // If the cache entry is still unresolved, query to see if the cache entry is
296 // still unresolved. If so, check its existence now.
297 if (FrameworkDirCache == 0) {
298 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000301 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000302 bool Exists;
303 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // Otherwise, if it does, remember that this is the right direntry for this
307 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000308 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Manuel Klimek74124942011-04-26 21:50:03 +0000311 if (RelativePath != NULL) {
312 RelativePath->clear();
313 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
314 }
315
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000316 // If we're allowed to look for modules, try to load or create the module
317 // corresponding to this framework.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000318 Module *Module = 0;
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000319 if (SuggestedModule) {
320 if (const DirectoryEntry *FrameworkDir
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000321 = FileMgr.getDirectory(FrameworkName))
322 Module = HS.getFrameworkModule(ModuleName, FrameworkDir);
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000323 }
324
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
326 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000329
330 if (SearchPath != NULL) {
331 SearchPath->clear();
332 // Without trailing '/'.
333 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
334 }
335
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000336 // Determine whether this is the module we're building or not.
Douglas Gregor09833922011-12-06 17:31:28 +0000337 bool AutomaticImport = Module;
Chris Lattnera1394812010-01-10 01:35:12 +0000338 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000339 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000340 /*openFile=*/!AutomaticImport)) {
341 if (AutomaticImport)
Douglas Gregor09833922011-12-06 17:31:28 +0000342 *SuggestedModule = HS.findModuleForHeader(FE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
347 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000348 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000350 if (SearchPath != NULL)
351 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
352 Private+strlen(Private));
353
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000354 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
355 /*openFile=*/!AutomaticImport);
356 if (FE && AutomaticImport)
Douglas Gregor09833922011-12-06 17:31:28 +0000357 *SuggestedModule = HS.findModuleForHeader(FE);
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000358 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000359}
360
Chris Lattnerdf772332007-12-17 07:52:39 +0000361
Chris Lattnerafded5b2007-12-17 08:13:48 +0000362//===----------------------------------------------------------------------===//
363// Header File Location.
364//===----------------------------------------------------------------------===//
365
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
368/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000369/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
370/// non-null, indicates where the #including file is, in case a relative search
371/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000372const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000373 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000374 bool isAngled,
375 const DirectoryLookup *FromDir,
376 const DirectoryLookup *&CurDir,
377 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000378 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000379 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000380 Module **SuggestedModule,
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000381 bool SkipCache)
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000382{
383 if (SuggestedModule)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000384 *SuggestedModule = 0;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000385
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000387 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 CurDir = 0;
389
390 // If this was an #include_next "/absolute/file", fail.
391 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Manuel Klimek74124942011-04-26 21:50:03 +0000393 if (SearchPath != NULL)
394 SearchPath->clear();
395 if (RelativePath != NULL) {
396 RelativePath->clear();
397 RelativePath->append(Filename.begin(), Filename.end());
398 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000400 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000403 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000404 // directory. This has to be based on CurFileEnt, not CurDir, because
405 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000406 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
407 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000408 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
409 llvm::SmallString<1024> TmpDir;
410 // Concatenate the requested file onto the directory.
411 // FIXME: Portability. Filename concatenation should be in sys::Path.
412 TmpDir += CurFileEnt->getDir()->getName();
413 TmpDir.push_back('/');
414 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000415 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000417 // This file is a system header or C++ unfriendly if the old file is.
418 //
419 // Note that the temporary 'DirInfo' is required here, as either call to
420 // getFileInfo could resize the vector and we don't want to rely on order
421 // of evaluation.
422 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000423 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000424 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000425 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000426 SearchPath->clear();
427 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
428 }
429 if (RelativePath != NULL) {
430 RelativePath->clear();
431 RelativePath->append(Filename.begin(), Filename.end());
432 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 return FE;
434 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 }
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 CurDir = 0;
438
439 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000440 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 // If this is a #include_next request, start searching after the directory the
443 // file was found in.
444 if (FromDir)
445 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner9960ae82007-07-22 07:28:00 +0000447 // Cache all of the lookups performed by this method. Many headers are
448 // multiply included, and the "pragma once" optimization prevents them from
449 // being relex/pp'd, but they would still have to search through a
450 // (potentially huge) series of SearchDirs to find it.
451 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000452 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000453
454 // If the entry has been previously looked up, the first value will be
455 // non-zero. If the value is equal to i (the start point of our search), then
456 // this is a matching hit.
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000457 if (!SkipCache && CacheLookup.first == i+1) {
Chris Lattner9960ae82007-07-22 07:28:00 +0000458 // Skip querying potentially lots of directories for this lookup.
459 i = CacheLookup.second;
460 } else {
461 // Otherwise, this is the first query, or the previous query didn't match
462 // our search start. We will fill in our found location below, so prime the
463 // start point value.
464 CacheLookup.first = i+1;
465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 // Check each directory in sequence to see if it contains this file.
468 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000469 const FileEntry *FE =
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000470 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000471 SuggestedModule);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000472 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Chris Lattnerafded5b2007-12-17 08:13:48 +0000474 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattnerafded5b2007-12-17 08:13:48 +0000476 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000477 HeaderFileInfo &HFI = getFileInfo(FE);
478 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000480 // If this file is found in a header map and uses the framework style of
481 // includes, then this header is part of a framework we're building.
482 if (CurDir->isIndexHeaderMap()) {
483 size_t SlashPos = Filename.find('/');
484 if (SlashPos != StringRef::npos) {
485 HFI.IndexHeaderMapHeader = 1;
486 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
487 SlashPos));
488 }
489 }
490
Chris Lattnerafded5b2007-12-17 08:13:48 +0000491 // Remember this location for the next lookup we do.
492 CacheLookup.second = i;
493 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000496 // If we are including a file with a quoted include "foo.h" from inside
497 // a header in a framework that is currently being built, and we couldn't
498 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
499 // "Foo" is the name of the framework in which the including header was found.
500 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
501 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
502 if (IncludingHFI.IndexHeaderMapHeader) {
503 llvm::SmallString<128> ScratchFilename;
504 ScratchFilename += IncludingHFI.Framework;
505 ScratchFilename += '/';
506 ScratchFilename += Filename;
507
508 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
509 FromDir, CurDir, CurFileEnt,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000510 SearchPath, RelativePath,
511 SuggestedModule);
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000512 std::pair<unsigned, unsigned> &CacheLookup
513 = LookupFileCache.GetOrCreateValue(Filename).getValue();
514 CacheLookup.second
515 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
516 return Result;
517 }
518 }
519
Chris Lattner9960ae82007-07-22 07:28:00 +0000520 // Otherwise, didn't find it. Remember we didn't find this.
521 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 return 0;
523}
524
525/// LookupSubframeworkHeader - Look up a subframework for the specified
526/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
527/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
528/// is a subframework within Carbon.framework. If so, return the FileEntry
529/// for the designated file, otherwise return null.
530const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000531LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000532 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000533 SmallVectorImpl<char> *SearchPath,
534 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000535 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 // Framework names must have a '/' in the filename. Find it.
Douglas Gregorefda0e82011-12-09 16:48:01 +0000538 // FIXME: Should we permit '\' on Windows?
Chris Lattnera1394812010-01-10 01:35:12 +0000539 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000540 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 // Look up the base framework name of the ContextFileEnt.
543 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 // If the context info wasn't a framework, couldn't be a subframework.
Douglas Gregorefda0e82011-12-09 16:48:01 +0000546 const unsigned DotFrameworkLen = 10;
547 const char *FrameworkPos = strstr(ContextName, ".framework");
548 if (FrameworkPos == 0 ||
549 (FrameworkPos[DotFrameworkLen] != '/' &&
550 FrameworkPos[DotFrameworkLen] != '\\'))
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000552
553 llvm::SmallString<1024> FrameworkName(ContextName,
Douglas Gregorefda0e82011-12-09 16:48:01 +0000554 FrameworkPos+DotFrameworkLen+1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000555
556 // Append Frameworks/HIToolbox.framework/
557 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000558 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 FrameworkName += ".framework/";
560
561 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000562 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 // Some other location?
565 if (CacheLookup.getValue() &&
566 CacheLookup.getKeyLength() == FrameworkName.size() &&
567 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
568 CacheLookup.getKeyLength()) != 0)
569 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 // Cache subframework.
572 if (CacheLookup.getValue() == 0) {
573 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000576 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 // Otherwise, if it does, remember that this is the right direntry for this
580 // framework.
581 CacheLookup.setValue(Dir);
582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 const FileEntry *FE = 0;
585
Manuel Klimek74124942011-04-26 21:50:03 +0000586 if (RelativePath != NULL) {
587 RelativePath->clear();
588 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
589 }
590
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
592 llvm::SmallString<1024> HeadersFilename(FrameworkName);
593 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000594 if (SearchPath != NULL) {
595 SearchPath->clear();
596 // Without trailing '/'.
597 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
598 }
599
Chris Lattnera1394812010-01-10 01:35:12 +0000600 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000601 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
604 HeadersFilename = FrameworkName;
605 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000606 if (SearchPath != NULL) {
607 SearchPath->clear();
608 // Without trailing '/'.
609 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
610 }
611
Chris Lattnera1394812010-01-10 01:35:12 +0000612 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000613 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 return 0;
615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000618 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000619 // Note that the temporary 'DirInfo' is required here, as either call to
620 // getFileInfo could resize the vector and we don't want to rely on order
621 // of evaluation.
622 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
623 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 return FE;
625}
626
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000627/// \brief Helper static function to normalize a path for injection into
628/// a synthetic header.
629/*static*/ std::string
630HeaderSearch::NormalizeDashIncludePath(StringRef File, FileManager &FileMgr) {
631 // Implicit include paths should be resolved relative to the current
632 // working directory first, and then use the regular header search
633 // mechanism. The proper way to handle this is to have the
634 // predefines buffer located at the current working directory, but
635 // it has no file entry. For now, workaround this by using an
636 // absolute path if we find the file here, and otherwise letting
637 // header search handle it.
638 llvm::SmallString<128> Path(File);
639 llvm::sys::fs::make_absolute(Path);
640 bool exists;
641 if (llvm::sys::fs::exists(Path.str(), exists) || !exists)
642 Path = File;
643 else if (exists)
644 FileMgr.getFile(File);
645
646 return Lexer::Stringify(Path.str());
647}
648
Reid Spencer5f016e22007-07-11 17:01:13 +0000649//===----------------------------------------------------------------------===//
650// File Info Management.
651//===----------------------------------------------------------------------===//
652
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000653/// \brief Merge the header file info provided by \p OtherHFI into the current
654/// header file info (\p HFI)
655static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
656 const HeaderFileInfo &OtherHFI) {
657 HFI.isImport |= OtherHFI.isImport;
658 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
659 HFI.NumIncludes += OtherHFI.NumIncludes;
660
661 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
662 HFI.ControllingMacro = OtherHFI.ControllingMacro;
663 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
664 }
665
666 if (OtherHFI.External) {
667 HFI.DirInfo = OtherHFI.DirInfo;
668 HFI.External = OtherHFI.External;
669 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
670 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000671
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000672 if (HFI.Framework.empty())
673 HFI.Framework = OtherHFI.Framework;
674
675 HFI.Resolved = true;
676}
677
Steve Naroff83d63c72009-04-24 20:03:17 +0000678/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000679/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000680HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 if (FE->getUID() >= FileInfo.size())
682 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000683
684 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000685 if (ExternalSource && !HFI.Resolved)
686 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000687 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000688}
Reid Spencer5f016e22007-07-11 17:01:13 +0000689
Douglas Gregordd3e5542011-05-04 00:14:37 +0000690bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
691 // Check if we've ever seen this file as a header.
692 if (File->getUID() >= FileInfo.size())
693 return false;
694
695 // Resolve header file info from the external source, if needed.
696 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000697 if (ExternalSource && !HFI.Resolved)
698 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregordd3e5542011-05-04 00:14:37 +0000699
700 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
701}
702
Steve Naroff83d63c72009-04-24 20:03:17 +0000703void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
704 if (UID >= FileInfo.size())
705 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000706 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000707 FileInfo[UID] = HFI;
708}
709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
711/// #include, #include_next, or #import directive. Return false if #including
712/// the file will have no effect or true if we should include it.
713bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
714 ++NumIncluded; // Count # of attempted #includes.
715
716 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000717 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 // If this is a #import directive, check that we have not already imported
720 // this header.
721 if (isImport) {
722 // If this has already been imported, don't import it again.
723 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 // Has this already been #import'ed or #include'd?
726 if (FileInfo.NumIncludes) return false;
727 } else {
728 // Otherwise, if this is a #include of a file that was previously #import'd
729 // or if this is the second #include of a #pragma once file, ignore it.
730 if (FileInfo.isImport)
731 return false;
732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
735 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000736 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000737 = FileInfo.getControllingMacro(ExternalLookup))
738 if (ControllingMacro->hasMacroDefinition()) {
739 ++NumMultiIncludeFileOptzn;
740 return false;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 // Increment the number of times this file has been included.
744 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 return true;
747}
748
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000749size_t HeaderSearch::getTotalMemory() const {
750 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000751 + llvm::capacity_in_bytes(FileInfo)
752 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000753 + LookupFileCache.getAllocator().getTotalMemory()
754 + FrameworkMap.getAllocator().getTotalMemory();
755}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000756
757StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
758 return FrameworkNames.GetOrCreateValue(Framework).getKey();
759}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000760
761bool HeaderSearch::hasModuleMap(StringRef FileName,
762 const DirectoryEntry *Root) {
763 llvm::SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
764
765 StringRef DirName = FileName;
766 do {
767 // Get the parent directory name.
768 DirName = llvm::sys::path::parent_path(DirName);
769 if (DirName.empty())
770 return false;
771
772 // Determine whether this directory exists.
773 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
774 if (!Dir)
775 return false;
776
Douglas Gregorcf70d782011-11-12 00:05:07 +0000777 // Try to load the module map file in this directory.
Douglas Gregor26697972011-11-12 00:22:19 +0000778 switch (loadModuleMapFile(Dir)) {
779 case LMM_NewlyLoaded:
780 case LMM_AlreadyLoaded:
Douglas Gregorcf70d782011-11-12 00:05:07 +0000781 // Success. All of the directories we stepped through inherit this module
782 // map file.
Douglas Gregora30cfe52011-11-11 19:10:28 +0000783 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
784 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
785
786 return true;
Douglas Gregor26697972011-11-12 00:22:19 +0000787
788 case LMM_NoDirectory:
789 case LMM_InvalidModuleMap:
790 break;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000791 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000792
Douglas Gregorcf70d782011-11-12 00:05:07 +0000793 // If we hit the top of our search, we're done.
794 if (Dir == Root)
795 return false;
796
Douglas Gregora30cfe52011-11-11 19:10:28 +0000797 // Keep track of all of the directories we checked, so we can mark them as
798 // having module maps if we eventually do find a module map.
799 FixUpDirectories.push_back(Dir);
800 } while (true);
801
802 return false;
803}
804
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000805Module *HeaderSearch::findModuleForHeader(const FileEntry *File) {
806 if (Module *Module = ModMap.findModuleForHeader(File))
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000807 return Module;
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000808
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000809 return 0;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000810}
811
Douglas Gregordb1cde72011-11-16 00:09:06 +0000812bool HeaderSearch::loadModuleMapFile(const FileEntry *File) {
813 const DirectoryEntry *Dir = File->getDir();
814
815 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
816 = DirectoryHasModuleMap.find(Dir);
817 if (KnownDir != DirectoryHasModuleMap.end())
818 return !KnownDir->second;
819
820 bool Result = ModMap.parseModuleMapFile(File);
Douglas Gregor4813442c2011-12-07 21:25:07 +0000821 if (!Result && llvm::sys::path::filename(File->getName()) == "module.map") {
822 // If the file we loaded was a module.map, look for the corresponding
823 // module_private.map.
824 llvm::SmallString<128> PrivateFilename(Dir->getName());
825 llvm::sys::path::append(PrivateFilename, "module_private.map");
826 if (const FileEntry *PrivateFile = FileMgr.getFile(PrivateFilename))
827 Result = ModMap.parseModuleMapFile(PrivateFile);
828 }
829
830 DirectoryHasModuleMap[Dir] = !Result;
Douglas Gregordb1cde72011-11-16 00:09:06 +0000831 return Result;
832}
833
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000834Module *HeaderSearch::getModule(StringRef Name, bool AllowSearch) {
835 if (Module *Module = ModMap.findModule(Name))
Douglas Gregordb1cde72011-11-16 00:09:06 +0000836 return Module;
837
838 if (!AllowSearch)
839 return 0;
840
841 for (unsigned I = 0, N = SearchDirs.size(); I != N; ++I) {
842 if (!SearchDirs[I].isNormalDir())
843 continue;
844
845 switch (loadModuleMapFile(SearchDirs[I].getDir())) {
846 case LMM_AlreadyLoaded:
847 case LMM_InvalidModuleMap:
848 case LMM_NoDirectory:
849 break;
850
851 case LMM_NewlyLoaded:
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000852 if (Module *Module = ModMap.findModule(Name))
Douglas Gregordb1cde72011-11-16 00:09:06 +0000853 return Module;
854 break;
855 }
856 }
857
858 return 0;
859}
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000860
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000861Module *HeaderSearch::getFrameworkModule(StringRef Name,
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000862 const DirectoryEntry *Dir) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000863 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000864 return Module;
865
866 // Try to load a module map file.
867 switch (loadModuleMapFile(Dir)) {
868 case LMM_InvalidModuleMap:
869 break;
870
871 case LMM_AlreadyLoaded:
872 case LMM_NoDirectory:
873 return 0;
874
875 case LMM_NewlyLoaded:
876 return ModMap.findModule(Name);
877 }
878
879 // Try to infer a module map.
Douglas Gregorac252a32011-12-06 19:39:29 +0000880 return ModMap.inferFrameworkModule(Name, Dir, /*Parent=*/0);
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000881}
882
Douglas Gregordb1cde72011-11-16 00:09:06 +0000883
Douglas Gregor26697972011-11-12 00:22:19 +0000884HeaderSearch::LoadModuleMapResult
885HeaderSearch::loadModuleMapFile(StringRef DirName) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000886 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
887 return loadModuleMapFile(Dir);
888
Douglas Gregor26697972011-11-12 00:22:19 +0000889 return LMM_NoDirectory;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000890}
891
Douglas Gregor26697972011-11-12 00:22:19 +0000892HeaderSearch::LoadModuleMapResult
893HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000894 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
895 = DirectoryHasModuleMap.find(Dir);
896 if (KnownDir != DirectoryHasModuleMap.end())
Douglas Gregor26697972011-11-12 00:22:19 +0000897 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000898
899 llvm::SmallString<128> ModuleMapFileName;
900 ModuleMapFileName += Dir->getName();
Douglas Gregor587986e2011-12-07 02:23:45 +0000901 unsigned ModuleMapDirNameLen = ModuleMapFileName.size();
Douglas Gregorcf70d782011-11-12 00:05:07 +0000902 llvm::sys::path::append(ModuleMapFileName, "module.map");
903 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) {
904 // We have found a module map file. Try to parse it.
Douglas Gregor587986e2011-12-07 02:23:45 +0000905 if (ModMap.parseModuleMapFile(ModuleMapFile)) {
906 // No suitable module map.
907 DirectoryHasModuleMap[Dir] = false;
908 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000909 }
Douglas Gregor587986e2011-12-07 02:23:45 +0000910
911 // This directory has a module map.
912 DirectoryHasModuleMap[Dir] = true;
913
914 // Check whether there is a private module map that we need to load as well.
915 ModuleMapFileName.erase(ModuleMapFileName.begin() + ModuleMapDirNameLen,
916 ModuleMapFileName.end());
917 llvm::sys::path::append(ModuleMapFileName, "module_private.map");
918 if (const FileEntry *PrivateModuleMapFile
919 = FileMgr.getFile(ModuleMapFileName)) {
920 if (ModMap.parseModuleMapFile(PrivateModuleMapFile)) {
921 // No suitable module map.
922 DirectoryHasModuleMap[Dir] = false;
923 return LMM_InvalidModuleMap;
924 }
925 }
926
927 return LMM_NewlyLoaded;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000928 }
929
930 // No suitable module map.
931 DirectoryHasModuleMap[Dir] = false;
Douglas Gregor26697972011-11-12 00:22:19 +0000932 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000933}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000934