Coverage Report

Created: 2023-09-30 09:22

/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/ADT/STLExtras.h"
33
#include "llvm/Support/Allocator.h"
34
#include "llvm/Support/Capacity.h"
35
#include "llvm/Support/Errc.h"
36
#include "llvm/Support/ErrorHandling.h"
37
#include "llvm/Support/FileSystem.h"
38
#include "llvm/Support/Path.h"
39
#include "llvm/Support/VirtualFileSystem.h"
40
#include <algorithm>
41
#include <cassert>
42
#include <cstddef>
43
#include <cstdio>
44
#include <cstring>
45
#include <string>
46
#include <system_error>
47
#include <utility>
48
49
using namespace clang;
50
51
#define DEBUG_TYPE "file-search"
52
53
ALWAYS_ENABLED_STATISTIC(NumIncluded, "Number of attempted #includes.");
54
ALWAYS_ENABLED_STATISTIC(
55
    NumMultiIncludeFileOptzn,
56
    "Number of #includes skipped due to the multi-include optimization.");
57
ALWAYS_ENABLED_STATISTIC(NumFrameworkLookups, "Number of framework lookups.");
58
ALWAYS_ENABLED_STATISTIC(NumSubFrameworkLookups,
59
                         "Number of subframework lookups.");
60
61
const IdentifierInfo *
62
3.67M
HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
63
3.67M
  if (ControllingMacro) {
64
2.80M
    if (ControllingMacro->isOutOfDate()) {
65
41
      assert(External && "We must have an external source if we have a "
66
41
                         "controlling macro that is out of date.");
67
41
      External->updateOutOfDateIdentifier(
68
41
          *const_cast<IdentifierInfo *>(ControllingMacro));
69
41
    }
70
2.80M
    return ControllingMacro;
71
2.80M
  }
72
73
870k
  if (!ControllingMacroID || 
!External726
)
74
869k
    return nullptr;
75
76
726
  ControllingMacro = External->GetIdentifier(ControllingMacroID);
77
726
  return ControllingMacro;
78
870k
}
79
80
14.9k
ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() = default;
81
82
HeaderSearch::HeaderSearch(std::shared_ptr<HeaderSearchOptions> HSOpts,
83
                           SourceManager &SourceMgr, DiagnosticsEngine &Diags,
84
                           const LangOptions &LangOpts,
85
                           const TargetInfo *Target)
86
93.8k
    : HSOpts(std::move(HSOpts)), Diags(Diags),
87
93.8k
      FileMgr(SourceMgr.getFileManager()), FrameworkMap(64),
88
93.8k
      ModMap(SourceMgr, Diags, LangOpts, Target, *this) {}
89
90
5
void HeaderSearch::PrintStats() {
91
5
  llvm::errs() << "\n*** HeaderSearch Stats:\n"
92
5
               << FileInfo.size() << " files tracked.\n";
93
5
  unsigned NumOnceOnlyFiles = 0;
94
156
  for (unsigned i = 0, e = FileInfo.size(); i != e; 
++i151
)
95
151
    NumOnceOnlyFiles += (FileInfo[i].isPragmaOnce || FileInfo[i].isImport);
96
5
  llvm::errs() << "  " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
97
98
5
  llvm::errs() << "  " << NumIncluded << " #include/#include_next/#import.\n"
99
5
               << "    " << NumMultiIncludeFileOptzn
100
5
               << " #includes skipped due to the multi-include optimization.\n";
101
102
5
  llvm::errs() << NumFrameworkLookups << " framework lookups.\n"
103
5
               << NumSubFrameworkLookups << " subframework lookups.\n";
104
5
}
105
106
void HeaderSearch::SetSearchPaths(
107
    std::vector<DirectoryLookup> dirs, unsigned int angledDirIdx,
108
    unsigned int systemDirIdx, bool noCurDirSearch,
109
93.4k
    llvm::DenseMap<unsigned int, unsigned int> searchDirToHSEntry) {
110
93.4k
  assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
111
93.4k
         "Directory indices are unordered");
112
93.4k
  SearchDirs = std::move(dirs);
113
93.4k
  SearchDirsUsage.assign(SearchDirs.size(), false);
114
93.4k
  AngledDirIdx = angledDirIdx;
115
93.4k
  SystemDirIdx = systemDirIdx;
116
93.4k
  NoCurDirSearch = noCurDirSearch;
117
93.4k
  SearchDirToHSEntry = std::move(searchDirToHSEntry);
118
  //LookupFileCache.clear();
119
93.4k
  indexInitialHeaderMaps();
120
93.4k
}
121
122
22
void HeaderSearch::AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
123
22
  unsigned idx = isAngled ? 
SystemDirIdx4
:
AngledDirIdx18
;
124
22
  SearchDirs.insert(SearchDirs.begin() + idx, dir);
125
22
  SearchDirsUsage.insert(SearchDirsUsage.begin() + idx, false);
126
22
  if (!isAngled)
127
18
    AngledDirIdx++;
128
22
  SystemDirIdx++;
129
22
}
130
131
7.24k
std::vector<bool> HeaderSearch::computeUserEntryUsage() const {
132
7.24k
  std::vector<bool> UserEntryUsage(HSOpts->UserEntries.size());
133
22.4k
  for (unsigned I = 0, E = SearchDirsUsage.size(); I < E; 
++I15.2k
) {
134
    // Check whether this DirectoryLookup has been successfully used.
135
15.2k
    if (SearchDirsUsage[I]) {
136
1.82k
      auto UserEntryIdxIt = SearchDirToHSEntry.find(I);
137
      // Check whether this DirectoryLookup maps to a HeaderSearch::UserEntry.
138
1.82k
      if (UserEntryIdxIt != SearchDirToHSEntry.end())
139
1.76k
        UserEntryUsage[UserEntryIdxIt->second] = true;
140
1.82k
    }
141
15.2k
  }
142
7.24k
  return UserEntryUsage;
143
7.24k
}
144
145
/// CreateHeaderMap - This method returns a HeaderMap for the specified
146
/// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
147
43
const HeaderMap *HeaderSearch::CreateHeaderMap(FileEntryRef FE) {
148
  // We expect the number of headermaps to be small, and almost always empty.
149
  // If it ever grows, use of a linear search should be re-evaluated.
150
43
  if (!HeaderMaps.empty()) {
151
22
    for (unsigned i = 0, e = HeaderMaps.size(); i != e; 
++i11
)
152
      // Pointer equality comparison of FileEntries works because they are
153
      // already uniqued by inode.
154
11
      if (HeaderMaps[i].first == FE)
155
0
        return HeaderMaps[i].second.get();
156
11
  }
157
158
43
  if (std::unique_ptr<HeaderMap> HM = HeaderMap::Create(FE, FileMgr)) {
159
39
    HeaderMaps.emplace_back(FE, std::move(HM));
160
39
    return HeaderMaps.back().second.get();
161
39
  }
162
163
4
  return nullptr;
164
43
}
165
166
/// Get filenames for all registered header maps.
167
void HeaderSearch::getHeaderMapFileNames(
168
36
    SmallVectorImpl<std::string> &Names) const {
169
36
  for (auto &HM : HeaderMaps)
170
1
    Names.push_back(std::string(HM.first.getName()));
171
36
}
172
173
11.4k
std::string HeaderSearch::getCachedModuleFileName(Module *Module) {
174
11.4k
  OptionalFileEntryRef ModuleMap =
175
11.4k
      getModuleMap().getModuleMapFileForUniquing(Module);
176
  // The ModuleMap maybe a nullptr, when we load a cached C++ module without
177
  // *.modulemap file. In this case, just return an empty string.
178
11.4k
  if (!ModuleMap)
179
0
    return {};
180
11.4k
  return getCachedModuleFileName(Module->Name, ModuleMap->getNameAsRequested());
181
11.4k
}
182
183
std::string HeaderSearch::getPrebuiltModuleFileName(StringRef ModuleName,
184
2.38k
                                                    bool FileMapOnly) {
185
  // First check the module name to pcm file map.
186
2.38k
  auto i(HSOpts->PrebuiltModuleFiles.find(ModuleName));
187
2.38k
  if (i != HSOpts->PrebuiltModuleFiles.end())
188
304
    return i->second;
189
190
2.08k
  if (FileMapOnly || 
HSOpts->PrebuiltModulePaths.empty()335
)
191
1.81k
    return {};
192
193
  // Then go through each prebuilt module directory and try to find the pcm
194
  // file.
195
265
  for (const std::string &Dir : HSOpts->PrebuiltModulePaths) {
196
265
    SmallString<256> Result(Dir);
197
265
    llvm::sys::fs::make_absolute(Result);
198
265
    if (ModuleName.contains(':'))
199
      // The separator of C++20 modules partitions (':') is not good for file
200
      // systems, here clang and gcc choose '-' by default since it is not a
201
      // valid character of C++ indentifiers. So we could avoid conflicts.
202
39
      llvm::sys::path::append(Result, ModuleName.split(':').first + "-" +
203
39
                                          ModuleName.split(':').second +
204
39
                                          ".pcm");
205
226
    else
206
226
      llvm::sys::path::append(Result, ModuleName + ".pcm");
207
265
    if (getFileMgr().getFile(Result.str()))
208
256
      return std::string(Result);
209
265
  }
210
211
9
  return {};
212
265
}
213
214
3
std::string HeaderSearch::getPrebuiltImplicitModuleFileName(Module *Module) {
215
3
  OptionalFileEntryRef ModuleMap =
216
3
      getModuleMap().getModuleMapFileForUniquing(Module);
217
3
  StringRef ModuleName = Module->Name;
218
3
  StringRef ModuleMapPath = ModuleMap->getName();
219
3
  StringRef ModuleCacheHash = HSOpts->DisableModuleHash ? 
""0
: getModuleHash();
220
3
  for (const std::string &Dir : HSOpts->PrebuiltModulePaths) {
221
3
    SmallString<256> CachePath(Dir);
222
3
    llvm::sys::fs::make_absolute(CachePath);
223
3
    llvm::sys::path::append(CachePath, ModuleCacheHash);
224
3
    std::string FileName =
225
3
        getCachedModuleFileNameImpl(ModuleName, ModuleMapPath, CachePath);
226
3
    if (!FileName.empty() && getFileMgr().getFile(FileName))
227
2
      return FileName;
228
3
  }
229
1
  return {};
230
3
}
231
232
std::string HeaderSearch::getCachedModuleFileName(StringRef ModuleName,
233
11.5k
                                                  StringRef ModuleMapPath) {
234
11.5k
  return getCachedModuleFileNameImpl(ModuleName, ModuleMapPath,
235
11.5k
                                     getModuleCachePath());
236
11.5k
}
237
238
std::string HeaderSearch::getCachedModuleFileNameImpl(StringRef ModuleName,
239
                                                      StringRef ModuleMapPath,
240
11.5k
                                                      StringRef CachePath) {
241
  // If we don't have a module cache path or aren't supposed to use one, we
242
  // can't do anything.
243
11.5k
  if (CachePath.empty())
244
13
    return {};
245
246
11.5k
  SmallString<256> Result(CachePath);
247
11.5k
  llvm::sys::fs::make_absolute(Result);
248
249
11.5k
  if (HSOpts->DisableModuleHash) {
250
198
    llvm::sys::path::append(Result, ModuleName + ".pcm");
251
11.3k
  } else {
252
    // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
253
    // ideally be globally unique to this particular module. Name collisions
254
    // in the hash are safe (because any translation unit can only import one
255
    // module with each name), but result in a loss of caching.
256
    //
257
    // To avoid false-negatives, we form as canonical a path as we can, and map
258
    // to lower-case in case we're on a case-insensitive file system.
259
11.3k
    SmallString<128> CanonicalPath(ModuleMapPath);
260
11.3k
    if (getModuleMap().canonicalizeModuleMapPath(CanonicalPath))
261
0
      return {};
262
263
11.3k
    llvm::hash_code Hash = llvm::hash_combine(CanonicalPath.str().lower());
264
265
11.3k
    SmallString<128> HashStr;
266
11.3k
    llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
267
11.3k
    llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
268
11.3k
  }
269
11.5k
  return Result.str().str();
270
11.5k
}
271
272
Module *HeaderSearch::lookupModule(StringRef ModuleName,
273
                                   SourceLocation ImportLoc, bool AllowSearch,
274
9.58M
                                   bool AllowExtraModuleMapSearch) {
275
  // Look in the module map to determine if there is a module by this name.
276
9.58M
  Module *Module = ModMap.findModule(ModuleName);
277
9.58M
  if (Module || 
!AllowSearch4.70k
||
!HSOpts->ImplicitModuleMaps4.70k
)
278
9.57M
    return Module;
279
280
3.56k
  StringRef SearchName = ModuleName;
281
3.56k
  Module = lookupModule(ModuleName, SearchName, ImportLoc,
282
3.56k
                        AllowExtraModuleMapSearch);
283
284
  // The facility for "private modules" -- adjacent, optional module maps named
285
  // module.private.modulemap that are supposed to define private submodules --
286
  // may have different flavors of names: FooPrivate, Foo_Private and Foo.Private.
287
  //
288
  // Foo.Private is now deprecated in favor of Foo_Private. Users of FooPrivate
289
  // should also rename to Foo_Private. Representing private as submodules
290
  // could force building unwanted dependencies into the parent module and cause
291
  // dependency cycles.
292
3.56k
  if (!Module && 
SearchName.consume_back("_Private")48
)
293
6
    Module = lookupModule(ModuleName, SearchName, ImportLoc,
294
6
                          AllowExtraModuleMapSearch);
295
3.56k
  if (!Module && 
SearchName.consume_back("Private")44
)
296
5
    Module = lookupModule(ModuleName, SearchName, ImportLoc,
297
5
                          AllowExtraModuleMapSearch);
298
3.56k
  return Module;
299
9.58M
}
300
301
Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
302
                                   SourceLocation ImportLoc,
