blob: 6c5c64bd266b0a5865bd91bcc00d6aced91fbaed [file] [log] [blame]
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner59a9ebd2006-10-18 05:34:33 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DirectoryLookup and HeaderSearch interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner59a9ebd2006-10-18 05:34:33 +000014#include "clang/Lex/HeaderSearch.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/IdentifierTable.h"
Richard Smith2aedca32015-07-01 02:29:35 +000017#include "clang/Lex/ExternalPreprocessorSource.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/HeaderMap.h"
19#include "clang/Lex/HeaderSearchOptions.h"
Will Wilson0fafd342013-12-27 19:46:16 +000020#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "clang/Lex/Lexer.h"
Richard Smith20e883e2015-04-29 23:20:19 +000022#include "clang/Lex/Preprocessor.h"
Ben Langmuirbeee15e2014-04-14 18:00:01 +000023#include "llvm/ADT/APInt.h"
24#include "llvm/ADT/Hashing.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenekae63d102011-07-27 18:41:18 +000026#include "llvm/Support/Capacity.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/Path.h"
Daniel Jasperba7f2f72013-09-24 09:14:14 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000030#include <cstdio>
Douglas Gregor01c7cfa2013-01-22 23:49:45 +000031#if defined(LLVM_ON_UNIX)
Dmitri Gribenkoeadae012013-01-26 16:29:36 +000032#include <limits.h>
Douglas Gregor01c7cfa2013-01-22 23:49:45 +000033#endif
Chris Lattner59a9ebd2006-10-18 05:34:33 +000034using namespace clang;
35
Douglas Gregor99734e72009-04-25 23:30:02 +000036const IdentifierInfo *
Richard Smith2aedca32015-07-01 02:29:35 +000037HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
38 if (ControllingMacro) {
39 if (ControllingMacro->isOutOfDate())
40 External->updateOutOfDateIdentifier(
41 *const_cast<IdentifierInfo *>(ControllingMacro));
Douglas Gregor99734e72009-04-25 23:30:02 +000042 return ControllingMacro;
Richard Smith2aedca32015-07-01 02:29:35 +000043 }
Douglas Gregor99734e72009-04-25 23:30:02 +000044
45 if (!ControllingMacroID || !External)
Craig Topperd2d442c2014-05-17 23:10:59 +000046 return nullptr;
Douglas Gregor99734e72009-04-25 23:30:02 +000047
48 ControllingMacro = External->GetIdentifier(ControllingMacroID);
49 return ControllingMacro;
50}
51
Douglas Gregor09b69892011-02-10 17:09:37 +000052ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
53
Dmitri Gribenkof8579502013-01-12 19:30:44 +000054HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000055 SourceManager &SourceMgr, DiagnosticsEngine &Diags,
Will Wilson0fafd342013-12-27 19:46:16 +000056 const LangOptions &LangOpts,
Douglas Gregor89929282012-01-30 06:01:29 +000057 const TargetInfo *Target)
Will Wilsonba2f1462013-12-27 20:02:27 +000058 : HSOpts(HSOpts), Diags(Diags), FileMgr(SourceMgr.getFileManager()),
Daniel Jasper21a0f552014-11-25 09:45:48 +000059 FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this),
60 LangOpts(LangOpts) {
Nico Weber3b1d1212011-05-24 04:31:14 +000061 AngledDirIdx = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000062 SystemDirIdx = 0;
63 NoCurDirSearch = false;
Mike Stump11289f42009-09-09 15:08:12 +000064
Craig Topperd2d442c2014-05-17 23:10:59 +000065 ExternalLookup = nullptr;
66 ExternalSource = nullptr;
Chris Lattner641a0be2006-10-20 06:23:14 +000067 NumIncluded = 0;
68 NumMultiIncludeFileOptzn = 0;
69 NumFrameworkLookups = NumSubFrameworkLookups = 0;
70}
71
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000072HeaderSearch::~HeaderSearch() {
73 // Delete headermaps.
74 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
75 delete HeaderMaps[i].second;
76}
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattner59a9ebd2006-10-18 05:34:33 +000078void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000079 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
80 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000081 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
82 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
83 NumOnceOnlyFiles += FileInfo[i].isImport;
84 if (MaxNumIncludes < FileInfo[i].NumIncludes)
85 MaxNumIncludes = FileInfo[i].NumIncludes;
86 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
87 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000088 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
89 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
90 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattner23b7eb62007-06-15 23:05:46 +000092 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
93 fprintf(stderr, " %d #includes skipped due to"
94 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump11289f42009-09-09 15:08:12 +000095
Chris Lattner23b7eb62007-06-15 23:05:46 +000096 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
97 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000098}
99
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000100/// CreateHeaderMap - This method returns a HeaderMap for the specified
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000101/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
Chris Lattner4ffe46c2007-12-17 18:34:53 +0000102const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000103 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerf62f7582007-12-17 07:52:39 +0000104 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000105 if (!HeaderMaps.empty()) {
106 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerf62f7582007-12-17 07:52:39 +0000107 // Pointer equality comparison of FileEntries works because they are
108 // already uniqued by inode.
Mike Stump11289f42009-09-09 15:08:12 +0000109 if (HeaderMaps[i].first == FE)
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000110 return HeaderMaps[i].second;
111 }
Mike Stump11289f42009-09-09 15:08:12 +0000112
Chris Lattner5159f612010-11-23 08:35:12 +0000113 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000114 HeaderMaps.push_back(std::make_pair(FE, HM));
115 return HM;
116 }
Mike Stump11289f42009-09-09 15:08:12 +0000117
Craig Topperd2d442c2014-05-17 23:10:59 +0000118 return nullptr;
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000119}
120
Douglas Gregor279a6c32012-01-29 17:08:11 +0000121std::string HeaderSearch::getModuleFileName(Module *Module) {
Ben Langmuir9d6448b2014-08-09 00:57:23 +0000122 const FileEntry *ModuleMap =
123 getModuleMap().getModuleMapFileForUniquing(Module);
Ben Langmuird89dc562015-02-19 20:23:22 +0000124 return getModuleFileName(Module->Name, ModuleMap->getName());
Douglas Gregor279a6c32012-01-29 17:08:11 +0000125}
126
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000127std::string HeaderSearch::getModuleFileName(StringRef ModuleName,
Ben Langmuird89dc562015-02-19 20:23:22 +0000128 StringRef ModuleMapPath) {
Douglas Gregor279a6c32012-01-29 17:08:11 +0000129 // If we don't have a module cache path, we can't do anything.
130 if (ModuleCachePath.empty())
131 return std::string();
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000132
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000133 SmallString<256> Result(ModuleCachePath);
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000134 llvm::sys::fs::make_absolute(Result);
135
136 if (HSOpts->DisableModuleHash) {
137 llvm::sys::path::append(Result, ModuleName + ".pcm");
138 } else {
139 // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
Richard Smith54cc3c22014-12-11 20:50:24 +0000140 // ideally be globally unique to this particular module. Name collisions
141 // in the hash are safe (because any translation unit can only import one
142 // module with each name), but result in a loss of caching.
143 //
144 // To avoid false-negatives, we form as canonical a path as we can, and map
145 // to lower-case in case we're on a case-insensitive file system.
146 auto *Dir =
147 FileMgr.getDirectory(llvm::sys::path::parent_path(ModuleMapPath));
148 if (!Dir)
149 return std::string();
150 auto DirName = FileMgr.getCanonicalName(Dir);
151 auto FileName = llvm::sys::path::filename(ModuleMapPath);
152
153 llvm::hash_code Hash =
154 llvm::hash_combine(DirName.lower(), FileName.lower());
155
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000156 SmallString<128> HashStr;
Richard Smith54cc3c22014-12-11 20:50:24 +0000157 llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
Yaron Keren92e1b622015-03-18 10:17:07 +0000158 llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000159 }
Douglas Gregor279a6c32012-01-29 17:08:11 +0000160 return Result.str().str();
161}
162
163Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000164 // Look in the module map to determine if there is a module by this name.
Douglas Gregor279a6c32012-01-29 17:08:11 +0000165 Module *Module = ModMap.findModule(ModuleName);
Richard Smith47972af2015-06-16 00:08:24 +0000166 if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
Douglas Gregor279a6c32012-01-29 17:08:11 +0000167 return Module;
168
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000169 // Look through the various header search paths to load any available module
Douglas Gregor279a6c32012-01-29 17:08:11 +0000170 // maps, searching for a module map that describes this module.
171 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
172 if (SearchDirs[Idx].isFramework()) {
173 // Search for or infer a module map for a framework.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000174 SmallString<128> FrameworkDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000175 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
176 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
177 if (const DirectoryEntry *FrameworkDir
178 = FileMgr.getDirectory(FrameworkDirName)) {
179 bool IsSystem
180 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
181 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000182 if (Module)
183 break;
184 }
Douglas Gregor279a6c32012-01-29 17:08:11 +0000185 }
186
187 // FIXME: Figure out how header maps and module maps will work together.
188
189 // Only deal with normal search directories.
190 if (!SearchDirs[Idx].isNormalDir())
191 continue;
Douglas Gregor963c5532013-06-21 16:28:10 +0000192
193 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
Douglas Gregor279a6c32012-01-29 17:08:11 +0000194 // Search for a module map file in this directory.
Ben Langmuir984e1df2014-03-19 20:23:34 +0000195 if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
196 /*IsFramework*/false) == LMM_NewlyLoaded) {
Douglas Gregor279a6c32012-01-29 17:08:11 +0000197 // We just loaded a module map file; check whether the module is
198 // available now.
199 Module = ModMap.findModule(ModuleName);
200 if (Module)
201 break;
202 }
203
204 // Search for a module map in a subdirectory with the same name as the
205 // module.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000206 SmallString<128> NestedModuleMapDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000207 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
208 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
Ben Langmuir984e1df2014-03-19 20:23:34 +0000209 if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
210 /*IsFramework*/false) == LMM_NewlyLoaded){
Douglas Gregor279a6c32012-01-29 17:08:11 +0000211 // If we just loaded a module map file, look for the module again.
212 Module = ModMap.findModule(ModuleName);
213 if (Module)
214 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000215 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000216
217 // If we've already performed the exhaustive search for module maps in this
218 // search directory, don't do it again.
219 if (SearchDirs[Idx].haveSearchedAllModuleMaps())
220 continue;
221
222 // Load all module maps in the immediate subdirectories of this search
223 // directory.
224 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
225
226 // Look again for the module.
227 Module = ModMap.findModule(ModuleName);
228 if (Module)
229 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000230 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000231
Douglas Gregor279a6c32012-01-29 17:08:11 +0000232 return Module;
Douglas Gregor1e44e022011-09-12 20:41:59 +0000233}
234
Chris Lattnerf62f7582007-12-17 07:52:39 +0000235//===----------------------------------------------------------------------===//
236// File lookup within a DirectoryLookup scope
237//===----------------------------------------------------------------------===//
238
Chris Lattner8d720d02007-12-17 17:57:27 +0000239/// getName - Return the directory or filename corresponding to this lookup
240/// object.
241const char *DirectoryLookup::getName() const {
242 if (isNormalDir())
243 return getDir()->getName();
244 if (isFramework())
245 return getFrameworkDir()->getName();
246 assert(isHeaderMap() && "Unknown DirectoryLookup");
247 return getHeaderMap()->getFileName();
248}
249
Richard Smith8c71eba2014-03-05 20:51:45 +0000250static const FileEntry *
251getFileAndSuggestModule(HeaderSearch &HS, StringRef FileName,
252 const DirectoryEntry *Dir, bool IsSystemHeaderDir,
253 ModuleMap::KnownHeader *SuggestedModule) {
254 // If we have a module map that might map this header, load it and
255 // check whether we'll have a suggestion for a module.
256 HS.hasModuleMap(FileName, Dir, IsSystemHeaderDir);
257 if (SuggestedModule) {
258 const FileEntry *File = HS.getFileMgr().getFile(FileName,
259 /*OpenFile=*/false);
260 if (File) {
261 // If there is a module that corresponds to this header, suggest it.
262 *SuggestedModule = HS.findModuleForHeader(File);
263
264 // FIXME: This appears to be a no-op. We loaded the module map for this
265 // directory at the start of this function.
266 if (!SuggestedModule->getModule() &&
267 HS.hasModuleMap(FileName, Dir, IsSystemHeaderDir))
268 *SuggestedModule = HS.findModuleForHeader(File);
269 }
270
271 return File;
272 }
273
274 return HS.getFileMgr().getFile(FileName, /*openFile=*/true);
275}
Chris Lattner8d720d02007-12-17 17:57:27 +0000276
Chris Lattnerf62f7582007-12-17 07:52:39 +0000277/// LookupFile - Lookup the specified file in this search path, returning it
278/// if it exists or returning null if not.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000279const FileEntry *DirectoryLookup::LookupFile(
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000280 StringRef &Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000281 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000282 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000283 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000284 ModuleMap::KnownHeader *SuggestedModule,
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000285 bool &InUserSpecifiedSystemFramework,
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000286 bool &HasBeenMapped,
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000287 SmallVectorImpl<char> &MappedName) const {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000288 InUserSpecifiedSystemFramework = false;
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000289 HasBeenMapped = false;
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000290
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000291 SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000292 if (isNormalDir()) {
293 // Concatenate the requested file onto the directory.
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000294 TmpDir = getDir()->getName();
295 llvm::sys::path::append(TmpDir, Filename);
Craig Topperd2d442c2014-05-17 23:10:59 +0000296 if (SearchPath) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000297 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000298 SearchPath->clear();
299 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
300 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000301 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000302 RelativePath->clear();
303 RelativePath->append(Filename.begin(), Filename.end());
304 }
Richard Smith8c71eba2014-03-05 20:51:45 +0000305
Yaron Keren92e1b622015-03-18 10:17:07 +0000306 return getFileAndSuggestModule(HS, TmpDir, getDir(),
Richard Smith8c71eba2014-03-05 20:51:45 +0000307 isSystemHeaderDirectory(),
308 SuggestedModule);
Chris Lattner712e3872007-12-17 08:13:48 +0000309 }
Mike Stump11289f42009-09-09 15:08:12 +0000310
Chris Lattner712e3872007-12-17 08:13:48 +0000311 if (isFramework())
Douglas Gregor97eec242011-09-15 22:00:41 +0000312 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000313 SuggestedModule, InUserSpecifiedSystemFramework);
Mike Stump11289f42009-09-09 15:08:12 +0000314
Chris Lattner44bd21b2007-12-17 08:17:39 +0000315 assert(isHeaderMap() && "Unknown directory lookup");
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000316 const HeaderMap *HM = getHeaderMap();
317 SmallString<1024> Path;
318 StringRef Dest = HM->lookupFilename(Filename, Path);
319 if (Dest.empty())
Craig Topperd2d442c2014-05-17 23:10:59 +0000320 return nullptr;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000321
322 const FileEntry *Result;
323
324 // Check if the headermap maps the filename to a framework include
325 // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
326 // framework include.
327 if (llvm::sys::path::is_relative(Dest)) {
328 MappedName.clear();
329 MappedName.append(Dest.begin(), Dest.end());
330 Filename = StringRef(MappedName.begin(), MappedName.size());
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000331 HasBeenMapped = true;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000332 Result = HM->LookupFile(Filename, HS.getFileMgr());
333
334 } else {
335 Result = HS.getFileMgr().getFile(Dest);
336 }
337
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000338 if (Result) {
Craig Topperd2d442c2014-05-17 23:10:59 +0000339 if (SearchPath) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000340 StringRef SearchPathRef(getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000341 SearchPath->clear();
342 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
343 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000344 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000345 RelativePath->clear();
346 RelativePath->append(Filename.begin(), Filename.end());
347 }
348 }
349 return Result;
Chris Lattnerf62f7582007-12-17 07:52:39 +0000350}
351
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000352/// \brief Given a framework directory, find the top-most framework directory.
353///
354/// \param FileMgr The file manager to use for directory lookups.
355/// \param DirName The name of the framework directory.
356/// \param SubmodulePath Will be populated with the submodule path from the
357/// returned top-level module to the originally named framework.
358static const DirectoryEntry *
359getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
360 SmallVectorImpl<std::string> &SubmodulePath) {
361 assert(llvm::sys::path::extension(DirName) == ".framework" &&
362 "Not a framework directory");
363
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000364 // Note: as an egregious but useful hack we use the real path here, because
365 // frameworks moving between top-level frameworks to embedded frameworks tend
366 // to be symlinked, and we base the logical structure of modules on the
367 // physical layout. In particular, we need to deal with crazy includes like
368 //
369 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
370 //
371 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
372 // which one should access with, e.g.,
373 //
374 // #include <Bar/Wibble.h>
375 //
376 // Similar issues occur when a top-level framework has moved into an
377 // embedded framework.
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000378 const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
Douglas Gregore00c8b22013-01-26 00:55:12 +0000379 DirName = FileMgr.getCanonicalName(TopFrameworkDir);
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000380 do {
381 // Get the parent directory name.
382 DirName = llvm::sys::path::parent_path(DirName);
383 if (DirName.empty())
384 break;
385
386 // Determine whether this directory exists.
387 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
388 if (!Dir)
389 break;
390
391 // If this is a framework directory, then we're a subframework of this
392 // framework.
393 if (llvm::sys::path::extension(DirName) == ".framework") {
394 SubmodulePath.push_back(llvm::sys::path::stem(DirName));
395 TopFrameworkDir = Dir;
396 }
397 } while (true);
398
399 return TopFrameworkDir;
400}
Chris Lattnerf62f7582007-12-17 07:52:39 +0000401
Chris Lattner712e3872007-12-17 08:13:48 +0000402/// DoFrameworkLookup - Do a lookup of the specified file in the current
403/// DirectoryLookup, which is a framework directory.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000404const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000405 StringRef Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000406 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000407 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000408 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000409 ModuleMap::KnownHeader *SuggestedModule,
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000410 bool &InUserSpecifiedSystemFramework) const
Douglas Gregor97eec242011-09-15 22:00:41 +0000411{
Chris Lattner712e3872007-12-17 08:13:48 +0000412 FileManager &FileMgr = HS.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000414 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000415 size_t SlashPos = Filename.find('/');
Craig Topperd2d442c2014-05-17 23:10:59 +0000416 if (SlashPos == StringRef::npos) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattner712e3872007-12-17 08:13:48 +0000418 // Find out if this is the home for the specified framework, by checking
Daniel Dunbar17138612012-04-05 17:09:40 +0000419 // HeaderSearch. Possible answers are yes/no and unknown.
420 HeaderSearch::FrameworkCacheEntry &CacheEntry =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000421 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattner712e3872007-12-17 08:13:48 +0000423 // If it is known and in some other directory, fail.
Daniel Dunbar17138612012-04-05 17:09:40 +0000424 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
Craig Topperd2d442c2014-05-17 23:10:59 +0000425 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattner712e3872007-12-17 08:13:48 +0000427 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000429 // FrameworkName = "/System/Library/Frameworks/"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000430 SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000431 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000432 if (FrameworkName.empty() || FrameworkName.back() != '/')
433 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000434
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000435 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor56c64012011-11-17 01:41:17 +0000436 StringRef ModuleName(Filename.begin(), SlashPos);
437 FrameworkName += ModuleName;
Mike Stump11289f42009-09-09 15:08:12 +0000438
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000439 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
440 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000441
Daniel Dunbar17138612012-04-05 17:09:40 +0000442 // If the cache entry was unresolved, populate it now.
Craig Topperd2d442c2014-05-17 23:10:59 +0000443 if (!CacheEntry.Directory) {
Chris Lattner712e3872007-12-17 08:13:48 +0000444 HS.IncrementFrameworkLookupCount();
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattner5ed76da2006-10-22 07:24:13 +0000446 // If the framework dir doesn't exist, we fail.
Yaron Keren92e1b622015-03-18 10:17:07 +0000447 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
Craig Topperd2d442c2014-05-17 23:10:59 +0000448 if (!Dir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000449
Chris Lattner5ed76da2006-10-22 07:24:13 +0000450 // Otherwise, if it does, remember that this is the right direntry for this
451 // framework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000452 CacheEntry.Directory = getFrameworkDir();
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000453
454 // If this is a user search directory, check if the framework has been
455 // user-specified as a system framework.
456 if (getDirCharacteristic() == SrcMgr::C_User) {
457 SmallString<1024> SystemFrameworkMarker(FrameworkName);
458 SystemFrameworkMarker += ".system_framework";
Yaron Keren92e1b622015-03-18 10:17:07 +0000459 if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000460 CacheEntry.IsUserSpecifiedSystemFramework = true;
461 }
462 }
Chris Lattner5ed76da2006-10-22 07:24:13 +0000463 }
Mike Stump11289f42009-09-09 15:08:12 +0000464
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000465 // Set the 'user-specified system framework' flag.
466 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
467
Craig Topperd2d442c2014-05-17 23:10:59 +0000468 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000469 RelativePath->clear();
470 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
471 }
Douglas Gregor56c64012011-11-17 01:41:17 +0000472
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000473 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000474 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000475
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000476 FrameworkName += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000477
Craig Topperd2d442c2014-05-17 23:10:59 +0000478 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000479 SearchPath->clear();
480 // Without trailing '/'.
481 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
482 }
483
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000484 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000485 const FileEntry *FE = FileMgr.getFile(FrameworkName,
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000486 /*openFile=*/!SuggestedModule);
487 if (!FE) {
488 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
489 const char *Private = "Private";
490 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
491 Private+strlen(Private));
Craig Topperd2d442c2014-05-17 23:10:59 +0000492 if (SearchPath)
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000493 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
494 Private+strlen(Private));
495
Yaron Keren92e1b622015-03-18 10:17:07 +0000496 FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000497 }
Mike Stump11289f42009-09-09 15:08:12 +0000498
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000499 // If we found the header and are allowed to suggest a module, do so now.
500 if (FE && SuggestedModule) {
501 // Find the framework in which this header occurs.
Ben Langmuiref914b82014-05-15 16:20:33 +0000502 StringRef FrameworkPath = FE->getDir()->getName();
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000503 bool FoundFramework = false;
504 do {
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000505 // Determine whether this directory exists.
506 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
507 if (!Dir)
508 break;
509
510 // If this is a framework directory, then we're a subframework of this
511 // framework.
512 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
513 FoundFramework = true;
514 break;
515 }
Ben Langmuiref914b82014-05-15 16:20:33 +0000516
517 // Get the parent directory name.
518 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
519 if (FrameworkPath.empty())
520 break;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000521 } while (true);
522
523 if (FoundFramework) {
524 // Find the top-level framework based on this framework.
525 SmallVector<std::string, 4> SubmodulePath;
526 const DirectoryEntry *TopFrameworkDir
527 = ::getTopFrameworkDir(FileMgr, FrameworkPath, SubmodulePath);
528
529 // Determine the name of the top-level framework.
530 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
531
532 // Load this framework module. If that succeeds, find the suggested module
533 // for this header, if any.
534 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
Ben Langmuira5254002015-07-02 13:19:48 +0000535 HS.loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem);
536
537 // FIXME: This can find a module not part of ModuleName, which is
538 // important so that we're consistent about whether this header
539 // corresponds to a module. Possibly we should lock down framework modules
540 // so that this is not possible.
541 *SuggestedModule = HS.findModuleForHeader(FE);
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000542 } else {
543 *SuggestedModule = HS.findModuleForHeader(FE);
544 }
545 }
Douglas Gregor97eec242011-09-15 22:00:41 +0000546 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000547}
548
Douglas Gregor89929282012-01-30 06:01:29 +0000549void HeaderSearch::setTarget(const TargetInfo &Target) {
550 ModMap.setTarget(Target);
551}
552
Chris Lattnerf62f7582007-12-17 07:52:39 +0000553
Chris Lattner712e3872007-12-17 08:13:48 +0000554//===----------------------------------------------------------------------===//
555// Header File Location.
556//===----------------------------------------------------------------------===//
557
Reid Klecknera97d4c02014-02-18 23:49:24 +0000558/// \brief Return true with a diagnostic if the file that MSVC would have found
559/// fails to match the one that Clang would have found with MSVC header search
560/// disabled.
561static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
562 const FileEntry *MSFE, const FileEntry *FE,
563 SourceLocation IncludeLoc) {
564 if (MSFE && FE != MSFE) {
565 Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
566 return true;
567 }
568 return false;
569}
Chris Lattner712e3872007-12-17 08:13:48 +0000570
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000571static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
572 assert(!Str.empty());
573 char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
574 std::copy(Str.begin(), Str.end(), CopyStr);
575 CopyStr[Str.size()] = '\0';
576 return CopyStr;
577}
578
James Dennettc07ab2c2012-06-20 00:56:32 +0000579/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000580/// return null on failure. isAngled indicates whether the file reference is
Will Wilson0fafd342013-12-27 19:46:16 +0000581/// for system \#include's or not (i.e. using <> instead of ""). Includers, if
582/// non-empty, indicates where the \#including file(s) are, in case a relative
583/// search is needed. Microsoft mode will pass all \#including files.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000584const FileEntry *HeaderSearch::LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000585 StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
586 const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000587 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
588 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
Will Wilson0fafd342013-12-27 19:46:16 +0000589 ModuleMap::KnownHeader *SuggestedModule, bool SkipCache) {
Douglas Gregor97eec242011-09-15 22:00:41 +0000590 if (SuggestedModule)
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000591 *SuggestedModule = ModuleMap::KnownHeader();
Douglas Gregor97eec242011-09-15 22:00:41 +0000592
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000593 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000594 if (llvm::sys::path::is_absolute(Filename)) {
Craig Topperd2d442c2014-05-17 23:10:59 +0000595 CurDir = nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000596
597 // If this was an #include_next "/absolute/file", fail.
Craig Topperd2d442c2014-05-17 23:10:59 +0000598 if (FromDir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Craig Topperd2d442c2014-05-17 23:10:59 +0000600 if (SearchPath)
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000601 SearchPath->clear();
Craig Topperd2d442c2014-05-17 23:10:59 +0000602 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000603 RelativePath->clear();
604 RelativePath->append(Filename.begin(), Filename.end());
605 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000606 // Otherwise, just return the file.
Richard Smitha0aafa32015-05-18 03:52:30 +0000607 const FileEntry *File = FileMgr.getFile(Filename, /*openFile=*/true);
608 if (File && SuggestedModule) {
609 // If there is a module that corresponds to this header, suggest it.
610 hasModuleMap(Filename, File->getDir(), /*SystemHeaderDir*/false);
611 *SuggestedModule = findModuleForHeader(File);
612 }
613 return File;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000614 }
Mike Stump11289f42009-09-09 15:08:12 +0000615
Reid Klecknera97d4c02014-02-18 23:49:24 +0000616 // This is the header that MSVC's header search would have found.
Craig Topperd2d442c2014-05-17 23:10:59 +0000617 const FileEntry *MSFE = nullptr;
Richard Smith8c71eba2014-03-05 20:51:45 +0000618 ModuleMap::KnownHeader MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000619
Douglas Gregor9f93e382011-07-28 04:45:53 +0000620 // Unless disabled, check to see if the file is in the #includer's
Will Wilson0fafd342013-12-27 19:46:16 +0000621 // directory. This cannot be based on CurDir, because each includer could be
622 // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
623 // include of "baz.h" should resolve to "whatever/foo/baz.h".
Chris Lattnerf62f7582007-12-17 07:52:39 +0000624 // This search is not done for <> headers.
Will Wilson0fafd342013-12-27 19:46:16 +0000625 if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
NAKAMURA Takumi9cb62642013-12-10 02:36:28 +0000626 SmallString<1024> TmpDir;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000627 bool First = true;
628 for (const auto &IncluderAndDir : Includers) {
629 const FileEntry *Includer = IncluderAndDir.first;
630
Will Wilson0fafd342013-12-27 19:46:16 +0000631 // Concatenate the requested file onto the directory.
Nikola Smiljaniccf385dc2015-05-08 06:02:37 +0000632 // FIXME: Portability. Filename concatenation should be in sys::Path.
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000633 TmpDir = IncluderAndDir.second->getName();
Nikola Smiljaniccf385dc2015-05-08 06:02:37 +0000634 TmpDir.push_back('/');
635 TmpDir.append(Filename.begin(), Filename.end());
Richard Smith8c71eba2014-03-05 20:51:45 +0000636
Richard Smith6f548ec2014-03-06 18:08:08 +0000637 // FIXME: We don't cache the result of getFileInfo across the call to
638 // getFileAndSuggestModule, because it's a reference to an element of
639 // a container that could be reallocated across this call.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000640 //
641 // FIXME: If we have no includer, that means we're processing a #include
642 // from a module build. We should treat this as a system header if we're
643 // building a [system] module.
Richard Smith6f548ec2014-03-06 18:08:08 +0000644 bool IncluderIsSystemHeader =
Richard Smith3c1a41a2014-12-02 00:08:08 +0000645 Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000646 if (const FileEntry *FE = getFileAndSuggestModule(
Yaron Keren92e1b622015-03-18 10:17:07 +0000647 *this, TmpDir, IncluderAndDir.second,
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000648 IncluderIsSystemHeader, SuggestedModule)) {
Richard Smith3c1a41a2014-12-02 00:08:08 +0000649 if (!Includer) {
650 assert(First && "only first includer can have no file");
651 return FE;
652 }
653
Will Wilson0fafd342013-12-27 19:46:16 +0000654 // Leave CurDir unset.
655 // This file is a system header or C++ unfriendly if the old file is.
656 //
657 // Note that we only use one of FromHFI/ToHFI at once, due to potential
658 // reallocation of the underlying vector potentially making the first
659 // reference binding dangling.
Richard Smith6f548ec2014-03-06 18:08:08 +0000660 HeaderFileInfo &FromHFI = getFileInfo(Includer);
Will Wilson0fafd342013-12-27 19:46:16 +0000661 unsigned DirInfo = FromHFI.DirInfo;
662 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
663 StringRef Framework = FromHFI.Framework;
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000664
Will Wilson0fafd342013-12-27 19:46:16 +0000665 HeaderFileInfo &ToHFI = getFileInfo(FE);
666 ToHFI.DirInfo = DirInfo;
667 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
668 ToHFI.Framework = Framework;
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000669
Craig Topperd2d442c2014-05-17 23:10:59 +0000670 if (SearchPath) {
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000671 StringRef SearchPathRef(IncluderAndDir.second->getName());
Will Wilson0fafd342013-12-27 19:46:16 +0000672 SearchPath->clear();
673 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
674 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000675 if (RelativePath) {
Will Wilson0fafd342013-12-27 19:46:16 +0000676 RelativePath->clear();
677 RelativePath->append(Filename.begin(), Filename.end());
678 }
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000679 if (First)
Reid Klecknera97d4c02014-02-18 23:49:24 +0000680 return FE;
681
682 // Otherwise, we found the path via MSVC header search rules. If
683 // -Wmsvc-include is enabled, we have to keep searching to see if we
684 // would've found this header in -I or -isystem directories.
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000685 if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
Reid Klecknera97d4c02014-02-18 23:49:24 +0000686 return FE;
687 } else {
688 MSFE = FE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000689 if (SuggestedModule) {
690 MSSuggestedModule = *SuggestedModule;
691 *SuggestedModule = ModuleMap::KnownHeader();
692 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000693 break;
694 }
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000695 }
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000696 First = false;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000697 }
698 }
Mike Stump11289f42009-09-09 15:08:12 +0000699
Craig Topperd2d442c2014-05-17 23:10:59 +0000700 CurDir = nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000701
702 // If this is a system #include, ignore the user #include locs.
Nico Weber3b1d1212011-05-24 04:31:14 +0000703 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000704
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000705 // If this is a #include_next request, start searching after the directory the
706 // file was found in.
707 if (FromDir)
708 i = FromDir-&SearchDirs[0];
Mike Stump11289f42009-09-09 15:08:12 +0000709
Chris Lattnerd4275422007-07-22 07:28:00 +0000710 // Cache all of the lookups performed by this method. Many headers are
711 // multiply included, and the "pragma once" optimization prevents them from
712 // being relex/pp'd, but they would still have to search through a
713 // (potentially huge) series of SearchDirs to find it.
David Blaikie13156b62014-11-19 03:06:06 +0000714 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
Chris Lattnerd4275422007-07-22 07:28:00 +0000715
716 // If the entry has been previously looked up, the first value will be
717 // non-zero. If the value is equal to i (the start point of our search), then
718 // this is a matching hit.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000719 if (!SkipCache && CacheLookup.StartIdx == i+1) {
Chris Lattnerd4275422007-07-22 07:28:00 +0000720 // Skip querying potentially lots of directories for this lookup.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000721 i = CacheLookup.HitIdx;
722 if (CacheLookup.MappedName)
723 Filename = CacheLookup.MappedName;
Chris Lattnerd4275422007-07-22 07:28:00 +0000724 } else {
725 // Otherwise, this is the first query, or the previous query didn't match
726 // our search start. We will fill in our found location below, so prime the
727 // start point value.
Argyrios Kyrtzidis7bd78a92014-03-29 03:22:54 +0000728 CacheLookup.reset(/*StartIdx=*/i+1);
Chris Lattnerd4275422007-07-22 07:28:00 +0000729 }
Mike Stump11289f42009-09-09 15:08:12 +0000730
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000731 SmallString<64> MappedName;
732
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000733 // Check each directory in sequence to see if it contains this file.
734 for (; i != SearchDirs.size(); ++i) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000735 bool InUserSpecifiedSystemFramework = false;
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000736 bool HasBeenMapped = false;
Mike Stump11289f42009-09-09 15:08:12 +0000737 const FileEntry *FE =
Douglas Gregor97eec242011-09-15 22:00:41 +0000738 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000739 SuggestedModule, InUserSpecifiedSystemFramework,
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000740 HasBeenMapped, MappedName);
741 if (HasBeenMapped) {
742 CacheLookup.MappedName =
743 copyString(Filename, LookupFileCache.getAllocator());
744 }
Chris Lattner712e3872007-12-17 08:13:48 +0000745 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000746
Chris Lattner712e3872007-12-17 08:13:48 +0000747 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattner712e3872007-12-17 08:13:48 +0000749 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor9f93e382011-07-28 04:45:53 +0000750 HeaderFileInfo &HFI = getFileInfo(FE);
751 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000752
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000753 // If the directory characteristic is User but this framework was
754 // user-specified to be treated as a system framework, promote the
755 // characteristic.
756 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
757 HFI.DirInfo = SrcMgr::C_System;
758
Richard Smith8acadcb2012-06-13 20:27:03 +0000759 // If the filename matches a known system header prefix, override
760 // whether the file is a system header.
Richard Trieu871f5f32012-06-13 20:52:36 +0000761 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
762 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
763 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
Richard Smith8acadcb2012-06-13 20:27:03 +0000764 : SrcMgr::C_User;
765 break;
766 }
767 }
768
Douglas Gregor9f93e382011-07-28 04:45:53 +0000769 // If this file is found in a header map and uses the framework style of
770 // includes, then this header is part of a framework we're building.
771 if (CurDir->isIndexHeaderMap()) {
772 size_t SlashPos = Filename.find('/');
773 if (SlashPos != StringRef::npos) {
774 HFI.IndexHeaderMapHeader = 1;
775 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
776 SlashPos));
777 }
778 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000779
Richard Smith8c71eba2014-03-05 20:51:45 +0000780 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
781 if (SuggestedModule)
782 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000783 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000784 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000785
Chris Lattner712e3872007-12-17 08:13:48 +0000786 // Remember this location for the next lookup we do.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000787 CacheLookup.HitIdx = i;
Chris Lattner712e3872007-12-17 08:13:48 +0000788 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000789 }
Mike Stump11289f42009-09-09 15:08:12 +0000790
Douglas Gregord8575e12011-07-30 06:28:34 +0000791 // If we are including a file with a quoted include "foo.h" from inside
792 // a header in a framework that is currently being built, and we couldn't
793 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
794 // "Foo" is the name of the framework in which the including header was found.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000795 if (!Includers.empty() && Includers.front().first && !isAngled &&
Will Wilson0fafd342013-12-27 19:46:16 +0000796 Filename.find('/') == StringRef::npos) {
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000797 HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
Douglas Gregord8575e12011-07-30 06:28:34 +0000798 if (IncludingHFI.IndexHeaderMapHeader) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000799 SmallString<128> ScratchFilename;
Douglas Gregord8575e12011-07-30 06:28:34 +0000800 ScratchFilename += IncludingHFI.Framework;
801 ScratchFilename += '/';
802 ScratchFilename += Filename;
Will Wilson0fafd342013-12-27 19:46:16 +0000803
Reid Klecknera97d4c02014-02-18 23:49:24 +0000804 const FileEntry *FE = LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000805 ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, CurDir,
806 Includers.front(), SearchPath, RelativePath, SuggestedModule);
Reid Klecknera97d4c02014-02-18 23:49:24 +0000807
Richard Smith8c71eba2014-03-05 20:51:45 +0000808 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
809 if (SuggestedModule)
810 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000811 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000812 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000813
David Blaikie3c8c46e2014-11-19 05:48:40 +0000814 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
David Blaikie13156b62014-11-19 03:06:06 +0000815 CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
Richard Smith8c71eba2014-03-05 20:51:45 +0000816 // FIXME: SuggestedModule.
Reid Klecknera97d4c02014-02-18 23:49:24 +0000817 return FE;
Douglas Gregord8575e12011-07-30 06:28:34 +0000818 }
819 }
820
Craig Topperd2d442c2014-05-17 23:10:59 +0000821 if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
Richard Smith8c71eba2014-03-05 20:51:45 +0000822 if (SuggestedModule)
823 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000824 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000825 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000826
Chris Lattnerd4275422007-07-22 07:28:00 +0000827 // Otherwise, didn't find it. Remember we didn't find this.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000828 CacheLookup.HitIdx = SearchDirs.size();
Craig Topperd2d442c2014-05-17 23:10:59 +0000829 return nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000830}
831
Chris Lattner63dd32b2006-10-20 04:42:40 +0000832/// LookupSubframeworkHeader - Look up a subframework for the specified
James Dennettc07ab2c2012-06-20 00:56:32 +0000833/// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
Chris Lattner63dd32b2006-10-20 04:42:40 +0000834/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
835/// is a subframework within Carbon.framework. If so, return the FileEntry
836/// for the designated file, otherwise return null.
837const FileEntry *HeaderSearch::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000838LookupSubframeworkHeader(StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000839 const FileEntry *ContextFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000840 SmallVectorImpl<char> *SearchPath,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000841 SmallVectorImpl<char> *RelativePath,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000842 ModuleMap::KnownHeader *SuggestedModule) {
Chris Lattner12261882008-02-01 05:34:02 +0000843 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000844
Chris Lattner63dd32b2006-10-20 04:42:40 +0000845 // Framework names must have a '/' in the filename. Find it.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000846 // FIXME: Should we permit '\' on Windows?
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000847 size_t SlashPos = Filename.find('/');
Craig Topperd2d442c2014-05-17 23:10:59 +0000848 if (SlashPos == StringRef::npos) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000849
Chris Lattner63dd32b2006-10-20 04:42:40 +0000850 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000851 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattner63dd32b2006-10-20 04:42:40 +0000853 // If the context info wasn't a framework, couldn't be a subframework.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000854 const unsigned DotFrameworkLen = 10;
855 const char *FrameworkPos = strstr(ContextName, ".framework");
Craig Topperd2d442c2014-05-17 23:10:59 +0000856 if (FrameworkPos == nullptr ||
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000857 (FrameworkPos[DotFrameworkLen] != '/' &&
858 FrameworkPos[DotFrameworkLen] != '\\'))
Craig Topperd2d442c2014-05-17 23:10:59 +0000859 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000860
Daniel Dunbar17138612012-04-05 17:09:40 +0000861 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000862
Chris Lattner63dd32b2006-10-20 04:42:40 +0000863 // Append Frameworks/HIToolbox.framework/
864 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000865 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000866 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000867
David Blaikie13156b62014-11-19 03:06:06 +0000868 auto &CacheLookup =
869 *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
870 FrameworkCacheEntry())).first;
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattner5ed76da2006-10-22 07:24:13 +0000872 // Some other location?
David Blaikie13156b62014-11-19 03:06:06 +0000873 if (CacheLookup.second.Directory &&
874 CacheLookup.first().size() == FrameworkName.size() &&
875 memcmp(CacheLookup.first().data(), &FrameworkName[0],
876 CacheLookup.first().size()) != 0)
Craig Topperd2d442c2014-05-17 23:10:59 +0000877 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000878
Chris Lattner5ed76da2006-10-22 07:24:13 +0000879 // Cache subframework.
David Blaikie13156b62014-11-19 03:06:06 +0000880 if (!CacheLookup.second.Directory) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000881 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000882
Chris Lattner5ed76da2006-10-22 07:24:13 +0000883 // If the framework dir doesn't exist, we fail.
Yaron Keren92e1b622015-03-18 10:17:07 +0000884 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
Craig Topperd2d442c2014-05-17 23:10:59 +0000885 if (!Dir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000886
Chris Lattner5ed76da2006-10-22 07:24:13 +0000887 // Otherwise, if it does, remember that this is the right direntry for this
888 // framework.
David Blaikie13156b62014-11-19 03:06:06 +0000889 CacheLookup.second.Directory = Dir;
Chris Lattner5ed76da2006-10-22 07:24:13 +0000890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Craig Topperd2d442c2014-05-17 23:10:59 +0000892 const FileEntry *FE = nullptr;
Chris Lattner577377e2006-10-20 04:55:45 +0000893
Craig Topperd2d442c2014-05-17 23:10:59 +0000894 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000895 RelativePath->clear();
896 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
897 }
898
Chris Lattner63dd32b2006-10-20 04:42:40 +0000899 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000900 SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000901 HeadersFilename += "Headers/";
Craig Topperd2d442c2014-05-17 23:10:59 +0000902 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000903 SearchPath->clear();
904 // Without trailing '/'.
905 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
906 }
907
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000908 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000909 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
Mike Stump11289f42009-09-09 15:08:12 +0000910
Chris Lattner63dd32b2006-10-20 04:42:40 +0000911 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000912 HeadersFilename = FrameworkName;
913 HeadersFilename += "PrivateHeaders/";
Craig Topperd2d442c2014-05-17 23:10:59 +0000914 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000915 SearchPath->clear();
916 // Without trailing '/'.
917 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
918 }
919
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000920 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000921 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true)))
Craig Topperd2d442c2014-05-17 23:10:59 +0000922 return nullptr;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000923 }
Mike Stump11289f42009-09-09 15:08:12 +0000924
Chris Lattner577377e2006-10-20 04:55:45 +0000925 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000926 //
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000927 // Note that the temporary 'DirInfo' is required here, as either call to
928 // getFileInfo could resize the vector and we don't want to rely on order
929 // of evaluation.
930 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
931 getFileInfo(FE).DirInfo = DirInfo;
Douglas Gregorf5f94522013-02-08 00:10:48 +0000932
933 // If we're supposed to suggest a module, look for one now.
934 if (SuggestedModule) {
935 // Find the top-level framework based on this framework.
936 FrameworkName.pop_back(); // remove the trailing '/'
937 SmallVector<std::string, 4> SubmodulePath;
938 const DirectoryEntry *TopFrameworkDir
939 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
940
941 // Determine the name of the top-level framework.
942 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
943
944 // Load this framework module. If that succeeds, find the suggested module
945 // for this header, if any.
946 bool IsSystem = false;
947 if (loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
948 *SuggestedModule = findModuleForHeader(FE);
949 }
950 }
951
Chris Lattner577377e2006-10-20 04:55:45 +0000952 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000953}
954
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000955//===----------------------------------------------------------------------===//
956// File Info Management.
957//===----------------------------------------------------------------------===//
958
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000959/// \brief Merge the header file info provided by \p OtherHFI into the current
960/// header file info (\p HFI)
961static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
962 const HeaderFileInfo &OtherHFI) {
963 HFI.isImport |= OtherHFI.isImport;
964 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000965 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000966 HFI.NumIncludes += OtherHFI.NumIncludes;
967
968 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
969 HFI.ControllingMacro = OtherHFI.ControllingMacro;
970 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
971 }
972
973 if (OtherHFI.External) {
974 HFI.DirInfo = OtherHFI.DirInfo;
975 HFI.External = OtherHFI.External;
976 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
977 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000978
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000979 if (HFI.Framework.empty())
980 HFI.Framework = OtherHFI.Framework;
981
982 HFI.Resolved = true;
983}
984
Steve Naroff3fa455a2009-04-24 20:03:17 +0000985/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000986/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000987HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000988 if (FE->getUID() >= FileInfo.size())
989 FileInfo.resize(FE->getUID()+1);
Douglas Gregor09b69892011-02-10 17:09:37 +0000990
991 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000992 if (ExternalSource && !HFI.Resolved)
993 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Ben Langmuird285c502014-03-13 16:46:36 +0000994 HFI.IsValid = 1;
Douglas Gregor09b69892011-02-10 17:09:37 +0000995 return HFI;
Mike Stump11289f42009-09-09 15:08:12 +0000996}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000997
Ben Langmuird285c502014-03-13 16:46:36 +0000998bool HeaderSearch::tryGetFileInfo(const FileEntry *FE, HeaderFileInfo &Result) const {
999 if (FE->getUID() >= FileInfo.size())
1000 return false;
1001 const HeaderFileInfo &HFI = FileInfo[FE->getUID()];
1002 if (HFI.IsValid) {
1003 Result = HFI;
1004 return true;
1005 }
1006 return false;
1007}
1008
Douglas Gregor37aa4932011-05-04 00:14:37 +00001009bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1010 // Check if we've ever seen this file as a header.
1011 if (File->getUID() >= FileInfo.size())
1012 return false;
1013
1014 // Resolve header file info from the external source, if needed.
1015 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor5d1bee22011-09-17 05:35:18 +00001016 if (ExternalSource && !HFI.Resolved)
1017 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregor37aa4932011-05-04 00:14:37 +00001018
Argyrios Kyrtzidis0d355df2012-12-10 20:08:37 +00001019 return HFI.isPragmaOnce || HFI.isImport ||
1020 HFI.ControllingMacro || HFI.ControllingMacroID;
Douglas Gregor37aa4932011-05-04 00:14:37 +00001021}
1022
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001023void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001024 ModuleMap::ModuleHeaderRole Role,
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001025 bool isCompilingModuleHeader) {
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001026 if (FE->getUID() >= FileInfo.size())
1027 FileInfo.resize(FE->getUID()+1);
1028
1029 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
1030 HFI.isModuleHeader = true;
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001031 HFI.isCompilingModuleHeader = isCompilingModuleHeader;
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001032 HFI.setHeaderRole(Role);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001033}
1034
Richard Smith20e883e2015-04-29 23:20:19 +00001035bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1036 const FileEntry *File,
Richard Smith035f6dc2015-07-01 01:51:38 +00001037 bool isImport, Module *M) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001038 ++NumIncluded; // Count # of attempted #includes.
1039
1040 // Get information about this file.
Steve Naroff3fa455a2009-04-24 20:03:17 +00001041 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump11289f42009-09-09 15:08:12 +00001042
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001043 // If this is a #import directive, check that we have not already imported
1044 // this header.
1045 if (isImport) {
1046 // If this has already been imported, don't import it again.
1047 FileInfo.isImport = true;
Mike Stump11289f42009-09-09 15:08:12 +00001048
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001049 // Has this already been #import'ed or #include'd?
1050 if (FileInfo.NumIncludes) return false;
1051 } else {
1052 // Otherwise, if this is a #include of a file that was previously #import'd
1053 // or if this is the second #include of a #pragma once file, ignore it.
1054 if (FileInfo.isImport)
1055 return false;
1056 }
Mike Stump11289f42009-09-09 15:08:12 +00001057
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001058 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1059 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump11289f42009-09-09 15:08:12 +00001060 if (const IdentifierInfo *ControllingMacro
Douglas Gregor99734e72009-04-25 23:30:02 +00001061 = FileInfo.getControllingMacro(ExternalLookup))
Richard Smith035f6dc2015-07-01 01:51:38 +00001062 // If the include file is part of a module, and we already know what its
1063 // controlling macro is, then we've already parsed it and can safely just
1064 // make it visible. This saves us needing to switch into the visibility
1065 // state of the module just to check whether the macro is defined within it.
1066 if (M || PP.isMacroDefined(ControllingMacro)) {
Douglas Gregor99734e72009-04-25 23:30:02 +00001067 ++NumMultiIncludeFileOptzn;
1068 return false;
1069 }
Mike Stump11289f42009-09-09 15:08:12 +00001070
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001071 // Increment the number of times this file has been included.
1072 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +00001073
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001074 return true;
1075}
1076
Ted Kremenekfbcce6f2011-07-26 23:46:11 +00001077size_t HeaderSearch::getTotalMemory() const {
1078 return SearchDirs.capacity()
Ted Kremenekae63d102011-07-27 18:41:18 +00001079 + llvm::capacity_in_bytes(FileInfo)
1080 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekfbcce6f2011-07-26 23:46:11 +00001081 + LookupFileCache.getAllocator().getTotalMemory()
1082 + FrameworkMap.getAllocator().getTotalMemory();
1083}
Douglas Gregor9f93e382011-07-28 04:45:53 +00001084
1085StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
David Blaikie13156b62014-11-19 03:06:06 +00001086 return FrameworkNames.insert(Framework).first->first();
Douglas Gregor9f93e382011-07-28 04:45:53 +00001087}
Douglas Gregor718292f2011-11-11 19:10:28 +00001088
1089bool HeaderSearch::hasModuleMap(StringRef FileName,
Douglas Gregor963c5532013-06-21 16:28:10 +00001090 const DirectoryEntry *Root,
1091 bool IsSystem) {
Richard Smith47972af2015-06-16 00:08:24 +00001092 if (!HSOpts->ImplicitModuleMaps)
Argyrios Kyrtzidis9955dbc2013-12-12 16:08:33 +00001093 return false;
1094
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001095 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
Douglas Gregor718292f2011-11-11 19:10:28 +00001096
1097 StringRef DirName = FileName;
1098 do {
1099 // Get the parent directory name.
1100 DirName = llvm::sys::path::parent_path(DirName);
1101 if (DirName.empty())
1102 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001103
Douglas Gregor718292f2011-11-11 19:10:28 +00001104 // Determine whether this directory exists.
1105 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
1106 if (!Dir)
1107 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001108
Ben Langmuir984e1df2014-03-19 20:23:34 +00001109 // Try to load the module map file in this directory.
Richard Smith3c1a41a2014-12-02 00:08:08 +00001110 switch (loadModuleMapFile(Dir, IsSystem,
1111 llvm::sys::path::extension(Dir->getName()) ==
1112 ".framework")) {
Douglas Gregor80b69042011-11-12 00:22:19 +00001113 case LMM_NewlyLoaded:
1114 case LMM_AlreadyLoaded:
Daniel Jasperca9f7382013-09-24 09:27:13 +00001115 // Success. All of the directories we stepped through inherit this module
1116 // map file.
1117 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1118 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1119 return true;
Daniel Jasper97da9172013-10-22 08:09:47 +00001120
1121 case LMM_NoDirectory:
1122 case LMM_InvalidModuleMap:
1123 break;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001124 }
1125
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001126 // If we hit the top of our search, we're done.
1127 if (Dir == Root)
1128 return false;
1129
Douglas Gregor718292f2011-11-11 19:10:28 +00001130 // Keep track of all of the directories we checked, so we can mark them as
1131 // having module maps if we eventually do find a module map.
1132 FixUpDirectories.push_back(Dir);
1133 } while (true);
Douglas Gregor718292f2011-11-11 19:10:28 +00001134}
1135
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001136ModuleMap::KnownHeader
1137HeaderSearch::findModuleForHeader(const FileEntry *File) const {
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001138 if (ExternalSource) {
1139 // Make sure the external source has handled header info about this file,
1140 // which includes whether the file is part of a module.
1141 (void)getFileInfo(File);
1142 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001143 return ModMap.findModuleForHeader(File);
Douglas Gregor718292f2011-11-11 19:10:28 +00001144}
1145
Richard Smith9acb99e32014-12-10 03:09:48 +00001146static const FileEntry *getPrivateModuleMap(const FileEntry *File,
Ben Langmuir984e1df2014-03-19 20:23:34 +00001147 FileManager &FileMgr) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001148 StringRef Filename = llvm::sys::path::filename(File->getName());
1149 SmallString<128> PrivateFilename(File->getDir()->getName());
Ben Langmuir984e1df2014-03-19 20:23:34 +00001150 if (Filename == "module.map")
Douglas Gregor80306772011-12-07 21:25:07 +00001151 llvm::sys::path::append(PrivateFilename, "module_private.map");
Ben Langmuir984e1df2014-03-19 20:23:34 +00001152 else if (Filename == "module.modulemap")
1153 llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1154 else
1155 return nullptr;
1156 return FileMgr.getFile(PrivateFilename);
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001157}
1158
Ben Langmuir984e1df2014-03-19 20:23:34 +00001159bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001160 // Find the directory for the module. For frameworks, that may require going
1161 // up from the 'Modules' directory.
1162 const DirectoryEntry *Dir = nullptr;
1163 if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
1164 Dir = FileMgr.getDirectory(".");
1165 else {
1166 Dir = File->getDir();
1167 StringRef DirName(Dir->getName());
1168 if (llvm::sys::path::filename(DirName) == "Modules") {
1169 DirName = llvm::sys::path::parent_path(DirName);
1170 if (DirName.endswith(".framework"))
1171 Dir = FileMgr.getDirectory(DirName);
1172 // FIXME: This assert can fail if there's a race between the above check
1173 // and the removal of the directory.
1174 assert(Dir && "parent must exist");
1175 }
1176 }
1177
1178 switch (loadModuleMapFileImpl(File, IsSystem, Dir)) {
Ben Langmuir984e1df2014-03-19 20:23:34 +00001179 case LMM_AlreadyLoaded:
1180 case LMM_NewlyLoaded:
1181 return false;
1182 case LMM_NoDirectory:
1183 case LMM_InvalidModuleMap:
1184 return true;
1185 }
Aaron Ballmand8de5b62014-03-20 14:22:33 +00001186 llvm_unreachable("Unknown load module map result");
Ben Langmuir984e1df2014-03-19 20:23:34 +00001187}
1188
1189HeaderSearch::LoadModuleMapResult
Richard Smith9acb99e32014-12-10 03:09:48 +00001190HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1191 const DirectoryEntry *Dir) {
Ben Langmuir984e1df2014-03-19 20:23:34 +00001192 assert(File && "expected FileEntry");
1193
Richard Smith9887d792014-10-17 01:42:53 +00001194 // Check whether we've already loaded this module map, and mark it as being
1195 // loaded in case we recursively try to load it from itself.
1196 auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1197 if (!AddResult.second)
1198 return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001199
Richard Smith9acb99e32014-12-10 03:09:48 +00001200 if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) {
Richard Smith9887d792014-10-17 01:42:53 +00001201 LoadedModuleMaps[File] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001202 return LMM_InvalidModuleMap;
1203 }
1204
1205 // Try to load a corresponding private module map.
Richard Smith9acb99e32014-12-10 03:09:48 +00001206 if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1207 if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
Richard Smith9887d792014-10-17 01:42:53 +00001208 LoadedModuleMaps[File] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001209 return LMM_InvalidModuleMap;
1210 }
1211 }
1212
1213 // This directory has a module map.
Ben Langmuir984e1df2014-03-19 20:23:34 +00001214 return LMM_NewlyLoaded;
1215}
1216
1217const FileEntry *
1218HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
Richard Smith47972af2015-06-16 00:08:24 +00001219 if (!HSOpts->ImplicitModuleMaps)
Daniel Jasper21a0f552014-11-25 09:45:48 +00001220 return nullptr;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001221 // For frameworks, the preferred spelling is Modules/module.modulemap, but
1222 // module.map at the framework root is also accepted.
1223 SmallString<128> ModuleMapFileName(Dir->getName());
1224 if (IsFramework)
1225 llvm::sys::path::append(ModuleMapFileName, "Modules");
1226 llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1227 if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
1228 return F;
1229
1230 // Continue to allow module.map
1231 ModuleMapFileName = Dir->getName();
1232 llvm::sys::path::append(ModuleMapFileName, "module.map");
1233 return FileMgr.getFile(ModuleMapFileName);
1234}
1235
1236Module *HeaderSearch::loadFrameworkModule(StringRef Name,
Douglas Gregor279a6c32012-01-29 17:08:11 +00001237 const DirectoryEntry *Dir,
1238 bool IsSystem) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001239 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor56c64012011-11-17 01:41:17 +00001240 return Module;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001241
Douglas Gregor56c64012011-11-17 01:41:17 +00001242 // Try to load a module map file.
Ben Langmuir984e1df2014-03-19 20:23:34 +00001243 switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
Douglas Gregor56c64012011-11-17 01:41:17 +00001244 case LMM_InvalidModuleMap:
Ben Langmuira5254002015-07-02 13:19:48 +00001245 // Try to infer a module map from the framework directory.
1246 if (HSOpts->ImplicitModuleMaps)
1247 ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
Douglas Gregor56c64012011-11-17 01:41:17 +00001248 break;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001249
Douglas Gregor56c64012011-11-17 01:41:17 +00001250 case LMM_AlreadyLoaded:
1251 case LMM_NoDirectory:
Craig Topperd2d442c2014-05-17 23:10:59 +00001252 return nullptr;
1253
Douglas Gregor56c64012011-11-17 01:41:17 +00001254 case LMM_NewlyLoaded:
Ben Langmuira5254002015-07-02 13:19:48 +00001255 break;
Douglas Gregor56c64012011-11-17 01:41:17 +00001256 }
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001257
Ben Langmuira5254002015-07-02 13:19:48 +00001258 return ModMap.findModule(Name);
Douglas Gregor56c64012011-11-17 01:41:17 +00001259}
1260
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001261
Douglas Gregor80b69042011-11-12 00:22:19 +00001262HeaderSearch::LoadModuleMapResult
Ben Langmuir984e1df2014-03-19 20:23:34 +00001263HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1264 bool IsFramework) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001265 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
Ben Langmuir984e1df2014-03-19 20:23:34 +00001266 return loadModuleMapFile(Dir, IsSystem, IsFramework);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001267
Douglas Gregor80b69042011-11-12 00:22:19 +00001268 return LMM_NoDirectory;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001269}
1270
Douglas Gregor80b69042011-11-12 00:22:19 +00001271HeaderSearch::LoadModuleMapResult
Ben Langmuir984e1df2014-03-19 20:23:34 +00001272HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1273 bool IsFramework) {
1274 auto KnownDir = DirectoryHasModuleMap.find(Dir);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001275 if (KnownDir != DirectoryHasModuleMap.end())
Richard Smith9887d792014-10-17 01:42:53 +00001276 return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregore7ab3662011-12-07 02:23:45 +00001277
Ben Langmuir984e1df2014-03-19 20:23:34 +00001278 if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001279 LoadModuleMapResult Result =
1280 loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
Ben Langmuir984e1df2014-03-19 20:23:34 +00001281 // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1282 // E.g. Foo.framework/Modules/module.modulemap
1283 // ^Dir ^ModuleMapFile
1284 if (Result == LMM_NewlyLoaded)
1285 DirectoryHasModuleMap[Dir] = true;
Richard Smith9887d792014-10-17 01:42:53 +00001286 else if (Result == LMM_InvalidModuleMap)
1287 DirectoryHasModuleMap[Dir] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001288 return Result;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001289 }
Douglas Gregor80b69042011-11-12 00:22:19 +00001290 return LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001291}
Douglas Gregor718292f2011-11-11 19:10:28 +00001292
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001293void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
Douglas Gregor07f43572012-01-29 18:15:03 +00001294 Modules.clear();
Daniel Jasper21a0f552014-11-25 09:45:48 +00001295
Richard Smith47972af2015-06-16 00:08:24 +00001296 if (HSOpts->ImplicitModuleMaps) {
Daniel Jasper21a0f552014-11-25 09:45:48 +00001297 // Load module maps for each of the header search directories.
1298 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1299 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1300 if (SearchDirs[Idx].isFramework()) {
1301 std::error_code EC;
1302 SmallString<128> DirNative;
1303 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1304 DirNative);
1305
1306 // Search each of the ".framework" directories to load them as modules.
Yaron Keren92e1b622015-03-18 10:17:07 +00001307 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001308 Dir != DirEnd && !EC; Dir.increment(EC)) {
1309 if (llvm::sys::path::extension(Dir->path()) != ".framework")
1310 continue;
1311
1312 const DirectoryEntry *FrameworkDir =
1313 FileMgr.getDirectory(Dir->path());
1314 if (!FrameworkDir)
1315 continue;
1316
1317 // Load this framework module.
1318 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1319 IsSystem);
1320 }
1321 continue;
Douglas Gregor07f43572012-01-29 18:15:03 +00001322 }
Daniel Jasper21a0f552014-11-25 09:45:48 +00001323
1324 // FIXME: Deal with header maps.
1325 if (SearchDirs[Idx].isHeaderMap())
1326 continue;
1327
1328 // Try to load a module map file for the search directory.
1329 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1330 /*IsFramework*/ false);
1331
1332 // Try to load module map files for immediate subdirectories of this
1333 // search directory.
1334 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
Douglas Gregor07f43572012-01-29 18:15:03 +00001335 }
Douglas Gregor07f43572012-01-29 18:15:03 +00001336 }
Daniel Jasper21a0f552014-11-25 09:45:48 +00001337
Douglas Gregor07f43572012-01-29 18:15:03 +00001338 // Populate the list of modules.
1339 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1340 MEnd = ModMap.module_end();
1341 M != MEnd; ++M) {
1342 Modules.push_back(M->getValue());
1343 }
1344}
Douglas Gregor0339a642013-03-21 01:08:50 +00001345
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001346void HeaderSearch::loadTopLevelSystemModules() {
Richard Smith47972af2015-06-16 00:08:24 +00001347 if (!HSOpts->ImplicitModuleMaps)
Daniel Jasper21a0f552014-11-25 09:45:48 +00001348 return;
1349
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001350 // Load module maps for each of the header search directories.
1351 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregor299787f2013-11-01 23:08:38 +00001352 // We only care about normal header directories.
1353 if (!SearchDirs[Idx].isNormalDir()) {
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001354 continue;
1355 }
1356
1357 // Try to load a module map file for the search directory.
Douglas Gregor963c5532013-06-21 16:28:10 +00001358 loadModuleMapFile(SearchDirs[Idx].getDir(),
Ben Langmuir984e1df2014-03-19 20:23:34 +00001359 SearchDirs[Idx].isSystemHeaderDirectory(),
1360 SearchDirs[Idx].isFramework());
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001361 }
1362}
1363
Douglas Gregor0339a642013-03-21 01:08:50 +00001364void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
Richard Smith47972af2015-06-16 00:08:24 +00001365 assert(HSOpts->ImplicitModuleMaps &&
Daniel Jasper21a0f552014-11-25 09:45:48 +00001366 "Should not be loading subdirectory module maps");
1367
Douglas Gregor0339a642013-03-21 01:08:50 +00001368 if (SearchDir.haveSearchedAllModuleMaps())
1369 return;
Rafael Espindolac0809172014-06-12 14:02:15 +00001370
1371 std::error_code EC;
Douglas Gregor0339a642013-03-21 01:08:50 +00001372 SmallString<128> DirNative;
1373 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
Yaron Keren92e1b622015-03-18 10:17:07 +00001374 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
Douglas Gregor0339a642013-03-21 01:08:50 +00001375 Dir != DirEnd && !EC; Dir.increment(EC)) {
Ben Langmuir1f6a32b2015-02-24 04:58:15 +00001376 bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1377 if (IsFramework == SearchDir.isFramework())
1378 loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1379 SearchDir.isFramework());
Douglas Gregor0339a642013-03-21 01:08:50 +00001380 }
1381
1382 SearchDir.setSearchedAllModuleMaps(true);
1383}