blob: 1d560fca024623185b568e80c141ba16196e8aab [file] [log] [blame]
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner59a9ebd2006-10-18 05:34:33 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DirectoryLookup and HeaderSearch interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner59a9ebd2006-10-18 05:34:33 +000014#include "clang/Lex/HeaderSearch.h"
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000015#include "clang/Lex/HeaderMap.h"
Douglas Gregor718292f2011-11-11 19:10:28 +000016#include "clang/Basic/Diagnostic.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000017#include "clang/Basic/FileManager.h"
18#include "clang/Basic/IdentifierTable.h"
Michael J. Spencerf6efe582011-01-10 02:34:13 +000019#include "llvm/Support/FileSystem.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000020#include "llvm/Support/Path.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +000021#include "llvm/ADT/SmallString.h"
Ted Kremenekae63d102011-07-27 18:41:18 +000022#include "llvm/Support/Capacity.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000023#include <cstdio>
Chris Lattner59a9ebd2006-10-18 05:34:33 +000024using namespace clang;
25
Douglas Gregor99734e72009-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 Gregor09b69892011-02-10 17:09:37 +000038ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
39
Douglas Gregor197ac202011-11-11 00:35:06 +000040HeaderSearch::HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags)
Douglas Gregor718292f2011-11-11 19:10:28 +000041 : FileMgr(FM), Diags(Diags), FrameworkMap(64),
42 ModMap(FileMgr, *Diags.getClient())
Douglas Gregor197ac202011-11-11 00:35:06 +000043{
Nico Weber3b1d1212011-05-24 04:31:14 +000044 AngledDirIdx = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000045 SystemDirIdx = 0;
46 NoCurDirSearch = false;
Mike Stump11289f42009-09-09 15:08:12 +000047
Douglas Gregor99734e72009-04-25 23:30:02 +000048 ExternalLookup = 0;
Douglas Gregor09b69892011-02-10 17:09:37 +000049 ExternalSource = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000050 NumIncluded = 0;
51 NumMultiIncludeFileOptzn = 0;
52 NumFrameworkLookups = NumSubFrameworkLookups = 0;
53}
54
Chris Lattnerc4ba38e2007-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 Stump11289f42009-09-09 15:08:12 +000060
Chris Lattner59a9ebd2006-10-18 05:34:33 +000061void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000062 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
63 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000064 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 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000071 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 Stump11289f42009-09-09 15:08:12 +000074
Chris Lattner23b7eb62007-06-15 23:05:46 +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 Stump11289f42009-09-09 15:08:12 +000078
Chris Lattner23b7eb62007-06-15 23:05:46 +000079 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
80 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000081}
82
Chris Lattnerc4ba38e2007-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 Lattner4ffe46c2007-12-17 18:34:53 +000085const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000086 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerf62f7582007-12-17 07:52:39 +000087 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000088 if (!HeaderMaps.empty()) {
89 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerf62f7582007-12-17 07:52:39 +000090 // Pointer equality comparison of FileEntries works because they are
91 // already uniqued by inode.
Mike Stump11289f42009-09-09 15:08:12 +000092 if (HeaderMaps[i].first == FE)
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000093 return HeaderMaps[i].second;
94 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Chris Lattner5159f612010-11-23 08:35:12 +000096 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000097 HeaderMaps.push_back(std::make_pair(FE, HM));
98 return HM;
99 }
Mike Stump11289f42009-09-09 15:08:12 +0000100
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000101 return 0;
102}
103
Douglas Gregorfaeb1d42011-09-12 23:31:24 +0000104const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName,
Douglas Gregorde3ef502011-11-30 23:21:26 +0000105 Module *&Module,
Douglas Gregorca295452011-11-28 23:16:06 +0000106 std::string *ModuleFileName) {
107 Module = 0;
108
Douglas Gregor1e44e022011-09-12 20:41:59 +0000109 // If we don't have a module cache path, we can't do anything.
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000110 if (ModuleCachePath.empty()) {
111 if (ModuleFileName)
112 ModuleFileName->clear();
Douglas Gregor1e44e022011-09-12 20:41:59 +0000113 return 0;
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000114 }
115
Douglas Gregorfaeb1d42011-09-12 23:31:24 +0000116 // Try to find the module path.
Douglas Gregor1e44e022011-09-12 20:41:59 +0000117 llvm::SmallString<256> FileName(ModuleCachePath);
118 llvm::sys::path::append(FileName, ModuleName + ".pcm");
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000119 if (ModuleFileName)
120 *ModuleFileName = FileName.str();
Douglas Gregorca295452011-11-28 23:16:06 +0000121
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000122 // Look in the module map to determine if there is a module by this name.
Douglas Gregorca295452011-11-28 23:16:06 +0000123 Module = ModMap.findModule(ModuleName);
Douglas Gregoraf28ec82011-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 Gregorca295452011-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 Gregoraf28ec82011-11-12 00:05:07 +0000144 if (!SearchDirs[Idx].isNormalDir())
145 continue;
146
Douglas Gregor80b69042011-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 Gregoraf28ec82011-11-12 00:05:07 +0000151 Module = ModMap.findModule(ModuleName);
152 if (Module)
153 break;
154 }
Douglas Gregor80b69042011-11-12 00:22:19 +0000155
Douglas Gregoraf28ec82011-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 Gregor80b69042011-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 Gregoraf28ec82011-11-12 00:05:07 +0000163 Module = ModMap.findModule(ModuleName);
164 if (Module)
165 break;
166 }
167 }
168 }
169
Douglas Gregorca295452011-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 Gregor1e44e022011-09-12 20:41:59 +0000175}
176
Chris Lattnerf62f7582007-12-17 07:52:39 +0000177//===----------------------------------------------------------------------===//
178// File lookup within a DirectoryLookup scope
179//===----------------------------------------------------------------------===//
180
Chris Lattner8d720d02007-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 Lattnerf62f7582007-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 Carruth3cc331a2011-03-16 18:34:36 +0000195const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000196 StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000197 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000198 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000199 SmallVectorImpl<char> *RelativePath,
200 StringRef BuildingModule,
Douglas Gregorde3ef502011-11-30 23:21:26 +0000201 Module **SuggestedModule) const {
Chris Lattnerf62f7582007-12-17 07:52:39 +0000202 llvm::SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000203 if (isNormalDir()) {
204 // Concatenate the requested file onto the directory.
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000205 TmpDir = getDir()->getName();
206 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000207 if (SearchPath != NULL) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000208 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek0c69fd22011-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 Gregor718292f2011-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 Gregorde3ef502011-11-30 23:21:26 +0000227 Module *Module = HS.findModuleForHeader(File);
Douglas Gregorc04f6442011-11-17 22:44:56 +0000228 if (Module && Module->getTopLevelModuleName() != BuildingModule)
Douglas Gregor718292f2011-11-11 19:10:28 +0000229 *SuggestedModule = Module;
230
231 return File;
232 }
233
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000234 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattner712e3872007-12-17 08:13:48 +0000235 }
Mike Stump11289f42009-09-09 15:08:12 +0000236
Chris Lattner712e3872007-12-17 08:13:48 +0000237 if (isFramework())
Douglas Gregor97eec242011-09-15 22:00:41 +0000238 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
239 BuildingModule, SuggestedModule);
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattner44bd21b2007-12-17 08:17:39 +0000241 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000242 const FileEntry * const Result = getHeaderMap()->LookupFile(
243 Filename, HS.getFileMgr());
244 if (Result) {
245 if (SearchPath != NULL) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000246 StringRef SearchPathRef(getName());
Manuel Klimek0c69fd22011-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 Lattnerf62f7582007-12-17 07:52:39 +0000256}
257
258
Chris Lattner712e3872007-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 Carruth3cc331a2011-03-16 18:34:36 +0000261const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000262 StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000263 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000264 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000265 SmallVectorImpl<char> *RelativePath,
266 StringRef BuildingModule,
Douglas Gregorde3ef502011-11-30 23:21:26 +0000267 Module **SuggestedModule) const
Douglas Gregor97eec242011-09-15 22:00:41 +0000268{
Chris Lattner712e3872007-12-17 08:13:48 +0000269 FileManager &FileMgr = HS.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000270
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000271 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000272 size_t SlashPos = Filename.find('/');
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000273 if (SlashPos == StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattner712e3872007-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 Stump11289f42009-09-09 15:08:12 +0000277 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000278 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000279
Chris Lattner712e3872007-12-17 08:13:48 +0000280 // If it is known and in some other directory, fail.
281 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Chris Lattner5ed76da2006-10-22 07:24:13 +0000282 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000283
Chris Lattner712e3872007-12-17 08:13:48 +0000284 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000285
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000286 // FrameworkName = "/System/Library/Frameworks/"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000287 llvm::SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000288 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000289 if (FrameworkName.empty() || FrameworkName.back() != '/')
290 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000291
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000292 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor56c64012011-11-17 01:41:17 +0000293 StringRef ModuleName(Filename.begin(), SlashPos);
294 FrameworkName += ModuleName;
Mike Stump11289f42009-09-09 15:08:12 +0000295
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000296 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
297 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattner712e3872007-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 Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattner5ed76da2006-10-22 07:24:13 +0000304 // If the framework dir doesn't exist, we fail.
Chris Lattner712e3872007-12-17 08:13:48 +0000305 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencerf6efe582011-01-10 02:34:13 +0000306 bool Exists;
307 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000308 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000309
Chris Lattner5ed76da2006-10-22 07:24:13 +0000310 // Otherwise, if it does, remember that this is the right direntry for this
311 // framework.
Chris Lattner712e3872007-12-17 08:13:48 +0000312 FrameworkDirCache = getFrameworkDir();
Chris Lattner5ed76da2006-10-22 07:24:13 +0000313 }
Mike Stump11289f42009-09-09 15:08:12 +0000314
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000315 if (RelativePath != NULL) {
316 RelativePath->clear();
317 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
318 }
319
Douglas Gregor56c64012011-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.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000322 Module *Module = 0;
Douglas Gregor56c64012011-11-17 01:41:17 +0000323 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
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000332 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000333 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000334
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000335 FrameworkName += "Headers/";
Manuel Klimek0c69fd22011-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 Gregor56c64012011-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 Gregor88b4ddf2011-09-16 00:22:46 +0000346 !Filename.substr(SlashPos + 1).startswith("..");
Douglas Gregor97eec242011-09-15 22:00:41 +0000347
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000348 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000349 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Douglas Gregor97eec242011-09-15 22:00:41 +0000350 /*openFile=*/!AutomaticImport)) {
351 if (AutomaticImport)
Douglas Gregorc04f6442011-11-17 22:44:56 +0000352 *SuggestedModule = Module;
Chris Lattner577377e2006-10-20 04:55:45 +0000353 return FE;
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000354 }
Mike Stump11289f42009-09-09 15:08:12 +0000355
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000356 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000357 const char *Private = "Private";
Mike Stump11289f42009-09-09 15:08:12 +0000358 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000359 Private+strlen(Private));
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000360 if (SearchPath != NULL)
361 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
362 Private+strlen(Private));
363
Douglas Gregor97eec242011-09-15 22:00:41 +0000364 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
365 /*openFile=*/!AutomaticImport);
366 if (FE && AutomaticImport)
Douglas Gregorc04f6442011-11-17 22:44:56 +0000367 *SuggestedModule = Module;
Douglas Gregor97eec242011-09-15 22:00:41 +0000368 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000369}
370
Chris Lattnerf62f7582007-12-17 07:52:39 +0000371
Chris Lattner712e3872007-12-17 08:13:48 +0000372//===----------------------------------------------------------------------===//
373// Header File Location.
374//===----------------------------------------------------------------------===//
375
376
Chris Lattner59a9ebd2006-10-18 05:34:33 +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 Gregor618e64a2010-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 Carruth3cc331a2011-03-16 18:34:36 +0000382const FileEntry *HeaderSearch::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000383 StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000384 bool isAngled,
385 const DirectoryLookup *FromDir,
386 const DirectoryLookup *&CurDir,
387 const FileEntry *CurFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000388 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000389 SmallVectorImpl<char> *RelativePath,
Douglas Gregorde3ef502011-11-30 23:21:26 +0000390 Module **SuggestedModule,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000391 bool SkipCache)
Douglas Gregor97eec242011-09-15 22:00:41 +0000392{
393 if (SuggestedModule)
Douglas Gregorc04f6442011-11-17 22:44:56 +0000394 *SuggestedModule = 0;
Douglas Gregor97eec242011-09-15 22:00:41 +0000395
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000396 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000397 if (llvm::sys::path::is_absolute(Filename)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000398 CurDir = 0;
399
400 // If this was an #include_next "/absolute/file", fail.
401 if (FromDir) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000402
Manuel Klimek0c69fd22011-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 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000409 // Otherwise, just return the file.
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000410 return FileMgr.getFile(Filename, /*openFile=*/true);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000411 }
Mike Stump11289f42009-09-09 15:08:12 +0000412
Douglas Gregor9f93e382011-07-28 04:45:53 +0000413 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor618e64a2010-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 Lattnerf62f7582007-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 Gregor618e64a2010-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 Kyrtzidisd6278e32011-03-16 19:17:25 +0000425 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000426 // Leave CurDir unset.
Douglas Gregor618e64a2010-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 Lattnerf5c619f2008-02-25 21:38:21 +0000433 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000434 if (SearchPath != NULL) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000435 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek0c69fd22011-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 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000443 return FE;
444 }
445 }
Mike Stump11289f42009-09-09 15:08:12 +0000446
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000447 CurDir = 0;
448
449 // If this is a system #include, ignore the user #include locs.
Nico Weber3b1d1212011-05-24 04:31:14 +0000450 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattner59a9ebd2006-10-18 05:34:33 +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 Stump11289f42009-09-09 15:08:12 +0000456
Chris Lattnerd4275422007-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 Lattnerd081f8c2010-01-10 01:35:12 +0000462 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattnerd4275422007-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 Gregor8ad31c22011-11-20 17:46:46 +0000467 if (!SkipCache && CacheLookup.first == i+1) {
Chris Lattnerd4275422007-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 Stump11289f42009-09-09 15:08:12 +0000476
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000477 // Check each directory in sequence to see if it contains this file.
478 for (; i != SearchDirs.size(); ++i) {
Mike Stump11289f42009-09-09 15:08:12 +0000479 const FileEntry *FE =
Douglas Gregor97eec242011-09-15 22:00:41 +0000480 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
481 BuildingModule, SuggestedModule);
Chris Lattner712e3872007-12-17 08:13:48 +0000482 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000483
Chris Lattner712e3872007-12-17 08:13:48 +0000484 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000485
Chris Lattner712e3872007-12-17 08:13:48 +0000486 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor9f93e382011-07-28 04:45:53 +0000487 HeaderFileInfo &HFI = getFileInfo(FE);
488 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000489
Douglas Gregor9f93e382011-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 Lattner712e3872007-12-17 08:13:48 +0000501 // Remember this location for the next lookup we do.
502 CacheLookup.second = i;
503 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000504 }
Mike Stump11289f42009-09-09 15:08:12 +0000505
Douglas Gregord8575e12011-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 Gregor97eec242011-09-15 22:00:41 +0000520 SearchPath, RelativePath,
521 SuggestedModule);
Douglas Gregord8575e12011-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 Lattnerd4275422007-07-22 07:28:00 +0000530 // Otherwise, didn't find it. Remember we didn't find this.
531 CacheLookup.second = SearchDirs.size();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000532 return 0;
533}
534
Chris Lattner63dd32b2006-10-20 04:42:40 +0000535/// 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 Lattner0e62c1c2011-07-23 10:55:15 +0000541LookupSubframeworkHeader(StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000542 const FileEntry *ContextFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000543 SmallVectorImpl<char> *SearchPath,
544 SmallVectorImpl<char> *RelativePath) {
Chris Lattner12261882008-02-01 05:34:02 +0000545 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000546
Chris Lattner63dd32b2006-10-20 04:42:40 +0000547 // Framework names must have a '/' in the filename. Find it.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000548 size_t SlashPos = Filename.find('/');
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000549 if (SlashPos == StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000550
Chris Lattner63dd32b2006-10-20 04:42:40 +0000551 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000552 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner63dd32b2006-10-20 04:42:40 +0000554 // If the context info wasn't a framework, couldn't be a subframework.
Chris Lattner48043482006-10-27 05:12:36 +0000555 const char *FrameworkPos = strstr(ContextName, ".framework/");
556 if (FrameworkPos == 0)
Chris Lattner63dd32b2006-10-20 04:42:40 +0000557 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000558
559 llvm::SmallString<1024> FrameworkName(ContextName,
Chris Lattner23b7eb62007-06-15 23:05:46 +0000560 FrameworkPos+strlen(".framework/"));
Chris Lattner5ed76da2006-10-22 07:24:13 +0000561
Chris Lattner63dd32b2006-10-20 04:42:40 +0000562 // Append Frameworks/HIToolbox.framework/
563 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000564 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000565 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000566
Chris Lattner23b7eb62007-06-15 23:05:46 +0000567 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner8afa6de2010-11-21 09:55:08 +0000568 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000569
Chris Lattner5ed76da2006-10-22 07:24:13 +0000570 // Some other location?
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000571 if (CacheLookup.getValue() &&
572 CacheLookup.getKeyLength() == FrameworkName.size() &&
573 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
574 CacheLookup.getKeyLength()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000575 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000576
Chris Lattner5ed76da2006-10-22 07:24:13 +0000577 // Cache subframework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000578 if (CacheLookup.getValue() == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000579 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattner5ed76da2006-10-22 07:24:13 +0000581 // If the framework dir doesn't exist, we fail.
Chris Lattner5159f612010-11-23 08:35:12 +0000582 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000583 if (Dir == 0) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000584
Chris Lattner5ed76da2006-10-22 07:24:13 +0000585 // Otherwise, if it does, remember that this is the right direntry for this
586 // framework.
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000587 CacheLookup.setValue(Dir);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000588 }
Mike Stump11289f42009-09-09 15:08:12 +0000589
Chris Lattner577377e2006-10-20 04:55:45 +0000590 const FileEntry *FE = 0;
591
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000592 if (RelativePath != NULL) {
593 RelativePath->clear();
594 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
595 }
596
Chris Lattner63dd32b2006-10-20 04:42:40 +0000597 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Chris Lattner23b7eb62007-06-15 23:05:46 +0000598 llvm::SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000599 HeadersFilename += "Headers/";
Manuel Klimek0c69fd22011-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 Lattnerd081f8c2010-01-10 01:35:12 +0000606 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000607 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump11289f42009-09-09 15:08:12 +0000608
Chris Lattner63dd32b2006-10-20 04:42:40 +0000609 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000610 HeadersFilename = FrameworkName;
611 HeadersFilename += "PrivateHeaders/";
Manuel Klimek0c69fd22011-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 Lattnerd081f8c2010-01-10 01:35:12 +0000618 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000619 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000620 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000621 }
Mike Stump11289f42009-09-09 15:08:12 +0000622
Chris Lattner577377e2006-10-20 04:55:45 +0000623 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000624 //
Chris Lattnerf5c619f2008-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;
Chris Lattner577377e2006-10-20 04:55:45 +0000630 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000631}
632
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000633//===----------------------------------------------------------------------===//
634// File Info Management.
635//===----------------------------------------------------------------------===//
636
Douglas Gregor5d1bee22011-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 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000655
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000656 if (HFI.Framework.empty())
657 HFI.Framework = OtherHFI.Framework;
658
659 HFI.Resolved = true;
660}
661
Steve Naroff3fa455a2009-04-24 20:03:17 +0000662/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000663/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000664HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000665 if (FE->getUID() >= FileInfo.size())
666 FileInfo.resize(FE->getUID()+1);
Douglas Gregor09b69892011-02-10 17:09:37 +0000667
668 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000669 if (ExternalSource && !HFI.Resolved)
670 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregor09b69892011-02-10 17:09:37 +0000671 return HFI;
Mike Stump11289f42009-09-09 15:08:12 +0000672}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000673
Douglas Gregor37aa4932011-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 Gregor5d1bee22011-09-17 05:35:18 +0000681 if (ExternalSource && !HFI.Resolved)
682 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregor37aa4932011-05-04 00:14:37 +0000683
684 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
685}
686
Steve Naroff3fa455a2009-04-24 20:03:17 +0000687void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
688 if (UID >= FileInfo.size())
689 FileInfo.resize(UID+1);
Douglas Gregor09b69892011-02-10 17:09:37 +0000690 HFI.Resolved = true;
Steve Naroff3fa455a2009-04-24 20:03:17 +0000691 FileInfo[UID] = HFI;
692}
693
Chris Lattner59a9ebd2006-10-18 05:34:33 +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 Naroff3fa455a2009-04-24 20:03:17 +0000701 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump11289f42009-09-09 15:08:12 +0000702
Chris Lattner59a9ebd2006-10-18 05:34:33 +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 Stump11289f42009-09-09 15:08:12 +0000708
Chris Lattner59a9ebd2006-10-18 05:34:33 +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 Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattner59a9ebd2006-10-18 05:34:33 +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 Stump11289f42009-09-09 15:08:12 +0000720 if (const IdentifierInfo *ControllingMacro
Douglas Gregor99734e72009-04-25 23:30:02 +0000721 = FileInfo.getControllingMacro(ExternalLookup))
722 if (ControllingMacro->hasMacroDefinition()) {
723 ++NumMultiIncludeFileOptzn;
724 return false;
725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000727 // Increment the number of times this file has been included.
728 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +0000729
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000730 return true;
731}
732
Ted Kremenekfbcce6f2011-07-26 23:46:11 +0000733size_t HeaderSearch::getTotalMemory() const {
734 return SearchDirs.capacity()
Ted Kremenekae63d102011-07-27 18:41:18 +0000735 + llvm::capacity_in_bytes(FileInfo)
736 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekfbcce6f2011-07-26 23:46:11 +0000737 + LookupFileCache.getAllocator().getTotalMemory()
738 + FrameworkMap.getAllocator().getTotalMemory();
739}
Douglas Gregor9f93e382011-07-28 04:45:53 +0000740
741StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
742 return FrameworkNames.GetOrCreateValue(Framework).getKey();
743}
Douglas Gregor718292f2011-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 Gregoraf28ec82011-11-12 00:05:07 +0000761 // Try to load the module map file in this directory.
Douglas Gregor80b69042011-11-12 00:22:19 +0000762 switch (loadModuleMapFile(Dir)) {
763 case LMM_NewlyLoaded:
764 case LMM_AlreadyLoaded:
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000765 // Success. All of the directories we stepped through inherit this module
766 // map file.
Douglas Gregor718292f2011-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 Gregor80b69042011-11-12 00:22:19 +0000771
772 case LMM_NoDirectory:
773 case LMM_InvalidModuleMap:
774 break;
Douglas Gregor718292f2011-11-11 19:10:28 +0000775 }
Douglas Gregor718292f2011-11-11 19:10:28 +0000776
Douglas Gregoraf28ec82011-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 Gregor718292f2011-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 Gregorde3ef502011-11-30 23:21:26 +0000789Module *HeaderSearch::findModuleForHeader(const FileEntry *File) {
790 if (Module *Module = ModMap.findModuleForHeader(File))
Douglas Gregorc04f6442011-11-17 22:44:56 +0000791 return Module;
Douglas Gregorab0c8a82011-11-11 22:18:48 +0000792
Douglas Gregorc04f6442011-11-17 22:44:56 +0000793 return 0;
Douglas Gregor718292f2011-11-11 19:10:28 +0000794}
795
Douglas Gregor2b20cb82011-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
Douglas Gregorde3ef502011-11-30 23:21:26 +0000809Module *HeaderSearch::getModule(StringRef Name, bool AllowSearch) {
810 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000811 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:
Douglas Gregorde3ef502011-11-30 23:21:26 +0000827 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000828 return Module;
829 break;
830 }
831 }
832
833 return 0;
834}
Douglas Gregor56c64012011-11-17 01:41:17 +0000835
Douglas Gregorde3ef502011-11-30 23:21:26 +0000836Module *HeaderSearch::getFrameworkModule(StringRef Name,
Douglas Gregor56c64012011-11-17 01:41:17 +0000837 const DirectoryEntry *Dir) {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000838 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor56c64012011-11-17 01:41:17 +0000839 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 Gregor2b20cb82011-11-16 00:09:06 +0000858
Douglas Gregor80b69042011-11-12 00:22:19 +0000859HeaderSearch::LoadModuleMapResult
860HeaderSearch::loadModuleMapFile(StringRef DirName) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000861 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
862 return loadModuleMapFile(Dir);
863
Douglas Gregor80b69042011-11-12 00:22:19 +0000864 return LMM_NoDirectory;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000865}
866
Douglas Gregor80b69042011-11-12 00:22:19 +0000867HeaderSearch::LoadModuleMapResult
868HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000869 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
870 = DirectoryHasModuleMap.find(Dir);
871 if (KnownDir != DirectoryHasModuleMap.end())
Douglas Gregor80b69042011-11-12 00:22:19 +0000872 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-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 Gregor80b69042011-11-12 00:22:19 +0000883 return LMM_NewlyLoaded;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000884 }
885 }
886
887 // No suitable module map.
888 DirectoryHasModuleMap[Dir] = false;
Douglas Gregor80b69042011-11-12 00:22:19 +0000889 return LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000890}
Douglas Gregor718292f2011-11-11 19:10:28 +0000891