Coverage Report

Created: 2021-06-15 06:44

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