Coverage Report

Created: 2020-11-24 06:42

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Lex/HeaderSearch.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- HeaderSearch.cpp - Resolve Header File Locations -------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file implements the DirectoryLookup and HeaderSearch interfaces.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Lex/HeaderSearch.h"
14
#include "clang/Basic/Diagnostic.h"
15
#include "clang/Basic/FileManager.h"
16
#include "clang/Basic/IdentifierTable.h"
17
#include "clang/Basic/Module.h"
18
#include "clang/Basic/SourceManager.h"
19
#include "clang/Lex/DirectoryLookup.h"
20
#include "clang/Lex/ExternalPreprocessorSource.h"
21
#include "clang/Lex/HeaderMap.h"
22
#include "clang/Lex/HeaderSearchOptions.h"
23
#include "clang/Lex/LexDiagnostic.h"
24
#include "clang/Lex/ModuleMap.h"
25
#include "clang/Lex/Preprocessor.h"
26
#include "llvm/ADT/APInt.h"
27
#include "llvm/ADT/Hashing.h"
28
#include "llvm/ADT/SmallString.h"
29
#include "llvm/ADT/SmallVector.h"
30
#include "llvm/ADT/Statistic.h"
31
#include "llvm/ADT/StringRef.h"
32
#include "llvm/Support/Allocator.h"
33
#include "llvm/Support/Capacity.h"
34
#include "llvm/Support/Errc.h"
35
#include "llvm/Support/ErrorHandling.h"
36
#include "llvm/Support/FileSystem.h"
37
#include "llvm/Support/Path.h"
38
#include "llvm/Support/VirtualFileSystem.h"
39
#include <algorithm>
40
#include <cassert>
41
#include <cstddef>
42
#include <cstdio>
43
#include <cstring>
44
#include <string>
45
#include <system_error>
46
#include <utility>
47
48
using namespace clang;
49
50
#define DEBUG_TYPE "file-search"
51
52
ALWAYS_ENABLED_STATISTIC(NumIncluded, "Number of attempted #includes.");
53
ALWAYS_ENABLED_STATISTIC(
54
    NumMultiIncludeFileOptzn,
55
    "Number of #includes skipped due to the multi-include optimization.");
56
ALWAYS_ENABLED_STATISTIC(NumFrameworkLookups, "Number of framework lookups.");
57
ALWAYS_ENABLED_STATISTIC(NumSubFrameworkLookups,
58
                         "Number of subframework lookups.");
59
60
const IdentifierInfo *
61
2.02M
HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
62
2.02M
  if (ControllingMacro) {
63
1.14M
    if (ControllingMacro->isOutOfDate()) {
64
716
      assert(External && "We must have an external source if we have a "
65
716
                         "controlling macro that is out of date.");
66
716
      External->updateOutOfDateIdentifier(
67
716
          *const_cast<IdentifierInfo *>(ControllingMacro));
68
716
    }
69
1.14M
    return ControllingMacro;
70
1.14M
  }
71
72
886k
  if (!ControllingMacroID || 
!External191
)
73
886k
    return nullptr;
74
75
191
  ControllingMacro = External->GetIdentifier(ControllingMacroID);
76
191
  return ControllingMacro;
77
191
}
78
79
11.6k
ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() = default;
80
81
HeaderSearch::HeaderSearch(std::shared_ptr<HeaderSearchOptions> HSOpts,
82
                           SourceManager &SourceMgr, DiagnosticsEngine &Diags,
83
                           const LangOptions &LangOpts,
84
                           const TargetInfo *Target)
85
    : HSOpts(std::move(HSOpts)), Diags(Diags),
86
      FileMgr(SourceMgr.getFileManager()), FrameworkMap(64),
87
83.6k
      ModMap(SourceMgr, Diags, LangOpts, Target, *this) {}
88
89
3
void HeaderSearch::PrintStats() {
90
3
  llvm::errs() << "\n*** HeaderSearch Stats:\n"
91
3
               << FileInfo.size() << " files tracked.\n";
92
3
  unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
93
154
  for (unsigned i = 0, e = FileInfo.size(); i != e; 
++i151
) {
94
151
    NumOnceOnlyFiles += FileInfo[i].isImport;
95
151
    if (MaxNumIncludes < FileInfo[i].NumIncludes)
96
3
      MaxNumIncludes = FileInfo[i].NumIncludes;
97
151
    NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
98
151
  }
99
3
  llvm::errs() << "  " << NumOnceOnlyFiles << " #import/#pragma once files.\n"
100
3
               << "  " << NumSingleIncludedFiles << " included exactly once.\n"
101
3
               << "  " << MaxNumIncludes << " max times a file is included.\n";
102
103
3
  llvm::errs() << "  " << NumIncluded << " #include/#include_next/#import.\n"
104
3
               << "    " << NumMultiIncludeFileOptzn
105
3
               << " #includes skipped due to the multi-include optimization.\n";
106
107
3
  llvm::errs() << NumFrameworkLookups << " framework lookups.\n"
108
3
               << NumSubFrameworkLookups << " subframework lookups.\n";
109
3
}
110
111
/// CreateHeaderMap - This method returns a HeaderMap for the specified
112
/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
113
33
const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
114
  // We expect the number of headermaps to be small, and almost always empty.
115
  // If it ever grows, use of a linear search should be re-evaluated.
116
33
  if (!HeaderMaps.empty()) {
117
22
    for (unsigned i = 0, e = HeaderMaps.size(); i != e; 
++i11
)
118
      // Pointer equality comparison of FileEntries works because they are
119
      // already uniqued by inode.
120
11
      if (HeaderMaps[i].first == FE)
121
0
        return HeaderMaps[i].second.get();
122
11
  }
123
124
33
  if (std::unique_ptr<HeaderMap> HM = HeaderMap::Create(FE, FileMgr)) {
125
29
    HeaderMaps.emplace_back(FE, std::move(HM));
126
29
    return HeaderMaps.back().second.get();
127
29
  }
128
129
4
  return nullptr;
130
4
}
131
132
/// Get filenames for all registered header maps.
133
void HeaderSearch::getHeaderMapFileNames(
134
54
    SmallVectorImpl<std::string> &Names) const {
135
54
  for (auto &HM : HeaderMaps)
136
1
    Names.push_back(std::string(HM.first->getName()));
137
54
}
138
139
4.85k
std::string HeaderSearch::getCachedModuleFileName(Module *Module) {
140
4.85k
  const FileEntry *ModuleMap =
141
4.85k
      getModuleMap().getModuleMapFileForUniquing(Module);
142
4.85k
  return getCachedModuleFileName(Module->Name, ModuleMap->getName());
143
4.85k
}
144
145
std::string HeaderSearch::getPrebuiltModuleFileName(StringRef ModuleName,
146
1.73k
                                                    bool FileMapOnly) {
147
  // First check the module name to pcm file map.
148
1.73k
  auto i(HSOpts->PrebuiltModuleFiles.find(ModuleName));
149
1.73k
  if (i != HSOpts->PrebuiltModuleFiles.end())
150
11
    return i->second;
151
152
1.72k
  if (FileMapOnly || 
HSOpts->PrebuiltModulePaths.empty()11
)
153
1.71k
    return {};
154
155
  // Then go through each prebuilt module directory and try to find the pcm
156
  // file.
157
11
  for (const std::string &Dir : HSOpts->PrebuiltModulePaths) {
158
11
    SmallString<256> Result(Dir);
159
11
    llvm::sys::fs::make_absolute(Result);
160
11
    llvm::sys::path::append(Result, ModuleName + ".pcm");
161
11
    if (getFileMgr().getFile(Result.str()))
162
8
      return std::string(Result);
163
11
  }
164
3
  return {};
165
11
}
166
167
3
std::string HeaderSearch::getPrebuiltImplicitModuleFileName(Module *Module) {
168
3
  const FileEntry *ModuleMap =
169
3
      getModuleMap().getModuleMapFileForUniquing(Module);
170
3
  StringRef ModuleName = Module->Name;
171
3
  StringRef ModuleMapPath = ModuleMap->getName();
172
3
  StringRef ModuleCacheHash = HSOpts->DisableModuleHash ? 
""0
: getModuleHash();
173
3
  for (const std::string &Dir : HSOpts->PrebuiltModulePaths) {
174
3
    SmallString<256> CachePath(Dir);
175
3
    llvm::sys::fs::make_absolute(CachePath);
176
3
    llvm::sys::path::append(CachePath, ModuleCacheHash);
177
3
    std::string FileName =
178
3
        getCachedModuleFileNameImpl(ModuleName, ModuleMapPath, CachePath);
179
3
    if (!FileName.empty() && getFileMgr().getFile(FileName))
180
2
      return FileName;
181
3
  }
182
1
  return {};
183
3
}
184
185
std::string HeaderSearch::getCachedModuleFileName(StringRef ModuleName,
186
4.89k
                                                  StringRef ModuleMapPath) {
187
4.89k
  return getCachedModuleFileNameImpl(ModuleName, ModuleMapPath,
188
4.89k
                                     getModuleCachePath());
189
4.89k
}
190
191
std::string HeaderSearch::getCachedModuleFileNameImpl(StringRef ModuleName,
192
                                                      StringRef ModuleMapPath,
193
4.90k
                                                      StringRef CachePath) {
194
  // If we don't have a module cache path or aren't supposed to use one, we
195
  // can't do anything.
196
4.90k
  if (CachePath.empty())
197
12
    return {};
198
199
4.89k
  SmallString<256> Result(CachePath);
200
4.89k
  llvm::sys::fs::make_absolute(Result);
201
202
4.89k
  if (HSOpts->DisableModuleHash) {
203
181
    llvm::sys::path::append(Result, ModuleName + ".pcm");
204
4.71k
  } else {
205
    // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
206
    // ideally be globally unique to this particular module. Name collisions
207
    // in the hash are safe (because any translation unit can only import one
208
    // module with each name), but result in a loss of caching.
209
    //
210
    // To avoid false-negatives, we form as canonical a path as we can, and map
211
    // to lower-case in case we're on a case-insensitive file system.
212
4.71k
    std::string Parent =
213
4.71k
        std::string(llvm::sys::path::parent_path(ModuleMapPath));
214
4.71k
    if (Parent.empty())
215
1
      Parent = ".";
216
4.71k
    auto Dir = FileMgr.getDirectory(Parent);
217
4.71k
    if (!Dir)
218
0
      return {};
219
4.71k
    auto DirName = FileMgr.getCanonicalName(*Dir);
220
4.71k
    auto FileName = llvm::sys::path::filename(ModuleMapPath);
221
222
4.71k
    llvm::hash_code Hash =
223
4.71k
      llvm::hash_combine(DirName.lower(), FileName.lower());
224
225
4.71k
    SmallString<128> HashStr;
226
4.71k
    llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
227
4.71k
    llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
228
4.71k
  }
229
4.89k
  return Result.str().str();
230
4.89k
}
231
232
Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch,
233
1.96M
                                   bool AllowExtraModuleMapSearch) {
234
  // Look in the module map to determine if there is a module by this name.
235
1.96M
  Module *Module = ModMap.findModule(ModuleName);
236
1.96M
  if (Module || 
!AllowSearch8.76k
||
!HSOpts->ImplicitModuleMaps8.76k
)
237
1.96M
    return Module;
238
239
8.35k
  StringRef SearchName = ModuleName;
240
8.35k
  Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
241
242
  // The facility for "private modules" -- adjacent, optional module maps named
243
  // module.private.modulemap that are supposed to define private submodules --
244
  // may have different flavors of names: FooPrivate, Foo_Private and Foo.Private.
245
  //
246
  // Foo.Private is now deprecated in favor of Foo_Private. Users of FooPrivate
247
  // should also rename to Foo_Private. Representing private as submodules
248
  // could force building unwanted dependencies into the parent module and cause
249
  // dependency cycles.
250
8.35k
  if (!Module && 
SearchName.consume_back("_Private")1.04k
)
251
7
    Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
252
8.35k
  if (!Module && 
SearchName.consume_back("Private")1.04k
)
253
4
    Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
254
8.35k
  return Module;
255
8.35k
}
256
257
Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
258
8.36k
                                   bool AllowExtraModuleMapSearch) {
259
8.36k
  Module *Module = nullptr;
260
261
  // Look through the various header search paths to load any available module
262
  // maps, searching for a module map that describes this module.
263
36.1k
  for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; 
++Idx27.7k
) {
264
35.0k
    if (SearchDirs[Idx].isFramework()) {
265
      // Search for or infer a module map for a framework. Here we use
266
      // SearchName rather than ModuleName, to permit finding private modules
267
      // named FooPrivate in buggy frameworks named Foo.
268
5.55k
      SmallString<128> FrameworkDirName;
269
5.55k
      FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
270
5.55k
      llvm::sys::path::append(FrameworkDirName, SearchName + ".framework");
271
5.55k
      if (auto FrameworkDir = FileMgr.getDirectory(FrameworkDirName)) {
272
4.48k
        bool IsSystem
273
4.48k
          = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
274
4.48k
        Module = loadFrameworkModule(ModuleName, *FrameworkDir, IsSystem);
275
4.48k
        if (Module)
276
4.47k
          break;
277
30.5k
      }
278
5.55k
    }
279
280
    // FIXME: Figure out how header maps and module maps will work together.
281
282
    // Only deal with normal search directories.
283
30.5k
    if (!SearchDirs[Idx].isNormalDir())
284
1.08k
      continue;
285
286
29.4k
    bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
287
    // Search for a module map file in this directory.
288
29.4k
    if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
289
4.78k
                          /*IsFramework*/false) == LMM_NewlyLoaded) {
290
      // We just loaded a module map file; check whether the module is
291
      // available now.
292
4.78k
      Module = ModMap.findModule(ModuleName);
293
4.78k
      if (Module)
294
2.71k
        break;
295
26.7k
    }
