blob: e5cc30e41c573554a9ea45e58c9037967d9848fd [file] [log] [blame]
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner59a9ebd2006-10-18 05:34:33 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DirectoryLookup and HeaderSearch interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner59a9ebd2006-10-18 05:34:33 +000014#include "clang/Lex/HeaderSearch.h"
Chris Lattneref6b1362007-10-07 08:58:51 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/IdentifierTable.h"
Richard Smith2aedca32015-07-01 02:29:35 +000017#include "clang/Lex/ExternalPreprocessorSource.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/HeaderMap.h"
19#include "clang/Lex/HeaderSearchOptions.h"
Will Wilson0fafd342013-12-27 19:46:16 +000020#include "clang/Lex/LexDiagnostic.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "clang/Lex/Lexer.h"
Richard Smith20e883e2015-04-29 23:20:19 +000022#include "clang/Lex/Preprocessor.h"
Ben Langmuirbeee15e2014-04-14 18:00:01 +000023#include "llvm/ADT/APInt.h"
24#include "llvm/ADT/Hashing.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenekae63d102011-07-27 18:41:18 +000026#include "llvm/Support/Capacity.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/Path.h"
Daniel Jasperba7f2f72013-09-24 09:14:14 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000030#include <cstdio>
Benjamin Kramercfeacf52016-05-27 14:27:13 +000031#include <utility>
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)
Benjamin Kramercfeacf52016-05-27 14:27:13 +000059 : HSOpts(std::move(HSOpts)), Diags(Diags),
60 FileMgr(SourceMgr.getFileManager()), FrameworkMap(64),
61 ModMap(SourceMgr, Diags, LangOpts, Target, *this) {
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(
Taewook Ohf42103c2016-06-13 20:40:21 +0000253 StringRef FileName, SourceLocation IncludeLoc, const DirectoryEntry *Dir,
254 bool IsSystemHeaderDir, Module *RequestingModule,
255 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);
Reid Klecknerafb9aae2015-10-20 18:45:57 +0000259 if (!File)
260 return nullptr;
Richard Smith8c71eba2014-03-05 20:51:45 +0000261
Richard Smith3d5b48c2015-10-16 21:42:56 +0000262 // If there is a module that corresponds to this header, suggest it.
263 if (!findUsableModuleForHeader(File, Dir ? Dir : File->getDir(),
264 RequestingModule, SuggestedModule,
265 IsSystemHeaderDir))
266 return nullptr;
Richard Smith8c71eba2014-03-05 20:51:45 +0000267
Richard Smith3d5b48c2015-10-16 21:42:56 +0000268 return File;
Richard Smith8c71eba2014-03-05 20:51:45 +0000269}
Chris Lattner8d720d02007-12-17 17:57:27 +0000270
Chris Lattnerf62f7582007-12-17 07:52:39 +0000271/// LookupFile - Lookup the specified file in this search path, returning it
272/// if it exists or returning null if not.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000273const FileEntry *DirectoryLookup::LookupFile(
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000274 StringRef &Filename,
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000275 HeaderSearch &HS,
Taewook Ohf42103c2016-06-13 20:40:21 +0000276 SourceLocation IncludeLoc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000277 SmallVectorImpl<char> *SearchPath,
Douglas Gregor97eec242011-09-15 22:00:41 +0000278 SmallVectorImpl<char> *RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000279 Module *RequestingModule,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000280 ModuleMap::KnownHeader *SuggestedModule,
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000281 bool &InUserSpecifiedSystemFramework,
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000282 bool &HasBeenMapped,
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000283 SmallVectorImpl<char> &MappedName) const {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000284 InUserSpecifiedSystemFramework = false;
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000285 HasBeenMapped = false;
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000286
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000287 SmallString<1024> TmpDir;
Chris Lattner712e3872007-12-17 08:13:48 +0000288 if (isNormalDir()) {
289 // Concatenate the requested file onto the directory.
Eli Friedmanf7ca26a2011-07-08 20:17:28 +0000290 TmpDir = getDir()->getName();
291 llvm::sys::path::append(TmpDir, Filename);
Craig Topperd2d442c2014-05-17 23:10:59 +0000292 if (SearchPath) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000293 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000294 SearchPath->clear();
295 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
296 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000297 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000298 RelativePath->clear();
299 RelativePath->append(Filename.begin(), Filename.end());
300 }
Richard Smith8c71eba2014-03-05 20:51:45 +0000301
Taewook Ohf42103c2016-06-13 20:40:21 +0000302 return HS.getFileAndSuggestModule(TmpDir, IncludeLoc, getDir(),
Richard Smith3d5b48c2015-10-16 21:42:56 +0000303 isSystemHeaderDirectory(),
304 RequestingModule, SuggestedModule);
Chris Lattner712e3872007-12-17 08:13:48 +0000305 }
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattner712e3872007-12-17 08:13:48 +0000307 if (isFramework())
Douglas Gregor97eec242011-09-15 22:00:41 +0000308 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000309 RequestingModule, SuggestedModule,
310 InUserSpecifiedSystemFramework);
Mike Stump11289f42009-09-09 15:08:12 +0000311
Chris Lattner44bd21b2007-12-17 08:17:39 +0000312 assert(isHeaderMap() && "Unknown directory lookup");
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000313 const HeaderMap *HM = getHeaderMap();
314 SmallString<1024> Path;
315 StringRef Dest = HM->lookupFilename(Filename, Path);
316 if (Dest.empty())
Craig Topperd2d442c2014-05-17 23:10:59 +0000317 return nullptr;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000318
319 const FileEntry *Result;
320
321 // Check if the headermap maps the filename to a framework include
322 // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
323 // framework include.
324 if (llvm::sys::path::is_relative(Dest)) {
325 MappedName.clear();
326 MappedName.append(Dest.begin(), Dest.end());
327 Filename = StringRef(MappedName.begin(), MappedName.size());
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000328 HasBeenMapped = true;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000329 Result = HM->LookupFile(Filename, HS.getFileMgr());
330
331 } else {
332 Result = HS.getFileMgr().getFile(Dest);
333 }
334
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000335 if (Result) {
Craig Topperd2d442c2014-05-17 23:10:59 +0000336 if (SearchPath) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000337 StringRef SearchPathRef(getName());
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000338 SearchPath->clear();
339 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
340 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000341 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000342 RelativePath->clear();
343 RelativePath->append(Filename.begin(), Filename.end());
344 }
345 }
346 return Result;
Chris Lattnerf62f7582007-12-17 07:52:39 +0000347}
348
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000349/// \brief Given a framework directory, find the top-most framework directory.
350///
351/// \param FileMgr The file manager to use for directory lookups.
352/// \param DirName The name of the framework directory.
353/// \param SubmodulePath Will be populated with the submodule path from the
354/// returned top-level module to the originally named framework.
355static const DirectoryEntry *
356getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
357 SmallVectorImpl<std::string> &SubmodulePath) {
358 assert(llvm::sys::path::extension(DirName) == ".framework" &&
359 "Not a framework directory");
360
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000361 // Note: as an egregious but useful hack we use the real path here, because
362 // frameworks moving between top-level frameworks to embedded frameworks tend
363 // to be symlinked, and we base the logical structure of modules on the
364 // physical layout. In particular, we need to deal with crazy includes like
365 //
366 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
367 //
368 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
369 // which one should access with, e.g.,
370 //
371 // #include <Bar/Wibble.h>
372 //
373 // Similar issues occur when a top-level framework has moved into an
374 // embedded framework.
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000375 const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
Douglas Gregore00c8b22013-01-26 00:55:12 +0000376 DirName = FileMgr.getCanonicalName(TopFrameworkDir);
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000377 do {
378 // Get the parent directory name.
379 DirName = llvm::sys::path::parent_path(DirName);
380 if (DirName.empty())
381 break;
382
383 // Determine whether this directory exists.
384 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
385 if (!Dir)
386 break;
387
388 // If this is a framework directory, then we're a subframework of this
389 // framework.
390 if (llvm::sys::path::extension(DirName) == ".framework") {
391 SubmodulePath.push_back(llvm::sys::path::stem(DirName));
392 TopFrameworkDir = Dir;
393 }
394 } while (true);
395
396 return TopFrameworkDir;
397}
Chris Lattnerf62f7582007-12-17 07:52:39 +0000398
Chris Lattner712e3872007-12-17 08:13:48 +0000399/// DoFrameworkLookup - Do a lookup of the specified file in the current
400/// DirectoryLookup, which is a framework directory.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000401const FileEntry *DirectoryLookup::DoFrameworkLookup(
Richard Smith3d5b48c2015-10-16 21:42:56 +0000402 StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
403 SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000404 ModuleMap::KnownHeader *SuggestedModule,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000405 bool &InUserSpecifiedSystemFramework) const {
Chris Lattner712e3872007-12-17 08:13:48 +0000406 FileManager &FileMgr = HS.getFileMgr();
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000408 // Framework names must have a '/' in the filename.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000409 size_t SlashPos = Filename.find('/');
Craig Topperd2d442c2014-05-17 23:10:59 +0000410 if (SlashPos == StringRef::npos) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000411
Chris Lattner712e3872007-12-17 08:13:48 +0000412 // Find out if this is the home for the specified framework, by checking
Daniel Dunbar17138612012-04-05 17:09:40 +0000413 // HeaderSearch. Possible answers are yes/no and unknown.
414 HeaderSearch::FrameworkCacheEntry &CacheEntry =
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000415 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump11289f42009-09-09 15:08:12 +0000416
Chris Lattner712e3872007-12-17 08:13:48 +0000417 // If it is known and in some other directory, fail.
Daniel Dunbar17138612012-04-05 17:09:40 +0000418 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
Craig Topperd2d442c2014-05-17 23:10:59 +0000419 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattner712e3872007-12-17 08:13:48 +0000421 // Otherwise, construct the path to this framework dir.
Mike Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000423 // FrameworkName = "/System/Library/Frameworks/"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000424 SmallString<1024> FrameworkName;
Chris Lattner712e3872007-12-17 08:13:48 +0000425 FrameworkName += getFrameworkDir()->getName();
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000426 if (FrameworkName.empty() || FrameworkName.back() != '/')
427 FrameworkName.push_back('/');
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000429 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Douglas Gregor56c64012011-11-17 01:41:17 +0000430 StringRef ModuleName(Filename.begin(), SlashPos);
431 FrameworkName += ModuleName;
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000433 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
434 FrameworkName += ".framework/";
Mike Stump11289f42009-09-09 15:08:12 +0000435
Daniel Dunbar17138612012-04-05 17:09:40 +0000436 // If the cache entry was unresolved, populate it now.
Craig Topperd2d442c2014-05-17 23:10:59 +0000437 if (!CacheEntry.Directory) {
Chris Lattner712e3872007-12-17 08:13:48 +0000438 HS.IncrementFrameworkLookupCount();
Mike Stump11289f42009-09-09 15:08:12 +0000439
Chris Lattner5ed76da2006-10-22 07:24:13 +0000440 // If the framework dir doesn't exist, we fail.
Yaron Keren92e1b622015-03-18 10:17:07 +0000441 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
Craig Topperd2d442c2014-05-17 23:10:59 +0000442 if (!Dir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000443
Chris Lattner5ed76da2006-10-22 07:24:13 +0000444 // Otherwise, if it does, remember that this is the right direntry for this
445 // framework.
Daniel Dunbar17138612012-04-05 17:09:40 +0000446 CacheEntry.Directory = getFrameworkDir();
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000447
448 // If this is a user search directory, check if the framework has been
449 // user-specified as a system framework.
450 if (getDirCharacteristic() == SrcMgr::C_User) {
451 SmallString<1024> SystemFrameworkMarker(FrameworkName);
452 SystemFrameworkMarker += ".system_framework";
Yaron Keren92e1b622015-03-18 10:17:07 +0000453 if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000454 CacheEntry.IsUserSpecifiedSystemFramework = true;
455 }
456 }
Chris Lattner5ed76da2006-10-22 07:24:13 +0000457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000459 // Set the 'user-specified system framework' flag.
460 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
461
Craig Topperd2d442c2014-05-17 23:10:59 +0000462 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000463 RelativePath->clear();
464 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
465 }
Douglas Gregor56c64012011-11-17 01:41:17 +0000466
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000467 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000468 unsigned OrigSize = FrameworkName.size();
Mike Stump11289f42009-09-09 15:08:12 +0000469
Chris Lattnerb201d9b2006-10-30 05:09:49 +0000470 FrameworkName += "Headers/";
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000471
Craig Topperd2d442c2014-05-17 23:10:59 +0000472 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000473 SearchPath->clear();
474 // Without trailing '/'.
475 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
476 }
477
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000478 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000479 const FileEntry *FE = FileMgr.getFile(FrameworkName,
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000480 /*openFile=*/!SuggestedModule);
481 if (!FE) {
482 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
483 const char *Private = "Private";
484 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
485 Private+strlen(Private));
Craig Topperd2d442c2014-05-17 23:10:59 +0000486 if (SearchPath)
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000487 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
488 Private+strlen(Private));
489
Yaron Keren92e1b622015-03-18 10:17:07 +0000490 FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000491 }
Mike Stump11289f42009-09-09 15:08:12 +0000492
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000493 // If we found the header and are allowed to suggest a module, do so now.
494 if (FE && SuggestedModule) {
495 // Find the framework in which this header occurs.
Ben Langmuiref914b82014-05-15 16:20:33 +0000496 StringRef FrameworkPath = FE->getDir()->getName();
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000497 bool FoundFramework = false;
498 do {
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000499 // Determine whether this directory exists.
500 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
501 if (!Dir)
502 break;
503
504 // If this is a framework directory, then we're a subframework of this
505 // framework.
506 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
507 FoundFramework = true;
508 break;
509 }
Ben Langmuiref914b82014-05-15 16:20:33 +0000510
511 // Get the parent directory name.
512 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
513 if (FrameworkPath.empty())
514 break;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000515 } while (true);
516
Richard Smith3d5b48c2015-10-16 21:42:56 +0000517 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000518 if (FoundFramework) {
Richard Smith3d5b48c2015-10-16 21:42:56 +0000519 if (!HS.findUsableModuleForFrameworkHeader(
520 FE, FrameworkPath, RequestingModule, SuggestedModule, IsSystem))
521 return nullptr;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000522 } else {
Richard Smith3d5b48c2015-10-16 21:42:56 +0000523 if (!HS.findUsableModuleForHeader(FE, getDir(), RequestingModule,
524 SuggestedModule, IsSystem))
525 return nullptr;
Douglas Gregor4ddf2222013-01-10 01:43:00 +0000526 }
527 }
Douglas Gregor97eec242011-09-15 22:00:41 +0000528 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000529}
530
Douglas Gregor89929282012-01-30 06:01:29 +0000531void HeaderSearch::setTarget(const TargetInfo &Target) {
532 ModMap.setTarget(Target);
533}
534
Chris Lattnerf62f7582007-12-17 07:52:39 +0000535
Chris Lattner712e3872007-12-17 08:13:48 +0000536//===----------------------------------------------------------------------===//
537// Header File Location.
538//===----------------------------------------------------------------------===//
539
Reid Klecknera97d4c02014-02-18 23:49:24 +0000540/// \brief Return true with a diagnostic if the file that MSVC would have found
541/// fails to match the one that Clang would have found with MSVC header search
542/// disabled.
543static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
544 const FileEntry *MSFE, const FileEntry *FE,
545 SourceLocation IncludeLoc) {
546 if (MSFE && FE != MSFE) {
547 Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
548 return true;
549 }
550 return false;
551}
Chris Lattner712e3872007-12-17 08:13:48 +0000552
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000553static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
554 assert(!Str.empty());
555 char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
556 std::copy(Str.begin(), Str.end(), CopyStr);
557 CopyStr[Str.size()] = '\0';
558 return CopyStr;
559}
560
James Dennettc07ab2c2012-06-20 00:56:32 +0000561/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000562/// return null on failure. isAngled indicates whether the file reference is
Will Wilson0fafd342013-12-27 19:46:16 +0000563/// for system \#include's or not (i.e. using <> instead of ""). Includers, if
564/// non-empty, indicates where the \#including file(s) are, in case a relative
565/// search is needed. Microsoft mode will pass all \#including files.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000566const FileEntry *HeaderSearch::LookupFile(
Will Wilson0fafd342013-12-27 19:46:16 +0000567 StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
568 const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000569 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
570 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000571 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
Manman Rene4a5d372016-05-17 02:15:12 +0000572 bool SkipCache, bool BuildSystemModule) {
Douglas Gregor97eec242011-09-15 22:00:41 +0000573 if (SuggestedModule)
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000574 *SuggestedModule = ModuleMap::KnownHeader();
Douglas Gregor97eec242011-09-15 22:00:41 +0000575
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000576 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencerf28df4c2010-12-17 21:22:22 +0000577 if (llvm::sys::path::is_absolute(Filename)) {
Craig Topperd2d442c2014-05-17 23:10:59 +0000578 CurDir = nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000579
580 // If this was an #include_next "/absolute/file", fail.
Craig Topperd2d442c2014-05-17 23:10:59 +0000581 if (FromDir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000582
Craig Topperd2d442c2014-05-17 23:10:59 +0000583 if (SearchPath)
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000584 SearchPath->clear();
Craig Topperd2d442c2014-05-17 23:10:59 +0000585 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000586 RelativePath->clear();
587 RelativePath->append(Filename.begin(), Filename.end());
588 }
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000589 // Otherwise, just return the file.
Taewook Ohf42103c2016-06-13 20:40:21 +0000590 return getFileAndSuggestModule(Filename, IncludeLoc, nullptr,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000591 /*IsSystemHeaderDir*/false,
592 RequestingModule, SuggestedModule);
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000593 }
Mike Stump11289f42009-09-09 15:08:12 +0000594
Reid Klecknera97d4c02014-02-18 23:49:24 +0000595 // This is the header that MSVC's header search would have found.
Craig Topperd2d442c2014-05-17 23:10:59 +0000596 const FileEntry *MSFE = nullptr;
Richard Smith8c71eba2014-03-05 20:51:45 +0000597 ModuleMap::KnownHeader MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000598
Douglas Gregor9f93e382011-07-28 04:45:53 +0000599 // Unless disabled, check to see if the file is in the #includer's
Will Wilson0fafd342013-12-27 19:46:16 +0000600 // directory. This cannot be based on CurDir, because each includer could be
601 // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
602 // include of "baz.h" should resolve to "whatever/foo/baz.h".
Chris Lattnerf62f7582007-12-17 07:52:39 +0000603 // This search is not done for <> headers.
Will Wilson0fafd342013-12-27 19:46:16 +0000604 if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
NAKAMURA Takumi9cb62642013-12-10 02:36:28 +0000605 SmallString<1024> TmpDir;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000606 bool First = true;
607 for (const auto &IncluderAndDir : Includers) {
608 const FileEntry *Includer = IncluderAndDir.first;
609
Will Wilson0fafd342013-12-27 19:46:16 +0000610 // Concatenate the requested file onto the directory.
Nikola Smiljaniccf385dc2015-05-08 06:02:37 +0000611 // FIXME: Portability. Filename concatenation should be in sys::Path.
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000612 TmpDir = IncluderAndDir.second->getName();
Nikola Smiljaniccf385dc2015-05-08 06:02:37 +0000613 TmpDir.push_back('/');
614 TmpDir.append(Filename.begin(), Filename.end());
Richard Smith8c71eba2014-03-05 20:51:45 +0000615
Richard Smith6f548ec2014-03-06 18:08:08 +0000616 // FIXME: We don't cache the result of getFileInfo across the call to
617 // getFileAndSuggestModule, because it's a reference to an element of
618 // a container that could be reallocated across this call.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000619 //
Manman Rene4a5d372016-05-17 02:15:12 +0000620 // If we have no includer, that means we're processing a #include
Richard Smith3c1a41a2014-12-02 00:08:08 +0000621 // from a module build. We should treat this as a system header if we're
622 // building a [system] module.
Richard Smith6f548ec2014-03-06 18:08:08 +0000623 bool IncluderIsSystemHeader =
Manman Rene39c8142016-05-17 18:04:38 +0000624 Includer ? getFileInfo(Includer).DirInfo != SrcMgr::C_User :
625 BuildSystemModule;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000626 if (const FileEntry *FE = getFileAndSuggestModule(
Taewook Ohf42103c2016-06-13 20:40:21 +0000627 TmpDir, IncludeLoc, IncluderAndDir.second, IncluderIsSystemHeader,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000628 RequestingModule, SuggestedModule)) {
Richard Smith3c1a41a2014-12-02 00:08:08 +0000629 if (!Includer) {
630 assert(First && "only first includer can have no file");
631 return FE;
632 }
633
Will Wilson0fafd342013-12-27 19:46:16 +0000634 // Leave CurDir unset.
635 // This file is a system header or C++ unfriendly if the old file is.
636 //
637 // Note that we only use one of FromHFI/ToHFI at once, due to potential
638 // reallocation of the underlying vector potentially making the first
639 // reference binding dangling.
Richard Smith6f548ec2014-03-06 18:08:08 +0000640 HeaderFileInfo &FromHFI = getFileInfo(Includer);
Will Wilson0fafd342013-12-27 19:46:16 +0000641 unsigned DirInfo = FromHFI.DirInfo;
642 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
643 StringRef Framework = FromHFI.Framework;
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000644
Will Wilson0fafd342013-12-27 19:46:16 +0000645 HeaderFileInfo &ToHFI = getFileInfo(FE);
646 ToHFI.DirInfo = DirInfo;
647 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
648 ToHFI.Framework = Framework;
Douglas Gregor03b5ebe2012-08-13 15:47:39 +0000649
Craig Topperd2d442c2014-05-17 23:10:59 +0000650 if (SearchPath) {
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000651 StringRef SearchPathRef(IncluderAndDir.second->getName());
Will Wilson0fafd342013-12-27 19:46:16 +0000652 SearchPath->clear();
653 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
654 }
Craig Topperd2d442c2014-05-17 23:10:59 +0000655 if (RelativePath) {
Will Wilson0fafd342013-12-27 19:46:16 +0000656 RelativePath->clear();
657 RelativePath->append(Filename.begin(), Filename.end());
658 }
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000659 if (First)
Reid Klecknera97d4c02014-02-18 23:49:24 +0000660 return FE;
661
662 // Otherwise, we found the path via MSVC header search rules. If
663 // -Wmsvc-include is enabled, we have to keep searching to see if we
664 // would've found this header in -I or -isystem directories.
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000665 if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
Reid Klecknera97d4c02014-02-18 23:49:24 +0000666 return FE;
667 } else {
668 MSFE = FE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000669 if (SuggestedModule) {
670 MSSuggestedModule = *SuggestedModule;
671 *SuggestedModule = ModuleMap::KnownHeader();
672 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000673 break;
674 }
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000675 }
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000676 First = false;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000677 }
678 }
Mike Stump11289f42009-09-09 15:08:12 +0000679
Craig Topperd2d442c2014-05-17 23:10:59 +0000680 CurDir = nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000681
682 // If this is a system #include, ignore the user #include locs.
Nico Weber3b1d1212011-05-24 04:31:14 +0000683 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump11289f42009-09-09 15:08:12 +0000684
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000685 // If this is a #include_next request, start searching after the directory the
686 // file was found in.
687 if (FromDir)
688 i = FromDir-&SearchDirs[0];
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattnerd4275422007-07-22 07:28:00 +0000690 // Cache all of the lookups performed by this method. Many headers are
691 // multiply included, and the "pragma once" optimization prevents them from
692 // being relex/pp'd, but they would still have to search through a
693 // (potentially huge) series of SearchDirs to find it.
David Blaikie13156b62014-11-19 03:06:06 +0000694 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
Chris Lattnerd4275422007-07-22 07:28:00 +0000695
696 // If the entry has been previously looked up, the first value will be
697 // non-zero. If the value is equal to i (the start point of our search), then
698 // this is a matching hit.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000699 if (!SkipCache && CacheLookup.StartIdx == i+1) {
Chris Lattnerd4275422007-07-22 07:28:00 +0000700 // Skip querying potentially lots of directories for this lookup.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000701 i = CacheLookup.HitIdx;
702 if (CacheLookup.MappedName)
703 Filename = CacheLookup.MappedName;
Chris Lattnerd4275422007-07-22 07:28:00 +0000704 } else {
705 // Otherwise, this is the first query, or the previous query didn't match
706 // our search start. We will fill in our found location below, so prime the
707 // start point value.
Argyrios Kyrtzidis7bd78a92014-03-29 03:22:54 +0000708 CacheLookup.reset(/*StartIdx=*/i+1);
Chris Lattnerd4275422007-07-22 07:28:00 +0000709 }
Mike Stump11289f42009-09-09 15:08:12 +0000710
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000711 SmallString<64> MappedName;
712
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000713 // Check each directory in sequence to see if it contains this file.
714 for (; i != SearchDirs.size(); ++i) {
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000715 bool InUserSpecifiedSystemFramework = false;
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000716 bool HasBeenMapped = false;
Richard Smith3d5b48c2015-10-16 21:42:56 +0000717 const FileEntry *FE = SearchDirs[i].LookupFile(
Taewook Ohf42103c2016-06-13 20:40:21 +0000718 Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000719 SuggestedModule, InUserSpecifiedSystemFramework, HasBeenMapped,
720 MappedName);
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000721 if (HasBeenMapped) {
722 CacheLookup.MappedName =
723 copyString(Filename, LookupFileCache.getAllocator());
724 }
Chris Lattner712e3872007-12-17 08:13:48 +0000725 if (!FE) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattner712e3872007-12-17 08:13:48 +0000727 CurDir = &SearchDirs[i];
Mike Stump11289f42009-09-09 15:08:12 +0000728
Chris Lattner712e3872007-12-17 08:13:48 +0000729 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor9f93e382011-07-28 04:45:53 +0000730 HeaderFileInfo &HFI = getFileInfo(FE);
731 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump11289f42009-09-09 15:08:12 +0000732
Daniel Dunbar3c9bc4d2012-04-05 17:10:06 +0000733 // If the directory characteristic is User but this framework was
734 // user-specified to be treated as a system framework, promote the
735 // characteristic.
736 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
737 HFI.DirInfo = SrcMgr::C_System;
738
Richard Smith8acadcb2012-06-13 20:27:03 +0000739 // If the filename matches a known system header prefix, override
740 // whether the file is a system header.
Richard Trieu871f5f32012-06-13 20:52:36 +0000741 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
742 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
743 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
Richard Smith8acadcb2012-06-13 20:27:03 +0000744 : SrcMgr::C_User;
745 break;
746 }
747 }
748
Douglas Gregor9f93e382011-07-28 04:45:53 +0000749 // If this file is found in a header map and uses the framework style of
750 // includes, then this header is part of a framework we're building.
751 if (CurDir->isIndexHeaderMap()) {
752 size_t SlashPos = Filename.find('/');
753 if (SlashPos != StringRef::npos) {
754 HFI.IndexHeaderMapHeader = 1;
755 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
756 SlashPos));
757 }
758 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000759
Richard Smith8c71eba2014-03-05 20:51:45 +0000760 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
761 if (SuggestedModule)
762 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000763 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000764 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000765
Chris Lattner712e3872007-12-17 08:13:48 +0000766 // Remember this location for the next lookup we do.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000767 CacheLookup.HitIdx = i;
Chris Lattner712e3872007-12-17 08:13:48 +0000768 return FE;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000769 }
Mike Stump11289f42009-09-09 15:08:12 +0000770
Douglas Gregord8575e12011-07-30 06:28:34 +0000771 // If we are including a file with a quoted include "foo.h" from inside
772 // a header in a framework that is currently being built, and we couldn't
773 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
774 // "Foo" is the name of the framework in which the including header was found.
Richard Smith3c1a41a2014-12-02 00:08:08 +0000775 if (!Includers.empty() && Includers.front().first && !isAngled &&
Will Wilson0fafd342013-12-27 19:46:16 +0000776 Filename.find('/') == StringRef::npos) {
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000777 HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
Douglas Gregord8575e12011-07-30 06:28:34 +0000778 if (IncludingHFI.IndexHeaderMapHeader) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000779 SmallString<128> ScratchFilename;
Douglas Gregord8575e12011-07-30 06:28:34 +0000780 ScratchFilename += IncludingHFI.Framework;
781 ScratchFilename += '/';
782 ScratchFilename += Filename;
Will Wilson0fafd342013-12-27 19:46:16 +0000783
Richard Smith3d5b48c2015-10-16 21:42:56 +0000784 const FileEntry *FE =
785 LookupFile(ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir,
786 CurDir, Includers.front(), SearchPath, RelativePath,
787 RequestingModule, SuggestedModule);
Reid Klecknera97d4c02014-02-18 23:49:24 +0000788
Richard Smith8c71eba2014-03-05 20:51:45 +0000789 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
790 if (SuggestedModule)
791 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000792 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000793 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000794
David Blaikie3c8c46e2014-11-19 05:48:40 +0000795 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
David Blaikie13156b62014-11-19 03:06:06 +0000796 CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
Richard Smith8c71eba2014-03-05 20:51:45 +0000797 // FIXME: SuggestedModule.
Reid Klecknera97d4c02014-02-18 23:49:24 +0000798 return FE;
Douglas Gregord8575e12011-07-30 06:28:34 +0000799 }
800 }
801
Craig Topperd2d442c2014-05-17 23:10:59 +0000802 if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
Richard Smith8c71eba2014-03-05 20:51:45 +0000803 if (SuggestedModule)
804 *SuggestedModule = MSSuggestedModule;
Reid Klecknera97d4c02014-02-18 23:49:24 +0000805 return MSFE;
Richard Smith8c71eba2014-03-05 20:51:45 +0000806 }
Reid Klecknera97d4c02014-02-18 23:49:24 +0000807
Chris Lattnerd4275422007-07-22 07:28:00 +0000808 // Otherwise, didn't find it. Remember we didn't find this.
Argyrios Kyrtzidis34fad422014-03-11 06:21:28 +0000809 CacheLookup.HitIdx = SearchDirs.size();
Craig Topperd2d442c2014-05-17 23:10:59 +0000810 return nullptr;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000811}
812
Chris Lattner63dd32b2006-10-20 04:42:40 +0000813/// LookupSubframeworkHeader - Look up a subframework for the specified
James Dennettc07ab2c2012-06-20 00:56:32 +0000814/// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
Chris Lattner63dd32b2006-10-20 04:42:40 +0000815/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
816/// is a subframework within Carbon.framework. If so, return the FileEntry
817/// for the designated file, otherwise return null.
818const FileEntry *HeaderSearch::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000819LookupSubframeworkHeader(StringRef Filename,
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000820 const FileEntry *ContextFileEnt,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000821 SmallVectorImpl<char> *SearchPath,
Douglas Gregorf5f94522013-02-08 00:10:48 +0000822 SmallVectorImpl<char> *RelativePath,
Richard Smith3d5b48c2015-10-16 21:42:56 +0000823 Module *RequestingModule,
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000824 ModuleMap::KnownHeader *SuggestedModule) {
Chris Lattner12261882008-02-01 05:34:02 +0000825 assert(ContextFileEnt && "No context file?");
Mike Stump11289f42009-09-09 15:08:12 +0000826
Chris Lattner63dd32b2006-10-20 04:42:40 +0000827 // Framework names must have a '/' in the filename. Find it.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000828 // FIXME: Should we permit '\' on Windows?
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000829 size_t SlashPos = Filename.find('/');
Craig Topperd2d442c2014-05-17 23:10:59 +0000830 if (SlashPos == StringRef::npos) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000831
Chris Lattner63dd32b2006-10-20 04:42:40 +0000832 // Look up the base framework name of the ContextFileEnt.
Chris Lattner48043482006-10-27 05:12:36 +0000833 const char *ContextName = ContextFileEnt->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000834
Chris Lattner63dd32b2006-10-20 04:42:40 +0000835 // If the context info wasn't a framework, couldn't be a subframework.
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000836 const unsigned DotFrameworkLen = 10;
837 const char *FrameworkPos = strstr(ContextName, ".framework");
Craig Topperd2d442c2014-05-17 23:10:59 +0000838 if (FrameworkPos == nullptr ||
Douglas Gregor5ca04bd2011-12-09 16:48:01 +0000839 (FrameworkPos[DotFrameworkLen] != '/' &&
840 FrameworkPos[DotFrameworkLen] != '\\'))
Craig Topperd2d442c2014-05-17 23:10:59 +0000841 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000842
Daniel Dunbar17138612012-04-05 17:09:40 +0000843 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
Chris Lattner5ed76da2006-10-22 07:24:13 +0000844
Chris Lattner63dd32b2006-10-20 04:42:40 +0000845 // Append Frameworks/HIToolbox.framework/
846 FrameworkName += "Frameworks/";
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000847 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Chris Lattner63dd32b2006-10-20 04:42:40 +0000848 FrameworkName += ".framework/";
Chris Lattner577377e2006-10-20 04:55:45 +0000849
David Blaikie13156b62014-11-19 03:06:06 +0000850 auto &CacheLookup =
851 *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
852 FrameworkCacheEntry())).first;
Mike Stump11289f42009-09-09 15:08:12 +0000853
Chris Lattner5ed76da2006-10-22 07:24:13 +0000854 // Some other location?
David Blaikie13156b62014-11-19 03:06:06 +0000855 if (CacheLookup.second.Directory &&
856 CacheLookup.first().size() == FrameworkName.size() &&
857 memcmp(CacheLookup.first().data(), &FrameworkName[0],
858 CacheLookup.first().size()) != 0)
Craig Topperd2d442c2014-05-17 23:10:59 +0000859 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattner5ed76da2006-10-22 07:24:13 +0000861 // Cache subframework.
David Blaikie13156b62014-11-19 03:06:06 +0000862 if (!CacheLookup.second.Directory) {
Chris Lattner5ed76da2006-10-22 07:24:13 +0000863 ++NumSubFrameworkLookups;
Mike Stump11289f42009-09-09 15:08:12 +0000864
Chris Lattner5ed76da2006-10-22 07:24:13 +0000865 // If the framework dir doesn't exist, we fail.
Yaron Keren92e1b622015-03-18 10:17:07 +0000866 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
Craig Topperd2d442c2014-05-17 23:10:59 +0000867 if (!Dir) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000868
Chris Lattner5ed76da2006-10-22 07:24:13 +0000869 // Otherwise, if it does, remember that this is the right direntry for this
870 // framework.
David Blaikie13156b62014-11-19 03:06:06 +0000871 CacheLookup.second.Directory = Dir;
Chris Lattner5ed76da2006-10-22 07:24:13 +0000872 }
Mike Stump11289f42009-09-09 15:08:12 +0000873
Craig Topperd2d442c2014-05-17 23:10:59 +0000874 const FileEntry *FE = nullptr;
Chris Lattner577377e2006-10-20 04:55:45 +0000875
Craig Topperd2d442c2014-05-17 23:10:59 +0000876 if (RelativePath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000877 RelativePath->clear();
878 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
879 }
880
Chris Lattner63dd32b2006-10-20 04:42:40 +0000881 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000882 SmallString<1024> HeadersFilename(FrameworkName);
Chris Lattner43fd42e2006-10-30 03:40:58 +0000883 HeadersFilename += "Headers/";
Craig Topperd2d442c2014-05-17 23:10:59 +0000884 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000885 SearchPath->clear();
886 // Without trailing '/'.
887 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
888 }
889
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000890 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000891 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
Mike Stump11289f42009-09-09 15:08:12 +0000892
Chris Lattner63dd32b2006-10-20 04:42:40 +0000893 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
Chris Lattner43fd42e2006-10-30 03:40:58 +0000894 HeadersFilename = FrameworkName;
895 HeadersFilename += "PrivateHeaders/";
Craig Topperd2d442c2014-05-17 23:10:59 +0000896 if (SearchPath) {
Manuel Klimek0c69fd22011-04-26 21:50:03 +0000897 SearchPath->clear();
898 // Without trailing '/'.
899 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
900 }
901
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000902 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Yaron Keren92e1b622015-03-18 10:17:07 +0000903 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true)))
Craig Topperd2d442c2014-05-17 23:10:59 +0000904 return nullptr;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000905 }
Mike Stump11289f42009-09-09 15:08:12 +0000906
Chris Lattner577377e2006-10-20 04:55:45 +0000907 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenek72be0682008-02-24 03:55:14 +0000908 //
Chris Lattnerf5c619f2008-02-25 21:38:21 +0000909 // Note that the temporary 'DirInfo' is required here, as either call to
910 // getFileInfo could resize the vector and we don't want to rely on order
911 // of evaluation.
912 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
913 getFileInfo(FE).DirInfo = DirInfo;
Douglas Gregorf5f94522013-02-08 00:10:48 +0000914
Richard Smith3d5b48c2015-10-16 21:42:56 +0000915 FrameworkName.pop_back(); // remove the trailing '/'
916 if (!findUsableModuleForFrameworkHeader(FE, FrameworkName, RequestingModule,
917 SuggestedModule, /*IsSystem*/ false))
918 return nullptr;
Douglas Gregorf5f94522013-02-08 00:10:48 +0000919
Chris Lattner577377e2006-10-20 04:55:45 +0000920 return FE;
Chris Lattner63dd32b2006-10-20 04:42:40 +0000921}
922
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000923//===----------------------------------------------------------------------===//
924// File Info Management.
925//===----------------------------------------------------------------------===//
926
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000927/// \brief Merge the header file info provided by \p OtherHFI into the current
928/// header file info (\p HFI)
929static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
930 const HeaderFileInfo &OtherHFI) {
Richard Smithd8879c82015-08-24 21:59:32 +0000931 assert(OtherHFI.External && "expected to merge external HFI");
932
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000933 HFI.isImport |= OtherHFI.isImport;
934 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +0000935 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000936 HFI.NumIncludes += OtherHFI.NumIncludes;
Richard Smithd8879c82015-08-24 21:59:32 +0000937
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000938 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
939 HFI.ControllingMacro = OtherHFI.ControllingMacro;
940 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
941 }
Richard Smithd8879c82015-08-24 21:59:32 +0000942
943 HFI.DirInfo = OtherHFI.DirInfo;
944 HFI.External = (!HFI.IsValid || HFI.External);
945 HFI.IsValid = true;
946 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000947
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000948 if (HFI.Framework.empty())
949 HFI.Framework = OtherHFI.Framework;
Douglas Gregor5d1bee22011-09-17 05:35:18 +0000950}
951
Steve Naroff3fa455a2009-04-24 20:03:17 +0000952/// getFileInfo - Return the HeaderFileInfo structure for the specified
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000953/// FileEntry.
Steve Naroff3fa455a2009-04-24 20:03:17 +0000954HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000955 if (FE->getUID() >= FileInfo.size())
Richard Smith386bb072015-08-18 23:42:23 +0000956 FileInfo.resize(FE->getUID() + 1);
957
Richard Smithd8879c82015-08-24 21:59:32 +0000958 HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
Richard Smith386bb072015-08-18 23:42:23 +0000959 // FIXME: Use a generation count to check whether this is really up to date.
Richard Smithd8879c82015-08-24 21:59:32 +0000960 if (ExternalSource && !HFI->Resolved) {
961 HFI->Resolved = true;
962 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
963
964 HFI = &FileInfo[FE->getUID()];
965 if (ExternalHFI.External)
966 mergeHeaderFileInfo(*HFI, ExternalHFI);
Richard Smith386bb072015-08-18 23:42:23 +0000967 }
968
Richard Smithd8879c82015-08-24 21:59:32 +0000969 HFI->IsValid = true;
Richard Smith386bb072015-08-18 23:42:23 +0000970 // We have local information about this header file, so it's no longer
971 // strictly external.
Richard Smithd8879c82015-08-24 21:59:32 +0000972 HFI->External = false;
973 return *HFI;
Mike Stump11289f42009-09-09 15:08:12 +0000974}
Chris Lattner59a9ebd2006-10-18 05:34:33 +0000975
Richard Smith386bb072015-08-18 23:42:23 +0000976const HeaderFileInfo *
Richard Smithd8879c82015-08-24 21:59:32 +0000977HeaderSearch::getExistingFileInfo(const FileEntry *FE,
978 bool WantExternal) const {
Richard Smith386bb072015-08-18 23:42:23 +0000979 // If we have an external source, ensure we have the latest information.
980 // FIXME: Use a generation count to check whether this is really up to date.
Richard Smithd8879c82015-08-24 21:59:32 +0000981 HeaderFileInfo *HFI;
982 if (ExternalSource) {
983 if (FE->getUID() >= FileInfo.size()) {
984 if (!WantExternal)
985 return nullptr;
986 FileInfo.resize(FE->getUID() + 1);
Richard Smith386bb072015-08-18 23:42:23 +0000987 }
Richard Smithd8879c82015-08-24 21:59:32 +0000988
989 HFI = &FileInfo[FE->getUID()];
990 if (!WantExternal && (!HFI->IsValid || HFI->External))
991 return nullptr;
992 if (!HFI->Resolved) {
993 HFI->Resolved = true;
994 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
995
996 HFI = &FileInfo[FE->getUID()];
997 if (ExternalHFI.External)
998 mergeHeaderFileInfo(*HFI, ExternalHFI);
999 }
1000 } else if (FE->getUID() >= FileInfo.size()) {
1001 return nullptr;
1002 } else {
1003 HFI = &FileInfo[FE->getUID()];
Ben Langmuird285c502014-03-13 16:46:36 +00001004 }
Richard Smith386bb072015-08-18 23:42:23 +00001005
Richard Smithd8879c82015-08-24 21:59:32 +00001006 if (!HFI->IsValid || (HFI->External && !WantExternal))
Richard Smith386bb072015-08-18 23:42:23 +00001007 return nullptr;
1008
Richard Smithd8879c82015-08-24 21:59:32 +00001009 return HFI;
Ben Langmuird285c502014-03-13 16:46:36 +00001010}
1011
Douglas Gregor37aa4932011-05-04 00:14:37 +00001012bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1013 // Check if we've ever seen this file as a header.
Richard Smith386bb072015-08-18 23:42:23 +00001014 if (auto *HFI = getExistingFileInfo(File))
1015 return HFI->isPragmaOnce || HFI->isImport || HFI->ControllingMacro ||
1016 HFI->ControllingMacroID;
1017 return false;
Douglas Gregor37aa4932011-05-04 00:14:37 +00001018}
1019
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001020void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001021 ModuleMap::ModuleHeaderRole Role,
Argyrios Kyrtzidis6f722b42013-05-08 23:46:46 +00001022 bool isCompilingModuleHeader) {
Richard Smithd8879c82015-08-24 21:59:32 +00001023 bool isModularHeader = !(Role & ModuleMap::TextualHeader);
1024
1025 // Don't mark the file info as non-external if there's nothing to change.
1026 if (!isCompilingModuleHeader) {
1027 if (!isModularHeader)
1028 return;
1029 auto *HFI = getExistingFileInfo(FE);
1030 if (HFI && HFI->isModuleHeader)
1031 return;
1032 }
1033
Richard Smith386bb072015-08-18 23:42:23 +00001034 auto &HFI = getFileInfo(FE);
Richard Smithd8879c82015-08-24 21:59:32 +00001035 HFI.isModuleHeader |= isModularHeader;
Richard Smithe70dadd2015-07-10 22:27:17 +00001036 HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001037}
1038
Richard Smith20e883e2015-04-29 23:20:19 +00001039bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1040 const FileEntry *File,
Richard Smith035f6dc2015-07-01 01:51:38 +00001041 bool isImport, Module *M) {
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001042 ++NumIncluded; // Count # of attempted #includes.
1043
1044 // Get information about this file.
Steve Naroff3fa455a2009-04-24 20:03:17 +00001045 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump11289f42009-09-09 15:08:12 +00001046
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001047 // If this is a #import directive, check that we have not already imported
1048 // this header.
1049 if (isImport) {
1050 // If this has already been imported, don't import it again.
1051 FileInfo.isImport = true;
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001053 // Has this already been #import'ed or #include'd?
1054 if (FileInfo.NumIncludes) return false;
1055 } else {
1056 // Otherwise, if this is a #include of a file that was previously #import'd
1057 // or if this is the second #include of a #pragma once file, ignore it.
1058 if (FileInfo.isImport)
1059 return false;
1060 }
Mike Stump11289f42009-09-09 15:08:12 +00001061
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001062 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1063 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump11289f42009-09-09 15:08:12 +00001064 if (const IdentifierInfo *ControllingMacro
Richard Smithe70dadd2015-07-10 22:27:17 +00001065 = FileInfo.getControllingMacro(ExternalLookup)) {
1066 // If the header corresponds to a module, check whether the macro is already
1067 // defined in that module rather than checking in the current set of visible
1068 // modules.
1069 if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M)
1070 : PP.isMacroDefined(ControllingMacro)) {
Douglas Gregor99734e72009-04-25 23:30:02 +00001071 ++NumMultiIncludeFileOptzn;
1072 return false;
1073 }
Richard Smithe70dadd2015-07-10 22:27:17 +00001074 }
Mike Stump11289f42009-09-09 15:08:12 +00001075
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001076 // Increment the number of times this file has been included.
1077 ++FileInfo.NumIncludes;
Mike Stump11289f42009-09-09 15:08:12 +00001078
Chris Lattner59a9ebd2006-10-18 05:34:33 +00001079 return true;
1080}
1081
Ted Kremenekfbcce6f2011-07-26 23:46:11 +00001082size_t HeaderSearch::getTotalMemory() const {
1083 return SearchDirs.capacity()
Ted Kremenekae63d102011-07-27 18:41:18 +00001084 + llvm::capacity_in_bytes(FileInfo)
1085 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekfbcce6f2011-07-26 23:46:11 +00001086 + LookupFileCache.getAllocator().getTotalMemory()
1087 + FrameworkMap.getAllocator().getTotalMemory();
1088}
Douglas Gregor9f93e382011-07-28 04:45:53 +00001089
1090StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
David Blaikie13156b62014-11-19 03:06:06 +00001091 return FrameworkNames.insert(Framework).first->first();
Douglas Gregor9f93e382011-07-28 04:45:53 +00001092}
Douglas Gregor718292f2011-11-11 19:10:28 +00001093
1094bool HeaderSearch::hasModuleMap(StringRef FileName,
Douglas Gregor963c5532013-06-21 16:28:10 +00001095 const DirectoryEntry *Root,
1096 bool IsSystem) {
Richard Smith47972af2015-06-16 00:08:24 +00001097 if (!HSOpts->ImplicitModuleMaps)
Argyrios Kyrtzidis9955dbc2013-12-12 16:08:33 +00001098 return false;
1099
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001100 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
Douglas Gregor718292f2011-11-11 19:10:28 +00001101
1102 StringRef DirName = FileName;
1103 do {
1104 // Get the parent directory name.
1105 DirName = llvm::sys::path::parent_path(DirName);
1106 if (DirName.empty())
1107 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001108
Douglas Gregor718292f2011-11-11 19:10:28 +00001109 // Determine whether this directory exists.
1110 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
1111 if (!Dir)
1112 return false;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001113
Ben Langmuir984e1df2014-03-19 20:23:34 +00001114 // Try to load the module map file in this directory.
Richard Smith3c1a41a2014-12-02 00:08:08 +00001115 switch (loadModuleMapFile(Dir, IsSystem,
1116 llvm::sys::path::extension(Dir->getName()) ==
1117 ".framework")) {
Douglas Gregor80b69042011-11-12 00:22:19 +00001118 case LMM_NewlyLoaded:
1119 case LMM_AlreadyLoaded:
Daniel Jasperca9f7382013-09-24 09:27:13 +00001120 // Success. All of the directories we stepped through inherit this module
1121 // map file.
1122 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1123 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1124 return true;
Daniel Jasper97da9172013-10-22 08:09:47 +00001125
1126 case LMM_NoDirectory:
1127 case LMM_InvalidModuleMap:
1128 break;
Daniel Jasperca9f7382013-09-24 09:27:13 +00001129 }
1130
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001131 // If we hit the top of our search, we're done.
1132 if (Dir == Root)
1133 return false;
1134
Douglas Gregor718292f2011-11-11 19:10:28 +00001135 // Keep track of all of the directories we checked, so we can mark them as
1136 // having module maps if we eventually do find a module map.
1137 FixUpDirectories.push_back(Dir);
1138 } while (true);
Douglas Gregor718292f2011-11-11 19:10:28 +00001139}
1140
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001141ModuleMap::KnownHeader
1142HeaderSearch::findModuleForHeader(const FileEntry *File) const {
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001143 if (ExternalSource) {
1144 // Make sure the external source has handled header info about this file,
1145 // which includes whether the file is part of a module.
Richard Smith386bb072015-08-18 23:42:23 +00001146 (void)getExistingFileInfo(File);
Argyrios Kyrtzidisb146baa2013-03-13 21:13:51 +00001147 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +00001148 return ModMap.findModuleForHeader(File);
Douglas Gregor718292f2011-11-11 19:10:28 +00001149}
1150
Richard Smith3d5b48c2015-10-16 21:42:56 +00001151bool HeaderSearch::findUsableModuleForHeader(
1152 const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
1153 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1154 if (File && SuggestedModule) {
1155 // If there is a module that corresponds to this header, suggest it.
1156 hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
1157 *SuggestedModule = findModuleForHeader(File);
1158 }
1159 return true;
1160}
1161
1162bool HeaderSearch::findUsableModuleForFrameworkHeader(
1163 const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
1164 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1165 // If we're supposed to suggest a module, look for one now.
1166 if (SuggestedModule) {
1167 // Find the top-level framework based on this framework.
1168 SmallVector<std::string, 4> SubmodulePath;
1169 const DirectoryEntry *TopFrameworkDir
1170 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1171
1172 // Determine the name of the top-level framework.
1173 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1174
1175 // Load this framework module. If that succeeds, find the suggested module
1176 // for this header, if any.
1177 loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1178
1179 // FIXME: This can find a module not part of ModuleName, which is
1180 // important so that we're consistent about whether this header
1181 // corresponds to a module. Possibly we should lock down framework modules
1182 // so that this is not possible.
1183 *SuggestedModule = findModuleForHeader(File);
1184 }
1185 return true;
1186}
1187
Richard Smith9acb99e32014-12-10 03:09:48 +00001188static const FileEntry *getPrivateModuleMap(const FileEntry *File,
Ben Langmuir984e1df2014-03-19 20:23:34 +00001189 FileManager &FileMgr) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001190 StringRef Filename = llvm::sys::path::filename(File->getName());
1191 SmallString<128> PrivateFilename(File->getDir()->getName());
Ben Langmuir984e1df2014-03-19 20:23:34 +00001192 if (Filename == "module.map")
Douglas Gregor80306772011-12-07 21:25:07 +00001193 llvm::sys::path::append(PrivateFilename, "module_private.map");
Ben Langmuir984e1df2014-03-19 20:23:34 +00001194 else if (Filename == "module.modulemap")
1195 llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1196 else
1197 return nullptr;
1198 return FileMgr.getFile(PrivateFilename);
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001199}
1200
Ben Langmuir984e1df2014-03-19 20:23:34 +00001201bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001202 // Find the directory for the module. For frameworks, that may require going
1203 // up from the 'Modules' directory.
1204 const DirectoryEntry *Dir = nullptr;
1205 if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
1206 Dir = FileMgr.getDirectory(".");
1207 else {
1208 Dir = File->getDir();
1209 StringRef DirName(Dir->getName());
1210 if (llvm::sys::path::filename(DirName) == "Modules") {
1211 DirName = llvm::sys::path::parent_path(DirName);
1212 if (DirName.endswith(".framework"))
1213 Dir = FileMgr.getDirectory(DirName);
1214 // FIXME: This assert can fail if there's a race between the above check
1215 // and the removal of the directory.
1216 assert(Dir && "parent must exist");
1217 }
1218 }
1219
1220 switch (loadModuleMapFileImpl(File, IsSystem, Dir)) {
Ben Langmuir984e1df2014-03-19 20:23:34 +00001221 case LMM_AlreadyLoaded:
1222 case LMM_NewlyLoaded:
1223 return false;
1224 case LMM_NoDirectory:
1225 case LMM_InvalidModuleMap:
1226 return true;
1227 }
Aaron Ballmand8de5b62014-03-20 14:22:33 +00001228 llvm_unreachable("Unknown load module map result");
Ben Langmuir984e1df2014-03-19 20:23:34 +00001229}
1230
1231HeaderSearch::LoadModuleMapResult
Richard Smith9acb99e32014-12-10 03:09:48 +00001232HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1233 const DirectoryEntry *Dir) {
Ben Langmuir984e1df2014-03-19 20:23:34 +00001234 assert(File && "expected FileEntry");
1235
Richard Smith9887d792014-10-17 01:42:53 +00001236 // Check whether we've already loaded this module map, and mark it as being
1237 // loaded in case we recursively try to load it from itself.
1238 auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1239 if (!AddResult.second)
1240 return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001241
Richard Smith9acb99e32014-12-10 03:09:48 +00001242 if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) {
Richard Smith9887d792014-10-17 01:42:53 +00001243 LoadedModuleMaps[File] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001244 return LMM_InvalidModuleMap;
1245 }
1246
1247 // Try to load a corresponding private module map.
Richard Smith9acb99e32014-12-10 03:09:48 +00001248 if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1249 if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
Richard Smith9887d792014-10-17 01:42:53 +00001250 LoadedModuleMaps[File] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001251 return LMM_InvalidModuleMap;
1252 }
1253 }
1254
1255 // This directory has a module map.
Ben Langmuir984e1df2014-03-19 20:23:34 +00001256 return LMM_NewlyLoaded;
1257}
1258
1259const FileEntry *
1260HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
Richard Smith47972af2015-06-16 00:08:24 +00001261 if (!HSOpts->ImplicitModuleMaps)
Daniel Jasper21a0f552014-11-25 09:45:48 +00001262 return nullptr;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001263 // For frameworks, the preferred spelling is Modules/module.modulemap, but
1264 // module.map at the framework root is also accepted.
1265 SmallString<128> ModuleMapFileName(Dir->getName());
1266 if (IsFramework)
1267 llvm::sys::path::append(ModuleMapFileName, "Modules");
1268 llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1269 if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
1270 return F;
1271
1272 // Continue to allow module.map
1273 ModuleMapFileName = Dir->getName();
1274 llvm::sys::path::append(ModuleMapFileName, "module.map");
1275 return FileMgr.getFile(ModuleMapFileName);
1276}
1277
1278Module *HeaderSearch::loadFrameworkModule(StringRef Name,
Douglas Gregor279a6c32012-01-29 17:08:11 +00001279 const DirectoryEntry *Dir,
1280 bool IsSystem) {
Douglas Gregorde3ef502011-11-30 23:21:26 +00001281 if (Module *Module = ModMap.findModule(Name))
Douglas Gregor56c64012011-11-17 01:41:17 +00001282 return Module;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001283
Douglas Gregor56c64012011-11-17 01:41:17 +00001284 // Try to load a module map file.
Ben Langmuir984e1df2014-03-19 20:23:34 +00001285 switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
Douglas Gregor56c64012011-11-17 01:41:17 +00001286 case LMM_InvalidModuleMap:
Ben Langmuira5254002015-07-02 13:19:48 +00001287 // Try to infer a module map from the framework directory.
1288 if (HSOpts->ImplicitModuleMaps)
1289 ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
Douglas Gregor56c64012011-11-17 01:41:17 +00001290 break;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001291
Douglas Gregor56c64012011-11-17 01:41:17 +00001292 case LMM_AlreadyLoaded:
1293 case LMM_NoDirectory:
Craig Topperd2d442c2014-05-17 23:10:59 +00001294 return nullptr;
1295
Douglas Gregor56c64012011-11-17 01:41:17 +00001296 case LMM_NewlyLoaded:
Ben Langmuira5254002015-07-02 13:19:48 +00001297 break;
Douglas Gregor56c64012011-11-17 01:41:17 +00001298 }
Douglas Gregor3a5999b2012-01-13 22:31:52 +00001299
Ben Langmuira5254002015-07-02 13:19:48 +00001300 return ModMap.findModule(Name);
Douglas Gregor56c64012011-11-17 01:41:17 +00001301}
1302
Douglas Gregor2b20cb82011-11-16 00:09:06 +00001303
Douglas Gregor80b69042011-11-12 00:22:19 +00001304HeaderSearch::LoadModuleMapResult
Ben Langmuir984e1df2014-03-19 20:23:34 +00001305HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1306 bool IsFramework) {
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001307 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
Ben Langmuir984e1df2014-03-19 20:23:34 +00001308 return loadModuleMapFile(Dir, IsSystem, IsFramework);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001309
Douglas Gregor80b69042011-11-12 00:22:19 +00001310 return LMM_NoDirectory;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001311}
1312
Douglas Gregor80b69042011-11-12 00:22:19 +00001313HeaderSearch::LoadModuleMapResult
Ben Langmuir984e1df2014-03-19 20:23:34 +00001314HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1315 bool IsFramework) {
1316 auto KnownDir = DirectoryHasModuleMap.find(Dir);
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001317 if (KnownDir != DirectoryHasModuleMap.end())
Richard Smith9887d792014-10-17 01:42:53 +00001318 return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
Douglas Gregore7ab3662011-12-07 02:23:45 +00001319
Ben Langmuir984e1df2014-03-19 20:23:34 +00001320 if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
Richard Smith9acb99e32014-12-10 03:09:48 +00001321 LoadModuleMapResult Result =
1322 loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
Ben Langmuir984e1df2014-03-19 20:23:34 +00001323 // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1324 // E.g. Foo.framework/Modules/module.modulemap
1325 // ^Dir ^ModuleMapFile
1326 if (Result == LMM_NewlyLoaded)
1327 DirectoryHasModuleMap[Dir] = true;
Richard Smith9887d792014-10-17 01:42:53 +00001328 else if (Result == LMM_InvalidModuleMap)
1329 DirectoryHasModuleMap[Dir] = false;
Ben Langmuir984e1df2014-03-19 20:23:34 +00001330 return Result;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001331 }
Douglas Gregor80b69042011-11-12 00:22:19 +00001332 return LMM_InvalidModuleMap;
Douglas Gregoraf28ec82011-11-12 00:05:07 +00001333}
Douglas Gregor718292f2011-11-11 19:10:28 +00001334
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001335void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
Douglas Gregor07f43572012-01-29 18:15:03 +00001336 Modules.clear();
Daniel Jasper21a0f552014-11-25 09:45:48 +00001337
Richard Smith47972af2015-06-16 00:08:24 +00001338 if (HSOpts->ImplicitModuleMaps) {
Daniel Jasper21a0f552014-11-25 09:45:48 +00001339 // Load module maps for each of the header search directories.
1340 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1341 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1342 if (SearchDirs[Idx].isFramework()) {
1343 std::error_code EC;
1344 SmallString<128> DirNative;
1345 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1346 DirNative);
1347
1348 // Search each of the ".framework" directories to load them as modules.
Bruno Cardoso Lopesb171a592016-05-16 16:46:01 +00001349 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
1350 for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
Daniel Jasper21a0f552014-11-25 09:45:48 +00001351 Dir != DirEnd && !EC; Dir.increment(EC)) {
Bruno Cardoso Lopesb171a592016-05-16 16:46:01 +00001352 if (llvm::sys::path::extension(Dir->getName()) != ".framework")
Daniel Jasper21a0f552014-11-25 09:45:48 +00001353 continue;
1354
1355 const DirectoryEntry *FrameworkDir =
Bruno Cardoso Lopesb171a592016-05-16 16:46:01 +00001356 FileMgr.getDirectory(Dir->getName());
Daniel Jasper21a0f552014-11-25 09:45:48 +00001357 if (!FrameworkDir)
1358 continue;
1359
1360 // Load this framework module.
Bruno Cardoso Lopesb171a592016-05-16 16:46:01 +00001361 loadFrameworkModule(llvm::sys::path::stem(Dir->getName()),
1362 FrameworkDir, IsSystem);
Daniel Jasper21a0f552014-11-25 09:45:48 +00001363 }
1364 continue;
Douglas Gregor07f43572012-01-29 18:15:03 +00001365 }
Daniel Jasper21a0f552014-11-25 09:45:48 +00001366
1367 // FIXME: Deal with header maps.
1368 if (SearchDirs[Idx].isHeaderMap())
1369 continue;
1370
1371 // Try to load a module map file for the search directory.
1372 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1373 /*IsFramework*/ false);
1374
1375 // Try to load module map files for immediate subdirectories of this
1376 // search directory.
1377 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
Douglas Gregor07f43572012-01-29 18:15:03 +00001378 }
Douglas Gregor07f43572012-01-29 18:15:03 +00001379 }
Daniel Jasper21a0f552014-11-25 09:45:48 +00001380
Douglas Gregor07f43572012-01-29 18:15:03 +00001381 // Populate the list of modules.
1382 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1383 MEnd = ModMap.module_end();
1384 M != MEnd; ++M) {
1385 Modules.push_back(M->getValue());
1386 }
1387}
Douglas Gregor0339a642013-03-21 01:08:50 +00001388
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001389void HeaderSearch::loadTopLevelSystemModules() {
Richard Smith47972af2015-06-16 00:08:24 +00001390 if (!HSOpts->ImplicitModuleMaps)
Daniel Jasper21a0f552014-11-25 09:45:48 +00001391 return;
1392
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001393 // Load module maps for each of the header search directories.
1394 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
Douglas Gregor299787f2013-11-01 23:08:38 +00001395 // We only care about normal header directories.
1396 if (!SearchDirs[Idx].isNormalDir()) {
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001397 continue;
1398 }
1399
1400 // Try to load a module map file for the search directory.
Douglas Gregor963c5532013-06-21 16:28:10 +00001401 loadModuleMapFile(SearchDirs[Idx].getDir(),
Ben Langmuir984e1df2014-03-19 20:23:34 +00001402 SearchDirs[Idx].isSystemHeaderDirectory(),
1403 SearchDirs[Idx].isFramework());
Douglas Gregor64a1fa52013-05-10 22:52:27 +00001404 }
1405}
1406
Douglas Gregor0339a642013-03-21 01:08:50 +00001407void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
Richard Smith47972af2015-06-16 00:08:24 +00001408 assert(HSOpts->ImplicitModuleMaps &&
Daniel Jasper21a0f552014-11-25 09:45:48 +00001409 "Should not be loading subdirectory module maps");
1410
Douglas Gregor0339a642013-03-21 01:08:50 +00001411 if (SearchDir.haveSearchedAllModuleMaps())
1412 return;
Rafael Espindolac0809172014-06-12 14:02:15 +00001413
1414 std::error_code EC;
Douglas Gregor0339a642013-03-21 01:08:50 +00001415 SmallString<128> DirNative;
1416 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
Bruno Cardoso Lopesb171a592016-05-16 16:46:01 +00001417 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
1418 for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
Douglas Gregor0339a642013-03-21 01:08:50 +00001419 Dir != DirEnd && !EC; Dir.increment(EC)) {
Bruno Cardoso Lopesb171a592016-05-16 16:46:01 +00001420 bool IsFramework =
1421 llvm::sys::path::extension(Dir->getName()) == ".framework";
Ben Langmuir1f6a32b2015-02-24 04:58:15 +00001422 if (IsFramework == SearchDir.isFramework())
Bruno Cardoso Lopesb171a592016-05-16 16:46:01 +00001423 loadModuleMapFile(Dir->getName(), SearchDir.isSystemHeaderDirectory(),
Ben Langmuir1f6a32b2015-02-24 04:58:15 +00001424 SearchDir.isFramework());
Douglas Gregor0339a642013-03-21 01:08:50 +00001425 }
1426
1427 SearchDir.setSearchedAllModuleMaps(true);
1428}
Richard Smith4eb83932016-04-27 21:57:05 +00001429
1430std::string HeaderSearch::suggestPathToFileForDiagnostics(const FileEntry *File,
1431 bool *IsSystem) {
1432 // FIXME: We assume that the path name currently cached in the FileEntry is
1433 // the most appropriate one for this analysis (and that it's spelled the same
1434 // way as the corresponding header search path).
1435 const char *Name = File->getName();
1436
1437 unsigned BestPrefixLength = 0;
1438 unsigned BestSearchDir;
1439
1440 for (unsigned I = 0; I != SearchDirs.size(); ++I) {
1441 // FIXME: Support this search within frameworks and header maps.
1442 if (!SearchDirs[I].isNormalDir())
1443 continue;
1444
1445 const char *Dir = SearchDirs[I].getDir()->getName();
1446 for (auto NI = llvm::sys::path::begin(Name),
1447 NE = llvm::sys::path::end(Name),
1448 DI = llvm::sys::path::begin(Dir),
1449 DE = llvm::sys::path::end(Dir);
1450 /*termination condition in loop*/; ++NI, ++DI) {
1451 // '.' components in Name are ignored.
1452 while (NI != NE && *NI == ".")
1453 ++NI;
1454 if (NI == NE)
1455 break;
1456
1457 // '.' components in Dir are ignored.
1458 while (DI != DE && *DI == ".")
1459 ++DI;
1460 if (DI == DE) {
1461 // Dir is a prefix of Name, up to '.' components and choice of path
1462 // separators.
1463 unsigned PrefixLength = NI - llvm::sys::path::begin(Name);
1464 if (PrefixLength > BestPrefixLength) {
1465 BestPrefixLength = PrefixLength;
1466 BestSearchDir = I;
1467 }
1468 break;
1469 }
1470
1471 if (*NI != *DI)
1472 break;
1473 }
1474 }
1475
1476 if (IsSystem)
1477 *IsSystem = BestPrefixLength ? BestSearchDir >= SystemDirIdx : false;
1478 return Name + BestPrefixLength;
1479}