303
3.57k
                                   bool AllowExtraModuleMapSearch) {
304
3.57k
  Module *Module = nullptr;
305
306
  // Look through the various header search paths to load any available module
307
  // maps, searching for a module map that describes this module.
308
8.95k
  for (DirectoryLookup &Dir : search_dir_range()) {
309
8.95k
    if (Dir.isFramework()) {
310
      // Search for or infer a module map for a framework. Here we use
311
      // SearchName rather than ModuleName, to permit finding private modules
312
      // named FooPrivate in buggy frameworks named Foo.
313
790
      SmallString<128> FrameworkDirName;
314
790
      FrameworkDirName += Dir.getFrameworkDirRef()->getName();
315
790
      llvm::sys::path::append(FrameworkDirName, SearchName + ".framework");
316
790
      if (auto FrameworkDir =
317
790
              FileMgr.getOptionalDirectoryRef(FrameworkDirName)) {
318
726
        bool IsSystem = Dir.getDirCharacteristic() != SrcMgr::C_User;
319
726
        Module = loadFrameworkModule(ModuleName, *FrameworkDir, IsSystem);
320
726
        if (Module)
321
719
          break;
322
726
      }
323
790
    }
324
325
    // FIXME: Figure out how header maps and module maps will work together.
326
327
    // Only deal with normal search directories.
328
8.23k
    if (!Dir.isNormalDir())
329
75
      continue;
330
331
8.15k
    bool IsSystem = Dir.isSystemHeaderDirectory();
332
    // Only returns std::nullopt if not a normal directory, which we just
333
    // checked
334
8.15k
    DirectoryEntryRef NormalDir = *Dir.getDirRef();
335
    // Search for a module map file in this directory.
336
8.15k
    if (loadModuleMapFile(NormalDir, IsSystem,
337
8.15k
                          /*IsFramework*/false) == LMM_NewlyLoaded) {
338
      // We just loaded a module map file; check whether the module is
339
      // available now.
340
3.17k
      Module = ModMap.findModule(ModuleName);
341
3.17k
      if (Module)
342
2.72k
        break;
343
3.17k
    }
344
345
    // Search for a module map in a subdirectory with the same name as the
346
    // module.
347
5.43k
    SmallString<128> NestedModuleMapDirName;
348
5.43k
    NestedModuleMapDirName = Dir.getDirRef()->getName();
349
5.43k
    llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
350
5.43k
    if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
351
5.43k
                          /*IsFramework*/false) == LMM_NewlyLoaded){
352
      // If we just loaded a module map file, look for the module again.
353
19
      Module = ModMap.findModule(ModuleName);
354
19
      if (Module)
355
19
        break;
356
19
    }
357
358
    // If we've already performed the exhaustive search for module maps in this
359
    // search directory, don't do it again.
360
5.41k
    if (Dir.haveSearchedAllModuleMaps())
361
2.61k
      continue;
362
363
    // Load all module maps in the immediate subdirectories of this search
364
    // directory if ModuleName was from @import.
365
2.79k
    if (AllowExtraModuleMapSearch)
366
2.72k
      loadSubdirectoryModuleMaps(Dir);
367
368
    // Look again for the module.
369
2.79k
    Module = ModMap.findModule(ModuleName);
370
2.79k
    if (Module)
371
64
      break;
372
2.79k
  }
373
374
3.57k
  return Module;
375
3.57k
}
376
377
93.4k
void HeaderSearch::indexInitialHeaderMaps() {
378
93.4k
  llvm::StringMap<unsigned, llvm::BumpPtrAllocator> Index(SearchDirs.size());
379
380
  // Iterate over all filename keys and associate them with the index i.
381
93.5k
  for (unsigned i = 0; i != SearchDirs.size(); 
++i37
) {
382
84.4k
    auto &Dir = SearchDirs[i];
383
384
    // We're concerned with only the initial contiguous run of header
385
    // maps within SearchDirs, which can be 99% of SearchDirs when
386
    // SearchDirs.size() is ~10000.
387
84.4k
    if (!Dir.isHeaderMap()) {
388
84.4k
      SearchDirHeaderMapIndex = std::move(Index);
389
84.4k
      FirstNonHeaderMapSearchDirIdx = i;
390
84.4k
      break;
391
84.4k
    }
392
393
    // Give earlier keys precedence over identical later keys.
394
46
    
auto Callback = [&](StringRef Filename) 37
{
395
46
      Index.try_emplace(Filename.lower(), i);
396
46
    };
397
37
    Dir.getHeaderMap()->forEachKey(Callback);
398
37
  }
399
93.4k
}
400
401
//===----------------------------------------------------------------------===//
402
// File lookup within a DirectoryLookup scope
403
//===----------------------------------------------------------------------===//
404
405
/// getName - Return the directory or filename corresponding to this lookup
406
/// object.
407
203
StringRef DirectoryLookup::getName() const {
408
203
  if (isNormalDir())
409
142
    return getDirRef()->getName();
410
61
  if (isFramework())
411
55
    return getFrameworkDirRef()->getName();
412
6
  assert(isHeaderMap() && "Unknown DirectoryLookup");
413
6
  return getHeaderMap()->getFileName();
414
6
}
415
416
OptionalFileEntryRef HeaderSearch::getFileAndSuggestModule(
417
    StringRef FileName, SourceLocation IncludeLoc, const DirectoryEntry *Dir,
418
    bool IsSystemHeaderDir, Module *RequestingModule,
419
    ModuleMap::KnownHeader *SuggestedModule, bool OpenFile /*=true*/,
420
6.13M
    bool CacheFailures /*=true*/) {
421
  // If we have a module map that might map this header, load it and
422
  // check whether we'll have a suggestion for a module.
423
6.13M
  auto File = getFileMgr().getFileRef(FileName, OpenFile, CacheFailures);
424
6.13M
  if (!File) {
425
    // For rare, surprising errors (e.g. "out of file handles"), diag the EC
426
    // message.
427
2.65M
    std::error_code EC = llvm::errorToErrorCode(File.takeError());
428
2.65M
    if (EC != llvm::errc::no_such_file_or_directory &&
429
2.65M
        
EC != llvm::errc::invalid_argument13
&&
430
2.65M
        
EC != llvm::errc::is_a_directory13
&&
EC != llvm::errc::not_a_directory1
) {
431
0
      Diags.Report(IncludeLoc, diag::err_cannot_open_file)
432
0
          << FileName << EC.message();
433
0
    }
434
2.65M
    return std::nullopt;
435
2.65M
  }
436
437
  // If there is a module that corresponds to this header, suggest it.
438
3.47M
  if (!findUsableModuleForHeader(
439
3.47M
          *File, Dir ? 
Dir3.47M
:
File->getFileEntry().getDir()4.36k
, RequestingModule,
440
3.47M
          SuggestedModule, IsSystemHeaderDir))
441
418
    return std::nullopt;
442
443
3.47M
  return *File;
444
3.47M
}
445
446
/// LookupFile - Lookup the specified file in this search path, returning it
447
/// if it exists or returning null if not.
448
OptionalFileEntryRef DirectoryLookup::LookupFile(
449
    StringRef &Filename, HeaderSearch &HS, SourceLocation IncludeLoc,
450
    SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
451
    Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
452
    bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound,
453
    bool &IsInHeaderMap, SmallVectorImpl<char> &MappedName,
454
6.39M
    bool OpenFile) const {
455
6.39M
  InUserSpecifiedSystemFramework = false;
456
6.39M
  IsInHeaderMap = false;
457
6.39M
  MappedName.clear();
458
459
6.39M
  SmallString<1024> TmpDir;
460
6.39M
  if (isNormalDir()) {
461
    // Concatenate the requested file onto the directory.
462
6.04M
    TmpDir = getDirRef()->getName();
463
6.04M
    llvm::sys::path::append(TmpDir, Filename);
464
6.04M
    if (SearchPath) {
465
5.98M
      StringRef SearchPathRef(getDirRef()->getName());
466
5.98M
      SearchPath->clear();
467
5.98M
      SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
468
5.98M
    }
469
6.04M
    if (RelativePath) {
470
5.98M
      RelativePath->clear();
471
5.98M
      RelativePath->append(Filename.begin(), Filename.end());
472
5.98M
    }
473
474
6.04M
    return HS.getFileAndSuggestModule(
475
6.04M
        TmpDir, IncludeLoc, getDir(), isSystemHeaderDirectory(),
476
6.04M
        RequestingModule, SuggestedModule, OpenFile);
477
6.04M
  }
478
479
349k
  if (isFramework())
480
349k
    return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
481
349k
                             RequestingModule, SuggestedModule,
482
349k
                             InUserSpecifiedSystemFramework, IsFrameworkFound);
483
484
32
  assert(isHeaderMap() && "Unknown directory lookup");
485
32
  const HeaderMap *HM = getHeaderMap();
486
32
  SmallString<1024> Path;
487
32
  StringRef Dest = HM->lookupFilename(Filename, Path);
488
32
  if (Dest.empty())
489
8
    return std::nullopt;
490
491
24
  IsInHeaderMap = true;
492
493
24
  auto FixupSearchPathAndFindUsableModule =
494
24
      [&](FileEntryRef File) -> OptionalFileEntryRef {
495
6
    if (SearchPath) {
496
5
      StringRef SearchPathRef(getName());
497
5
      SearchPath->clear();
498
5
      SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
499
5
    }
500
6
    if (RelativePath) {
501
5
      RelativePath->clear();
502
5
      RelativePath->append(Filename.begin(), Filename.end());
503
5
    }
504
6
    if (!HS.findUsableModuleForHeader(File, File.getFileEntry().getDir(),
505
6
                                      RequestingModule, SuggestedModule,
506
6
                                      isSystemHeaderDirectory())) {
507
0
      return std::nullopt;
508
0
    }
509
6
    return File;
510
6
  };
511
512
  // Check if the headermap maps the filename to a framework include
513
  // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
514
  // framework include.
515
24
  if (llvm::sys::path::is_relative(Dest)) {
516
18
    MappedName.append(Dest.begin(), Dest.end());
517
18
    Filename = StringRef(MappedName.begin(), MappedName.size());
518
18
    Dest = HM->lookupFilename(Filename, Path);
519
18
  }