296
297
    // Search for a module map in a subdirectory with the same name as the
298
    // module.
299
26.7k
    SmallString<128> NestedModuleMapDirName;
300
26.7k
    NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
301
26.7k
    llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
302
26.7k
    if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
303
14
                          /*IsFramework*/false) == LMM_NewlyLoaded){
304
      // If we just loaded a module map file, look for the module again.
305
14
      Module = ModMap.findModule(ModuleName);
306
14
      if (Module)
307
14
        break;
308
26.7k
    }
309
310
    // If we've already performed the exhaustive search for module maps in this
311
    // search directory, don't do it again.
312
26.7k
    if (SearchDirs[Idx].haveSearchedAllModuleMaps())
313
17.5k
      continue;
314
315
    // Load all module maps in the immediate subdirectories of this search
316
    // directory if ModuleName was from @import.
317
9.18k
    if (AllowExtraModuleMapSearch)
318
6.97k
      loadSubdirectoryModuleMaps(SearchDirs[Idx]);
319
320
    // Look again for the module.
321
9.18k
    Module = ModMap.findModule(ModuleName);
322
9.18k
    if (Module)
323
111
      break;
324
9.18k
  }
325
326
8.36k
  return Module;
327
8.36k
}
328
329
//===----------------------------------------------------------------------===//
330
// File lookup within a DirectoryLookup scope
331
//===----------------------------------------------------------------------===//
332
333
/// getName - Return the directory or filename corresponding to this lookup
334
/// object.
335
214
StringRef DirectoryLookup::getName() const {
336
  // FIXME: Use the name from \c DirectoryEntryRef.
337
214
  if (isNormalDir())
338
149
    return getDir()->getName();
339
65
  if (isFramework())
340
65
    return getFrameworkDir()->getName();
341
0
  assert(isHeaderMap() && "Unknown DirectoryLookup");
342
0
  return getHeaderMap()->getFileName();
343
0
}
344
345
Optional<FileEntryRef> HeaderSearch::getFileAndSuggestModule(
346
    StringRef FileName, SourceLocation IncludeLoc, const DirectoryEntry *Dir,
347
    bool IsSystemHeaderDir, Module *RequestingModule,
348
4.85M
    ModuleMap::KnownHeader *SuggestedModule) {
349
  // If we have a module map that might map this header, load it and
350
  // check whether we'll have a suggestion for a module.
351
4.85M
  auto File = getFileMgr().getFileRef(FileName, /*OpenFile=*/true);
352
4.85M
  if (!File) {
353
    // For rare, surprising errors (e.g. "out of file handles"), diag the EC
354
    // message.
355
3.37M
    std::error_code EC = llvm::errorToErrorCode(File.takeError());
356
3.37M
    if (EC != llvm::errc::no_such_file_or_directory &&
357
11
        EC != llvm::errc::invalid_argument &&
358
11
        EC != llvm::errc::is_a_directory && 
EC != llvm::errc::not_a_directory1
) {
359
0
      Diags.Report(IncludeLoc, diag::err_cannot_open_file)
360
0
          << FileName << EC.message();
361
0
    }
362
3.37M
    return None;
363
3.37M
  }
364
365
  // If there is a module that corresponds to this header, suggest it.
366
1.47M
  if (!findUsableModuleForHeader(
367
1.46M
          &File->getFileEntry(), Dir ? Dir : 
File->getFileEntry().getDir()8.45k
,
368
1.47M
          RequestingModule, SuggestedModule, IsSystemHeaderDir))
369
163
    return None;
370
371
1.47M
  return *File;
372
1.47M
}
373
374
/// LookupFile - Lookup the specified file in this search path, returning it
375
/// if it exists or returning null if not.
376
Optional<FileEntryRef> DirectoryLookup::LookupFile(
377
    StringRef &Filename, HeaderSearch &HS, SourceLocation IncludeLoc,
378
    SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
379
    Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
380
    bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound,
381
5.65M
    bool &IsInHeaderMap, SmallVectorImpl<char> &MappedName) const {
382
5.65M
  InUserSpecifiedSystemFramework = false;
383
5.65M
  IsInHeaderMap = false;
384
5.65M
  MappedName.clear();
385
386
5.65M
  SmallString<1024> TmpDir;
387
5.65M
  if (isNormalDir()) {
388
    // Concatenate the requested file onto the directory.
389
4.76M
    TmpDir = getDir()->getName();
390
4.76M
    llvm::sys::path::append(TmpDir, Filename);
391
4.76M
    if (SearchPath) {
392
4.67M
      StringRef SearchPathRef(getDir()->getName());
393
4.67M
      SearchPath->clear();
394
4.67M
      SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
395
4.67M
    }
396
4.76M
    if (RelativePath) {
397
4.67M
      RelativePath->clear();
398
4.67M
      RelativePath->append(Filename.begin(), Filename.end());
399
4.67M
    }
400
401
4.76M
    return HS.getFileAndSuggestModule(TmpDir, IncludeLoc, getDir(),
402
4.76M
                                      isSystemHeaderDirectory(),
403
4.76M
                                      RequestingModule, SuggestedModule);
404
4.76M
  }
405
406
887k
  if (isFramework())
407
887k
    return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
408
887k
                             RequestingModule, SuggestedModule,
409
887k
                             InUserSpecifiedSystemFramework, IsFrameworkFound);
410
411
42
  assert(isHeaderMap() && "Unknown directory lookup");
412
42
  const HeaderMap *HM = getHeaderMap();
413
42
  SmallString<1024> Path;
414
42
  StringRef Dest = HM->lookupFilename(Filename, Path);
415
42
  if (Dest.empty())
416
22
    return None;
417
418
20
  IsInHeaderMap = true;
419
420
0
  auto FixupSearchPath = [&]() {
421
0
    if (SearchPath) {
422
0
      StringRef SearchPathRef(getName());
423
0
      SearchPath->clear();
424
0
      SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
425
0
    }
426
0
    if (RelativePath) {
427
0
      RelativePath->clear();
428
0
      RelativePath->append(Filename.begin(), Filename.end());
429
0
    }
430
0
  };
431
432
  // Check if the headermap maps the filename to a framework include
433
  // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
434
  // framework include.
435
20
  if (llvm::sys::path::is_relative(Dest)) {
436
17
    MappedName.append(Dest.begin(), Dest.end());
437
17
    Filename = StringRef(MappedName.begin(), MappedName.size());
438
17
    Optional<FileEntryRef> Result = HM->LookupFile(Filename, HS.getFileMgr());
439
17
    if (Result) {
440
0
      FixupSearchPath();
441
0
      return *Result;
442
0
    }
443
3
  } else if (auto Res = HS.getFileMgr().getOptionalFileRef(Dest)) {
444
0
    FixupSearchPath();
445
0
    return *Res;
446
0
  }
447
448
20
  return None;
