blob: 5770aaadf4046d6b2263e4052145668a9db13a14 [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"
Douglas Gregor718292f2011-11-11 19:10:28 +000015#include "clang/Basic/Diagnostic.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/IdentifierTable.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/HeaderMap.h"
19#include "clang/Lex/HeaderSearchOptions.h"
20#include "clang/Lex/Lexer.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"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Support/FileSystem.h"
24#include "llvm/Support/Path.h"
Daniel Jasperba7f2f72013-09-24 09:14:14 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000026#include <cstdio>
Douglas Gregor01c7cfa2013-01-22 23:49:45 +000027#if defined(LLVM_ON_UNIX)
Dmitri Gribenkoeadae012013-01-26 16:29:36 +000028#include <limits.h>
Douglas Gregor01c7cfa2013-01-22 23:49:45 +000029#endif
Chris Lattner59a9ebd2006-10-18 05:34:33 +000030using namespace clang;
31
Douglas Gregor99734e72009-04-25 23:30:02 +000032const IdentifierInfo *
33HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
34 if (ControllingMacro)
35 return ControllingMacro;
36
37 if (!ControllingMacroID || !External)
38 return 0;
39
40 ControllingMacro = External->GetIdentifier(ControllingMacroID);
41 return ControllingMacro;
42}
43
Douglas Gregor09b69892011-02-10 17:09:37 +000044ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
45
Dmitri Gribenkof8579502013-01-12 19:30:44 +000046HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000047 SourceManager &SourceMgr, DiagnosticsEngine &Diags,
Douglas Gregor89929282012-01-30 06:01:29 +000048 const LangOptions &LangOpts,
49 const TargetInfo *Target)
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000050 : HSOpts(HSOpts), FileMgr(SourceMgr.getFileManager()), FrameworkMap(64),
51 ModMap(SourceMgr, *Diags.getClient(), LangOpts, Target, *this)
Douglas Gregor197ac202011-11-11 00:35:06 +000052{
Nico Weber3b1d1212011-05-24 04:31:14 +000053 AngledDirIdx = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000054 SystemDirIdx = 0;
55 NoCurDirSearch = false;
Mike Stump11289f42009-09-09 15:08:12 +000056
Douglas Gregor99734e72009-04-25 23:30:02 +000057 ExternalLookup = 0;
Douglas Gregor09b69892011-02-10 17:09:37 +000058 ExternalSource = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000059 NumIncluded = 0;
60 NumMultiIncludeFileOptzn = 0;
61 NumFrameworkLookups = NumSubFrameworkLookups = 0;
Argyrios Kyrtzidis9955dbc2013-12-12 16:08:33 +000062
63 EnabledModules = LangOpts.Modules;
Chris Lattner641a0be2006-10-20 06:23:14 +000064}
65
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000066HeaderSearch::~HeaderSearch() {
67 // Delete headermaps.
68 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
69 delete HeaderMaps[i].second;
70}
Mike Stump11289f42009-09-09 15:08:12 +000071
Chris Lattner59a9ebd2006-10-18 05:34:33 +000072void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000073 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
74 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000075 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
76 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
77 NumOnceOnlyFiles += FileInfo[i].isImport;
78 if (MaxNumIncludes < FileInfo[i].NumIncludes)
79 MaxNumIncludes = FileInfo[i].NumIncludes;
80 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
81 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000082 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
83 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
84 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattner23b7eb62007-06-15 23:05:46 +000086 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
87 fprintf(stderr, " %d #includes skipped due to"
88 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner23b7eb62007-06-15 23:05:46 +000090 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
91 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000092}
93
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000094/// CreateHeaderMap - This method returns a HeaderMap for the specified
Sylvestre Ledru830885c2012-07-23 08:59:39 +000095/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
Chris Lattner4ffe46c2007-12-17 18:34:53 +000096const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000097 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerf62f7582007-12-17 07:52:39 +000098 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000099 if (!HeaderMaps.empty()) {
100 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerf62f7582007-12-17 07:52:39 +0000101 // Pointer equality comparison of FileEntries works because they are
102 // already uniqued by inode.
Mike Stump11289f42009-09-09 15:08:12 +0000103 if (HeaderMaps[i].first == FE)
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000104 return HeaderMaps[i].second;
105 }
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattner5159f612010-11-23 08:35:12 +0000107 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000108 HeaderMaps.push_back(std::make_pair(FE, HM));
109 return HM;
110 }
Mike Stump11289f42009-09-09 15:08:12 +0000111
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000112 return 0;
113}
114
Douglas Gregor279a6c32012-01-29 17:08:11 +0000115std::string HeaderSearch::getModuleFileName(Module *Module) {
Douglas Gregor1e44e022011-09-12 20:41:59 +0000116 // If we don't have a module cache path, we can't do anything.
Douglas Gregor279a6c32012-01-29 17:08:11 +0000117 if (ModuleCachePath.empty())
118 return std::string();
119
120
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000121 SmallString<256> Result(ModuleCachePath);
Douglas Gregor279a6c32012-01-29 17:08:11 +0000122 llvm::sys::path::append(Result, Module->getTopLevelModule()->Name + ".pcm");
123 return Result.str().str();
124}
125
126std::string HeaderSearch::getModuleFileName(StringRef ModuleName) {
127 // If we don't have a module cache path, we can't do anything.
128 if (ModuleCachePath.empty())
129 return std::string();
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000130
Douglas Gregor279a6c32012-01-29 17:08:11 +0000131
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000132 SmallString<256> Result(ModuleCachePath);
Douglas Gregor279a6c32012-01-29 17:08:11 +0000133 llvm::sys::path::append(Result, ModuleName + ".pcm");
134 return Result.str().str();
135}
136
137Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000138 // Look in the module map to determine if there is a module by this name.
Douglas Gregor279a6c32012-01-29 17:08:11 +0000139 Module *Module = ModMap.findModule(ModuleName);
140 if (Module || !AllowSearch)
141 return Module;
142
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000143 // Look through the various header search paths to load any available module
Douglas Gregor279a6c32012-01-29 17:08:11 +0000144 // maps, searching for a module map that describes this module.
145 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
146 if (SearchDirs[Idx].isFramework()) {
147 // Search for or infer a module map for a framework.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000148 SmallString<128> FrameworkDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000149 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
150 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
151 if (const DirectoryEntry *FrameworkDir
152 = FileMgr.getDirectory(FrameworkDirName)) {
153 bool IsSystem
154 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
155 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000156 if (Module)
157 break;
158 }
Douglas Gregor279a6c32012-01-29 17:08:11 +0000159 }
160
161 // FIXME: Figure out how header maps and module maps will work together.
162
163 // Only deal with normal search directories.
164 if (!SearchDirs[Idx].isNormalDir())
165 continue;
Douglas Gregor963c5532013-06-21 16:28:10 +0000166
167 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
Douglas Gregor279a6c32012-01-29 17:08:11 +0000168 // Search for a module map file in this directory.
Douglas Gregor963c5532013-06-21 16:28:10 +0000169 if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem)
170 == LMM_NewlyLoaded) {
Douglas Gregor279a6c32012-01-29 17:08:11 +0000171 // We just loaded a module map file; check whether the module is
172 // available now.
173 Module = ModMap.findModule(ModuleName);
174 if (Module)
175 break;
176 }
177
178 // Search for a module map in a subdirectory with the same name as the
179 // module.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000180 SmallString<128> NestedModuleMapDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000181 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
182 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
Douglas Gregor963c5532013-06-21 16:28:10 +0000183 if (loadModuleMapFile(NestedModuleMapDirName, IsSystem) == LMM_NewlyLoaded){
Douglas Gregor279a6c32012-01-29 17:08:11 +0000184 // If we just loaded a module map file, look for the module again.
185 Module = ModMap.findModule(ModuleName);
186 if (Module)
187 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000188 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000189
190 // If we've already performed the exhaustive search for module maps in this
191 // search directory, don't do it again.
192 if (SearchDirs[Idx].haveSearchedAllModuleMaps())
193 continue;
194
195 // Load all module maps in the immediate subdirectories of this search
196 // directory.
197 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
198
199 // Look again for the module.
200 Module = ModMap.findModule(ModuleName);
201 if (Module)
202 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000203 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000204
Douglas Gregor279a6c32012-01-29 17:08:11 +0000205 return Module;
Douglas Gregor1e44e022011-09-12 20:41:59 +0000206}
207
Chris Lattnerf62f7582007-12-17 07:52:39 +0000208//===----------------------------------------------------------------------===//
209// File lookup within a DirectoryLookup scope
210//===----------------------------------------------------------------------===//
211
Chris Lattner8d720d02007-12-17 17:57:27 +0000212/// getName - Return the directory or filename corresponding to this lookup
213/// object.
214const char *DirectoryLookup::getName() const {
215 if (isNormalDir())
216 return getDir()->getName();
217 if (isFramework())
218 return getFrameworkDir()->getName();
219 assert(isHeaderMap() && "Unknown DirectoryLookup");
220 return getHeaderMap()->getFileName();
221}
222
223
Chris Lattnerf62f7582007-12-17 07:52:39 +0000224/// LookupFile - Lookup the specified file in this search path, returning it
225/// if it exists or returning null if not.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000226const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000227 StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000228 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000229 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000230 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000231 ModuleMap::KnownHeader *SuggestedModule,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000232 bool &InUserSpecifiedSystemFramework) const {
233 InUserSpecifiedSystemFramework = false;
234
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000235 SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000236 if (isNormalDir()) {
237 // Concatenate the requested file onto the directory.
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000238 TmpDir = getDir()->getName();
239 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000240 if (SearchPath != NULL) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000241 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000242 SearchPath->clear();
243 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
244 }
245 if (RelativePath != NULL) {
246 RelativePath->clear();
247 RelativePath->append(Filename.begin(), Filename.end());
248 }
Douglas Gregor718292f2011-11-11 19:10:28 +0000249
250 // If we have a module map that might map this header, load it and
251 // check whether we'll have a suggestion for a module.
Daniel Jasper97da9172013-10-22 08:09:47 +0000252 HS.hasModuleMap(TmpDir, getDir(), isSystemHeaderDirectory());
253 if (SuggestedModule) {
254 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
Douglas Gregor718292f2011-11-11 19:10:28 +0000255 /*openFile=*/false);
256 if (!File)
257 return File;
258
Daniel Jasper97da9172013-10-22 08:09:47 +0000259 // If there is a module that corresponds to this header, suggest it.
Douglas Gregor2537a362011-12-08 17:01:29 +0000260 *SuggestedModule = HS.findModuleForHeader(File);
Daniel Jasper97da9172013-10-22 08:09:47 +0000261 if (!SuggestedModule->getModule() &&
262 HS.hasModuleMap(TmpDir, getDir(), isSystemHeaderDirectory()))
263 *SuggestedModule = HS.findModuleForHeader(File);
Douglas Gregor718292f2011-11-11 19:10:28 +0000264 return File;
265 }
266
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000267 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattner712e3872007-12-17 08:13:48 +0000268 }
Mike Stump11289f42009-09-09 15:08:12 +0000269
Chris Lattner712e3872007-12-17 08:13:48 +0000270 if (isFramework())
Douglas Gregor97eec242011-09-15 22:00:41 +0000271 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000272 SuggestedModule, InUserSpecifiedSystemFramework);
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattner44bd21b2007-12-17 08:17:39 +0000274 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000275 const FileEntry * const Result = getHeaderMap()->LookupFile(
276 Filename, HS.getFileMgr());
277 if (Result) {
278 if (SearchPath != NULL) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000279 StringRef SearchPathRef(getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000280 SearchPath->clear();
281 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
282 }
283 if (RelativePath != NULL) {
284 RelativePath->clear();
285 RelativePath->append(Filename.begin(), Filename.end());
286 }
287 }
288 return Result;
Chris Lattnerf62f7582007-12-17 07:52:39 +0000289}
290
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000291/// \brief Given a framework directory, find the top-most framework directory.
292///
293/// \param FileMgr The file manager to use for directory lookups.
294/// \param DirName The name of the framework directory.
295/// \param SubmodulePath Will be populated with the submodule path from the
296/// returned top-level module to the originally named framework.
297static const DirectoryEntry *
298getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
299 SmallVectorImpl<std::string> &SubmodulePath) {
300 assert(llvm::sys::path::extension(DirName) == ".framework" &&
301 "Not a framework directory");
302
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000303 // Note: as an egregious but useful hack we use the real path here, because
304 // frameworks moving between top-level frameworks to embedded frameworks tend
305 // to be symlinked, and we base the logical structure of modules on the
306 // physical layout. In particular, we need to deal with crazy includes like
307 //
308 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
309 //
310 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
311 // which one should access with, e.g.,
312 //
313 // #include <Bar/Wibble.h>
314 //
315 // Similar issues occur when a top-level framework has moved into an
316 // embedded framework.
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000317 const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
Douglas Gregore00c8b22013-01-26 00:55:12 +0000318 DirName = FileMgr.getCanonicalName(TopFrameworkDir);
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000319 do {
320 // Get the parent directory name.
321 DirName = llvm::sys::path::parent_path(DirName);
322 if (DirName.empty())
323 break;
324
325 // Determine whether this directory exists.
326 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
327 if (!Dir)
328 break;
329
330 // If this is a framework directory, then we're a subframework of this
331 // framework.
332 if (llvm::sys::path::extension(DirName) == ".framework") {
333 SubmodulePath.push_back(llvm::sys::path::stem(DirName));
334 TopFrameworkDir = Dir;
335 }
336 } while (true);
337
338 return TopFrameworkDir;
339}
Chris Lattnerf62f7582007-12-17 07:52:39 +0000340
Chris Lattner712e3872007-12-17 08:13:48 +0000341/// DoFrameworkLookup - Do a lookup of the specified file in the current
342/// DirectoryLookup, which is a framework directory.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000343const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000344 StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000345 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000346 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000347 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000348 ModuleMap::KnownHeader *SuggestedModule,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000349 bool &InUserSpecifiedSystemFramework) const
Douglas Gregor97eec242011-09-15 22:00:41 +0000350{
Chris Lattner712e3872007-12-17 08:13:48 +0000351 FileManager &FileMgr = HS.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000353 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000354 size_t SlashPos = Filename.find('/');
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000355 if (SlashPos == StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000356
Chris Lattner712e3872007-12-17 08:13:48 +0000357 // Find out if this is the home for the specified framework, by checking
Daniel Dunbar17138612012-04-05 17:09:40 +0000358 // HeaderSearch. Possible answers are yes/no and unknown.
359 HeaderSearch::FrameworkCacheEntry &CacheEntry =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000360 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattner712e3872007-12-17 08:13:48 +0000362 // If it is known and in some other directory, fail.
Daniel Dunbar17138612012-04-05 17:09:40 +0000363 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
Chris Lattner5ed76da2006-10-22 07:24:13 +0000364 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner712e3872007-12-17 08:13:48 +0000366 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000368 // FrameworkName = "/System/Library/Frameworks/"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000369 SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000370 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000371 if (FrameworkName.empty() || FrameworkName.back() != '/')
372 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000373
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000374 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor56c64012011-11-17 01:41:17 +0000375 StringRef ModuleName(Filename.begin(), SlashPos);
376 FrameworkName += ModuleName;
Mike Stump11289f42009-09-09 15:08:12 +0000377
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000378 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
379 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000380
Daniel Dunbar17138612012-04-05 17:09:40 +0000381 // If the cache entry was unresolved, populate it now.
382 if (CacheEntry.Directory == 0) {
Chris Lattner712e3872007-12-17 08:13:48 +0000383 HS.IncrementFrameworkLookupCount();
Mike Stump11289f42009-09-09 15:08:12 +0000384
Chris Lattner5ed76da2006-10-22 07:24:13 +0000385 // If the framework dir doesn't exist, we fail.
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000386 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
387 if (Dir == 0) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000388
Chris Lattner5ed76da2006-10-22 07:24:13 +0000389 // Otherwise, if it does, remember that this is the right direntry for this
390 // framework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000391 CacheEntry.Directory = getFrameworkDir();
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000392
393 // If this is a user search directory, check if the framework has been
394 // user-specified as a system framework.
395 if (getDirCharacteristic() == SrcMgr::C_User) {
396 SmallString<1024> SystemFrameworkMarker(FrameworkName);
397 SystemFrameworkMarker += ".system_framework";
398 if (llvm::sys::fs::exists(SystemFrameworkMarker.str())) {
399 CacheEntry.IsUserSpecifiedSystemFramework = true;
400 }
401 }
Chris Lattner5ed76da2006-10-22 07:24:13 +0000402 }
Mike Stump11289f42009-09-09 15:08:12 +0000403
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000404 // Set the 'user-specified system framework' flag.
405 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
406
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000407 if (RelativePath != NULL) {
408 RelativePath->clear();
409 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
410 }
Douglas Gregor56c64012011-11-17 01:41:17 +0000411
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000412 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000413 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000414
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000415 FrameworkName += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000416
417 if (SearchPath != NULL) {
418 SearchPath->clear();
419 // Without trailing '/'.
420 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
421 }
422
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000423 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000424 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
425 /*openFile=*/!SuggestedModule);
426 if (!FE) {
427 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
428 const char *Private = "Private";
429 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
430 Private+strlen(Private));
431 if (SearchPath != NULL)
432 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
433 Private+strlen(Private));
434
435 FE = FileMgr.getFile(FrameworkName.str(), /*openFile=*/!SuggestedModule);
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000436 }
Mike Stump11289f42009-09-09 15:08:12 +0000437
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000438 // If we found the header and are allowed to suggest a module, do so now.
439 if (FE && SuggestedModule) {
440 // Find the framework in which this header occurs.
441 StringRef FrameworkPath = FE->getName();
442 bool FoundFramework = false;
443 do {
444 // Get the parent directory name.
445 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
446 if (FrameworkPath.empty())
447 break;
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000448
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000449 // Determine whether this directory exists.
450 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
451 if (!Dir)
452 break;
453
454 // If this is a framework directory, then we're a subframework of this
455 // framework.
456 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
457 FoundFramework = true;
458 break;
459 }
460 } while (true);
461
462 if (FoundFramework) {
463 // Find the top-level framework based on this framework.
464 SmallVector<std::string, 4> SubmodulePath;
465 const DirectoryEntry *TopFrameworkDir
466 = ::getTopFrameworkDir(FileMgr, FrameworkPath, SubmodulePath);
467
468 // Determine the name of the top-level framework.
469 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
470
471 // Load this framework module. If that succeeds, find the suggested module
472 // for this header, if any.
473 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
474 if (HS.loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
475 *SuggestedModule = HS.findModuleForHeader(FE);
476 }
477 } else {
478 *SuggestedModule = HS.findModuleForHeader(FE);
479 }
480 }
Douglas Gregor97eec242011-09-15 22:00:41 +0000481 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000482}
483
Douglas Gregor89929282012-01-30 06:01:29 +0000484void HeaderSearch::setTarget(const TargetInfo &Target) {
485 ModMap.setTarget(Target);
486}
487
Chris Lattnerf62f7582007-12-17 07:52:39 +0000488
Chris Lattner712e3872007-12-17 08:13:48 +0000489//===----------------------------------------------------------------------===//
490// Header File Location.
491//===----------------------------------------------------------------------===//
492
493
James Dennettc07ab2c2012-06-20 00:56:32 +0000494/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000495/// return null on failure. isAngled indicates whether the file reference is
James Dennettc07ab2c2012-06-20 00:56:32 +0000496/// for system \#include's or not (i.e. using <> instead of ""). CurFileEnt, if
497/// non-null, indicates where the \#including file is, in case a relative search
Douglas Gregor618e64a2010-08-08 07:49:23 +0000498/// is needed.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000499const FileEntry *HeaderSearch::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000500 StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000501 bool isAngled,
502 const DirectoryLookup *FromDir,
503 const DirectoryLookup *&CurDir,
504 const FileEntry *CurFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000505 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000506 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000507 ModuleMap::KnownHeader *SuggestedModule,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000508 bool SkipCache)
Douglas Gregor97eec242011-09-15 22:00:41 +0000509{
Daniel Jasper97da9172013-10-22 08:09:47 +0000510 if (!HSOpts->ModuleMapFiles.empty()) {
511 // Preload all explicitly specified module map files. This enables modules
512 // map files lying in a directory structure separate from the header files
513 // that they describe. These cannot be loaded lazily upon encountering a
514 // header file, as there is no other knwon mapping from a header file to its
515 // module map file.
516 for (llvm::SetVector<std::string>::iterator
517 I = HSOpts->ModuleMapFiles.begin(),
518 E = HSOpts->ModuleMapFiles.end();
519 I != E; ++I) {
520 const FileEntry *File = FileMgr.getFile(*I);
521 if (!File)
522 continue;
523 loadModuleMapFile(File, /*IsSystem=*/false);
524 }
525 HSOpts->ModuleMapFiles.clear();
526 }
527
Douglas Gregor97eec242011-09-15 22:00:41 +0000528 if (SuggestedModule)
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000529 *SuggestedModule = ModuleMap::KnownHeader();
Douglas Gregor97eec242011-09-15 22:00:41 +0000530
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000531 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000532 if (llvm::sys::path::is_absolute(Filename)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000533 CurDir = 0;
534
535 // If this was an #include_next "/absolute/file", fail.
536 if (FromDir) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000537
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000538 if (SearchPath != NULL)
539 SearchPath->clear();
540 if (RelativePath != NULL) {
541 RelativePath->clear();
542 RelativePath->append(Filename.begin(), Filename.end());
543 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000544 // Otherwise, just return the file.
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000545 return FileMgr.getFile(Filename, /*openFile=*/true);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000546 }
Mike Stump11289f42009-09-09 15:08:12 +0000547
Douglas Gregor9f93e382011-07-28 04:45:53 +0000548 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor618e64a2010-08-08 07:49:23 +0000549 // directory. This has to be based on CurFileEnt, not CurDir, because
550 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerf62f7582007-12-17 07:52:39 +0000551 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
552 // This search is not done for <> headers.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000553 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
NAKAMURA Takumi9cb62642013-12-10 02:36:28 +0000554 SmallString<1024> TmpDir;
Douglas Gregor618e64a2010-08-08 07:49:23 +0000555 // Concatenate the requested file onto the directory.
NAKAMURA Takumi9cb62642013-12-10 02:36:28 +0000556 // FIXME: Portability. Filename concatenation should be in sys::Path.
557 TmpDir += CurFileEnt->getDir()->getName();
558 TmpDir.push_back('/');
559 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000560 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000561 // Leave CurDir unset.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000562 // This file is a system header or C++ unfriendly if the old file is.
563 //
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000564 // Note that we only use one of FromHFI/ToHFI at once, due to potential
565 // reallocation of the underlying vector potentially making the first
566 // reference binding dangling.
567 HeaderFileInfo &FromHFI = getFileInfo(CurFileEnt);
568 unsigned DirInfo = FromHFI.DirInfo;
569 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
570 StringRef Framework = FromHFI.Framework;
571
572 HeaderFileInfo &ToHFI = getFileInfo(FE);
573 ToHFI.DirInfo = DirInfo;
574 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
575 ToHFI.Framework = Framework;
576
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000577 if (SearchPath != NULL) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000578 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000579 SearchPath->clear();
580 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
581 }
582 if (RelativePath != NULL) {
583 RelativePath->clear();
584 RelativePath->append(Filename.begin(), Filename.end());
585 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000586 return FE;
587 }
588 }
Mike Stump11289f42009-09-09 15:08:12 +0000589
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000590 CurDir = 0;
591
592 // If this is a system #include, ignore the user #include locs.
Nico Weber3b1d1212011-05-24 04:31:14 +0000593 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000595 // If this is a #include_next request, start searching after the directory the
596 // file was found in.
597 if (FromDir)
598 i = FromDir-&SearchDirs[0];
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattnerd4275422007-07-22 07:28:00 +0000600 // Cache all of the lookups performed by this method. Many headers are
601 // multiply included, and the "pragma once" optimization prevents them from
602 // being relex/pp'd, but they would still have to search through a
603 // (potentially huge) series of SearchDirs to find it.
604 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000605 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattnerd4275422007-07-22 07:28:00 +0000606
607 // If the entry has been previously looked up, the first value will be
608 // non-zero. If the value is equal to i (the start point of our search), then
609 // this is a matching hit.
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000610 if (!SkipCache && CacheLookup.first == i+1) {
Chris Lattnerd4275422007-07-22 07:28:00 +0000611 // Skip querying potentially lots of directories for this lookup.
612 i = CacheLookup.second;
613 } else {
614 // Otherwise, this is the first query, or the previous query didn't match
615 // our search start. We will fill in our found location below, so prime the
616 // start point value.
617 CacheLookup.first = i+1;
618 }
Mike Stump11289f42009-09-09 15:08:12 +0000619
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000620 // Check each directory in sequence to see if it contains this file.
621 for (; i != SearchDirs.size(); ++i) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000622 bool InUserSpecifiedSystemFramework = false;
Mike Stump11289f42009-09-09 15:08:12 +0000623 const FileEntry *FE =
Douglas Gregor97eec242011-09-15 22:00:41 +0000624 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000625 SuggestedModule, InUserSpecifiedSystemFramework);
Chris Lattner712e3872007-12-17 08:13:48 +0000626 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000627
Chris Lattner712e3872007-12-17 08:13:48 +0000628 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000629
Chris Lattner712e3872007-12-17 08:13:48 +0000630 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor9f93e382011-07-28 04:45:53 +0000631 HeaderFileInfo &HFI = getFileInfo(FE);
632 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000633
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000634 // If the directory characteristic is User but this framework was
635 // user-specified to be treated as a system framework, promote the
636 // characteristic.
637 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
638 HFI.DirInfo = SrcMgr::C_System;
639
Richard Smith8acadcb2012-06-13 20:27:03 +0000640 // If the filename matches a known system header prefix, override
641 // whether the file is a system header.
Richard Trieu871f5f32012-06-13 20:52:36 +0000642 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
643 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
644 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
Richard Smith8acadcb2012-06-13 20:27:03 +0000645 : SrcMgr::C_User;
646 break;
647 }
648 }
649
Douglas Gregor9f93e382011-07-28 04:45:53 +0000650 // If this file is found in a header map and uses the framework style of
651 // includes, then this header is part of a framework we're building.
652 if (CurDir->isIndexHeaderMap()) {
653 size_t SlashPos = Filename.find('/');
654 if (SlashPos != StringRef::npos) {
655 HFI.IndexHeaderMapHeader = 1;
656 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
657 SlashPos));
658 }
659 }
660
Chris Lattner712e3872007-12-17 08:13:48 +0000661 // Remember this location for the next lookup we do.
662 CacheLookup.second = i;
663 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000664 }
Mike Stump11289f42009-09-09 15:08:12 +0000665
Douglas Gregord8575e12011-07-30 06:28:34 +0000666 // If we are including a file with a quoted include "foo.h" from inside
667 // a header in a framework that is currently being built, and we couldn't
668 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
669 // "Foo" is the name of the framework in which the including header was found.
670 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
671 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
672 if (IncludingHFI.IndexHeaderMapHeader) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000673 SmallString<128> ScratchFilename;
Douglas Gregord8575e12011-07-30 06:28:34 +0000674 ScratchFilename += IncludingHFI.Framework;
675 ScratchFilename += '/';
676 ScratchFilename += Filename;
677
678 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
679 FromDir, CurDir, CurFileEnt,
Douglas Gregor97eec242011-09-15 22:00:41 +0000680 SearchPath, RelativePath,
681 SuggestedModule);
Douglas Gregord8575e12011-07-30 06:28:34 +0000682 std::pair<unsigned, unsigned> &CacheLookup
683 = LookupFileCache.GetOrCreateValue(Filename).getValue();
684 CacheLookup.second
685 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
686 return Result;
687 }
688 }
689
Chris Lattnerd4275422007-07-22 07:28:00 +0000690 // Otherwise, didn't find it. Remember we didn't find this.
691 CacheLookup.second = SearchDirs.size();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000692 return 0;
693}
694
Chris Lattner63dd32b2006-10-20 04:42:40 +0000695/// LookupSubframeworkHeader - Look up a subframework for the specified
James Dennettc07ab2c2012-06-20 00:56:32 +0000696/// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
Chris Lattner63dd32b2006-10-20 04:42:40 +0000697/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
698/// is a subframework within Carbon.framework. If so, return the FileEntry
699/// for the designated file, otherwise return null.
700const FileEntry *HeaderSearch::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000701LookupSubframeworkHeader(StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000702 const FileEntry *ContextFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000703 SmallVectorImpl<char> *SearchPath,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000704 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000705 ModuleMap::KnownHeader *SuggestedModule) {
Chris Lattner12261882008-02-01 05:34:02 +0000706 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000707
Chris Lattner63dd32b2006-10-20 04:42:40 +0000708 // Framework names must have a '/' in the filename. Find it.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000709 // FIXME: Should we permit '\' on Windows?
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000710 size_t SlashPos = Filename.find('/');
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000711 if (SlashPos == StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000712
Chris Lattner63dd32b2006-10-20 04:42:40 +0000713 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000714 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000715
Chris Lattner63dd32b2006-10-20 04:42:40 +0000716 // If the context info wasn't a framework, couldn't be a subframework.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000717 const unsigned DotFrameworkLen = 10;
718 const char *FrameworkPos = strstr(ContextName, ".framework");
719 if (FrameworkPos == 0 ||
720 (FrameworkPos[DotFrameworkLen] != '/' &&
721 FrameworkPos[DotFrameworkLen] != '\\'))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000722 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000723
Daniel Dunbar17138612012-04-05 17:09:40 +0000724 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000725
Chris Lattner63dd32b2006-10-20 04:42:40 +0000726 // Append Frameworks/HIToolbox.framework/
727 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000728 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000729 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000730
Daniel Dunbar17138612012-04-05 17:09:40 +0000731 llvm::StringMapEntry<FrameworkCacheEntry> &CacheLookup =
Chris Lattner8afa6de2010-11-21 09:55:08 +0000732 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattner5ed76da2006-10-22 07:24:13 +0000734 // Some other location?
Daniel Dunbar17138612012-04-05 17:09:40 +0000735 if (CacheLookup.getValue().Directory &&
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000736 CacheLookup.getKeyLength() == FrameworkName.size() &&
737 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
738 CacheLookup.getKeyLength()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000739 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000740
Chris Lattner5ed76da2006-10-22 07:24:13 +0000741 // Cache subframework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000742 if (CacheLookup.getValue().Directory == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000743 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000744
Chris Lattner5ed76da2006-10-22 07:24:13 +0000745 // If the framework dir doesn't exist, we fail.
Chris Lattner5159f612010-11-23 08:35:12 +0000746 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000747 if (Dir == 0) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattner5ed76da2006-10-22 07:24:13 +0000749 // Otherwise, if it does, remember that this is the right direntry for this
750 // framework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000751 CacheLookup.getValue().Directory = Dir;
Chris Lattner5ed76da2006-10-22 07:24:13 +0000752 }
Mike Stump11289f42009-09-09 15:08:12 +0000753
Chris Lattner577377e2006-10-20 04:55:45 +0000754 const FileEntry *FE = 0;
755
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000756 if (RelativePath != NULL) {
757 RelativePath->clear();
758 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
759 }
760
Chris Lattner63dd32b2006-10-20 04:42:40 +0000761 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000762 SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000763 HeadersFilename += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000764 if (SearchPath != NULL) {
765 SearchPath->clear();
766 // Without trailing '/'.
767 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
768 }
769
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000770 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000771 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump11289f42009-09-09 15:08:12 +0000772
Chris Lattner63dd32b2006-10-20 04:42:40 +0000773 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000774 HeadersFilename = FrameworkName;
775 HeadersFilename += "PrivateHeaders/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000776 if (SearchPath != NULL) {
777 SearchPath->clear();
778 // Without trailing '/'.
779 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
780 }
781
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000782 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000783 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000784 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000785 }
Mike Stump11289f42009-09-09 15:08:12 +0000786
Chris Lattner577377e2006-10-20 04:55:45 +0000787 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000788 //
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000789 // Note that the temporary 'DirInfo' is required here, as either call to
790 // getFileInfo could resize the vector and we don't want to rely on order
791 // of evaluation.
792 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
793 getFileInfo(FE).DirInfo = DirInfo;
Douglas Gregorf5f94522013-02-08 00:10:48 +0000794
795 // If we're supposed to suggest a module, look for one now.
796 if (SuggestedModule) {
797 // Find the top-level framework based on this framework.
798 FrameworkName.pop_back(); // remove the trailing '/'
799 SmallVector<std::string, 4> SubmodulePath;
800 const DirectoryEntry *TopFrameworkDir
801 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
802
803 // Determine the name of the top-level framework.
804 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
805
806 // Load this framework module. If that succeeds, find the suggested module
807 // for this header, if any.
808 bool IsSystem = false;
809 if (loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
810 *SuggestedModule = findModuleForHeader(FE);
811 }
812 }
813
Chris Lattner577377e2006-10-20 04:55:45 +0000814 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000815}
816
Chandler Carruthb0ffe502011-12-09 01:33:57 +0000817/// \brief Helper static function to normalize a path for injection into
818/// a synthetic header.
819/*static*/ std::string
820HeaderSearch::NormalizeDashIncludePath(StringRef File, FileManager &FileMgr) {
821 // Implicit include paths should be resolved relative to the current
822 // working directory first, and then use the regular header search
823 // mechanism. The proper way to handle this is to have the
824 // predefines buffer located at the current working directory, but
825 // it has no file entry. For now, workaround this by using an
826 // absolute path if we find the file here, and otherwise letting
827 // header search handle it.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000828 SmallString<128> Path(File);
Chandler Carruthb0ffe502011-12-09 01:33:57 +0000829 llvm::sys::fs::make_absolute(Path);
830 bool exists;
831 if (llvm::sys::fs::exists(Path.str(), exists) || !exists)
832 Path = File;
833 else if (exists)
834 FileMgr.getFile(File);
835
836 return Lexer::Stringify(Path.str());
837}
838
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000839//===----------------------------------------------------------------------===//
840// File Info Management.
841//===----------------------------------------------------------------------===//
842
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000843/// \brief Merge the header file info provided by \p OtherHFI into the current
844/// header file info (\p HFI)
845static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
846 const HeaderFileInfo &OtherHFI) {
847 HFI.isImport |= OtherHFI.isImport;
848 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000849 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000850 HFI.NumIncludes += OtherHFI.NumIncludes;
851
852 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
853 HFI.ControllingMacro = OtherHFI.ControllingMacro;
854 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
855 }
856
857 if (OtherHFI.External) {
858 HFI.DirInfo = OtherHFI.DirInfo;
859 HFI.External = OtherHFI.External;
860 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
861 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000862
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000863 if (HFI.Framework.empty())
864 HFI.Framework = OtherHFI.Framework;
865
866 HFI.Resolved = true;
867}
868
Steve Naroff3fa455a2009-04-24 20:03:17 +0000869/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000870/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000871HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000872 if (FE->getUID() >= FileInfo.size())
873 FileInfo.resize(FE->getUID()+1);
Douglas Gregor09b69892011-02-10 17:09:37 +0000874
875 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000876 if (ExternalSource && !HFI.Resolved)
877 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregor09b69892011-02-10 17:09:37 +0000878 return HFI;
Mike Stump11289f42009-09-09 15:08:12 +0000879}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000880
Douglas Gregor37aa4932011-05-04 00:14:37 +0000881bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
882 // Check if we've ever seen this file as a header.
883 if (File->getUID() >= FileInfo.size())
884 return false;
885
886 // Resolve header file info from the external source, if needed.
887 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000888 if (ExternalSource && !HFI.Resolved)
889 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregor37aa4932011-05-04 00:14:37 +0000890
Argyrios Kyrtzidis0d355df2012-12-10 20:08:37 +0000891 return HFI.isPragmaOnce || HFI.isImport ||
892 HFI.ControllingMacro || HFI.ControllingMacroID;
Douglas Gregor37aa4932011-05-04 00:14:37 +0000893}
894
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +0000895void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000896 ModuleMap::ModuleHeaderRole Role,
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +0000897 bool isCompilingModuleHeader) {
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000898 if (FE->getUID() >= FileInfo.size())
899 FileInfo.resize(FE->getUID()+1);
900
901 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
902 HFI.isModuleHeader = true;
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +0000903 HFI.isCompilingModuleHeader = isCompilingModuleHeader;
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000904 HFI.setHeaderRole(Role);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000905}
906
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000907bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
908 ++NumIncluded; // Count # of attempted #includes.
909
910 // Get information about this file.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000911 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump11289f42009-09-09 15:08:12 +0000912
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000913 // If this is a #import directive, check that we have not already imported
914 // this header.
915 if (isImport) {
916 // If this has already been imported, don't import it again.
917 FileInfo.isImport = true;
Mike Stump11289f42009-09-09 15:08:12 +0000918
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000919 // Has this already been #import'ed or #include'd?
920 if (FileInfo.NumIncludes) return false;
921 } else {
922 // Otherwise, if this is a #include of a file that was previously #import'd
923 // or if this is the second #include of a #pragma once file, ignore it.
924 if (FileInfo.isImport)
925 return false;
926 }
Mike Stump11289f42009-09-09 15:08:12 +0000927
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000928 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
929 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump11289f42009-09-09 15:08:12 +0000930 if (const IdentifierInfo *ControllingMacro
Douglas Gregor99734e72009-04-25 23:30:02 +0000931 = FileInfo.getControllingMacro(ExternalLookup))
932 if (ControllingMacro->hasMacroDefinition()) {
933 ++NumMultiIncludeFileOptzn;
934 return false;
935 }
Mike Stump11289f42009-09-09 15:08:12 +0000936
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000937 // Increment the number of times this file has been included.
938 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +0000939
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000940 return true;
941}
942
Ted Kremenekfbcce6f2011-07-26 23:46:11 +0000943size_t HeaderSearch::getTotalMemory() const {
944 return SearchDirs.capacity()
Ted Kremenekae63d102011-07-27 18:41:18 +0000945 + llvm::capacity_in_bytes(FileInfo)
946 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekfbcce6f2011-07-26 23:46:11 +0000947 + LookupFileCache.getAllocator().getTotalMemory()
948 + FrameworkMap.getAllocator().getTotalMemory();
949}
Douglas Gregor9f93e382011-07-28 04:45:53 +0000950
951StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
952 return FrameworkNames.GetOrCreateValue(Framework).getKey();
953}
Douglas Gregor718292f2011-11-11 19:10:28 +0000954
955bool HeaderSearch::hasModuleMap(StringRef FileName,
Douglas Gregor963c5532013-06-21 16:28:10 +0000956 const DirectoryEntry *Root,
957 bool IsSystem) {
Argyrios Kyrtzidis9955dbc2013-12-12 16:08:33 +0000958 if (!enabledModules())
959 return false;
960
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000961 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
Douglas Gregor718292f2011-11-11 19:10:28 +0000962
963 StringRef DirName = FileName;
964 do {
965 // Get the parent directory name.
966 DirName = llvm::sys::path::parent_path(DirName);
967 if (DirName.empty())
968 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +0000969
Douglas Gregor718292f2011-11-11 19:10:28 +0000970 // Determine whether this directory exists.
971 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
972 if (!Dir)
973 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +0000974
975 // Try to load the "module.map" file in this directory.
Douglas Gregor963c5532013-06-21 16:28:10 +0000976 switch (loadModuleMapFile(Dir, IsSystem)) {
Douglas Gregor80b69042011-11-12 00:22:19 +0000977 case LMM_NewlyLoaded:
978 case LMM_AlreadyLoaded:
Daniel Jasperca9f7382013-09-24 09:27:13 +0000979 // Success. All of the directories we stepped through inherit this module
980 // map file.
981 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
982 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
983 return true;
Daniel Jasper97da9172013-10-22 08:09:47 +0000984
985 case LMM_NoDirectory:
986 case LMM_InvalidModuleMap:
987 break;
Daniel Jasperca9f7382013-09-24 09:27:13 +0000988 }
989
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000990 // If we hit the top of our search, we're done.
991 if (Dir == Root)
992 return false;
993
Douglas Gregor718292f2011-11-11 19:10:28 +0000994 // Keep track of all of the directories we checked, so we can mark them as
995 // having module maps if we eventually do find a module map.
996 FixUpDirectories.push_back(Dir);
997 } while (true);
Douglas Gregor718292f2011-11-11 19:10:28 +0000998}
999
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001000ModuleMap::KnownHeader
1001HeaderSearch::findModuleForHeader(const FileEntry *File) const {
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001002 if (ExternalSource) {
1003 // Make sure the external source has handled header info about this file,
1004 // which includes whether the file is part of a module.
1005 (void)getFileInfo(File);
1006 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001007 return ModMap.findModuleForHeader(File);
Douglas Gregor718292f2011-11-11 19:10:28 +00001008}
1009
Douglas Gregor963c5532013-06-21 16:28:10 +00001010bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001011 const DirectoryEntry *Dir = File->getDir();
1012
1013 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
1014 = DirectoryHasModuleMap.find(Dir);
1015 if (KnownDir != DirectoryHasModuleMap.end())
1016 return !KnownDir->second;
1017
Douglas Gregor963c5532013-06-21 16:28:10 +00001018 bool Result = ModMap.parseModuleMapFile(File, IsSystem);
Douglas Gregor80306772011-12-07 21:25:07 +00001019 if (!Result && llvm::sys::path::filename(File->getName()) == "module.map") {
1020 // If the file we loaded was a module.map, look for the corresponding
1021 // module_private.map.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001022 SmallString<128> PrivateFilename(Dir->getName());
Douglas Gregor80306772011-12-07 21:25:07 +00001023 llvm::sys::path::append(PrivateFilename, "module_private.map");
1024 if (const FileEntry *PrivateFile = FileMgr.getFile(PrivateFilename))
Douglas Gregor963c5532013-06-21 16:28:10 +00001025 Result = ModMap.parseModuleMapFile(PrivateFile, IsSystem);
Douglas Gregor80306772011-12-07 21:25:07 +00001026 }
1027
1028 DirectoryHasModuleMap[Dir] = !Result;
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001029 return Result;
1030}
1031
Douglas Gregor279a6c32012-01-29 17:08:11 +00001032Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1033 const DirectoryEntry *Dir,
1034 bool IsSystem) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001035 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor56c64012011-11-17 01:41:17 +00001036 return Module;
1037
1038 // Try to load a module map file.
Douglas Gregor963c5532013-06-21 16:28:10 +00001039 switch (loadModuleMapFile(Dir, IsSystem)) {
Douglas Gregor56c64012011-11-17 01:41:17 +00001040 case LMM_InvalidModuleMap:
1041 break;
1042
1043 case LMM_AlreadyLoaded:
1044 case LMM_NoDirectory:
1045 return 0;
1046
1047 case LMM_NewlyLoaded:
1048 return ModMap.findModule(Name);
1049 }
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001050
Douglas Gregor4ddf2222013-01-10 01:43:00 +00001051 // Figure out the top-level framework directory and the submodule path from
1052 // that top-level framework to the requested framework.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001053 SmallVector<std::string, 2> SubmodulePath;
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001054 SubmodulePath.push_back(Name);
Douglas Gregor4ddf2222013-01-10 01:43:00 +00001055 const DirectoryEntry *TopFrameworkDir
1056 = ::getTopFrameworkDir(FileMgr, Dir->getName(), SubmodulePath);
Douglas Gregor9194a912012-11-06 19:39:40 +00001057
Douglas Gregor9194a912012-11-06 19:39:40 +00001058
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001059 // Try to infer a module map from the top-level framework directory.
1060 Module *Result = ModMap.inferFrameworkModule(SubmodulePath.back(),
Douglas Gregora686e1b2012-01-27 19:52:33 +00001061 TopFrameworkDir,
1062 IsSystem,
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001063 /*Parent=*/0);
Douglas Gregor4ddf2222013-01-10 01:43:00 +00001064 if (!Result)
1065 return 0;
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001066
1067 // Follow the submodule path to find the requested (sub)framework module
1068 // within the top-level framework module.
1069 SubmodulePath.pop_back();
1070 while (!SubmodulePath.empty() && Result) {
1071 Result = ModMap.lookupModuleQualified(SubmodulePath.back(), Result);
1072 SubmodulePath.pop_back();
1073 }
1074 return Result;
Douglas Gregor56c64012011-11-17 01:41:17 +00001075}
1076
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001077
Douglas Gregor80b69042011-11-12 00:22:19 +00001078HeaderSearch::LoadModuleMapResult
Douglas Gregor963c5532013-06-21 16:28:10 +00001079HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001080 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
Douglas Gregor963c5532013-06-21 16:28:10 +00001081 return loadModuleMapFile(Dir, IsSystem);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001082
Douglas Gregor80b69042011-11-12 00:22:19 +00001083 return LMM_NoDirectory;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001084}
1085
Douglas Gregor80b69042011-11-12 00:22:19 +00001086HeaderSearch::LoadModuleMapResult
Douglas Gregor963c5532013-06-21 16:28:10 +00001087HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001088 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
1089 = DirectoryHasModuleMap.find(Dir);
1090 if (KnownDir != DirectoryHasModuleMap.end())
Douglas Gregor80b69042011-11-12 00:22:19 +00001091 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001092
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001093 SmallString<128> ModuleMapFileName;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001094 ModuleMapFileName += Dir->getName();
Douglas Gregore7ab3662011-12-07 02:23:45 +00001095 unsigned ModuleMapDirNameLen = ModuleMapFileName.size();
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001096 llvm::sys::path::append(ModuleMapFileName, "module.map");
1097 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) {
1098 // We have found a module map file. Try to parse it.
Douglas Gregor963c5532013-06-21 16:28:10 +00001099 if (ModMap.parseModuleMapFile(ModuleMapFile, IsSystem)) {
Douglas Gregore7ab3662011-12-07 02:23:45 +00001100 // No suitable module map.
1101 DirectoryHasModuleMap[Dir] = false;
1102 return LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001103 }
Douglas Gregore7ab3662011-12-07 02:23:45 +00001104
1105 // This directory has a module map.
1106 DirectoryHasModuleMap[Dir] = true;
1107
1108 // Check whether there is a private module map that we need to load as well.
1109 ModuleMapFileName.erase(ModuleMapFileName.begin() + ModuleMapDirNameLen,
1110 ModuleMapFileName.end());
1111 llvm::sys::path::append(ModuleMapFileName, "module_private.map");
1112 if (const FileEntry *PrivateModuleMapFile
1113 = FileMgr.getFile(ModuleMapFileName)) {
Douglas Gregor963c5532013-06-21 16:28:10 +00001114 if (ModMap.parseModuleMapFile(PrivateModuleMapFile, IsSystem)) {
Douglas Gregore7ab3662011-12-07 02:23:45 +00001115 // No suitable module map.
1116 DirectoryHasModuleMap[Dir] = false;
1117 return LMM_InvalidModuleMap;
1118 }
1119 }
1120
1121 return LMM_NewlyLoaded;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001122 }
1123
1124 // No suitable module map.
1125 DirectoryHasModuleMap[Dir] = false;
Douglas Gregor80b69042011-11-12 00:22:19 +00001126 return LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001127}
Douglas Gregor718292f2011-11-11 19:10:28 +00001128
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001129void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
Douglas Gregor07f43572012-01-29 18:15:03 +00001130 Modules.clear();
1131
1132 // Load module maps for each of the header search directories.
1133 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregor963c5532013-06-21 16:28:10 +00001134 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
Douglas Gregor07f43572012-01-29 18:15:03 +00001135 if (SearchDirs[Idx].isFramework()) {
1136 llvm::error_code EC;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001137 SmallString<128> DirNative;
Douglas Gregor07f43572012-01-29 18:15:03 +00001138 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1139 DirNative);
1140
1141 // Search each of the ".framework" directories to load them as modules.
Douglas Gregor07f43572012-01-29 18:15:03 +00001142 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
1143 Dir != DirEnd && !EC; Dir.increment(EC)) {
1144 if (llvm::sys::path::extension(Dir->path()) != ".framework")
1145 continue;
1146
1147 const DirectoryEntry *FrameworkDir = FileMgr.getDirectory(Dir->path());
1148 if (!FrameworkDir)
1149 continue;
1150
1151 // Load this framework module.
1152 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1153 IsSystem);
1154 }
1155 continue;
1156 }
1157
1158 // FIXME: Deal with header maps.
1159 if (SearchDirs[Idx].isHeaderMap())
1160 continue;
1161
1162 // Try to load a module map file for the search directory.
Douglas Gregor963c5532013-06-21 16:28:10 +00001163 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem);
Douglas Gregor07f43572012-01-29 18:15:03 +00001164
1165 // Try to load module map files for immediate subdirectories of this search
1166 // directory.
Douglas Gregor0339a642013-03-21 01:08:50 +00001167 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
Douglas Gregor07f43572012-01-29 18:15:03 +00001168 }
1169
1170 // Populate the list of modules.
1171 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1172 MEnd = ModMap.module_end();
1173 M != MEnd; ++M) {
1174 Modules.push_back(M->getValue());
1175 }
1176}
Douglas Gregor0339a642013-03-21 01:08:50 +00001177
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001178void HeaderSearch::loadTopLevelSystemModules() {
1179 // Load module maps for each of the header search directories.
1180 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregor299787f2013-11-01 23:08:38 +00001181 // We only care about normal header directories.
1182 if (!SearchDirs[Idx].isNormalDir()) {
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001183 continue;
1184 }
1185
1186 // Try to load a module map file for the search directory.
Douglas Gregor963c5532013-06-21 16:28:10 +00001187 loadModuleMapFile(SearchDirs[Idx].getDir(),
1188 SearchDirs[Idx].isSystemHeaderDirectory());
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001189 }
1190}
1191
Douglas Gregor0339a642013-03-21 01:08:50 +00001192void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1193 if (SearchDir.haveSearchedAllModuleMaps())
1194 return;
1195
1196 llvm::error_code EC;
1197 SmallString<128> DirNative;
1198 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
1199 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
1200 Dir != DirEnd && !EC; Dir.increment(EC)) {
Douglas Gregor963c5532013-06-21 16:28:10 +00001201 loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory());
Douglas Gregor0339a642013-03-21 01:08:50 +00001202 }
1203
1204 SearchDir.setSearchedAllModuleMaps(true);
1205}