blob: 2d005dd2e1f07c437a9645fe5948629533f364ab [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 Prantl793038d32016-01-12 21:01:56 +0000156 llvm::hash_combine(DirName.lower(), FileName.lower());
Richard Smith54cc3c22014-12-11 20:50:24 +0000157
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000158 SmallString<128> HashStr;
Richard Smith54cc3c22014-12-11 20:50:24 +0000159 llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
Yaron Keren92e1b622015-03-18 10:17:07 +0000160 llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000161 }
Douglas Gregor279a6c32012-01-29 17:08:11 +0000162 return Result.str().str();
163}
164
165Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000166 // Look in the module map to determine if there is a module by this name.
Douglas Gregor279a6c32012-01-29 17:08:11 +0000167 Module *Module = ModMap.findModule(ModuleName);
Richard Smith47972af2015-06-16 00:08:24 +0000168 if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
Douglas Gregor279a6c32012-01-29 17:08:11 +0000169 return Module;
170
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000171 // Look through the various header search paths to load any available module
Douglas Gregor279a6c32012-01-29 17:08:11 +0000172 // maps, searching for a module map that describes this module.
173 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
174 if (SearchDirs[Idx].isFramework()) {
175 // Search for or infer a module map for a framework.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000176 SmallString<128> FrameworkDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000177 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
178 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
179 if (const DirectoryEntry *FrameworkDir
180 = FileMgr.getDirectory(FrameworkDirName)) {
181 bool IsSystem
182 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
183 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000184 if (Module)
185 break;
186 }
Douglas Gregor279a6c32012-01-29 17:08:11 +0000187 }
188
189 // FIXME: Figure out how header maps and module maps will work together.
190
191 // Only deal with normal search directories.
192 if (!SearchDirs[Idx].isNormalDir())
193 continue;
Douglas Gregor963c5532013-06-21 16:28:10 +0000194
195 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
Douglas Gregor279a6c32012-01-29 17:08:11 +0000196 // Search for a module map file in this directory.
Ben Langmuir984e1df2014-03-19 20:23:34 +0000197 if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
198 /*IsFramework*/false) == LMM_NewlyLoaded) {
Douglas Gregor279a6c32012-01-29 17:08:11 +0000199 // We just loaded a module map file; check whether the module is
200 // available now.
201 Module = ModMap.findModule(ModuleName);
202 if (Module)
203 break;
204 }
205
206 // Search for a module map in a subdirectory with the same name as the
207 // module.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000208 SmallString<128> NestedModuleMapDirName;
Douglas Gregor279a6c32012-01-29 17:08:11 +0000209 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
210 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
Ben Langmuir984e1df2014-03-19 20:23:34 +0000211 if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
212 /*IsFramework*/false) == LMM_NewlyLoaded){
Douglas Gregor279a6c32012-01-29 17:08:11 +0000213 // If we just loaded a module map file, look for the module again.
214 Module = ModMap.findModule(ModuleName);
215 if (Module)
216 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000217 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000218
219 // If we've already performed the exhaustive search for module maps in this
220 // search directory, don't do it again.
221 if (SearchDirs[Idx].haveSearchedAllModuleMaps())
222 continue;
223
224 // Load all module maps in the immediate subdirectories of this search
225 // directory.
226 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
227
228 // Look again for the module.
229 Module = ModMap.findModule(ModuleName);
230 if (Module)
231 break;
Douglas Gregoraf28ec82011-11-12 00:05:07 +0000232 }
Douglas Gregor0339a642013-03-21 01:08:50 +0000233
Douglas Gregor279a6c32012-01-29 17:08:11 +0000234 return Module;
Douglas Gregor1e44e022011-09-12 20:41:59 +0000235}
236
Chris Lattnerf62f7582007-12-17 07:52:39 +0000237//===----------------------------------------------------------------------===//
238// File lookup within a DirectoryLookup scope
239//===----------------------------------------------------------------------===//
240
Chris Lattner8d720d02007-12-17 17:57:27 +0000241/// getName - Return the directory or filename corresponding to this lookup
242/// object.
243const char *DirectoryLookup::getName() const {
244 if (isNormalDir())
245 return getDir()->getName();
246 if (isFramework())
247 return getFrameworkDir()->getName();
248 assert(isHeaderMap() && "Unknown DirectoryLookup");
249 return getHeaderMap()->getFileName();
250}
251
Richard Smith3d5b48c2015-10-16 21:42:56 +0000252const FileEntry *HeaderSearch::getFileAndSuggestModule(
253 StringRef FileName, const DirectoryEntry *Dir, bool IsSystemHeaderDir,
254 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
Richard Smith8c71eba2014-03-05 20:51:45 +0000255 // If we have a module map that might map this header, load it and
256 // check whether we'll have a suggestion for a module.
Richard Smith3d5b48c2015-10-16 21:42:56 +0000257 const FileEntry *File = getFileMgr().getFile(FileName, /*OpenFile=*/true);
Reid Klecknerafb9aae2015-10-20 18:45:57 +0000258 if (!File)
259 return nullptr;
Richard Smith8c71eba2014-03-05 20:51:45 +0000260
Richard Smith3d5b48c2015-10-16 21:42:56 +0000261 // If there is a module that corresponds to this header, suggest it.
262 if (!findUsableModuleForHeader(File, Dir ? Dir : File->getDir(),
263 RequestingModule, SuggestedModule,
264 IsSystemHeaderDir))
265 return nullptr;
Richard Smith8c71eba2014-03-05 20:51:45 +0000266
Richard Smith3d5b48c2015-10-16 21:42:56 +0000267 return File;
Richard Smith8c71eba2014-03-05 20:51:45 +0000268}
Chris Lattner8d720d02007-12-17 17:57:27 +0000269
Chris Lattnerf62f7582007-12-17 07:52:39 +0000270/// LookupFile - Lookup the specified file in this search path, returning it
271/// if it exists or returning null if not.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000272const FileEntry *DirectoryLookup::LookupFile(
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000273 StringRef &Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000274 HeaderSearch &HS,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000275 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000276 SmallVectorImpl<char> *RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000277 Module *RequestingModule,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000278 ModuleMap::KnownHeader *SuggestedModule,
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000279 bool &InUserSpecifiedSystemFramework,
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000280 bool &HasBeenMapped,
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000281 SmallVectorImpl<char> &MappedName) const {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000282 InUserSpecifiedSystemFramework = false;
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000283 HasBeenMapped = false;
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000284
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000285 SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000286 if (isNormalDir()) {
287 // Concatenate the requested file onto the directory.
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000288 TmpDir = getDir()->getName();
289 llvm::sys::path::append(TmpDir, Filename);
Craig Topperd2d442c2014-05-17 23:10:59 +0000290 if (SearchPath) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000291 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000292 SearchPath->clear();
293 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
294 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000295 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000296 RelativePath->clear();
297 RelativePath->append(Filename.begin(), Filename.end());
298 }
Richard Smith8c71eba2014-03-05 20:51:45 +0000299
Richard Smith3d5b48c2015-10-16 21:42:56 +0000300 return HS.getFileAndSuggestModule(TmpDir, getDir(),
301 isSystemHeaderDirectory(),
302 RequestingModule, SuggestedModule);
Chris Lattner712e3872007-12-17 08:13:48 +0000303 }
Mike Stump11289f42009-09-09 15:08:12 +0000304
Chris Lattner712e3872007-12-17 08:13:48 +0000305 if (isFramework())
Douglas Gregor97eec242011-09-15 22:00:41 +0000306 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000307 RequestingModule, SuggestedModule,
308 InUserSpecifiedSystemFramework);
Mike Stump11289f42009-09-09 15:08:12 +0000309
Chris Lattner44bd21b2007-12-17 08:17:39 +0000310 assert(isHeaderMap() && "Unknown directory lookup");
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000311 const HeaderMap *HM = getHeaderMap();
312 SmallString<1024> Path;
313 StringRef Dest = HM->lookupFilename(Filename, Path);
314 if (Dest.empty())
Craig Topperd2d442c2014-05-17 23:10:59 +0000315 return nullptr;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000316
317 const FileEntry *Result;
318
319 // Check if the headermap maps the filename to a framework include
320 // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
321 // framework include.
322 if (llvm::sys::path::is_relative(Dest)) {
323 MappedName.clear();
324 MappedName.append(Dest.begin(), Dest.end());
325 Filename = StringRef(MappedName.begin(), MappedName.size());
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000326 HasBeenMapped = true;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000327 Result = HM->LookupFile(Filename, HS.getFileMgr());
328
329 } else {
330 Result = HS.getFileMgr().getFile(Dest);
331 }
332
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000333 if (Result) {
Craig Topperd2d442c2014-05-17 23:10:59 +0000334 if (SearchPath) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000335 StringRef SearchPathRef(getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000336 SearchPath->clear();
337 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
338 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000339 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000340 RelativePath->clear();
341 RelativePath->append(Filename.begin(), Filename.end());
342 }
343 }
344 return Result;
Chris Lattnerf62f7582007-12-17 07:52:39 +0000345}
346
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000347/// \brief Given a framework directory, find the top-most framework directory.
348///
349/// \param FileMgr The file manager to use for directory lookups.
350/// \param DirName The name of the framework directory.
351/// \param SubmodulePath Will be populated with the submodule path from the
352/// returned top-level module to the originally named framework.
353static const DirectoryEntry *
354getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
355 SmallVectorImpl<std::string> &SubmodulePath) {
356 assert(llvm::sys::path::extension(DirName) == ".framework" &&
357 "Not a framework directory");
358
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000359 // Note: as an egregious but useful hack we use the real path here, because
360 // frameworks moving between top-level frameworks to embedded frameworks tend
361 // to be symlinked, and we base the logical structure of modules on the
362 // physical layout. In particular, we need to deal with crazy includes like
363 //
364 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
365 //
366 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
367 // which one should access with, e.g.,
368 //
369 // #include <Bar/Wibble.h>
370 //
371 // Similar issues occur when a top-level framework has moved into an
372 // embedded framework.
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000373 const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
Douglas Gregore00c8b22013-01-26 00:55:12 +0000374 DirName = FileMgr.getCanonicalName(TopFrameworkDir);
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000375 do {
376 // Get the parent directory name.
377 DirName = llvm::sys::path::parent_path(DirName);
378 if (DirName.empty())
379 break;
380
381 // Determine whether this directory exists.
382 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
383 if (!Dir)
384 break;
385
386 // If this is a framework directory, then we're a subframework of this
387 // framework.
388 if (llvm::sys::path::extension(DirName) == ".framework") {
389 SubmodulePath.push_back(llvm::sys::path::stem(DirName));
390 TopFrameworkDir = Dir;
391 }
392 } while (true);
393
394 return TopFrameworkDir;
395}
Chris Lattnerf62f7582007-12-17 07:52:39 +0000396
Chris Lattner712e3872007-12-17 08:13:48 +0000397/// DoFrameworkLookup - Do a lookup of the specified file in the current
398/// DirectoryLookup, which is a framework directory.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000399const FileEntry *DirectoryLookup::DoFrameworkLookup(
Richard Smith3d5b48c2015-10-16 21:42:56 +0000400 StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
401 SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000402 ModuleMap::KnownHeader *SuggestedModule,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000403 bool &InUserSpecifiedSystemFramework) const {
Chris Lattner712e3872007-12-17 08:13:48 +0000404 FileManager &FileMgr = HS.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000406 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000407 size_t SlashPos = Filename.find('/');
Craig Topperd2d442c2014-05-17 23:10:59 +0000408 if (SlashPos == StringRef::npos) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattner712e3872007-12-17 08:13:48 +0000410 // Find out if this is the home for the specified framework, by checking
Daniel Dunbar17138612012-04-05 17:09:40 +0000411 // HeaderSearch. Possible answers are yes/no and unknown.
412 HeaderSearch::FrameworkCacheEntry &CacheEntry =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000413 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000414
Chris Lattner712e3872007-12-17 08:13:48 +0000415 // If it is known and in some other directory, fail.
Daniel Dunbar17138612012-04-05 17:09:40 +0000416 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
Craig Topperd2d442c2014-05-17 23:10:59 +0000417 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000418
Chris Lattner712e3872007-12-17 08:13:48 +0000419 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000421 // FrameworkName = "/System/Library/Frameworks/"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000422 SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000423 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000424 if (FrameworkName.empty() || FrameworkName.back() != '/')
425 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000427 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor56c64012011-11-17 01:41:17 +0000428 StringRef ModuleName(Filename.begin(), SlashPos);
429 FrameworkName += ModuleName;
Mike Stump11289f42009-09-09 15:08:12 +0000430
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000431 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
432 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000433
Daniel Dunbar17138612012-04-05 17:09:40 +0000434 // If the cache entry was unresolved, populate it now.
Craig Topperd2d442c2014-05-17 23:10:59 +0000435 if (!CacheEntry.Directory) {
Chris Lattner712e3872007-12-17 08:13:48 +0000436 HS.IncrementFrameworkLookupCount();
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattner5ed76da2006-10-22 07:24:13 +0000438 // If the framework dir doesn't exist, we fail.
Yaron Keren92e1b622015-03-18 10:17:07 +0000439 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
Craig Topperd2d442c2014-05-17 23:10:59 +0000440 if (!Dir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000441
Chris Lattner5ed76da2006-10-22 07:24:13 +0000442 // Otherwise, if it does, remember that this is the right direntry for this
443 // framework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000444 CacheEntry.Directory = getFrameworkDir();
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000445
446 // If this is a user search directory, check if the framework has been
447 // user-specified as a system framework.
448 if (getDirCharacteristic() == SrcMgr::C_User) {
449 SmallString<1024> SystemFrameworkMarker(FrameworkName);
450 SystemFrameworkMarker += ".system_framework";
Yaron Keren92e1b622015-03-18 10:17:07 +0000451 if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000452 CacheEntry.IsUserSpecifiedSystemFramework = true;
453 }
454 }
Chris Lattner5ed76da2006-10-22 07:24:13 +0000455 }
Mike Stump11289f42009-09-09 15:08:12 +0000456
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000457 // Set the 'user-specified system framework' flag.
458 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
459
Craig Topperd2d442c2014-05-17 23:10:59 +0000460 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000461 RelativePath->clear();
462 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
463 }
Douglas Gregor56c64012011-11-17 01:41:17 +0000464
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000465 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000466 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000468 FrameworkName += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000469
Craig Topperd2d442c2014-05-17 23:10:59 +0000470 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000471 SearchPath->clear();
472 // Without trailing '/'.
473 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
474 }
475
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000476 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000477 const FileEntry *FE = FileMgr.getFile(FrameworkName,
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000478 /*openFile=*/!SuggestedModule);
479 if (!FE) {
480 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
481 const char *Private = "Private";
482 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
483 Private+strlen(Private));
Craig Topperd2d442c2014-05-17 23:10:59 +0000484 if (SearchPath)
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000485 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
486 Private+strlen(Private));
487
Yaron Keren92e1b622015-03-18 10:17:07 +0000488 FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000491 // If we found the header and are allowed to suggest a module, do so now.
492 if (FE && SuggestedModule) {
493 // Find the framework in which this header occurs.
Ben Langmuiref914b82014-05-15 16:20:33 +0000494 StringRef FrameworkPath = FE->getDir()->getName();
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000495 bool FoundFramework = false;
496 do {
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000497 // Determine whether this directory exists.
498 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
499 if (!Dir)
500 break;
501
502 // If this is a framework directory, then we're a subframework of this
503 // framework.
504 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
505 FoundFramework = true;
506 break;
507 }
Ben Langmuiref914b82014-05-15 16:20:33 +0000508
509 // Get the parent directory name.
510 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
511 if (FrameworkPath.empty())
512 break;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000513 } while (true);
514
Richard Smith3d5b48c2015-10-16 21:42:56 +0000515 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000516 if (FoundFramework) {
Richard Smith3d5b48c2015-10-16 21:42:56 +0000517 if (!HS.findUsableModuleForFrameworkHeader(
518 FE, FrameworkPath, RequestingModule, SuggestedModule, IsSystem))
519 return nullptr;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000520 } else {
Richard Smith3d5b48c2015-10-16 21:42:56 +0000521 if (!HS.findUsableModuleForHeader(FE, getDir(), RequestingModule,
522 SuggestedModule, IsSystem))
523 return nullptr;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000524 }
525 }
Douglas Gregor97eec242011-09-15 22:00:41 +0000526 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000527}
528
Douglas Gregor89929282012-01-30 06:01:29 +0000529void HeaderSearch::setTarget(const TargetInfo &Target) {
530 ModMap.setTarget(Target);
531}
532
Chris Lattnerf62f7582007-12-17 07:52:39 +0000533
Chris Lattner712e3872007-12-17 08:13:48 +0000534//===----------------------------------------------------------------------===//
535// Header File Location.
536//===----------------------------------------------------------------------===//
537
Reid Klecknera97d4c02014-02-18 23:49:24 +0000538/// \brief Return true with a diagnostic if the file that MSVC would have found
539/// fails to match the one that Clang would have found with MSVC header search
540/// disabled.
541static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
542 const FileEntry *MSFE, const FileEntry *FE,
543 SourceLocation IncludeLoc) {
544 if (MSFE && FE != MSFE) {
545 Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
546 return true;
547 }
548 return false;
549}
Chris Lattner712e3872007-12-17 08:13:48 +0000550
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000551static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
552 assert(!Str.empty());
553 char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
554 std::copy(Str.begin(), Str.end(), CopyStr);
555 CopyStr[Str.size()] = '\0';
556 return CopyStr;
557}
558
James Dennettc07ab2c2012-06-20 00:56:32 +0000559/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000560/// return null on failure. isAngled indicates whether the file reference is
Will Wilson0fafd342013-12-27 19:46:16 +0000561/// for system \#include's or not (i.e. using <> instead of ""). Includers, if
562/// non-empty, indicates where the \#including file(s) are, in case a relative
563/// search is needed. Microsoft mode will pass all \#including files.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000564const FileEntry *HeaderSearch::LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000565 StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
566 const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000567 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
568 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000569 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
570 bool SkipCache) {
Douglas Gregor97eec242011-09-15 22:00:41 +0000571 if (SuggestedModule)
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000572 *SuggestedModule = ModuleMap::KnownHeader();
Douglas Gregor97eec242011-09-15 22:00:41 +0000573
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000574 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000575 if (llvm::sys::path::is_absolute(Filename)) {
Craig Topperd2d442c2014-05-17 23:10:59 +0000576 CurDir = nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000577
578 // If this was an #include_next "/absolute/file", fail.
Craig Topperd2d442c2014-05-17 23:10:59 +0000579 if (FromDir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000580
Craig Topperd2d442c2014-05-17 23:10:59 +0000581 if (SearchPath)
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000582 SearchPath->clear();
Craig Topperd2d442c2014-05-17 23:10:59 +0000583 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000584 RelativePath->clear();
585 RelativePath->append(Filename.begin(), Filename.end());
586 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000587 // Otherwise, just return the file.
Richard Smith3d5b48c2015-10-16 21:42:56 +0000588 return getFileAndSuggestModule(Filename, nullptr,
589 /*IsSystemHeaderDir*/false,
590 RequestingModule, SuggestedModule);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Reid Klecknera97d4c02014-02-18 23:49:24 +0000593 // This is the header that MSVC's header search would have found.
Craig Topperd2d442c2014-05-17 23:10:59 +0000594 const FileEntry *MSFE = nullptr;
Richard Smith8c71eba2014-03-05 20:51:45 +0000595 ModuleMap::KnownHeader MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000596
Douglas Gregor9f93e382011-07-28 04:45:53 +0000597 // Unless disabled, check to see if the file is in the #includer's
Will Wilson0fafd342013-12-27 19:46:16 +0000598 // directory. This cannot be based on CurDir, because each includer could be
599 // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
600 // include of "baz.h" should resolve to "whatever/foo/baz.h".
Chris Lattnerf62f7582007-12-17 07:52:39 +0000601 // This search is not done for <> headers.
Will Wilson0fafd342013-12-27 19:46:16 +0000602 if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
NAKAMURA Takumi9cb62642013-12-10 02:36:28 +0000603 SmallString<1024> TmpDir;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000604 bool First = true;
605 for (const auto &IncluderAndDir : Includers) {
606 const FileEntry *Includer = IncluderAndDir.first;
607
Will Wilson0fafd342013-12-27 19:46:16 +0000608 // Concatenate the requested file onto the directory.
Nikola Smiljaniccf385dc2015-05-08 06:02:37 +0000609 // FIXME: Portability. Filename concatenation should be in sys::Path.
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000610 TmpDir = IncluderAndDir.second->getName();
Nikola Smiljaniccf385dc2015-05-08 06:02:37 +0000611 TmpDir.push_back('/');
612 TmpDir.append(Filename.begin(), Filename.end());
Richard Smith8c71eba2014-03-05 20:51:45 +0000613
Richard Smith6f548ec2014-03-06 18:08:08 +0000614 // FIXME: We don't cache the result of getFileInfo across the call to
615 // getFileAndSuggestModule, because it's a reference to an element of
616 // a container that could be reallocated across this call.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000617 //
618 // FIXME: If we have no includer, that means we're processing a #include
619 // from a module build. We should treat this as a system header if we're
620 // building a [system] module.
Richard Smith6f548ec2014-03-06 18:08:08 +0000621 bool IncluderIsSystemHeader =
Richard Smith3c1a41a2014-12-02 00:08:08 +0000622 Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000623 if (const FileEntry *FE = getFileAndSuggestModule(
Richard Smith3d5b48c2015-10-16 21:42:56 +0000624 TmpDir, IncluderAndDir.second, IncluderIsSystemHeader,
625 RequestingModule, SuggestedModule)) {
Richard Smith3c1a41a2014-12-02 00:08:08 +0000626 if (!Includer) {
627 assert(First && "only first includer can have no file");
628 return FE;
629 }
630
Will Wilson0fafd342013-12-27 19:46:16 +0000631 // Leave CurDir unset.
632 // This file is a system header or C++ unfriendly if the old file is.
633 //
634 // Note that we only use one of FromHFI/ToHFI at once, due to potential
635 // reallocation of the underlying vector potentially making the first
636 // reference binding dangling.
Richard Smith6f548ec2014-03-06 18:08:08 +0000637 HeaderFileInfo &FromHFI = getFileInfo(Includer);
Will Wilson0fafd342013-12-27 19:46:16 +0000638 unsigned DirInfo = FromHFI.DirInfo;
639 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
640 StringRef Framework = FromHFI.Framework;
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000641
Will Wilson0fafd342013-12-27 19:46:16 +0000642 HeaderFileInfo &ToHFI = getFileInfo(FE);
643 ToHFI.DirInfo = DirInfo;
644 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
645 ToHFI.Framework = Framework;
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000646
Craig Topperd2d442c2014-05-17 23:10:59 +0000647 if (SearchPath) {
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000648 StringRef SearchPathRef(IncluderAndDir.second->getName());
Will Wilson0fafd342013-12-27 19:46:16 +0000649 SearchPath->clear();
650 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
651 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000652 if (RelativePath) {
Will Wilson0fafd342013-12-27 19:46:16 +0000653 RelativePath->clear();
654 RelativePath->append(Filename.begin(), Filename.end());
655 }
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000656 if (First)
Reid Klecknera97d4c02014-02-18 23:49:24 +0000657 return FE;
658
659 // Otherwise, we found the path via MSVC header search rules. If
660 // -Wmsvc-include is enabled, we have to keep searching to see if we
661 // would've found this header in -I or -isystem directories.
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000662 if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
Reid Klecknera97d4c02014-02-18 23:49:24 +0000663 return FE;
664 } else {
665 MSFE = FE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000666 if (SuggestedModule) {
667 MSSuggestedModule = *SuggestedModule;
668 *SuggestedModule = ModuleMap::KnownHeader();
669 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000670 break;
671 }
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000672 }
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000673 First = false;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000674 }
675 }
Mike Stump11289f42009-09-09 15:08:12 +0000676
Craig Topperd2d442c2014-05-17 23:10:59 +0000677 CurDir = nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000678
679 // If this is a system #include, ignore the user #include locs.
Nico Weber3b1d1212011-05-24 04:31:14 +0000680 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000682 // If this is a #include_next request, start searching after the directory the
683 // file was found in.
684 if (FromDir)
685 i = FromDir-&SearchDirs[0];
Mike Stump11289f42009-09-09 15:08:12 +0000686
Chris Lattnerd4275422007-07-22 07:28:00 +0000687 // Cache all of the lookups performed by this method. Many headers are
688 // multiply included, and the "pragma once" optimization prevents them from
689 // being relex/pp'd, but they would still have to search through a
690 // (potentially huge) series of SearchDirs to find it.
David Blaikie13156b62014-11-19 03:06:06 +0000691 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
Chris Lattnerd4275422007-07-22 07:28:00 +0000692
693 // If the entry has been previously looked up, the first value will be
694 // non-zero. If the value is equal to i (the start point of our search), then
695 // this is a matching hit.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000696 if (!SkipCache && CacheLookup.StartIdx == i+1) {
Chris Lattnerd4275422007-07-22 07:28:00 +0000697 // Skip querying potentially lots of directories for this lookup.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000698 i = CacheLookup.HitIdx;
699 if (CacheLookup.MappedName)
700 Filename = CacheLookup.MappedName;
Chris Lattnerd4275422007-07-22 07:28:00 +0000701 } else {
702 // Otherwise, this is the first query, or the previous query didn't match
703 // our search start. We will fill in our found location below, so prime the
704 // start point value.
Argyrios Kyrtzidis7bd78a92014-03-29 03:22:54 +0000705 CacheLookup.reset(/*StartIdx=*/i+1);
Chris Lattnerd4275422007-07-22 07:28:00 +0000706 }
Mike Stump11289f42009-09-09 15:08:12 +0000707
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000708 SmallString<64> MappedName;
709
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000710 // Check each directory in sequence to see if it contains this file.
711 for (; i != SearchDirs.size(); ++i) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000712 bool InUserSpecifiedSystemFramework = false;
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000713 bool HasBeenMapped = false;
Richard Smith3d5b48c2015-10-16 21:42:56 +0000714 const FileEntry *FE = SearchDirs[i].LookupFile(
715 Filename, *this, SearchPath, RelativePath, RequestingModule,
716 SuggestedModule, InUserSpecifiedSystemFramework, HasBeenMapped,
717 MappedName);
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000718 if (HasBeenMapped) {
719 CacheLookup.MappedName =
720 copyString(Filename, LookupFileCache.getAllocator());
721 }
Chris Lattner712e3872007-12-17 08:13:48 +0000722 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000723
Chris Lattner712e3872007-12-17 08:13:48 +0000724 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000725
Chris Lattner712e3872007-12-17 08:13:48 +0000726 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor9f93e382011-07-28 04:45:53 +0000727 HeaderFileInfo &HFI = getFileInfo(FE);
728 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000729
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000730 // If the directory characteristic is User but this framework was
731 // user-specified to be treated as a system framework, promote the
732 // characteristic.
733 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
734 HFI.DirInfo = SrcMgr::C_System;
735
Richard Smith8acadcb2012-06-13 20:27:03 +0000736 // If the filename matches a known system header prefix, override
737 // whether the file is a system header.
Richard Trieu871f5f32012-06-13 20:52:36 +0000738 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
739 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
740 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
Richard Smith8acadcb2012-06-13 20:27:03 +0000741 : SrcMgr::C_User;
742 break;
743 }
744 }
745
Douglas Gregor9f93e382011-07-28 04:45:53 +0000746 // If this file is found in a header map and uses the framework style of
747 // includes, then this header is part of a framework we're building.
748 if (CurDir->isIndexHeaderMap()) {
749 size_t SlashPos = Filename.find('/');
750 if (SlashPos != StringRef::npos) {
751 HFI.IndexHeaderMapHeader = 1;
752 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
753 SlashPos));
754 }
755 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000756
Richard Smith8c71eba2014-03-05 20:51:45 +0000757 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
758 if (SuggestedModule)
759 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000760 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000761 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000762
Chris Lattner712e3872007-12-17 08:13:48 +0000763 // Remember this location for the next lookup we do.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000764 CacheLookup.HitIdx = i;
Chris Lattner712e3872007-12-17 08:13:48 +0000765 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000766 }
Mike Stump11289f42009-09-09 15:08:12 +0000767
Douglas Gregord8575e12011-07-30 06:28:34 +0000768 // If we are including a file with a quoted include "foo.h" from inside
769 // a header in a framework that is currently being built, and we couldn't
770 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
771 // "Foo" is the name of the framework in which the including header was found.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000772 if (!Includers.empty() && Includers.front().first && !isAngled &&
Will Wilson0fafd342013-12-27 19:46:16 +0000773 Filename.find('/') == StringRef::npos) {
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000774 HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
Douglas Gregord8575e12011-07-30 06:28:34 +0000775 if (IncludingHFI.IndexHeaderMapHeader) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000776 SmallString<128> ScratchFilename;
Douglas Gregord8575e12011-07-30 06:28:34 +0000777 ScratchFilename += IncludingHFI.Framework;
778 ScratchFilename += '/';
779 ScratchFilename += Filename;
Will Wilson0fafd342013-12-27 19:46:16 +0000780
Richard Smith3d5b48c2015-10-16 21:42:56 +0000781 const FileEntry *FE =
782 LookupFile(ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir,
783 CurDir, Includers.front(), SearchPath, RelativePath,
784 RequestingModule, SuggestedModule);
Reid Klecknera97d4c02014-02-18 23:49:24 +0000785
Richard Smith8c71eba2014-03-05 20:51:45 +0000786 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
787 if (SuggestedModule)
788 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000789 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000790 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000791
David Blaikie3c8c46e2014-11-19 05:48:40 +0000792 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
David Blaikie13156b62014-11-19 03:06:06 +0000793 CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
Richard Smith8c71eba2014-03-05 20:51:45 +0000794 // FIXME: SuggestedModule.
Reid Klecknera97d4c02014-02-18 23:49:24 +0000795 return FE;
Douglas Gregord8575e12011-07-30 06:28:34 +0000796 }
797 }
798
Craig Topperd2d442c2014-05-17 23:10:59 +0000799 if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
Richard Smith8c71eba2014-03-05 20:51:45 +0000800 if (SuggestedModule)
801 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000802 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000803 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000804
Chris Lattnerd4275422007-07-22 07:28:00 +0000805 // Otherwise, didn't find it. Remember we didn't find this.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000806 CacheLookup.HitIdx = SearchDirs.size();
Craig Topperd2d442c2014-05-17 23:10:59 +0000807 return nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000808}
809
Chris Lattner63dd32b2006-10-20 04:42:40 +0000810/// LookupSubframeworkHeader - Look up a subframework for the specified
James Dennettc07ab2c2012-06-20 00:56:32 +0000811/// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
Chris Lattner63dd32b2006-10-20 04:42:40 +0000812/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
813/// is a subframework within Carbon.framework. If so, return the FileEntry
814/// for the designated file, otherwise return null.
815const FileEntry *HeaderSearch::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000816LookupSubframeworkHeader(StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000817 const FileEntry *ContextFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000818 SmallVectorImpl<char> *SearchPath,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000819 SmallVectorImpl<char> *RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000820 Module *RequestingModule,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000821 ModuleMap::KnownHeader *SuggestedModule) {
Chris Lattner12261882008-02-01 05:34:02 +0000822 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattner63dd32b2006-10-20 04:42:40 +0000824 // Framework names must have a '/' in the filename. Find it.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000825 // FIXME: Should we permit '\' on Windows?
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000826 size_t SlashPos = Filename.find('/');
Craig Topperd2d442c2014-05-17 23:10:59 +0000827 if (SlashPos == StringRef::npos) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000828
Chris Lattner63dd32b2006-10-20 04:42:40 +0000829 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000830 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattner63dd32b2006-10-20 04:42:40 +0000832 // If the context info wasn't a framework, couldn't be a subframework.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000833 const unsigned DotFrameworkLen = 10;
834 const char *FrameworkPos = strstr(ContextName, ".framework");
Craig Topperd2d442c2014-05-17 23:10:59 +0000835 if (FrameworkPos == nullptr ||
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000836 (FrameworkPos[DotFrameworkLen] != '/' &&
837 FrameworkPos[DotFrameworkLen] != '\\'))
Craig Topperd2d442c2014-05-17 23:10:59 +0000838 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000839
Daniel Dunbar17138612012-04-05 17:09:40 +0000840 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000841
Chris Lattner63dd32b2006-10-20 04:42:40 +0000842 // Append Frameworks/HIToolbox.framework/
843 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000844 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000845 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000846
David Blaikie13156b62014-11-19 03:06:06 +0000847 auto &CacheLookup =
848 *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
849 FrameworkCacheEntry())).first;
Mike Stump11289f42009-09-09 15:08:12 +0000850
Chris Lattner5ed76da2006-10-22 07:24:13 +0000851 // Some other location?
David Blaikie13156b62014-11-19 03:06:06 +0000852 if (CacheLookup.second.Directory &&
853 CacheLookup.first().size() == FrameworkName.size() &&
854 memcmp(CacheLookup.first().data(), &FrameworkName[0],
855 CacheLookup.first().size()) != 0)
Craig Topperd2d442c2014-05-17 23:10:59 +0000856 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000857
Chris Lattner5ed76da2006-10-22 07:24:13 +0000858 // Cache subframework.
David Blaikie13156b62014-11-19 03:06:06 +0000859 if (!CacheLookup.second.Directory) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000860 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000861
Chris Lattner5ed76da2006-10-22 07:24:13 +0000862 // If the framework dir doesn't exist, we fail.
Yaron Keren92e1b622015-03-18 10:17:07 +0000863 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
Craig Topperd2d442c2014-05-17 23:10:59 +0000864 if (!Dir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000865
Chris Lattner5ed76da2006-10-22 07:24:13 +0000866 // Otherwise, if it does, remember that this is the right direntry for this
867 // framework.
David Blaikie13156b62014-11-19 03:06:06 +0000868 CacheLookup.second.Directory = Dir;
Chris Lattner5ed76da2006-10-22 07:24:13 +0000869 }
Mike Stump11289f42009-09-09 15:08:12 +0000870
Craig Topperd2d442c2014-05-17 23:10:59 +0000871 const FileEntry *FE = nullptr;
Chris Lattner577377e2006-10-20 04:55:45 +0000872
Craig Topperd2d442c2014-05-17 23:10:59 +0000873 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000874 RelativePath->clear();
875 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
876 }
877
Chris Lattner63dd32b2006-10-20 04:42:40 +0000878 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000879 SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000880 HeadersFilename += "Headers/";
Craig Topperd2d442c2014-05-17 23:10:59 +0000881 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000882 SearchPath->clear();
883 // Without trailing '/'.
884 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
885 }
886
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000887 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000888 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
Mike Stump11289f42009-09-09 15:08:12 +0000889
Chris Lattner63dd32b2006-10-20 04:42:40 +0000890 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000891 HeadersFilename = FrameworkName;
892 HeadersFilename += "PrivateHeaders/";
Craig Topperd2d442c2014-05-17 23:10:59 +0000893 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000894 SearchPath->clear();
895 // Without trailing '/'.
896 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
897 }
898
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000899 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000900 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true)))
Craig Topperd2d442c2014-05-17 23:10:59 +0000901 return nullptr;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000902 }
Mike Stump11289f42009-09-09 15:08:12 +0000903
Chris Lattner577377e2006-10-20 04:55:45 +0000904 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000905 //
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000906 // Note that the temporary 'DirInfo' is required here, as either call to
907 // getFileInfo could resize the vector and we don't want to rely on order
908 // of evaluation.
909 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
910 getFileInfo(FE).DirInfo = DirInfo;
Douglas Gregorf5f94522013-02-08 00:10:48 +0000911
Richard Smith3d5b48c2015-10-16 21:42:56 +0000912 FrameworkName.pop_back(); // remove the trailing '/'
913 if (!findUsableModuleForFrameworkHeader(FE, FrameworkName, RequestingModule,
914 SuggestedModule, /*IsSystem*/ false))
915 return nullptr;
Douglas Gregorf5f94522013-02-08 00:10:48 +0000916
Chris Lattner577377e2006-10-20 04:55:45 +0000917 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000918}
919
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000920//===----------------------------------------------------------------------===//
921// File Info Management.
922//===----------------------------------------------------------------------===//
923
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000924/// \brief Merge the header file info provided by \p OtherHFI into the current
925/// header file info (\p HFI)
926static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
927 const HeaderFileInfo &OtherHFI) {
Richard Smithd8879c82015-08-24 21:59:32 +0000928 assert(OtherHFI.External && "expected to merge external HFI");
929
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000930 HFI.isImport |= OtherHFI.isImport;
931 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000932 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000933 HFI.NumIncludes += OtherHFI.NumIncludes;
Richard Smithd8879c82015-08-24 21:59:32 +0000934
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000935 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
936 HFI.ControllingMacro = OtherHFI.ControllingMacro;
937 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
938 }
Richard Smithd8879c82015-08-24 21:59:32 +0000939
940 HFI.DirInfo = OtherHFI.DirInfo;
941 HFI.External = (!HFI.IsValid || HFI.External);
942 HFI.IsValid = true;
943 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000944
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000945 if (HFI.Framework.empty())
946 HFI.Framework = OtherHFI.Framework;
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000947}
948
Steve Naroff3fa455a2009-04-24 20:03:17 +0000949/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000950/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000951HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000952 if (FE->getUID() >= FileInfo.size())
Richard Smith386bb072015-08-18 23:42:23 +0000953 FileInfo.resize(FE->getUID() + 1);
954
Richard Smithd8879c82015-08-24 21:59:32 +0000955 HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
Richard Smith386bb072015-08-18 23:42:23 +0000956 // FIXME: Use a generation count to check whether this is really up to date.
Richard Smithd8879c82015-08-24 21:59:32 +0000957 if (ExternalSource && !HFI->Resolved) {
958 HFI->Resolved = true;
959 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
960
961 HFI = &FileInfo[FE->getUID()];
962 if (ExternalHFI.External)
963 mergeHeaderFileInfo(*HFI, ExternalHFI);
Richard Smith386bb072015-08-18 23:42:23 +0000964 }
965
Richard Smithd8879c82015-08-24 21:59:32 +0000966 HFI->IsValid = true;
Richard Smith386bb072015-08-18 23:42:23 +0000967 // We have local information about this header file, so it's no longer
968 // strictly external.
Richard Smithd8879c82015-08-24 21:59:32 +0000969 HFI->External = false;
970 return *HFI;
Mike Stump11289f42009-09-09 15:08:12 +0000971}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000972
Richard Smith386bb072015-08-18 23:42:23 +0000973const HeaderFileInfo *
Richard Smithd8879c82015-08-24 21:59:32 +0000974HeaderSearch::getExistingFileInfo(const FileEntry *FE,
975 bool WantExternal) const {
Richard Smith386bb072015-08-18 23:42:23 +0000976 // If we have an external source, ensure we have the latest information.
977 // FIXME: Use a generation count to check whether this is really up to date.
Richard Smithd8879c82015-08-24 21:59:32 +0000978 HeaderFileInfo *HFI;
979 if (ExternalSource) {
980 if (FE->getUID() >= FileInfo.size()) {
981 if (!WantExternal)
982 return nullptr;
983 FileInfo.resize(FE->getUID() + 1);
Richard Smith386bb072015-08-18 23:42:23 +0000984 }
Richard Smithd8879c82015-08-24 21:59:32 +0000985
986 HFI = &FileInfo[FE->getUID()];
987 if (!WantExternal && (!HFI->IsValid || HFI->External))
988 return nullptr;
989 if (!HFI->Resolved) {
990 HFI->Resolved = true;
991 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
992
993 HFI = &FileInfo[FE->getUID()];
994 if (ExternalHFI.External)
995 mergeHeaderFileInfo(*HFI, ExternalHFI);
996 }
997 } else if (FE->getUID() >= FileInfo.size()) {
998 return nullptr;
999 } else {
1000 HFI = &FileInfo[FE->getUID()];
Ben Langmuird285c502014-03-13 16:46:36 +00001001 }
Richard Smith386bb072015-08-18 23:42:23 +00001002
Richard Smithd8879c82015-08-24 21:59:32 +00001003 if (!HFI->IsValid || (HFI->External && !WantExternal))
Richard Smith386bb072015-08-18 23:42:23 +00001004 return nullptr;
1005
Richard Smithd8879c82015-08-24 21:59:32 +00001006 return HFI;
Ben Langmuird285c502014-03-13 16:46:36 +00001007}
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.
Richard Smith386bb072015-08-18 23:42:23 +00001011 if (auto *HFI = getExistingFileInfo(File))
1012 return HFI->isPragmaOnce || HFI->isImport || HFI->ControllingMacro ||
1013 HFI->ControllingMacroID;
1014 return false;
Douglas Gregor37aa4932011-05-04 00:14:37 +00001015}
1016
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001017void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001018 ModuleMap::ModuleHeaderRole Role,
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001019 bool isCompilingModuleHeader) {
Richard Smithd8879c82015-08-24 21:59:32 +00001020 bool isModularHeader = !(Role & ModuleMap::TextualHeader);
1021
1022 // Don't mark the file info as non-external if there's nothing to change.
1023 if (!isCompilingModuleHeader) {
1024 if (!isModularHeader)
1025 return;
1026 auto *HFI = getExistingFileInfo(FE);
1027 if (HFI && HFI->isModuleHeader)
1028 return;
1029 }
1030
Richard Smith386bb072015-08-18 23:42:23 +00001031 auto &HFI = getFileInfo(FE);
Richard Smithd8879c82015-08-24 21:59:32 +00001032 HFI.isModuleHeader |= isModularHeader;
Richard Smithe70dadd2015-07-10 22:27:17 +00001033 HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001034}
1035
Richard Smith20e883e2015-04-29 23:20:19 +00001036bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1037 const FileEntry *File,
Richard Smith035f6dc2015-07-01 01:51:38 +00001038 bool isImport, Module *M) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001039 ++NumIncluded; // Count # of attempted #includes.
1040
1041 // Get information about this file.
Steve Naroff3fa455a2009-04-24 20:03:17 +00001042 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump11289f42009-09-09 15:08:12 +00001043
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001044 // If this is a #import directive, check that we have not already imported
1045 // this header.
1046 if (isImport) {
1047 // If this has already been imported, don't import it again.
1048 FileInfo.isImport = true;
Mike Stump11289f42009-09-09 15:08:12 +00001049
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001050 // Has this already been #import'ed or #include'd?
1051 if (FileInfo.NumIncludes) return false;
1052 } else {
1053 // Otherwise, if this is a #include of a file that was previously #import'd
1054 // or if this is the second #include of a #pragma once file, ignore it.
1055 if (FileInfo.isImport)
1056 return false;
1057 }
Mike Stump11289f42009-09-09 15:08:12 +00001058
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001059 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1060 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump11289f42009-09-09 15:08:12 +00001061 if (const IdentifierInfo *ControllingMacro
Richard Smithe70dadd2015-07-10 22:27:17 +00001062 = FileInfo.getControllingMacro(ExternalLookup)) {
1063 // If the header corresponds to a module, check whether the macro is already
1064 // defined in that module rather than checking in the current set of visible
1065 // modules.
1066 if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M)
1067 : PP.isMacroDefined(ControllingMacro)) {
Douglas Gregor99734e72009-04-25 23:30:02 +00001068 ++NumMultiIncludeFileOptzn;
1069 return false;
1070 }
Richard Smithe70dadd2015-07-10 22:27:17 +00001071 }
Mike Stump11289f42009-09-09 15:08:12 +00001072
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001073 // Increment the number of times this file has been included.
1074 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +00001075
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001076 return true;
1077}
1078
Ted Kremenekfbcce6f2011-07-26 23:46:11 +00001079size_t HeaderSearch::getTotalMemory() const {
1080 return SearchDirs.capacity()
Ted Kremenekae63d102011-07-27 18:41:18 +00001081 + llvm::capacity_in_bytes(FileInfo)
1082 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekfbcce6f2011-07-26 23:46:11 +00001083 + LookupFileCache.getAllocator().getTotalMemory()
1084 + FrameworkMap.getAllocator().getTotalMemory();
1085}
Douglas Gregor9f93e382011-07-28 04:45:53 +00001086
1087StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
David Blaikie13156b62014-11-19 03:06:06 +00001088 return FrameworkNames.insert(Framework).first->first();
Douglas Gregor9f93e382011-07-28 04:45:53 +00001089}
Douglas Gregor718292f2011-11-11 19:10:28 +00001090
1091bool HeaderSearch::hasModuleMap(StringRef FileName,
Douglas Gregor963c5532013-06-21 16:28:10 +00001092 const DirectoryEntry *Root,
1093 bool IsSystem) {
Richard Smith47972af2015-06-16 00:08:24 +00001094 if (!HSOpts->ImplicitModuleMaps)
Argyrios Kyrtzidis9955dbc2013-12-12 16:08:33 +00001095 return false;
1096
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001097 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
Douglas Gregor718292f2011-11-11 19:10:28 +00001098
1099 StringRef DirName = FileName;
1100 do {
1101 // Get the parent directory name.
1102 DirName = llvm::sys::path::parent_path(DirName);
1103 if (DirName.empty())
1104 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001105
Douglas Gregor718292f2011-11-11 19:10:28 +00001106 // Determine whether this directory exists.
1107 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
1108 if (!Dir)
1109 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001110
Ben Langmuir984e1df2014-03-19 20:23:34 +00001111 // Try to load the module map file in this directory.
Richard Smith3c1a41a2014-12-02 00:08:08 +00001112 switch (loadModuleMapFile(Dir, IsSystem,
1113 llvm::sys::path::extension(Dir->getName()) ==
1114 ".framework")) {
Douglas Gregor80b69042011-11-12 00:22:19 +00001115 case LMM_NewlyLoaded:
1116 case LMM_AlreadyLoaded:
Daniel Jasperca9f7382013-09-24 09:27:13 +00001117 // Success. All of the directories we stepped through inherit this module
1118 // map file.
1119 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1120 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1121 return true;
Daniel Jasper97da9172013-10-22 08:09:47 +00001122
1123 case LMM_NoDirectory:
1124 case LMM_InvalidModuleMap:
1125 break;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001126 }
1127
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001128 // If we hit the top of our search, we're done.
1129 if (Dir == Root)
1130 return false;
1131
Douglas Gregor718292f2011-11-11 19:10:28 +00001132 // Keep track of all of the directories we checked, so we can mark them as
1133 // having module maps if we eventually do find a module map.
1134 FixUpDirectories.push_back(Dir);
1135 } while (true);
Douglas Gregor718292f2011-11-11 19:10:28 +00001136}
1137
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001138ModuleMap::KnownHeader
1139HeaderSearch::findModuleForHeader(const FileEntry *File) const {
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001140 if (ExternalSource) {
1141 // Make sure the external source has handled header info about this file,
1142 // which includes whether the file is part of a module.
Richard Smith386bb072015-08-18 23:42:23 +00001143 (void)getExistingFileInfo(File);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001144 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001145 return ModMap.findModuleForHeader(File);
Douglas Gregor718292f2011-11-11 19:10:28 +00001146}
1147
Richard Smith3d5b48c2015-10-16 21:42:56 +00001148bool HeaderSearch::findUsableModuleForHeader(
1149 const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
1150 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1151 if (File && SuggestedModule) {
1152 // If there is a module that corresponds to this header, suggest it.
1153 hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
1154 *SuggestedModule = findModuleForHeader(File);
1155 }
1156 return true;
1157}
1158
1159bool HeaderSearch::findUsableModuleForFrameworkHeader(
1160 const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
1161 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1162 // If we're supposed to suggest a module, look for one now.
1163 if (SuggestedModule) {
1164 // Find the top-level framework based on this framework.
1165 SmallVector<std::string, 4> SubmodulePath;
1166 const DirectoryEntry *TopFrameworkDir
1167 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1168
1169 // Determine the name of the top-level framework.
1170 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1171
1172 // Load this framework module. If that succeeds, find the suggested module
1173 // for this header, if any.
1174 loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1175
1176 // FIXME: This can find a module not part of ModuleName, which is
1177 // important so that we're consistent about whether this header
1178 // corresponds to a module. Possibly we should lock down framework modules
1179 // so that this is not possible.
1180 *SuggestedModule = findModuleForHeader(File);
1181 }
1182 return true;
1183}
1184
Richard Smith9acb99e32014-12-10 03:09:48 +00001185static const FileEntry *getPrivateModuleMap(const FileEntry *File,
Ben Langmuir984e1df2014-03-19 20:23:34 +00001186 FileManager &FileMgr) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001187 StringRef Filename = llvm::sys::path::filename(File->getName());
1188 SmallString<128> PrivateFilename(File->getDir()->getName());
Ben Langmuir984e1df2014-03-19 20:23:34 +00001189 if (Filename == "module.map")
Douglas Gregor80306772011-12-07 21:25:07 +00001190 llvm::sys::path::append(PrivateFilename, "module_private.map");
Ben Langmuir984e1df2014-03-19 20:23:34 +00001191 else if (Filename == "module.modulemap")
1192 llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1193 else
1194 return nullptr;
1195 return FileMgr.getFile(PrivateFilename);
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001196}
1197
Ben Langmuir984e1df2014-03-19 20:23:34 +00001198bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001199 // Find the directory for the module. For frameworks, that may require going
1200 // up from the 'Modules' directory.
1201 const DirectoryEntry *Dir = nullptr;
1202 if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
1203 Dir = FileMgr.getDirectory(".");
1204 else {
1205 Dir = File->getDir();
1206 StringRef DirName(Dir->getName());
1207 if (llvm::sys::path::filename(DirName) == "Modules") {
1208 DirName = llvm::sys::path::parent_path(DirName);
1209 if (DirName.endswith(".framework"))
1210 Dir = FileMgr.getDirectory(DirName);
1211 // FIXME: This assert can fail if there's a race between the above check
1212 // and the removal of the directory.
1213 assert(Dir && "parent must exist");
1214 }
1215 }
1216
1217 switch (loadModuleMapFileImpl(File, IsSystem, Dir)) {
Ben Langmuir984e1df2014-03-19 20:23:34 +00001218 case LMM_AlreadyLoaded:
1219 case LMM_NewlyLoaded:
1220 return false;
1221 case LMM_NoDirectory:
1222 case LMM_InvalidModuleMap:
1223 return true;
1224 }
Aaron Ballmand8de5b62014-03-20 14:22:33 +00001225 llvm_unreachable("Unknown load module map result");
Ben Langmuir984e1df2014-03-19 20:23:34 +00001226}
1227
1228HeaderSearch::LoadModuleMapResult
Richard Smith9acb99e32014-12-10 03:09:48 +00001229HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1230 const DirectoryEntry *Dir) {
Ben Langmuir984e1df2014-03-19 20:23:34 +00001231 assert(File && "expected FileEntry");
1232
Richard Smith9887d792014-10-17 01:42:53 +00001233 // Check whether we've already loaded this module map, and mark it as being
1234 // loaded in case we recursively try to load it from itself.
1235 auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1236 if (!AddResult.second)
1237 return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001238
Richard Smith9acb99e32014-12-10 03:09:48 +00001239 if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) {
Richard Smith9887d792014-10-17 01:42:53 +00001240 LoadedModuleMaps[File] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001241 return LMM_InvalidModuleMap;
1242 }
1243
1244 // Try to load a corresponding private module map.
Richard Smith9acb99e32014-12-10 03:09:48 +00001245 if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1246 if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
Richard Smith9887d792014-10-17 01:42:53 +00001247 LoadedModuleMaps[File] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001248 return LMM_InvalidModuleMap;
1249 }
1250 }
1251
1252 // This directory has a module map.
Ben Langmuir984e1df2014-03-19 20:23:34 +00001253 return LMM_NewlyLoaded;
1254}
1255
1256const FileEntry *
1257HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
Richard Smith47972af2015-06-16 00:08:24 +00001258 if (!HSOpts->ImplicitModuleMaps)
Daniel Jasper21a0f552014-11-25 09:45:48 +00001259 return nullptr;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001260 // For frameworks, the preferred spelling is Modules/module.modulemap, but
1261 // module.map at the framework root is also accepted.
1262 SmallString<128> ModuleMapFileName(Dir->getName());
1263 if (IsFramework)
1264 llvm::sys::path::append(ModuleMapFileName, "Modules");
1265 llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1266 if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
1267 return F;
1268
1269 // Continue to allow module.map
1270 ModuleMapFileName = Dir->getName();
1271 llvm::sys::path::append(ModuleMapFileName, "module.map");
1272 return FileMgr.getFile(ModuleMapFileName);
1273}
1274
1275Module *HeaderSearch::loadFrameworkModule(StringRef Name,
Douglas Gregor279a6c32012-01-29 17:08:11 +00001276 const DirectoryEntry *Dir,
1277 bool IsSystem) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001278 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor56c64012011-11-17 01:41:17 +00001279 return Module;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001280
Douglas Gregor56c64012011-11-17 01:41:17 +00001281 // Try to load a module map file.
Ben Langmuir984e1df2014-03-19 20:23:34 +00001282 switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
Douglas Gregor56c64012011-11-17 01:41:17 +00001283 case LMM_InvalidModuleMap:
Ben Langmuira5254002015-07-02 13:19:48 +00001284 // Try to infer a module map from the framework directory.
1285 if (HSOpts->ImplicitModuleMaps)
1286 ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
Douglas Gregor56c64012011-11-17 01:41:17 +00001287 break;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001288
Douglas Gregor56c64012011-11-17 01:41:17 +00001289 case LMM_AlreadyLoaded:
1290 case LMM_NoDirectory:
Craig Topperd2d442c2014-05-17 23:10:59 +00001291 return nullptr;
1292
Douglas Gregor56c64012011-11-17 01:41:17 +00001293 case LMM_NewlyLoaded:
Ben Langmuira5254002015-07-02 13:19:48 +00001294 break;
Douglas Gregor56c64012011-11-17 01:41:17 +00001295 }
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001296
Ben Langmuira5254002015-07-02 13:19:48 +00001297 return ModMap.findModule(Name);
Douglas Gregor56c64012011-11-17 01:41:17 +00001298}
1299
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001300
Douglas Gregor80b69042011-11-12 00:22:19 +00001301HeaderSearch::LoadModuleMapResult
Ben Langmuir984e1df2014-03-19 20:23:34 +00001302HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1303 bool IsFramework) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001304 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
Ben Langmuir984e1df2014-03-19 20:23:34 +00001305 return loadModuleMapFile(Dir, IsSystem, IsFramework);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001306
Douglas Gregor80b69042011-11-12 00:22:19 +00001307 return LMM_NoDirectory;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001308}
1309
Douglas Gregor80b69042011-11-12 00:22:19 +00001310HeaderSearch::LoadModuleMapResult
Ben Langmuir984e1df2014-03-19 20:23:34 +00001311HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1312 bool IsFramework) {
1313 auto KnownDir = DirectoryHasModuleMap.find(Dir);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001314 if (KnownDir != DirectoryHasModuleMap.end())
Richard Smith9887d792014-10-17 01:42:53 +00001315 return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregore7ab3662011-12-07 02:23:45 +00001316
Ben Langmuir984e1df2014-03-19 20:23:34 +00001317 if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001318 LoadModuleMapResult Result =
1319 loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
Ben Langmuir984e1df2014-03-19 20:23:34 +00001320 // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1321 // E.g. Foo.framework/Modules/module.modulemap
1322 // ^Dir ^ModuleMapFile
1323 if (Result == LMM_NewlyLoaded)
1324 DirectoryHasModuleMap[Dir] = true;
Richard Smith9887d792014-10-17 01:42:53 +00001325 else if (Result == LMM_InvalidModuleMap)
1326 DirectoryHasModuleMap[Dir] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001327 return Result;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001328 }
Douglas Gregor80b69042011-11-12 00:22:19 +00001329 return LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001330}
Douglas Gregor718292f2011-11-11 19:10:28 +00001331
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001332void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
Douglas Gregor07f43572012-01-29 18:15:03 +00001333 Modules.clear();
Daniel Jasper21a0f552014-11-25 09:45:48 +00001334
Richard Smith47972af2015-06-16 00:08:24 +00001335 if (HSOpts->ImplicitModuleMaps) {
Daniel Jasper21a0f552014-11-25 09:45:48 +00001336 // Load module maps for each of the header search directories.
1337 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1338 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1339 if (SearchDirs[Idx].isFramework()) {
1340 std::error_code EC;
1341 SmallString<128> DirNative;
1342 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1343 DirNative);
1344
1345 // Search each of the ".framework" directories to load them as modules.
Yaron Keren92e1b622015-03-18 10:17:07 +00001346 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001347 Dir != DirEnd && !EC; Dir.increment(EC)) {
1348 if (llvm::sys::path::extension(Dir->path()) != ".framework")
1349 continue;
1350
1351 const DirectoryEntry *FrameworkDir =
1352 FileMgr.getDirectory(Dir->path());
1353 if (!FrameworkDir)
1354 continue;
1355
1356 // Load this framework module.
1357 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1358 IsSystem);
1359 }
1360 continue;
Douglas Gregor07f43572012-01-29 18:15:03 +00001361 }
Daniel Jasper21a0f552014-11-25 09:45:48 +00001362
1363 // FIXME: Deal with header maps.
1364 if (SearchDirs[Idx].isHeaderMap())
1365 continue;
1366
1367 // Try to load a module map file for the search directory.
1368 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1369 /*IsFramework*/ false);
1370
1371 // Try to load module map files for immediate subdirectories of this
1372 // search directory.
1373 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
Douglas Gregor07f43572012-01-29 18:15:03 +00001374 }
Douglas Gregor07f43572012-01-29 18:15:03 +00001375 }
Daniel Jasper21a0f552014-11-25 09:45:48 +00001376
Douglas Gregor07f43572012-01-29 18:15:03 +00001377 // Populate the list of modules.
1378 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1379 MEnd = ModMap.module_end();
1380 M != MEnd; ++M) {
1381 Modules.push_back(M->getValue());
1382 }
1383}
Douglas Gregor0339a642013-03-21 01:08:50 +00001384
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001385void HeaderSearch::loadTopLevelSystemModules() {
Richard Smith47972af2015-06-16 00:08:24 +00001386 if (!HSOpts->ImplicitModuleMaps)
Daniel Jasper21a0f552014-11-25 09:45:48 +00001387 return;
1388
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001389 // Load module maps for each of the header search directories.
1390 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregor299787f2013-11-01 23:08:38 +00001391 // We only care about normal header directories.
1392 if (!SearchDirs[Idx].isNormalDir()) {
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001393 continue;
1394 }
1395
1396 // Try to load a module map file for the search directory.
Douglas Gregor963c5532013-06-21 16:28:10 +00001397 loadModuleMapFile(SearchDirs[Idx].getDir(),
Ben Langmuir984e1df2014-03-19 20:23:34 +00001398 SearchDirs[Idx].isSystemHeaderDirectory(),
1399 SearchDirs[Idx].isFramework());
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001400 }
1401}
1402
Douglas Gregor0339a642013-03-21 01:08:50 +00001403void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
Richard Smith47972af2015-06-16 00:08:24 +00001404 assert(HSOpts->ImplicitModuleMaps &&
Daniel Jasper21a0f552014-11-25 09:45:48 +00001405 "Should not be loading subdirectory module maps");
1406
Douglas Gregor0339a642013-03-21 01:08:50 +00001407 if (SearchDir.haveSearchedAllModuleMaps())
1408 return;
Rafael Espindolac0809172014-06-12 14:02:15 +00001409
1410 std::error_code EC;
Douglas Gregor0339a642013-03-21 01:08:50 +00001411 SmallString<128> DirNative;
1412 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
Yaron Keren92e1b622015-03-18 10:17:07 +00001413 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
Douglas Gregor0339a642013-03-21 01:08:50 +00001414 Dir != DirEnd && !EC; Dir.increment(EC)) {
Ben Langmuir1f6a32b2015-02-24 04:58:15 +00001415 bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1416 if (IsFramework == SearchDir.isFramework())
1417 loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1418 SearchDir.isFramework());
Douglas Gregor0339a642013-03-21 01:08:50 +00001419 }
1420
1421 SearchDir.setSearchedAllModuleMaps(true);
1422}