449
20
}
450
451
/// Given a framework directory, find the top-most framework directory.
452
///
453
/// \param FileMgr The file manager to use for directory lookups.
454
/// \param DirName The name of the framework directory.
455
/// \param SubmodulePath Will be populated with the submodule path from the
456
/// returned top-level module to the originally named framework.
457
static const DirectoryEntry *
458
getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
459
882k
                   SmallVectorImpl<std::string> &SubmodulePath) {
460
882k
  assert(llvm::sys::path::extension(DirName) == ".framework" &&
461
882k
         "Not a framework directory");
462
463
  // Note: as an egregious but useful hack we use the real path here, because
464
  // frameworks moving between top-level frameworks to embedded frameworks tend
465
  // to be symlinked, and we base the logical structure of modules on the
466
  // physical layout. In particular, we need to deal with crazy includes like
467
  //
468
  //   #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
469
  //
470
  // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
471
  // which one should access with, e.g.,
472
  //
473
  //   #include <Bar/Wibble.h>
474
  //
475
  // Similar issues occur when a top-level framework has moved into an
476
  // embedded framework.
477
882k
  const DirectoryEntry *TopFrameworkDir = nullptr;
478
882k
  if (auto TopFrameworkDirOrErr = FileMgr.getDirectory(DirName))
479
882k
    TopFrameworkDir = *TopFrameworkDirOrErr;
480
481
882k
  if (TopFrameworkDir)
482
882k
    DirName = FileMgr.getCanonicalName(TopFrameworkDir);
483
12.7M
  do {
484
    // Get the parent directory name.
485
12.7M
    DirName = llvm::sys::path::parent_path(DirName);
486
12.7M
    if (DirName.empty())
487
882k
      break;
488
489
    // Determine whether this directory exists.
490
11.8M
    auto Dir = FileMgr.getDirectory(DirName);
491
11.8M
    if (!Dir)
492
0
      break;
493
494
    // If this is a framework directory, then we're a subframework of this
495
    // framework.
496
11.8M
    if (llvm::sys::path::extension(DirName) == ".framework") {
497
89.2k
      SubmodulePath.push_back(std::string(llvm::sys::path::stem(DirName)));
498
89.2k
      TopFrameworkDir = *Dir;
499
89.2k
    }
500
11.8M
  } while (true);
501
502
882k
  return TopFrameworkDir;
503
882k
}
504
505
static bool needModuleLookup(Module *RequestingModule,
506
3.15M
                             bool HasSuggestedModule) {
507
3.15M
  return HasSuggestedModule ||
508
20.8k
         (RequestingModule && 
RequestingModule->NoUndeclaredIncludes815
);
509
3.15M
}
510
511
/// DoFrameworkLookup - Do a lookup of the specified file in the current
512
/// DirectoryLookup, which is a framework directory.
513
Optional<FileEntryRef> DirectoryLookup::DoFrameworkLookup(
514
    StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
515
    SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
516
    ModuleMap::KnownHeader *SuggestedModule,
517
887k
    bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound) const {
518
887k
  FileManager &FileMgr = HS.getFileMgr();
519
520
  // Framework names must have a '/' in the filename.
521
887k
  size_t SlashPos = Filename.find('/');
522
887k
  if (SlashPos == StringRef::npos)
523
8.28k
    return None;
524
525
  // Find out if this is the home for the specified framework, by checking
526
  // HeaderSearch.  Possible answers are yes/no and unknown.
527
879k
  FrameworkCacheEntry &CacheEntry =
528
879k
    HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
529
530
  // If it is known and in some other directory, fail.
531
879k
  if (CacheEntry.Directory && 
CacheEntry.Directory != getFrameworkDir()861k
)
532
74.8k
    return None;
533
534
  // Otherwise, construct the path to this framework dir.
535
536
  // FrameworkName = "/System/Library/Frameworks/"
537
804k
  SmallString<1024> FrameworkName;
538
804k
  FrameworkName += getFrameworkDirRef()->getName();
539
804k
  if (FrameworkName.empty() || FrameworkName.back() != '/')
540
804k
    FrameworkName.push_back('/');
541
542
  // FrameworkName = "/System/Library/Frameworks/Cocoa"
543
804k
  StringRef ModuleName(Filename.begin(), SlashPos);
544
804k
  FrameworkName += ModuleName;
545
546
  // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
547
804k
  FrameworkName += ".framework/";
548
549
  // If the cache entry was unresolved, populate it now.
550
804k
  if (!CacheEntry.Directory) {
551
18.2k
    ++NumFrameworkLookups;
552
553
    // If the framework dir doesn't exist, we fail.
554
18.2k
    auto Dir = FileMgr.getDirectory(FrameworkName);
555
18.2k
    if (!Dir)
556
10.4k
      return None;
557
558
    // Otherwise, if it does, remember that this is the right direntry for this
559
    // framework.
560
7.85k
    CacheEntry.Directory = getFrameworkDir();
561
562
    // If this is a user search directory, check if the framework has been
563
    // user-specified as a system framework.
564
7.85k
    if (getDirCharacteristic() == SrcMgr::C_User) {
565
237
      SmallString<1024> SystemFrameworkMarker(FrameworkName);
566
237
      SystemFrameworkMarker += ".system_framework";
567
237
      if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
568
5
        CacheEntry.IsUserSpecifiedSystemFramework = true;
569
5
      }
570
237
    }
571
7.85k
  }
572
573
  // Set out flags.
574
794k
  InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
575
794k
  IsFrameworkFound = CacheEntry.Directory;
576
577
794k
  if (RelativePath) {
578
793k
    RelativePath->clear();
579
793k
    RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
580
793k
  }
581
582
  // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
583
794k
  unsigned OrigSize = FrameworkName.size();
584
585
794k
  FrameworkName += "Headers/";
586
587
794k
  if (SearchPath) {
588
793k
    SearchPath->clear();
589
    // Without trailing '/'.
590
793k
    SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
591
793k
  }
592
593
794k
  FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
594
595
794k
  auto File =
596
794k
      FileMgr.getOptionalFileRef(FrameworkName, /*OpenFile=*/!SuggestedModule);
597
794k
  if (!File) {
598
    // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
599
614
    const char *Private = "Private";
600
614
    FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
601
614
                         Private+strlen(Private));
602
614
    if (SearchPath)
603
33
      SearchPath->insert(SearchPath->begin()+OrigSize, Private,
604
33
                         Private+strlen(Private));
605
606
614
    File = FileMgr.getOptionalFileRef(FrameworkName,
607
614
                                      /*OpenFile=*/!SuggestedModule);
608
614
  }
609
610
  // If we found the header and are allowed to suggest a module, do so now.
611
794k
  if (File && 
needModuleLookup(RequestingModule, SuggestedModule)793k
) {
612
    // Find the framework in which this header occurs.
613
793k
    StringRef FrameworkPath = File->getFileEntry().getDir()->getName();
614
793k
    bool FoundFramework = false;
615
1.58M
    do {
616
      // Determine whether this directory exists.
617
1.58M
      auto Dir = FileMgr.getDirectory(FrameworkPath);
618
1.58M
      if (!Dir)
619
0
        break;
620
621
      // If this is a framework directory, then we're a subframework of this
622
      // framework.
623
1.58M
      if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
624
793k
        FoundFramework = true;
625
793k
        break;
626
793k
      }
627
628
      // Get the parent directory name.
629
795k
      FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
630
795k
      if (FrameworkPath.empty())
631
0
        break;
632
795k
    } while (true);
633
634
793k
    bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
635
795k
    if (FoundFramework) {
636
793k
      if (!HS.findUsableModuleForFrameworkHeader(
637
793k
              &File->getFileEntry(), FrameworkPath, RequestingModule,
638
793k
              SuggestedModule, IsSystem))
639
0
        return None;
640
2.37k
    } else {
641
2.37k
      if (!HS.findUsableModuleForHeader(&File->getFileEntry(), getDir(),
642
2.37k
                                        RequestingModule, SuggestedModule,
643
2.37k
                                        IsSystem))
644
0
        return None;
645
796k
    }
646
793k
  }
647
796k
  if (File)
648
793k
    return *File;
649
2.96k
  return None;
650
2.96k
}
651
652
83.1k
void HeaderSearch::setTarget(const TargetInfo &Target) {
653
83.1k
  ModMap.setTarget(Target);
654
83.1k
}
655
656
//===----------------------------------------------------------------------===//
657
// Header File Location.
658
//===----------------------------------------------------------------------===//
659
660
/// Return true with a diagnostic if the file that MSVC would have found
661
/// fails to match the one that Clang would have found with MSVC header search
662
/// disabled.
663
static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
664
                                  const FileEntry *MSFE, const FileEntry *FE,
665
2.31M
                                  SourceLocation IncludeLoc) {
666
2.31M
  if (MSFE && 
FE != MSFE2
) {
667
1
    Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
668
1
    return true;
669
1
  }
670
2.31M
  return false;
671
2.31M
}
672
673
17
static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
674
17
  assert(!Str.empty());
675
17
  char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
676
17
  std::copy(Str.begin(), Str.end(), CopyStr);
677
17
  CopyStr[Str.size()] = '\0';
678
17
  return CopyStr;
