blob: 318e57dc01a0619fba9c053b18d9d7b1c8e20c30 [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 Gregor6e975c42011-09-13 23:15:45 +0000105 std::string *ModuleFileName,
Douglas Gregor21cae202011-09-12 23:31:24 +0000106 std::string *UmbrellaHeader) {
Douglas Gregor9a6da692011-09-12 20:41:59 +0000107 // If we don't have a module cache path, we can't do anything.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000108 if (ModuleCachePath.empty()) {
109 if (ModuleFileName)
110 ModuleFileName->clear();
Douglas Gregor9a6da692011-09-12 20:41:59 +0000111 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000112 }
113
Douglas Gregor21cae202011-09-12 23:31:24 +0000114 // Try to find the module path.
Douglas Gregor9a6da692011-09-12 20:41:59 +0000115 llvm::SmallString<256> FileName(ModuleCachePath);
116 llvm::sys::path::append(FileName, ModuleName + ".pcm");
Douglas Gregor6e975c42011-09-13 23:15:45 +0000117 if (ModuleFileName)
118 *ModuleFileName = FileName.str();
119
120 if (const FileEntry *ModuleFile
121 = getFileMgr().getFile(FileName, /*OpenFile=*/false,
122 /*CacheFailure=*/false))
Douglas Gregor21cae202011-09-12 23:31:24 +0000123 return ModuleFile;
124
125 // We didn't find the module. If we're not supposed to look for an
126 // umbrella header, this is the end of the road.
127 if (!UmbrellaHeader)
128 return 0;
129
Douglas Gregorcf70d782011-11-12 00:05:07 +0000130 // Look in the module map to determine if there is a module by this name.
131 ModuleMap::Module *Module = ModMap.findModule(ModuleName);
132 if (!Module) {
133 // Look through the various header search paths to load any avaiable module
134 // maps, searching for a module map that describes this module.
135 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
136 // Skip non-normal include paths
137 if (!SearchDirs[Idx].isNormalDir())
138 continue;
139
Douglas Gregor26697972011-11-12 00:22:19 +0000140 // Search for a module map file in this directory.
141 if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) {
142 // We just loaded a module map file; check whether the module is
143 // available now.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000144 Module = ModMap.findModule(ModuleName);
145 if (Module)
146 break;
147 }
Douglas Gregor26697972011-11-12 00:22:19 +0000148
Douglas Gregorcf70d782011-11-12 00:05:07 +0000149 // Search for a module map in a subdirectory with the same name as the
150 // module.
151 llvm::SmallString<128> NestedModuleMapDirName;
152 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
153 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
Douglas Gregor26697972011-11-12 00:22:19 +0000154 if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) {
155 // If we just loaded a module map file, look for the module again.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000156 Module = ModMap.findModule(ModuleName);
157 if (Module)
158 break;
159 }
160 }
161 }
162
163 // If we have a module with an umbrella header
Douglas Gregor484535e2011-11-11 23:20:24 +0000164 // FIXME: Even if it doesn't have an umbrella header, we should be able to
165 // handle the module. However, the caller isn't ready for that yet.
Douglas Gregorcf70d782011-11-12 00:05:07 +0000166 if (Module && Module->UmbrellaHeader) {
167 *UmbrellaHeader = Module->UmbrellaHeader->getName();
168 return 0;
Douglas Gregor484535e2011-11-11 23:20:24 +0000169 }
170
Douglas Gregor21cae202011-09-12 23:31:24 +0000171 // Look in each of the framework directories for an umbrella header with
172 // the same name as the module.
Douglas Gregor21cae202011-09-12 23:31:24 +0000173 llvm::SmallString<128> UmbrellaHeaderName;
174 UmbrellaHeaderName = ModuleName;
175 UmbrellaHeaderName += '/';
176 UmbrellaHeaderName += ModuleName;
177 UmbrellaHeaderName += ".h";
178 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
179 // Skip non-framework include paths
180 if (!SearchDirs[Idx].isFramework())
181 continue;
182
183 // Look for the umbrella header in this directory.
184 if (const FileEntry *HeaderFile
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000185 = SearchDirs[Idx].LookupFile(UmbrellaHeaderName, *this, 0, 0,
186 StringRef(), 0)) {
Douglas Gregor21cae202011-09-12 23:31:24 +0000187 *UmbrellaHeader = HeaderFile->getName();
188 return 0;
189 }
190 }
191
192 // We did not find an umbrella header. Clear out the UmbrellaHeader pointee
193 // so our caller knows that we failed.
194 UmbrellaHeader->clear();
195 return 0;
Douglas Gregor9a6da692011-09-12 20:41:59 +0000196}
197
Chris Lattnerdf772332007-12-17 07:52:39 +0000198//===----------------------------------------------------------------------===//
199// File lookup within a DirectoryLookup scope
200//===----------------------------------------------------------------------===//
201
Chris Lattner3af66a92007-12-17 17:57:27 +0000202/// getName - Return the directory or filename corresponding to this lookup
203/// object.
204const char *DirectoryLookup::getName() const {
205 if (isNormalDir())
206 return getDir()->getName();
207 if (isFramework())
208 return getFrameworkDir()->getName();
209 assert(isHeaderMap() && "Unknown DirectoryLookup");
210 return getHeaderMap()->getFileName();
211}
212
213
Chris Lattnerdf772332007-12-17 07:52:39 +0000214/// LookupFile - Lookup the specified file in this search path, returning it
215/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000216const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000217 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000218 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000219 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000220 SmallVectorImpl<char> *RelativePath,
221 StringRef BuildingModule,
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000222 ModuleMap::Module **SuggestedModule) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000223 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000224 if (isNormalDir()) {
225 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000226 TmpDir = getDir()->getName();
227 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000228 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000229 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000230 SearchPath->clear();
231 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
232 }
233 if (RelativePath != NULL) {
234 RelativePath->clear();
235 RelativePath->append(Filename.begin(), Filename.end());
236 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000237
238 // If we have a module map that might map this header, load it and
239 // check whether we'll have a suggestion for a module.
240 if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) {
241 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
242 /*openFile=*/false);
243 if (!File)
244 return File;
245
246 // If there is a module that corresponds to this header,
247 // suggest it.
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000248 ModuleMap::Module *Module = HS.findModuleForHeader(File);
249 if (Module && Module->getTopLevelModuleName() != BuildingModule)
Douglas Gregora30cfe52011-11-11 19:10:28 +0000250 *SuggestedModule = Module;
251
252 return File;
253 }
254
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000255 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattnerafded5b2007-12-17 08:13:48 +0000258 if (isFramework())
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000259 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
260 BuildingModule, SuggestedModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000262 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000263 const FileEntry * const Result = getHeaderMap()->LookupFile(
264 Filename, HS.getFileMgr());
265 if (Result) {
266 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000267 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000268 SearchPath->clear();
269 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
270 }
271 if (RelativePath != NULL) {
272 RelativePath->clear();
273 RelativePath->append(Filename.begin(), Filename.end());
274 }
275 }
276 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000277}
278
279
Chris Lattnerafded5b2007-12-17 08:13:48 +0000280/// DoFrameworkLookup - Do a lookup of the specified file in the current
281/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000282const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000283 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000284 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000285 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000286 SmallVectorImpl<char> *RelativePath,
287 StringRef BuildingModule,
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000288 ModuleMap::Module **SuggestedModule) const
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000289{
Chris Lattnerafded5b2007-12-17 08:13:48 +0000290 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Reid Spencer5f016e22007-07-11 17:01:13 +0000292 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000293 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000294 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Chris Lattnerafded5b2007-12-17 08:13:48 +0000296 // Find out if this is the home for the specified framework, by checking
297 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000298 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000299 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattnerafded5b2007-12-17 08:13:48 +0000301 // If it is known and in some other directory, fail.
302 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Chris Lattnerafded5b2007-12-17 08:13:48 +0000305 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // FrameworkName = "/System/Library/Frameworks/"
308 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000309 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 if (FrameworkName.empty() || FrameworkName.back() != '/')
311 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000314 StringRef ModuleName(Filename.begin(), SlashPos);
315 FrameworkName += ModuleName;
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
318 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Chris Lattnerafded5b2007-12-17 08:13:48 +0000320 // If the cache entry is still unresolved, query to see if the cache entry is
321 // still unresolved. If so, check its existence now.
322 if (FrameworkDirCache == 0) {
323 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000326 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000327 bool Exists;
328 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 // Otherwise, if it does, remember that this is the right direntry for this
332 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000333 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Manuel Klimek74124942011-04-26 21:50:03 +0000336 if (RelativePath != NULL) {
337 RelativePath->clear();
338 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
339 }
340
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000341 // If we're allowed to look for modules, try to load or create the module
342 // corresponding to this framework.
343 ModuleMap::Module *Module = 0;
344 if (SuggestedModule) {
345 if (const DirectoryEntry *FrameworkDir
346 = FileMgr.getDirectory(FrameworkName)) {
347 if ((Module = HS.getFrameworkModule(ModuleName, FrameworkDir)) &&
348 Module->Name == BuildingModule)
349 Module = 0;
350 }
351 }
352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
354 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000357
358 if (SearchPath != NULL) {
359 SearchPath->clear();
360 // Without trailing '/'.
361 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
362 }
363
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000364 // Determine whether this is the module we're building or not.
365 // FIXME: Do we still need the ".." hack?
366 bool AutomaticImport = Module &&
Douglas Gregor0fd787b2011-09-16 00:22:46 +0000367 !Filename.substr(SlashPos + 1).startswith("..");
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000368
Chris Lattnera1394812010-01-10 01:35:12 +0000369 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000370 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000371 /*openFile=*/!AutomaticImport)) {
372 if (AutomaticImport)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000373 *SuggestedModule = Module;
Reid Spencer5f016e22007-07-11 17:01:13 +0000374 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
378 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000379 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000381 if (SearchPath != NULL)
382 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
383 Private+strlen(Private));
384
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000385 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
386 /*openFile=*/!AutomaticImport);
387 if (FE && AutomaticImport)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000388 *SuggestedModule = Module;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000389 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000390}
391
Chris Lattnerdf772332007-12-17 07:52:39 +0000392
Chris Lattnerafded5b2007-12-17 08:13:48 +0000393//===----------------------------------------------------------------------===//
394// Header File Location.
395//===----------------------------------------------------------------------===//
396
397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
399/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000400/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
401/// non-null, indicates where the #including file is, in case a relative search
402/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000403const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000404 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000405 bool isAngled,
406 const DirectoryLookup *FromDir,
407 const DirectoryLookup *&CurDir,
408 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000409 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000410 SmallVectorImpl<char> *RelativePath,
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000411 ModuleMap::Module **SuggestedModule,
412 bool SkipCache)
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000413{
414 if (SuggestedModule)
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000415 *SuggestedModule = 0;
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000418 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 CurDir = 0;
420
421 // If this was an #include_next "/absolute/file", fail.
422 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Manuel Klimek74124942011-04-26 21:50:03 +0000424 if (SearchPath != NULL)
425 SearchPath->clear();
426 if (RelativePath != NULL) {
427 RelativePath->clear();
428 RelativePath->append(Filename.begin(), Filename.end());
429 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000431 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000434 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000435 // directory. This has to be based on CurFileEnt, not CurDir, because
436 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000437 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
438 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000439 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
440 llvm::SmallString<1024> TmpDir;
441 // Concatenate the requested file onto the directory.
442 // FIXME: Portability. Filename concatenation should be in sys::Path.
443 TmpDir += CurFileEnt->getDir()->getName();
444 TmpDir.push_back('/');
445 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000446 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000448 // This file is a system header or C++ unfriendly if the old file is.
449 //
450 // Note that the temporary 'DirInfo' is required here, as either call to
451 // getFileInfo could resize the vector and we don't want to rely on order
452 // of evaluation.
453 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000454 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000455 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000456 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000457 SearchPath->clear();
458 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
459 }
460 if (RelativePath != NULL) {
461 RelativePath->clear();
462 RelativePath->append(Filename.begin(), Filename.end());
463 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 return FE;
465 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 CurDir = 0;
469
470 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000471 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 // If this is a #include_next request, start searching after the directory the
474 // file was found in.
475 if (FromDir)
476 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattner9960ae82007-07-22 07:28:00 +0000478 // Cache all of the lookups performed by this method. Many headers are
479 // multiply included, and the "pragma once" optimization prevents them from
480 // being relex/pp'd, but they would still have to search through a
481 // (potentially huge) series of SearchDirs to find it.
482 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000483 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000484
485 // If the entry has been previously looked up, the first value will be
486 // non-zero. If the value is equal to i (the start point of our search), then
487 // this is a matching hit.
Douglas Gregor1c2e9332011-11-20 17:46:46 +0000488 if (!SkipCache && CacheLookup.first == i+1) {
Chris Lattner9960ae82007-07-22 07:28:00 +0000489 // Skip querying potentially lots of directories for this lookup.
490 i = CacheLookup.second;
491 } else {
492 // Otherwise, this is the first query, or the previous query didn't match
493 // our search start. We will fill in our found location below, so prime the
494 // start point value.
495 CacheLookup.first = i+1;
496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 // Check each directory in sequence to see if it contains this file.
499 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000500 const FileEntry *FE =
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000501 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
502 BuildingModule, SuggestedModule);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000503 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Chris Lattnerafded5b2007-12-17 08:13:48 +0000505 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Chris Lattnerafded5b2007-12-17 08:13:48 +0000507 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000508 HeaderFileInfo &HFI = getFileInfo(FE);
509 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000511 // If this file is found in a header map and uses the framework style of
512 // includes, then this header is part of a framework we're building.
513 if (CurDir->isIndexHeaderMap()) {
514 size_t SlashPos = Filename.find('/');
515 if (SlashPos != StringRef::npos) {
516 HFI.IndexHeaderMapHeader = 1;
517 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
518 SlashPos));
519 }
520 }
521
Chris Lattnerafded5b2007-12-17 08:13:48 +0000522 // Remember this location for the next lookup we do.
523 CacheLookup.second = i;
524 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000527 // If we are including a file with a quoted include "foo.h" from inside
528 // a header in a framework that is currently being built, and we couldn't
529 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
530 // "Foo" is the name of the framework in which the including header was found.
531 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
532 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
533 if (IncludingHFI.IndexHeaderMapHeader) {
534 llvm::SmallString<128> ScratchFilename;
535 ScratchFilename += IncludingHFI.Framework;
536 ScratchFilename += '/';
537 ScratchFilename += Filename;
538
539 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
540 FromDir, CurDir, CurFileEnt,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000541 SearchPath, RelativePath,
542 SuggestedModule);
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000543 std::pair<unsigned, unsigned> &CacheLookup
544 = LookupFileCache.GetOrCreateValue(Filename).getValue();
545 CacheLookup.second
546 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
547 return Result;
548 }
549 }
550
Chris Lattner9960ae82007-07-22 07:28:00 +0000551 // Otherwise, didn't find it. Remember we didn't find this.
552 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 return 0;
554}
555
556/// LookupSubframeworkHeader - Look up a subframework for the specified
557/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
558/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
559/// is a subframework within Carbon.framework. If so, return the FileEntry
560/// for the designated file, otherwise return null.
561const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000562LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000563 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000564 SmallVectorImpl<char> *SearchPath,
565 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000566 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000569 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000570 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 // Look up the base framework name of the ContextFileEnt.
573 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 // If the context info wasn't a framework, couldn't be a subframework.
576 const char *FrameworkPos = strstr(ContextName, ".framework/");
577 if (FrameworkPos == 0)
578 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000579
580 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 FrameworkPos+strlen(".framework/"));
582
583 // Append Frameworks/HIToolbox.framework/
584 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000585 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 FrameworkName += ".framework/";
587
588 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000589 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 // Some other location?
592 if (CacheLookup.getValue() &&
593 CacheLookup.getKeyLength() == FrameworkName.size() &&
594 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
595 CacheLookup.getKeyLength()) != 0)
596 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 // Cache subframework.
599 if (CacheLookup.getValue() == 0) {
600 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000603 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 // Otherwise, if it does, remember that this is the right direntry for this
607 // framework.
608 CacheLookup.setValue(Dir);
609 }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 const FileEntry *FE = 0;
612
Manuel Klimek74124942011-04-26 21:50:03 +0000613 if (RelativePath != NULL) {
614 RelativePath->clear();
615 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
616 }
617
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
619 llvm::SmallString<1024> HeadersFilename(FrameworkName);
620 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000621 if (SearchPath != NULL) {
622 SearchPath->clear();
623 // Without trailing '/'.
624 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
625 }
626
Chris Lattnera1394812010-01-10 01:35:12 +0000627 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000628 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
631 HeadersFilename = FrameworkName;
632 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000633 if (SearchPath != NULL) {
634 SearchPath->clear();
635 // Without trailing '/'.
636 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
637 }
638
Chris Lattnera1394812010-01-10 01:35:12 +0000639 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000640 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 return 0;
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000645 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000646 // Note that the temporary 'DirInfo' is required here, as either call to
647 // getFileInfo could resize the vector and we don't want to rely on order
648 // of evaluation.
649 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
650 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 return FE;
652}
653
654//===----------------------------------------------------------------------===//
655// File Info Management.
656//===----------------------------------------------------------------------===//
657
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000658/// \brief Merge the header file info provided by \p OtherHFI into the current
659/// header file info (\p HFI)
660static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
661 const HeaderFileInfo &OtherHFI) {
662 HFI.isImport |= OtherHFI.isImport;
663 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
664 HFI.NumIncludes += OtherHFI.NumIncludes;
665
666 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
667 HFI.ControllingMacro = OtherHFI.ControllingMacro;
668 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
669 }
670
671 if (OtherHFI.External) {
672 HFI.DirInfo = OtherHFI.DirInfo;
673 HFI.External = OtherHFI.External;
674 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
675 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000676
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000677 if (HFI.Framework.empty())
678 HFI.Framework = OtherHFI.Framework;
679
680 HFI.Resolved = true;
681}
682
Steve Naroff83d63c72009-04-24 20:03:17 +0000683/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000684/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000685HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 if (FE->getUID() >= FileInfo.size())
687 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000688
689 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000690 if (ExternalSource && !HFI.Resolved)
691 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000692 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000693}
Reid Spencer5f016e22007-07-11 17:01:13 +0000694
Douglas Gregordd3e5542011-05-04 00:14:37 +0000695bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
696 // Check if we've ever seen this file as a header.
697 if (File->getUID() >= FileInfo.size())
698 return false;
699
700 // Resolve header file info from the external source, if needed.
701 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000702 if (ExternalSource && !HFI.Resolved)
703 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregordd3e5542011-05-04 00:14:37 +0000704
705 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
706}
707
Steve Naroff83d63c72009-04-24 20:03:17 +0000708void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
709 if (UID >= FileInfo.size())
710 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000711 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000712 FileInfo[UID] = HFI;
713}
714
Reid Spencer5f016e22007-07-11 17:01:13 +0000715/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
716/// #include, #include_next, or #import directive. Return false if #including
717/// the file will have no effect or true if we should include it.
718bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
719 ++NumIncluded; // Count # of attempted #includes.
720
721 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000722 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 // If this is a #import directive, check that we have not already imported
725 // this header.
726 if (isImport) {
727 // If this has already been imported, don't import it again.
728 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 // Has this already been #import'ed or #include'd?
731 if (FileInfo.NumIncludes) return false;
732 } else {
733 // Otherwise, if this is a #include of a file that was previously #import'd
734 // or if this is the second #include of a #pragma once file, ignore it.
735 if (FileInfo.isImport)
736 return false;
737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
740 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000741 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000742 = FileInfo.getControllingMacro(ExternalLookup))
743 if (ControllingMacro->hasMacroDefinition()) {
744 ++NumMultiIncludeFileOptzn;
745 return false;
746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 // Increment the number of times this file has been included.
749 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 return true;
752}
753
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000754size_t HeaderSearch::getTotalMemory() const {
755 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000756 + llvm::capacity_in_bytes(FileInfo)
757 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000758 + LookupFileCache.getAllocator().getTotalMemory()
759 + FrameworkMap.getAllocator().getTotalMemory();
760}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000761
762StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
763 return FrameworkNames.GetOrCreateValue(Framework).getKey();
764}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000765
766bool HeaderSearch::hasModuleMap(StringRef FileName,
767 const DirectoryEntry *Root) {
768 llvm::SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
769
770 StringRef DirName = FileName;
771 do {
772 // Get the parent directory name.
773 DirName = llvm::sys::path::parent_path(DirName);
774 if (DirName.empty())
775 return false;
776
777 // Determine whether this directory exists.
778 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
779 if (!Dir)
780 return false;
781
Douglas Gregorcf70d782011-11-12 00:05:07 +0000782 // Try to load the module map file in this directory.
Douglas Gregor26697972011-11-12 00:22:19 +0000783 switch (loadModuleMapFile(Dir)) {
784 case LMM_NewlyLoaded:
785 case LMM_AlreadyLoaded:
Douglas Gregorcf70d782011-11-12 00:05:07 +0000786 // Success. All of the directories we stepped through inherit this module
787 // map file.
Douglas Gregora30cfe52011-11-11 19:10:28 +0000788 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
789 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
790
791 return true;
Douglas Gregor26697972011-11-12 00:22:19 +0000792
793 case LMM_NoDirectory:
794 case LMM_InvalidModuleMap:
795 break;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000796 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000797
Douglas Gregorcf70d782011-11-12 00:05:07 +0000798 // If we hit the top of our search, we're done.
799 if (Dir == Root)
800 return false;
801
Douglas Gregora30cfe52011-11-11 19:10:28 +0000802 // Keep track of all of the directories we checked, so we can mark them as
803 // having module maps if we eventually do find a module map.
804 FixUpDirectories.push_back(Dir);
805 } while (true);
806
807 return false;
808}
809
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000810ModuleMap::Module *HeaderSearch::findModuleForHeader(const FileEntry *File) {
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000811 if (ModuleMap::Module *Module = ModMap.findModuleForHeader(File))
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000812 return Module;
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000813
Douglas Gregorc69c42e2011-11-17 22:44:56 +0000814 return 0;
Douglas Gregora30cfe52011-11-11 19:10:28 +0000815}
816
Douglas Gregordb1cde72011-11-16 00:09:06 +0000817bool HeaderSearch::loadModuleMapFile(const FileEntry *File) {
818 const DirectoryEntry *Dir = File->getDir();
819
820 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
821 = DirectoryHasModuleMap.find(Dir);
822 if (KnownDir != DirectoryHasModuleMap.end())
823 return !KnownDir->second;
824
825 bool Result = ModMap.parseModuleMapFile(File);
826 DirectoryHasModuleMap[Dir] = !Result;
827 return Result;
828}
829
830ModuleMap::Module *HeaderSearch::getModule(StringRef Name, bool AllowSearch) {
831 if (ModuleMap::Module *Module = ModMap.findModule(Name))
832 return Module;
833
834 if (!AllowSearch)
835 return 0;
836
837 for (unsigned I = 0, N = SearchDirs.size(); I != N; ++I) {
838 if (!SearchDirs[I].isNormalDir())
839 continue;
840
841 switch (loadModuleMapFile(SearchDirs[I].getDir())) {
842 case LMM_AlreadyLoaded:
843 case LMM_InvalidModuleMap:
844 case LMM_NoDirectory:
845 break;
846
847 case LMM_NewlyLoaded:
848 if (ModuleMap::Module *Module = ModMap.findModule(Name))
849 return Module;
850 break;
851 }
852 }
853
854 return 0;
855}
Douglas Gregor2821c7f2011-11-17 01:41:17 +0000856
857ModuleMap::Module *HeaderSearch::getFrameworkModule(StringRef Name,
858 const DirectoryEntry *Dir) {
859 if (ModuleMap::Module *Module = ModMap.findModule(Name))
860 return Module;
861
862 // Try to load a module map file.
863 switch (loadModuleMapFile(Dir)) {
864 case LMM_InvalidModuleMap:
865 break;
866
867 case LMM_AlreadyLoaded:
868 case LMM_NoDirectory:
869 return 0;
870
871 case LMM_NewlyLoaded:
872 return ModMap.findModule(Name);
873 }
874
875 // Try to infer a module map.
876 return ModMap.inferFrameworkModule(Name, Dir);
877}
878
Douglas Gregordb1cde72011-11-16 00:09:06 +0000879
Douglas Gregor26697972011-11-12 00:22:19 +0000880HeaderSearch::LoadModuleMapResult
881HeaderSearch::loadModuleMapFile(StringRef DirName) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000882 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
883 return loadModuleMapFile(Dir);
884
Douglas Gregor26697972011-11-12 00:22:19 +0000885 return LMM_NoDirectory;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000886}
887
Douglas Gregor26697972011-11-12 00:22:19 +0000888HeaderSearch::LoadModuleMapResult
889HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) {
Douglas Gregorcf70d782011-11-12 00:05:07 +0000890 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
891 = DirectoryHasModuleMap.find(Dir);
892 if (KnownDir != DirectoryHasModuleMap.end())
Douglas Gregor26697972011-11-12 00:22:19 +0000893 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000894
895 llvm::SmallString<128> ModuleMapFileName;
896 ModuleMapFileName += Dir->getName();
897 llvm::sys::path::append(ModuleMapFileName, "module.map");
898 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) {
899 // We have found a module map file. Try to parse it.
900 if (!ModMap.parseModuleMapFile(ModuleMapFile)) {
901 // This directory has a module map.
902 DirectoryHasModuleMap[Dir] = true;
903
Douglas Gregor26697972011-11-12 00:22:19 +0000904 return LMM_NewlyLoaded;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000905 }
906 }
907
908 // No suitable module map.
909 DirectoryHasModuleMap[Dir] = false;
Douglas Gregor26697972011-11-12 00:22:19 +0000910 return LMM_InvalidModuleMap;
Douglas Gregorcf70d782011-11-12 00:05:07 +0000911}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000912