blob: 3f50285430eff883cb1d41703f128d37b88d25a9 [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 Gregor484535e2011-11-11 23:20:24 +0000130 // Look in the module map to determine if there is a module by this name
131 // that has an umbrella header.
132 // FIXME: Even if it doesn't have an umbrella header, we should be able to
133 // handle the module. However, the caller isn't ready for that yet.
134 if (ModuleMap::Module *Module = ModMap.findModule(ModuleName)) {
135 if (Module->UmbrellaHeader) {
136 *UmbrellaHeader = Module->UmbrellaHeader->getName();
137 return 0;
138 }
139 }
140
Douglas Gregor21cae202011-09-12 23:31:24 +0000141 // Look in each of the framework directories for an umbrella header with
142 // the same name as the module.
Douglas Gregor21cae202011-09-12 23:31:24 +0000143 llvm::SmallString<128> UmbrellaHeaderName;
144 UmbrellaHeaderName = ModuleName;
145 UmbrellaHeaderName += '/';
146 UmbrellaHeaderName += ModuleName;
147 UmbrellaHeaderName += ".h";
148 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
149 // Skip non-framework include paths
150 if (!SearchDirs[Idx].isFramework())
151 continue;
152
153 // Look for the umbrella header in this directory.
154 if (const FileEntry *HeaderFile
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000155 = SearchDirs[Idx].LookupFile(UmbrellaHeaderName, *this, 0, 0,
156 StringRef(), 0)) {
Douglas Gregor21cae202011-09-12 23:31:24 +0000157 *UmbrellaHeader = HeaderFile->getName();
158 return 0;
159 }
160 }
161
162 // We did not find an umbrella header. Clear out the UmbrellaHeader pointee
163 // so our caller knows that we failed.
164 UmbrellaHeader->clear();
165 return 0;
Douglas Gregor9a6da692011-09-12 20:41:59 +0000166}
167
Chris Lattnerdf772332007-12-17 07:52:39 +0000168//===----------------------------------------------------------------------===//
169// File lookup within a DirectoryLookup scope
170//===----------------------------------------------------------------------===//
171
Chris Lattner3af66a92007-12-17 17:57:27 +0000172/// getName - Return the directory or filename corresponding to this lookup
173/// object.
174const char *DirectoryLookup::getName() const {
175 if (isNormalDir())
176 return getDir()->getName();
177 if (isFramework())
178 return getFrameworkDir()->getName();
179 assert(isHeaderMap() && "Unknown DirectoryLookup");
180 return getHeaderMap()->getFileName();
181}
182
183
Chris Lattnerdf772332007-12-17 07:52:39 +0000184/// LookupFile - Lookup the specified file in this search path, returning it
185/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000186const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000188 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000189 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000190 SmallVectorImpl<char> *RelativePath,
191 StringRef BuildingModule,
192 StringRef *SuggestedModule) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000193 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000194 if (isNormalDir()) {
195 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000196 TmpDir = getDir()->getName();
197 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000198 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000200 SearchPath->clear();
201 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
202 }
203 if (RelativePath != NULL) {
204 RelativePath->clear();
205 RelativePath->append(Filename.begin(), Filename.end());
206 }
Douglas Gregora30cfe52011-11-11 19:10:28 +0000207
208 // If we have a module map that might map this header, load it and
209 // check whether we'll have a suggestion for a module.
210 if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) {
211 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
212 /*openFile=*/false);
213 if (!File)
214 return File;
215
216 // If there is a module that corresponds to this header,
217 // suggest it.
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000218 StringRef Module = HS.findModuleForHeader(File);
Douglas Gregora30cfe52011-11-11 19:10:28 +0000219 if (!Module.empty() && Module != BuildingModule)
220 *SuggestedModule = Module;
221
222 return File;
223 }
224
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000225 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattnerafded5b2007-12-17 08:13:48 +0000228 if (isFramework())
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000229 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
230 BuildingModule, SuggestedModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000232 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000233 const FileEntry * const Result = getHeaderMap()->LookupFile(
234 Filename, HS.getFileMgr());
235 if (Result) {
236 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000237 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000238 SearchPath->clear();
239 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
240 }
241 if (RelativePath != NULL) {
242 RelativePath->clear();
243 RelativePath->append(Filename.begin(), Filename.end());
244 }
245 }
246 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000247}
248
249
Chris Lattnerafded5b2007-12-17 08:13:48 +0000250/// DoFrameworkLookup - Do a lookup of the specified file in the current
251/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000252const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000254 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000255 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000256 SmallVectorImpl<char> *RelativePath,
257 StringRef BuildingModule,
258 StringRef *SuggestedModule) const
259{
Chris Lattnerafded5b2007-12-17 08:13:48 +0000260 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000263 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattnerafded5b2007-12-17 08:13:48 +0000266 // Find out if this is the home for the specified framework, by checking
267 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000268 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000269 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattnerafded5b2007-12-17 08:13:48 +0000271 // If it is known and in some other directory, fail.
272 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnerafded5b2007-12-17 08:13:48 +0000275 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 // FrameworkName = "/System/Library/Frameworks/"
278 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000279 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 if (FrameworkName.empty() || FrameworkName.back() != '/')
281 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000284 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
287 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattnerafded5b2007-12-17 08:13:48 +0000289 // If the cache entry is still unresolved, query to see if the cache entry is
290 // still unresolved. If so, check its existence now.
291 if (FrameworkDirCache == 0) {
292 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000295 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000296 bool Exists;
297 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 // Otherwise, if it does, remember that this is the right direntry for this
301 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000302 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Manuel Klimek74124942011-04-26 21:50:03 +0000305 if (RelativePath != NULL) {
306 RelativePath->clear();
307 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
308 }
309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
311 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000314
315 if (SearchPath != NULL) {
316 SearchPath->clear();
317 // Without trailing '/'.
318 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
319 }
320
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000321 /// Determine whether this is the module we're building or not.
322 bool AutomaticImport = SuggestedModule &&
Douglas Gregor0fd787b2011-09-16 00:22:46 +0000323 (BuildingModule != StringRef(Filename.begin(), SlashPos)) &&
324 !Filename.substr(SlashPos + 1).startswith("..");
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000325
Chris Lattnera1394812010-01-10 01:35:12 +0000326 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000327 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000328 /*openFile=*/!AutomaticImport)) {
329 if (AutomaticImport)
330 *SuggestedModule = StringRef(Filename.begin(), SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
335 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000336 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000338 if (SearchPath != NULL)
339 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
340 Private+strlen(Private));
341
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000342 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
343 /*openFile=*/!AutomaticImport);
344 if (FE && AutomaticImport)
345 *SuggestedModule = StringRef(Filename.begin(), SlashPos);
346 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000347}
348
Chris Lattnerdf772332007-12-17 07:52:39 +0000349
Chris Lattnerafded5b2007-12-17 08:13:48 +0000350//===----------------------------------------------------------------------===//
351// Header File Location.
352//===----------------------------------------------------------------------===//
353
354
Reid Spencer5f016e22007-07-11 17:01:13 +0000355/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
356/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000357/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
358/// non-null, indicates where the #including file is, in case a relative search
359/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000360const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000361 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000362 bool isAngled,
363 const DirectoryLookup *FromDir,
364 const DirectoryLookup *&CurDir,
365 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000366 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000367 SmallVectorImpl<char> *RelativePath,
368 StringRef *SuggestedModule)
369{
370 if (SuggestedModule)
371 *SuggestedModule = StringRef();
372
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000374 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 CurDir = 0;
376
377 // If this was an #include_next "/absolute/file", fail.
378 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Manuel Klimek74124942011-04-26 21:50:03 +0000380 if (SearchPath != NULL)
381 SearchPath->clear();
382 if (RelativePath != NULL) {
383 RelativePath->clear();
384 RelativePath->append(Filename.begin(), Filename.end());
385 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000387 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000390 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000391 // directory. This has to be based on CurFileEnt, not CurDir, because
392 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000393 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
394 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000395 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
396 llvm::SmallString<1024> TmpDir;
397 // Concatenate the requested file onto the directory.
398 // FIXME: Portability. Filename concatenation should be in sys::Path.
399 TmpDir += CurFileEnt->getDir()->getName();
400 TmpDir.push_back('/');
401 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000402 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000404 // This file is a system header or C++ unfriendly if the old file is.
405 //
406 // Note that the temporary 'DirInfo' is required here, as either call to
407 // getFileInfo could resize the vector and we don't want to rely on order
408 // of evaluation.
409 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000410 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000411 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000412 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000413 SearchPath->clear();
414 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
415 }
416 if (RelativePath != NULL) {
417 RelativePath->clear();
418 RelativePath->append(Filename.begin(), Filename.end());
419 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 return FE;
421 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 CurDir = 0;
425
426 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000427 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 // If this is a #include_next request, start searching after the directory the
430 // file was found in.
431 if (FromDir)
432 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattner9960ae82007-07-22 07:28:00 +0000434 // Cache all of the lookups performed by this method. Many headers are
435 // multiply included, and the "pragma once" optimization prevents them from
436 // being relex/pp'd, but they would still have to search through a
437 // (potentially huge) series of SearchDirs to find it.
438 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000439 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000440
441 // If the entry has been previously looked up, the first value will be
442 // non-zero. If the value is equal to i (the start point of our search), then
443 // this is a matching hit.
444 if (CacheLookup.first == i+1) {
445 // Skip querying potentially lots of directories for this lookup.
446 i = CacheLookup.second;
447 } else {
448 // Otherwise, this is the first query, or the previous query didn't match
449 // our search start. We will fill in our found location below, so prime the
450 // start point value.
451 CacheLookup.first = i+1;
452 }
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 // Check each directory in sequence to see if it contains this file.
455 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000456 const FileEntry *FE =
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000457 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
458 BuildingModule, SuggestedModule);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000459 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Chris Lattnerafded5b2007-12-17 08:13:48 +0000461 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattnerafded5b2007-12-17 08:13:48 +0000463 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000464 HeaderFileInfo &HFI = getFileInfo(FE);
465 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000467 // If this file is found in a header map and uses the framework style of
468 // includes, then this header is part of a framework we're building.
469 if (CurDir->isIndexHeaderMap()) {
470 size_t SlashPos = Filename.find('/');
471 if (SlashPos != StringRef::npos) {
472 HFI.IndexHeaderMapHeader = 1;
473 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
474 SlashPos));
475 }
476 }
477
Chris Lattnerafded5b2007-12-17 08:13:48 +0000478 // Remember this location for the next lookup we do.
479 CacheLookup.second = i;
480 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000483 // If we are including a file with a quoted include "foo.h" from inside
484 // a header in a framework that is currently being built, and we couldn't
485 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
486 // "Foo" is the name of the framework in which the including header was found.
487 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
488 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
489 if (IncludingHFI.IndexHeaderMapHeader) {
490 llvm::SmallString<128> ScratchFilename;
491 ScratchFilename += IncludingHFI.Framework;
492 ScratchFilename += '/';
493 ScratchFilename += Filename;
494
495 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
496 FromDir, CurDir, CurFileEnt,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000497 SearchPath, RelativePath,
498 SuggestedModule);
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000499 std::pair<unsigned, unsigned> &CacheLookup
500 = LookupFileCache.GetOrCreateValue(Filename).getValue();
501 CacheLookup.second
502 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
503 return Result;
504 }
505 }
506
Chris Lattner9960ae82007-07-22 07:28:00 +0000507 // Otherwise, didn't find it. Remember we didn't find this.
508 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 return 0;
510}
511
512/// LookupSubframeworkHeader - Look up a subframework for the specified
513/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
514/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
515/// is a subframework within Carbon.framework. If so, return the FileEntry
516/// for the designated file, otherwise return null.
517const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000518LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000519 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000520 SmallVectorImpl<char> *SearchPath,
521 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000522 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000525 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000526 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 // Look up the base framework name of the ContextFileEnt.
529 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 // If the context info wasn't a framework, couldn't be a subframework.
532 const char *FrameworkPos = strstr(ContextName, ".framework/");
533 if (FrameworkPos == 0)
534 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
536 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 FrameworkPos+strlen(".framework/"));
538
539 // Append Frameworks/HIToolbox.framework/
540 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000541 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 FrameworkName += ".framework/";
543
544 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000545 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Reid Spencer5f016e22007-07-11 17:01:13 +0000547 // Some other location?
548 if (CacheLookup.getValue() &&
549 CacheLookup.getKeyLength() == FrameworkName.size() &&
550 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
551 CacheLookup.getKeyLength()) != 0)
552 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 // Cache subframework.
555 if (CacheLookup.getValue() == 0) {
556 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000559 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 // Otherwise, if it does, remember that this is the right direntry for this
563 // framework.
564 CacheLookup.setValue(Dir);
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 const FileEntry *FE = 0;
568
Manuel Klimek74124942011-04-26 21:50:03 +0000569 if (RelativePath != NULL) {
570 RelativePath->clear();
571 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
572 }
573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
575 llvm::SmallString<1024> HeadersFilename(FrameworkName);
576 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000577 if (SearchPath != NULL) {
578 SearchPath->clear();
579 // Without trailing '/'.
580 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
581 }
582
Chris Lattnera1394812010-01-10 01:35:12 +0000583 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000584 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
587 HeadersFilename = FrameworkName;
588 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000589 if (SearchPath != NULL) {
590 SearchPath->clear();
591 // Without trailing '/'.
592 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
593 }
594
Chris Lattnera1394812010-01-10 01:35:12 +0000595 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000596 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 return 0;
598 }
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000601 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000602 // Note that the temporary 'DirInfo' is required here, as either call to
603 // getFileInfo could resize the vector and we don't want to rely on order
604 // of evaluation.
605 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
606 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 return FE;
608}
609
610//===----------------------------------------------------------------------===//
611// File Info Management.
612//===----------------------------------------------------------------------===//
613
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000614/// \brief Merge the header file info provided by \p OtherHFI into the current
615/// header file info (\p HFI)
616static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
617 const HeaderFileInfo &OtherHFI) {
618 HFI.isImport |= OtherHFI.isImport;
619 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
620 HFI.NumIncludes += OtherHFI.NumIncludes;
621
622 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
623 HFI.ControllingMacro = OtherHFI.ControllingMacro;
624 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
625 }
626
627 if (OtherHFI.External) {
628 HFI.DirInfo = OtherHFI.DirInfo;
629 HFI.External = OtherHFI.External;
630 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
631 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000632
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000633 if (HFI.Framework.empty())
634 HFI.Framework = OtherHFI.Framework;
635
636 HFI.Resolved = true;
637}
638
Steve Naroff83d63c72009-04-24 20:03:17 +0000639/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000640/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000641HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 if (FE->getUID() >= FileInfo.size())
643 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000644
645 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000646 if (ExternalSource && !HFI.Resolved)
647 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000648 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000649}
Reid Spencer5f016e22007-07-11 17:01:13 +0000650
Douglas Gregordd3e5542011-05-04 00:14:37 +0000651bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
652 // Check if we've ever seen this file as a header.
653 if (File->getUID() >= FileInfo.size())
654 return false;
655
656 // Resolve header file info from the external source, if needed.
657 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000658 if (ExternalSource && !HFI.Resolved)
659 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregordd3e5542011-05-04 00:14:37 +0000660
661 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
662}
663
Steve Naroff83d63c72009-04-24 20:03:17 +0000664void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
665 if (UID >= FileInfo.size())
666 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000667 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000668 FileInfo[UID] = HFI;
669}
670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
672/// #include, #include_next, or #import directive. Return false if #including
673/// the file will have no effect or true if we should include it.
674bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
675 ++NumIncluded; // Count # of attempted #includes.
676
677 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000678 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 // If this is a #import directive, check that we have not already imported
681 // this header.
682 if (isImport) {
683 // If this has already been imported, don't import it again.
684 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // Has this already been #import'ed or #include'd?
687 if (FileInfo.NumIncludes) return false;
688 } else {
689 // Otherwise, if this is a #include of a file that was previously #import'd
690 // or if this is the second #include of a #pragma once file, ignore it.
691 if (FileInfo.isImport)
692 return false;
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
696 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000697 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000698 = FileInfo.getControllingMacro(ExternalLookup))
699 if (ControllingMacro->hasMacroDefinition()) {
700 ++NumMultiIncludeFileOptzn;
701 return false;
702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 // Increment the number of times this file has been included.
705 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 return true;
708}
709
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000710size_t HeaderSearch::getTotalMemory() const {
711 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000712 + llvm::capacity_in_bytes(FileInfo)
713 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000714 + LookupFileCache.getAllocator().getTotalMemory()
715 + FrameworkMap.getAllocator().getTotalMemory();
716}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000717
718StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
719 return FrameworkNames.GetOrCreateValue(Framework).getKey();
720}
Douglas Gregora30cfe52011-11-11 19:10:28 +0000721
722bool HeaderSearch::hasModuleMap(StringRef FileName,
723 const DirectoryEntry *Root) {
724 llvm::SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
725
726 StringRef DirName = FileName;
727 do {
728 // Get the parent directory name.
729 DirName = llvm::sys::path::parent_path(DirName);
730 if (DirName.empty())
731 return false;
732
733 // Determine whether this directory exists.
734 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
735 if (!Dir)
736 return false;
737
738 llvm::DenseMap<const DirectoryEntry *, bool>::iterator
739 KnownDir = DirectoryHasModuleMap.find(Dir);
740 if (KnownDir != DirectoryHasModuleMap.end()) {
741 // We have seen this directory before. If it has no module map file,
742 // we're done.
743 if (!KnownDir->second)
744 return false;
745
746 // All of the directories we stepped through inherit this module map
747 // file.
748 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
749 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
750
751 return true;
752 }
753
754 // We have not checked for a module map file in this directory yet;
755 // do so now.
756 llvm::SmallString<128> ModuleMapFileName;
757 ModuleMapFileName += Dir->getName();
758 llvm::sys::path::append(ModuleMapFileName, "module.map");
759 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) {
760 // We have found a module map file. Try to parse it.
761 if (!ModMap.parseModuleMapFile(ModuleMapFile)) {
762 // This directory has a module map.
763 DirectoryHasModuleMap[Dir] = true;
764
765 // All of the directories we stepped through inherit this module map
766 // file.
767 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
768 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
769
770 return true;
771 }
772 }
773
774 // This directory did not have a module map file.
775 DirectoryHasModuleMap[Dir] = false;
776
777 // Keep track of all of the directories we checked, so we can mark them as
778 // having module maps if we eventually do find a module map.
779 FixUpDirectories.push_back(Dir);
780 } while (true);
781
782 return false;
783}
784
Douglas Gregor65f3b5e2011-11-11 22:18:48 +0000785StringRef HeaderSearch::findModuleForHeader(const FileEntry *File) {
786 if (ModuleMap::Module *Module = ModMap.findModuleForHeader(File))
787 return Module->getTopLevelModuleName();
788
Douglas Gregora30cfe52011-11-11 19:10:28 +0000789 return StringRef();
790}
791
792