679
17
}
680
681
static bool isFrameworkStylePath(StringRef Path, bool &IsPrivateHeader,
682
3.22M
                                 SmallVectorImpl<char> &FrameworkName) {
683
3.22M
  using namespace llvm::sys;
684
3.22M
  path::const_iterator I = path::begin(Path);
685
3.22M
  path::const_iterator E = path::end(Path);
686
3.22M
  IsPrivateHeader = false;
687
688
  // Detect different types of framework style paths:
689
  //
690
  //   ...Foo.framework/{Headers,PrivateHeaders}
691
  //   ...Foo.framework/Versions/{A,Current}/{Headers,PrivateHeaders}
692
  //   ...Foo.framework/Frameworks/Nested.framework/{Headers,PrivateHeaders}
693
  //   ...<other variations with 'Versions' like in the above path>
694
  //
695
  // and some other variations among these lines.
696
3.22M
  int FoundComp = 0;
697
49.2M
  while (I != E) {
698
45.9M
    if (*I == "Headers")
699
1.79M
      ++FoundComp;
700
45.9M
    if (I->endswith(".framework")) {
701
1.90M
      FrameworkName.append(I->begin(), I->end());
702
1.90M
      ++FoundComp;
703
1.90M
    }
704
45.9M
    if (*I == "PrivateHeaders") {
705
22
      ++FoundComp;
706
22
      IsPrivateHeader = true;
707
22
    }
708
45.9M
    ++I;
709
45.9M
  }
710
711
3.22M
  return !FrameworkName.empty() && 
FoundComp >= 21.79M
;
712
3.22M
}
713
714
static void
715
diagnoseFrameworkInclude(DiagnosticsEngine &Diags, SourceLocation IncludeLoc,
716
                         StringRef Includer, StringRef IncludeFilename,
717
                         const FileEntry *IncludeFE, bool isAngled = false,
718
2.22M
                         bool FoundByHeaderMap = false) {
719
2.22M
  bool IsIncluderPrivateHeader = false;
720
2.22M
  SmallString<128> FromFramework, ToFramework;
721
2.22M
  if (!isFrameworkStylePath(Includer, IsIncluderPrivateHeader, FromFramework))
722
1.22M
    return;
723
996k
  bool IsIncludeePrivateHeader = false;
724
996k
  bool IsIncludeeInFramework = isFrameworkStylePath(
725
996k
      IncludeFE->getName(), IsIncludeePrivateHeader, ToFramework);
726
727
996k
  if (!isAngled && 
!FoundByHeaderMap2.94k
) {
728
2.93k
    SmallString<128> NewInclude("<");
729
2.93k
    if (IsIncludeeInFramework) {
730
2.91k
      NewInclude += StringRef(ToFramework).drop_back(10); // drop .framework
731
2.91k
      NewInclude += "/";
732
2.91k
    }
733
2.93k
    NewInclude += IncludeFilename;
734
2.93k
    NewInclude += ">";
735
2.93k
    Diags.Report(IncludeLoc, diag::warn_quoted_include_in_framework_header)
736
2.93k
        << IncludeFilename
737
2.93k
        << FixItHint::CreateReplacement(IncludeLoc, NewInclude);
738
2.93k
  }
739
740
  // Headers in Foo.framework/Headers should not include headers
741
  // from Foo.framework/PrivateHeaders, since this violates public/private
742
  // API boundaries and can cause modular dependency cycles.
743
996k
  if (!IsIncluderPrivateHeader && 
IsIncludeeInFramework996k
&&
744
795k
      IsIncludeePrivateHeader && 
FromFramework == ToFramework8
)
745
8
    Diags.Report(IncludeLoc, diag::warn_framework_include_private_from_public)
746
8
        << IncludeFilename;
747
996k
}
748
749
/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
750
/// return null on failure.  isAngled indicates whether the file reference is
751
/// for system \#include's or not (i.e. using <> instead of ""). Includers, if
752
/// non-empty, indicates where the \#including file(s) are, in case a relative
753
/// search is needed. Microsoft mode will pass all \#including files.
754
Optional<FileEntryRef> HeaderSearch::LookupFile(
755
    StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
756
    const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
757
    ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
758
    SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
759
    Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
760
    bool *IsMapped, bool *IsFrameworkFound, bool SkipCache,
761
2.37M
    bool BuildSystemModule) {
762
2.37M
  if (IsMapped)
763
2.33M
    *IsMapped = false;
764
765
2.37M
  if (IsFrameworkFound)
766
2.33M
    *IsFrameworkFound = false;
767
768
2.37M
  if (SuggestedModule)
769
2.33M
    *SuggestedModule = ModuleMap::KnownHeader();
770
771
  // If 'Filename' is absolute, check to see if it exists and no searching.
772
2.37M
  if (llvm::sys::path::is_absolute(Filename)) {
773
8.46k
    CurDir = nullptr;
774
775
    // If this was an #include_next "/absolute/file", fail.
776
8.46k
    if (FromDir)
777
0
      return None;
778
779
8.46k
    if (SearchPath)
780
8.44k
      SearchPath->clear();
781
8.46k
    if (RelativePath) {
782
8.44k
      RelativePath->clear();
783
8.44k
      RelativePath->append(Filename.begin(), Filename.end());
784
8.44k
    }
785
    // Otherwise, just return the file.
786
8.46k
    return getFileAndSuggestModule(Filename, IncludeLoc, nullptr,
787
8.46k
                                   /*IsSystemHeaderDir*/false,
788
8.46k
                                   RequestingModule, SuggestedModule);
789
8.46k
  }
790
791
  // This is the header that MSVC's header search would have found.
792
2.36M
  ModuleMap::KnownHeader MSSuggestedModule;
793
2.36M
  Optional<FileEntryRef> MSFE;
794
795
  // Unless disabled, check to see if the file is in the #includer's
796
  // directory.  This cannot be based on CurDir, because each includer could be
797
  // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
798
  // include of "baz.h" should resolve to "whatever/foo/baz.h".
799
  // This search is not done for <> headers.
800
2.36M
  if (!Includers.empty() && 
!isAngled2.33M
&&
!NoCurDirSearch81.6k
) {
801
81.6k
    SmallString<1024> TmpDir;
802
81.6k
    bool First = true;
803
81.6k
    for (const auto &IncluderAndDir : Includers) {
804
81.6k
      const FileEntry *Includer = IncluderAndDir.first;
805
806
      // Concatenate the requested file onto the directory.
807
      // FIXME: Portability.  Filename concatenation should be in sys::Path.
808
81.6k
      TmpDir = IncluderAndDir.second->getName();
809
81.6k
      TmpDir.push_back('/');
810
81.6k
      TmpDir.append(Filename.begin(), Filename.end());
811
812
      // FIXME: We don't cache the result of getFileInfo across the call to
813
      // getFileAndSuggestModule, because it's a reference to an element of
814
      // a container that could be reallocated across this call.
815
      //
816
      // If we have no includer, that means we're processing a #include
817
      // from a module build. We should treat this as a system header if we're
818
      // building a [system] module.
819
81.6k
      bool IncluderIsSystemHeader =
820
66.3k
          Includer ? getFileInfo(Includer).DirInfo != SrcMgr::C_User :
821
15.3k
          BuildSystemModule;
822
81.6k
      if (Optional<FileEntryRef> FE = getFileAndSuggestModule(
823
52.2k
              TmpDir, IncludeLoc, IncluderAndDir.second, IncluderIsSystemHeader,
824
52.2k
              RequestingModule, SuggestedModule)) {
825
52.2k
        if (!Includer) {
826
15.3k
          assert(First && "only first includer can have no file");
827
15.3k
          return FE;
828
15.3k
        }
829
830
        // Leave CurDir unset.
831
        // This file is a system header or C++ unfriendly if the old file is.
832
        //
833
        // Note that we only use one of FromHFI/ToHFI at once, due to potential
834
        // reallocation of the underlying vector potentially making the first
835
        // reference binding dangling.
836
36.8k
        HeaderFileInfo &FromHFI = getFileInfo(Includer);
837
36.8k
        unsigned DirInfo = FromHFI.DirInfo;
838
36.8k
        bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
839
36.8k
        StringRef Framework = FromHFI.Framework;
840
841
36.8k
        HeaderFileInfo &ToHFI = getFileInfo(&FE->getFileEntry());
842
36.8k
        ToHFI.DirInfo = DirInfo;
843
36.8k
        ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
844
36.8k
        ToHFI.Framework = Framework;
845
846
36.8k
        if (SearchPath) {
847
36.5k
          StringRef SearchPathRef(IncluderAndDir.second->getName());
848
36.5k
          SearchPath->clear();
849
36.5k
          SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
850
36.5k
        }
851
36.8k
        if (RelativePath) {
852
36.5k
          RelativePath->clear();
853
36.5k
          RelativePath->append(Filename.begin(), Filename.end());
854
36.5k
        }
855
36.8k
        if (First) {
856
36.8k
          diagnoseFrameworkInclude(Diags, IncludeLoc,
857
36.8k
                                   IncluderAndDir.second->getName(), Filename,
858
36.8k
                                   &FE->getFileEntry());
859
36.8k
          return FE;
860
36.8k
        }
861
862
        // Otherwise, we found the path via MSVC header search rules.  If
863
        // -Wmsvc-include is enabled, we have to keep searching to see if we
864
        // would've found this header in -I or -isystem directories.
865
6
        if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
866
4
          return FE;
867
2
        } else {
868
2
          MSFE = FE;
869
2
          if (SuggestedModule) {
870
2
            MSSuggestedModule = *SuggestedModule;
871
2
            *SuggestedModule = ModuleMap::KnownHeader();
872
2
          }
873
2
          break;
874
2
        }
875
29.4k
      }
876
29.4k
      First = false;
877
29.4k
    }
878
81.6k
  }
879
880
2.31M
  CurDir = nullptr;
881
882
  // If this is a system #include, ignore the user #include locs.
883
2.28M
  unsigned i = isAngled ? AngledDirIdx : 
029.4k
;
884
885
  // If this is a #include_next request, start searching after the directory the
886
  // file was found in.
887
2.31M
  if (FromDir)
888
24.5k
    i = FromDir-&SearchDirs[0];
889
890
  // Cache all of the lookups performed by this method.  Many headers are
891
  // multiply included, and the "pragma once" optimization prevents them from
892
  // being relex/pp'd, but they would still have to search through a
893
  // (potentially huge) series of SearchDirs to find it.
894
2.31M
  LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
895
896
  // If the entry has been previously looked up, the first value will be
897
  // non-zero.  If the value is equal to i (the start point of our search), then
898
  // this is a matching hit.
899
2.31M
  if (!SkipCache && 
CacheLookup.StartIdx == i+12.31M
) {
900
    // Skip querying potentially lots of directories for this lookup.
901
1.53M
    i = CacheLookup.HitIdx;
902
1.53M
    if (CacheLookup.MappedName) {
903
6
      Filename = CacheLookup.MappedName;
904
6
      if (IsMapped)
905
6
        *IsMapped = true;
906
6
    }
907
780k
  } else {
908
    // Otherwise, this is the first query, or the previous query didn't match
909
    // our search start.  We will fill in our found location below, so prime the
910
    // start point value.
911
780k
    CacheLookup.reset(/*StartIdx=*/i+1);
912
780k
  }
913
914
2.31M
  SmallString<64> MappedName;
915
916
  // Check each directory in sequence to see if it contains this file.
