blob: 070daa0cd12627762eec5abee5ce8fc8d922ef3e [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 Gregor51f564f2011-12-31 04:05:44 +000041HeaderSearch::HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags,
42 const LangOptions &LangOpts)
Douglas Gregora30cfe52011-11-11 19:10:28 +000043 : FileMgr(FM), Diags(Diags), FrameworkMap(64),
Douglas Gregor51f564f2011-12-31 04:05:44 +000044 ModMap(FileMgr, *Diags.getClient(), LangOpts)
Douglas Gregor8e238062011-11-11 00:35:06 +000045{
Nico Weber74a5fd82011-05-24 04:31:14 +000046 AngledDirIdx = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000047 SystemDirIdx = 0;
48 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000049
Douglas Gregor8c5a7602009-04-25 23:30:02 +000050 ExternalLookup = 0;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000051 ExternalSource = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000052 NumIncluded = 0;
53 NumMultiIncludeFileOptzn = 0;
54 NumFrameworkLookups = NumSubFrameworkLookups = 0;
55}
56
Chris Lattner822da612007-12-17 06:36:45 +000057HeaderSearch::~HeaderSearch() {
58 // Delete headermaps.
59 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
60 delete HeaderMaps[i].second;
61}
Mike Stump1eb44332009-09-09 15:08:12 +000062
Reid Spencer5f016e22007-07-11 17:01:13 +000063void HeaderSearch::PrintStats() {
64 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
65 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
66 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
67 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
68 NumOnceOnlyFiles += FileInfo[i].isImport;
69 if (MaxNumIncludes < FileInfo[i].NumIncludes)
70 MaxNumIncludes = FileInfo[i].NumIncludes;
71 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
72 }
73 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
74 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
75 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000076
Reid Spencer5f016e22007-07-11 17:01:13 +000077 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
78 fprintf(stderr, " %d #includes skipped due to"
79 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Reid Spencer5f016e22007-07-11 17:01:13 +000081 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
82 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
83}
84
Chris Lattner822da612007-12-17 06:36:45 +000085/// CreateHeaderMap - This method returns a HeaderMap for the specified
86/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000087const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000088 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000089 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000090 if (!HeaderMaps.empty()) {
91 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000092 // Pointer equality comparison of FileEntries works because they are
93 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +000094 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +000095 return HeaderMaps[i].second;
96 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner39b49bc2010-11-23 08:35:12 +000098 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattner822da612007-12-17 06:36:45 +000099 HeaderMaps.push_back(std::make_pair(FE, HM));
100 return HM;
101 }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner822da612007-12-17 06:36:45 +0000103 return 0;
104}
105
Douglas Gregor21cae202011-09-12 23:31:24 +0000106const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000107 Module *&Module,
Douglas Gregora4d36a62011-11-28 23:16:06 +0000108 std::string *ModuleFileName) {
109 Module = 0;
110
Douglas Gregor9a6da692011-09-12 20:41:59 +0000111 // If we don't have a module cache path, we can't do anything.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000112 if (ModuleCachePath.empty()) {
113 if (ModuleFileName)
114 ModuleFileName->clear();
Douglas Gregor9a6da692011-09-12 20:41:59 +0000115 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000116 }
117
Douglas Gregor21cae202011-09-12 23:31:24 +0000118 // Try to find the module path.
Douglas Gregor9a6da692011-09-12 20:41:59 +0000119 llvm::SmallString<256> FileName(ModuleCachePath);
120 llvm::sys::path::append(FileName, ModuleName + ".pcm");
Douglas Gregor6e975c42011-09-13 23:15:45 +0000121 if (ModuleFileName)
122 *ModuleFileName = FileName.str();
Douglas Gregora4d36a62011-11-28 23:16:06 +0000123
Douglas Gregorcf70d782011-11-12 00:05:07 +0000124 // Look in the module map to determine if there is a module by this name.
Douglas Gregora4d36a62011-11-28 23:16:06 +0000125 Module = ModMap.findModule(ModuleName);
Douglas Gregorcf70d782011-11-12 00:05:07 +0000126 if (!Module) {
127 // Look through the various header search paths to load any avaiable module
128 // maps, searching for a module map that describes this module.
129 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregora4d36a62011-11-28 23:16:06 +0000130 if (SearchDirs[Idx].isFramework()) {
131 // Search for or infer a module map for a framework.
132 llvm::SmallString<128> FrameworkDirName;
133 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
134 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
135 if (const DirectoryEntry *FrameworkDir
136 = FileMgr.getDirectory(FrameworkDirName)) {
137 Module = getFrameworkModule(ModuleName, FrameworkDir);
138 if (Module)
139 break;
140 }
141 }
142
143 // FIXME: Figure out how header maps and module maps will work together.
144
145 // Only deal with normal search directories.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000146 if (!SearchDirs[Idx].isNormalDir())
147 continue;
148
Douglas Gregor26697972011-11-12 00:22:19 +0000149 // Search for a module map file in this directory.
150 if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) {
151 // We just loaded a module map file; check whether the module is
152 // available now.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000153 Module = ModMap.findModule(ModuleName);
154 if (Module)
155 break;
156 }
Douglas Gregor26697972011-11-12 00:22:19 +0000157
Douglas Gregorcf70d782011-11-12 00:05:07 +0000158 // Search for a module map in a subdirectory with the same name as the
159 // module.
160 llvm::SmallString<128> NestedModuleMapDirName;
161 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
162 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
Douglas Gregor26697972011-11-12 00:22:19 +0000163 if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) {
164 // If we just loaded a module map file, look for the module again.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000165 Module = ModMap.findModule(ModuleName);
166 if (Module)
167 break;
168 }
169 }
170 }
171
Douglas Gregora4d36a62011-11-28 23:16:06 +0000172 // Look for the module file in the module cache.
173 // FIXME: If we didn't find a description of the module itself, should we
174 // even try to find the module in the cache?
175 return getFileMgr().getFile(FileName, /*OpenFile=*/false,
176 /*CacheFailure=*/false);
Douglas Gregor9a6da692011-09-12 20:41:59 +0000177}
178
Chris Lattnerdf772332007-12-17 07:52:39 +0000179//===----------------------------------------------------------------------===//
180// File lookup within a DirectoryLookup scope
181//===----------------------------------------------------------------------===//
182
Chris Lattner3af66a92007-12-17 17:57:27 +0000183/// getName - Return the directory or filename corresponding to this lookup
184/// object.
185const char *DirectoryLookup::getName() const {
186 if (isNormalDir())
187 return getDir()->getName();
188 if (isFramework())
189 return getFrameworkDir()->getName();
190 assert(isHeaderMap() && "Unknown DirectoryLookup");
191 return getHeaderMap()->getFileName();
192}
193
194
Chris Lattnerdf772332007-12-17 07:52:39 +0000195/// LookupFile - Lookup the specified file in this search path, returning it
196/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000197const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000198 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000199 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000200 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000201 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000202 Module **SuggestedModule) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000203 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000204 if (isNormalDir()) {
205 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000206 TmpDir = getDir()->getName();
207 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000208 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000209 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000210 SearchPath->clear();
211 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
212 }
213 if (RelativePath != NULL) {
214 RelativePath->clear();
215 RelativePath->append(Filename.begin(), Filename.end());
216 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000217
218 // If we have a module map that might map this header, load it and
219 // check whether we'll have a suggestion for a module.
220 if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) {
221 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
222 /*openFile=*/false);
223 if (!File)
224 return File;
225
226 // If there is a module that corresponds to this header,
227 // suggest it.
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000228 *SuggestedModule = HS.findModuleForHeader(File);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000229 return File;
230 }
231
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000232 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000233 }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Chris Lattnerafded5b2007-12-17 08:13:48 +0000235 if (isFramework())
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000236 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000237 SuggestedModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000239 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000240 const FileEntry * const Result = getHeaderMap()->LookupFile(
241 Filename, HS.getFileMgr());
242 if (Result) {
243 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000244 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000245 SearchPath->clear();
246 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
247 }
248 if (RelativePath != NULL) {
249 RelativePath->clear();
250 RelativePath->append(Filename.begin(), Filename.end());
251 }
252 }
253 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000254}
255
256
Chris Lattnerafded5b2007-12-17 08:13:48 +0000257/// DoFrameworkLookup - Do a lookup of the specified file in the current
258/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000259const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000260 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000261 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000262 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000263 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000264 Module **SuggestedModule) const
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000265{
Chris Lattnerafded5b2007-12-17 08:13:48 +0000266 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000269 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000270 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattnerafded5b2007-12-17 08:13:48 +0000272 // Find out if this is the home for the specified framework, by checking
273 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000274 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000275 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattnerafded5b2007-12-17 08:13:48 +0000277 // If it is known and in some other directory, fail.
278 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Chris Lattnerafded5b2007-12-17 08:13:48 +0000281 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // FrameworkName = "/System/Library/Frameworks/"
284 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000285 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 if (FrameworkName.empty() || FrameworkName.back() != '/')
287 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000290 StringRef ModuleName(Filename.begin(), SlashPos);
291 FrameworkName += ModuleName;
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
294 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Chris Lattnerafded5b2007-12-17 08:13:48 +0000296 // If the cache entry is still unresolved, query to see if the cache entry is
297 // still unresolved. If so, check its existence now.
298 if (FrameworkDirCache == 0) {
299 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000302 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000303 bool Exists;
304 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // Otherwise, if it does, remember that this is the right direntry for this
308 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000309 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Manuel Klimek74124942011-04-26 21:50:03 +0000312 if (RelativePath != NULL) {
313 RelativePath->clear();
314 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
315 }
316
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000317 // If we're allowed to look for modules, try to load or create the module
318 // corresponding to this framework.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000319 Module *Module = 0;
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000320 if (SuggestedModule) {
321 if (const DirectoryEntry *FrameworkDir
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000322 = FileMgr.getDirectory(FrameworkName))
323 Module = HS.getFrameworkModule(ModuleName, FrameworkDir);
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000324 }
325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
327 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000330
331 if (SearchPath != NULL) {
332 SearchPath->clear();
333 // Without trailing '/'.
334 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
335 }
336
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000337 // Determine whether this is the module we're building or not.
Douglas Gregor09833922011-12-06 17:31:28 +0000338 bool AutomaticImport = Module;
Chris Lattnera1394812010-01-10 01:35:12 +0000339 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000340 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000341 /*openFile=*/!AutomaticImport)) {
342 if (AutomaticImport)
Douglas Gregor09833922011-12-06 17:31:28 +0000343 *SuggestedModule = HS.findModuleForHeader(FE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
348 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000349 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000351 if (SearchPath != NULL)
352 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
353 Private+strlen(Private));
354
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000355 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
356 /*openFile=*/!AutomaticImport);
357 if (FE && AutomaticImport)
Douglas Gregor09833922011-12-06 17:31:28 +0000358 *SuggestedModule = HS.findModuleForHeader(FE);
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000359 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000360}
361
Chris Lattnerdf772332007-12-17 07:52:39 +0000362
Chris Lattnerafded5b2007-12-17 08:13:48 +0000363//===----------------------------------------------------------------------===//
364// Header File Location.
365//===----------------------------------------------------------------------===//
366
367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
369/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000370/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
371/// non-null, indicates where the #including file is, in case a relative search
372/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000373const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000374 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000375 bool isAngled,
376 const DirectoryLookup *FromDir,
377 const DirectoryLookup *&CurDir,
378 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000379 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000380 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000381 Module **SuggestedModule,
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000382 bool SkipCache)
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000383{
384 if (SuggestedModule)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000385 *SuggestedModule = 0;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000386
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000388 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 CurDir = 0;
390
391 // If this was an #include_next "/absolute/file", fail.
392 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Manuel Klimek74124942011-04-26 21:50:03 +0000394 if (SearchPath != NULL)
395 SearchPath->clear();
396 if (RelativePath != NULL) {
397 RelativePath->clear();
398 RelativePath->append(Filename.begin(), Filename.end());
399 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000401 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000404 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000405 // directory. This has to be based on CurFileEnt, not CurDir, because
406 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000407 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
408 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000409 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
410 llvm::SmallString<1024> TmpDir;
411 // Concatenate the requested file onto the directory.
412 // FIXME: Portability. Filename concatenation should be in sys::Path.
413 TmpDir += CurFileEnt->getDir()->getName();
414 TmpDir.push_back('/');
415 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000416 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000418 // This file is a system header or C++ unfriendly if the old file is.
419 //
420 // Note that the temporary 'DirInfo' is required here, as either call to
421 // getFileInfo could resize the vector and we don't want to rely on order
422 // of evaluation.
423 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000424 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000425 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000426 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000427 SearchPath->clear();
428 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
429 }
430 if (RelativePath != NULL) {
431 RelativePath->clear();
432 RelativePath->append(Filename.begin(), Filename.end());
433 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 return FE;
435 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 }
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 CurDir = 0;
439
440 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000441 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 // If this is a #include_next request, start searching after the directory the
444 // file was found in.
445 if (FromDir)
446 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner9960ae82007-07-22 07:28:00 +0000448 // Cache all of the lookups performed by this method. Many headers are
449 // multiply included, and the "pragma once" optimization prevents them from
450 // being relex/pp'd, but they would still have to search through a
451 // (potentially huge) series of SearchDirs to find it.
452 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000453 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000454
455 // If the entry has been previously looked up, the first value will be
456 // non-zero. If the value is equal to i (the start point of our search), then
457 // this is a matching hit.
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000458 if (!SkipCache && CacheLookup.first == i+1) {
Chris Lattner9960ae82007-07-22 07:28:00 +0000459 // Skip querying potentially lots of directories for this lookup.
460 i = CacheLookup.second;
461 } else {
462 // Otherwise, this is the first query, or the previous query didn't match
463 // our search start. We will fill in our found location below, so prime the
464 // start point value.
465 CacheLookup.first = i+1;
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 // Check each directory in sequence to see if it contains this file.
469 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000470 const FileEntry *FE =
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000471 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000472 SuggestedModule);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000473 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattnerafded5b2007-12-17 08:13:48 +0000475 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattnerafded5b2007-12-17 08:13:48 +0000477 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000478 HeaderFileInfo &HFI = getFileInfo(FE);
479 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000481 // If this file is found in a header map and uses the framework style of
482 // includes, then this header is part of a framework we're building.
483 if (CurDir->isIndexHeaderMap()) {
484 size_t SlashPos = Filename.find('/');
485 if (SlashPos != StringRef::npos) {
486 HFI.IndexHeaderMapHeader = 1;
487 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
488 SlashPos));
489 }
490 }
491
Chris Lattnerafded5b2007-12-17 08:13:48 +0000492 // Remember this location for the next lookup we do.
493 CacheLookup.second = i;
494 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000497 // If we are including a file with a quoted include "foo.h" from inside
498 // a header in a framework that is currently being built, and we couldn't
499 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
500 // "Foo" is the name of the framework in which the including header was found.
501 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
502 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
503 if (IncludingHFI.IndexHeaderMapHeader) {
504 llvm::SmallString<128> ScratchFilename;
505 ScratchFilename += IncludingHFI.Framework;
506 ScratchFilename += '/';
507 ScratchFilename += Filename;
508
509 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
510 FromDir, CurDir, CurFileEnt,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000511 SearchPath, RelativePath,
512 SuggestedModule);
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000513 std::pair<unsigned, unsigned> &CacheLookup
514 = LookupFileCache.GetOrCreateValue(Filename).getValue();
515 CacheLookup.second
516 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
517 return Result;
518 }
519 }
520
Chris Lattner9960ae82007-07-22 07:28:00 +0000521 // Otherwise, didn't find it. Remember we didn't find this.
522 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 return 0;
524}
525
526/// LookupSubframeworkHeader - Look up a subframework for the specified
527/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
528/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
529/// is a subframework within Carbon.framework. If so, return the FileEntry
530/// for the designated file, otherwise return null.
531const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000532LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000533 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000534 SmallVectorImpl<char> *SearchPath,
535 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000536 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 // Framework names must have a '/' in the filename. Find it.
Douglas Gregorefda0e82011-12-09 16:48:01 +0000539 // FIXME: Should we permit '\' on Windows?
Chris Lattnera1394812010-01-10 01:35:12 +0000540 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000541 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 // Look up the base framework name of the ContextFileEnt.
544 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 // If the context info wasn't a framework, couldn't be a subframework.
Douglas Gregorefda0e82011-12-09 16:48:01 +0000547 const unsigned DotFrameworkLen = 10;
548 const char *FrameworkPos = strstr(ContextName, ".framework");
549 if (FrameworkPos == 0 ||
550 (FrameworkPos[DotFrameworkLen] != '/' &&
551 FrameworkPos[DotFrameworkLen] != '\\'))
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
554 llvm::SmallString<1024> FrameworkName(ContextName,
Douglas Gregorefda0e82011-12-09 16:48:01 +0000555 FrameworkPos+DotFrameworkLen+1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000556
557 // Append Frameworks/HIToolbox.framework/
558 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000559 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 FrameworkName += ".framework/";
561
562 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000563 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 // Some other location?
566 if (CacheLookup.getValue() &&
567 CacheLookup.getKeyLength() == FrameworkName.size() &&
568 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
569 CacheLookup.getKeyLength()) != 0)
570 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 // Cache subframework.
573 if (CacheLookup.getValue() == 0) {
574 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000577 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 // Otherwise, if it does, remember that this is the right direntry for this
581 // framework.
582 CacheLookup.setValue(Dir);
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 const FileEntry *FE = 0;
586
Manuel Klimek74124942011-04-26 21:50:03 +0000587 if (RelativePath != NULL) {
588 RelativePath->clear();
589 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
590 }
591
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
593 llvm::SmallString<1024> HeadersFilename(FrameworkName);
594 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000595 if (SearchPath != NULL) {
596 SearchPath->clear();
597 // Without trailing '/'.
598 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
599 }
600
Chris Lattnera1394812010-01-10 01:35:12 +0000601 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000602 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
605 HeadersFilename = FrameworkName;
606 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000607 if (SearchPath != NULL) {
608 SearchPath->clear();
609 // Without trailing '/'.
610 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
611 }
612
Chris Lattnera1394812010-01-10 01:35:12 +0000613 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000614 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 return 0;
616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000619 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000620 // Note that the temporary 'DirInfo' is required here, as either call to
621 // getFileInfo could resize the vector and we don't want to rely on order
622 // of evaluation.
623 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
624 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 return FE;
626}
627
Chandler Carruthcb381ea2011-12-09 01:33:57 +0000628/// \brief Helper static function to normalize a path for injection into
629/// a synthetic header.
630/*static*/ std::string
631HeaderSearch::NormalizeDashIncludePath(StringRef File, FileManager &FileMgr) {
632 // Implicit include paths should be resolved relative to the current
633 // working directory first, and then use the regular header search
634 // mechanism. The proper way to handle this is to have the
635 // predefines buffer located at the current working directory, but
636 // it has no file entry. For now, workaround this by using an
637 // absolute path if we find the file here, and otherwise letting
638 // header search handle it.
639 llvm::SmallString<128> Path(File);
640 llvm::sys::fs::make_absolute(Path);
641 bool exists;
642 if (llvm::sys::fs::exists(Path.str(), exists) || !exists)
643 Path = File;
644 else if (exists)
645 FileMgr.getFile(File);
646
647 return Lexer::Stringify(Path.str());
648}
649
Reid Spencer5f016e22007-07-11 17:01:13 +0000650//===----------------------------------------------------------------------===//
651// File Info Management.
652//===----------------------------------------------------------------------===//
653
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000654/// \brief Merge the header file info provided by \p OtherHFI into the current
655/// header file info (\p HFI)
656static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
657 const HeaderFileInfo &OtherHFI) {
658 HFI.isImport |= OtherHFI.isImport;
659 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
660 HFI.NumIncludes += OtherHFI.NumIncludes;
661
662 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
663 HFI.ControllingMacro = OtherHFI.ControllingMacro;
664 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
665 }
666
667 if (OtherHFI.External) {
668 HFI.DirInfo = OtherHFI.DirInfo;
669 HFI.External = OtherHFI.External;
670 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
671 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000672
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000673 if (HFI.Framework.empty())
674 HFI.Framework = OtherHFI.Framework;
675
676 HFI.Resolved = true;
677}
678
Steve Naroff83d63c72009-04-24 20:03:17 +0000679/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000680/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000681HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 if (FE->getUID() >= FileInfo.size())
683 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000684
685 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000686 if (ExternalSource && !HFI.Resolved)
687 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000688 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000689}
Reid Spencer5f016e22007-07-11 17:01:13 +0000690
Douglas Gregordd3e5542011-05-04 00:14:37 +0000691bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
692 // Check if we've ever seen this file as a header.
693 if (File->getUID() >= FileInfo.size())
694 return false;
695
696 // Resolve header file info from the external source, if needed.
697 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000698 if (ExternalSource && !HFI.Resolved)
699 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregordd3e5542011-05-04 00:14:37 +0000700
701 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
702}
703
Steve Naroff83d63c72009-04-24 20:03:17 +0000704void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
705 if (UID >= FileInfo.size())
706 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000707 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000708 FileInfo[UID] = HFI;
709}
710
Reid Spencer5f016e22007-07-11 17:01:13 +0000711/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
712/// #include, #include_next, or #import directive. Return false if #including
713/// the file will have no effect or true if we should include it.
714bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
715 ++NumIncluded; // Count # of attempted #includes.
716
717 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000718 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 // If this is a #import directive, check that we have not already imported
721 // this header.
722 if (isImport) {
723 // If this has already been imported, don't import it again.
724 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 // Has this already been #import'ed or #include'd?
727 if (FileInfo.NumIncludes) return false;
728 } else {
729 // Otherwise, if this is a #include of a file that was previously #import'd
730 // or if this is the second #include of a #pragma once file, ignore it.
731 if (FileInfo.isImport)
732 return false;
733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
736 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000737 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000738 = FileInfo.getControllingMacro(ExternalLookup))
739 if (ControllingMacro->hasMacroDefinition()) {
740 ++NumMultiIncludeFileOptzn;
741 return false;
742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 // Increment the number of times this file has been included.
745 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 return true;
748}
749
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000750size_t HeaderSearch::getTotalMemory() const {
751 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000752 + llvm::capacity_in_bytes(FileInfo)
753 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000754 + LookupFileCache.getAllocator().getTotalMemory()
755 + FrameworkMap.getAllocator().getTotalMemory();
756}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000757
758StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
759 return FrameworkNames.GetOrCreateValue(Framework).getKey();
760}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000761
762bool HeaderSearch::hasModuleMap(StringRef FileName,
763 const DirectoryEntry *Root) {
764 llvm::SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
765
766 StringRef DirName = FileName;
767 do {
768 // Get the parent directory name.
769 DirName = llvm::sys::path::parent_path(DirName);
770 if (DirName.empty())
771 return false;
772
773 // Determine whether this directory exists.
774 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
775 if (!Dir)
776 return false;
777
Douglas Gregorcf70d782011-11-12 00:05:07 +0000778 // Try to load the module map file in this directory.
Douglas Gregor26697972011-11-12 00:22:19 +0000779 switch (loadModuleMapFile(Dir)) {
780 case LMM_NewlyLoaded:
781 case LMM_AlreadyLoaded:
Douglas Gregorcf70d782011-11-12 00:05:07 +0000782 // Success. All of the directories we stepped through inherit this module
783 // map file.
Douglas Gregora30cfe52011-11-11 19:10:28 +0000784 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
785 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
786
787 return true;
Douglas Gregor26697972011-11-12 00:22:19 +0000788
789 case LMM_NoDirectory:
790 case LMM_InvalidModuleMap:
791 break;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000792 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000793
Douglas Gregorcf70d782011-11-12 00:05:07 +0000794 // If we hit the top of our search, we're done.
795 if (Dir == Root)
796 return false;
797
Douglas Gregora30cfe52011-11-11 19:10:28 +0000798 // Keep track of all of the directories we checked, so we can mark them as
799 // having module maps if we eventually do find a module map.
800 FixUpDirectories.push_back(Dir);
801 } while (true);
802
803 return false;
804}
805
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000806Module *HeaderSearch::findModuleForHeader(const FileEntry *File) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000807 if (Module *Mod = ModMap.findModuleForHeader(File))
808 return Mod;
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000809
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000810 return 0;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000811}
812
Douglas Gregordb1cde72011-11-16 00:09:06 +0000813bool HeaderSearch::loadModuleMapFile(const FileEntry *File) {
814 const DirectoryEntry *Dir = File->getDir();
815
816 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
817 = DirectoryHasModuleMap.find(Dir);
818 if (KnownDir != DirectoryHasModuleMap.end())
819 return !KnownDir->second;
820
821 bool Result = ModMap.parseModuleMapFile(File);
Douglas Gregor4813442c2011-12-07 21:25:07 +0000822 if (!Result && llvm::sys::path::filename(File->getName()) == "module.map") {
823 // If the file we loaded was a module.map, look for the corresponding
824 // module_private.map.
825 llvm::SmallString<128> PrivateFilename(Dir->getName());
826 llvm::sys::path::append(PrivateFilename, "module_private.map");
827 if (const FileEntry *PrivateFile = FileMgr.getFile(PrivateFilename))
828 Result = ModMap.parseModuleMapFile(PrivateFile);
829 }
830
831 DirectoryHasModuleMap[Dir] = !Result;
Douglas Gregordb1cde72011-11-16 00:09:06 +0000832 return Result;
833}
834
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000835Module *HeaderSearch::getModule(StringRef Name, bool AllowSearch) {
836 if (Module *Module = ModMap.findModule(Name))
Douglas Gregordb1cde72011-11-16 00:09:06 +0000837 return Module;
838
839 if (!AllowSearch)
840 return 0;
841
842 for (unsigned I = 0, N = SearchDirs.size(); I != N; ++I) {
843 if (!SearchDirs[I].isNormalDir())
844 continue;
845
846 switch (loadModuleMapFile(SearchDirs[I].getDir())) {
847 case LMM_AlreadyLoaded:
848 case LMM_InvalidModuleMap:
849 case LMM_NoDirectory:
850 break;
851
852 case LMM_NewlyLoaded:
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000853 if (Module *Module = ModMap.findModule(Name))
Douglas Gregordb1cde72011-11-16 00:09:06 +0000854 return Module;
855 break;
856 }
857 }
858
859 return 0;
860}
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000861
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000862Module *HeaderSearch::getFrameworkModule(StringRef Name,
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000863 const DirectoryEntry *Dir) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000864 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000865 return Module;
866
867 // Try to load a module map file.
868 switch (loadModuleMapFile(Dir)) {
869 case LMM_InvalidModuleMap:
870 break;
871
872 case LMM_AlreadyLoaded:
873 case LMM_NoDirectory:
874 return 0;
875
876 case LMM_NewlyLoaded:
877 return ModMap.findModule(Name);
878 }
879
880 // Try to infer a module map.
Douglas Gregorac252a32011-12-06 19:39:29 +0000881 return ModMap.inferFrameworkModule(Name, Dir, /*Parent=*/0);
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000882}
883
Douglas Gregordb1cde72011-11-16 00:09:06 +0000884
Douglas Gregor26697972011-11-12 00:22:19 +0000885HeaderSearch::LoadModuleMapResult
886HeaderSearch::loadModuleMapFile(StringRef DirName) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000887 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
888 return loadModuleMapFile(Dir);
889
Douglas Gregor26697972011-11-12 00:22:19 +0000890 return LMM_NoDirectory;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000891}
892
Douglas Gregor26697972011-11-12 00:22:19 +0000893HeaderSearch::LoadModuleMapResult
894HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000895 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
896 = DirectoryHasModuleMap.find(Dir);
897 if (KnownDir != DirectoryHasModuleMap.end())
Douglas Gregor26697972011-11-12 00:22:19 +0000898 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000899
900 llvm::SmallString<128> ModuleMapFileName;
901 ModuleMapFileName += Dir->getName();
Douglas Gregor587986e2011-12-07 02:23:45 +0000902 unsigned ModuleMapDirNameLen = ModuleMapFileName.size();
Douglas Gregorcf70d782011-11-12 00:05:07 +0000903 llvm::sys::path::append(ModuleMapFileName, "module.map");
904 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) {
905 // We have found a module map file. Try to parse it.
Douglas Gregor587986e2011-12-07 02:23:45 +0000906 if (ModMap.parseModuleMapFile(ModuleMapFile)) {
907 // No suitable module map.
908 DirectoryHasModuleMap[Dir] = false;
909 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000910 }
Douglas Gregor587986e2011-12-07 02:23:45 +0000911
912 // This directory has a module map.
913 DirectoryHasModuleMap[Dir] = true;
914
915 // Check whether there is a private module map that we need to load as well.
916 ModuleMapFileName.erase(ModuleMapFileName.begin() + ModuleMapDirNameLen,
917 ModuleMapFileName.end());
918 llvm::sys::path::append(ModuleMapFileName, "module_private.map");
919 if (const FileEntry *PrivateModuleMapFile
920 = FileMgr.getFile(ModuleMapFileName)) {
921 if (ModMap.parseModuleMapFile(PrivateModuleMapFile)) {
922 // No suitable module map.
923 DirectoryHasModuleMap[Dir] = false;
924 return LMM_InvalidModuleMap;
925 }
926 }
927
928 return LMM_NewlyLoaded;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000929 }
930
931 // No suitable module map.
932 DirectoryHasModuleMap[Dir] = false;
Douglas Gregor26697972011-11-12 00:22:19 +0000933 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000934}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000935