blob: 17d518b8d85b8bfc1835b2cd0d3dae8a5060e397 [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;
62}
63
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000064HeaderSearch::~HeaderSearch() {
65 // Delete headermaps.
66 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
67 delete HeaderMaps[i].second;
68}
Mike Stump11289f42009-09-09 15:08:12 +000069
Chris Lattner59a9ebd2006-10-18 05:34:33 +000070void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000071 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
72 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000073 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
74 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
75 NumOnceOnlyFiles += FileInfo[i].isImport;
76 if (MaxNumIncludes < FileInfo[i].NumIncludes)
77 MaxNumIncludes = FileInfo[i].NumIncludes;
78 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
79 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000080 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
81 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
82 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattner23b7eb62007-06-15 23:05:46 +000084 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
85 fprintf(stderr, " %d #includes skipped due to"
86 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner23b7eb62007-06-15 23:05:46 +000088 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
89 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000090}
91
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000092/// CreateHeaderMap - This method returns a HeaderMap for the specified
Sylvestre Ledru830885c2012-07-23 08:59:39 +000093/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
Chris Lattner4ffe46c2007-12-17 18:34:53 +000094const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000095 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerf62f7582007-12-17 07:52:39 +000096 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000097 if (!HeaderMaps.empty()) {
98 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerf62f7582007-12-17 07:52:39 +000099 // Pointer equality comparison of FileEntries works because they are
100 // already uniqued by inode.
Mike Stump11289f42009-09-09 15:08:12 +0000101 if (HeaderMaps[i].first == FE)
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000102 return HeaderMaps[i].second;
103 }
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattner5159f612010-11-23 08:35:12 +0000105 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000106 HeaderMaps.push_back(std::make_pair(FE, HM));
107 return HM;
108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000110 return 0;
111}
112
Douglas Gregor279a6c32012-01-29 17:08:11 +0000113std::string HeaderSearch::getModuleFileName(Module *Module) {
Douglas Gregor1e44e022011-09-12 20:41:59 +0000114 // If we don't have a module cache path, we can't do anything.
Douglas Gregor279a6c32012-01-29 17:08:11 +0000115 if (ModuleCachePath.empty())
116 return std::string();
117
118
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000119 SmallString<256> Result(ModuleCachePath);
Douglas Gregor279a6c32012-01-29 17:08:11 +0000120 llvm::sys::path::append(Result, Module->getTopLevelModule()->Name + ".pcm");
121 return Result.str().str();
122}
123
124std::string HeaderSearch::getModuleFileName(StringRef ModuleName) {
125 // If we don't have a module cache path, we can't do anything.
126 if (ModuleCachePath.empty())
127 return std::string();
Douglas Gregor1735f4e2011-09-13 23:15:45 +0000128
Douglas Gregor279a6c32012-01-29 17:08:11 +0000129
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000130 SmallString<256> Result(ModuleCachePath);
Douglas Gregor279a6c32012-01-29 17:08:11 +0000131 llvm::sys::path::append(Result, ModuleName + ".pcm");
132 return Result.str().str();
133}
134
135Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000136 // Look in the module map to determine if there is a module by this name.
Douglas Gregor279a6c32012-01-29 17:08:11 +0000137 Module *Module = ModMap.findModule(ModuleName);
138 if (Module || !AllowSearch)
139 return Module;
140
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000141 // Look through the various header search paths to load any available module
Douglas Gregor279a6c32012-01-29 17:08:11 +0000142 // maps, searching for a module map that describes this module.
143 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
144 if (SearchDirs[Idx].isFramework()) {
145 // Search for or infer a module map for a framework.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000146 SmallString<128> FrameworkDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000147 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
148 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
149 if (const DirectoryEntry *FrameworkDir
150 = FileMgr.getDirectory(FrameworkDirName)) {
151 bool IsSystem
152 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
153 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000154 if (Module)
155 break;
156 }
Douglas Gregor279a6c32012-01-29 17:08:11 +0000157 }
158
159 // FIXME: Figure out how header maps and module maps will work together.
160
161 // Only deal with normal search directories.
162 if (!SearchDirs[Idx].isNormalDir())
163 continue;
Douglas Gregor963c5532013-06-21 16:28:10 +0000164
165 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
Douglas Gregor279a6c32012-01-29 17:08:11 +0000166 // Search for a module map file in this directory.
Douglas Gregor963c5532013-06-21 16:28:10 +0000167 if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem)
168 == LMM_NewlyLoaded) {
Douglas Gregor279a6c32012-01-29 17:08:11 +0000169 // We just loaded a module map file; check whether the module is
170 // available now.
171 Module = ModMap.findModule(ModuleName);
172 if (Module)
173 break;
174 }
175
176 // Search for a module map in a subdirectory with the same name as the
177 // module.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000178 SmallString<128> NestedModuleMapDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000179 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
180 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
Douglas Gregor963c5532013-06-21 16:28:10 +0000181 if (loadModuleMapFile(NestedModuleMapDirName, IsSystem) == LMM_NewlyLoaded){
Douglas Gregor279a6c32012-01-29 17:08:11 +0000182 // If we just loaded a module map file, look for the module again.
183 Module = ModMap.findModule(ModuleName);
184 if (Module)
185 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000186 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000187
188 // If we've already performed the exhaustive search for module maps in this
189 // search directory, don't do it again.
190 if (SearchDirs[Idx].haveSearchedAllModuleMaps())
191 continue;
192
193 // Load all module maps in the immediate subdirectories of this search
194 // directory.
195 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
196
197 // Look again for the module.
198 Module = ModMap.findModule(ModuleName);
199 if (Module)
200 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000201 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000202
Douglas Gregor279a6c32012-01-29 17:08:11 +0000203 return Module;
Douglas Gregor1e44e022011-09-12 20:41:59 +0000204}
205
Chris Lattnerf62f7582007-12-17 07:52:39 +0000206//===----------------------------------------------------------------------===//
207// File lookup within a DirectoryLookup scope
208//===----------------------------------------------------------------------===//
209
Chris Lattner8d720d02007-12-17 17:57:27 +0000210/// getName - Return the directory or filename corresponding to this lookup
211/// object.
212const char *DirectoryLookup::getName() const {
213 if (isNormalDir())
214 return getDir()->getName();
215 if (isFramework())
216 return getFrameworkDir()->getName();
217 assert(isHeaderMap() && "Unknown DirectoryLookup");
218 return getHeaderMap()->getFileName();
219}
220
221
Chris Lattnerf62f7582007-12-17 07:52:39 +0000222/// LookupFile - Lookup the specified file in this search path, returning it
223/// if it exists or returning null if not.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000224const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000225 StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000226 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000227 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000228 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000229 ModuleMap::KnownHeader *SuggestedModule,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000230 bool &InUserSpecifiedSystemFramework) const {
231 InUserSpecifiedSystemFramework = false;
232
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000233 SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000234 if (isNormalDir()) {
235 // Concatenate the requested file onto the directory.
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000236 TmpDir = getDir()->getName();
237 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000238 if (SearchPath != NULL) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000239 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000240 SearchPath->clear();
241 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
242 }
243 if (RelativePath != NULL) {
244 RelativePath->clear();
245 RelativePath->append(Filename.begin(), Filename.end());
246 }
Douglas Gregor718292f2011-11-11 19:10:28 +0000247
248 // If we have a module map that might map this header, load it and
249 // check whether we'll have a suggestion for a module.
Daniel Jasper97da9172013-10-22 08:09:47 +0000250 HS.hasModuleMap(TmpDir, getDir(), isSystemHeaderDirectory());
251 if (SuggestedModule) {
252 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(),
Douglas Gregor718292f2011-11-11 19:10:28 +0000253 /*openFile=*/false);
254 if (!File)
255 return File;
256
Daniel Jasper97da9172013-10-22 08:09:47 +0000257 // If there is a module that corresponds to this header, suggest it.
Douglas Gregor2537a362011-12-08 17:01:29 +0000258 *SuggestedModule = HS.findModuleForHeader(File);
Daniel Jasper97da9172013-10-22 08:09:47 +0000259 if (!SuggestedModule->getModule() &&
260 HS.hasModuleMap(TmpDir, getDir(), isSystemHeaderDirectory()))
261 *SuggestedModule = HS.findModuleForHeader(File);
Douglas Gregor718292f2011-11-11 19:10:28 +0000262 return File;
263 }
264
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000265 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattner712e3872007-12-17 08:13:48 +0000266 }
Mike Stump11289f42009-09-09 15:08:12 +0000267
Chris Lattner712e3872007-12-17 08:13:48 +0000268 if (isFramework())
Douglas Gregor97eec242011-09-15 22:00:41 +0000269 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000270 SuggestedModule, InUserSpecifiedSystemFramework);
Mike Stump11289f42009-09-09 15:08:12 +0000271
Chris Lattner44bd21b2007-12-17 08:17:39 +0000272 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000273 const FileEntry * const Result = getHeaderMap()->LookupFile(
274 Filename, HS.getFileMgr());
275 if (Result) {
276 if (SearchPath != NULL) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000277 StringRef SearchPathRef(getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000278 SearchPath->clear();
279 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
280 }
281 if (RelativePath != NULL) {
282 RelativePath->clear();
283 RelativePath->append(Filename.begin(), Filename.end());
284 }
285 }
286 return Result;
Chris Lattnerf62f7582007-12-17 07:52:39 +0000287}
288
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000289/// \brief Given a framework directory, find the top-most framework directory.
290///
291/// \param FileMgr The file manager to use for directory lookups.
292/// \param DirName The name of the framework directory.
293/// \param SubmodulePath Will be populated with the submodule path from the
294/// returned top-level module to the originally named framework.
295static const DirectoryEntry *
296getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
297 SmallVectorImpl<std::string> &SubmodulePath) {
298 assert(llvm::sys::path::extension(DirName) == ".framework" &&
299 "Not a framework directory");
300
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000301 // Note: as an egregious but useful hack we use the real path here, because
302 // frameworks moving between top-level frameworks to embedded frameworks tend
303 // to be symlinked, and we base the logical structure of modules on the
304 // physical layout. In particular, we need to deal with crazy includes like
305 //
306 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
307 //
308 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
309 // which one should access with, e.g.,
310 //
311 // #include <Bar/Wibble.h>
312 //
313 // Similar issues occur when a top-level framework has moved into an
314 // embedded framework.
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000315 const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
Douglas Gregore00c8b22013-01-26 00:55:12 +0000316 DirName = FileMgr.getCanonicalName(TopFrameworkDir);
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000317 do {
318 // Get the parent directory name.
319 DirName = llvm::sys::path::parent_path(DirName);
320 if (DirName.empty())
321 break;
322
323 // Determine whether this directory exists.
324 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
325 if (!Dir)
326 break;
327
328 // If this is a framework directory, then we're a subframework of this
329 // framework.
330 if (llvm::sys::path::extension(DirName) == ".framework") {
331 SubmodulePath.push_back(llvm::sys::path::stem(DirName));
332 TopFrameworkDir = Dir;
333 }
334 } while (true);
335
336 return TopFrameworkDir;
337}
Chris Lattnerf62f7582007-12-17 07:52:39 +0000338
Chris Lattner712e3872007-12-17 08:13:48 +0000339/// DoFrameworkLookup - Do a lookup of the specified file in the current
340/// DirectoryLookup, which is a framework directory.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000341const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000342 StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000343 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000344 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000345 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000346 ModuleMap::KnownHeader *SuggestedModule,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000347 bool &InUserSpecifiedSystemFramework) const
Douglas Gregor97eec242011-09-15 22:00:41 +0000348{
Chris Lattner712e3872007-12-17 08:13:48 +0000349 FileManager &FileMgr = HS.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000350
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000351 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000352 size_t SlashPos = Filename.find('/');
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000353 if (SlashPos == StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Chris Lattner712e3872007-12-17 08:13:48 +0000355 // Find out if this is the home for the specified framework, by checking
Daniel Dunbar17138612012-04-05 17:09:40 +0000356 // HeaderSearch. Possible answers are yes/no and unknown.
357 HeaderSearch::FrameworkCacheEntry &CacheEntry =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000358 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000359
Chris Lattner712e3872007-12-17 08:13:48 +0000360 // If it is known and in some other directory, fail.
Daniel Dunbar17138612012-04-05 17:09:40 +0000361 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
Chris Lattner5ed76da2006-10-22 07:24:13 +0000362 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000363
Chris Lattner712e3872007-12-17 08:13:48 +0000364 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000365
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000366 // FrameworkName = "/System/Library/Frameworks/"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000367 SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000368 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000369 if (FrameworkName.empty() || FrameworkName.back() != '/')
370 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000371
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000372 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor56c64012011-11-17 01:41:17 +0000373 StringRef ModuleName(Filename.begin(), SlashPos);
374 FrameworkName += ModuleName;
Mike Stump11289f42009-09-09 15:08:12 +0000375
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000376 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
377 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000378
Daniel Dunbar17138612012-04-05 17:09:40 +0000379 // If the cache entry was unresolved, populate it now.
380 if (CacheEntry.Directory == 0) {
Chris Lattner712e3872007-12-17 08:13:48 +0000381 HS.IncrementFrameworkLookupCount();
Mike Stump11289f42009-09-09 15:08:12 +0000382
Chris Lattner5ed76da2006-10-22 07:24:13 +0000383 // If the framework dir doesn't exist, we fail.
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000384 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
385 if (Dir == 0) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000386
Chris Lattner5ed76da2006-10-22 07:24:13 +0000387 // Otherwise, if it does, remember that this is the right direntry for this
388 // framework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000389 CacheEntry.Directory = getFrameworkDir();
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000390
391 // If this is a user search directory, check if the framework has been
392 // user-specified as a system framework.
393 if (getDirCharacteristic() == SrcMgr::C_User) {
394 SmallString<1024> SystemFrameworkMarker(FrameworkName);
395 SystemFrameworkMarker += ".system_framework";
396 if (llvm::sys::fs::exists(SystemFrameworkMarker.str())) {
397 CacheEntry.IsUserSpecifiedSystemFramework = true;
398 }
399 }
Chris Lattner5ed76da2006-10-22 07:24:13 +0000400 }
Mike Stump11289f42009-09-09 15:08:12 +0000401
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000402 // Set the 'user-specified system framework' flag.
403 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
404
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000405 if (RelativePath != NULL) {
406 RelativePath->clear();
407 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
408 }
Douglas Gregor56c64012011-11-17 01:41:17 +0000409
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000410 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000411 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000413 FrameworkName += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000414
415 if (SearchPath != NULL) {
416 SearchPath->clear();
417 // Without trailing '/'.
418 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
419 }
420
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000421 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000422 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
423 /*openFile=*/!SuggestedModule);
424 if (!FE) {
425 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
426 const char *Private = "Private";
427 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
428 Private+strlen(Private));
429 if (SearchPath != NULL)
430 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
431 Private+strlen(Private));
432
433 FE = FileMgr.getFile(FrameworkName.str(), /*openFile=*/!SuggestedModule);
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000436 // If we found the header and are allowed to suggest a module, do so now.
437 if (FE && SuggestedModule) {
438 // Find the framework in which this header occurs.
439 StringRef FrameworkPath = FE->getName();
440 bool FoundFramework = false;
441 do {
442 // Get the parent directory name.
443 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
444 if (FrameworkPath.empty())
445 break;
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000446
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000447 // Determine whether this directory exists.
448 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
449 if (!Dir)
450 break;
451
452 // If this is a framework directory, then we're a subframework of this
453 // framework.
454 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
455 FoundFramework = true;
456 break;
457 }
458 } while (true);
459
460 if (FoundFramework) {
461 // Find the top-level framework based on this framework.
462 SmallVector<std::string, 4> SubmodulePath;
463 const DirectoryEntry *TopFrameworkDir
464 = ::getTopFrameworkDir(FileMgr, FrameworkPath, SubmodulePath);
465
466 // Determine the name of the top-level framework.
467 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
468
469 // Load this framework module. If that succeeds, find the suggested module
470 // for this header, if any.
471 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
472 if (HS.loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
473 *SuggestedModule = HS.findModuleForHeader(FE);
474 }
475 } else {
476 *SuggestedModule = HS.findModuleForHeader(FE);
477 }
478 }
Douglas Gregor97eec242011-09-15 22:00:41 +0000479 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000480}
481
Douglas Gregor89929282012-01-30 06:01:29 +0000482void HeaderSearch::setTarget(const TargetInfo &Target) {
483 ModMap.setTarget(Target);
484}
485
Chris Lattnerf62f7582007-12-17 07:52:39 +0000486
Chris Lattner712e3872007-12-17 08:13:48 +0000487//===----------------------------------------------------------------------===//
488// Header File Location.
489//===----------------------------------------------------------------------===//
490
491
James Dennettc07ab2c2012-06-20 00:56:32 +0000492/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000493/// return null on failure. isAngled indicates whether the file reference is
James Dennettc07ab2c2012-06-20 00:56:32 +0000494/// for system \#include's or not (i.e. using <> instead of ""). CurFileEnt, if
495/// non-null, indicates where the \#including file is, in case a relative search
Douglas Gregor618e64a2010-08-08 07:49:23 +0000496/// is needed.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000497const FileEntry *HeaderSearch::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000498 StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000499 bool isAngled,
500 const DirectoryLookup *FromDir,
501 const DirectoryLookup *&CurDir,
502 const FileEntry *CurFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000503 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000504 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000505 ModuleMap::KnownHeader *SuggestedModule,
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000506 bool SkipCache)
Douglas Gregor97eec242011-09-15 22:00:41 +0000507{
Daniel Jasper97da9172013-10-22 08:09:47 +0000508 if (!HSOpts->ModuleMapFiles.empty()) {
509 // Preload all explicitly specified module map files. This enables modules
510 // map files lying in a directory structure separate from the header files
511 // that they describe. These cannot be loaded lazily upon encountering a
512 // header file, as there is no other knwon mapping from a header file to its
513 // module map file.
514 for (llvm::SetVector<std::string>::iterator
515 I = HSOpts->ModuleMapFiles.begin(),
516 E = HSOpts->ModuleMapFiles.end();
517 I != E; ++I) {
518 const FileEntry *File = FileMgr.getFile(*I);
519 if (!File)
520 continue;
521 loadModuleMapFile(File, /*IsSystem=*/false);
522 }
523 HSOpts->ModuleMapFiles.clear();
524 }
525
Douglas Gregor97eec242011-09-15 22:00:41 +0000526 if (SuggestedModule)
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000527 *SuggestedModule = ModuleMap::KnownHeader();
Douglas Gregor97eec242011-09-15 22:00:41 +0000528
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000529 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000530 if (llvm::sys::path::is_absolute(Filename)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000531 CurDir = 0;
532
533 // If this was an #include_next "/absolute/file", fail.
534 if (FromDir) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000535
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000536 if (SearchPath != NULL)
537 SearchPath->clear();
538 if (RelativePath != NULL) {
539 RelativePath->clear();
540 RelativePath->append(Filename.begin(), Filename.end());
541 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000542 // Otherwise, just return the file.
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000543 return FileMgr.getFile(Filename, /*openFile=*/true);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregor9f93e382011-07-28 04:45:53 +0000546 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor618e64a2010-08-08 07:49:23 +0000547 // directory. This has to be based on CurFileEnt, not CurDir, because
548 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerf62f7582007-12-17 07:52:39 +0000549 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
550 // This search is not done for <> headers.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000551 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
Douglas Gregor618e64a2010-08-08 07:49:23 +0000552 // Concatenate the requested file onto the directory.
Dmitri Gribenko76857f82013-12-10 01:36:10 +0000553 SmallString<1024> TmpDir(CurFileEnt->getDir()->getName());
554 llvm::sys::path::append(TmpDir, Filename);
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000555 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000556 // Leave CurDir unset.
Douglas Gregor618e64a2010-08-08 07:49:23 +0000557 // This file is a system header or C++ unfriendly if the old file is.
558 //
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000559 // Note that we only use one of FromHFI/ToHFI at once, due to potential
560 // reallocation of the underlying vector potentially making the first
561 // reference binding dangling.
562 HeaderFileInfo &FromHFI = getFileInfo(CurFileEnt);
563 unsigned DirInfo = FromHFI.DirInfo;
564 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
565 StringRef Framework = FromHFI.Framework;
566
567 HeaderFileInfo &ToHFI = getFileInfo(FE);
568 ToHFI.DirInfo = DirInfo;
569 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
570 ToHFI.Framework = Framework;
571
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000572 if (SearchPath != NULL) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000573 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000574 SearchPath->clear();
575 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
576 }
577 if (RelativePath != NULL) {
578 RelativePath->clear();
579 RelativePath->append(Filename.begin(), Filename.end());
580 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000581 return FE;
582 }
583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000585 CurDir = 0;
586
587 // If this is a system #include, ignore the user #include locs.
Nico Weber3b1d1212011-05-24 04:31:14 +0000588 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000589
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000590 // If this is a #include_next request, start searching after the directory the
591 // file was found in.
592 if (FromDir)
593 i = FromDir-&SearchDirs[0];
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattnerd4275422007-07-22 07:28:00 +0000595 // Cache all of the lookups performed by this method. Many headers are
596 // multiply included, and the "pragma once" optimization prevents them from
597 // being relex/pp'd, but they would still have to search through a
598 // (potentially huge) series of SearchDirs to find it.
599 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000600 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattnerd4275422007-07-22 07:28:00 +0000601
602 // If the entry has been previously looked up, the first value will be
603 // non-zero. If the value is equal to i (the start point of our search), then
604 // this is a matching hit.
Douglas Gregor8ad31c22011-11-20 17:46:46 +0000605 if (!SkipCache && CacheLookup.first == i+1) {
Chris Lattnerd4275422007-07-22 07:28:00 +0000606 // Skip querying potentially lots of directories for this lookup.
607 i = CacheLookup.second;
608 } else {
609 // Otherwise, this is the first query, or the previous query didn't match
610 // our search start. We will fill in our found location below, so prime the
611 // start point value.
612 CacheLookup.first = i+1;
613 }
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000615 // Check each directory in sequence to see if it contains this file.
616 for (; i != SearchDirs.size(); ++i) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000617 bool InUserSpecifiedSystemFramework = false;
Mike Stump11289f42009-09-09 15:08:12 +0000618 const FileEntry *FE =
Douglas Gregor97eec242011-09-15 22:00:41 +0000619 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000620 SuggestedModule, InUserSpecifiedSystemFramework);
Chris Lattner712e3872007-12-17 08:13:48 +0000621 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000622
Chris Lattner712e3872007-12-17 08:13:48 +0000623 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000624
Chris Lattner712e3872007-12-17 08:13:48 +0000625 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor9f93e382011-07-28 04:45:53 +0000626 HeaderFileInfo &HFI = getFileInfo(FE);
627 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000628
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000629 // If the directory characteristic is User but this framework was
630 // user-specified to be treated as a system framework, promote the
631 // characteristic.
632 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
633 HFI.DirInfo = SrcMgr::C_System;
634
Richard Smith8acadcb2012-06-13 20:27:03 +0000635 // If the filename matches a known system header prefix, override
636 // whether the file is a system header.
Richard Trieu871f5f32012-06-13 20:52:36 +0000637 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
638 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
639 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
Richard Smith8acadcb2012-06-13 20:27:03 +0000640 : SrcMgr::C_User;
641 break;
642 }
643 }
644
Douglas Gregor9f93e382011-07-28 04:45:53 +0000645 // If this file is found in a header map and uses the framework style of
646 // includes, then this header is part of a framework we're building.
647 if (CurDir->isIndexHeaderMap()) {
648 size_t SlashPos = Filename.find('/');
649 if (SlashPos != StringRef::npos) {
650 HFI.IndexHeaderMapHeader = 1;
651 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
652 SlashPos));
653 }
654 }
655
Chris Lattner712e3872007-12-17 08:13:48 +0000656 // Remember this location for the next lookup we do.
657 CacheLookup.second = i;
658 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000659 }
Mike Stump11289f42009-09-09 15:08:12 +0000660
Douglas Gregord8575e12011-07-30 06:28:34 +0000661 // If we are including a file with a quoted include "foo.h" from inside
662 // a header in a framework that is currently being built, and we couldn't
663 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
664 // "Foo" is the name of the framework in which the including header was found.
665 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
666 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
667 if (IncludingHFI.IndexHeaderMapHeader) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000668 SmallString<128> ScratchFilename;
Douglas Gregord8575e12011-07-30 06:28:34 +0000669 ScratchFilename += IncludingHFI.Framework;
670 ScratchFilename += '/';
671 ScratchFilename += Filename;
672
673 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
674 FromDir, CurDir, CurFileEnt,
Douglas Gregor97eec242011-09-15 22:00:41 +0000675 SearchPath, RelativePath,
676 SuggestedModule);
Douglas Gregord8575e12011-07-30 06:28:34 +0000677 std::pair<unsigned, unsigned> &CacheLookup
678 = LookupFileCache.GetOrCreateValue(Filename).getValue();
679 CacheLookup.second
680 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
681 return Result;
682 }
683 }
684
Chris Lattnerd4275422007-07-22 07:28:00 +0000685 // Otherwise, didn't find it. Remember we didn't find this.
686 CacheLookup.second = SearchDirs.size();
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000687 return 0;
688}
689
Chris Lattner63dd32b2006-10-20 04:42:40 +0000690/// LookupSubframeworkHeader - Look up a subframework for the specified
James Dennettc07ab2c2012-06-20 00:56:32 +0000691/// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
Chris Lattner63dd32b2006-10-20 04:42:40 +0000692/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
693/// is a subframework within Carbon.framework. If so, return the FileEntry
694/// for the designated file, otherwise return null.
695const FileEntry *HeaderSearch::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000696LookupSubframeworkHeader(StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000697 const FileEntry *ContextFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000698 SmallVectorImpl<char> *SearchPath,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000699 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000700 ModuleMap::KnownHeader *SuggestedModule) {
Chris Lattner12261882008-02-01 05:34:02 +0000701 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000702
Chris Lattner63dd32b2006-10-20 04:42:40 +0000703 // Framework names must have a '/' in the filename. Find it.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000704 // FIXME: Should we permit '\' on Windows?
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000705 size_t SlashPos = Filename.find('/');
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000706 if (SlashPos == StringRef::npos) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000707
Chris Lattner63dd32b2006-10-20 04:42:40 +0000708 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000709 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000710
Chris Lattner63dd32b2006-10-20 04:42:40 +0000711 // If the context info wasn't a framework, couldn't be a subframework.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000712 const unsigned DotFrameworkLen = 10;
713 const char *FrameworkPos = strstr(ContextName, ".framework");
714 if (FrameworkPos == 0 ||
715 (FrameworkPos[DotFrameworkLen] != '/' &&
716 FrameworkPos[DotFrameworkLen] != '\\'))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000717 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000718
Daniel Dunbar17138612012-04-05 17:09:40 +0000719 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000720
Chris Lattner63dd32b2006-10-20 04:42:40 +0000721 // Append Frameworks/HIToolbox.framework/
722 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000723 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000724 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000725
Daniel Dunbar17138612012-04-05 17:09:40 +0000726 llvm::StringMapEntry<FrameworkCacheEntry> &CacheLookup =
Chris Lattner8afa6de2010-11-21 09:55:08 +0000727 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000728
Chris Lattner5ed76da2006-10-22 07:24:13 +0000729 // Some other location?
Daniel Dunbar17138612012-04-05 17:09:40 +0000730 if (CacheLookup.getValue().Directory &&
Chris Lattner34d1f5a2007-02-08 19:08:49 +0000731 CacheLookup.getKeyLength() == FrameworkName.size() &&
732 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
733 CacheLookup.getKeyLength()) != 0)
Chris Lattner5ed76da2006-10-22 07:24:13 +0000734 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000735
Chris Lattner5ed76da2006-10-22 07:24:13 +0000736 // Cache subframework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000737 if (CacheLookup.getValue().Directory == 0) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000738 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000739
Chris Lattner5ed76da2006-10-22 07:24:13 +0000740 // If the framework dir doesn't exist, we fail.
Chris Lattner5159f612010-11-23 08:35:12 +0000741 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Chris Lattner5ed76da2006-10-22 07:24:13 +0000742 if (Dir == 0) return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000743
Chris Lattner5ed76da2006-10-22 07:24:13 +0000744 // Otherwise, if it does, remember that this is the right direntry for this
745 // framework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000746 CacheLookup.getValue().Directory = Dir;
Chris Lattner5ed76da2006-10-22 07:24:13 +0000747 }
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattner577377e2006-10-20 04:55:45 +0000749 const FileEntry *FE = 0;
750
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000751 if (RelativePath != NULL) {
752 RelativePath->clear();
753 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
754 }
755
Chris Lattner63dd32b2006-10-20 04:42:40 +0000756 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000757 SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000758 HeadersFilename += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000759 if (SearchPath != NULL) {
760 SearchPath->clear();
761 // Without trailing '/'.
762 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
763 }
764
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000765 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000766 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump11289f42009-09-09 15:08:12 +0000767
Chris Lattner63dd32b2006-10-20 04:42:40 +0000768 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000769 HeadersFilename = FrameworkName;
770 HeadersFilename += "PrivateHeaders/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000771 if (SearchPath != NULL) {
772 SearchPath->clear();
773 // Without trailing '/'.
774 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
775 }
776
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000777 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidisd6278e32011-03-16 19:17:25 +0000778 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Chris Lattner63dd32b2006-10-20 04:42:40 +0000779 return 0;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000780 }
Mike Stump11289f42009-09-09 15:08:12 +0000781
Chris Lattner577377e2006-10-20 04:55:45 +0000782 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000783 //
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000784 // Note that the temporary 'DirInfo' is required here, as either call to
785 // getFileInfo could resize the vector and we don't want to rely on order
786 // of evaluation.
787 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
788 getFileInfo(FE).DirInfo = DirInfo;
Douglas Gregorf5f94522013-02-08 00:10:48 +0000789
790 // If we're supposed to suggest a module, look for one now.
791 if (SuggestedModule) {
792 // Find the top-level framework based on this framework.
793 FrameworkName.pop_back(); // remove the trailing '/'
794 SmallVector<std::string, 4> SubmodulePath;
795 const DirectoryEntry *TopFrameworkDir
796 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
797
798 // Determine the name of the top-level framework.
799 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
800
801 // Load this framework module. If that succeeds, find the suggested module
802 // for this header, if any.
803 bool IsSystem = false;
804 if (loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
805 *SuggestedModule = findModuleForHeader(FE);
806 }
807 }
808
Chris Lattner577377e2006-10-20 04:55:45 +0000809 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000810}
811
Chandler Carruthb0ffe502011-12-09 01:33:57 +0000812/// \brief Helper static function to normalize a path for injection into
813/// a synthetic header.
814/*static*/ std::string
815HeaderSearch::NormalizeDashIncludePath(StringRef File, FileManager &FileMgr) {
816 // Implicit include paths should be resolved relative to the current
817 // working directory first, and then use the regular header search
818 // mechanism. The proper way to handle this is to have the
819 // predefines buffer located at the current working directory, but
820 // it has no file entry. For now, workaround this by using an
821 // absolute path if we find the file here, and otherwise letting
822 // header search handle it.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000823 SmallString<128> Path(File);
Chandler Carruthb0ffe502011-12-09 01:33:57 +0000824 llvm::sys::fs::make_absolute(Path);
825 bool exists;
826 if (llvm::sys::fs::exists(Path.str(), exists) || !exists)
827 Path = File;
828 else if (exists)
829 FileMgr.getFile(File);
830
831 return Lexer::Stringify(Path.str());
832}
833
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000834//===----------------------------------------------------------------------===//
835// File Info Management.
836//===----------------------------------------------------------------------===//
837
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000838/// \brief Merge the header file info provided by \p OtherHFI into the current
839/// header file info (\p HFI)
840static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
841 const HeaderFileInfo &OtherHFI) {
842 HFI.isImport |= OtherHFI.isImport;
843 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000844 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000845 HFI.NumIncludes += OtherHFI.NumIncludes;
846
847 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
848 HFI.ControllingMacro = OtherHFI.ControllingMacro;
849 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
850 }
851
852 if (OtherHFI.External) {
853 HFI.DirInfo = OtherHFI.DirInfo;
854 HFI.External = OtherHFI.External;
855 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
856 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000857
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000858 if (HFI.Framework.empty())
859 HFI.Framework = OtherHFI.Framework;
860
861 HFI.Resolved = true;
862}
863
Steve Naroff3fa455a2009-04-24 20:03:17 +0000864/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000865/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000866HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000867 if (FE->getUID() >= FileInfo.size())
868 FileInfo.resize(FE->getUID()+1);
Douglas Gregor09b69892011-02-10 17:09:37 +0000869
870 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000871 if (ExternalSource && !HFI.Resolved)
872 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregor09b69892011-02-10 17:09:37 +0000873 return HFI;
Mike Stump11289f42009-09-09 15:08:12 +0000874}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000875
Douglas Gregor37aa4932011-05-04 00:14:37 +0000876bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
877 // Check if we've ever seen this file as a header.
878 if (File->getUID() >= FileInfo.size())
879 return false;
880
881 // Resolve header file info from the external source, if needed.
882 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000883 if (ExternalSource && !HFI.Resolved)
884 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregor37aa4932011-05-04 00:14:37 +0000885
Argyrios Kyrtzidis0d355df2012-12-10 20:08:37 +0000886 return HFI.isPragmaOnce || HFI.isImport ||
887 HFI.ControllingMacro || HFI.ControllingMacroID;
Douglas Gregor37aa4932011-05-04 00:14:37 +0000888}
889
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +0000890void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000891 ModuleMap::ModuleHeaderRole Role,
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +0000892 bool isCompilingModuleHeader) {
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000893 if (FE->getUID() >= FileInfo.size())
894 FileInfo.resize(FE->getUID()+1);
895
896 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
897 HFI.isModuleHeader = true;
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +0000898 HFI.isCompilingModuleHeader = isCompilingModuleHeader;
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000899 HFI.setHeaderRole(Role);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000900}
901
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000902bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
903 ++NumIncluded; // Count # of attempted #includes.
904
905 // Get information about this file.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000906 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump11289f42009-09-09 15:08:12 +0000907
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000908 // If this is a #import directive, check that we have not already imported
909 // this header.
910 if (isImport) {
911 // If this has already been imported, don't import it again.
912 FileInfo.isImport = true;
Mike Stump11289f42009-09-09 15:08:12 +0000913
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000914 // Has this already been #import'ed or #include'd?
915 if (FileInfo.NumIncludes) return false;
916 } else {
917 // Otherwise, if this is a #include of a file that was previously #import'd
918 // or if this is the second #include of a #pragma once file, ignore it.
919 if (FileInfo.isImport)
920 return false;
921 }
Mike Stump11289f42009-09-09 15:08:12 +0000922
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000923 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
924 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump11289f42009-09-09 15:08:12 +0000925 if (const IdentifierInfo *ControllingMacro
Douglas Gregor99734e72009-04-25 23:30:02 +0000926 = FileInfo.getControllingMacro(ExternalLookup))
927 if (ControllingMacro->hasMacroDefinition()) {
928 ++NumMultiIncludeFileOptzn;
929 return false;
930 }
Mike Stump11289f42009-09-09 15:08:12 +0000931
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000932 // Increment the number of times this file has been included.
933 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +0000934
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000935 return true;
936}
937
Ted Kremenekfbcce6f2011-07-26 23:46:11 +0000938size_t HeaderSearch::getTotalMemory() const {
939 return SearchDirs.capacity()
Ted Kremenekae63d102011-07-27 18:41:18 +0000940 + llvm::capacity_in_bytes(FileInfo)
941 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekfbcce6f2011-07-26 23:46:11 +0000942 + LookupFileCache.getAllocator().getTotalMemory()
943 + FrameworkMap.getAllocator().getTotalMemory();
944}
Douglas Gregor9f93e382011-07-28 04:45:53 +0000945
946StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
947 return FrameworkNames.GetOrCreateValue(Framework).getKey();
948}
Douglas Gregor718292f2011-11-11 19:10:28 +0000949
950bool HeaderSearch::hasModuleMap(StringRef FileName,
Douglas Gregor963c5532013-06-21 16:28:10 +0000951 const DirectoryEntry *Root,
952 bool IsSystem) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000953 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
Douglas Gregor718292f2011-11-11 19:10:28 +0000954
955 StringRef DirName = FileName;
956 do {
957 // Get the parent directory name.
958 DirName = llvm::sys::path::parent_path(DirName);
959 if (DirName.empty())
960 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +0000961
Douglas Gregor718292f2011-11-11 19:10:28 +0000962 // Determine whether this directory exists.
963 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
964 if (!Dir)
965 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +0000966
967 // Try to load the "module.map" file in this directory.
Douglas Gregor963c5532013-06-21 16:28:10 +0000968 switch (loadModuleMapFile(Dir, IsSystem)) {
Douglas Gregor80b69042011-11-12 00:22:19 +0000969 case LMM_NewlyLoaded:
970 case LMM_AlreadyLoaded:
Daniel Jasperca9f7382013-09-24 09:27:13 +0000971 // Success. All of the directories we stepped through inherit this module
972 // map file.
973 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
974 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
975 return true;
Daniel Jasper97da9172013-10-22 08:09:47 +0000976
977 case LMM_NoDirectory:
978 case LMM_InvalidModuleMap:
979 break;
Daniel Jasperca9f7382013-09-24 09:27:13 +0000980 }
981
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000982 // If we hit the top of our search, we're done.
983 if (Dir == Root)
984 return false;
985
Douglas Gregor718292f2011-11-11 19:10:28 +0000986 // Keep track of all of the directories we checked, so we can mark them as
987 // having module maps if we eventually do find a module map.
988 FixUpDirectories.push_back(Dir);
989 } while (true);
Douglas Gregor718292f2011-11-11 19:10:28 +0000990}
991
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000992ModuleMap::KnownHeader
993HeaderSearch::findModuleForHeader(const FileEntry *File) const {
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000994 if (ExternalSource) {
995 // Make sure the external source has handled header info about this file,
996 // which includes whether the file is part of a module.
997 (void)getFileInfo(File);
998 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000999 return ModMap.findModuleForHeader(File);
Douglas Gregor718292f2011-11-11 19:10:28 +00001000}
1001
Douglas Gregor963c5532013-06-21 16:28:10 +00001002bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001003 const DirectoryEntry *Dir = File->getDir();
1004
1005 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
1006 = DirectoryHasModuleMap.find(Dir);
1007 if (KnownDir != DirectoryHasModuleMap.end())
1008 return !KnownDir->second;
1009
Douglas Gregor963c5532013-06-21 16:28:10 +00001010 bool Result = ModMap.parseModuleMapFile(File, IsSystem);
Douglas Gregor80306772011-12-07 21:25:07 +00001011 if (!Result && llvm::sys::path::filename(File->getName()) == "module.map") {
1012 // If the file we loaded was a module.map, look for the corresponding
1013 // module_private.map.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001014 SmallString<128> PrivateFilename(Dir->getName());
Douglas Gregor80306772011-12-07 21:25:07 +00001015 llvm::sys::path::append(PrivateFilename, "module_private.map");
1016 if (const FileEntry *PrivateFile = FileMgr.getFile(PrivateFilename))
Douglas Gregor963c5532013-06-21 16:28:10 +00001017 Result = ModMap.parseModuleMapFile(PrivateFile, IsSystem);
Douglas Gregor80306772011-12-07 21:25:07 +00001018 }
1019
1020 DirectoryHasModuleMap[Dir] = !Result;
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001021 return Result;
1022}
1023
Douglas Gregor279a6c32012-01-29 17:08:11 +00001024Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1025 const DirectoryEntry *Dir,
1026 bool IsSystem) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001027 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor56c64012011-11-17 01:41:17 +00001028 return Module;
1029
1030 // Try to load a module map file.
Douglas Gregor963c5532013-06-21 16:28:10 +00001031 switch (loadModuleMapFile(Dir, IsSystem)) {
Douglas Gregor56c64012011-11-17 01:41:17 +00001032 case LMM_InvalidModuleMap:
1033 break;
1034
1035 case LMM_AlreadyLoaded:
1036 case LMM_NoDirectory:
1037 return 0;
1038
1039 case LMM_NewlyLoaded:
1040 return ModMap.findModule(Name);
1041 }
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001042
Douglas Gregor4ddf2222013-01-10 01:43:00 +00001043 // Figure out the top-level framework directory and the submodule path from
1044 // that top-level framework to the requested framework.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001045 SmallVector<std::string, 2> SubmodulePath;
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001046 SubmodulePath.push_back(Name);
Douglas Gregor4ddf2222013-01-10 01:43:00 +00001047 const DirectoryEntry *TopFrameworkDir
1048 = ::getTopFrameworkDir(FileMgr, Dir->getName(), SubmodulePath);
Douglas Gregor9194a912012-11-06 19:39:40 +00001049
Douglas Gregor9194a912012-11-06 19:39:40 +00001050
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001051 // Try to infer a module map from the top-level framework directory.
1052 Module *Result = ModMap.inferFrameworkModule(SubmodulePath.back(),
Douglas Gregora686e1b2012-01-27 19:52:33 +00001053 TopFrameworkDir,
1054 IsSystem,
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001055 /*Parent=*/0);
Douglas Gregor4ddf2222013-01-10 01:43:00 +00001056 if (!Result)
1057 return 0;
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001058
1059 // Follow the submodule path to find the requested (sub)framework module
1060 // within the top-level framework module.
1061 SubmodulePath.pop_back();
1062 while (!SubmodulePath.empty() && Result) {
1063 Result = ModMap.lookupModuleQualified(SubmodulePath.back(), Result);
1064 SubmodulePath.pop_back();
1065 }
1066 return Result;
Douglas Gregor56c64012011-11-17 01:41:17 +00001067}
1068
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001069
Douglas Gregor80b69042011-11-12 00:22:19 +00001070HeaderSearch::LoadModuleMapResult
Douglas Gregor963c5532013-06-21 16:28:10 +00001071HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001072 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
Douglas Gregor963c5532013-06-21 16:28:10 +00001073 return loadModuleMapFile(Dir, IsSystem);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001074
Douglas Gregor80b69042011-11-12 00:22:19 +00001075 return LMM_NoDirectory;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001076}
1077
Douglas Gregor80b69042011-11-12 00:22:19 +00001078HeaderSearch::LoadModuleMapResult
Douglas Gregor963c5532013-06-21 16:28:10 +00001079HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001080 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir
1081 = DirectoryHasModuleMap.find(Dir);
1082 if (KnownDir != DirectoryHasModuleMap.end())
Douglas Gregor80b69042011-11-12 00:22:19 +00001083 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001084
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001085 SmallString<128> ModuleMapFileName;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001086 ModuleMapFileName += Dir->getName();
Douglas Gregore7ab3662011-12-07 02:23:45 +00001087 unsigned ModuleMapDirNameLen = ModuleMapFileName.size();
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001088 llvm::sys::path::append(ModuleMapFileName, "module.map");
1089 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) {
1090 // We have found a module map file. Try to parse it.
Douglas Gregor963c5532013-06-21 16:28:10 +00001091 if (ModMap.parseModuleMapFile(ModuleMapFile, IsSystem)) {
Douglas Gregore7ab3662011-12-07 02:23:45 +00001092 // No suitable module map.
1093 DirectoryHasModuleMap[Dir] = false;
1094 return LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001095 }
Douglas Gregore7ab3662011-12-07 02:23:45 +00001096
1097 // This directory has a module map.
1098 DirectoryHasModuleMap[Dir] = true;
1099
1100 // Check whether there is a private module map that we need to load as well.
1101 ModuleMapFileName.erase(ModuleMapFileName.begin() + ModuleMapDirNameLen,
1102 ModuleMapFileName.end());
1103 llvm::sys::path::append(ModuleMapFileName, "module_private.map");
1104 if (const FileEntry *PrivateModuleMapFile
1105 = FileMgr.getFile(ModuleMapFileName)) {
Douglas Gregor963c5532013-06-21 16:28:10 +00001106 if (ModMap.parseModuleMapFile(PrivateModuleMapFile, IsSystem)) {
Douglas Gregore7ab3662011-12-07 02:23:45 +00001107 // No suitable module map.
1108 DirectoryHasModuleMap[Dir] = false;
1109 return LMM_InvalidModuleMap;
1110 }
1111 }
1112
1113 return LMM_NewlyLoaded;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001114 }
1115
1116 // No suitable module map.
1117 DirectoryHasModuleMap[Dir] = false;
Douglas Gregor80b69042011-11-12 00:22:19 +00001118 return LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001119}
Douglas Gregor718292f2011-11-11 19:10:28 +00001120
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001121void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
Douglas Gregor07f43572012-01-29 18:15:03 +00001122 Modules.clear();
1123
1124 // Load module maps for each of the header search directories.
1125 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregor963c5532013-06-21 16:28:10 +00001126 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
Douglas Gregor07f43572012-01-29 18:15:03 +00001127 if (SearchDirs[Idx].isFramework()) {
1128 llvm::error_code EC;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00001129 SmallString<128> DirNative;
Douglas Gregor07f43572012-01-29 18:15:03 +00001130 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1131 DirNative);
1132
1133 // Search each of the ".framework" directories to load them as modules.
Douglas Gregor07f43572012-01-29 18:15:03 +00001134 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
1135 Dir != DirEnd && !EC; Dir.increment(EC)) {
1136 if (llvm::sys::path::extension(Dir->path()) != ".framework")
1137 continue;
1138
1139 const DirectoryEntry *FrameworkDir = FileMgr.getDirectory(Dir->path());
1140 if (!FrameworkDir)
1141 continue;
1142
1143 // Load this framework module.
1144 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1145 IsSystem);
1146 }
1147 continue;
1148 }
1149
1150 // FIXME: Deal with header maps.
1151 if (SearchDirs[Idx].isHeaderMap())
1152 continue;
1153
1154 // Try to load a module map file for the search directory.
Douglas Gregor963c5532013-06-21 16:28:10 +00001155 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem);
Douglas Gregor07f43572012-01-29 18:15:03 +00001156
1157 // Try to load module map files for immediate subdirectories of this search
1158 // directory.
Douglas Gregor0339a642013-03-21 01:08:50 +00001159 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
Douglas Gregor07f43572012-01-29 18:15:03 +00001160 }
1161
1162 // Populate the list of modules.
1163 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1164 MEnd = ModMap.module_end();
1165 M != MEnd; ++M) {
1166 Modules.push_back(M->getValue());
1167 }
1168}
Douglas Gregor0339a642013-03-21 01:08:50 +00001169
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001170void HeaderSearch::loadTopLevelSystemModules() {
1171 // Load module maps for each of the header search directories.
1172 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregor299787f2013-11-01 23:08:38 +00001173 // We only care about normal header directories.
1174 if (!SearchDirs[Idx].isNormalDir()) {
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001175 continue;
1176 }
1177
1178 // Try to load a module map file for the search directory.
Douglas Gregor963c5532013-06-21 16:28:10 +00001179 loadModuleMapFile(SearchDirs[Idx].getDir(),
1180 SearchDirs[Idx].isSystemHeaderDirectory());
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001181 }
1182}
1183
Douglas Gregor0339a642013-03-21 01:08:50 +00001184void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1185 if (SearchDir.haveSearchedAllModuleMaps())
1186 return;
1187
1188 llvm::error_code EC;
1189 SmallString<128> DirNative;
1190 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
1191 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
1192 Dir != DirEnd && !EC; Dir.increment(EC)) {
Douglas Gregor963c5532013-06-21 16:28:10 +00001193 loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory());
Douglas Gregor0339a642013-03-21 01:08:50 +00001194 }
1195
1196 SearchDir.setSearchedAllModuleMaps(true);
1197}