917
5.75M
  for (; i != SearchDirs.size(); 
++i3.44M
) {
918
5.65M
    bool InUserSpecifiedSystemFramework = false;
919
5.65M
    bool IsInHeaderMap = false;
920
5.65M
    bool IsFrameworkFoundInDir = false;
921
5.65M
    Optional<FileEntryRef> File = SearchDirs[i].LookupFile(
922
5.65M
        Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
923
5.65M
        SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
924
5.65M
        IsInHeaderMap, MappedName);
925
5.65M
    if (!MappedName.empty()) {
926
17
      assert(IsInHeaderMap && "MappedName should come from a header map");
927
17
      CacheLookup.MappedName =
928
17
          copyString(MappedName, LookupFileCache.getAllocator());
929
17
    }
930
5.65M
    if (IsMapped)
931
      // A filename is mapped when a header map remapped it to a relative path
932
      // used in subsequent header search or to an absolute path pointing to an
933
      // existing file.
934
5.54M
      *IsMapped |= (!MappedName.empty() || 
(5.54M
IsInHeaderMap5.54M
&&
File0
));
935
5.65M
    if (IsFrameworkFound)
936
      // Because we keep a filename remapped for subsequent search directory
937
      // lookups, ignore IsFrameworkFoundInDir after the first remapping and not
938
      // just for remapping in a current search directory.
939
5.54M
      *IsFrameworkFound |= (IsFrameworkFoundInDir && 
!CacheLookup.MappedName793k
);
940
5.65M
    if (!File)
941
3.44M
      continue;
942
943
2.21M
    CurDir = &SearchDirs[i];
944
945
    // This file is a system header or C++ unfriendly if the dir is.
946
2.21M
    HeaderFileInfo &HFI = getFileInfo(&File->getFileEntry());
947
2.21M
    HFI.DirInfo = CurDir->getDirCharacteristic();
948
949
    // If the directory characteristic is User but this framework was
950
    // user-specified to be treated as a system framework, promote the
951
    // characteristic.
952
2.21M
    if (HFI.DirInfo == SrcMgr::C_User && 
InUserSpecifiedSystemFramework7.76k
)
953
1
      HFI.DirInfo = SrcMgr::C_System;
954
955
    // If the filename matches a known system header prefix, override
956
    // whether the file is a system header.
957
2.21M
    for (unsigned j = SystemHeaderPrefixes.size(); j; 
--j12
) {
958
20
      if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
959
4
        HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
960
4
                                                       : SrcMgr::C_User;
961
8
        break;
962
8
      }
963
20
    }
964
965
    // If this file is found in a header map and uses the framework style of
966
    // includes, then this header is part of a framework we're building.
967
2.21M
    if (CurDir->isIndexHeaderMap()) {
968
0
      size_t SlashPos = Filename.find('/');
969
0
      if (SlashPos != StringRef::npos) {
970
0
        HFI.IndexHeaderMapHeader = 1;
971
0
        HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
972
0
                                                         SlashPos));
973
0
      }
974
0
    }
975
976
2.21M
    if (checkMSVCHeaderSearch(Diags, MSFE ? 
&MSFE->getFileEntry()2
:
nullptr2.21M
,
977
1
                              &File->getFileEntry(), IncludeLoc)) {
978
1
      if (SuggestedModule)
979
1
        *SuggestedModule = MSSuggestedModule;
980
1
      return MSFE;
981
1
    }
982
983
2.21M
    bool FoundByHeaderMap = !IsMapped ? 
false20.7k
:
*IsMapped2.18M
;
984
2.21M
    if (!Includers.empty())
985
2.18M
      diagnoseFrameworkInclude(
986
2.18M
          Diags, IncludeLoc, Includers.front().second->getName(), Filename,
987
2.18M
          &File->getFileEntry(), isAngled, FoundByHeaderMap);
988
989
    // Remember this location for the next lookup we do.
990
2.21M
    CacheLookup.HitIdx = i;
991
2.21M
    return File;
992
2.21M
  }
993
994
  // If we are including a file with a quoted include "foo.h" from inside
995
  // a header in a framework that is currently being built, and we couldn't
996
  // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
997
  // "Foo" is the name of the framework in which the including header was found.
998
101k
  if (!Includers.empty() && 
Includers.front().first99.4k
&&
!isAngled99.4k
&&
999
140
      Filename.find('/') == StringRef::npos) {
1000
94
    HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
1001
94
    if (IncludingHFI.IndexHeaderMapHeader) {
1002
0
      SmallString<128> ScratchFilename;
1003
0
      ScratchFilename += IncludingHFI.Framework;
1004
0
      ScratchFilename += '/';
1005
0
      ScratchFilename += Filename;
1006
1007
0
      Optional<FileEntryRef> File = LookupFile(
1008
0
          ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, CurDir,
1009
0
          Includers.front(), SearchPath, RelativePath, RequestingModule,
1010
0
          SuggestedModule, IsMapped, /*IsFrameworkFound=*/nullptr);
1011
1012
0
      if (checkMSVCHeaderSearch(Diags, MSFE ? &MSFE->getFileEntry() : nullptr,
1013
0
                                File ? &File->getFileEntry() : nullptr,
1014
0
                                IncludeLoc)) {
1015
0
        if (SuggestedModule)
1016
0
          *SuggestedModule = MSSuggestedModule;
1017
0
        return MSFE;
1018
0
      }
1019
1020
0
      LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
1021
0
      CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
1022
      // FIXME: SuggestedModule.
1023
0
      return File;
1024
0
    }
1025
94
  }
1026
1027
101k
  if (checkMSVCHeaderSearch(Diags, MSFE ? 
&MSFE->getFileEntry()0
: nullptr,
1028
0
                            nullptr, IncludeLoc)) {
1029
0
    if (SuggestedModule)
1030
0
      *SuggestedModule = MSSuggestedModule;
1031
0
    return MSFE;
1032
0
  }
1033
1034
  // Otherwise, didn't find it. Remember we didn't find this.
1035
101k
  CacheLookup.HitIdx = SearchDirs.size();
1036
101k
  return None;
1037
101k
}
1038
1039
/// LookupSubframeworkHeader - Look up a subframework for the specified
1040
/// \#include file.  For example, if \#include'ing <HIToolbox/HIToolbox.h> from
1041
/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
1042
/// is a subframework within Carbon.framework.  If so, return the FileEntry
1043
/// for the designated file, otherwise return null.
1044
Optional<FileEntryRef> HeaderSearch::LookupSubframeworkHeader(
1045
    StringRef Filename, const FileEntry *ContextFileEnt,
1046
    SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
1047
154k
    Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
1048
154k
  assert(ContextFileEnt && "No context file?");
1049
1050
  // Framework names must have a '/' in the filename.  Find it.
1051
  // FIXME: Should we permit '\' on Windows?
1052
154k
  size_t SlashPos = Filename.find('/');
1053
154k
  if (SlashPos == StringRef::npos)
1054
59.9k
    return None;
1055
1056
  // Look up the base framework name of the ContextFileEnt.
1057
95.0k
  StringRef ContextName = ContextFileEnt->getName();
1058
1059
  // If the context info wasn't a framework, couldn't be a subframework.
1060
95.0k
  const unsigned DotFrameworkLen = 10;
1061
95.0k
  auto FrameworkPos = ContextName.find(".framework");
1062
95.0k
  if (FrameworkPos == StringRef::npos ||
1063
93.1k
      (ContextName[FrameworkPos + DotFrameworkLen] != '/' &&
1064
0
       ContextName[FrameworkPos + DotFrameworkLen] != '\\'))
1065
1.95k
    return None;
1066
1067
93.1k
  SmallString<1024> FrameworkName(ContextName.data(), ContextName.data() +
1068
93.1k
                                                          FrameworkPos +
1069
93.1k
                                                          DotFrameworkLen + 1);
1070
1071
  // Append Frameworks/HIToolbox.framework/
1072
93.1k
  FrameworkName += "Frameworks/";
1073
93.1k
  FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
1074
93.1k
  FrameworkName += ".framework/";
1075
1076
93.1k
  auto &CacheLookup =
1077
93.1k
      *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
1078
93.1k
                                          FrameworkCacheEntry())).first;
1079
1080
  // Some other location?
1081
93.1k
  if (CacheLookup.second.Directory &&
1082
81.9k
      CacheLookup.first().size() == FrameworkName.size() &&
1083
0
      memcmp(CacheLookup.first().data(), &FrameworkName[0],
1084
0
             CacheLookup.first().size()) != 0)
1085
0
    return None;
1086
1087
  // Cache subframework.
1088
93.1k
  if (!CacheLookup.second.Directory) {
1089
11.1k
    ++NumSubFrameworkLookups;
1090
1091
    // If the framework dir doesn't exist, we fail.
1092
11.1k
    auto Dir = FileMgr.getDirectory(FrameworkName);
1093
11.1k
    if (!Dir)
1094
1.57k
      return None;
1095
1096
    // Otherwise, if it does, remember that this is the right direntry for this
1097
    // framework.
1098
9.61k
    CacheLookup.second.Directory = *Dir;
1099
9.61k
  }
1100
1101
1102
91.5k
  if (RelativePath) {
1103
89.2k
    RelativePath->clear();
1104
89.2k
    RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
1105
89.2k
  }
1106
1107
  // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
1108
91.5k
  SmallString<1024> HeadersFilename(FrameworkName);
1109
91.5k
  HeadersFilename += "Headers/";
1110
91.5k
  if (SearchPath) {
1111
89.2k
    SearchPath->clear();
1112
    // Without trailing '/'.
1113
89.2k
    SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1114
89.2k
  }
1115
1116
91.5k
  HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1117
91.5k
  auto File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
1118
91.5k
  if (!File) {
1119
    // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
1120
2.30k
    HeadersFilename = FrameworkName;
1121
2.30k
    HeadersFilename += "PrivateHeaders/";
1122
2.30k
    if (SearchPath) {
1123
2
      SearchPath->clear();
1124
      // Without trailing '/'.
1125
2
      SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1126
2
    }
1127
1128
2.30k
    HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1129
2.30k
    File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
1130
1131
2.30k
    if (!File)
1132
2.30k
      return None;
1133
89.2k
  }
1134
1135
  // This file is a system header or C++ unfriendly if the old file is.
1136
  //
1137
  // Note that the temporary 'DirInfo' is required here, as either call to
1138
  // getFileInfo could resize the vector and we don't want to rely on order
1139
  // of evaluation.
1140
89.2k
  unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
1141
89.2k
  getFileInfo(&File->getFileEntry()).DirInfo = DirInfo;
1142
1143
89.2k
  FrameworkName.pop_back(); // remove the trailing '/'
1144
89.2k
  if (!findUsableModuleForFrameworkHeader(&File->getFileEntry(), FrameworkName,
1145
89.2k
                                          RequestingModule, SuggestedModule,
1146
89.2k
                                          /*IsSystem*/ false))
1147
0
    return None;
1148
1149
89.2k
  return *File;
