blob: c1c3a2f7bf3729e6f71f9691f96286101ddabd7f [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 Gregora4d36a62011-11-28 23:16:06 +0000105 ModuleMap::Module *&Module,
106 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,
200 StringRef BuildingModule,
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000201 ModuleMap::Module **SuggestedModule) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000202 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000203 if (isNormalDir()) {
204 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000205 TmpDir = getDir()->getName();
206 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000207 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000209 SearchPath->clear();
210 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
211 }
212 if (RelativePath != NULL) {
213 RelativePath->clear();
214 RelativePath->append(Filename.begin(), Filename.end());
215 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000216
217 // If we have a module map that might map this header, load it and
218 // check whether we'll have a suggestion for a module.
219 if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) {
220 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
221 /*openFile=*/false);
222 if (!File)
223 return File;
224
225 // If there is a module that corresponds to this header,
226 // suggest it.
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000227 ModuleMap::Module *Module = HS.findModuleForHeader(File);
228 if (Module && Module->getTopLevelModuleName() != BuildingModule)
Douglas Gregora30cfe52011-11-11 19:10:28 +0000229 *SuggestedModule = Module;
230
231 return File;
232 }
233
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000234 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000235 }
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattnerafded5b2007-12-17 08:13:48 +0000237 if (isFramework())
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000238 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
239 BuildingModule, SuggestedModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000241 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000242 const FileEntry * const Result = getHeaderMap()->LookupFile(
243 Filename, HS.getFileMgr());
244 if (Result) {
245 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000246 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000247 SearchPath->clear();
248 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
249 }
250 if (RelativePath != NULL) {
251 RelativePath->clear();
252 RelativePath->append(Filename.begin(), Filename.end());
253 }
254 }
255 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000256}
257
258
Chris Lattnerafded5b2007-12-17 08:13:48 +0000259/// DoFrameworkLookup - Do a lookup of the specified file in the current
260/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000261const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000262 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000263 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000265 SmallVectorImpl<char> *RelativePath,
266 StringRef BuildingModule,
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000267 ModuleMap::Module **SuggestedModule) const
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000268{
Chris Lattnerafded5b2007-12-17 08:13:48 +0000269 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000272 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnerafded5b2007-12-17 08:13:48 +0000275 // Find out if this is the home for the specified framework, by checking
276 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000277 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000278 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattnerafded5b2007-12-17 08:13:48 +0000280 // If it is known and in some other directory, fail.
281 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Chris Lattnerafded5b2007-12-17 08:13:48 +0000284 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 // FrameworkName = "/System/Library/Frameworks/"
287 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000288 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 if (FrameworkName.empty() || FrameworkName.back() != '/')
290 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000293 StringRef ModuleName(Filename.begin(), SlashPos);
294 FrameworkName += ModuleName;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
297 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Chris Lattnerafded5b2007-12-17 08:13:48 +0000299 // If the cache entry is still unresolved, query to see if the cache entry is
300 // still unresolved. If so, check its existence now.
301 if (FrameworkDirCache == 0) {
302 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000305 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000306 bool Exists;
307 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // Otherwise, if it does, remember that this is the right direntry for this
311 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000312 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 }
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Manuel Klimek74124942011-04-26 21:50:03 +0000315 if (RelativePath != NULL) {
316 RelativePath->clear();
317 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
318 }
319
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000320 // If we're allowed to look for modules, try to load or create the module
321 // corresponding to this framework.
322 ModuleMap::Module *Module = 0;
323 if (SuggestedModule) {
324 if (const DirectoryEntry *FrameworkDir
325 = FileMgr.getDirectory(FrameworkName)) {
326 if ((Module = HS.getFrameworkModule(ModuleName, FrameworkDir)) &&
327 Module->Name == BuildingModule)
328 Module = 0;
329 }
330 }
331
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
333 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000336
337 if (SearchPath != NULL) {
338 SearchPath->clear();
339 // Without trailing '/'.
340 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
341 }
342
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000343 // Determine whether this is the module we're building or not.
344 // FIXME: Do we still need the ".." hack?
345 bool AutomaticImport = Module &&
Douglas Gregor0fd787b2011-09-16 00:22:46 +0000346 !Filename.substr(SlashPos + 1).startswith("..");
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000347
Chris Lattnera1394812010-01-10 01:35:12 +0000348 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000349 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000350 /*openFile=*/!AutomaticImport)) {
351 if (AutomaticImport)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000352 *SuggestedModule = Module;
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000354 }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
357 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000358 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000360 if (SearchPath != NULL)
361 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
362 Private+strlen(Private));
363
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000364 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
365 /*openFile=*/!AutomaticImport);
366 if (FE && AutomaticImport)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000367 *SuggestedModule = Module;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000368 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000369}
370
Chris Lattnerdf772332007-12-17 07:52:39 +0000371
Chris Lattnerafded5b2007-12-17 08:13:48 +0000372//===----------------------------------------------------------------------===//
373// Header File Location.
374//===----------------------------------------------------------------------===//
375
376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
378/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000379/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
380/// non-null, indicates where the #including file is, in case a relative search
381/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000382const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000383 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000384 bool isAngled,
385 const DirectoryLookup *FromDir,
386 const DirectoryLookup *&CurDir,
387 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000388 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000389 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000390 ModuleMap::Module **SuggestedModule,
391 bool SkipCache)
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000392{
393 if (SuggestedModule)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000394 *SuggestedModule = 0;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000395
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000397 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 CurDir = 0;
399
400 // If this was an #include_next "/absolute/file", fail.
401 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Manuel Klimek74124942011-04-26 21:50:03 +0000403 if (SearchPath != NULL)
404 SearchPath->clear();
405 if (RelativePath != NULL) {
406 RelativePath->clear();
407 RelativePath->append(Filename.begin(), Filename.end());
408 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000410 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000413 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000414 // directory. This has to be based on CurFileEnt, not CurDir, because
415 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000416 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
417 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000418 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
419 llvm::SmallString<1024> TmpDir;
420 // Concatenate the requested file onto the directory.
421 // FIXME: Portability. Filename concatenation should be in sys::Path.
422 TmpDir += CurFileEnt->getDir()->getName();
423 TmpDir.push_back('/');
424 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000425 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000427 // This file is a system header or C++ unfriendly if the old file is.
428 //
429 // Note that the temporary 'DirInfo' is required here, as either call to
430 // getFileInfo could resize the vector and we don't want to rely on order
431 // of evaluation.
432 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000433 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000434 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000435 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000436 SearchPath->clear();
437 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
438 }
439 if (RelativePath != NULL) {
440 RelativePath->clear();
441 RelativePath->append(Filename.begin(), Filename.end());
442 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 return FE;
444 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 CurDir = 0;
448
449 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000450 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 // If this is a #include_next request, start searching after the directory the
453 // file was found in.
454 if (FromDir)
455 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Chris Lattner9960ae82007-07-22 07:28:00 +0000457 // Cache all of the lookups performed by this method. Many headers are
458 // multiply included, and the "pragma once" optimization prevents them from
459 // being relex/pp'd, but they would still have to search through a
460 // (potentially huge) series of SearchDirs to find it.
461 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000462 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000463
464 // If the entry has been previously looked up, the first value will be
465 // non-zero. If the value is equal to i (the start point of our search), then
466 // this is a matching hit.
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000467 if (!SkipCache && CacheLookup.first == i+1) {
Chris Lattner9960ae82007-07-22 07:28:00 +0000468 // Skip querying potentially lots of directories for this lookup.
469 i = CacheLookup.second;
470 } else {
471 // Otherwise, this is the first query, or the previous query didn't match
472 // our search start. We will fill in our found location below, so prime the
473 // start point value.
474 CacheLookup.first = i+1;
475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 // Check each directory in sequence to see if it contains this file.
478 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000479 const FileEntry *FE =
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000480 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
481 BuildingModule, SuggestedModule);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000482 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattnerafded5b2007-12-17 08:13:48 +0000484 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattnerafded5b2007-12-17 08:13:48 +0000486 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000487 HeaderFileInfo &HFI = getFileInfo(FE);
488 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000490 // If this file is found in a header map and uses the framework style of
491 // includes, then this header is part of a framework we're building.
492 if (CurDir->isIndexHeaderMap()) {
493 size_t SlashPos = Filename.find('/');
494 if (SlashPos != StringRef::npos) {
495 HFI.IndexHeaderMapHeader = 1;
496 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
497 SlashPos));
498 }
499 }
500
Chris Lattnerafded5b2007-12-17 08:13:48 +0000501 // Remember this location for the next lookup we do.
502 CacheLookup.second = i;
503 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000506 // If we are including a file with a quoted include "foo.h" from inside
507 // a header in a framework that is currently being built, and we couldn't
508 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
509 // "Foo" is the name of the framework in which the including header was found.
510 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
511 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
512 if (IncludingHFI.IndexHeaderMapHeader) {
513 llvm::SmallString<128> ScratchFilename;
514 ScratchFilename += IncludingHFI.Framework;
515 ScratchFilename += '/';
516 ScratchFilename += Filename;
517
518 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
519 FromDir, CurDir, CurFileEnt,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000520 SearchPath, RelativePath,
521 SuggestedModule);
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000522 std::pair<unsigned, unsigned> &CacheLookup
523 = LookupFileCache.GetOrCreateValue(Filename).getValue();
524 CacheLookup.second
525 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
526 return Result;
527 }
528 }
529
Chris Lattner9960ae82007-07-22 07:28:00 +0000530 // Otherwise, didn't find it. Remember we didn't find this.
531 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 return 0;
533}
534
535/// LookupSubframeworkHeader - Look up a subframework for the specified
536/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
537/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
538/// is a subframework within Carbon.framework. If so, return the FileEntry
539/// for the designated file, otherwise return null.
540const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000541LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000542 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000543 SmallVectorImpl<char> *SearchPath,
544 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000545 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000548 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000549 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 // Look up the base framework name of the ContextFileEnt.
552 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 // If the context info wasn't a framework, couldn't be a subframework.
555 const char *FrameworkPos = strstr(ContextName, ".framework/");
556 if (FrameworkPos == 0)
557 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000558
559 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 FrameworkPos+strlen(".framework/"));
561
562 // Append Frameworks/HIToolbox.framework/
563 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000564 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 FrameworkName += ".framework/";
566
567 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000568 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 // Some other location?
571 if (CacheLookup.getValue() &&
572 CacheLookup.getKeyLength() == FrameworkName.size() &&
573 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
574 CacheLookup.getKeyLength()) != 0)
575 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 // Cache subframework.
578 if (CacheLookup.getValue() == 0) {
579 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000582 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 // Otherwise, if it does, remember that this is the right direntry for this
586 // framework.
587 CacheLookup.setValue(Dir);
588 }
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 const FileEntry *FE = 0;
591
Manuel Klimek74124942011-04-26 21:50:03 +0000592 if (RelativePath != NULL) {
593 RelativePath->clear();
594 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
595 }
596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
598 llvm::SmallString<1024> HeadersFilename(FrameworkName);
599 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000600 if (SearchPath != NULL) {
601 SearchPath->clear();
602 // Without trailing '/'.
603 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
604 }
605
Chris Lattnera1394812010-01-10 01:35:12 +0000606 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000607 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
610 HeadersFilename = FrameworkName;
611 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000612 if (SearchPath != NULL) {
613 SearchPath->clear();
614 // Without trailing '/'.
615 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
616 }
617
Chris Lattnera1394812010-01-10 01:35:12 +0000618 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000619 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 return 0;
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000624 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000625 // Note that the temporary 'DirInfo' is required here, as either call to
626 // getFileInfo could resize the vector and we don't want to rely on order
627 // of evaluation.
628 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
629 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 return FE;
631}
632
633//===----------------------------------------------------------------------===//
634// File Info Management.
635//===----------------------------------------------------------------------===//
636
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000637/// \brief Merge the header file info provided by \p OtherHFI into the current
638/// header file info (\p HFI)
639static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
640 const HeaderFileInfo &OtherHFI) {
641 HFI.isImport |= OtherHFI.isImport;
642 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
643 HFI.NumIncludes += OtherHFI.NumIncludes;
644
645 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
646 HFI.ControllingMacro = OtherHFI.ControllingMacro;
647 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
648 }
649
650 if (OtherHFI.External) {
651 HFI.DirInfo = OtherHFI.DirInfo;
652 HFI.External = OtherHFI.External;
653 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
654 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000655
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000656 if (HFI.Framework.empty())
657 HFI.Framework = OtherHFI.Framework;
658
659 HFI.Resolved = true;
660}
661
Steve Naroff83d63c72009-04-24 20:03:17 +0000662/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000663/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000664HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 if (FE->getUID() >= FileInfo.size())
666 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000667
668 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000669 if (ExternalSource && !HFI.Resolved)
670 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000671 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000672}
Reid Spencer5f016e22007-07-11 17:01:13 +0000673
Douglas Gregordd3e5542011-05-04 00:14:37 +0000674bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
675 // Check if we've ever seen this file as a header.
676 if (File->getUID() >= FileInfo.size())
677 return false;
678
679 // Resolve header file info from the external source, if needed.
680 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000681 if (ExternalSource && !HFI.Resolved)
682 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregordd3e5542011-05-04 00:14:37 +0000683
684 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
685}
686
Steve Naroff83d63c72009-04-24 20:03:17 +0000687void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
688 if (UID >= FileInfo.size())
689 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000690 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000691 FileInfo[UID] = HFI;
692}
693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
695/// #include, #include_next, or #import directive. Return false if #including
696/// the file will have no effect or true if we should include it.
697bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
698 ++NumIncluded; // Count # of attempted #includes.
699
700 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000701 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 // If this is a #import directive, check that we have not already imported
704 // this header.
705 if (isImport) {
706 // If this has already been imported, don't import it again.
707 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 // Has this already been #import'ed or #include'd?
710 if (FileInfo.NumIncludes) return false;
711 } else {
712 // Otherwise, if this is a #include of a file that was previously #import'd
713 // or if this is the second #include of a #pragma once file, ignore it.
714 if (FileInfo.isImport)
715 return false;
716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
719 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000720 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000721 = FileInfo.getControllingMacro(ExternalLookup))
722 if (ControllingMacro->hasMacroDefinition()) {
723 ++NumMultiIncludeFileOptzn;
724 return false;
725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 // Increment the number of times this file has been included.
728 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 return true;
731}
732
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000733size_t HeaderSearch::getTotalMemory() const {
734 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000735 + llvm::capacity_in_bytes(FileInfo)
736 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000737 + LookupFileCache.getAllocator().getTotalMemory()
738 + FrameworkMap.getAllocator().getTotalMemory();
739}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000740
741StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
742 return FrameworkNames.GetOrCreateValue(Framework).getKey();
743}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000744
745bool HeaderSearch::hasModuleMap(StringRef FileName,
746 const DirectoryEntry *Root) {
747 llvm::SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
748
749 StringRef DirName = FileName;
750 do {
751 // Get the parent directory name.
752 DirName = llvm::sys::path::parent_path(DirName);
753 if (DirName.empty())
754 return false;
755
756 // Determine whether this directory exists.
757 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
758 if (!Dir)
759 return false;
760
Douglas Gregorcf70d782011-11-12 00:05:07 +0000761 // Try to load the module map file in this directory.
Douglas Gregor26697972011-11-12 00:22:19 +0000762 switch (loadModuleMapFile(Dir)) {
763 case LMM_NewlyLoaded:
764 case LMM_AlreadyLoaded:
Douglas Gregorcf70d782011-11-12 00:05:07 +0000765 // Success. All of the directories we stepped through inherit this module
766 // map file.
Douglas Gregora30cfe52011-11-11 19:10:28 +0000767 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
768 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
769
770 return true;
Douglas Gregor26697972011-11-12 00:22:19 +0000771
772 case LMM_NoDirectory:
773 case LMM_InvalidModuleMap:
774 break;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000775 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000776
Douglas Gregorcf70d782011-11-12 00:05:07 +0000777 // If we hit the top of our search, we're done.
778 if (Dir == Root)
779 return false;
780
Douglas Gregora30cfe52011-11-11 19:10:28 +0000781 // Keep track of all of the directories we checked, so we can mark them as
782 // having module maps if we eventually do find a module map.
783 FixUpDirectories.push_back(Dir);
784 } while (true);
785
786 return false;
787}
788
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000789ModuleMap::Module *HeaderSearch::findModuleForHeader(const FileEntry *File) {
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000790 if (ModuleMap::Module *Module = ModMap.findModuleForHeader(File))
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000791 return Module;
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000792
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000793 return 0;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000794}
795
Douglas Gregordb1cde72011-11-16 00:09:06 +0000796bool HeaderSearch::loadModuleMapFile(const FileEntry *File) {
797 const DirectoryEntry *Dir = File->getDir();
798
799 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
800 = DirectoryHasModuleMap.find(Dir);
801 if (KnownDir != DirectoryHasModuleMap.end())
802 return !KnownDir->second;
803
804 bool Result = ModMap.parseModuleMapFile(File);
805 DirectoryHasModuleMap[Dir] = !Result;
806 return Result;
807}
808
809ModuleMap::Module *HeaderSearch::getModule(StringRef Name, bool AllowSearch) {
810 if (ModuleMap::Module *Module = ModMap.findModule(Name))
811 return Module;
812
813 if (!AllowSearch)
814 return 0;
815
816 for (unsigned I = 0, N = SearchDirs.size(); I != N; ++I) {
817 if (!SearchDirs[I].isNormalDir())
818 continue;
819
820 switch (loadModuleMapFile(SearchDirs[I].getDir())) {
821 case LMM_AlreadyLoaded:
822 case LMM_InvalidModuleMap:
823 case LMM_NoDirectory:
824 break;
825
826 case LMM_NewlyLoaded:
827 if (ModuleMap::Module *Module = ModMap.findModule(Name))
828 return Module;
829 break;
830 }
831 }
832
833 return 0;
834}
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000835
836ModuleMap::Module *HeaderSearch::getFrameworkModule(StringRef Name,
837 const DirectoryEntry *Dir) {
838 if (ModuleMap::Module *Module = ModMap.findModule(Name))
839 return Module;
840
841 // Try to load a module map file.
842 switch (loadModuleMapFile(Dir)) {
843 case LMM_InvalidModuleMap:
844 break;
845
846 case LMM_AlreadyLoaded:
847 case LMM_NoDirectory:
848 return 0;
849
850 case LMM_NewlyLoaded:
851 return ModMap.findModule(Name);
852 }
853
854 // Try to infer a module map.
855 return ModMap.inferFrameworkModule(Name, Dir);
856}
857
Douglas Gregordb1cde72011-11-16 00:09:06 +0000858
Douglas Gregor26697972011-11-12 00:22:19 +0000859HeaderSearch::LoadModuleMapResult
860HeaderSearch::loadModuleMapFile(StringRef DirName) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000861 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
862 return loadModuleMapFile(Dir);
863
Douglas Gregor26697972011-11-12 00:22:19 +0000864 return LMM_NoDirectory;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000865}
866
Douglas Gregor26697972011-11-12 00:22:19 +0000867HeaderSearch::LoadModuleMapResult
868HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000869 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
870 = DirectoryHasModuleMap.find(Dir);
871 if (KnownDir != DirectoryHasModuleMap.end())
Douglas Gregor26697972011-11-12 00:22:19 +0000872 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000873
874 llvm::SmallString<128> ModuleMapFileName;
875 ModuleMapFileName += Dir->getName();
876 llvm::sys::path::append(ModuleMapFileName, "module.map");
877 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) {
878 // We have found a module map file. Try to parse it.
879 if (!ModMap.parseModuleMapFile(ModuleMapFile)) {
880 // This directory has a module map.
881 DirectoryHasModuleMap[Dir] = true;
882
Douglas Gregor26697972011-11-12 00:22:19 +0000883 return LMM_NewlyLoaded;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000884 }
885 }
886
887 // No suitable module map.
888 DirectoryHasModuleMap[Dir] = false;
Douglas Gregor26697972011-11-12 00:22:19 +0000889 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000890}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000891