520
521
24
  if (auto Res = HS.getFileMgr().getOptionalFileRef(Dest, OpenFile)) {
522
6
    return FixupSearchPathAndFindUsableModule(*Res);
523
6
  }
524
525
  // Header maps need to be marked as used whenever the filename matches.
526
  // The case where the target file **exists** is handled by callee of this
527
  // function as part of the regular logic that applies to include search paths.
528
  // The case where the target file **does not exist** is handled here:
529
18
  HS.noteLookupUsage(HS.searchDirIdx(*this), IncludeLoc);
530
18
  return std::nullopt;
531
24
}
532
533
/// Given a framework directory, find the top-most framework directory.
534
///
535
/// \param FileMgr The file manager to use for directory lookups.
536
/// \param DirName The name of the framework directory.
537
/// \param SubmodulePath Will be populated with the submodule path from the
538
/// returned top-level module to the originally named framework.
539
static OptionalDirectoryEntryRef
540
getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
541
345k
                   SmallVectorImpl<std::string> &SubmodulePath) {
542
345k
  assert(llvm::sys::path::extension(DirName) == ".framework" &&
543
345k
         "Not a framework directory");
544
545
  // Note: as an egregious but useful hack we use the real path here, because
546
  // frameworks moving between top-level frameworks to embedded frameworks tend
547
  // to be symlinked, and we base the logical structure of modules on the
548
  // physical layout. In particular, we need to deal with crazy includes like
549
  //
550
  //   #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
551
  //
552
  // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
553
  // which one should access with, e.g.,
554
  //
555
  //   #include <Bar/Wibble.h>
556
  //
557
  // Similar issues occur when a top-level framework has moved into an
558
  // embedded framework.
559
345k
  auto TopFrameworkDir = FileMgr.getOptionalDirectoryRef(DirName);
560
561
345k
  if (TopFrameworkDir)
562
345k
    DirName = FileMgr.getCanonicalName(*TopFrameworkDir);
563
4.97M
  do {
564
    // Get the parent directory name.
565
4.97M
    DirName = llvm::sys::path::parent_path(DirName);
566
4.97M
    if (DirName.empty())
567
345k
      break;
568
569
    // Determine whether this directory exists.
570
4.63M
    auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
571
4.63M
    if (!Dir)
572
0
      break;
573
574
    // If this is a framework directory, then we're a subframework of this
575
    // framework.
576
4.63M
    if (llvm::sys::path::extension(DirName) == ".framework") {
577
34.7k
      SubmodulePath.push_back(std::string(llvm::sys::path::stem(DirName)));
578
34.7k
      TopFrameworkDir = *Dir;
579
34.7k
    }
580
4.63M
  } while (true);
581
582
0
  return TopFrameworkDir;
583
345k
}
584
585
static bool needModuleLookup(Module *RequestingModule,
586
4.13M
                             bool HasSuggestedModule) {
587
4.13M
  return HasSuggestedModule ||
588
4.13M
         
(17.2k
RequestingModule17.2k
&&
RequestingModule->NoUndeclaredIncludes461
);
589
4.13M
}
590
591
/// DoFrameworkLookup - Do a lookup of the specified file in the current
592
/// DirectoryLookup, which is a framework directory.
593
OptionalFileEntryRef DirectoryLookup::DoFrameworkLookup(
594
    StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
595
    SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
596
    ModuleMap::KnownHeader *SuggestedModule,
597
349k
    bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound) const {
598
349k
  FileManager &FileMgr = HS.getFileMgr();
599
600
  // Framework names must have a '/' in the filename.
601
349k
  size_t SlashPos = Filename.find('/');
602
349k
  if (SlashPos == StringRef::npos)
603
4.70k
    return std::nullopt;
604
605
  // Find out if this is the home for the specified framework, by checking
606
  // HeaderSearch.  Possible answers are yes/no and unknown.
607
345k
  FrameworkCacheEntry &CacheEntry =
608
345k
    HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
609
610
  // If it is known and in some other directory, fail.
611
345k
  if (CacheEntry.Directory && 
CacheEntry.Directory != getFrameworkDirRef()337k
)
612
29.1k
    return std::nullopt;
613
614
  // Otherwise, construct the path to this framework dir.
615
616
  // FrameworkName = "/System/Library/Frameworks/"
617
316k
  SmallString<1024> FrameworkName;
618
316k
  FrameworkName += getFrameworkDirRef()->getName();
619
316k
  if (FrameworkName.empty() || FrameworkName.back() != '/')
620
316k
    FrameworkName.push_back('/');
621
622
  // FrameworkName = "/System/Library/Frameworks/Cocoa"
623
316k
  StringRef ModuleName(Filename.begin(), SlashPos);
624
316k
  FrameworkName += ModuleName;
625
626
  // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
627
316k
  FrameworkName += ".framework/";
628
629
  // If the cache entry was unresolved, populate it now.
630
316k
  if (!CacheEntry.Directory) {
631
8.14k
    ++NumFrameworkLookups;
632
633
    // If the framework dir doesn't exist, we fail.
634
8.14k
    auto Dir = FileMgr.getDirectory(FrameworkName);
635
8.14k
    if (!Dir)
636
4.87k
      return std::nullopt;
637
638
    // Otherwise, if it does, remember that this is the right direntry for this
639
    // framework.
640
3.26k
    CacheEntry.Directory = getFrameworkDirRef();
641
642
    // If this is a user search directory, check if the framework has been
643
    // user-specified as a system framework.
644
3.26k
    if (getDirCharacteristic() == SrcMgr::C_User) {
645
354
      SmallString<1024> SystemFrameworkMarker(FrameworkName);
646
354
      SystemFrameworkMarker += ".system_framework";
647
354
      if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
648
5
        CacheEntry.IsUserSpecifiedSystemFramework = true;
649
5
      }
650
354
    }
651
3.26k
  }
652
653
  // Set out flags.
654
311k
  InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
655
311k
  IsFrameworkFound = CacheEntry.Directory.has_value();
656
657
311k
  if (RelativePath) {
658
310k
    RelativePath->clear();
659
310k
    RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
660
310k
  }
661
662
  // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
663
311k
  unsigned OrigSize = FrameworkName.size();
664
665
311k
  FrameworkName += "Headers/";
666
667
311k
  if (SearchPath) {
668
310k
    SearchPath->clear();
669
    // Without trailing '/'.
670
310k
    SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
671
310k
  }
672
673
311k
  FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
674
675
311k
  auto File =
676
311k
      FileMgr.getOptionalFileRef(FrameworkName, /*OpenFile=*/!SuggestedModule);
677
311k
  if (!File) {
678
    // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
679
280
    const char *Private = "Private";
680
280
    FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
681
280
                         Private+strlen(Private));
682
280
    if (SearchPath)
683
48
      SearchPath->insert(SearchPath->begin()+OrigSize, Private,
684
48
                         Private+strlen(Private));
685
686
280
    File = FileMgr.getOptionalFileRef(FrameworkName,
687
280
                                      /*OpenFile=*/!SuggestedModule);
688
280
  }
689
690
  // If we found the header and are allowed to suggest a module, do so now.
691
311k
  if (File && 
needModuleLookup(RequestingModule, SuggestedModule)310k
) {
692
    // Find the framework in which this header occurs.
693
310k
    StringRef FrameworkPath = File->getDir().getName();
694
310k
    bool FoundFramework = false;
695
622k
    do {
696
      // Determine whether this directory exists.
697
622k
      auto Dir = FileMgr.getDirectory(FrameworkPath);
698
622k
      if (!Dir)
699
0
        break;
700
701
      // If this is a framework directory, then we're a subframework of this
702
      // framework.
703
622k
      if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
704
310k
        FoundFramework = true;
705
310k
        break;
706
310k
      }
707
708
      // Get the parent directory name.
709
311k
      FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
710
311k
      if (FrameworkPath.empty())
711
0
        break;
712
311k
    } while (true);
713
714
0
    bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
715
310k
    if (FoundFramework) {
716
310k
      if (!HS.findUsableModuleForFrameworkHeader(*File, FrameworkPath,
717
310k
                                                 RequestingModule,
718
310k
                                                 SuggestedModule, IsSystem))
719
0
        return std::nullopt;
720
310k
    } else {
721
0
      if (!HS.findUsableModuleForHeader(*File, getDir(), RequestingModule,
722
0
                                        SuggestedModule, IsSystem))
723
0
        return std::nullopt;
724
0
    }
725
310k
  }
726
311k
  if (File)
727
310k
    return *File;
728
236
  return std::nullopt;
729
311k
}
730
731
void HeaderSearch::cacheLookupSuccess(LookupFileCacheInfo &CacheLookup,
732
                                      ConstSearchDirIterator HitIt,
733
3.71M
                                      SourceLocation Loc) {
734
3.71M
  CacheLookup.HitIt = HitIt;
735
3.71M
  noteLookupUsage(HitIt.Idx, Loc);
736
3.71M
}
737
738
3.71M
void HeaderSearch::noteLookupUsage(unsigned HitIdx, SourceLocation Loc) {
739
3.71M
  SearchDirsUsage[HitIdx] = true;
740
741
3.71M
  auto UserEntryIdxIt = SearchDirToHSEntry.find(HitIdx);
742
3.71M
  if (UserEntryIdxIt != SearchDirToHSEntry.end())
743
3.40M
    Diags.Report(Loc, diag::remark_pp_search_path_usage)
744
3.40M
        << HSOpts->UserEntries[UserEntryIdxIt->second].Path;
745
3.71M
}
746
747
93.7k
void HeaderSearch::setTarget(const TargetInfo &Target) {
748
93.7k
  ModMap.setTarget(Target);
749
93.7k
}
750
751
//===----------------------------------------------------------------------===//
752
// Header File Location.
753
//===----------------------------------------------------------------------===//
754
755
/// Return true with a diagnostic if the file that MSVC would have found
756
/// fails to match the one that Clang would have found with MSVC header search
757
/// disabled.
758
static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
759
                                  OptionalFileEntryRef MSFE,
760
                                  const FileEntry *FE,
761
3.76M
                                  SourceLocation IncludeLoc) {
762
3.76M
  if (MSFE && 
FE != *MSFE3
) {
763
2
    Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
764
2
    return true;
765
2
  }
766
3.76M
  return false;
767
3.76M
}
768
769
18
static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
770
18
  assert(!Str.empty());
771
18
  char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
772
18
  std::copy(Str.begin(), Str.end(), CopyStr);
773
18
  CopyStr[Str.size()] = '\0';
774
18
  return CopyStr;