1150
89.2k
}
1151
1152
//===----------------------------------------------------------------------===//
1153
// File Info Management.
1154
//===----------------------------------------------------------------------===//
1155
1156
/// Merge the header file info provided by \p OtherHFI into the current
1157
/// header file info (\p HFI)
1158
static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
1159
86.8k
                                const HeaderFileInfo &OtherHFI) {
1160
86.8k
  assert(OtherHFI.External && "expected to merge external HFI");
1161
1162
86.8k
  HFI.isImport |= OtherHFI.isImport;
1163
86.8k
  HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
1164
86.8k
  HFI.isModuleHeader |= OtherHFI.isModuleHeader;
1165
86.8k
  HFI.NumIncludes += OtherHFI.NumIncludes;
1166
1167
86.8k
  if (!HFI.ControllingMacro && 
!HFI.ControllingMacroID86.8k
) {
1168
86.8k
    HFI.ControllingMacro = OtherHFI.ControllingMacro;
1169
86.8k
    HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
1170
86.8k
  }
1171
1172
86.8k
  HFI.DirInfo = OtherHFI.DirInfo;
1173
86.8k
  HFI.External = (!HFI.IsValid || 
HFI.External82.2k
);
1174
86.8k
  HFI.IsValid = true;
1175
86.8k
  HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
1176
1177
86.8k
  if (HFI.Framework.empty())
1178
86.8k
    HFI.Framework = OtherHFI.Framework;
1179
86.8k
}
1180
1181
/// getFileInfo - Return the HeaderFileInfo structure for the specified
1182
/// FileEntry.
1183
9.16M
HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
1184
9.16M
  if (FE->getUID() >= FileInfo.size())
1185
1.87M
    FileInfo.resize(FE->getUID() + 1);
1186
1187
9.16M
  HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
1188
  // FIXME: Use a generation count to check whether this is really up to date.
1189
9.16M
  if (ExternalSource && 
!HFI->Resolved323k
) {
1190
296k
    auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1191
296k
    if (ExternalHFI.IsValid) {
1192
5.91k
      HFI->Resolved = true;
1193
5.91k
      if (ExternalHFI.External)
1194
5.91k
        mergeHeaderFileInfo(*HFI, ExternalHFI);
1195
5.91k
    }
1196
296k
  }
1197
1198
9.16M
  HFI->IsValid = true;
1199
  // We have local information about this header file, so it's no longer
1200
  // strictly external.
1201
9.16M
  HFI->External = false;
1202
9.16M
  return *HFI;
1203
9.16M
}
1204
1205
const HeaderFileInfo *
1206
HeaderSearch::getExistingFileInfo(const FileEntry *FE,
1207
1.60M
                                  bool WantExternal) const {
1208
  // If we have an external source, ensure we have the latest information.
1209
  // FIXME: Use a generation count to check whether this is really up to date.
1210
1.60M
  HeaderFileInfo *HFI;
1211
1.60M
  if (ExternalSource) {
1212
433k
    if (FE->getUID() >= FileInfo.size()) {
1213
78.8k
      if (!WantExternal)
1214
0
        return nullptr;
1215
78.8k
      FileInfo.resize(FE->getUID() + 1);
1216
78.8k
    }
1217
1218
433k
    HFI = &FileInfo[FE->getUID()];
1219
433k
    if (!WantExternal && 
(264k
!HFI->IsValid264k
||
HFI->External151k
))
1220
113k
      return nullptr;
1221
320k
    if (!HFI->Resolved) {
1222
307k
      auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1223
307k
      if (ExternalHFI.IsValid) {
1224
80.9k
        HFI->Resolved = true;
1225
80.9k
        if (ExternalHFI.External)
1226
80.9k
          mergeHeaderFileInfo(*HFI, ExternalHFI);
1227
80.9k
      }
1228
307k
    }
1229
1.17M
  } else if (FE->getUID() >= FileInfo.size()) {
1230
1.03M
    return nullptr;
1231
142k
  } else {
1232
142k
    HFI = &FileInfo[FE->getUID()];
1233
142k
  }
1234
1235
462k
  if (!HFI->IsValid || 
(261k
HFI->External261k
&&
!WantExternal1.78k
))
1236
200k
    return nullptr;
1237
1238
261k
  return HFI;
1239
261k
}
1240
1241
606
bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1242
  // Check if we've entered this file and found an include guard or #pragma
1243
  // once. Note that we dor't check for #import, because that's not a property
1244
  // of the file itself.
1245
606
  if (auto *HFI = getExistingFileInfo(File))
1246
606
    return HFI->isPragmaOnce || 
HFI->ControllingMacro598
||
1247
581
           HFI->ControllingMacroID;
1248
0
  return false;
1249
0
}
1250
1251
void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
1252
                                        ModuleMap::ModuleHeaderRole Role,
1253
1.29M
                                        bool isCompilingModuleHeader) {
1254
1.29M
  bool isModularHeader = !(Role & ModuleMap::TextualHeader);
1255
1256
  // Don't mark the file info as non-external if there's nothing to change.
1257
1.29M
  if (!isCompilingModuleHeader) {
1258
1.28M
    if (!isModularHeader)
1259
84.7k
      return;
1260
1.20M
    auto *HFI = getExistingFileInfo(FE);
1261
1.20M
    if (HFI && 
HFI->isModuleHeader3.54k
)
1262
3.33k
      return;
1263
1.21M
  }
1264
1265
1.21M
  auto &HFI = getFileInfo(FE);
1266
1.21M
  HFI.isModuleHeader |= isModularHeader;
1267
1.21M
  HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
1268
1.21M
}
1269
1270
bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1271
                                          const FileEntry *File, bool isImport,
1272
2.32M
                                          bool ModulesEnabled, Module *M) {
1273
2.32M
  ++NumIncluded; // Count # of attempted #includes.
1274
1275
  // Get information about this file.
1276
2.32M
  HeaderFileInfo &FileInfo = getFileInfo(File);
1277
1278
  // FIXME: this is a workaround for the lack of proper modules-aware support
1279
  // for #import / #pragma once
1280
304k
  auto TryEnterImported = [&]() -> bool {
1281
304k
    if (!ModulesEnabled)
1282
294k
      return false;
1283
    // Ensure FileInfo bits are up to date.
1284
10.5k
    ModMap.resolveHeaderDirectives(File);
1285
    // Modules with builtins are special; multiple modules use builtins as
1286
    // modular headers, example:
1287
    //
1288
    //    module stddef { header "stddef.h" export * }
1289
    //
1290
    // After module map parsing, this expands to:
1291
    //
1292
    //    module stddef {
1293
    //      header "/path_to_builtin_dirs/stddef.h"
1294
    //      textual "stddef.h"
1295
    //    }
1296
    //
1297
    // It's common that libc++ and system modules will both define such
1298
    // submodules. Make sure cached results for a builtin header won't
1299
    // prevent other builtin modules from potentially entering the builtin
1300
    // header. Note that builtins are header guarded and the decision to
1301
    // actually enter them is postponed to the controlling macros logic below.
1302
10.5k
    bool TryEnterHdr = false;
1303
10.5k
    if (FileInfo.isCompilingModuleHeader && 
FileInfo.isModuleHeader5.67k
)
1304
5.67k
      TryEnterHdr = ModMap.isBuiltinHeader(File);
1305
1306
    // Textual headers can be #imported from different modules. Since ObjC
1307
    // headers find in the wild might rely only on #import and do not contain
1308
    // controlling macros, be conservative and only try to enter textual headers
1309
    // if such macro is present.
1310
10.5k
    if (!FileInfo.isModuleHeader &&
1311
4.91k
        FileInfo.getControllingMacro(ExternalLookup))
1312
1.67k
      TryEnterHdr = true;
1313
10.5k
    return TryEnterHdr;
1314
10.5k
  };
1315
1316
  // If this is a #import directive, check that we have not already imported
1317
  // this header.
1318
2.32M
  if (isImport) {
1319
    // If this has already been imported, don't import it again.
1320
305k
    FileInfo.isImport = true;
1321
1322
    // Has this already been #import'ed or #include'd?
1323
305k
    if (FileInfo.NumIncludes && 
!TryEnterImported()200k
)
1324
199k
      return false;
1325
2.02M
  } else {
1326
    // Otherwise, if this is a #include of a file that was previously #import'd
1327
    // or if this is the second #include of a #pragma once file, ignore it.
1328
2.02M
    if (FileInfo.isImport && 
!TryEnterImported()103k
)
1329
103k
      return false;
1330
2.02M
  }
1331
1332
  // Next, check to see if the file is wrapped with #ifndef guards.  If so, and
1333
  // if the macro that guards it is defined, we know the #include has no effect.
1334
2.02M
  if (const IdentifierInfo *ControllingMacro
1335
1.13M
      = FileInfo.getControllingMacro(ExternalLookup)) {
1336
    // If the header corresponds to a module, check whether the macro is already
1337
    // defined in that module rather than checking in the current set of visible
1338
    // modules.
1339
1.13M
    if (M ? 
PP.isMacroDefinedInLocalModule(ControllingMacro, M)39.5k
1340
1.10M
          : PP.isMacroDefined(ControllingMacro)) {
1341
1.09M
      ++NumMultiIncludeFileOptzn;
1342
1.09M
      return false;
1343
1.09M
    }
1344
924k
  }
1345
1346
  // Increment the number of times this file has been included.
1347
924k
  ++FileInfo.NumIncludes;
1348
1349
924k
  return true;
1350
924k
}
1351
1352
1
size_t HeaderSearch::getTotalMemory() const {
1353
1
  return SearchDirs.capacity()
1354
1
    + llvm::capacity_in_bytes(FileInfo)
1355
1
    + llvm::capacity_in_bytes(HeaderMaps)
1356
1
    + LookupFileCache.getAllocator().getTotalMemory()
1357
1
    + FrameworkMap.getAllocator().getTotalMemory();
1358
1
}
1359
1360
0
StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
1361
0
  return FrameworkNames.insert(Framework).first->first();
