Coverage Report

Created: 2020-09-15 12:33

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