775
18
}
776
777
static bool isFrameworkStylePath(StringRef Path, bool &IsPrivateHeader,
778
                                 SmallVectorImpl<char> &FrameworkName,
779
4.14M
                                 SmallVectorImpl<char> &IncludeSpelling) {
780
4.14M
  using namespace llvm::sys;
781
4.14M
  path::const_iterator I = path::begin(Path);
782
4.14M
  path::const_iterator E = path::end(Path);
783
4.14M
  IsPrivateHeader = false;
784
785
  // Detect different types of framework style paths:
786
  //
787
  //   ...Foo.framework/{Headers,PrivateHeaders}
788
  //   ...Foo.framework/Versions/{A,Current}/{Headers,PrivateHeaders}
789
  //   ...Foo.framework/Frameworks/Nested.framework/{Headers,PrivateHeaders}
790
  //   ...<other variations with 'Versions' like in the above path>
791
  //
792
  // and some other variations among these lines.
793
4.14M
  int FoundComp = 0;
794
53.4M
  while (I != E) {
795
49.3M
    if (*I == "Headers") {
796
700k
      ++FoundComp;
797
48.6M
    } else if (*I == "PrivateHeaders") {
798
32
      ++FoundComp;
799
32
      IsPrivateHeader = true;
800
48.6M
    } else if (I->endswith(".framework")) {
801
744k
      StringRef Name = I->drop_back(10); // Drop .framework
802
      // Need to reset the strings and counter to support nested frameworks.
803
744k
      FrameworkName.clear();
804
744k
      FrameworkName.append(Name.begin(), Name.end());
805
744k
      IncludeSpelling.clear();
806
744k
      IncludeSpelling.append(Name.begin(), Name.end());
807
744k
      FoundComp = 1;
808
47.8M
    } else if (FoundComp >= 2) {
809
314k
      IncludeSpelling.push_back('/');
810
314k
      IncludeSpelling.append(I->begin(), I->end());
811
314k
    }
812
49.3M
    ++I;
813
49.3M
  }
814
815
4.14M
  return !FrameworkName.empty() && 
FoundComp >= 2699k
;
816
4.14M
}
817
818
static void
819
diagnoseFrameworkInclude(DiagnosticsEngine &Diags, SourceLocation IncludeLoc,
820
                         StringRef Includer, StringRef IncludeFilename,
821
                         FileEntryRef IncludeFE, bool isAngled = false,
822
3.75M
                         bool FoundByHeaderMap = false) {
823
3.75M
  bool IsIncluderPrivateHeader = false;
824
3.75M
  SmallString<128> FromFramework, ToFramework;
825
3.75M
  SmallString<128> FromIncludeSpelling, ToIncludeSpelling;
826
3.75M
  if (!isFrameworkStylePath(Includer, IsIncluderPrivateHeader, FromFramework,
827
3.75M
                            FromIncludeSpelling))
828
3.36M
    return;
829
389k
  bool IsIncludeePrivateHeader = false;
830
389k
  bool IsIncludeeInFramework =
831
389k
      isFrameworkStylePath(IncludeFE.getName(), IsIncludeePrivateHeader,
832
389k
                           ToFramework, ToIncludeSpelling);
833
834
389k
  if (!isAngled && 
!FoundByHeaderMap69
) {
835
63
    SmallString<128> NewInclude("<");
836
63
    if (IsIncludeeInFramework) {
837
42
      NewInclude += ToIncludeSpelling;
838
42
      NewInclude += ">";
839
42
    } else {
840
21
      NewInclude += IncludeFilename;
841
21
      NewInclude += ">";
842
21
    }
843
63
    Diags.Report(IncludeLoc, diag::warn_quoted_include_in_framework_header)
844
63
        << IncludeFilename
845
63
        << FixItHint::CreateReplacement(IncludeLoc, NewInclude);
846
63
  }
847
848
  // Headers in Foo.framework/Headers should not include headers
849
  // from Foo.framework/PrivateHeaders, since this violates public/private
850
  // API boundaries and can cause modular dependency cycles.
851
389k
  if (!IsIncluderPrivateHeader && 
IsIncludeeInFramework389k
&&
852
389k
      
IsIncludeePrivateHeader310k
&&
FromFramework == ToFramework8
)
853
8
    Diags.Report(IncludeLoc, diag::warn_framework_include_private_from_public)
854
8
        << IncludeFilename;
855
389k
}
856
857
/// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
858
/// return null on failure.  isAngled indicates whether the file reference is
859
/// for system \#include's or not (i.e. using <> instead of ""). Includers, if
860
/// non-empty, indicates where the \#including file(s) are, in case a relative
861
/// search is needed. Microsoft mode will pass all \#including files.
862
OptionalFileEntryRef HeaderSearch::LookupFile(
863
    StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
864
    ConstSearchDirIterator FromDir, ConstSearchDirIterator *CurDirArg,
865
    ArrayRef<std::pair<OptionalFileEntryRef, DirectoryEntryRef>> Includers,
866
    SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
867
    Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
868
    bool *IsMapped, bool *IsFrameworkFound, bool SkipCache,
869
3.83M
    bool BuildSystemModule, bool OpenFile, bool CacheFailures) {
870
3.83M
  ConstSearchDirIterator CurDirLocal = nullptr;
871
3.83M
  ConstSearchDirIterator &CurDir = CurDirArg ? 
*CurDirArg3.83M
:
CurDirLocal26
;
872
873
3.83M
  if (IsMapped)
874
3.80M
    *IsMapped = false;
875
876
3.83M
  if (IsFrameworkFound)
877
3.80M
    *IsFrameworkFound = false;
878
879
3.83M
  if (SuggestedModule)
880
3.80M
    *SuggestedModule = ModuleMap::KnownHeader();
881
882
  // If 'Filename' is absolute, check to see if it exists and no searching.
883
3.83M
  if (llvm::sys::path::is_absolute(Filename)) {
884
4.37k
    CurDir = nullptr;
885
886
    // If this was an #include_next "/absolute/file", fail.
887
4.37k
    if (FromDir)
888
0
      return std::nullopt;
889
890
4.37k
    if (SearchPath)
891
4.35k
      SearchPath->clear();
892
4.37k
    if (RelativePath) {
893
4.35k
      RelativePath->clear();
894
4.35k
      RelativePath->append(Filename.begin(), Filename.end());
895
4.35k
    }
896
    // Otherwise, just return the file.
897
4.37k
    return getFileAndSuggestModule(Filename, IncludeLoc, nullptr,
898
4.37k
                                   /*IsSystemHeaderDir*/ false,
899
4.37k
                                   RequestingModule, SuggestedModule, OpenFile,
900
4.37k
                                   CacheFailures);
901
4.37k
  }
902
903
  // This is the header that MSVC's header search would have found.
904
3.82M
  ModuleMap::KnownHeader MSSuggestedModule;
905
3.82M
  OptionalFileEntryRef MSFE;
906
907
  // Unless disabled, check to see if the file is in the #includer's
908
  // directory.  This cannot be based on CurDir, because each includer could be
909
  // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
910
  // include of "baz.h" should resolve to "whatever/foo/baz.h".
911
  // This search is not done for <> headers.
912
3.82M
  if (!Includers.empty() && 
!isAngled3.80M
&&
!NoCurDirSearch84.0k
) {
913
84.0k
    SmallString<1024> TmpDir;
914
84.0k
    bool First = true;
915
84.0k
    for (const auto &IncluderAndDir : Includers) {
916
84.0k
      OptionalFileEntryRef Includer = IncluderAndDir.first;
917
918
      // Concatenate the requested file onto the directory.
919
84.0k
      TmpDir = IncluderAndDir.second.getName();
920
84.0k
      llvm::sys::path::append(TmpDir, Filename);
921
922
      // FIXME: We don't cache the result of getFileInfo across the call to
923
      // getFileAndSuggestModule, because it's a reference to an element of
924
      // a container that could be reallocated across this call.
925
      //
926
      // If we have no includer, that means we're processing a #include
927
      // from a module build. We should treat this as a system header if we're
928
      // building a [system] module.
929
84.0k
      bool IncluderIsSystemHeader =
930
84.0k
          Includer ? 
getFileInfo(*Includer).DirInfo != SrcMgr::C_User75.6k
:
931
84.0k
          
BuildSystemModule8.39k
;
932
84.0k
      if (OptionalFileEntryRef FE = getFileAndSuggestModule(
933
84.0k
              TmpDir, IncludeLoc, IncluderAndDir.second, IncluderIsSystemHeader,
934
84.0k
              RequestingModule, SuggestedModule)) {
935
67.2k
        if (!Includer) {
936
8.39k
          assert(First && "only first includer can have no file");
937
8.39k
          return FE;
938
8.39k
        }
939
940
        // Leave CurDir unset.
941
        // This file is a system header or C++ unfriendly if the old file is.
942
        //
943
        // Note that we only use one of FromHFI/ToHFI at once, due to potential
944
        // reallocation of the underlying vector potentially making the first
945
        // reference binding dangling.
946
58.8k
        HeaderFileInfo &FromHFI = getFileInfo(*Includer);
947
58.8k
        unsigned DirInfo = FromHFI.DirInfo;
948
58.8k
        bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
949
58.8k
        StringRef Framework = FromHFI.Framework;
950
951
58.8k
        HeaderFileInfo &ToHFI = getFileInfo(*FE);
952
58.8k
        ToHFI.DirInfo = DirInfo;
953
58.8k
        ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
954
58.8k
        ToHFI.Framework = Framework;
955
956
58.8k
        if (SearchPath) {
957
58.4k
          StringRef SearchPathRef(IncluderAndDir.second.getName());
958
58.4k
          SearchPath->clear();
959
58.4k
          SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
960
58.4k
        }
961
58.8k
        if (RelativePath) {
962
58.4k
          RelativePath->clear();
963
58.4k
          RelativePath->append(Filename.begin(), Filename.end());
964
58.4k
        }
965
58.8k
        if (First) {
966
58.8k
          diagnoseFrameworkInclude(Diags, IncludeLoc,
967
58.8k
                                   IncluderAndDir.second.getName(), Filename,
968
58.8k
                                   *FE);
969
58.8k
          return FE;
970
58.8k
        }
971
972
        // Otherwise, we found the path via MSVC header search rules.  If
973
        // -Wmsvc-include is enabled, we have to keep searching to see if we
974
        // would've found this header in -I or -isystem directories.
975
7
        if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
976
4
          return FE;
977
4
        } else {
978
3
          MSFE = FE;
979
3
          if (SuggestedModule) {
980
3
            MSSuggestedModule = *SuggestedModule;
981
3
            *SuggestedModule = ModuleMap::KnownHeader();
982
3
          }
983
3
          break;
984
3
        }
985
7
      }
986
16.7k
      First = false;
987
16.7k
    }
988
84.0k
  }
989
990
3.76M
  CurDir = nullptr;
991
992
  // If this is a system #include, ignore the user #include locs.
993
3.76M
  ConstSearchDirIterator It =
994
3.76M
      isAngled ? 
angled_dir_begin()3.74M
:
search_dir_begin()16.8k
;
995
996
  // If this is a #include_next request, start searching after the directory the
997
  // file was found in.
998
3.76M
  if (FromDir)
999
21.7k
    It = FromDir;
1000
1001
  // Cache all of the lookups performed by this method.  Many headers are
1002
  // multiply included, and the "pragma once" optimization prevents them from
1003
  // being relex/pp'd, but they would still have to search through a
1004
  // (potentially huge) series of SearchDirs to find it.
1005
3.76M
  LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
1006
1007
3.76M
  ConstSearchDirIterator NextIt = std::next(It);
1008
1009
3.76M
  if (!SkipCache) {
1010
3.76M
    if (CacheLookup.StartIt == NextIt &&
1011
3.76M
        
CacheLookup.RequestingModule == RequestingModule3.00M
) {
1012
      // HIT: Skip querying potentially lots of directories for this lookup.
1013
2.98M
      if (CacheLookup.HitIt)
1014
2.98M
        It = CacheLookup.HitIt;
1015
2.98M
      if (CacheLookup.MappedName) {
1016
6
        Filename = CacheLookup.MappedName;
1017
6
        if (IsMapped)
1018
6
          *IsMapped = true;
1019
6
      }
1020
2.98M
    } else {
1021
      // MISS: This is the first query, or the previous query didn't match
1022
      // our search start.  We will fill in our found location below, so prime
1023
      // the start point value.
1024
772k
      CacheLookup.reset(RequestingModule, /*NewStartIt=*/NextIt);
1025
1026
772k
      if (It == search_dir_begin() && 
FirstNonHeaderMapSearchDirIdx > 0759k
) {
1027
        // Handle cold misses of user includes in the presence of many header
1028
        // maps.  We avoid searching perhaps thousands of header maps by
1029
        // jumping directly to the correct one or jumping beyond all of them.
1030
28
        auto Iter = SearchDirHeaderMapIndex.find(Filename.lower());
1031
28
        if (Iter == SearchDirHeaderMapIndex.end())
1032
          // Not in index => Skip to first SearchDir after initial header maps
1033
7
          It = search_dir_nth(FirstNonHeaderMapSearchDirIdx);
1034
21
        else
1035
          // In index => Start with a specific header map
1036
21
          It = search_dir_nth(Iter->second);
1037
28
      }
1038
772k
    }
1039
3.76M
  } else {
1040
1
    CacheLookup.reset(RequestingModule, /*NewStartIt=*/NextIt);
1041
1
  }
1042
1043
3.76M
  SmallString<64> MappedName;
1044
1045
  // Check each directory in sequence to see if it contains this file.