1362
0
}
1363
1364
bool HeaderSearch::hasModuleMap(StringRef FileName,
1365
                                const DirectoryEntry *Root,
1366
1.45M
                                bool IsSystem) {
1367
1.45M
  if (!HSOpts->ImplicitModuleMaps)
1368
1.38M
    return false;
1369
1370
73.6k
  SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
1371
1372
73.6k
  StringRef DirName = FileName;
1373
76.1k
  do {
1374
    // Get the parent directory name.
1375
76.1k
    DirName = llvm::sys::path::parent_path(DirName);
1376
76.1k
    if (DirName.empty())
1377
20
      return false;
1378
1379
    // Determine whether this directory exists.
1380
76.0k
    auto Dir = FileMgr.getDirectory(DirName);
1381
76.0k
    if (!Dir)
1382
0
      return false;
1383
1384
    // Try to load the module map file in this directory.
1385
76.0k
    switch (loadModuleMapFile(*Dir, IsSystem,
1386
76.0k
                              llvm::sys::path::extension((*Dir)->getName()) ==
1387
76.0k
                                  ".framework")) {
1388
71.3k
    case LMM_NewlyLoaded:
1389
71.3k
    case LMM_AlreadyLoaded:
1390
      // Success. All of the directories we stepped through inherit this module
1391
      // map file.
1392
73.1k
      for (unsigned I = 0, N = FixUpDirectories.size(); I != N; 
++I1.77k
)
1393
1.77k
        DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1394
71.3k
      return true;
1395
1396
4.73k
    case LMM_NoDirectory:
1397
4.73k
    case LMM_InvalidModuleMap:
1398
4.73k
      break;
1399
4.73k
    }
1400
1401
    // If we hit the top of our search, we're done.
1402
4.73k
    if (*Dir == Root)
1403
2.27k
      return false;
1404
1405
    // Keep track of all of the directories we checked, so we can mark them as
1406
    // having module maps if we eventually do find a module map.
1407
2.46k
    FixUpDirectories.push_back(*Dir);
1408
2.46k
  } while (true);
1409
73.6k
}
1410
1411
ModuleMap::KnownHeader
1412
HeaderSearch::findModuleForHeader(const FileEntry *File,
1413
2.34M
                                  bool AllowTextual) const {
1414
2.34M
  if (ExternalSource) {
1415
    // Make sure the external source has handled header info about this file,
1416
    // which includes whether the file is part of a module.
1417
76.8k
    (void)getExistingFileInfo(File);
1418
76.8k
  }
1419
2.34M
  return ModMap.findModuleForHeader(File, AllowTextual);
1420
2.34M
}
1421
1422
ArrayRef<ModuleMap::KnownHeader>
1423
937
HeaderSearch::findAllModulesForHeader(const FileEntry *File) const {
1424
937
  if (ExternalSource) {
1425
    // Make sure the external source has handled header info about this file,
1426
    // which includes whether the file is part of a module.
1427
850
    (void)getExistingFileInfo(File);
1428
850
  }
1429
937
  return ModMap.findAllModulesForHeader(File);
1430
937
}
1431
1432
static bool suggestModule(HeaderSearch &HS, const FileEntry *File,
1433
                          Module *RequestingModule,
1434
2.34M
                          ModuleMap::KnownHeader *SuggestedModule) {
1435
2.34M
  ModuleMap::KnownHeader Module =
1436
2.34M
      HS.findModuleForHeader(File, /*AllowTextual*/true);
1437
1438
  // If this module specifies [no_undeclared_includes], we cannot find any
1439
  // file that's in a non-dependency module.
1440
2.34M
  if (RequestingModule && 
Module81.7k
&&
RequestingModule->NoUndeclaredIncludes78.7k
) {
1441
44.0k
    HS.getModuleMap().resolveUses(RequestingModule, /*Complain*/ false);
1442
44.0k
    if (!RequestingModule->directlyUses(Module.getModule())) {
1443
      // Builtin headers are a special case. Multiple modules can use the same
1444
      // builtin as a modular header (see also comment in
1445
      // ShouldEnterIncludeFile()), so the builtin header may have been
1446
      // "claimed" by an unrelated module. This shouldn't prevent us from
1447
      // including the builtin header textually in this module.
1448
164
      if (HS.getModuleMap().isBuiltinHeader(File)) {
1449
1
        if (SuggestedModule)
1450
1
          *SuggestedModule = ModuleMap::KnownHeader();
1451
1
        return true;
1452
1
      }
1453
163
      return false;
1454
163
    }
1455
44.0k
  }
1456
1457
2.33M
  if (SuggestedModule)
1458
2.33M
    *SuggestedModule = (Module.getRole() & ModuleMap::TextualHeader)
1459
1.24k
                           ? ModuleMap::KnownHeader()
1460
2.33M
                           : Module;
1461
1462
2.33M
  return true;
1463
2.33M
}
1464
1465
bool HeaderSearch::findUsableModuleForHeader(
1466
    const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
1467
1.47M
    ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1468
1.47M
  if (File && needModuleLookup(RequestingModule, SuggestedModule)) {
1469
    // If there is a module that corresponds to this header, suggest it.
1470
1.45M
    hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
1471
1.45M
    return suggestModule(*this, File, RequestingModule, SuggestedModule);
1472
1.45M
  }
1473
20.1k
  return true;
1474
20.1k
}
1475
1476
bool HeaderSearch::findUsableModuleForFrameworkHeader(
1477
    const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
1478
882k
    ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1479
  // If we're supposed to suggest a module, look for one now.
1480
882k
  if (needModuleLookup(RequestingModule, SuggestedModule)) {
1481
    // Find the top-level framework based on this framework.
1482
882k
    SmallVector<std::string, 4> SubmodulePath;
1483
882k
    const DirectoryEntry *TopFrameworkDir
1484
882k
      = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1485
1486
    // Determine the name of the top-level framework.
1487
882k
    StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1488
1489
    // Load this framework module. If that succeeds, find the suggested module
1490
    // for this header, if any.
1491
882k
    loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1492
1493
    // FIXME: This can find a module not part of ModuleName, which is
1494
    // important so that we're consistent about whether this header
1495
    // corresponds to a module. Possibly we should lock down framework modules
1496
    // so that this is not possible.
1497
882k
    return suggestModule(*this, File, RequestingModule, SuggestedModule);
1498
882k
  }
1499
0
  return true;
1500
0
}
1501
1502
static const FileEntry *getPrivateModuleMap(const FileEntry *File,
1503
20.4k
                                            FileManager &FileMgr) {
1504
20.4k
  StringRef Filename = llvm::sys::path::filename(File->getName());
1505
20.4k
  SmallString<128>  PrivateFilename(File->getDir()->getName());
1506
20.4k
  if (Filename == "module.map")
1507
1.94k
    llvm::sys::path::append(PrivateFilename, "module_private.map");
1508
18.4k
  else if (Filename == "module.modulemap")
1509
18.0k
    llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1510
414
  else
1511
414
    return nullptr;
1512
20.0k
  if (auto File = FileMgr.getFile(PrivateFilename))
1513
142
    return *File;
1514
19.8k
  return nullptr;
1515
19.8k
}
1516
1517
bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
1518
                                     FileID ID, unsigned *Offset,
1519
4.26k
                                     StringRef OriginalModuleMapFile) {
1520
  // Find the directory for the module. For frameworks, that may require going
1521
  // up from the 'Modules' directory.
1522
4.26k
  const DirectoryEntry *Dir = nullptr;
1523
4.26k
  if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) {
1524
51
    if (auto DirOrErr = FileMgr.getDirectory("."))
1525
51
      Dir = *DirOrErr;
1526
4.21k
  } else {
1527
4.21k
    if (!OriginalModuleMapFile.empty()) {
1528
      // We're building a preprocessed module map. Find or invent the directory
1529
      // that it originally occupied.
1530
16
      auto DirOrErr = FileMgr.getDirectory(
1531
16
          llvm::sys::path::parent_path(OriginalModuleMapFile));
1532
16
      if (DirOrErr) {
1533
16
        Dir = *DirOrErr;
1534
0
      } else {
1535
0
        auto *FakeFile = FileMgr.getVirtualFile(OriginalModuleMapFile, 0, 0);
1536
0
        Dir = FakeFile->getDir();
1537
0
      }
1538
4.20k
    } else {
1539
4.20k
      Dir = File->getDir();
1540
4.20k
    }
1541
1542
4.21k
    StringRef DirName(Dir->getName());
1543
4.21k
    if (llvm::sys::path::filename(DirName) == "Modules") {
1544
199
      DirName = llvm::sys::path::parent_path(DirName);
1545
199
      if (DirName.endswith(".framework"))
1546
178
        if (auto DirOrErr = FileMgr.getDirectory(DirName))
1547
178
          Dir = *DirOrErr;
1548
      // FIXME: This assert can fail if there's a race between the above check
1549
      // and the removal of the directory.
1550
199
      assert(Dir && "parent must exist");
1551
199
    }
1552
4.21k
  }
1553
1554
4.26k
  switch (loadModuleMapFileImpl(File, IsSystem, Dir, ID, Offset)) {
1555
4.26k
  case LMM_AlreadyLoaded:
1556
4.26k
  case LMM_NewlyLoaded:
1557
4.26k
    return false;
1558
2
  case LMM_NoDirectory:
1559
2
  case LMM_InvalidModuleMap:
1560
2
    return true;
1561
0
  }
1562
0
  llvm_unreachable("Unknown load module map result");
1563
0
}
1564
1565
HeaderSearch::LoadModuleMapResult
1566
HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1567
                                    const DirectoryEntry *Dir, FileID ID,
1568
42.9k
                                    unsigned *Offset) {
1569
42.9k
  assert(File && "expected FileEntry");
1570
1571
  // Check whether we've already loaded this module map, and mark it as being
1572
  // loaded in case we recursively try to load it from itself.
1573
42.9k
  auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1574
42.9k
  if (!AddResult.second)
1575
22.4k
    return AddResult.first->second ? LMM_AlreadyLoaded : 
LMM_InvalidModuleMap0
;
1576
1577
20.5k
  if (ModMap.parseModuleMapFile(File, IsSystem, Dir, ID, Offset)) {
1578
96
    LoadedModuleMaps[File] = false;
1579
96
    return LMM_InvalidModuleMap;
1580
96
  }
1581
1582
  // Try to load a corresponding private module map.
1583
20.4k
  if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1584
142
    if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
1585
0
      LoadedModuleMaps[File] = false;
1586
0
      return LMM_InvalidModuleMap;
1587
0
    }
1588
20.4k
  }
1589
1590
  // This directory has a module map.
1591
20.4k
  return LMM_NewlyLoaded;
1592
20.4k
}
1593
1594
const FileEntry *
1595
961k
HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
1596
961k
  if (!HSOpts->ImplicitModuleMaps)
1597
866k
    return nullptr;
1598
  // For frameworks, the preferred spelling is Modules/module.modulemap, but
1599
  // module.map at the framework root is also accepted.
1600
94.9k
  SmallString<128> ModuleMapFileName(Dir->getName());
1601
94.9k
  if (IsFramework)
1602
6.20k
    llvm::sys::path::append(ModuleMapFileName, "Modules");
1603
94.9k
  llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1604
94.9k
  if (auto F = FileMgr.getFile(ModuleMapFileName))
1605
38.4k
    return *F;
1606
1607
  // Continue to allow module.map
1608
56.5k
  ModuleMapFileName = Dir->getName();
1609
56.5k
  llvm::sys::path::append(ModuleMapFileName, "module.map");
1610
56.5k
  if (auto F = FileMgr.getFile(ModuleMapFileName))
1611
2.91k
    return *F;
1612
1613
  // For frameworks, allow to have a private module map with a preferred
1614
  // spelling when a public module map is absent.
