blob: b0668c53a44345e26123d1af5ae37f3417588943 [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"
Douglas Gregora30cfe52011-11-11 19:10:28 +000016#include "clang/Basic/Diagnostic.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/FileManager.h"
18#include "clang/Basic/IdentifierTable.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000019#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000020#include "llvm/Support/Path.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "llvm/ADT/SmallString.h"
Ted Kremenekeabea452011-07-27 18:41:18 +000022#include "llvm/Support/Capacity.h"
Chris Lattner3daed522009-03-02 22:20:04 +000023#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Douglas Gregor8c5a7602009-04-25 23:30:02 +000026const IdentifierInfo *
27HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
28 if (ControllingMacro)
29 return ControllingMacro;
30
31 if (!ControllingMacroID || !External)
32 return 0;
33
34 ControllingMacro = External->GetIdentifier(ControllingMacroID);
35 return ControllingMacro;
36}
37
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000038ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
39
Douglas Gregor8e238062011-11-11 00:35:06 +000040HeaderSearch::HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags)
Douglas Gregora30cfe52011-11-11 19:10:28 +000041 : FileMgr(FM), Diags(Diags), FrameworkMap(64),
42 ModMap(FileMgr, *Diags.getClient())
Douglas Gregor8e238062011-11-11 00:35:06 +000043{
Nico Weber74a5fd82011-05-24 04:31:14 +000044 AngledDirIdx = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000045 SystemDirIdx = 0;
46 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor8c5a7602009-04-25 23:30:02 +000048 ExternalLookup = 0;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049 ExternalSource = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000050 NumIncluded = 0;
51 NumMultiIncludeFileOptzn = 0;
52 NumFrameworkLookups = NumSubFrameworkLookups = 0;
53}
54
Chris Lattner822da612007-12-17 06:36:45 +000055HeaderSearch::~HeaderSearch() {
56 // Delete headermaps.
57 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
58 delete HeaderMaps[i].second;
59}
Mike Stump1eb44332009-09-09 15:08:12 +000060
Reid Spencer5f016e22007-07-11 17:01:13 +000061void HeaderSearch::PrintStats() {
62 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
63 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
64 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
65 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
66 NumOnceOnlyFiles += FileInfo[i].isImport;
67 if (MaxNumIncludes < FileInfo[i].NumIncludes)
68 MaxNumIncludes = FileInfo[i].NumIncludes;
69 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
70 }
71 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
72 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
73 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000074
Reid Spencer5f016e22007-07-11 17:01:13 +000075 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
76 fprintf(stderr, " %d #includes skipped due to"
77 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000078
Reid Spencer5f016e22007-07-11 17:01:13 +000079 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
80 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
81}
82
Chris Lattner822da612007-12-17 06:36:45 +000083/// CreateHeaderMap - This method returns a HeaderMap for the specified
84/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000085const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000086 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000087 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000088 if (!HeaderMaps.empty()) {
89 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000090 // Pointer equality comparison of FileEntries works because they are
91 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +000092 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +000093 return HeaderMaps[i].second;
94 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Chris Lattner39b49bc2010-11-23 08:35:12 +000096 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattner822da612007-12-17 06:36:45 +000097 HeaderMaps.push_back(std::make_pair(FE, HM));
98 return HM;
99 }
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Chris Lattner822da612007-12-17 06:36:45 +0000101 return 0;
102}
103
Douglas Gregor21cae202011-09-12 23:31:24 +0000104const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000105 Module *&Module,
Douglas Gregora4d36a62011-11-28 23:16:06 +0000106 std::string *ModuleFileName) {
107 Module = 0;
108
Douglas Gregor9a6da692011-09-12 20:41:59 +0000109 // If we don't have a module cache path, we can't do anything.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000110 if (ModuleCachePath.empty()) {
111 if (ModuleFileName)
112 ModuleFileName->clear();
Douglas Gregor9a6da692011-09-12 20:41:59 +0000113 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000114 }
115
Douglas Gregor21cae202011-09-12 23:31:24 +0000116 // Try to find the module path.
Douglas Gregor9a6da692011-09-12 20:41:59 +0000117 llvm::SmallString<256> FileName(ModuleCachePath);
118 llvm::sys::path::append(FileName, ModuleName + ".pcm");
Douglas Gregor6e975c42011-09-13 23:15:45 +0000119 if (ModuleFileName)
120 *ModuleFileName = FileName.str();
Douglas Gregora4d36a62011-11-28 23:16:06 +0000121
Douglas Gregorcf70d782011-11-12 00:05:07 +0000122 // Look in the module map to determine if there is a module by this name.
Douglas Gregora4d36a62011-11-28 23:16:06 +0000123 Module = ModMap.findModule(ModuleName);
Douglas Gregorcf70d782011-11-12 00:05:07 +0000124 if (!Module) {
125 // Look through the various header search paths to load any avaiable module
126 // maps, searching for a module map that describes this module.
127 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregora4d36a62011-11-28 23:16:06 +0000128 if (SearchDirs[Idx].isFramework()) {
129 // Search for or infer a module map for a framework.
130 llvm::SmallString<128> FrameworkDirName;
131 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
132 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
133 if (const DirectoryEntry *FrameworkDir
134 = FileMgr.getDirectory(FrameworkDirName)) {
135 Module = getFrameworkModule(ModuleName, FrameworkDir);
136 if (Module)
137 break;
138 }
139 }
140
141 // FIXME: Figure out how header maps and module maps will work together.
142
143 // Only deal with normal search directories.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000144 if (!SearchDirs[Idx].isNormalDir())
145 continue;
146
Douglas Gregor26697972011-11-12 00:22:19 +0000147 // Search for a module map file in this directory.
148 if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) {
149 // We just loaded a module map file; check whether the module is
150 // available now.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000151 Module = ModMap.findModule(ModuleName);
152 if (Module)
153 break;
154 }
Douglas Gregor26697972011-11-12 00:22:19 +0000155
Douglas Gregorcf70d782011-11-12 00:05:07 +0000156 // Search for a module map in a subdirectory with the same name as the
157 // module.
158 llvm::SmallString<128> NestedModuleMapDirName;
159 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
160 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
Douglas Gregor26697972011-11-12 00:22:19 +0000161 if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) {
162 // If we just loaded a module map file, look for the module again.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000163 Module = ModMap.findModule(ModuleName);
164 if (Module)
165 break;
166 }
167 }
168 }
169
Douglas Gregora4d36a62011-11-28 23:16:06 +0000170 // Look for the module file in the module cache.
171 // FIXME: If we didn't find a description of the module itself, should we
172 // even try to find the module in the cache?
173 return getFileMgr().getFile(FileName, /*OpenFile=*/false,
174 /*CacheFailure=*/false);
Douglas Gregor9a6da692011-09-12 20:41:59 +0000175}
176
Chris Lattnerdf772332007-12-17 07:52:39 +0000177//===----------------------------------------------------------------------===//
178// File lookup within a DirectoryLookup scope
179//===----------------------------------------------------------------------===//
180
Chris Lattner3af66a92007-12-17 17:57:27 +0000181/// getName - Return the directory or filename corresponding to this lookup
182/// object.
183const char *DirectoryLookup::getName() const {
184 if (isNormalDir())
185 return getDir()->getName();
186 if (isFramework())
187 return getFrameworkDir()->getName();
188 assert(isHeaderMap() && "Unknown DirectoryLookup");
189 return getHeaderMap()->getFileName();
190}
191
192
Chris Lattnerdf772332007-12-17 07:52:39 +0000193/// LookupFile - Lookup the specified file in this search path, returning it
194/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000195const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000196 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000197 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000198 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000199 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000200 Module **SuggestedModule) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000201 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000202 if (isNormalDir()) {
203 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000204 TmpDir = getDir()->getName();
205 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000206 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000208 SearchPath->clear();
209 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
210 }
211 if (RelativePath != NULL) {
212 RelativePath->clear();
213 RelativePath->append(Filename.begin(), Filename.end());
214 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000215
216 // If we have a module map that might map this header, load it and
217 // check whether we'll have a suggestion for a module.
218 if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) {
219 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
220 /*openFile=*/false);
221 if (!File)
222 return File;
223
224 // If there is a module that corresponds to this header,
225 // suggest it.
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000226 *SuggestedModule = HS.findModuleForHeader(File);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000227 return File;
228 }
229
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000230 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattnerafded5b2007-12-17 08:13:48 +0000233 if (isFramework())
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000234 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000235 SuggestedModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000237 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000238 const FileEntry * const Result = getHeaderMap()->LookupFile(
239 Filename, HS.getFileMgr());
240 if (Result) {
241 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000242 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000243 SearchPath->clear();
244 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
245 }
246 if (RelativePath != NULL) {
247 RelativePath->clear();
248 RelativePath->append(Filename.begin(), Filename.end());
249 }
250 }
251 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000252}
253
254
Chris Lattnerafded5b2007-12-17 08:13:48 +0000255/// DoFrameworkLookup - Do a lookup of the specified file in the current
256/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000257const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000258 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000259 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000260 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000261 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000262 Module **SuggestedModule) const
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000263{
Chris Lattnerafded5b2007-12-17 08:13:48 +0000264 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000267 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000268 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattnerafded5b2007-12-17 08:13:48 +0000270 // Find out if this is the home for the specified framework, by checking
271 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000272 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000273 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnerafded5b2007-12-17 08:13:48 +0000275 // If it is known and in some other directory, fail.
276 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattnerafded5b2007-12-17 08:13:48 +0000279 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 // FrameworkName = "/System/Library/Frameworks/"
282 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000283 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 if (FrameworkName.empty() || FrameworkName.back() != '/')
285 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000288 StringRef ModuleName(Filename.begin(), SlashPos);
289 FrameworkName += ModuleName;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
292 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattnerafded5b2007-12-17 08:13:48 +0000294 // If the cache entry is still unresolved, query to see if the cache entry is
295 // still unresolved. If so, check its existence now.
296 if (FrameworkDirCache == 0) {
297 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000300 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000301 bool Exists;
302 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 // Otherwise, if it does, remember that this is the right direntry for this
306 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000307 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Manuel Klimek74124942011-04-26 21:50:03 +0000310 if (RelativePath != NULL) {
311 RelativePath->clear();
312 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
313 }
314
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000315 // If we're allowed to look for modules, try to load or create the module
316 // corresponding to this framework.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000317 Module *Module = 0;
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000318 if (SuggestedModule) {
319 if (const DirectoryEntry *FrameworkDir
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000320 = FileMgr.getDirectory(FrameworkName))
321 Module = HS.getFrameworkModule(ModuleName, FrameworkDir);
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000322 }
323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
325 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000328
329 if (SearchPath != NULL) {
330 SearchPath->clear();
331 // Without trailing '/'.
332 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
333 }
334
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000335 // Determine whether this is the module we're building or not.
Douglas Gregor09833922011-12-06 17:31:28 +0000336 bool AutomaticImport = Module;
Chris Lattnera1394812010-01-10 01:35:12 +0000337 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000338 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000339 /*openFile=*/!AutomaticImport)) {
340 if (AutomaticImport)
Douglas Gregor09833922011-12-06 17:31:28 +0000341 *SuggestedModule = HS.findModuleForHeader(FE);
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
346 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000347 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000349 if (SearchPath != NULL)
350 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
351 Private+strlen(Private));
352
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000353 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
354 /*openFile=*/!AutomaticImport);
355 if (FE && AutomaticImport)
Douglas Gregor09833922011-12-06 17:31:28 +0000356 *SuggestedModule = HS.findModuleForHeader(FE);
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000357 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000358}
359
Chris Lattnerdf772332007-12-17 07:52:39 +0000360
Chris Lattnerafded5b2007-12-17 08:13:48 +0000361//===----------------------------------------------------------------------===//
362// Header File Location.
363//===----------------------------------------------------------------------===//
364
365
Reid Spencer5f016e22007-07-11 17:01:13 +0000366/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
367/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000368/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
369/// non-null, indicates where the #including file is, in case a relative search
370/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000371const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000372 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000373 bool isAngled,
374 const DirectoryLookup *FromDir,
375 const DirectoryLookup *&CurDir,
376 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000377 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000378 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000379 Module **SuggestedModule,
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000380 bool SkipCache)
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000381{
382 if (SuggestedModule)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000383 *SuggestedModule = 0;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000384
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000386 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 CurDir = 0;
388
389 // If this was an #include_next "/absolute/file", fail.
390 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Manuel Klimek74124942011-04-26 21:50:03 +0000392 if (SearchPath != NULL)
393 SearchPath->clear();
394 if (RelativePath != NULL) {
395 RelativePath->clear();
396 RelativePath->append(Filename.begin(), Filename.end());
397 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000399 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000402 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000403 // directory. This has to be based on CurFileEnt, not CurDir, because
404 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000405 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
406 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000407 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
408 llvm::SmallString<1024> TmpDir;
409 // Concatenate the requested file onto the directory.
410 // FIXME: Portability. Filename concatenation should be in sys::Path.
411 TmpDir += CurFileEnt->getDir()->getName();
412 TmpDir.push_back('/');
413 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000414 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000416 // This file is a system header or C++ unfriendly if the old file is.
417 //
418 // Note that the temporary 'DirInfo' is required here, as either call to
419 // getFileInfo could resize the vector and we don't want to rely on order
420 // of evaluation.
421 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000422 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000423 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000424 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000425 SearchPath->clear();
426 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
427 }
428 if (RelativePath != NULL) {
429 RelativePath->clear();
430 RelativePath->append(Filename.begin(), Filename.end());
431 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 return FE;
433 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 CurDir = 0;
437
438 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000439 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 // If this is a #include_next request, start searching after the directory the
442 // file was found in.
443 if (FromDir)
444 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattner9960ae82007-07-22 07:28:00 +0000446 // Cache all of the lookups performed by this method. Many headers are
447 // multiply included, and the "pragma once" optimization prevents them from
448 // being relex/pp'd, but they would still have to search through a
449 // (potentially huge) series of SearchDirs to find it.
450 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000451 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000452
453 // If the entry has been previously looked up, the first value will be
454 // non-zero. If the value is equal to i (the start point of our search), then
455 // this is a matching hit.
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000456 if (!SkipCache && CacheLookup.first == i+1) {
Chris Lattner9960ae82007-07-22 07:28:00 +0000457 // Skip querying potentially lots of directories for this lookup.
458 i = CacheLookup.second;
459 } else {
460 // Otherwise, this is the first query, or the previous query didn't match
461 // our search start. We will fill in our found location below, so prime the
462 // start point value.
463 CacheLookup.first = i+1;
464 }
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 // Check each directory in sequence to see if it contains this file.
467 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000468 const FileEntry *FE =
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000469 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
Douglas Gregor5e3f9222011-12-08 17:01:29 +0000470 SuggestedModule);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000471 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattnerafded5b2007-12-17 08:13:48 +0000473 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattnerafded5b2007-12-17 08:13:48 +0000475 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000476 HeaderFileInfo &HFI = getFileInfo(FE);
477 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000479 // If this file is found in a header map and uses the framework style of
480 // includes, then this header is part of a framework we're building.
481 if (CurDir->isIndexHeaderMap()) {
482 size_t SlashPos = Filename.find('/');
483 if (SlashPos != StringRef::npos) {
484 HFI.IndexHeaderMapHeader = 1;
485 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
486 SlashPos));
487 }
488 }
489
Chris Lattnerafded5b2007-12-17 08:13:48 +0000490 // Remember this location for the next lookup we do.
491 CacheLookup.second = i;
492 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000495 // If we are including a file with a quoted include "foo.h" from inside
496 // a header in a framework that is currently being built, and we couldn't
497 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
498 // "Foo" is the name of the framework in which the including header was found.
499 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
500 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
501 if (IncludingHFI.IndexHeaderMapHeader) {
502 llvm::SmallString<128> ScratchFilename;
503 ScratchFilename += IncludingHFI.Framework;
504 ScratchFilename += '/';
505 ScratchFilename += Filename;
506
507 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
508 FromDir, CurDir, CurFileEnt,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000509 SearchPath, RelativePath,
510 SuggestedModule);
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000511 std::pair<unsigned, unsigned> &CacheLookup
512 = LookupFileCache.GetOrCreateValue(Filename).getValue();
513 CacheLookup.second
514 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
515 return Result;
516 }
517 }
518
Chris Lattner9960ae82007-07-22 07:28:00 +0000519 // Otherwise, didn't find it. Remember we didn't find this.
520 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 return 0;
522}
523
524/// LookupSubframeworkHeader - Look up a subframework for the specified
525/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
526/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
527/// is a subframework within Carbon.framework. If so, return the FileEntry
528/// for the designated file, otherwise return null.
529const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000530LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000531 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000532 SmallVectorImpl<char> *SearchPath,
533 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000534 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000537 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000538 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 // Look up the base framework name of the ContextFileEnt.
541 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 // If the context info wasn't a framework, couldn't be a subframework.
544 const char *FrameworkPos = strstr(ContextName, ".framework/");
545 if (FrameworkPos == 0)
546 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
548 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 FrameworkPos+strlen(".framework/"));
550
551 // Append Frameworks/HIToolbox.framework/
552 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000553 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 FrameworkName += ".framework/";
555
556 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000557 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 // Some other location?
560 if (CacheLookup.getValue() &&
561 CacheLookup.getKeyLength() == FrameworkName.size() &&
562 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
563 CacheLookup.getKeyLength()) != 0)
564 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 // Cache subframework.
567 if (CacheLookup.getValue() == 0) {
568 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000571 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 // Otherwise, if it does, remember that this is the right direntry for this
575 // framework.
576 CacheLookup.setValue(Dir);
577 }
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 const FileEntry *FE = 0;
580
Manuel Klimek74124942011-04-26 21:50:03 +0000581 if (RelativePath != NULL) {
582 RelativePath->clear();
583 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
584 }
585
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
587 llvm::SmallString<1024> HeadersFilename(FrameworkName);
588 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000589 if (SearchPath != NULL) {
590 SearchPath->clear();
591 // Without trailing '/'.
592 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
593 }
594
Chris Lattnera1394812010-01-10 01:35:12 +0000595 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000596 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
599 HeadersFilename = FrameworkName;
600 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000601 if (SearchPath != NULL) {
602 SearchPath->clear();
603 // Without trailing '/'.
604 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
605 }
606
Chris Lattnera1394812010-01-10 01:35:12 +0000607 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000608 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 return 0;
610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000613 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000614 // Note that the temporary 'DirInfo' is required here, as either call to
615 // getFileInfo could resize the vector and we don't want to rely on order
616 // of evaluation.
617 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
618 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 return FE;
620}
621
622//===----------------------------------------------------------------------===//
623// File Info Management.
624//===----------------------------------------------------------------------===//
625
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000626/// \brief Merge the header file info provided by \p OtherHFI into the current
627/// header file info (\p HFI)
628static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
629 const HeaderFileInfo &OtherHFI) {
630 HFI.isImport |= OtherHFI.isImport;
631 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
632 HFI.NumIncludes += OtherHFI.NumIncludes;
633
634 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
635 HFI.ControllingMacro = OtherHFI.ControllingMacro;
636 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
637 }
638
639 if (OtherHFI.External) {
640 HFI.DirInfo = OtherHFI.DirInfo;
641 HFI.External = OtherHFI.External;
642 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
643 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000644
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000645 if (HFI.Framework.empty())
646 HFI.Framework = OtherHFI.Framework;
647
648 HFI.Resolved = true;
649}
650
Steve Naroff83d63c72009-04-24 20:03:17 +0000651/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000652/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000653HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 if (FE->getUID() >= FileInfo.size())
655 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000656
657 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000658 if (ExternalSource && !HFI.Resolved)
659 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000660 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000661}
Reid Spencer5f016e22007-07-11 17:01:13 +0000662
Douglas Gregordd3e5542011-05-04 00:14:37 +0000663bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
664 // Check if we've ever seen this file as a header.
665 if (File->getUID() >= FileInfo.size())
666 return false;
667
668 // Resolve header file info from the external source, if needed.
669 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000670 if (ExternalSource && !HFI.Resolved)
671 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregordd3e5542011-05-04 00:14:37 +0000672
673 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
674}
675
Steve Naroff83d63c72009-04-24 20:03:17 +0000676void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
677 if (UID >= FileInfo.size())
678 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000679 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000680 FileInfo[UID] = HFI;
681}
682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
684/// #include, #include_next, or #import directive. Return false if #including
685/// the file will have no effect or true if we should include it.
686bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
687 ++NumIncluded; // Count # of attempted #includes.
688
689 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000690 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 // If this is a #import directive, check that we have not already imported
693 // this header.
694 if (isImport) {
695 // If this has already been imported, don't import it again.
696 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 // Has this already been #import'ed or #include'd?
699 if (FileInfo.NumIncludes) return false;
700 } else {
701 // Otherwise, if this is a #include of a file that was previously #import'd
702 // or if this is the second #include of a #pragma once file, ignore it.
703 if (FileInfo.isImport)
704 return false;
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
708 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000709 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000710 = FileInfo.getControllingMacro(ExternalLookup))
711 if (ControllingMacro->hasMacroDefinition()) {
712 ++NumMultiIncludeFileOptzn;
713 return false;
714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 // Increment the number of times this file has been included.
717 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 return true;
720}
721
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000722size_t HeaderSearch::getTotalMemory() const {
723 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000724 + llvm::capacity_in_bytes(FileInfo)
725 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000726 + LookupFileCache.getAllocator().getTotalMemory()
727 + FrameworkMap.getAllocator().getTotalMemory();
728}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000729
730StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
731 return FrameworkNames.GetOrCreateValue(Framework).getKey();
732}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000733
734bool HeaderSearch::hasModuleMap(StringRef FileName,
735 const DirectoryEntry *Root) {
736 llvm::SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
737
738 StringRef DirName = FileName;
739 do {
740 // Get the parent directory name.
741 DirName = llvm::sys::path::parent_path(DirName);
742 if (DirName.empty())
743 return false;
744
745 // Determine whether this directory exists.
746 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
747 if (!Dir)
748 return false;
749
Douglas Gregorcf70d782011-11-12 00:05:07 +0000750 // Try to load the module map file in this directory.
Douglas Gregor26697972011-11-12 00:22:19 +0000751 switch (loadModuleMapFile(Dir)) {
752 case LMM_NewlyLoaded:
753 case LMM_AlreadyLoaded:
Douglas Gregorcf70d782011-11-12 00:05:07 +0000754 // Success. All of the directories we stepped through inherit this module
755 // map file.
Douglas Gregora30cfe52011-11-11 19:10:28 +0000756 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
757 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
758
759 return true;
Douglas Gregor26697972011-11-12 00:22:19 +0000760
761 case LMM_NoDirectory:
762 case LMM_InvalidModuleMap:
763 break;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000764 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000765
Douglas Gregorcf70d782011-11-12 00:05:07 +0000766 // If we hit the top of our search, we're done.
767 if (Dir == Root)
768 return false;
769
Douglas Gregora30cfe52011-11-11 19:10:28 +0000770 // Keep track of all of the directories we checked, so we can mark them as
771 // having module maps if we eventually do find a module map.
772 FixUpDirectories.push_back(Dir);
773 } while (true);
774
775 return false;
776}
777
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000778Module *HeaderSearch::findModuleForHeader(const FileEntry *File) {
779 if (Module *Module = ModMap.findModuleForHeader(File))
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000780 return Module;
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000781
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000782 return 0;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000783}
784
Douglas Gregordb1cde72011-11-16 00:09:06 +0000785bool HeaderSearch::loadModuleMapFile(const FileEntry *File) {
786 const DirectoryEntry *Dir = File->getDir();
787
788 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
789 = DirectoryHasModuleMap.find(Dir);
790 if (KnownDir != DirectoryHasModuleMap.end())
791 return !KnownDir->second;
792
793 bool Result = ModMap.parseModuleMapFile(File);
Douglas Gregor4813442c2011-12-07 21:25:07 +0000794 if (!Result && llvm::sys::path::filename(File->getName()) == "module.map") {
795 // If the file we loaded was a module.map, look for the corresponding
796 // module_private.map.
797 llvm::SmallString<128> PrivateFilename(Dir->getName());
798 llvm::sys::path::append(PrivateFilename, "module_private.map");
799 if (const FileEntry *PrivateFile = FileMgr.getFile(PrivateFilename))
800 Result = ModMap.parseModuleMapFile(PrivateFile);
801 }
802
803 DirectoryHasModuleMap[Dir] = !Result;
Douglas Gregordb1cde72011-11-16 00:09:06 +0000804 return Result;
805}
806
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000807Module *HeaderSearch::getModule(StringRef Name, bool AllowSearch) {
808 if (Module *Module = ModMap.findModule(Name))
Douglas Gregordb1cde72011-11-16 00:09:06 +0000809 return Module;
810
811 if (!AllowSearch)
812 return 0;
813
814 for (unsigned I = 0, N = SearchDirs.size(); I != N; ++I) {
815 if (!SearchDirs[I].isNormalDir())
816 continue;
817
818 switch (loadModuleMapFile(SearchDirs[I].getDir())) {
819 case LMM_AlreadyLoaded:
820 case LMM_InvalidModuleMap:
821 case LMM_NoDirectory:
822 break;
823
824 case LMM_NewlyLoaded:
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000825 if (Module *Module = ModMap.findModule(Name))
Douglas Gregordb1cde72011-11-16 00:09:06 +0000826 return Module;
827 break;
828 }
829 }
830
831 return 0;
832}
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000833
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000834Module *HeaderSearch::getFrameworkModule(StringRef Name,
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000835 const DirectoryEntry *Dir) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000836 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000837 return Module;
838
839 // Try to load a module map file.
840 switch (loadModuleMapFile(Dir)) {
841 case LMM_InvalidModuleMap:
842 break;
843
844 case LMM_AlreadyLoaded:
845 case LMM_NoDirectory:
846 return 0;
847
848 case LMM_NewlyLoaded:
849 return ModMap.findModule(Name);
850 }
851
852 // Try to infer a module map.
Douglas Gregorac252a32011-12-06 19:39:29 +0000853 return ModMap.inferFrameworkModule(Name, Dir, /*Parent=*/0);
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000854}
855
Douglas Gregordb1cde72011-11-16 00:09:06 +0000856
Douglas Gregor26697972011-11-12 00:22:19 +0000857HeaderSearch::LoadModuleMapResult
858HeaderSearch::loadModuleMapFile(StringRef DirName) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000859 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
860 return loadModuleMapFile(Dir);
861
Douglas Gregor26697972011-11-12 00:22:19 +0000862 return LMM_NoDirectory;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000863}
864
Douglas Gregor26697972011-11-12 00:22:19 +0000865HeaderSearch::LoadModuleMapResult
866HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000867 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
868 = DirectoryHasModuleMap.find(Dir);
869 if (KnownDir != DirectoryHasModuleMap.end())
Douglas Gregor26697972011-11-12 00:22:19 +0000870 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000871
872 llvm::SmallString<128> ModuleMapFileName;
873 ModuleMapFileName += Dir->getName();
Douglas Gregor587986e2011-12-07 02:23:45 +0000874 unsigned ModuleMapDirNameLen = ModuleMapFileName.size();
Douglas Gregorcf70d782011-11-12 00:05:07 +0000875 llvm::sys::path::append(ModuleMapFileName, "module.map");
876 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) {
877 // We have found a module map file. Try to parse it.
Douglas Gregor587986e2011-12-07 02:23:45 +0000878 if (ModMap.parseModuleMapFile(ModuleMapFile)) {
879 // No suitable module map.
880 DirectoryHasModuleMap[Dir] = false;
881 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000882 }
Douglas Gregor587986e2011-12-07 02:23:45 +0000883
884 // This directory has a module map.
885 DirectoryHasModuleMap[Dir] = true;
886
887 // Check whether there is a private module map that we need to load as well.
888 ModuleMapFileName.erase(ModuleMapFileName.begin() + ModuleMapDirNameLen,
889 ModuleMapFileName.end());
890 llvm::sys::path::append(ModuleMapFileName, "module_private.map");
891 if (const FileEntry *PrivateModuleMapFile
892 = FileMgr.getFile(ModuleMapFileName)) {
893 if (ModMap.parseModuleMapFile(PrivateModuleMapFile)) {
894 // No suitable module map.
895 DirectoryHasModuleMap[Dir] = false;
896 return LMM_InvalidModuleMap;
897 }
898 }
899
900 return LMM_NewlyLoaded;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000901 }
902
903 // No suitable module map.
904 DirectoryHasModuleMap[Dir] = false;
Douglas Gregor26697972011-11-12 00:22:19 +0000905 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000906}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000907