blob: 469d782deb316a78998281fea1089923f95ce698 [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"
Adrian Prantlfb2398d2015-07-17 01:19:54 +000017#include "clang/Frontend/PCHContainerOperations.h"
Richard Smith2aedca32015-07-01 02:29:35 +000018#include "clang/Lex/ExternalPreprocessorSource.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Lex/HeaderMap.h"
20#include "clang/Lex/HeaderSearchOptions.h"
Will Wilson0fafd342013-12-27 19:46:16 +000021#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000022#include "clang/Lex/Lexer.h"
Richard Smith20e883e2015-04-29 23:20:19 +000023#include "clang/Lex/Preprocessor.h"
Ben Langmuirbeee15e2014-04-14 18:00:01 +000024#include "llvm/ADT/APInt.h"
25#include "llvm/ADT/Hashing.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +000026#include "llvm/ADT/SmallString.h"
Ted Kremenekae63d102011-07-27 18:41:18 +000027#include "llvm/Support/Capacity.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000028#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/Path.h"
Daniel Jasperba7f2f72013-09-24 09:14:14 +000030#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000031#include <cstdio>
Douglas Gregor01c7cfa2013-01-22 23:49:45 +000032#if defined(LLVM_ON_UNIX)
Dmitri Gribenkoeadae012013-01-26 16:29:36 +000033#include <limits.h>
Douglas Gregor01c7cfa2013-01-22 23:49:45 +000034#endif
Chris Lattner59a9ebd2006-10-18 05:34:33 +000035using namespace clang;
36
Douglas Gregor99734e72009-04-25 23:30:02 +000037const IdentifierInfo *
Richard Smith2aedca32015-07-01 02:29:35 +000038HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
39 if (ControllingMacro) {
40 if (ControllingMacro->isOutOfDate())
41 External->updateOutOfDateIdentifier(
42 *const_cast<IdentifierInfo *>(ControllingMacro));
Douglas Gregor99734e72009-04-25 23:30:02 +000043 return ControllingMacro;
Richard Smith2aedca32015-07-01 02:29:35 +000044 }
Douglas Gregor99734e72009-04-25 23:30:02 +000045
46 if (!ControllingMacroID || !External)
Craig Topperd2d442c2014-05-17 23:10:59 +000047 return nullptr;
Douglas Gregor99734e72009-04-25 23:30:02 +000048
49 ControllingMacro = External->GetIdentifier(ControllingMacroID);
50 return ControllingMacro;
51}
52
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000053ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
Douglas Gregor09b69892011-02-10 17:09:37 +000054
Dmitri Gribenkof8579502013-01-12 19:30:44 +000055HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000056 SourceManager &SourceMgr, DiagnosticsEngine &Diags,
Will Wilson0fafd342013-12-27 19:46:16 +000057 const LangOptions &LangOpts,
Douglas Gregor89929282012-01-30 06:01:29 +000058 const TargetInfo *Target)
Will Wilsonba2f1462013-12-27 20:02:27 +000059 : HSOpts(HSOpts), Diags(Diags), FileMgr(SourceMgr.getFileManager()),
Daniel Jasper21a0f552014-11-25 09:45:48 +000060 FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this),
61 LangOpts(LangOpts) {
Nico Weber3b1d1212011-05-24 04:31:14 +000062 AngledDirIdx = 0;
Chris Lattner641a0be2006-10-20 06:23:14 +000063 SystemDirIdx = 0;
64 NoCurDirSearch = false;
Mike Stump11289f42009-09-09 15:08:12 +000065
Craig Topperd2d442c2014-05-17 23:10:59 +000066 ExternalLookup = nullptr;
67 ExternalSource = nullptr;
Chris Lattner641a0be2006-10-20 06:23:14 +000068 NumIncluded = 0;
69 NumMultiIncludeFileOptzn = 0;
70 NumFrameworkLookups = NumSubFrameworkLookups = 0;
71}
72
Chris Lattnerc4ba38e2007-12-17 06:36:45 +000073HeaderSearch::~HeaderSearch() {
74 // Delete headermaps.
75 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
76 delete HeaderMaps[i].second;
77}
Mike Stump11289f42009-09-09 15:08:12 +000078
Chris Lattner59a9ebd2006-10-18 05:34:33 +000079void HeaderSearch::PrintStats() {
Chris Lattner23b7eb62007-06-15 23:05:46 +000080 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
81 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
Chris Lattner59a9ebd2006-10-18 05:34:33 +000082 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
83 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
84 NumOnceOnlyFiles += FileInfo[i].isImport;
85 if (MaxNumIncludes < FileInfo[i].NumIncludes)
86 MaxNumIncludes = FileInfo[i].NumIncludes;
87 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
88 }
Chris Lattner23b7eb62007-06-15 23:05:46 +000089 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
90 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
91 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump11289f42009-09-09 15:08:12 +000092
Chris Lattner23b7eb62007-06-15 23:05:46 +000093 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
94 fprintf(stderr, " %d #includes skipped due to"
95 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump11289f42009-09-09 15:08:12 +000096
Chris Lattner23b7eb62007-06-15 23:05:46 +000097 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
98 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
Chris Lattner59a9ebd2006-10-18 05:34:33 +000099}
100
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000101/// CreateHeaderMap - This method returns a HeaderMap for the specified
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000102/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
Chris Lattner4ffe46c2007-12-17 18:34:53 +0000103const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000104 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerf62f7582007-12-17 07:52:39 +0000105 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000106 if (!HeaderMaps.empty()) {
107 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerf62f7582007-12-17 07:52:39 +0000108 // Pointer equality comparison of FileEntries works because they are
109 // already uniqued by inode.
Mike Stump11289f42009-09-09 15:08:12 +0000110 if (HeaderMaps[i].first == FE)
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000111 return HeaderMaps[i].second;
112 }
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattner5159f612010-11-23 08:35:12 +0000114 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000115 HeaderMaps.push_back(std::make_pair(FE, HM));
116 return HM;
117 }
Mike Stump11289f42009-09-09 15:08:12 +0000118
Craig Topperd2d442c2014-05-17 23:10:59 +0000119 return nullptr;
Chris Lattnerc4ba38e2007-12-17 06:36:45 +0000120}
121
Douglas Gregor279a6c32012-01-29 17:08:11 +0000122std::string HeaderSearch::getModuleFileName(Module *Module) {
Ben Langmuir9d6448b2014-08-09 00:57:23 +0000123 const FileEntry *ModuleMap =
124 getModuleMap().getModuleMapFileForUniquing(Module);
Ben Langmuird89dc562015-02-19 20:23:22 +0000125 return getModuleFileName(Module->Name, ModuleMap->getName());
Douglas Gregor279a6c32012-01-29 17:08:11 +0000126}
127
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000128std::string HeaderSearch::getModuleFileName(StringRef ModuleName,
Ben Langmuird89dc562015-02-19 20:23:22 +0000129 StringRef ModuleMapPath) {
Richard Smithd520a252015-07-21 18:07:47 +0000130 // If we don't have a module cache path or aren't supposed to use one, we
131 // can't do anything.
Richard Smith3938f0c2015-08-15 00:34:15 +0000132 if (getModuleCachePath().empty())
Douglas Gregor279a6c32012-01-29 17:08:11 +0000133 return std::string();
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000134
Richard Smith3938f0c2015-08-15 00:34:15 +0000135 SmallString<256> Result(getModuleCachePath());
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000136 llvm::sys::fs::make_absolute(Result);
137
138 if (HSOpts->DisableModuleHash) {
139 llvm::sys::path::append(Result, ModuleName + ".pcm");
140 } else {
141 // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
Richard Smith54cc3c22014-12-11 20:50:24 +0000142 // ideally be globally unique to this particular module. Name collisions
143 // in the hash are safe (because any translation unit can only import one
144 // module with each name), but result in a loss of caching.
145 //
146 // To avoid false-negatives, we form as canonical a path as we can, and map
147 // to lower-case in case we're on a case-insensitive file system.
148 auto *Dir =
149 FileMgr.getDirectory(llvm::sys::path::parent_path(ModuleMapPath));
150 if (!Dir)
151 return std::string();
152 auto DirName = FileMgr.getCanonicalName(Dir);
153 auto FileName = llvm::sys::path::filename(ModuleMapPath);
154
155 llvm::hash_code Hash =
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000156 llvm::hash_combine(DirName.lower(), FileName.lower(),
Adrian Prantl6b21ab22015-08-27 19:46:20 +0000157 HSOpts->ModuleFormat, HSOpts->UseDebugInfo);
Richard Smith54cc3c22014-12-11 20:50:24 +0000158
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000159 SmallString<128> HashStr;
Richard Smith54cc3c22014-12-11 20:50:24 +0000160 llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
Yaron Keren92e1b622015-03-18 10:17:07 +0000161 llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000162 }
Douglas Gregor279a6c32012-01-29 17:08:11 +0000163 return Result.str().str();
164}
165
166Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000167 // Look in the module map to determine if there is a module by this name.
Douglas Gregor279a6c32012-01-29 17:08:11 +0000168 Module *Module = ModMap.findModule(ModuleName);
Richard Smith47972af2015-06-16 00:08:24 +0000169 if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
Douglas Gregor279a6c32012-01-29 17:08:11 +0000170 return Module;
171
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000172 // Look through the various header search paths to load any available module
Douglas Gregor279a6c32012-01-29 17:08:11 +0000173 // maps, searching for a module map that describes this module.
174 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
175 if (SearchDirs[Idx].isFramework()) {
176 // Search for or infer a module map for a framework.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000177 SmallString<128> FrameworkDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000178 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
179 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
180 if (const DirectoryEntry *FrameworkDir
181 = FileMgr.getDirectory(FrameworkDirName)) {
182 bool IsSystem
183 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
184 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000185 if (Module)
186 break;
187 }
Douglas Gregor279a6c32012-01-29 17:08:11 +0000188 }
189
190 // FIXME: Figure out how header maps and module maps will work together.
191
192 // Only deal with normal search directories.
193 if (!SearchDirs[Idx].isNormalDir())
194 continue;
Douglas Gregor963c5532013-06-21 16:28:10 +0000195
196 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
Douglas Gregor279a6c32012-01-29 17:08:11 +0000197 // Search for a module map file in this directory.
Ben Langmuir984e1df2014-03-19 20:23:34 +0000198 if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
199 /*IsFramework*/false) == LMM_NewlyLoaded) {
Douglas Gregor279a6c32012-01-29 17:08:11 +0000200 // We just loaded a module map file; check whether the module is
201 // available now.
202 Module = ModMap.findModule(ModuleName);
203 if (Module)
204 break;
205 }
206
207 // Search for a module map in a subdirectory with the same name as the
208 // module.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000209 SmallString<128> NestedModuleMapDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000210 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
211 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
Ben Langmuir984e1df2014-03-19 20:23:34 +0000212 if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
213 /*IsFramework*/false) == LMM_NewlyLoaded){
Douglas Gregor279a6c32012-01-29 17:08:11 +0000214 // If we just loaded a module map file, look for the module again.
215 Module = ModMap.findModule(ModuleName);
216 if (Module)
217 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000218 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000219
220 // If we've already performed the exhaustive search for module maps in this
221 // search directory, don't do it again.
222 if (SearchDirs[Idx].haveSearchedAllModuleMaps())
223 continue;
224
225 // Load all module maps in the immediate subdirectories of this search
226 // directory.
227 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
228
229 // Look again for the module.
230 Module = ModMap.findModule(ModuleName);
231 if (Module)
232 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000233 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000234
Douglas Gregor279a6c32012-01-29 17:08:11 +0000235 return Module;
Douglas Gregor1e44e022011-09-12 20:41:59 +0000236}
237
Chris Lattnerf62f7582007-12-17 07:52:39 +0000238//===----------------------------------------------------------------------===//
239// File lookup within a DirectoryLookup scope
240//===----------------------------------------------------------------------===//
241
Chris Lattner8d720d02007-12-17 17:57:27 +0000242/// getName - Return the directory or filename corresponding to this lookup
243/// object.
244const char *DirectoryLookup::getName() const {
245 if (isNormalDir())
246 return getDir()->getName();
247 if (isFramework())
248 return getFrameworkDir()->getName();
249 assert(isHeaderMap() && "Unknown DirectoryLookup");
250 return getHeaderMap()->getFileName();
251}
252
Richard Smith3d5b48c2015-10-16 21:42:56 +0000253const FileEntry *HeaderSearch::getFileAndSuggestModule(
254 StringRef FileName, const DirectoryEntry *Dir, bool IsSystemHeaderDir,
255 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
Richard Smith8c71eba2014-03-05 20:51:45 +0000256 // If we have a module map that might map this header, load it and
257 // check whether we'll have a suggestion for a module.
Richard Smith3d5b48c2015-10-16 21:42:56 +0000258 const FileEntry *File = getFileMgr().getFile(FileName, /*OpenFile=*/true);
Richard Smith8c71eba2014-03-05 20:51:45 +0000259
Richard Smith3d5b48c2015-10-16 21:42:56 +0000260 // If there is a module that corresponds to this header, suggest it.
261 if (!findUsableModuleForHeader(File, Dir ? Dir : File->getDir(),
262 RequestingModule, SuggestedModule,
263 IsSystemHeaderDir))
264 return nullptr;
Richard Smith8c71eba2014-03-05 20:51:45 +0000265
Richard Smith3d5b48c2015-10-16 21:42:56 +0000266 return File;
Richard Smith8c71eba2014-03-05 20:51:45 +0000267}
Chris Lattner8d720d02007-12-17 17:57:27 +0000268
Chris Lattnerf62f7582007-12-17 07:52:39 +0000269/// LookupFile - Lookup the specified file in this search path, returning it
270/// if it exists or returning null if not.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000271const FileEntry *DirectoryLookup::LookupFile(
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000272 StringRef &Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000273 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000274 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000275 SmallVectorImpl<char> *RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000276 Module *RequestingModule,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000277 ModuleMap::KnownHeader *SuggestedModule,
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000278 bool &InUserSpecifiedSystemFramework,
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000279 bool &HasBeenMapped,
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000280 SmallVectorImpl<char> &MappedName) const {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000281 InUserSpecifiedSystemFramework = false;
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000282 HasBeenMapped = false;
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000283
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000284 SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000285 if (isNormalDir()) {
286 // Concatenate the requested file onto the directory.
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000287 TmpDir = getDir()->getName();
288 llvm::sys::path::append(TmpDir, Filename);
Craig Topperd2d442c2014-05-17 23:10:59 +0000289 if (SearchPath) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000290 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000291 SearchPath->clear();
292 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
293 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000294 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000295 RelativePath->clear();
296 RelativePath->append(Filename.begin(), Filename.end());
297 }
Richard Smith8c71eba2014-03-05 20:51:45 +0000298
Richard Smith3d5b48c2015-10-16 21:42:56 +0000299 return HS.getFileAndSuggestModule(TmpDir, getDir(),
300 isSystemHeaderDirectory(),
301 RequestingModule, SuggestedModule);
Chris Lattner712e3872007-12-17 08:13:48 +0000302 }
Mike Stump11289f42009-09-09 15:08:12 +0000303
Chris Lattner712e3872007-12-17 08:13:48 +0000304 if (isFramework())
Douglas Gregor97eec242011-09-15 22:00:41 +0000305 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000306 RequestingModule, SuggestedModule,
307 InUserSpecifiedSystemFramework);
Mike Stump11289f42009-09-09 15:08:12 +0000308
Chris Lattner44bd21b2007-12-17 08:17:39 +0000309 assert(isHeaderMap() && "Unknown directory lookup");
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000310 const HeaderMap *HM = getHeaderMap();
311 SmallString<1024> Path;
312 StringRef Dest = HM->lookupFilename(Filename, Path);
313 if (Dest.empty())
Craig Topperd2d442c2014-05-17 23:10:59 +0000314 return nullptr;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000315
316 const FileEntry *Result;
317
318 // Check if the headermap maps the filename to a framework include
319 // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
320 // framework include.
321 if (llvm::sys::path::is_relative(Dest)) {
322 MappedName.clear();
323 MappedName.append(Dest.begin(), Dest.end());
324 Filename = StringRef(MappedName.begin(), MappedName.size());
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000325 HasBeenMapped = true;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000326 Result = HM->LookupFile(Filename, HS.getFileMgr());
327
328 } else {
329 Result = HS.getFileMgr().getFile(Dest);
330 }
331
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000332 if (Result) {
Craig Topperd2d442c2014-05-17 23:10:59 +0000333 if (SearchPath) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000334 StringRef SearchPathRef(getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000335 SearchPath->clear();
336 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
337 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000338 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000339 RelativePath->clear();
340 RelativePath->append(Filename.begin(), Filename.end());
341 }
342 }
343 return Result;
Chris Lattnerf62f7582007-12-17 07:52:39 +0000344}
345
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000346/// \brief Given a framework directory, find the top-most framework directory.
347///
348/// \param FileMgr The file manager to use for directory lookups.
349/// \param DirName The name of the framework directory.
350/// \param SubmodulePath Will be populated with the submodule path from the
351/// returned top-level module to the originally named framework.
352static const DirectoryEntry *
353getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
354 SmallVectorImpl<std::string> &SubmodulePath) {
355 assert(llvm::sys::path::extension(DirName) == ".framework" &&
356 "Not a framework directory");
357
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000358 // Note: as an egregious but useful hack we use the real path here, because
359 // frameworks moving between top-level frameworks to embedded frameworks tend
360 // to be symlinked, and we base the logical structure of modules on the
361 // physical layout. In particular, we need to deal with crazy includes like
362 //
363 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
364 //
365 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
366 // which one should access with, e.g.,
367 //
368 // #include <Bar/Wibble.h>
369 //
370 // Similar issues occur when a top-level framework has moved into an
371 // embedded framework.
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000372 const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
Douglas Gregore00c8b22013-01-26 00:55:12 +0000373 DirName = FileMgr.getCanonicalName(TopFrameworkDir);
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000374 do {
375 // Get the parent directory name.
376 DirName = llvm::sys::path::parent_path(DirName);
377 if (DirName.empty())
378 break;
379
380 // Determine whether this directory exists.
381 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
382 if (!Dir)
383 break;
384
385 // If this is a framework directory, then we're a subframework of this
386 // framework.
387 if (llvm::sys::path::extension(DirName) == ".framework") {
388 SubmodulePath.push_back(llvm::sys::path::stem(DirName));
389 TopFrameworkDir = Dir;
390 }
391 } while (true);
392
393 return TopFrameworkDir;
394}
Chris Lattnerf62f7582007-12-17 07:52:39 +0000395
Chris Lattner712e3872007-12-17 08:13:48 +0000396/// DoFrameworkLookup - Do a lookup of the specified file in the current
397/// DirectoryLookup, which is a framework directory.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000398const FileEntry *DirectoryLookup::DoFrameworkLookup(
Richard Smith3d5b48c2015-10-16 21:42:56 +0000399 StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
400 SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000401 ModuleMap::KnownHeader *SuggestedModule,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000402 bool &InUserSpecifiedSystemFramework) const {
Chris Lattner712e3872007-12-17 08:13:48 +0000403 FileManager &FileMgr = HS.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000404
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000405 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000406 size_t SlashPos = Filename.find('/');
Craig Topperd2d442c2014-05-17 23:10:59 +0000407 if (SlashPos == StringRef::npos) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000408
Chris Lattner712e3872007-12-17 08:13:48 +0000409 // Find out if this is the home for the specified framework, by checking
Daniel Dunbar17138612012-04-05 17:09:40 +0000410 // HeaderSearch. Possible answers are yes/no and unknown.
411 HeaderSearch::FrameworkCacheEntry &CacheEntry =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000412 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000413
Chris Lattner712e3872007-12-17 08:13:48 +0000414 // If it is known and in some other directory, fail.
Daniel Dunbar17138612012-04-05 17:09:40 +0000415 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
Craig Topperd2d442c2014-05-17 23:10:59 +0000416 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattner712e3872007-12-17 08:13:48 +0000418 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000419
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000420 // FrameworkName = "/System/Library/Frameworks/"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000421 SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000422 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000423 if (FrameworkName.empty() || FrameworkName.back() != '/')
424 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000426 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor56c64012011-11-17 01:41:17 +0000427 StringRef ModuleName(Filename.begin(), SlashPos);
428 FrameworkName += ModuleName;
Mike Stump11289f42009-09-09 15:08:12 +0000429
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000430 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
431 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000432
Daniel Dunbar17138612012-04-05 17:09:40 +0000433 // If the cache entry was unresolved, populate it now.
Craig Topperd2d442c2014-05-17 23:10:59 +0000434 if (!CacheEntry.Directory) {
Chris Lattner712e3872007-12-17 08:13:48 +0000435 HS.IncrementFrameworkLookupCount();
Mike Stump11289f42009-09-09 15:08:12 +0000436
Chris Lattner5ed76da2006-10-22 07:24:13 +0000437 // If the framework dir doesn't exist, we fail.
Yaron Keren92e1b622015-03-18 10:17:07 +0000438 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
Craig Topperd2d442c2014-05-17 23:10:59 +0000439 if (!Dir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000440
Chris Lattner5ed76da2006-10-22 07:24:13 +0000441 // Otherwise, if it does, remember that this is the right direntry for this
442 // framework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000443 CacheEntry.Directory = getFrameworkDir();
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000444
445 // If this is a user search directory, check if the framework has been
446 // user-specified as a system framework.
447 if (getDirCharacteristic() == SrcMgr::C_User) {
448 SmallString<1024> SystemFrameworkMarker(FrameworkName);
449 SystemFrameworkMarker += ".system_framework";
Yaron Keren92e1b622015-03-18 10:17:07 +0000450 if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000451 CacheEntry.IsUserSpecifiedSystemFramework = true;
452 }
453 }
Chris Lattner5ed76da2006-10-22 07:24:13 +0000454 }
Mike Stump11289f42009-09-09 15:08:12 +0000455
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000456 // Set the 'user-specified system framework' flag.
457 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
458
Craig Topperd2d442c2014-05-17 23:10:59 +0000459 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000460 RelativePath->clear();
461 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
462 }
Douglas Gregor56c64012011-11-17 01:41:17 +0000463
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000464 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000465 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000466
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000467 FrameworkName += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000468
Craig Topperd2d442c2014-05-17 23:10:59 +0000469 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000470 SearchPath->clear();
471 // Without trailing '/'.
472 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
473 }
474
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000475 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000476 const FileEntry *FE = FileMgr.getFile(FrameworkName,
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000477 /*openFile=*/!SuggestedModule);
478 if (!FE) {
479 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
480 const char *Private = "Private";
481 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
482 Private+strlen(Private));
Craig Topperd2d442c2014-05-17 23:10:59 +0000483 if (SearchPath)
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000484 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
485 Private+strlen(Private));
486
Yaron Keren92e1b622015-03-18 10:17:07 +0000487 FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000490 // If we found the header and are allowed to suggest a module, do so now.
491 if (FE && SuggestedModule) {
492 // Find the framework in which this header occurs.
Ben Langmuiref914b82014-05-15 16:20:33 +0000493 StringRef FrameworkPath = FE->getDir()->getName();
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000494 bool FoundFramework = false;
495 do {
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000496 // Determine whether this directory exists.
497 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
498 if (!Dir)
499 break;
500
501 // If this is a framework directory, then we're a subframework of this
502 // framework.
503 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
504 FoundFramework = true;
505 break;
506 }
Ben Langmuiref914b82014-05-15 16:20:33 +0000507
508 // Get the parent directory name.
509 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
510 if (FrameworkPath.empty())
511 break;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000512 } while (true);
513
Richard Smith3d5b48c2015-10-16 21:42:56 +0000514 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000515 if (FoundFramework) {
Richard Smith3d5b48c2015-10-16 21:42:56 +0000516 if (!HS.findUsableModuleForFrameworkHeader(
517 FE, FrameworkPath, RequestingModule, SuggestedModule, IsSystem))
518 return nullptr;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000519 } else {
Richard Smith3d5b48c2015-10-16 21:42:56 +0000520 if (!HS.findUsableModuleForHeader(FE, getDir(), RequestingModule,
521 SuggestedModule, IsSystem))
522 return nullptr;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000523 }
524 }
Douglas Gregor97eec242011-09-15 22:00:41 +0000525 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000526}
527
Douglas Gregor89929282012-01-30 06:01:29 +0000528void HeaderSearch::setTarget(const TargetInfo &Target) {
529 ModMap.setTarget(Target);
530}
531
Chris Lattnerf62f7582007-12-17 07:52:39 +0000532
Chris Lattner712e3872007-12-17 08:13:48 +0000533//===----------------------------------------------------------------------===//
534// Header File Location.
535//===----------------------------------------------------------------------===//
536
Reid Klecknera97d4c02014-02-18 23:49:24 +0000537/// \brief Return true with a diagnostic if the file that MSVC would have found
538/// fails to match the one that Clang would have found with MSVC header search
539/// disabled.
540static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
541 const FileEntry *MSFE, const FileEntry *FE,
542 SourceLocation IncludeLoc) {
543 if (MSFE && FE != MSFE) {
544 Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
545 return true;
546 }
547 return false;
548}
Chris Lattner712e3872007-12-17 08:13:48 +0000549
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000550static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
551 assert(!Str.empty());
552 char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
553 std::copy(Str.begin(), Str.end(), CopyStr);
554 CopyStr[Str.size()] = '\0';
555 return CopyStr;
556}
557
James Dennettc07ab2c2012-06-20 00:56:32 +0000558/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000559/// return null on failure. isAngled indicates whether the file reference is
Will Wilson0fafd342013-12-27 19:46:16 +0000560/// for system \#include's or not (i.e. using <> instead of ""). Includers, if
561/// non-empty, indicates where the \#including file(s) are, in case a relative
562/// search is needed. Microsoft mode will pass all \#including files.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000563const FileEntry *HeaderSearch::LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000564 StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
565 const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000566 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
567 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000568 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
569 bool SkipCache) {
Douglas Gregor97eec242011-09-15 22:00:41 +0000570 if (SuggestedModule)
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000571 *SuggestedModule = ModuleMap::KnownHeader();
Douglas Gregor97eec242011-09-15 22:00:41 +0000572
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000573 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000574 if (llvm::sys::path::is_absolute(Filename)) {
Craig Topperd2d442c2014-05-17 23:10:59 +0000575 CurDir = nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000576
577 // If this was an #include_next "/absolute/file", fail.
Craig Topperd2d442c2014-05-17 23:10:59 +0000578 if (FromDir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000579
Craig Topperd2d442c2014-05-17 23:10:59 +0000580 if (SearchPath)
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000581 SearchPath->clear();
Craig Topperd2d442c2014-05-17 23:10:59 +0000582 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000583 RelativePath->clear();
584 RelativePath->append(Filename.begin(), Filename.end());
585 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000586 // Otherwise, just return the file.
Richard Smith3d5b48c2015-10-16 21:42:56 +0000587 return getFileAndSuggestModule(Filename, nullptr,
588 /*IsSystemHeaderDir*/false,
589 RequestingModule, SuggestedModule);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000590 }
Mike Stump11289f42009-09-09 15:08:12 +0000591
Reid Klecknera97d4c02014-02-18 23:49:24 +0000592 // This is the header that MSVC's header search would have found.
Craig Topperd2d442c2014-05-17 23:10:59 +0000593 const FileEntry *MSFE = nullptr;
Richard Smith8c71eba2014-03-05 20:51:45 +0000594 ModuleMap::KnownHeader MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000595
Douglas Gregor9f93e382011-07-28 04:45:53 +0000596 // Unless disabled, check to see if the file is in the #includer's
Will Wilson0fafd342013-12-27 19:46:16 +0000597 // directory. This cannot be based on CurDir, because each includer could be
598 // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
599 // include of "baz.h" should resolve to "whatever/foo/baz.h".
Chris Lattnerf62f7582007-12-17 07:52:39 +0000600 // This search is not done for <> headers.
Will Wilson0fafd342013-12-27 19:46:16 +0000601 if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
NAKAMURA Takumi9cb62642013-12-10 02:36:28 +0000602 SmallString<1024> TmpDir;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000603 bool First = true;
604 for (const auto &IncluderAndDir : Includers) {
605 const FileEntry *Includer = IncluderAndDir.first;
606
Will Wilson0fafd342013-12-27 19:46:16 +0000607 // Concatenate the requested file onto the directory.
Nikola Smiljaniccf385dc2015-05-08 06:02:37 +0000608 // FIXME: Portability. Filename concatenation should be in sys::Path.
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000609 TmpDir = IncluderAndDir.second->getName();
Nikola Smiljaniccf385dc2015-05-08 06:02:37 +0000610 TmpDir.push_back('/');
611 TmpDir.append(Filename.begin(), Filename.end());
Richard Smith8c71eba2014-03-05 20:51:45 +0000612
Richard Smith6f548ec2014-03-06 18:08:08 +0000613 // FIXME: We don't cache the result of getFileInfo across the call to
614 // getFileAndSuggestModule, because it's a reference to an element of
615 // a container that could be reallocated across this call.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000616 //
617 // FIXME: If we have no includer, that means we're processing a #include
618 // from a module build. We should treat this as a system header if we're
619 // building a [system] module.
Richard Smith6f548ec2014-03-06 18:08:08 +0000620 bool IncluderIsSystemHeader =
Richard Smith3c1a41a2014-12-02 00:08:08 +0000621 Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000622 if (const FileEntry *FE = getFileAndSuggestModule(
Richard Smith3d5b48c2015-10-16 21:42:56 +0000623 TmpDir, IncluderAndDir.second, IncluderIsSystemHeader,
624 RequestingModule, SuggestedModule)) {
Richard Smith3c1a41a2014-12-02 00:08:08 +0000625 if (!Includer) {
626 assert(First && "only first includer can have no file");
627 return FE;
628 }
629
Will Wilson0fafd342013-12-27 19:46:16 +0000630 // Leave CurDir unset.
631 // This file is a system header or C++ unfriendly if the old file is.
632 //
633 // Note that we only use one of FromHFI/ToHFI at once, due to potential
634 // reallocation of the underlying vector potentially making the first
635 // reference binding dangling.
Richard Smith6f548ec2014-03-06 18:08:08 +0000636 HeaderFileInfo &FromHFI = getFileInfo(Includer);
Will Wilson0fafd342013-12-27 19:46:16 +0000637 unsigned DirInfo = FromHFI.DirInfo;
638 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
639 StringRef Framework = FromHFI.Framework;
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000640
Will Wilson0fafd342013-12-27 19:46:16 +0000641 HeaderFileInfo &ToHFI = getFileInfo(FE);
642 ToHFI.DirInfo = DirInfo;
643 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
644 ToHFI.Framework = Framework;
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000645
Craig Topperd2d442c2014-05-17 23:10:59 +0000646 if (SearchPath) {
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000647 StringRef SearchPathRef(IncluderAndDir.second->getName());
Will Wilson0fafd342013-12-27 19:46:16 +0000648 SearchPath->clear();
649 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
650 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000651 if (RelativePath) {
Will Wilson0fafd342013-12-27 19:46:16 +0000652 RelativePath->clear();
653 RelativePath->append(Filename.begin(), Filename.end());
654 }
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000655 if (First)
Reid Klecknera97d4c02014-02-18 23:49:24 +0000656 return FE;
657
658 // Otherwise, we found the path via MSVC header search rules. If
659 // -Wmsvc-include is enabled, we have to keep searching to see if we
660 // would've found this header in -I or -isystem directories.
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000661 if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
Reid Klecknera97d4c02014-02-18 23:49:24 +0000662 return FE;
663 } else {
664 MSFE = FE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000665 if (SuggestedModule) {
666 MSSuggestedModule = *SuggestedModule;
667 *SuggestedModule = ModuleMap::KnownHeader();
668 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000669 break;
670 }
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000671 }
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000672 First = false;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000673 }
674 }
Mike Stump11289f42009-09-09 15:08:12 +0000675
Craig Topperd2d442c2014-05-17 23:10:59 +0000676 CurDir = nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000677
678 // If this is a system #include, ignore the user #include locs.
Nico Weber3b1d1212011-05-24 04:31:14 +0000679 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000681 // If this is a #include_next request, start searching after the directory the
682 // file was found in.
683 if (FromDir)
684 i = FromDir-&SearchDirs[0];
Mike Stump11289f42009-09-09 15:08:12 +0000685
Chris Lattnerd4275422007-07-22 07:28:00 +0000686 // Cache all of the lookups performed by this method. Many headers are
687 // multiply included, and the "pragma once" optimization prevents them from
688 // being relex/pp'd, but they would still have to search through a
689 // (potentially huge) series of SearchDirs to find it.
David Blaikie13156b62014-11-19 03:06:06 +0000690 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
Chris Lattnerd4275422007-07-22 07:28:00 +0000691
692 // If the entry has been previously looked up, the first value will be
693 // non-zero. If the value is equal to i (the start point of our search), then
694 // this is a matching hit.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000695 if (!SkipCache && CacheLookup.StartIdx == i+1) {
Chris Lattnerd4275422007-07-22 07:28:00 +0000696 // Skip querying potentially lots of directories for this lookup.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000697 i = CacheLookup.HitIdx;
698 if (CacheLookup.MappedName)
699 Filename = CacheLookup.MappedName;
Chris Lattnerd4275422007-07-22 07:28:00 +0000700 } else {
701 // Otherwise, this is the first query, or the previous query didn't match
702 // our search start. We will fill in our found location below, so prime the
703 // start point value.
Argyrios Kyrtzidis7bd78a92014-03-29 03:22:54 +0000704 CacheLookup.reset(/*StartIdx=*/i+1);
Chris Lattnerd4275422007-07-22 07:28:00 +0000705 }
Mike Stump11289f42009-09-09 15:08:12 +0000706
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000707 SmallString<64> MappedName;
708
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000709 // Check each directory in sequence to see if it contains this file.
710 for (; i != SearchDirs.size(); ++i) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000711 bool InUserSpecifiedSystemFramework = false;
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000712 bool HasBeenMapped = false;
Richard Smith3d5b48c2015-10-16 21:42:56 +0000713 const FileEntry *FE = SearchDirs[i].LookupFile(
714 Filename, *this, SearchPath, RelativePath, RequestingModule,
715 SuggestedModule, InUserSpecifiedSystemFramework, HasBeenMapped,
716 MappedName);
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000717 if (HasBeenMapped) {
718 CacheLookup.MappedName =
719 copyString(Filename, LookupFileCache.getAllocator());
720 }
Chris Lattner712e3872007-12-17 08:13:48 +0000721 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000722
Chris Lattner712e3872007-12-17 08:13:48 +0000723 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000724
Chris Lattner712e3872007-12-17 08:13:48 +0000725 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor9f93e382011-07-28 04:45:53 +0000726 HeaderFileInfo &HFI = getFileInfo(FE);
727 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000728
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000729 // If the directory characteristic is User but this framework was
730 // user-specified to be treated as a system framework, promote the
731 // characteristic.
732 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
733 HFI.DirInfo = SrcMgr::C_System;
734
Richard Smith8acadcb2012-06-13 20:27:03 +0000735 // If the filename matches a known system header prefix, override
736 // whether the file is a system header.
Richard Trieu871f5f32012-06-13 20:52:36 +0000737 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
738 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
739 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
Richard Smith8acadcb2012-06-13 20:27:03 +0000740 : SrcMgr::C_User;
741 break;
742 }
743 }
744
Douglas Gregor9f93e382011-07-28 04:45:53 +0000745 // If this file is found in a header map and uses the framework style of
746 // includes, then this header is part of a framework we're building.
747 if (CurDir->isIndexHeaderMap()) {
748 size_t SlashPos = Filename.find('/');
749 if (SlashPos != StringRef::npos) {
750 HFI.IndexHeaderMapHeader = 1;
751 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
752 SlashPos));
753 }
754 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000755
Richard Smith8c71eba2014-03-05 20:51:45 +0000756 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
757 if (SuggestedModule)
758 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000759 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000760 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000761
Chris Lattner712e3872007-12-17 08:13:48 +0000762 // Remember this location for the next lookup we do.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000763 CacheLookup.HitIdx = i;
Chris Lattner712e3872007-12-17 08:13:48 +0000764 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000765 }
Mike Stump11289f42009-09-09 15:08:12 +0000766
Douglas Gregord8575e12011-07-30 06:28:34 +0000767 // If we are including a file with a quoted include "foo.h" from inside
768 // a header in a framework that is currently being built, and we couldn't
769 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
770 // "Foo" is the name of the framework in which the including header was found.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000771 if (!Includers.empty() && Includers.front().first && !isAngled &&
Will Wilson0fafd342013-12-27 19:46:16 +0000772 Filename.find('/') == StringRef::npos) {
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000773 HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
Douglas Gregord8575e12011-07-30 06:28:34 +0000774 if (IncludingHFI.IndexHeaderMapHeader) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000775 SmallString<128> ScratchFilename;
Douglas Gregord8575e12011-07-30 06:28:34 +0000776 ScratchFilename += IncludingHFI.Framework;
777 ScratchFilename += '/';
778 ScratchFilename += Filename;
Will Wilson0fafd342013-12-27 19:46:16 +0000779
Richard Smith3d5b48c2015-10-16 21:42:56 +0000780 const FileEntry *FE =
781 LookupFile(ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir,
782 CurDir, Includers.front(), SearchPath, RelativePath,
783 RequestingModule, SuggestedModule);
Reid Klecknera97d4c02014-02-18 23:49:24 +0000784
Richard Smith8c71eba2014-03-05 20:51:45 +0000785 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
786 if (SuggestedModule)
787 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000788 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000789 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000790
David Blaikie3c8c46e2014-11-19 05:48:40 +0000791 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
David Blaikie13156b62014-11-19 03:06:06 +0000792 CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
Richard Smith8c71eba2014-03-05 20:51:45 +0000793 // FIXME: SuggestedModule.
Reid Klecknera97d4c02014-02-18 23:49:24 +0000794 return FE;
Douglas Gregord8575e12011-07-30 06:28:34 +0000795 }
796 }
797
Craig Topperd2d442c2014-05-17 23:10:59 +0000798 if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
Richard Smith8c71eba2014-03-05 20:51:45 +0000799 if (SuggestedModule)
800 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000801 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000802 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000803
Chris Lattnerd4275422007-07-22 07:28:00 +0000804 // Otherwise, didn't find it. Remember we didn't find this.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000805 CacheLookup.HitIdx = SearchDirs.size();
Craig Topperd2d442c2014-05-17 23:10:59 +0000806 return nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000807}
808
Chris Lattner63dd32b2006-10-20 04:42:40 +0000809/// LookupSubframeworkHeader - Look up a subframework for the specified
James Dennettc07ab2c2012-06-20 00:56:32 +0000810/// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
Chris Lattner63dd32b2006-10-20 04:42:40 +0000811/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
812/// is a subframework within Carbon.framework. If so, return the FileEntry
813/// for the designated file, otherwise return null.
814const FileEntry *HeaderSearch::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000815LookupSubframeworkHeader(StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000816 const FileEntry *ContextFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000817 SmallVectorImpl<char> *SearchPath,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000818 SmallVectorImpl<char> *RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000819 Module *RequestingModule,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000820 ModuleMap::KnownHeader *SuggestedModule) {
Chris Lattner12261882008-02-01 05:34:02 +0000821 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattner63dd32b2006-10-20 04:42:40 +0000823 // Framework names must have a '/' in the filename. Find it.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000824 // FIXME: Should we permit '\' on Windows?
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000825 size_t SlashPos = Filename.find('/');
Craig Topperd2d442c2014-05-17 23:10:59 +0000826 if (SlashPos == StringRef::npos) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000827
Chris Lattner63dd32b2006-10-20 04:42:40 +0000828 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000829 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner63dd32b2006-10-20 04:42:40 +0000831 // If the context info wasn't a framework, couldn't be a subframework.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000832 const unsigned DotFrameworkLen = 10;
833 const char *FrameworkPos = strstr(ContextName, ".framework");
Craig Topperd2d442c2014-05-17 23:10:59 +0000834 if (FrameworkPos == nullptr ||
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000835 (FrameworkPos[DotFrameworkLen] != '/' &&
836 FrameworkPos[DotFrameworkLen] != '\\'))
Craig Topperd2d442c2014-05-17 23:10:59 +0000837 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000838
Daniel Dunbar17138612012-04-05 17:09:40 +0000839 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000840
Chris Lattner63dd32b2006-10-20 04:42:40 +0000841 // Append Frameworks/HIToolbox.framework/
842 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000843 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000844 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000845
David Blaikie13156b62014-11-19 03:06:06 +0000846 auto &CacheLookup =
847 *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
848 FrameworkCacheEntry())).first;
Mike Stump11289f42009-09-09 15:08:12 +0000849
Chris Lattner5ed76da2006-10-22 07:24:13 +0000850 // Some other location?
David Blaikie13156b62014-11-19 03:06:06 +0000851 if (CacheLookup.second.Directory &&
852 CacheLookup.first().size() == FrameworkName.size() &&
853 memcmp(CacheLookup.first().data(), &FrameworkName[0],
854 CacheLookup.first().size()) != 0)
Craig Topperd2d442c2014-05-17 23:10:59 +0000855 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000856
Chris Lattner5ed76da2006-10-22 07:24:13 +0000857 // Cache subframework.
David Blaikie13156b62014-11-19 03:06:06 +0000858 if (!CacheLookup.second.Directory) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000859 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattner5ed76da2006-10-22 07:24:13 +0000861 // If the framework dir doesn't exist, we fail.
Yaron Keren92e1b622015-03-18 10:17:07 +0000862 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
Craig Topperd2d442c2014-05-17 23:10:59 +0000863 if (!Dir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000864
Chris Lattner5ed76da2006-10-22 07:24:13 +0000865 // Otherwise, if it does, remember that this is the right direntry for this
866 // framework.
David Blaikie13156b62014-11-19 03:06:06 +0000867 CacheLookup.second.Directory = Dir;
Chris Lattner5ed76da2006-10-22 07:24:13 +0000868 }
Mike Stump11289f42009-09-09 15:08:12 +0000869
Craig Topperd2d442c2014-05-17 23:10:59 +0000870 const FileEntry *FE = nullptr;
Chris Lattner577377e2006-10-20 04:55:45 +0000871
Craig Topperd2d442c2014-05-17 23:10:59 +0000872 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000873 RelativePath->clear();
874 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
875 }
876
Chris Lattner63dd32b2006-10-20 04:42:40 +0000877 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000878 SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000879 HeadersFilename += "Headers/";
Craig Topperd2d442c2014-05-17 23:10:59 +0000880 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000881 SearchPath->clear();
882 // Without trailing '/'.
883 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
884 }
885
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000886 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000887 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
Mike Stump11289f42009-09-09 15:08:12 +0000888
Chris Lattner63dd32b2006-10-20 04:42:40 +0000889 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000890 HeadersFilename = FrameworkName;
891 HeadersFilename += "PrivateHeaders/";
Craig Topperd2d442c2014-05-17 23:10:59 +0000892 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000893 SearchPath->clear();
894 // Without trailing '/'.
895 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
896 }
897
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000898 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000899 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true)))
Craig Topperd2d442c2014-05-17 23:10:59 +0000900 return nullptr;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000901 }
Mike Stump11289f42009-09-09 15:08:12 +0000902
Chris Lattner577377e2006-10-20 04:55:45 +0000903 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000904 //
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000905 // Note that the temporary 'DirInfo' is required here, as either call to
906 // getFileInfo could resize the vector and we don't want to rely on order
907 // of evaluation.
908 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
909 getFileInfo(FE).DirInfo = DirInfo;
Douglas Gregorf5f94522013-02-08 00:10:48 +0000910
Richard Smith3d5b48c2015-10-16 21:42:56 +0000911 FrameworkName.pop_back(); // remove the trailing '/'
912 if (!findUsableModuleForFrameworkHeader(FE, FrameworkName, RequestingModule,
913 SuggestedModule, /*IsSystem*/ false))
914 return nullptr;
Douglas Gregorf5f94522013-02-08 00:10:48 +0000915
Chris Lattner577377e2006-10-20 04:55:45 +0000916 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000917}
918
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000919//===----------------------------------------------------------------------===//
920// File Info Management.
921//===----------------------------------------------------------------------===//
922
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000923/// \brief Merge the header file info provided by \p OtherHFI into the current
924/// header file info (\p HFI)
925static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
926 const HeaderFileInfo &OtherHFI) {
Richard Smithd8879c82015-08-24 21:59:32 +0000927 assert(OtherHFI.External && "expected to merge external HFI");
928
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000929 HFI.isImport |= OtherHFI.isImport;
930 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000931 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000932 HFI.NumIncludes += OtherHFI.NumIncludes;
Richard Smithd8879c82015-08-24 21:59:32 +0000933
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000934 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
935 HFI.ControllingMacro = OtherHFI.ControllingMacro;
936 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
937 }
Richard Smithd8879c82015-08-24 21:59:32 +0000938
939 HFI.DirInfo = OtherHFI.DirInfo;
940 HFI.External = (!HFI.IsValid || HFI.External);
941 HFI.IsValid = true;
942 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000943
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000944 if (HFI.Framework.empty())
945 HFI.Framework = OtherHFI.Framework;
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000946}
947
Steve Naroff3fa455a2009-04-24 20:03:17 +0000948/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000949/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000950HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000951 if (FE->getUID() >= FileInfo.size())
Richard Smith386bb072015-08-18 23:42:23 +0000952 FileInfo.resize(FE->getUID() + 1);
953
Richard Smithd8879c82015-08-24 21:59:32 +0000954 HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
Richard Smith386bb072015-08-18 23:42:23 +0000955 // FIXME: Use a generation count to check whether this is really up to date.
Richard Smithd8879c82015-08-24 21:59:32 +0000956 if (ExternalSource && !HFI->Resolved) {
957 HFI->Resolved = true;
958 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
959
960 HFI = &FileInfo[FE->getUID()];
961 if (ExternalHFI.External)
962 mergeHeaderFileInfo(*HFI, ExternalHFI);
Richard Smith386bb072015-08-18 23:42:23 +0000963 }
964
Richard Smithd8879c82015-08-24 21:59:32 +0000965 HFI->IsValid = true;
Richard Smith386bb072015-08-18 23:42:23 +0000966 // We have local information about this header file, so it's no longer
967 // strictly external.
Richard Smithd8879c82015-08-24 21:59:32 +0000968 HFI->External = false;
969 return *HFI;
Mike Stump11289f42009-09-09 15:08:12 +0000970}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000971
Richard Smith386bb072015-08-18 23:42:23 +0000972const HeaderFileInfo *
Richard Smithd8879c82015-08-24 21:59:32 +0000973HeaderSearch::getExistingFileInfo(const FileEntry *FE,
974 bool WantExternal) const {
Richard Smith386bb072015-08-18 23:42:23 +0000975 // If we have an external source, ensure we have the latest information.
976 // FIXME: Use a generation count to check whether this is really up to date.
Richard Smithd8879c82015-08-24 21:59:32 +0000977 HeaderFileInfo *HFI;
978 if (ExternalSource) {
979 if (FE->getUID() >= FileInfo.size()) {
980 if (!WantExternal)
981 return nullptr;
982 FileInfo.resize(FE->getUID() + 1);
Richard Smith386bb072015-08-18 23:42:23 +0000983 }
Richard Smithd8879c82015-08-24 21:59:32 +0000984
985 HFI = &FileInfo[FE->getUID()];
986 if (!WantExternal && (!HFI->IsValid || HFI->External))
987 return nullptr;
988 if (!HFI->Resolved) {
989 HFI->Resolved = true;
990 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
991
992 HFI = &FileInfo[FE->getUID()];
993 if (ExternalHFI.External)
994 mergeHeaderFileInfo(*HFI, ExternalHFI);
995 }
996 } else if (FE->getUID() >= FileInfo.size()) {
997 return nullptr;
998 } else {
999 HFI = &FileInfo[FE->getUID()];
Ben Langmuird285c502014-03-13 16:46:36 +00001000 }
Richard Smith386bb072015-08-18 23:42:23 +00001001
Richard Smithd8879c82015-08-24 21:59:32 +00001002 if (!HFI->IsValid || (HFI->External && !WantExternal))
Richard Smith386bb072015-08-18 23:42:23 +00001003 return nullptr;
1004
Richard Smithd8879c82015-08-24 21:59:32 +00001005 return HFI;
Ben Langmuird285c502014-03-13 16:46:36 +00001006}
1007
Douglas Gregor37aa4932011-05-04 00:14:37 +00001008bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1009 // Check if we've ever seen this file as a header.
Richard Smith386bb072015-08-18 23:42:23 +00001010 if (auto *HFI = getExistingFileInfo(File))
1011 return HFI->isPragmaOnce || HFI->isImport || HFI->ControllingMacro ||
1012 HFI->ControllingMacroID;
1013 return false;
Douglas Gregor37aa4932011-05-04 00:14:37 +00001014}
1015
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001016void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001017 ModuleMap::ModuleHeaderRole Role,
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001018 bool isCompilingModuleHeader) {
Richard Smithd8879c82015-08-24 21:59:32 +00001019 bool isModularHeader = !(Role & ModuleMap::TextualHeader);
1020
1021 // Don't mark the file info as non-external if there's nothing to change.
1022 if (!isCompilingModuleHeader) {
1023 if (!isModularHeader)
1024 return;
1025 auto *HFI = getExistingFileInfo(FE);
1026 if (HFI && HFI->isModuleHeader)
1027 return;
1028 }
1029
Richard Smith386bb072015-08-18 23:42:23 +00001030 auto &HFI = getFileInfo(FE);
Richard Smithd8879c82015-08-24 21:59:32 +00001031 HFI.isModuleHeader |= isModularHeader;
Richard Smithe70dadd2015-07-10 22:27:17 +00001032 HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
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
Richard Smithe70dadd2015-07-10 22:27:17 +00001061 = FileInfo.getControllingMacro(ExternalLookup)) {
1062 // If the header corresponds to a module, check whether the macro is already
1063 // defined in that module rather than checking in the current set of visible
1064 // modules.
1065 if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M)
1066 : PP.isMacroDefined(ControllingMacro)) {
Douglas Gregor99734e72009-04-25 23:30:02 +00001067 ++NumMultiIncludeFileOptzn;
1068 return false;
1069 }
Richard Smithe70dadd2015-07-10 22:27:17 +00001070 }
Mike Stump11289f42009-09-09 15:08:12 +00001071
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001072 // Increment the number of times this file has been included.
1073 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +00001074
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001075 return true;
1076}
1077
Ted Kremenekfbcce6f2011-07-26 23:46:11 +00001078size_t HeaderSearch::getTotalMemory() const {
1079 return SearchDirs.capacity()
Ted Kremenekae63d102011-07-27 18:41:18 +00001080 + llvm::capacity_in_bytes(FileInfo)
1081 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekfbcce6f2011-07-26 23:46:11 +00001082 + LookupFileCache.getAllocator().getTotalMemory()
1083 + FrameworkMap.getAllocator().getTotalMemory();
1084}
Douglas Gregor9f93e382011-07-28 04:45:53 +00001085
1086StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
David Blaikie13156b62014-11-19 03:06:06 +00001087 return FrameworkNames.insert(Framework).first->first();
Douglas Gregor9f93e382011-07-28 04:45:53 +00001088}
Douglas Gregor718292f2011-11-11 19:10:28 +00001089
1090bool HeaderSearch::hasModuleMap(StringRef FileName,
Douglas Gregor963c5532013-06-21 16:28:10 +00001091 const DirectoryEntry *Root,
1092 bool IsSystem) {
Richard Smith47972af2015-06-16 00:08:24 +00001093 if (!HSOpts->ImplicitModuleMaps)
Argyrios Kyrtzidis9955dbc2013-12-12 16:08:33 +00001094 return false;
1095
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001096 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
Douglas Gregor718292f2011-11-11 19:10:28 +00001097
1098 StringRef DirName = FileName;
1099 do {
1100 // Get the parent directory name.
1101 DirName = llvm::sys::path::parent_path(DirName);
1102 if (DirName.empty())
1103 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001104
Douglas Gregor718292f2011-11-11 19:10:28 +00001105 // Determine whether this directory exists.
1106 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
1107 if (!Dir)
1108 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001109
Ben Langmuir984e1df2014-03-19 20:23:34 +00001110 // Try to load the module map file in this directory.
Richard Smith3c1a41a2014-12-02 00:08:08 +00001111 switch (loadModuleMapFile(Dir, IsSystem,
1112 llvm::sys::path::extension(Dir->getName()) ==
1113 ".framework")) {
Douglas Gregor80b69042011-11-12 00:22:19 +00001114 case LMM_NewlyLoaded:
1115 case LMM_AlreadyLoaded:
Daniel Jasperca9f7382013-09-24 09:27:13 +00001116 // Success. All of the directories we stepped through inherit this module
1117 // map file.
1118 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1119 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1120 return true;
Daniel Jasper97da9172013-10-22 08:09:47 +00001121
1122 case LMM_NoDirectory:
1123 case LMM_InvalidModuleMap:
1124 break;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001125 }
1126
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001127 // If we hit the top of our search, we're done.
1128 if (Dir == Root)
1129 return false;
1130
Douglas Gregor718292f2011-11-11 19:10:28 +00001131 // Keep track of all of the directories we checked, so we can mark them as
1132 // having module maps if we eventually do find a module map.
1133 FixUpDirectories.push_back(Dir);
1134 } while (true);
Douglas Gregor718292f2011-11-11 19:10:28 +00001135}
1136
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001137ModuleMap::KnownHeader
1138HeaderSearch::findModuleForHeader(const FileEntry *File) const {
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001139 if (ExternalSource) {
1140 // Make sure the external source has handled header info about this file,
1141 // which includes whether the file is part of a module.
Richard Smith386bb072015-08-18 23:42:23 +00001142 (void)getExistingFileInfo(File);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001143 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001144 return ModMap.findModuleForHeader(File);
Douglas Gregor718292f2011-11-11 19:10:28 +00001145}
1146
Richard Smith3d5b48c2015-10-16 21:42:56 +00001147bool HeaderSearch::findUsableModuleForHeader(
1148 const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
1149 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1150 if (File && SuggestedModule) {
1151 // If there is a module that corresponds to this header, suggest it.
1152 hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
1153 *SuggestedModule = findModuleForHeader(File);
1154 }
1155 return true;
1156}
1157
1158bool HeaderSearch::findUsableModuleForFrameworkHeader(
1159 const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
1160 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1161 // If we're supposed to suggest a module, look for one now.
1162 if (SuggestedModule) {
1163 // Find the top-level framework based on this framework.
1164 SmallVector<std::string, 4> SubmodulePath;
1165 const DirectoryEntry *TopFrameworkDir
1166 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1167
1168 // Determine the name of the top-level framework.
1169 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1170
1171 // Load this framework module. If that succeeds, find the suggested module
1172 // for this header, if any.
1173 loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1174
1175 // FIXME: This can find a module not part of ModuleName, which is
1176 // important so that we're consistent about whether this header
1177 // corresponds to a module. Possibly we should lock down framework modules
1178 // so that this is not possible.
1179 *SuggestedModule = findModuleForHeader(File);
1180 }
1181 return true;
1182}
1183
Richard Smith9acb99e32014-12-10 03:09:48 +00001184static const FileEntry *getPrivateModuleMap(const FileEntry *File,
Ben Langmuir984e1df2014-03-19 20:23:34 +00001185 FileManager &FileMgr) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001186 StringRef Filename = llvm::sys::path::filename(File->getName());
1187 SmallString<128> PrivateFilename(File->getDir()->getName());
Ben Langmuir984e1df2014-03-19 20:23:34 +00001188 if (Filename == "module.map")
Douglas Gregor80306772011-12-07 21:25:07 +00001189 llvm::sys::path::append(PrivateFilename, "module_private.map");
Ben Langmuir984e1df2014-03-19 20:23:34 +00001190 else if (Filename == "module.modulemap")
1191 llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1192 else
1193 return nullptr;
1194 return FileMgr.getFile(PrivateFilename);
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001195}
1196
Ben Langmuir984e1df2014-03-19 20:23:34 +00001197bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001198 // Find the directory for the module. For frameworks, that may require going
1199 // up from the 'Modules' directory.
1200 const DirectoryEntry *Dir = nullptr;
1201 if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
1202 Dir = FileMgr.getDirectory(".");
1203 else {
1204 Dir = File->getDir();
1205 StringRef DirName(Dir->getName());
1206 if (llvm::sys::path::filename(DirName) == "Modules") {
1207 DirName = llvm::sys::path::parent_path(DirName);
1208 if (DirName.endswith(".framework"))
1209 Dir = FileMgr.getDirectory(DirName);
1210 // FIXME: This assert can fail if there's a race between the above check
1211 // and the removal of the directory.
1212 assert(Dir && "parent must exist");
1213 }
1214 }
1215
1216 switch (loadModuleMapFileImpl(File, IsSystem, Dir)) {
Ben Langmuir984e1df2014-03-19 20:23:34 +00001217 case LMM_AlreadyLoaded:
1218 case LMM_NewlyLoaded:
1219 return false;
1220 case LMM_NoDirectory:
1221 case LMM_InvalidModuleMap:
1222 return true;
1223 }
Aaron Ballmand8de5b62014-03-20 14:22:33 +00001224 llvm_unreachable("Unknown load module map result");
Ben Langmuir984e1df2014-03-19 20:23:34 +00001225}
1226
1227HeaderSearch::LoadModuleMapResult
Richard Smith9acb99e32014-12-10 03:09:48 +00001228HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1229 const DirectoryEntry *Dir) {
Ben Langmuir984e1df2014-03-19 20:23:34 +00001230 assert(File && "expected FileEntry");
1231
Richard Smith9887d792014-10-17 01:42:53 +00001232 // Check whether we've already loaded this module map, and mark it as being
1233 // loaded in case we recursively try to load it from itself.
1234 auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1235 if (!AddResult.second)
1236 return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001237
Richard Smith9acb99e32014-12-10 03:09:48 +00001238 if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) {
Richard Smith9887d792014-10-17 01:42:53 +00001239 LoadedModuleMaps[File] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001240 return LMM_InvalidModuleMap;
1241 }
1242
1243 // Try to load a corresponding private module map.
Richard Smith9acb99e32014-12-10 03:09:48 +00001244 if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1245 if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
Richard Smith9887d792014-10-17 01:42:53 +00001246 LoadedModuleMaps[File] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001247 return LMM_InvalidModuleMap;
1248 }
1249 }
1250
1251 // This directory has a module map.
Ben Langmuir984e1df2014-03-19 20:23:34 +00001252 return LMM_NewlyLoaded;
1253}
1254
1255const FileEntry *
1256HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
Richard Smith47972af2015-06-16 00:08:24 +00001257 if (!HSOpts->ImplicitModuleMaps)
Daniel Jasper21a0f552014-11-25 09:45:48 +00001258 return nullptr;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001259 // For frameworks, the preferred spelling is Modules/module.modulemap, but
1260 // module.map at the framework root is also accepted.
1261 SmallString<128> ModuleMapFileName(Dir->getName());
1262 if (IsFramework)
1263 llvm::sys::path::append(ModuleMapFileName, "Modules");
1264 llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1265 if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
1266 return F;
1267
1268 // Continue to allow module.map
1269 ModuleMapFileName = Dir->getName();
1270 llvm::sys::path::append(ModuleMapFileName, "module.map");
1271 return FileMgr.getFile(ModuleMapFileName);
1272}
1273
1274Module *HeaderSearch::loadFrameworkModule(StringRef Name,
Douglas Gregor279a6c32012-01-29 17:08:11 +00001275 const DirectoryEntry *Dir,
1276 bool IsSystem) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001277 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor56c64012011-11-17 01:41:17 +00001278 return Module;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001279
Douglas Gregor56c64012011-11-17 01:41:17 +00001280 // Try to load a module map file.
Ben Langmuir984e1df2014-03-19 20:23:34 +00001281 switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
Douglas Gregor56c64012011-11-17 01:41:17 +00001282 case LMM_InvalidModuleMap:
Ben Langmuira5254002015-07-02 13:19:48 +00001283 // Try to infer a module map from the framework directory.
1284 if (HSOpts->ImplicitModuleMaps)
1285 ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
Douglas Gregor56c64012011-11-17 01:41:17 +00001286 break;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001287
Douglas Gregor56c64012011-11-17 01:41:17 +00001288 case LMM_AlreadyLoaded:
1289 case LMM_NoDirectory:
Craig Topperd2d442c2014-05-17 23:10:59 +00001290 return nullptr;
1291
Douglas Gregor56c64012011-11-17 01:41:17 +00001292 case LMM_NewlyLoaded:
Ben Langmuira5254002015-07-02 13:19:48 +00001293 break;
Douglas Gregor56c64012011-11-17 01:41:17 +00001294 }
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001295
Ben Langmuira5254002015-07-02 13:19:48 +00001296 return ModMap.findModule(Name);
Douglas Gregor56c64012011-11-17 01:41:17 +00001297}
1298
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001299
Douglas Gregor80b69042011-11-12 00:22:19 +00001300HeaderSearch::LoadModuleMapResult
Ben Langmuir984e1df2014-03-19 20:23:34 +00001301HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1302 bool IsFramework) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001303 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
Ben Langmuir984e1df2014-03-19 20:23:34 +00001304 return loadModuleMapFile(Dir, IsSystem, IsFramework);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001305
Douglas Gregor80b69042011-11-12 00:22:19 +00001306 return LMM_NoDirectory;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001307}
1308
Douglas Gregor80b69042011-11-12 00:22:19 +00001309HeaderSearch::LoadModuleMapResult
Ben Langmuir984e1df2014-03-19 20:23:34 +00001310HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1311 bool IsFramework) {
1312 auto KnownDir = DirectoryHasModuleMap.find(Dir);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001313 if (KnownDir != DirectoryHasModuleMap.end())
Richard Smith9887d792014-10-17 01:42:53 +00001314 return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregore7ab3662011-12-07 02:23:45 +00001315
Ben Langmuir984e1df2014-03-19 20:23:34 +00001316 if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001317 LoadModuleMapResult Result =
1318 loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
Ben Langmuir984e1df2014-03-19 20:23:34 +00001319 // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1320 // E.g. Foo.framework/Modules/module.modulemap
1321 // ^Dir ^ModuleMapFile
1322 if (Result == LMM_NewlyLoaded)
1323 DirectoryHasModuleMap[Dir] = true;
Richard Smith9887d792014-10-17 01:42:53 +00001324 else if (Result == LMM_InvalidModuleMap)
1325 DirectoryHasModuleMap[Dir] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001326 return Result;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001327 }
Douglas Gregor80b69042011-11-12 00:22:19 +00001328 return LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001329}
Douglas Gregor718292f2011-11-11 19:10:28 +00001330
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001331void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
Douglas Gregor07f43572012-01-29 18:15:03 +00001332 Modules.clear();
Daniel Jasper21a0f552014-11-25 09:45:48 +00001333
Richard Smith47972af2015-06-16 00:08:24 +00001334 if (HSOpts->ImplicitModuleMaps) {
Daniel Jasper21a0f552014-11-25 09:45:48 +00001335 // Load module maps for each of the header search directories.
1336 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1337 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1338 if (SearchDirs[Idx].isFramework()) {
1339 std::error_code EC;
1340 SmallString<128> DirNative;
1341 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1342 DirNative);
1343
1344 // Search each of the ".framework" directories to load them as modules.
Yaron Keren92e1b622015-03-18 10:17:07 +00001345 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001346 Dir != DirEnd && !EC; Dir.increment(EC)) {
1347 if (llvm::sys::path::extension(Dir->path()) != ".framework")
1348 continue;
1349
1350 const DirectoryEntry *FrameworkDir =
1351 FileMgr.getDirectory(Dir->path());
1352 if (!FrameworkDir)
1353 continue;
1354
1355 // Load this framework module.
1356 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1357 IsSystem);
1358 }
1359 continue;
Douglas Gregor07f43572012-01-29 18:15:03 +00001360 }
Daniel Jasper21a0f552014-11-25 09:45:48 +00001361
1362 // FIXME: Deal with header maps.
1363 if (SearchDirs[Idx].isHeaderMap())
1364 continue;
1365
1366 // Try to load a module map file for the search directory.
1367 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1368 /*IsFramework*/ false);
1369
1370 // Try to load module map files for immediate subdirectories of this
1371 // search directory.
1372 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
Douglas Gregor07f43572012-01-29 18:15:03 +00001373 }
Douglas Gregor07f43572012-01-29 18:15:03 +00001374 }
Daniel Jasper21a0f552014-11-25 09:45:48 +00001375
Douglas Gregor07f43572012-01-29 18:15:03 +00001376 // Populate the list of modules.
1377 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1378 MEnd = ModMap.module_end();
1379 M != MEnd; ++M) {
1380 Modules.push_back(M->getValue());
1381 }
1382}
Douglas Gregor0339a642013-03-21 01:08:50 +00001383
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001384void HeaderSearch::loadTopLevelSystemModules() {
Richard Smith47972af2015-06-16 00:08:24 +00001385 if (!HSOpts->ImplicitModuleMaps)
Daniel Jasper21a0f552014-11-25 09:45:48 +00001386 return;
1387
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001388 // Load module maps for each of the header search directories.
1389 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregor299787f2013-11-01 23:08:38 +00001390 // We only care about normal header directories.
1391 if (!SearchDirs[Idx].isNormalDir()) {
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001392 continue;
1393 }
1394
1395 // Try to load a module map file for the search directory.
Douglas Gregor963c5532013-06-21 16:28:10 +00001396 loadModuleMapFile(SearchDirs[Idx].getDir(),
Ben Langmuir984e1df2014-03-19 20:23:34 +00001397 SearchDirs[Idx].isSystemHeaderDirectory(),
1398 SearchDirs[Idx].isFramework());
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001399 }
1400}
1401
Douglas Gregor0339a642013-03-21 01:08:50 +00001402void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
Richard Smith47972af2015-06-16 00:08:24 +00001403 assert(HSOpts->ImplicitModuleMaps &&
Daniel Jasper21a0f552014-11-25 09:45:48 +00001404 "Should not be loading subdirectory module maps");
1405
Douglas Gregor0339a642013-03-21 01:08:50 +00001406 if (SearchDir.haveSearchedAllModuleMaps())
1407 return;
Rafael Espindolac0809172014-06-12 14:02:15 +00001408
1409 std::error_code EC;
Douglas Gregor0339a642013-03-21 01:08:50 +00001410 SmallString<128> DirNative;
1411 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
Yaron Keren92e1b622015-03-18 10:17:07 +00001412 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
Douglas Gregor0339a642013-03-21 01:08:50 +00001413 Dir != DirEnd && !EC; Dir.increment(EC)) {
Ben Langmuir1f6a32b2015-02-24 04:58:15 +00001414 bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1415 if (IsFramework == SearchDir.isFramework())
1416 loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1417 SearchDir.isFramework());
Douglas Gregor0339a642013-03-21 01:08:50 +00001418 }
1419
1420 SearchDir.setSearchedAllModuleMaps(true);
1421}