1046
6.43M
  for (; It != search_dir_end(); 
++It2.67M
) {
1047
6.39M
    bool InUserSpecifiedSystemFramework = false;
1048
6.39M
    bool IsInHeaderMap = false;
1049
6.39M
    bool IsFrameworkFoundInDir = false;
1050
6.39M
    OptionalFileEntryRef File = It->LookupFile(
1051
6.39M
        Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
1052
6.39M
        SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
1053
6.39M
        IsInHeaderMap, MappedName, OpenFile);
1054
6.39M
    if (!MappedName.empty()) {
1055
18
      assert(IsInHeaderMap && "MappedName should come from a header map");
1056
18
      CacheLookup.MappedName =
1057
18
          copyString(MappedName, LookupFileCache.getAllocator());
1058
18
    }
1059
6.39M
    if (IsMapped)
1060
      // A filename is mapped when a header map remapped it to a relative path
1061
      // used in subsequent header search or to an absolute path pointing to an
1062
      // existing file.
1063
6.32M
      *IsMapped |= (!MappedName.empty() || 
(6.32M
IsInHeaderMap6.32M
&&
File6
));
1064
6.39M
    if (IsFrameworkFound)
1065
      // Because we keep a filename remapped for subsequent search directory
1066
      // lookups, ignore IsFrameworkFoundInDir after the first remapping and not
1067
      // just for remapping in a current search directory.
1068
6.32M
      *IsFrameworkFound |= (IsFrameworkFoundInDir && 
!CacheLookup.MappedName310k
);
1069
6.39M
    if (!File)
1070
2.67M
      continue;
1071
1072
3.71M
    CurDir = It;
1073
1074
3.71M
    IncludeNames[*File] = Filename;
1075
1076
    // This file is a system header or C++ unfriendly if the dir is.
1077
3.71M
    HeaderFileInfo &HFI = getFileInfo(*File);
1078
3.71M
    HFI.DirInfo = CurDir->getDirCharacteristic();
1079
1080
    // If the directory characteristic is User but this framework was
1081
    // user-specified to be treated as a system framework, promote the
1082
    // characteristic.
1083
3.71M
    if (HFI.DirInfo == SrcMgr::C_User && 
InUserSpecifiedSystemFramework6.24k
)
1084
1
      HFI.DirInfo = SrcMgr::C_System;
1085
1086
    // If the filename matches a known system header prefix, override
1087
    // whether the file is a system header.
1088
3.71M
    for (unsigned j = SystemHeaderPrefixes.size(); j; 
--j12
) {
1089
20
      if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
1090
8
        HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? 
SrcMgr::C_System4
1091
8
                                                       : 
SrcMgr::C_User4
;
1092
8
        break;
1093
8
      }
1094
20
    }
1095
1096
    // Set the `Framework` info if this file is in a header map with framework
1097
    // style include spelling or found in a framework dir. The header map case
1098
    // is possible when building frameworks which use header maps.
1099
3.71M
    if (CurDir->isHeaderMap() && 
isAngled6
) {
1100
3
      size_t SlashPos = Filename.find('/');
1101
3
      if (SlashPos != StringRef::npos)
1102
3
        HFI.Framework =
1103
3
            getUniqueFrameworkName(StringRef(Filename.begin(), SlashPos));
1104
3
      if (CurDir->isIndexHeaderMap())
1105
0
        HFI.IndexHeaderMapHeader = 1;
1106
3.71M
    } else if (CurDir->isFramework()) {
1107
310k
      size_t SlashPos = Filename.find('/');
1108
310k
      if (SlashPos != StringRef::npos)
1109
310k
        HFI.Framework =
1110
310k
            getUniqueFrameworkName(StringRef(Filename.begin(), SlashPos));
1111
310k
    }
1112
1113
3.71M
    if (checkMSVCHeaderSearch(Diags, MSFE, &File->getFileEntry(), IncludeLoc)) {
1114
1
      if (SuggestedModule)
1115
1
        *SuggestedModule = MSSuggestedModule;
1116
1
      return MSFE;
1117
1
    }
1118
1119
3.71M
    bool FoundByHeaderMap = !IsMapped ? 
false16.8k
:
*IsMapped3.70M
;
1120
3.71M
    if (!Includers.empty())
1121
3.69M
      diagnoseFrameworkInclude(Diags, IncludeLoc,
1122
3.69M
                               Includers.front().second.getName(), Filename,
1123
3.69M
                               *File, isAngled, FoundByHeaderMap);
1124
1125
    // Remember this location for the next lookup we do.
1126
3.71M
    cacheLookupSuccess(CacheLookup, It, IncludeLoc);
1127
3.71M
    return File;
1128
3.71M
  }
1129
1130
  // If we are including a file with a quoted include "foo.h" from inside
1131
  // a header in a framework that is currently being built, and we couldn't
1132
  // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
1133
  // "Foo" is the name of the framework in which the including header was found.
1134
44.3k
  if (!Includers.empty() && 
Includers.front().first41.4k
&&
!isAngled41.4k
&&
1135
44.3k
      
!Filename.contains('/')194
) {
1136
110
    HeaderFileInfo &IncludingHFI = getFileInfo(*Includers.front().first);
1137
110
    if (IncludingHFI.IndexHeaderMapHeader) {
1138
0
      SmallString<128> ScratchFilename;
1139
0
      ScratchFilename += IncludingHFI.Framework;
1140
0
      ScratchFilename += '/';
1141
0
      ScratchFilename += Filename;
1142
1143
0
      OptionalFileEntryRef File = LookupFile(
1144
0
          ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, &CurDir,
1145
0
          Includers.front(), SearchPath, RelativePath, RequestingModule,
1146
0
          SuggestedModule, IsMapped, /*IsFrameworkFound=*/nullptr);
1147
1148
0
      if (checkMSVCHeaderSearch(Diags, MSFE,
1149
0
                                File ? &File->getFileEntry() : nullptr,
1150
0
                                IncludeLoc)) {
1151
0
        if (SuggestedModule)
1152
0
          *SuggestedModule = MSSuggestedModule;
1153
0
        return MSFE;
1154
0
      }
1155
1156
0
      cacheLookupSuccess(LookupFileCache[Filename],
1157
0
                         LookupFileCache[ScratchFilename].HitIt, IncludeLoc);
1158
      // FIXME: SuggestedModule.
1159
0
      return File;
1160
0
    }
1161
110
  }
1162
1163
44.3k
  if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
1164
1
    if (SuggestedModule)
1165
1
      *SuggestedModule = MSSuggestedModule;
1166
1
    return MSFE;
1167
1
  }
1168
1169
  // Otherwise, didn't find it. Remember we didn't find this.
1170
44.3k
  CacheLookup.HitIt = search_dir_end();
1171
44.3k
  return std::nullopt;
1172
44.3k
}
1173
1174
/// LookupSubframeworkHeader - Look up a subframework for the specified
1175
/// \#include file.  For example, if \#include'ing <HIToolbox/HIToolbox.h> from
1176
/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
1177
/// is a subframework within Carbon.framework.  If so, return the FileEntry
1178
/// for the designated file, otherwise return null.
1179
OptionalFileEntryRef HeaderSearch::LookupSubframeworkHeader(
1180
    StringRef Filename, FileEntryRef ContextFileEnt,
1181
    SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
1182
85.1k
    Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
1183
  // Framework names must have a '/' in the filename.  Find it.
1184
  // FIXME: Should we permit '\' on Windows?
1185
85.1k
  size_t SlashPos = Filename.find('/');
1186
85.1k
  if (SlashPos == StringRef::npos)
1187
41.0k
    return std::nullopt;
1188
1189
  // Look up the base framework name of the ContextFileEnt.
1190
44.0k
  StringRef ContextName = ContextFileEnt.getName();
1191
1192
  // If the context info wasn't a framework, couldn't be a subframework.
1193
44.0k
  const unsigned DotFrameworkLen = 10;
1194
44.0k
  auto FrameworkPos = ContextName.find(".framework");
1195
44.0k
  if (FrameworkPos == StringRef::npos ||
1196
44.0k
      
(38.3k
ContextName[FrameworkPos + DotFrameworkLen] != '/'38.3k
&&
1197
38.3k
       
ContextName[FrameworkPos + DotFrameworkLen] != '\\'0
))
1198
5.68k
    return std::nullopt;
1199
1200
38.3k
  SmallString<1024> FrameworkName(ContextName.data(), ContextName.data() +
1201
38.3k
                                                          FrameworkPos +
1202
38.3k
                                                          DotFrameworkLen + 1);
1203
1204
  // Append Frameworks/HIToolbox.framework/
1205
38.3k
  FrameworkName += "Frameworks/";
1206
38.3k
  FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
1207
38.3k
  FrameworkName += ".framework/";
1208
1209
38.3k
  auto &CacheLookup =
1210
38.3k
      *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
1211
38.3k
                                          FrameworkCacheEntry())).first;
1212
1213
  // Some other location?
1214
38.3k
  if (CacheLookup.second.Directory &&
1215
38.3k
      
CacheLookup.first().size() == FrameworkName.size()32.5k
&&
1216
38.3k
      memcmp(CacheLookup.first().data(), &FrameworkName[0],
1217
0
             CacheLookup.first().size()) != 0)
1218
0
    return std::nullopt;
1219
1220
  // Cache subframework.
1221
38.3k
  if (!CacheLookup.second.Directory) {
1222
5.80k
    ++NumSubFrameworkLookups;
1223
1224
    // If the framework dir doesn't exist, we fail.
1225
5.80k
    auto Dir = FileMgr.getOptionalDirectoryRef(FrameworkName);
1226
5.80k
    if (!Dir)
1227
2.04k
      return std::nullopt;
1228
1229
    // Otherwise, if it does, remember that this is the right direntry for this
1230
    // framework.
1231
3.76k
    CacheLookup.second.Directory = Dir;
1232
3.76k
  }
1233
1234
1235
36.3k
  if (RelativePath) {
1236
34.7k
    RelativePath->clear();
1237
34.7k
    RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
1238
34.7k
  }
1239
1240
  // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
1241
36.3k
  SmallString<1024> HeadersFilename(FrameworkName);
1242
36.3k
  HeadersFilename += "Headers/";
1243
36.3k
  if (SearchPath) {
1244
34.7k
    SearchPath->clear();
1245
    // Without trailing '/'.
1246
34.7k
    SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1247
34.7k
  }
1248
1249
36.3k
  HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1250
36.3k
  auto File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
1251
36.3k
  if (!File) {
1252
    // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
1253
1.57k
    HeadersFilename = FrameworkName;
1254
1.57k
    HeadersFilename += "PrivateHeaders/";
1255
1.57k
    if (SearchPath) {
1256
2
      SearchPath->clear();
1257
      // Without trailing '/'.
1258
2
      SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1259
2
    }
1260
1261
1.57k
    HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1262
1.57k
    File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
1263
1264
1.57k
    if (!File)
1265
1.57k
      return std::nullopt;
1266
1.57k
  }
1267
1268
  // This file is a system header or C++ unfriendly if the old file is.
1269
  //
1270
  // Note that the temporary 'DirInfo' is required here, as either call to
1271
  // getFileInfo could resize the vector and we don't want to rely on order
1272
  // of evaluation.
1273
34.7k
  unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
1274
34.7k
  getFileInfo(*File).DirInfo = DirInfo;
1275
1276
34.7k
  FrameworkName.pop_back(); // remove the trailing '/'
1277
34.7k
  if (!findUsableModuleForFrameworkHeader(*File, FrameworkName,
1278
34.7k
                                          RequestingModule, SuggestedModule,
1279
34.7k
                                          /*IsSystem*/ false))
1280
0
    return std::nullopt;
1281
1282
34.7k
  return *File;