1615
53.6k
  if (IsFramework) {
1616
1.04k
    ModuleMapFileName = Dir->getName();
1617
1.04k
    llvm::sys::path::append(ModuleMapFileName, "Modules",
1618
1.04k
                            "module.private.modulemap");
1619
1.04k
    if (auto F = FileMgr.getFile(ModuleMapFileName))
1620
2
      return *F;
1621
53.6k
  }
1622
53.6k
  return nullptr;
1623
53.6k
}
1624
1625
Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1626
                                          const DirectoryEntry *Dir,
1627
887k
                                          bool IsSystem) {
1628
887k
  if (Module *Module = ModMap.findModule(Name))
1629
15.9k
    return Module;
1630
1631
  // Try to load a module map file.
1632
871k
  switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
1633
866k
  case LMM_InvalidModuleMap:
1634
    // Try to infer a module map from the framework directory.
1635
866k
    if (HSOpts->ImplicitModuleMaps)
1636
611
      ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
1637
866k
    break;
1638
1639
3
  case LMM_AlreadyLoaded:
1640
3
  case LMM_NoDirectory:
1641
3
    return nullptr;
1642
1643
4.87k
  case LMM_NewlyLoaded:
1644
4.87k
    break;
1645
871k
  }
1646
1647
871k
  return ModMap.findModule(Name);
1648
871k
}
1649
1650
HeaderSearch::LoadModuleMapResult
1651
HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1652
589k
                                bool IsFramework) {
1653
589k
  if (auto Dir = FileMgr.getDirectory(DirName))
1654
42.5k
    return loadModuleMapFile(*Dir, IsSystem, IsFramework);
1655
1656
546k
  return LMM_NoDirectory;
1657
546k
}
1658
1659
HeaderSearch::LoadModuleMapResult
1660
HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1661
1.01M
                                bool IsFramework) {
1662
1.01M
  auto KnownDir = DirectoryHasModuleMap.find(Dir);
1663
1.01M
  if (KnownDir != DirectoryHasModuleMap.end())
1664
61.5k
    return KnownDir->second ? LMM_AlreadyLoaded : 
LMM_InvalidModuleMap0
;
1665
1666
958k
  if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
1667
38.7k
    LoadModuleMapResult Result =
1668
38.7k
        loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
1669
    // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1670
    // E.g. Foo.framework/Modules/module.modulemap
1671
    //      ^Dir                  ^ModuleMapFile
1672
38.7k
    if (Result == LMM_NewlyLoaded)
1673
18.2k
      DirectoryHasModuleMap[Dir] = true;
1674
20.4k
    else if (Result == LMM_InvalidModuleMap)
1675
94
      DirectoryHasModuleMap[Dir] = false;
1676
38.7k
    return Result;
1677
38.7k
  }
1678
919k
  return LMM_InvalidModuleMap;
1679
919k
}
1680
1681
2
void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
1682
2
  Modules.clear();
1683
1684
2
  if (HSOpts->ImplicitModuleMaps) {
1685
    // Load module maps for each of the header search directories.
1686
10
    for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; 
++Idx8
) {
1687
8
      bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1688
8
      if (SearchDirs[Idx].isFramework()) {
1689
4
        std::error_code EC;
1690
4
        SmallString<128> DirNative;
1691
4
        llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1692
4
                                DirNative);
1693
1694
        // Search each of the ".framework" directories to load them as modules.
1695
4
        llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1696
4
        for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
1697
4
                                           DirEnd;
1698
362
             Dir != DirEnd && 
!EC358
;
Dir.increment(EC)358
) {
1699
358
          if (llvm::sys::path::extension(Dir->path()) != ".framework")
1700
4
            continue;
1701
1702
354
          auto FrameworkDir =
1703
354
              FileMgr.getDirectory(Dir->path());
1704
354
          if (!FrameworkDir)
1705
0
            continue;
1706
1707
          // Load this framework module.
1708
354
          loadFrameworkModule(llvm::sys::path::stem(Dir->path()), *FrameworkDir,
1709
354
                              IsSystem);
1710
354
        }
1711
4
        continue;
1712
4
      }
1713
1714
      // FIXME: Deal with header maps.
1715
4
      if (SearchDirs[Idx].isHeaderMap())
1716
0
        continue;
1717
1718
      // Try to load a module map file for the search directory.
1719
4
      loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1720
4
                        /*IsFramework*/ false);
1721
1722
      // Try to load module map files for immediate subdirectories of this
1723
      // search directory.
1724
4
      loadSubdirectoryModuleMaps(SearchDirs[Idx]);
1725
4
    }
1726
2
  }
1727
1728
  // Populate the list of modules.
1729
2
  for (ModuleMap::module_iterator M = ModMap.module_begin(),
1730
2
                               MEnd = ModMap.module_end();
1731
396
       M != MEnd; 
++M394
) {
1732
394
    Modules.push_back(M->getValue());
1733
394
  }
1734
2
}
1735
1736
73
void HeaderSearch::loadTopLevelSystemModules() {
1737
73
  if (!HSOpts->ImplicitModuleMaps)
1738
0
    return;
1739
1740
  // Load module maps for each of the header search directories.
1741
341
  
for (unsigned Idx = 0, N = SearchDirs.size(); 73
Idx != N;
++Idx268
) {
1742
    // We only care about normal header directories.
1743
268
    if (!SearchDirs[Idx].isNormalDir()) {
1744
39
      continue;
1745
39
    }
1746
1747
    // Try to load a module map file for the search directory.
1748
229
    loadModuleMapFile(SearchDirs[Idx].getDir(),
1749
229
                      SearchDirs[Idx].isSystemHeaderDirectory(),
1750
229
                      SearchDirs[Idx].isFramework());
1751
229
  }
1752
73
}
1753
1754
6.97k
void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1755
6.97k
  assert(HSOpts->ImplicitModuleMaps &&
1756
6.97k
         "Should not be loading subdirectory module maps");
1757
1758
6.97k
  if (SearchDir.haveSearchedAllModuleMaps())
1759
0
    return;
1760
1761
6.97k
  std::error_code EC;
1762
6.97k
  SmallString<128> Dir = SearchDir.getDir()->getName();
1763
6.97k
  FileMgr.makeAbsolutePath(Dir);
1764
6.97k
  SmallString<128> DirNative;
1765
6.97k
  llvm::sys::path::native(Dir, DirNative);
1766
6.97k
  llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1767
6.97k
  for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
1768
569k
       Dir != DirEnd && 
!EC562k
;
Dir.increment(EC)562k
) {
1769
562k
    bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1770
562k
    if (IsFramework == SearchDir.isFramework())
1771
562k
      loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1772
562k
                        SearchDir.isFramework());
1773
562k
  }
1774
1775
6.97k
  SearchDir.setSearchedAllModuleMaps(true);
1776
6.97k
}
1777
1778
std::string HeaderSearch::suggestPathToFileForDiagnostics(
1779
273
    const FileEntry *File, llvm::StringRef MainFile, bool *IsSystem) {
1780
  // FIXME: We assume that the path name currently cached in the FileEntry is
1781
  // the most appropriate one for this analysis (and that it's spelled the
1782
  // same way as the corresponding header search path).
1783
273
  return suggestPathToFileForDiagnostics(File->getName(), /*WorkingDir=*/"",
1784
273
                                         MainFile, IsSystem);
1785
273
}
1786
1787
std::string HeaderSearch::suggestPathToFileForDiagnostics(
1788
    llvm::StringRef File, llvm::StringRef WorkingDir, llvm::StringRef MainFile,
1789
282
    bool *IsSystem) {
1790
282
  using namespace llvm::sys;
1791
1792
282
  unsigned BestPrefixLength = 0;
1793
  // Checks whether Dir and File shares a common prefix, if they do and that's
1794
  // the longest prefix we've seen so for it returns true and updates the
1795
  // BestPrefixLength accordingly.
1796
559
  auto CheckDir = [&](llvm::StringRef Dir) -> bool {
1797
559
    llvm::SmallString<32> DirPath(Dir.begin(), Dir.end());
1798
559
    if (!WorkingDir.empty() && 
!path::is_absolute(Dir)3
)
1799
2
      fs::make_absolute(WorkingDir, DirPath);
1800
559
    path::remove_dots(DirPath, /*remove_dot_dot=*/true);
1801
559
    Dir = DirPath;
1802
559
    for (auto NI = path::begin(File), NE = path::end(File),
1803
559
              DI = path::begin(Dir), DE = path::end(Dir);
1804
5.52k
         /*termination condition in loop*/; 
++NI, ++DI4.96k
) {
1805
      // '.' components in File are ignored.
1806
5.52k
      while (NI != NE && *NI == ".")
1807
2
        ++NI;
1808
5.52k
      if (NI == NE)
1809
0
        break;
1810
1811
      // '.' components in Dir are ignored.
1812
5.52k
      while (DI != DE && 
*DI == "."5.24k
)
1813
0
        ++DI;
1814
5.52k
      if (DI == DE) {
1815
        // Dir is a prefix of File, up to '.' components and choice of path
1816
        // separators.
1817
283
        unsigned PrefixLength = NI - path::begin(File);
1818
283
        if (PrefixLength > BestPrefixLength) {
1819
282
          BestPrefixLength = PrefixLength;
1820
282
          return true;
1821
282
        }
1822
1
        break;
1823
1
      }
1824
1825
      // Consider all path separators equal.
1826
5.24k
      if (NI->size() == 1 && 
DI->size() == 1578
&&
1827
578
          path::is_separator(NI->front()) && 
path::is_separator(DI->front())558
)
1828
558
        continue;
1829
1830
4.68k
      if (*NI != *DI)
1831
276
        break;
1832
4.68k
    }
1833
277
    return false;
1834
559
  };
1835
1836
839
  for (unsigned I = 0; I != SearchDirs.size(); 
++I557
) {
1837
    // FIXME: Support this search within frameworks and header maps.
1838
557
    if (!SearchDirs[I].isNormalDir())
1839
0
      continue;
1840
1841
557
    StringRef Dir = SearchDirs[I].getDir()->getName();
1842
557
    if (CheckDir(Dir) && 
IsSystem281
)
1843
273
      *IsSystem = BestPrefixLength ? I >= SystemDirIdx : 
false0
;
1844
557
  }
1845
1846
  // Try to shorten include path using TUs directory, if we couldn't find any
1847
  // suitable prefix in include search paths.
1848
282
  if (!BestPrefixLength && 
CheckDir(path::parent_path(MainFile))2
&&
IsSystem1
)
1849
0
    *IsSystem = false;
1850
1851
1852
282
  return path::convert_to_slash(File.drop_front(BestPrefixLength));
1853
282
}