1283
34.7k
}
1284
1285
//===----------------------------------------------------------------------===//
1286
// File Info Management.
1287
//===----------------------------------------------------------------------===//
1288
1289
/// Merge the header file info provided by \p OtherHFI into the current
1290
/// header file info (\p HFI)
1291
static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
1292
376k
                                const HeaderFileInfo &OtherHFI) {
1293
376k
  assert(OtherHFI.External && "expected to merge external HFI");
1294
1295
376k
  HFI.isImport |= OtherHFI.isImport;
1296
376k
  HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
1297
376k
  HFI.isModuleHeader |= OtherHFI.isModuleHeader;
1298
1299
376k
  if (!HFI.ControllingMacro && 
!HFI.ControllingMacroID375k
) {
1300
375k
    HFI.ControllingMacro = OtherHFI.ControllingMacro;
1301
375k
    HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
1302
375k
  }
1303
1304
376k
  HFI.DirInfo = OtherHFI.DirInfo;
1305
376k
  HFI.External = (!HFI.IsValid || 
HFI.External370k
);
1306
376k
  HFI.IsValid = true;
1307
376k
  HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
1308
1309
376k
  if (HFI.Framework.empty())
1310
375k
    HFI.Framework = OtherHFI.Framework;
1311
376k
}
1312
1313
/// getFileInfo - Return the HeaderFileInfo structure for the specified
1314
/// FileEntry.
1315
15.6M
HeaderFileInfo &HeaderSearch::getFileInfo(FileEntryRef FE) {
1316
15.6M
  if (FE.getUID() >= FileInfo.size())
1317
2.53M
    FileInfo.resize(FE.getUID() + 1);
1318
1319
15.6M
  HeaderFileInfo *HFI = &FileInfo[FE.getUID()];
1320
  // FIXME: Use a generation count to check whether this is really up to date.
1321
15.6M
  if (ExternalSource && 
!HFI->Resolved290k
) {
1322
260k
    auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1323
260k
    if (ExternalHFI.IsValid) {
1324
9.49k
      HFI->Resolved = true;
1325
9.49k
      if (ExternalHFI.External)
1326
9.49k
        mergeHeaderFileInfo(*HFI, ExternalHFI);
1327
9.49k
    }
1328
260k
  }
1329
1330
15.6M
  HFI->IsValid = true;
1331
  // We have local information about this header file, so it's no longer
1332
  // strictly external.
1333
15.6M
  HFI->External = false;
1334
15.6M
  return *HFI;
1335
15.6M
}
1336
1337
const HeaderFileInfo *
1338
5.44M
HeaderSearch::getExistingFileInfo(FileEntryRef FE, bool WantExternal) const {
1339
  // If we have an external source, ensure we have the latest information.
1340
  // FIXME: Use a generation count to check whether this is really up to date.
1341
5.44M
  HeaderFileInfo *HFI;
1342
5.44M
  if (ExternalSource) {
1343
3.34M
    if (FE.getUID() >= FileInfo.size()) {
1344
118k
      if (!WantExternal)
1345
0
        return nullptr;
1346
118k
      FileInfo.resize(FE.getUID() + 1);
1347
118k
    }
1348
1349
3.34M
    HFI = &FileInfo[FE.getUID()];
1350
3.34M
    if (!WantExternal && 
(3.15M
!HFI->IsValid3.15M
||
HFI->External2.60M
))
1351
556k
      return nullptr;
1352
2.78M
    if (!HFI->Resolved) {
1353
2.39M
      auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1354
2.39M
      if (ExternalHFI.IsValid) {
1355
366k
        HFI->Resolved = true;
1356
366k
        if (ExternalHFI.External)
1357
366k
          mergeHeaderFileInfo(*HFI, ExternalHFI);
1358
366k
      }
1359
2.39M
    }
1360
2.78M
  } else 
if (2.09M
FE.getUID() >= FileInfo.size()2.09M
) {
1361
1.71M
    return nullptr;
1362
1.71M
  } else {
1363
384k
    HFI = &FileInfo[FE.getUID()];
1364
384k
  }
1365
1366
3.17M
  if (!HFI->IsValid || 
(2.90M
HFI->External2.90M
&&
!WantExternal2.29k
))
1367
263k
    return nullptr;
1368
1369
2.90M
  return HFI;
1370
3.17M
}
1371
1372
614
bool HeaderSearch::isFileMultipleIncludeGuarded(FileEntryRef File) const {
1373
  // Check if we've entered this file and found an include guard or #pragma
1374
  // once. Note that we dor't check for #import, because that's not a property
1375
  // of the file itself.
1376
614
  if (auto *HFI = getExistingFileInfo(File))
1377
614
    return HFI->isPragmaOnce || 
HFI->ControllingMacro604
||
1378
614
           
HFI->ControllingMacroID584
;
1379
0
  return false;
1380
614
}
1381
1382
void HeaderSearch::MarkFileModuleHeader(FileEntryRef FE,
1383
                                        ModuleMap::ModuleHeaderRole Role,
1384
2.00M
                                        bool isCompilingModuleHeader) {
1385
2.00M
  bool isModularHeader = ModuleMap::isModular(Role);
1386
1387
  // Don't mark the file info as non-external if there's nothing to change.
1388
2.00M
  if (!isCompilingModuleHeader) {
1389
1.99M
    if (!isModularHeader)
1390
80.4k
      return;
1391
1.91M
    auto *HFI = getExistingFileInfo(FE);
1392
1.91M
    if (HFI && 
HFI->isModuleHeader5.40k
)
1393
5.19k
      return;
1394
1.91M
  }
1395
1396
1.91M
  auto &HFI = getFileInfo(FE);
1397
1.91M
  HFI.isModuleHeader |= isModularHeader;
1398
1.91M
  HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
1399
1.91M
}
1400
1401
bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1402
                                          FileEntryRef File, bool isImport,
1403
                                          bool ModulesEnabled, Module *M,
1404
3.79M
                                          bool &IsFirstIncludeOfFile) {
1405
3.79M
  ++NumIncluded; // Count # of attempted #includes.
1406
1407
3.79M
  IsFirstIncludeOfFile = false;
1408
1409
  // Get information about this file.
1410
3.79M
  HeaderFileInfo &FileInfo = getFileInfo(File);
1411
1412
  // FIXME: this is a workaround for the lack of proper modules-aware support
1413
  // for #import / #pragma once
1414
3.79M
  auto TryEnterImported = [&]() -> bool {
1415
122k
    if (!ModulesEnabled)
1416
114k
      return false;
1417
    // Ensure FileInfo bits are up to date.
1418
8.79k
    ModMap.resolveHeaderDirectives(File);
1419
    // Modules with builtins are special; multiple modules use builtins as
1420
    // modular headers, example:
1421
    //
1422
    //    module stddef { header "stddef.h" export * }
1423
    //
1424
    // After module map parsing, this expands to:
1425
    //
1426
    //    module stddef {
1427
    //      header "/path_to_builtin_dirs/stddef.h"
1428
    //      textual "stddef.h"
1429
    //    }
1430
    //
1431
    // It's common that libc++ and system modules will both define such
1432
    // submodules. Make sure cached results for a builtin header won't
1433
    // prevent other builtin modules from potentially entering the builtin
1434
    // header. Note that builtins are header guarded and the decision to
1435
    // actually enter them is postponed to the controlling macros logic below.
1436
8.79k
    bool TryEnterHdr = false;
1437
8.79k
    if (FileInfo.isCompilingModuleHeader && 
FileInfo.isModuleHeader4.66k
)
1438
4.66k
      TryEnterHdr = ModMap.isBuiltinHeader(File);
1439
1440
    // Textual headers can be #imported from different modules. Since ObjC
1441
    // headers find in the wild might rely only on #import and do not contain
1442
    // controlling macros, be conservative and only try to enter textual headers
1443
    // if such macro is present.
1444
8.79k
    if (!FileInfo.isModuleHeader &&
1445
8.79k
        
FileInfo.getControllingMacro(ExternalLookup)4.13k
)
1446
1.75k
      TryEnterHdr = true;
1447
8.79k
    return TryEnterHdr;
1448
122k
  };
1449
1450
  // If this is a #import directive, check that we have not already imported
1451
  // this header.
1452
3.79M
  if (isImport) {
1453
    // If this has already been imported, don't import it again.
1454
124k
    FileInfo.isImport = true;
1455
1456
    // Has this already been #import'ed or #include'd?
1457
124k
    if (PP.alreadyIncluded(File) && 
!TryEnterImported()80.5k
)
1458
79.2k
      return false;
1459
3.66M
  } else {
1460
    // Otherwise, if this is a #include of a file that was previously #import'd
1461
    // or if this is the second #include of a #pragma once file, ignore it.
1462
3.66M
    if ((FileInfo.isPragmaOnce || 
FileInfo.isImport3.66M
) &&
!TryEnterImported()42.3k
)
1463
41.5k
      return false;
1464
3.66M
  }
1465
1466
  // Next, check to see if the file is wrapped with #ifndef guards.  If so, and
1467
  // if the macro that guards it is defined, we know the #include has no effect.
1468
3.67M
  if (const IdentifierInfo *ControllingMacro
1469
3.67M
      = FileInfo.getControllingMacro(ExternalLookup)) {
1470
    // If the header corresponds to a module, check whether the macro is already
1471
    // defined in that module rather than checking in the current set of visible
1472
    // modules.
1473
2.80M
    if (M ? 
PP.isMacroDefinedInLocalModule(ControllingMacro, M)11.6k
1474
2.80M
          : 
PP.isMacroDefined(ControllingMacro)2.79M
) {
1475
2.79M
      ++NumMultiIncludeFileOptzn;
1476
2.79M
      return false;
1477
2.79M
    }
1478
2.80M
  }
1479
1480
880k
  IsFirstIncludeOfFile = PP.markIncluded(File);
1481
1482
880k
  return true;
1483
3.67M
}
1484
1485
1
size_t HeaderSearch::getTotalMemory() const {
1486
1
  return SearchDirs.capacity()
1487
1
    + llvm::capacity_in_bytes(FileInfo)
1488
1
    + llvm::capacity_in_bytes(HeaderMaps)
1489
1
    + LookupFileCache.getAllocator().getTotalMemory()
1490
1
    + FrameworkMap.getAllocator().getTotalMemory();
1491
1
}
1492
1493
19
unsigned HeaderSearch::searchDirIdx(const DirectoryLookup &DL) const {
1494
19
  return &DL - &*SearchDirs.begin();
1495
19
}
1496
1497
313k
StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
1498
313k
  return FrameworkNames.insert(Framework).first->first();
1499
313k
}
1500
1501
2
StringRef HeaderSearch::getIncludeNameForHeader(const FileEntry *File) const {
1502
2
  auto It = IncludeNames.find(File);
1503
2
  if (It == IncludeNames.end())
1504
0
    return {};
1505
2
  return It->second;
1506
2
}
1507
1508
bool HeaderSearch::hasModuleMap(StringRef FileName,
1509
                                const DirectoryEntry *Root,
1510
3.46M
                                bool IsSystem) {
1511
3.46M
  if (!HSOpts->ImplicitModuleMaps)
1512
3.42M
    return false;
1513
1514
40.7k
  SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
1515
1516
40.7k
  StringRef DirName = FileName;
1517
45.2k
  do {
1518
    // Get the parent directory name.
1519
45.2k
    DirName = llvm::sys::path::parent_path(DirName);
1520
45.2k
    if (DirName.empty())
1521
34
      return false;
1522
1523
    // Determine whether this directory exists.
1524
45.1k
    auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
1525
45.1k
    if (!Dir)
1526
0
      return false;
1527
1528
    // Try to load the module map file in this directory.
1529
45.1k
    switch (loadModuleMapFile(*Dir, IsSystem,
1530
45.1k
                              llvm::sys::path::extension(Dir->getName()) ==
1531
45.1k
                                  ".framework")) {
1532
732
    case LMM_NewlyLoaded:
1533
40.4k
    case LMM_AlreadyLoaded:
1534
      // Success. All of the directories we stepped through inherit this module
1535
      // map file.
1536
44.2k
      for (unsigned I = 0, N = FixUpDirectories.size(); I != N; 
++I3.82k
)
1537
3.82k
        DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1538
40.4k
      return true;
1539
1540
0
    case LMM_NoDirectory:
1541
4.78k
    case LMM_InvalidModuleMap:
1542
4.78k
      break;
1543
45.1k
    }
1544
1545
    // If we hit the top of our search, we're done.
1546
4.78k
    if (*Dir == Root)
1547
260
      return false;
1548
1549
    // Keep track of all of the directories we checked, so we can mark them as
1550
    // having module maps if we eventually do find a module map.
1551
4.52k
    FixUpDirectories.push_back(*Dir);
1552
4.52k
  } while (true);
1553
40.7k
}
1554
1555
ModuleMap::KnownHeader
1556
HeaderSearch::findModuleForHeader(FileEntryRef File, bool AllowTextual,
1557
3.80M
                                  bool AllowExcluded) const {
1558
3.80M
  if (ExternalSource) {
1559
    // Make sure the external source has handled header info about this file,
1560
    // which includes whether the file is part of a module.
1561
41.1k
    (void)getExistingFileInfo(File);
1562
41.1k
  }
1563
3.80M
  return ModMap.findModuleForHeader(File, AllowTextual, AllowExcluded);
1564
3.80M
}
1565
1566
ArrayRef<ModuleMap::KnownHeader>
1567
969
HeaderSearch::findAllModulesForHeader(FileEntryRef File) const {
1568
969
  if (ExternalSource) {
1569
    // Make sure the external source has handled header info about this file,
1570
    // which includes whether the file is part of a module.
1571
879
    (void)getExistingFileInfo(File);
1572
879
  }
1573
969
  return ModMap.findAllModulesForHeader(File);
1574
969
}
1575
1576
ArrayRef<ModuleMap::KnownHeader>
1577
15.9k
HeaderSearch::findResolvedModulesForHeader(FileEntryRef File) const {
1578
15.9k
  if (ExternalSource) {
1579
    // Make sure the external source has handled header info about this file,
1580
    // which includes whether the file is part of a module.
1581
11.8k
    (void)getExistingFileInfo(File);
1582
11.8k
  }
1583
15.9k
  return ModMap.findResolvedModulesForHeader(File);
1584
15.9k
}
1585
1586
static bool suggestModule(HeaderSearch &HS, FileEntryRef File,
1587
                          Module *RequestingModule,
1588
3.80M
                          ModuleMap::KnownHeader *SuggestedModule) {
1589
3.80M
  ModuleMap::KnownHeader Module =
1590
3.80M
      HS.findModuleForHeader(File, /*AllowTextual*/true);
1591
1592
  // If this module specifies [no_undeclared_includes], we cannot find any
1593
  // file that's in a non-dependency module.
1594
3.80M
  if (RequestingModule && 
Module48.4k
&&
RequestingModule->NoUndeclaredIncludes45.7k
) {
1595
16.1k
    HS.getModuleMap().resolveUses(RequestingModule, /*Complain*/ false);
1596
16.1k
    if (!RequestingModule->directlyUses(Module.getModule())) {
1597
      // Builtin headers are a special case. Multiple modules can use the same
1598
      // builtin as a modular header (see also comment in
1599
      // ShouldEnterIncludeFile()), so the builtin header may have been
1600
      // "claimed" by an unrelated module. This shouldn't prevent us from
1601
      // including the builtin header textually in this module.
1602
418
      if (HS.getModuleMap().isBuiltinHeader(File)) {
1603
0
        if (SuggestedModule)
1604
0
          *SuggestedModule = ModuleMap::KnownHeader();
1605
0
        return true;
1606
0
      }
1607
      // TODO: Add this module (or just its module map file) into something like
1608
      // `RequestingModule->AffectingClangModules`.
1609
418
      return false;
1610
418
    }
1611
16.1k
  }
1612
1613
3.80M
  if (SuggestedModule)
1614
3.80M
    *SuggestedModule = (Module.getRole() & ModuleMap::TextualHeader)
1615
3.80M
                           ? 
ModuleMap::KnownHeader()2.07k
1616
3.80M
                           : 
Module3.80M
;
1617
1618
3.80M
  return true;
1619
3.80M
}
1620
1621
bool HeaderSearch::findUsableModuleForHeader(
1622
    FileEntryRef File, const DirectoryEntry *Root, Module *RequestingModule,
1623
3.47M
    ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1624
3.47M
  if (needModuleLookup(RequestingModule, SuggestedModule)) {
1625
    // If there is a module that corresponds to this header, suggest it.
1626
3.46M
    hasModuleMap(File.getName(), Root, IsSystemHeaderDir);
1627
3.46M
    return suggestModule(*this, File, RequestingModule, SuggestedModule);
1628
3.46M
  }
1629
16.9k
  return true;
1630
3.47M
}
1631
1632
bool HeaderSearch::findUsableModuleForFrameworkHeader(
1633
    FileEntryRef File, StringRef FrameworkName, Module *RequestingModule,
1634
345k
    ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1635
  // If we're supposed to suggest a module, look for one now.
1636
345k
  if (needModuleLookup(RequestingModule, SuggestedModule)) {
1637
    // Find the top-level framework based on this framework.
1638
345k
    SmallVector<std::string, 4> SubmodulePath;
1639
345k
    OptionalDirectoryEntryRef TopFrameworkDir =
1640
345k
        ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1641
345k
    assert(TopFrameworkDir && "Could not find the top-most framework dir");
1642
1643
    // Determine the name of the top-level framework.
1644
345k
    StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1645
1646
    // Load this framework module. If that succeeds, find the suggested module
1647
    // for this header, if any.
1648
345k
    loadFrameworkModule(ModuleName, *TopFrameworkDir, IsSystemFramework);
1649
1650
    // FIXME: This can find a module not part of ModuleName, which is
1651
    // important so that we're consistent about whether this header
1652
    // corresponds to a module. Possibly we should lock down framework modules
1653
    // so that this is not possible.
1654
345k
    return suggestModule(*this, File, RequestingModule, SuggestedModule);
1655
345k
  }
1656
0
  return true;
1657
345k
}
1658
1659
static OptionalFileEntryRef getPrivateModuleMap(FileEntryRef File,
1660
10.0k
                                                FileManager &FileMgr) {
1661
10.0k
  StringRef Filename = llvm::sys::path::filename(File.getName());
1662
10.0k
  SmallString<128>  PrivateFilename(File.getDir().getName());
1663
10.0k
  if (Filename == "module.map")
1664
1.10k
    llvm::sys::path::append(PrivateFilename, "module_private.map");
1665
8.90k
  else if (Filename == "module.modulemap")
1666
8.42k
    llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1667
476
  else
1668
476
    return std::nullopt;
1669
9.53k
  return FileMgr.getOptionalFileRef(PrivateFilename);
1670
10.0k
}
1671
1672
bool HeaderSearch::loadModuleMapFile(FileEntryRef File, bool IsSystem,
1673
                                     FileID ID, unsigned *Offset,
1674
3.65k
                                     StringRef OriginalModuleMapFile) {
1675
  // Find the directory for the module. For frameworks, that may require going
1676
  // up from the 'Modules' directory.
1677
3.65k
  OptionalDirectoryEntryRef Dir;
1678
3.65k
  if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) {
1679
51
    Dir = FileMgr.getOptionalDirectoryRef(".");
1680
3.60k
  } else {
1681
3.60k
    if (!OriginalModuleMapFile.empty()) {
1682
      // We're building a preprocessed module map. Find or invent the directory
1683
      // that it originally occupied.
1684
16
      Dir = FileMgr.getOptionalDirectoryRef(
1685
16
          llvm::sys::path::parent_path(OriginalModuleMapFile));
1686
16
      if (!Dir) {
1687
0
        auto FakeFile = FileMgr.getVirtualFileRef(OriginalModuleMapFile, 0, 0);
1688
0
        Dir = FakeFile.getDir();
1689
0
      }
1690
3.58k
    } else {
1691
3.58k
      Dir = File.getDir();
1692
3.58k
    }
1693
1694
3.60k
    assert(Dir && "parent must exist");
1695
3.60k
    StringRef DirName(Dir->getName());
1696
3.60k
    if (llvm::sys::path::filename(DirName) == "Modules") {
1697
266
      DirName = llvm::sys::path::parent_path(DirName);
1698
266
      if (DirName.endswith(".framework"))
1699
245
        if (auto MaybeDir = FileMgr.getOptionalDirectoryRef(DirName))
1700
245
          Dir = *MaybeDir;
1701
      // FIXME: This assert can fail if there's a race between the above check
1702
      // and the removal of the directory.
1703
266
      assert(Dir && "parent must exist");
1704
266
    }
1705
3.60k
  }
1706
1707
3.65k
  assert(Dir && "module map home directory must exist");
1708
3.65k
  switch (loadModuleMapFileImpl(File, IsSystem, *Dir, ID, Offset)) {
1709
154
  case LMM_AlreadyLoaded:
1710
3.65k
  case LMM_NewlyLoaded:
1711
3.65k
    return false;
1712
0
  case LMM_NoDirectory:
1713
2
  case LMM_InvalidModuleMap:
1714
2
    return true;
1715
3.65k
  }
1716
0
  llvm_unreachable("Unknown load module map result");
1717
0
}
1718
1719
HeaderSearch::LoadModuleMapResult
1720
HeaderSearch::loadModuleMapFileImpl(FileEntryRef File, bool IsSystem,
1721
                                    DirectoryEntryRef Dir, FileID ID,
1722
33.8k
                                    unsigned *Offset) {
1723
  // Check whether we've already loaded this module map, and mark it as being
1724
  // loaded in case we recursively try to load it from itself.
1725
33.8k
  auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1726
33.8k
  if (!AddResult.second)
1727
23.7k
    return AddResult.first->second ? LMM_AlreadyLoaded : 
LMM_InvalidModuleMap0
;
1728
1729
10.1k
  if (ModMap.parseModuleMapFile(File, IsSystem, Dir, ID, Offset)) {
1730
97
    LoadedModuleMaps[File] = false;
1731
97
    return LMM_InvalidModuleMap;
1732
97
  }
1733
1734
  // Try to load a corresponding private module map.
1735
10.0k
  if (OptionalFileEntryRef PMMFile = getPrivateModuleMap(File, FileMgr)) {
1736
189
    if (ModMap.parseModuleMapFile(*PMMFile, IsSystem, Dir)) {
1737
0
      LoadedModuleMaps[File] = false;
1738
0
      return LMM_InvalidModuleMap;
1739
0
    }
1740
189
  }
1741
1742
  // This directory has a module map.
1743
10.0k
  return LMM_NewlyLoaded;
1744
10.0k
}
1745
1746
OptionalFileEntryRef
1747
424k
HeaderSearch::lookupModuleMapFile(DirectoryEntryRef Dir, bool IsFramework) {
1748
424k
  if (!HSOpts->ImplicitModuleMaps)
1749
335k
    return std::nullopt;
1750
  // For frameworks, the preferred spelling is Modules/module.modulemap, but
1751
  // module.map at the framework root is also accepted.
1752
89.0k
  SmallString<128> ModuleMapFileName(Dir.getName());
1753
89.0k
  if (IsFramework)
1754
9.55k
    llvm::sys::path::append(ModuleMapFileName, "Modules");
1755
89.0k
  llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1756
89.0k
  if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName))
1757
28.6k
    return *F;
1758
1759
  // Continue to allow module.map
1760
60.3k
  ModuleMapFileName = Dir.getName();
1761
60.3k
  llvm::sys::path::append(ModuleMapFileName, "module.map");
1762
60.3k
  if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName))
1763
1.70k
    return *F;
1764
1765
  // For frameworks, allow to have a private module map with a preferred
1766
  // spelling when a public module map is absent.
1767
58.6k
  if (IsFramework) {
1768
719
    ModuleMapFileName = Dir.getName();
1769
719
    llvm::sys::path::append(ModuleMapFileName, "Modules",
1770
719
                            "module.private.modulemap");
1771
719
    if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName))
1772
2
      return *F;
1773
719
  }
1774
58.6k
  return std::nullopt;
1775
58.6k
}
1776
1777
Module *HeaderSearch::loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
1778
346k
                                          bool IsSystem) {
1779
  // Try to load a module map file.
1780
346k
  switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
1781
336k
  case LMM_InvalidModuleMap:
1782
    // Try to infer a module map from the framework directory.
1783
336k
    if (HSOpts->ImplicitModuleMaps)
1784
416
      ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
1785
336k
    break;
1786
1787
0
  case LMM_NoDirectory:
1788
0
    return nullptr;
1789
1790
9.50k
  case LMM_AlreadyLoaded:
1791
10.7k
  case LMM_NewlyLoaded:
1792
10.7k
    break;
1793
346k
  }
1794
1795
346k
  return ModMap.findModule(Name);
1796
346k
}
1797
1798
HeaderSearch::LoadModuleMapResult
1799
HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1800
62.0k
                                bool IsFramework) {
1801
62.0k
  if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName))
1802
55.2k
    return loadModuleMapFile(*Dir, IsSystem, IsFramework);
1803
1804
6.87k
  return LMM_NoDirectory;
1805
62.0k
}
1806
1807
HeaderSearch::LoadModuleMapResult
1808
HeaderSearch::loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
1809
455k
                                bool IsFramework) {
1810
455k
  auto KnownDir = DirectoryHasModuleMap.find(Dir);
1811
455k
  if (KnownDir != DirectoryHasModuleMap.end())
1812
31.0k
    return KnownDir->second ? LMM_AlreadyLoaded : 
LMM_InvalidModuleMap0
;
1813
1814
424k
  if (OptionalFileEntryRef ModuleMapFile =
1815
424k
          lookupModuleMapFile(Dir, IsFramework)) {
1816
30.2k
    LoadModuleMapResult Result =
1817
30.2k
        loadModuleMapFileImpl(*ModuleMapFile, IsSystem, Dir);
1818
    // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1819
    // E.g. Foo.framework/Modules/module.modulemap
1820
    //      ^Dir                  ^ModuleMapFile
1821
30.2k
    if (Result == LMM_NewlyLoaded)
1822
6.51k
      DirectoryHasModuleMap[Dir] = true;
1823
23.7k
    else if (Result == LMM_InvalidModuleMap)
1824
95
      DirectoryHasModuleMap[Dir] = false;
1825
30.2k
    return Result;
1826
30.2k
  }
1827
394k
  return LMM_InvalidModuleMap;
1828
424k
}
1829
1830
2
void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
1831
2
  Modules.clear();
1832
1833
2
  if (HSOpts->ImplicitModuleMaps) {
1834
    // Load module maps for each of the header search directories.
1835
8
    for (DirectoryLookup &DL : search_dir_range()) {
1836
8
      bool IsSystem = DL.isSystemHeaderDirectory();
1837
8
      if (DL.isFramework()) {
1838
4
        std::error_code EC;
1839
4
        SmallString<128> DirNative;
1840
4
        llvm::sys::path::native(DL.getFrameworkDirRef()->getName(), DirNative);
1841
1842
        // Search each of the ".framework" directories to load them as modules.
1843
4
        llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1844
4
        for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
1845
4
                                           DirEnd;
1846
410
             Dir != DirEnd && 
!EC406
;
Dir.increment(EC)406
) {
1847
406
          if (llvm::sys::path::extension(Dir->path()) != ".framework")
1848
2
            continue;
1849
1850
404
          auto FrameworkDir = FileMgr.getOptionalDirectoryRef(Dir->path());
1851
404
          if (!FrameworkDir)
1852
0
            continue;
1853
1854
          // Load this framework module.
1855
404
          loadFrameworkModule(llvm::sys::path::stem(Dir->path()), *FrameworkDir,
1856
404
                              IsSystem);
1857
404
        }
1858
4
        continue;
1859
4
      }
1860
1861
      // FIXME: Deal with header maps.
1862
4
      if (DL.isHeaderMap())
1863
0
        continue;
1864
1865
      // Try to load a module map file for the search directory.
1866
4
      loadModuleMapFile(*DL.getDirRef(), IsSystem, /*IsFramework*/ false);
1867
1868
      // Try to load module map files for immediate subdirectories of this
1869
      // search directory.
1870
4
      loadSubdirectoryModuleMaps(DL);
1871
4
    }
1872
2
  }
1873
1874
  // Populate the list of modules.
1875
2
  llvm::transform(ModMap.modules(), std::back_inserter(Modules),
1876
438
                  [](const auto &NameAndMod) { return NameAndMod.second; });
1877
2
}
1878
1879
49
void HeaderSearch::loadTopLevelSystemModules() {
1880
49
  if (!HSOpts->ImplicitModuleMaps)
1881
0
    return;
1882
1883
  // Load module maps for each of the header search directories.
1884
128
  
for (const DirectoryLookup &DL : search_dir_range())49
{
1885
    // We only care about normal header directories.
1886
128
    if (!DL.isNormalDir())
1887
14
      continue;
1888
1889
    // Try to load a module map file for the search directory.
1890
114
    loadModuleMapFile(*DL.getDirRef(), DL.isSystemHeaderDirectory(),
1891
114
                      DL.isFramework());
1892
114
  }
1893
49
}
1894
1895
2.72k
void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1896
2.72k
  assert(HSOpts->ImplicitModuleMaps &&
1897
2.72k
         "Should not be loading subdirectory module maps");
1898
1899
2.72k
  if (SearchDir.haveSearchedAllModuleMaps())
1900
0
    return;
1901
1902
2.72k
  std::error_code EC;
1903
2.72k
  SmallString<128> Dir = SearchDir.getDirRef()->getName();
1904
2.72k
  FileMgr.makeAbsolutePath(Dir);
1905
2.72k
  SmallString<128> DirNative;
1906
2.72k
  llvm::sys::path::native(Dir, DirNative);
1907
2.72k
  llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1908
2.72k
  for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
1909
434k
       Dir != DirEnd && 
!EC432k
;
Dir.increment(EC)432k
) {
1910
432k
    if (Dir->type() == llvm::sys::fs::file_type::regular_file)
1911
375k
      continue;
1912
56.6k
    bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1913
56.6k
    if (IsFramework == SearchDir.isFramework())
1914
56.6k
      loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1915
56.6k
                        SearchDir.isFramework());
1916
56.6k
  }
1917
1918
2.72k
  SearchDir.setSearchedAllModuleMaps(true);
1919
2.72k
}
1920
1921
std::string HeaderSearch::suggestPathToFileForDiagnostics(
1922
272
    FileEntryRef File, llvm::StringRef MainFile, bool *IsAngled) const {
1923
272
  return suggestPathToFileForDiagnostics(File.getName(), /*WorkingDir=*/"",
1924
272
                                         MainFile, IsAngled);
1925
272
}
1926
1927
std::string HeaderSearch::suggestPathToFileForDiagnostics(
1928
    llvm::StringRef File, llvm::StringRef WorkingDir, llvm::StringRef MainFile,
1929
286
    bool *IsAngled) const {
1930
286
  using namespace llvm::sys;
1931
1932
286
  llvm::SmallString<32> FilePath = File;
1933
  // remove_dots switches to backslashes on windows as a side-effect!
1934
  // We always want to suggest forward slashes for includes.
1935
  // (not remove_dots(..., posix) as that misparses windows paths).
1936
286
  path::remove_dots(FilePath, /*remove_dot_dot=*/true);
1937
286
  path::native(FilePath, path::Style::posix);
1938
286
  File = FilePath;
1939
1940
286
  unsigned BestPrefixLength = 0;
1941
  // Checks whether `Dir` is a strict path prefix of `File`. If so and that's
1942
  // the longest prefix we've seen so for it, returns true and updates the
1943
  // `BestPrefixLength` accordingly.
1944
569
  auto CheckDir = [&](llvm::SmallString<32> Dir) -> bool {
1945
569
    if (!WorkingDir.empty() && 
!path::is_absolute(Dir)3
)
1946
2
      fs::make_absolute(WorkingDir, Dir);
1947
569
    path::remove_dots(Dir, /*remove_dot_dot=*/true);
1948
569
    for (auto NI = path::begin(File), NE = path::end(File),
1949
569
              DI = path::begin(Dir), DE = path::end(Dir);
1950
5.55k
         NI != NE; 
++NI, ++DI4.98k
) {
1951
5.55k
      if (DI == DE) {
1952
        // Dir is a prefix of File, up to choice of path separators.
1953
287
        unsigned PrefixLength = NI - path::begin(File);
1954
287
        if (PrefixLength > BestPrefixLength) {
1955
286
          BestPrefixLength = PrefixLength;
1956
286
          return true;
1957
286
        }
1958
1
        break;
1959
287
      }
1960
1961
      // Consider all path separators equal.
1962
5.26k
      if (NI->size() == 1 && 
DI->size() == 1590
&&
1963
5.26k
          
path::is_separator(NI->front())590
&&
path::is_separator(DI->front())568
)
1964
568
        continue;
1965
1966
      // Special case Apple .sdk folders since the search path is typically a
1967
      // symlink like `iPhoneSimulator14.5.sdk` while the file is instead
1968
      // located in `iPhoneSimulator.sdk` (the real folder).
1969
4.69k
      if (NI->endswith(".sdk") && 
DI->endswith(".sdk")1
) {
1970
1
        StringRef NBasename = path::stem(*NI);
1971
1
        StringRef DBasename = path::stem(*DI);
1972
1
        if (DBasename.startswith(NBasename))
1973
1
          continue;
1974
1
      }
1975
1976
4.69k
      if (*NI != *DI)
1977
282
        break;
1978
4.69k
    }
1979
283
    return false;
1980
569
  };
1981
1982
286
  bool BestPrefixIsFramework = false;
1983
567
  for (const DirectoryLookup &DL : search_dir_range()) {
1984
567
    if (DL.isNormalDir()) {
1985
560
      StringRef Dir = DL.getDirRef()->getName();
1986
560
      if (CheckDir(Dir)) {
1987
281
        if (IsAngled)
1988
271
          *IsAngled = BestPrefixLength && isSystem(DL.getDirCharacteristic());
1989
281
        BestPrefixIsFramework = false;
1990
281
      }
1991
560
    } else 
if (7
DL.isFramework()7
) {
1992
6
      StringRef Dir = DL.getFrameworkDirRef()->getName();
1993
6
      if (CheckDir(Dir)) {
1994
        // Framework includes by convention use <>.
1995
3
        if (IsAngled)
1996
2
          *IsAngled = BestPrefixLength;
1997
3
        BestPrefixIsFramework = true;
1998
3
      }
1999
6
    }
2000
567
  }
2001
2002
  // Try to shorten include path using TUs directory, if we couldn't find any
2003
  // suitable prefix in include search paths.
2004
286
  if (!BestPrefixLength && 
CheckDir(path::parent_path(MainFile))3
) {
2005
2
    if (IsAngled)
2006
1
      *IsAngled = false;
2007
2
    BestPrefixIsFramework = false;
2008
2
  }
2009
2010
  // Try resolving resulting filename via reverse search in header maps,
2011
  // key from header name is user preferred name for the include file.
2012
286
  StringRef Filename = File.drop_front(BestPrefixLength);
2013
566
  for (const DirectoryLookup &DL : search_dir_range()) {
2014
566
    if (!DL.isHeaderMap())
2015
565
      continue;
2016
2017
1
    StringRef SpelledFilename =
2018
1
        DL.getHeaderMap()->reverseLookupFilename(Filename);
2019
1
    if (!SpelledFilename.empty()) {
2020
1
      Filename = SpelledFilename;
2021
1
      BestPrefixIsFramework = false;
2022
1
      break;
2023
1
    }
2024
1
  }
2025
2026
  // If the best prefix is a framework path, we need to compute the proper
2027
  // include spelling for the framework header.
2028
286
  bool IsPrivateHeader;
2029
286
  SmallString<128> FrameworkName, IncludeSpelling;
2030
286
  if (BestPrefixIsFramework &&
2031
286
      isFrameworkStylePath(Filename, IsPrivateHeader, FrameworkName,
2032
3
                           IncludeSpelling)) {
2033
3
    Filename = IncludeSpelling;
2034
3
  }
2035
286
  return path::convert_to_slash(Filename);
2036
286
}