Coverage Report

Created: 2020-02-25 14:32

/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.87M
HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
62
1.87M
  if (ControllingMacro) {
63
1.06M
    if (ControllingMacro->isOutOfDate()) {
64
730
      assert(External && "We must have an external source if we have a "
65
730
                         "controlling macro that is out of date.");
66
730
      External->updateOutOfDateIdentifier(
67
730
          *const_cast<IdentifierInfo *>(ControllingMacro));
68
730
    }
69
1.06M
    return ControllingMacro;
70
1.06M
  }
71
806k
72
806k
  if (!ControllingMacroID || 
!External199
)
73
806k
    return nullptr;
74
199
75
199
  ControllingMacro = External->GetIdentifier(ControllingMacroID);
76
199
  return ControllingMacro;
77
199
}
78
79
7.80k
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
59.4k
      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
152
  for (unsigned i = 0, e = FileInfo.size(); i != e; 
++i149
) {
94
149
    NumOnceOnlyFiles += FileInfo[i].isImport;
95
149
    if (MaxNumIncludes < FileInfo[i].NumIncludes)
96
3
      MaxNumIncludes = FileInfo[i].NumIncludes;
97
149
    NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
98
149
  }
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
33
  // We expect the number of headermaps to be small, and almost always empty.
115
33
  // 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
11
      // Pointer equality comparison of FileEntries works because they are
119
11
      // 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
52
    SmallVectorImpl<std::string> &Names) const {
135
52
  for (auto &HM : HeaderMaps)
136
1
    Names.push_back(std::string(HM.first->getName()));
137
52
}
138
139
4.71k
std::string HeaderSearch::getCachedModuleFileName(Module *Module) {
140
4.71k
  const FileEntry *ModuleMap =
141
4.71k
      getModuleMap().getModuleMapFileForUniquing(Module);
142
4.71k
  return getCachedModuleFileName(Module->Name, ModuleMap->getName());
143
4.71k
}
144
145
std::string HeaderSearch::getPrebuiltModuleFileName(StringRef ModuleName,
146
1.73k
                                                    bool FileMapOnly) {
147
1.73k
  // 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
7
  // Then go through each prebuilt module directory and try to find the pcm
156
7
  // 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
7
  
return {}0
;
165
7
}
166
167
std::string HeaderSearch::getCachedModuleFileName(StringRef ModuleName,
168
4.75k
                                                  StringRef ModuleMapPath) {
169
4.75k
  // If we don't have a module cache path or aren't supposed to use one, we
170
4.75k
  // can't do anything.
171
4.75k
  if (getModuleCachePath().empty())
172
12
    return {};
173
4.74k
174
4.74k
  SmallString<256> Result(getModuleCachePath());
175
4.74k
  llvm::sys::fs::make_absolute(Result);
176
4.74k
177
4.74k
  if (HSOpts->DisableModuleHash) {
178
161
    llvm::sys::path::append(Result, ModuleName + ".pcm");
179
4.58k
  } else {
180
4.58k
    // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
181
4.58k
    // ideally be globally unique to this particular module. Name collisions
182
4.58k
    // in the hash are safe (because any translation unit can only import one
183
4.58k
    // module with each name), but result in a loss of caching.
184
4.58k
    //
185
4.58k
    // To avoid false-negatives, we form as canonical a path as we can, and map
186
4.58k
    // to lower-case in case we're on a case-insensitive file system.
187
4.58k
    std::string Parent =
188
4.58k
        std::string(llvm::sys::path::parent_path(ModuleMapPath));
189
4.58k
    if (Parent.empty())
190
1
      Parent = ".";
191
4.58k
    auto Dir = FileMgr.getDirectory(Parent);
192
4.58k
    if (!Dir)
193
0
      return {};
194
4.58k
    auto DirName = FileMgr.getCanonicalName(*Dir);
195
4.58k
    auto FileName = llvm::sys::path::filename(ModuleMapPath);
196
4.58k
197
4.58k
    llvm::hash_code Hash =
198
4.58k
      llvm::hash_combine(DirName.lower(), FileName.lower());
199
4.58k
200
4.58k
    SmallString<128> HashStr;
201
4.58k
    llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
202
4.58k
    llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
203
4.58k
  }
204
4.74k
  return Result.str().str();
205
4.74k
}
206
207
Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch,
208
2.01M
                                   bool AllowExtraModuleMapSearch) {
209
2.01M
  // Look in the module map to determine if there is a module by this name.
210
2.01M
  Module *Module = ModMap.findModule(ModuleName);
211
2.01M
  if (Module || 
!AllowSearch8.45k
||
!HSOpts->ImplicitModuleMaps8.45k
)
212
2.01M
    return Module;
213
8.05k
214
8.05k
  StringRef SearchName = ModuleName;
215
8.05k
  Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
216
8.05k
217
8.05k
  // The facility for "private modules" -- adjacent, optional module maps named
218
8.05k
  // module.private.modulemap that are supposed to define private submodules --
219
8.05k
  // may have different flavors of names: FooPrivate, Foo_Private and Foo.Private.
220
8.05k
  //
221
8.05k
  // Foo.Private is now deprecated in favor of Foo_Private. Users of FooPrivate
222
8.05k
  // should also rename to Foo_Private. Representing private as submodules
223
8.05k
  // could force building unwanted dependencies into the parent module and cause
224
8.05k
  // dependency cycles.
225
8.05k
  if (!Module && 
SearchName.consume_back("_Private")1.04k
)
226
4
    Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
227
8.05k
  if (!Module && 
SearchName.consume_back("Private")1.04k
)
228
4
    Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
229
8.05k
  return Module;
230
8.05k
}
231
232
Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
233
8.06k
                                   bool AllowExtraModuleMapSearch) {
234
8.06k
  Module *Module = nullptr;
235
8.06k
236
8.06k
  // Look through the various header search paths to load any available module
237
8.06k
  // maps, searching for a module map that describes this module.
238
35.7k
  for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; 
++Idx27.6k
) {
239
34.6k
    if (SearchDirs[Idx].isFramework()) {
240
5.56k
      // Search for or infer a module map for a framework. Here we use
241
5.56k
      // SearchName rather than ModuleName, to permit finding private modules
242
5.56k
      // named FooPrivate in buggy frameworks named Foo.
243
5.56k
      SmallString<128> FrameworkDirName;
244
5.56k
      FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
245
5.56k
      llvm::sys::path::append(FrameworkDirName, SearchName + ".framework");
246
5.56k
      if (auto FrameworkDir = FileMgr.getDirectory(FrameworkDirName)) {
247
4.50k
        bool IsSystem
248
4.50k
          = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
249
4.50k
        Module = loadFrameworkModule(ModuleName, *FrameworkDir, IsSystem);
250
4.50k
        if (Module)
251
4.49k
          break;
252
30.1k
      }
253
5.56k
    }
254
30.1k
255
30.1k
    // FIXME: Figure out how header maps and module maps will work together.
256
30.1k
257
30.1k
    // Only deal with normal search directories.
258
30.1k
    if (!SearchDirs[Idx].isNormalDir())
259
1.08k
      continue;
260
29.0k
261
29.0k
    bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
262
29.0k
    // Search for a module map file in this directory.
263
29.0k
    if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
264
29.0k
                          /*IsFramework*/false) == LMM_NewlyLoaded) {
265
4.30k
      // We just loaded a module map file; check whether the module is
266
4.30k
      // available now.
267
4.30k
      Module = ModMap.findModule(ModuleName);
268
4.30k
      if (Module)
269
2.40k
        break;
270
26.6k
    }
271
26.6k
272
26.6k
    // Search for a module map in a subdirectory with the same name as the
273
26.6k
    // module.
274
26.6k
    SmallString<128> NestedModuleMapDirName;
275
26.6k
    NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
276
26.6k
    llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
277
26.6k
    if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
278
26.6k
                          /*IsFramework*/false) == LMM_NewlyLoaded){
279
14
      // 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.6k
    }
284
26.6k
285
26.6k
    // If we've already performed the exhaustive search for module maps in this
286
26.6k
    // search directory, don't do it again.
287
26.6k
    if (SearchDirs[Idx].haveSearchedAllModuleMaps())
288
17.7k
      continue;
289
8.88k
290
8.88k
    // Load all module maps in the immediate subdirectories of this search
291
8.88k
    // directory if ModuleName was from @import.
292
8.88k
    if (AllowExtraModuleMapSearch)
293
6.67k
      loadSubdirectoryModuleMaps(SearchDirs[Idx]);
294
8.88k
295
8.88k
    // Look again for the module.
296
8.88k
    Module = ModMap.findModule(ModuleName);
297
8.88k
    if (Module)
298
107
      break;
299
8.88k
  }
300
8.06k
301
8.06k
  return Module;
302
8.06k
}
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
201
StringRef DirectoryLookup::getName() const {
311
201
  // FIXME: Use the name from \c DirectoryEntryRef.
312
201
  if (isNormalDir())
313
140
    return getDir()->getName();
314
61
  if (isFramework())
315
61
    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.60M
    ModuleMap::KnownHeader *SuggestedModule) {
324
4.60M
  // If we have a module map that might map this header, load it and
325
4.60M
  // check whether we'll have a suggestion for a module.
326
4.60M
  auto File = getFileMgr().getFileRef(FileName, /*OpenFile=*/true);
327
4.60M
  if (!File) {
328
3.28M
    // For rare, surprising errors (e.g. "out of file handles"), diag the EC
329
3.28M
    // message.
330
3.28M
    std::error_code EC = llvm::errorToErrorCode(File.takeError());
331
3.28M
    if (EC != llvm::errc::no_such_file_or_directory &&
332
3.28M
        
EC != llvm::errc::invalid_argument11
&&
333
3.28M
        
EC != llvm::errc::is_a_directory11
&&
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.28M
    return None;
338
3.28M
  }
339
1.31M
340
1.31M
  // If there is a module that corresponds to this header, suggest it.
341
1.31M
  if (!findUsableModuleForHeader(
342
1.31M
          &File->getFileEntry(), Dir ? 
Dir1.31M
:
File->getFileEntry().getDir()7.82k
,
343
1.31M
          RequestingModule, SuggestedModule, IsSystemHeaderDir))
344
260
    return None;
345
1.31M
346
1.31M
  return *File;
347
1.31M
}
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.38M
    bool &IsInHeaderMap, SmallVectorImpl<char> &MappedName) const {
357
5.38M
  InUserSpecifiedSystemFramework = false;
358
5.38M
  IsInHeaderMap = false;
359
5.38M
  MappedName.clear();
360
5.38M
361
5.38M
  SmallString<1024> TmpDir;
362
5.38M
  if (isNormalDir()) {
363
4.51M
    // Concatenate the requested file onto the directory.
364
4.51M
    TmpDir = getDir()->getName();
365
4.51M
    llvm::sys::path::append(TmpDir, Filename);
366
4.51M
    if (SearchPath) {
367
4.44M
      StringRef SearchPathRef(getDir()->getName());
368
4.44M
      SearchPath->clear();
369
4.44M
      SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
370
4.44M
    }
371
4.51M
    if (RelativePath) {
372
4.44M
      RelativePath->clear();
373
4.44M
      RelativePath->append(Filename.begin(), Filename.end());
374
4.44M
    }
375
4.51M
376
4.51M
    return HS.getFileAndSuggestModule(TmpDir, IncludeLoc, getDir(),
377
4.51M
                                      isSystemHeaderDirectory(),
378
4.51M
                                      RequestingModule, SuggestedModule);
379
4.51M
  }
380
869k
381
869k
  if (isFramework())
382
869k
    return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
383
869k
                             RequestingModule, SuggestedModule,
384
869k
                             InUserSpecifiedSystemFramework, IsFrameworkFound);
385
39
386
39
  assert(isHeaderMap() && "Unknown directory lookup");
387
39
  const HeaderMap *HM = getHeaderMap();
388
39
  SmallString<1024> Path;
389
39
  StringRef Dest = HM->lookupFilename(Filename, Path);
390
39
  if (Dest.empty())
391
22
    return None;
392
17
393
17
  IsInHeaderMap = true;
394
17
395
17
  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
17
407
17
  // Check if the headermap maps the filename to a framework include
408
17
  // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
409
17
  // framework include.
410
17
  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
0
  } else if (auto Res = HS.getFileMgr().getOptionalFileRef(Dest)) {
419
0
    FixupSearchPath();
420
0
    return *Res;
421
0
  }
422
17
423
17
  return None;
424
17
}
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
865k
                   SmallVectorImpl<std::string> &SubmodulePath) {
435
865k
  assert(llvm::sys::path::extension(DirName) == ".framework" &&
436
865k
         "Not a framework directory");
437
865k
438
865k
  // Note: as an egregious but useful hack we use the real path here, because
439
865k
  // frameworks moving between top-level frameworks to embedded frameworks tend
440
865k
  // to be symlinked, and we base the logical structure of modules on the
441
865k
  // physical layout. In particular, we need to deal with crazy includes like
442
865k
  //
443
865k
  //   #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
444
865k
  //
445
865k
  // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
446
865k
  // which one should access with, e.g.,
447
865k
  //
448
865k
  //   #include <Bar/Wibble.h>
449
865k
  //
450
865k
  // Similar issues occur when a top-level framework has moved into an
451
865k
  // embedded framework.
452
865k
  const DirectoryEntry *TopFrameworkDir = nullptr;
453
865k
  if (auto TopFrameworkDirOrErr = FileMgr.getDirectory(DirName))
454
865k
    TopFrameworkDir = *TopFrameworkDirOrErr;
455
865k
456
865k
  if (TopFrameworkDir)
457
865k
    DirName = FileMgr.getCanonicalName(TopFrameworkDir);
458
12.4M
  do {
459
12.4M
    // Get the parent directory name.
460
12.4M
    DirName = llvm::sys::path::parent_path(DirName);
461
12.4M
    if (DirName.empty())
462
865k
      break;
463
11.6M
464
11.6M
    // Determine whether this directory exists.
465
11.6M
    auto Dir = FileMgr.getDirectory(DirName);
466
11.6M
    if (!Dir)
467
0
      break;
468
11.6M
469
11.6M
    // If this is a framework directory, then we're a subframework of this
470
11.6M
    // framework.
471
11.6M
    if (llvm::sys::path::extension(DirName) == ".framework") {
472
88.0k
      SubmodulePath.push_back(std::string(llvm::sys::path::stem(DirName)));
473
88.0k
      TopFrameworkDir = *Dir;
474
88.0k
    }
475
11.6M
  } while (true);
476
865k
477
865k
  return TopFrameworkDir;
478
865k
}
479
480
static bool needModuleLookup(Module *RequestingModule,
481
2.96M
                             bool HasSuggestedModule) {
482
2.96M
  return HasSuggestedModule ||
483
2.96M
         
(9.63k
RequestingModule9.63k
&&
RequestingModule->NoUndeclaredIncludes952
);
484
2.96M
}
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
869k
    bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound) const {
493
869k
  FileManager &FileMgr = HS.getFileMgr();
494
869k
495
869k
  // Framework names must have a '/' in the filename.
496
869k
  size_t SlashPos = Filename.find('/');
497
869k
  if (SlashPos == StringRef::npos)
498
8.27k
    return None;
499
861k
500
861k
  // Find out if this is the home for the specified framework, by checking
501
861k
  // HeaderSearch.  Possible answers are yes/no and unknown.
502
861k
  FrameworkCacheEntry &CacheEntry =
503
861k
    HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
504
861k
505
861k
  // If it is known and in some other directory, fail.
506
861k
  if (CacheEntry.Directory && 
CacheEntry.Directory != getFrameworkDir()844k
)
507
74.3k
    return None;
508
787k
509
787k
  // Otherwise, construct the path to this framework dir.
510
787k
511
787k
  // FrameworkName = "/System/Library/Frameworks/"
512
787k
  SmallString<1024> FrameworkName;
513
787k
  FrameworkName += getFrameworkDirRef()->getName();
514
787k
  if (FrameworkName.empty() || FrameworkName.back() != '/')
515
787k
    FrameworkName.push_back('/');
516
787k
517
787k
  // FrameworkName = "/System/Library/Frameworks/Cocoa"
518
787k
  StringRef ModuleName(Filename.begin(), SlashPos);
519
787k
  FrameworkName += ModuleName;
520
787k
521
787k
  // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
522
787k
  FrameworkName += ".framework/";
523
787k
524
787k
  // If the cache entry was unresolved, populate it now.
525
787k
  if (!CacheEntry.Directory) {
526
17.0k
    ++NumFrameworkLookups;
527
17.0k
528
17.0k
    // If the framework dir doesn't exist, we fail.
529
17.0k
    auto Dir = FileMgr.getDirectory(FrameworkName);
530
17.0k
    if (!Dir)
531
9.26k
      return None;
532
7.80k
533
7.80k
    // Otherwise, if it does, remember that this is the right direntry for this
534
7.80k
    // framework.
535
7.80k
    CacheEntry.Directory = getFrameworkDir();
536
7.80k
537
7.80k
    // If this is a user search directory, check if the framework has been
538
7.80k
    // user-specified as a system framework.
539
7.80k
    if (getDirCharacteristic() == SrcMgr::C_User) {
540
235
      SmallString<1024> SystemFrameworkMarker(FrameworkName);
541
235
      SystemFrameworkMarker += ".system_framework";
542
235
      if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
543
5
        CacheEntry.IsUserSpecifiedSystemFramework = true;
544
5
      }
545
235
    }
546
7.80k
  }
547
787k
548
787k
  // Set out flags.
549
787k
  InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
550
777k
  IsFrameworkFound = CacheEntry.Directory;
551
777k
552
777k
  if (RelativePath) {
553
777k
    RelativePath->clear();
554
777k
    RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
555
777k
  }
556
777k
557
777k
  // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
558
777k
  unsigned OrigSize = FrameworkName.size();
559
777k
560
777k
  FrameworkName += "Headers/";
561
777k
562
777k
  if (SearchPath) {
563
777k
    SearchPath->clear();
564
777k
    // Without trailing '/'.
565
777k
    SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
566
777k
  }
567
777k
568
777k
  FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
569
777k
570
777k
  auto File =
571
777k
      FileMgr.getOptionalFileRef(FrameworkName, /*OpenFile=*/!SuggestedModule);
572
777k
  if (!File) {
573
34
    // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
574
34
    const char *Private = "Private";
575
34
    FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
576
34
                         Private+strlen(Private));
577
34
    if (SearchPath)
578
33
      SearchPath->insert(SearchPath->begin()+OrigSize, Private,
579
33
                         Private+strlen(Private));
580
34
581
34
    File = FileMgr.getOptionalFileRef(FrameworkName,
582
34
                                      /*OpenFile=*/!SuggestedModule);
583
34
  }
584
777k
585
777k
  // If we found the header and are allowed to suggest a module, do so now.
586
777k
  if (File && 
needModuleLookup(RequestingModule, SuggestedModule)777k
) {
587
777k
    // Find the framework in which this header occurs.
588
777k
    StringRef FrameworkPath = File->getFileEntry().getDir()->getName();
589
777k
    bool FoundFramework = false;
590
1.55M
    do {
591
1.55M
      // Determine whether this directory exists.
592
1.55M
      auto Dir = FileMgr.getDirectory(FrameworkPath);
593
1.55M
      if (!Dir)
594
0
        break;
595
1.55M
596
1.55M
      // If this is a framework directory, then we're a subframework of this
597
1.55M
      // framework.
598
1.55M
      if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
599
777k
        FoundFramework = true;
600
777k
        break;
601
777k
      }
602
780k
603
780k
      // Get the parent directory name.
604
780k
      FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
605
780k
      if (FrameworkPath.empty())
606
0
        break;
607
780k
    } while (true);
608
777k
609
777k
    bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
610
780k
    if (FoundFramework) {
611
777k
      if (!HS.findUsableModuleForFrameworkHeader(
612
777k
              &File->getFileEntry(), FrameworkPath, RequestingModule,
613
777k
              SuggestedModule, IsSystem))
614
0
        return None;
615
2.36k
    } else {
616
2.36k
      if (!HS.findUsableModuleForHeader(&File->getFileEntry(), getDir(),
617
2.36k
                                        RequestingModule, SuggestedModule,
618
2.36k
                                        IsSystem))
619
0
        return None;
620
780k
    }
621
777k
  }
622
780k
  if (File)
623
777k
    return *File;
624
2.36k
  return None;
625
2.36k
}
626
627
59.4k
void HeaderSearch::setTarget(const TargetInfo &Target) {
628
59.4k
  ModMap.setTarget(Target);
629
59.4k
}
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.13M
                                  SourceLocation IncludeLoc) {
641
2.13M
  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.13M
  return false;
646
2.13M
}
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.01M
                                 SmallVectorImpl<char> &FrameworkName) {
658
3.01M
  using namespace llvm::sys;
659
3.01M
  path::const_iterator I = path::begin(Path);
660
3.01M
  path::const_iterator E = path::end(Path);
661
3.01M
  IsPrivateHeader = false;
662
3.01M
663
3.01M
  // Detect different types of framework style paths:
664
3.01M
  //
665
3.01M
  //   ...Foo.framework/{Headers,PrivateHeaders}
666
3.01M
  //   ...Foo.framework/Versions/{A,Current}/{Headers,PrivateHeaders}
667
3.01M
  //   ...Foo.framework/Frameworks/Nested.framework/{Headers,PrivateHeaders}
668
3.01M
  //   ...<other variations with 'Versions' like in the above path>
669
3.01M
  //
670
3.01M
  // and some other variations among these lines.
671
3.01M
  int FoundComp = 0;
672
46.3M
  while (I != E) {
673
43.3M
    if (*I == "Headers")
674
1.75M
      ++FoundComp;
675
43.3M
    if (I->endswith(".framework")) {
676
1.87M
      FrameworkName.append(I->begin(), I->end());
677
1.87M
      ++FoundComp;
678
1.87M
    }
679
43.3M
    if (*I == "PrivateHeaders") {
680
22
      ++FoundComp;
681
22
      IsPrivateHeader = true;
682
22
    }
683
43.3M
    ++I;
684
43.3M
  }
685
3.01M
686
3.01M
  return !FrameworkName.empty() && 
FoundComp >= 21.75M
;
687
3.01M
}
688
689
static void
690
diagnoseFrameworkInclude(DiagnosticsEngine &Diags, SourceLocation IncludeLoc,
691
                         StringRef Includer, StringRef IncludeFilename,
692
                         const FileEntry *IncludeFE, bool isAngled = false,
693
2.04M
                         bool FoundByHeaderMap = false) {
694
2.04M
  bool IsIncluderPrivateHeader = false;
695
2.04M
  SmallString<128> FromFramework, ToFramework;
696
2.04M
  if (!isFrameworkStylePath(Includer, IsIncluderPrivateHeader, FromFramework))
697
1.07M
    return;
698
971k
  bool IsIncludeePrivateHeader = false;
699
971k
  bool IsIncludeeInFramework = isFrameworkStylePath(
700
971k
      IncludeFE->getName(), IsIncludeePrivateHeader, ToFramework);
701
971k
702
971k
  if (!isAngled && 
!FoundByHeaderMap2.91k
) {
703
2.90k
    SmallString<128> NewInclude("<");
704
2.90k
    if (IsIncludeeInFramework) {
705
2.88k
      NewInclude += StringRef(ToFramework).drop_back(10); // drop .framework
706
2.88k
      NewInclude += "/";
707
2.88k
    }
708
2.90k
    NewInclude += IncludeFilename;
709
2.90k
    NewInclude += ">";
710
2.90k
    Diags.Report(IncludeLoc, diag::warn_quoted_include_in_framework_header)
711
2.90k
        << IncludeFilename
712
2.90k
        << FixItHint::CreateReplacement(IncludeLoc, NewInclude);
713
2.90k
  }
714
971k
715
971k
  // Headers in Foo.framework/Headers should not include headers
716
971k
  // from Foo.framework/PrivateHeaders, since this violates public/private
717
971k
  // API boundaries and can cause modular dependency cycles.
718
971k
  if (!IsIncluderPrivateHeader && 
IsIncludeeInFramework971k
&&
719
971k
      
IsIncludeePrivateHeader779k
&&
FromFramework == ToFramework8
)
720
8
    Diags.Report(IncludeLoc, diag::warn_framework_include_private_from_public)
721
8
        << IncludeFilename;
722
971k
}
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.19M
    bool BuildSystemModule) {
737
2.19M
  if (IsMapped)
738
2.17M
    *IsMapped = false;
739
2.19M
740
2.19M
  if (IsFrameworkFound)
741
2.17M
    *IsFrameworkFound = false;
742
2.19M
743
2.19M
  if (SuggestedModule)
744
2.17M
    *SuggestedModule = ModuleMap::KnownHeader();
745
2.19M
746
2.19M
  // If 'Filename' is absolute, check to see if it exists and no searching.
747
2.19M
  if (llvm::sys::path::is_absolute(Filename)) {
748
7.83k
    CurDir = nullptr;
749
7.83k
750
7.83k
    // If this was an #include_next "/absolute/file", fail.
751
7.83k
    if (FromDir)
752
0
      return None;
753
7.83k
754
7.83k
    if (SearchPath)
755
7.81k
      SearchPath->clear();
756
7.83k
    if (RelativePath) {
757
7.81k
      RelativePath->clear();
758
7.81k
      RelativePath->append(Filename.begin(), Filename.end());
759
7.81k
    }
760
7.83k
    // Otherwise, just return the file.
761
7.83k
    return getFileAndSuggestModule(Filename, IncludeLoc, nullptr,
762
7.83k
                                   /*IsSystemHeaderDir*/false,
763
7.83k
                                   RequestingModule, SuggestedModule);
764
7.83k
  }
765
2.18M
766
2.18M
  // This is the header that MSVC's header search would have found.
767
2.18M
  ModuleMap::KnownHeader MSSuggestedModule;
768
2.18M
  const FileEntry *MSFE_FE = nullptr;
769
2.18M
  StringRef MSFE_Name;
770
2.18M
771
2.18M
  // Unless disabled, check to see if the file is in the #includer's
772
2.18M
  // directory.  This cannot be based on CurDir, because each includer could be
773
2.18M
  // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
774
2.18M
  // include of "baz.h" should resolve to "whatever/foo/baz.h".
775
2.18M
  // This search is not done for <> headers.
776
2.18M
  if (!Includers.empty() && 
!isAngled2.16M
&&
!NoCurDirSearch83.3k
) {
777
83.3k
    SmallString<1024> TmpDir;
778
83.3k
    bool First = true;
779
83.3k
    for (const auto &IncluderAndDir : Includers) {
780
83.3k
      const FileEntry *Includer = IncluderAndDir.first;
781
83.3k
782
83.3k
      // Concatenate the requested file onto the directory.
783
83.3k
      // FIXME: Portability.  Filename concatenation should be in sys::Path.
784
83.3k
      TmpDir = IncluderAndDir.second->getName();
785
83.3k
      TmpDir.push_back('/');
786
83.3k
      TmpDir.append(Filename.begin(), Filename.end());
787
83.3k
788
83.3k
      // FIXME: We don't cache the result of getFileInfo across the call to
789
83.3k
      // getFileAndSuggestModule, because it's a reference to an element of
790
83.3k
      // a container that could be reallocated across this call.
791
83.3k
      //
792
83.3k
      // If we have no includer, that means we're processing a #include
793
83.3k
      // from a module build. We should treat this as a system header if we're
794
83.3k
      // building a [system] module.
795
83.3k
      bool IncluderIsSystemHeader =
796
83.3k
          Includer ? 
getFileInfo(Includer).DirInfo != SrcMgr::C_User64.2k
:
797
83.3k
          
BuildSystemModule19.1k
;
798
83.3k
      if (Optional<FileEntryRef> FE = getFileAndSuggestModule(
799
55.8k
              TmpDir, IncludeLoc, IncluderAndDir.second, IncluderIsSystemHeader,
800
55.8k
              RequestingModule, SuggestedModule)) {
801
55.8k
        if (!Includer) {
802
19.1k
          assert(First && "only first includer can have no file");
803
19.1k
          return FE;
804
19.1k
        }
805
36.7k
806
36.7k
        // Leave CurDir unset.
807
36.7k
        // This file is a system header or C++ unfriendly if the old file is.
808
36.7k
        //
809
36.7k
        // Note that we only use one of FromHFI/ToHFI at once, due to potential
810
36.7k
        // reallocation of the underlying vector potentially making the first
811
36.7k
        // reference binding dangling.
812
36.7k
        HeaderFileInfo &FromHFI = getFileInfo(Includer);
813
36.7k
        unsigned DirInfo = FromHFI.DirInfo;
814
36.7k
        bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
815
36.7k
        StringRef Framework = FromHFI.Framework;
816
36.7k
817
36.7k
        HeaderFileInfo &ToHFI = getFileInfo(&FE->getFileEntry());
818
36.7k
        ToHFI.DirInfo = DirInfo;
819
36.7k
        ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
820
36.7k
        ToHFI.Framework = Framework;
821
36.7k
822
36.7k
        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.7k
        if (RelativePath) {
828
36.4k
          RelativePath->clear();
829
36.4k
          RelativePath->append(Filename.begin(), Filename.end());
830
36.4k
        }
831
36.7k
        if (First) {
832
36.7k
          diagnoseFrameworkInclude(Diags, IncludeLoc,
833
36.7k
                                   IncluderAndDir.second->getName(), Filename,
834
36.7k
                                   &FE->getFileEntry());
835
36.7k
          return FE;
836
36.7k
        }
837
6
838
6
        // Otherwise, we found the path via MSVC header search rules.  If
839
6
        // -Wmsvc-include is enabled, we have to keep searching to see if we
840
6
        // would've found this header in -I or -isystem directories.
841
6
        if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
842
4
          return FE;
843
4
        } else {
844
2
          MSFE_FE = &FE->getFileEntry();
845
2
          MSFE_Name = FE->getName();
846
2
          if (SuggestedModule) {
847
2
            MSSuggestedModule = *SuggestedModule;
848
2
            *SuggestedModule = ModuleMap::KnownHeader();
849
2
          }
850
2
          break;
851
2
        }
852
27.4k
      }
853
27.4k
      First = false;
854
27.4k
    }
855
83.3k
  }
856
2.18M
857
2.18M
  
Optional<FileEntryRef> MSFE(MSFE_FE 2.13M
?
FileEntryRef(MSFE_Name, *MSFE_FE)2
858
2.13M
                                      : 
Optional<FileEntryRef>()2.13M
);
859
2.13M
860
2.13M
  CurDir = nullptr;
861
2.13M
862
2.13M
  // If this is a system #include, ignore the user #include locs.
863
2.13M
  unsigned i = isAngled ? 
AngledDirIdx2.10M
:
027.4k
;
864
2.13M
865
2.13M
  // If this is a #include_next request, start searching after the directory the
866
2.13M
  // file was found in.
867
2.13M
  if (FromDir)
868
23.3k
    i = FromDir-&SearchDirs[0];
869
2.13M
870
2.13M
  // Cache all of the lookups performed by this method.  Many headers are
871
2.13M
  // multiply included, and the "pragma once" optimization prevents them from
872
2.13M
  // being relex/pp'd, but they would still have to search through a
873
2.13M
  // (potentially huge) series of SearchDirs to find it.
874
2.13M
  LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
875
2.13M
876
2.13M
  // If the entry has been previously looked up, the first value will be
877
2.13M
  // non-zero.  If the value is equal to i (the start point of our search), then
878
2.13M
  // this is a matching hit.
879
2.13M
  if (!SkipCache && CacheLookup.StartIdx == i+1) {
880
1.38M
    // Skip querying potentially lots of directories for this lookup.
881
1.38M
    i = CacheLookup.HitIdx;
882
1.38M
    if (CacheLookup.MappedName) {
883
6
      Filename = CacheLookup.MappedName;
884
6
      if (IsMapped)
885
6
        *IsMapped = true;
886
6
    }
887
1.38M
  } else {
888
745k
    // Otherwise, this is the first query, or the previous query didn't match
889
745k
    // our search start.  We will fill in our found location below, so prime the
890
745k
    // start point value.
891
745k
    CacheLookup.reset(/*StartIdx=*/i+1);
892
745k
  }
893
2.13M
894
2.13M
  SmallString<64> MappedName;
895
2.13M
896
2.13M
  // Check each directory in sequence to see if it contains this file.
897
5.47M
  for (; i != SearchDirs.size(); 
++i3.34M
) {
898
5.38M
    bool InUserSpecifiedSystemFramework = false;
899
5.38M
    bool IsInHeaderMap = false;
900
5.38M
    bool IsFrameworkFoundInDir = false;
901
5.38M
    Optional<FileEntryRef> File = SearchDirs[i].LookupFile(
902
5.38M
        Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
903
5.38M
        SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
904
5.38M
        IsInHeaderMap, MappedName);
905
5.38M
    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.38M
    if (IsMapped)
911
5.30M
      // A filename is mapped when a header map remapped it to a relative path
912
5.30M
      // used in subsequent header search or to an absolute path pointing to an
913
5.30M
      // existing file.
914
5.30M
      *IsMapped |= (!MappedName.empty() || 
(5.30M
IsInHeaderMap5.30M
&&
File0
));
915
5.38M
    if (IsFrameworkFound)
916
5.30M
      // Because we keep a filename remapped for subsequent search directory
917
5.30M
      // lookups, ignore IsFrameworkFoundInDir after the first remapping and not
918
5.30M
      // just for remapping in a current search directory.
919
5.30M
      *IsFrameworkFound |= (IsFrameworkFoundInDir && 
!CacheLookup.MappedName777k
);
920
5.38M
    if (!File)
921
3.34M
      continue;
922
2.03M
923
2.03M
    CurDir = &SearchDirs[i];
924
2.03M
925
2.03M
    // This file is a system header or C++ unfriendly if the dir is.
926
2.03M
    HeaderFileInfo &HFI = getFileInfo(&File->getFileEntry());
927
2.03M
    HFI.DirInfo = CurDir->getDirCharacteristic();
928
2.03M
929
2.03M
    // If the directory characteristic is User but this framework was
930
2.03M
    // user-specified to be treated as a system framework, promote the
931
2.03M
    // characteristic.
932
2.03M
    if (HFI.DirInfo == SrcMgr::C_User && 
InUserSpecifiedSystemFramework6.69k
)
933
1
      HFI.DirInfo = SrcMgr::C_System;
934
2.03M
935
2.03M
    // If the filename matches a known system header prefix, override
936
2.03M
    // whether the file is a system header.
937
2.03M
    for (unsigned j = SystemHeaderPrefixes.size(); j; 
--j12
) {
938
20
      if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
939
8
        HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? 
SrcMgr::C_System4
940
8
                                                       : 
SrcMgr::C_User4
;
941
8
        break;
942
8
      }
943
20
    }
944
2.03M
945
2.03M
    // If this file is found in a header map and uses the framework style of
946
2.03M
    // includes, then this header is part of a framework we're building.
947
2.03M
    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.03M
956
2.03M
    if (checkMSVCHeaderSearch(Diags, MSFE ? 
&MSFE->getFileEntry()2
:
nullptr2.03M
,
957
2.03M
                              &File->getFileEntry(), IncludeLoc)) {
958
1
      if (SuggestedModule)
959
1
        *SuggestedModule = MSSuggestedModule;
960
1
      return MSFE;
961
1
    }
962
2.03M
963
2.03M
    bool FoundByHeaderMap = !IsMapped ? 
false9.66k
:
*IsMapped2.02M
;
964
2.03M
    if (!Includers.empty())
965
2.01M
      diagnoseFrameworkInclude(
966
2.01M
          Diags, IncludeLoc, Includers.front().second->getName(), Filename,
967
2.01M
          &File->getFileEntry(), isAngled, FoundByHeaderMap);
968
2.03M
969
2.03M
    // Remember this location for the next lookup we do.
970
2.03M
    CacheLookup.HitIdx = i;
971
2.03M
    return File;
972
2.03M
  }
973
2.13M
974
2.13M
  // If we are including a file with a quoted include "foo.h" from inside
975
2.13M
  // a header in a framework that is currently being built, and we couldn't
976
2.13M
  // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
977
2.13M
  // "Foo" is the name of the framework in which the including header was found.
978
2.13M
  
if (97.5k
!Includers.empty()97.5k
&&
Includers.front().first96.9k
&&
!isAngled96.9k
&&
979
97.5k
      
Filename.find('/') == StringRef::npos138
) {
980
92
    HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
981
92
    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
0
      // FIXME: SuggestedModule.
1003
0
      return File;
1004
0
    }
1005
92
  }
1006
97.5k
1007
97.5k
  if (checkMSVCHeaderSearch(Diags, MSFE ? 
&MSFE->getFileEntry()0
: nullptr,
1008
97.5k
                            nullptr, IncludeLoc)) {
1009
0
    if (SuggestedModule)
1010
0
      *SuggestedModule = MSSuggestedModule;
1011
0
    return MSFE;
1012
0
  }
1013
97.5k
1014
97.5k
  // Otherwise, didn't find it. Remember we didn't find this.
1015
97.5k
  CacheLookup.HitIdx = SearchDirs.size();
1016
97.5k
  return None;
1017
97.5k
}
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
140k
    Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
1028
140k
  assert(ContextFileEnt && "No context file?");
1029
140k
1030
140k
  // Framework names must have a '/' in the filename.  Find it.
1031
140k
  // FIXME: Should we permit '\' on Windows?
1032
140k
  size_t SlashPos = Filename.find('/');
1033
140k
  if (SlashPos == StringRef::npos)
1034
52.6k
    return None;
1035
88.0k
1036
88.0k
  // Look up the base framework name of the ContextFileEnt.
1037
88.0k
  StringRef ContextName = ContextFileEnt->getName();
1038
88.0k
1039
88.0k
  // If the context info wasn't a framework, couldn't be a subframework.
1040
88.0k
  const unsigned DotFrameworkLen = 10;
1041
88.0k
  auto FrameworkPos = ContextName.find(".framework");
1042
88.0k
  if (FrameworkPos == StringRef::npos ||
1043
88.0k
      
(88.0k
ContextName[FrameworkPos + DotFrameworkLen] != '/'88.0k
&&
1044
88.0k
       
ContextName[FrameworkPos + DotFrameworkLen] != '\\'0
))
1045
24
    return None;
1046
88.0k
1047
88.0k
  SmallString<1024> FrameworkName(ContextName.data(), ContextName.data() +
1048
88.0k
                                                          FrameworkPos +
1049
88.0k
                                                          DotFrameworkLen + 1);
1050
88.0k
1051
88.0k
  // Append Frameworks/HIToolbox.framework/
1052
88.0k
  FrameworkName += "Frameworks/";
1053
88.0k
  FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
1054
88.0k
  FrameworkName += ".framework/";
1055
88.0k
1056
88.0k
  auto &CacheLookup =
1057
88.0k
      *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
1058
88.0k
                                          FrameworkCacheEntry())).first;
1059
88.0k
1060
88.0k
  // Some other location?
1061
88.0k
  if (CacheLookup.second.Directory &&
1062
88.0k
      
CacheLookup.first().size() == FrameworkName.size()79.0k
&&
1063
88.0k
      memcmp(CacheLookup.first().data(), &FrameworkName[0],
1064
0
             CacheLookup.first().size()) != 0)
1065
0
    return None;
1066
88.0k
1067
88.0k
  // Cache subframework.
1068
88.0k
  if (!CacheLookup.second.Directory) {
1069
9.00k
    ++NumSubFrameworkLookups;
1070
9.00k
1071
9.00k
    // If the framework dir doesn't exist, we fail.
1072
9.00k
    auto Dir = FileMgr.getDirectory(FrameworkName);
1073
9.00k
    if (!Dir)
1074
0
      return None;
1075
9.00k
1076
9.00k
    // Otherwise, if it does, remember that this is the right direntry for this
1077
9.00k
    // framework.
1078
9.00k
    CacheLookup.second.Directory = *Dir;
1079
9.00k
  }
1080
88.0k
1081
88.0k
1082
88.0k
  if (RelativePath) {
1083
88.0k
    RelativePath->clear();
1084
88.0k
    RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
1085
88.0k
  }
1086
88.0k
1087
88.0k
  // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
1088
88.0k
  SmallString<1024> HeadersFilename(FrameworkName);
1089
88.0k
  HeadersFilename += "Headers/";
1090
88.0k
  if (SearchPath) {
1091
88.0k
    SearchPath->clear();
1092
88.0k
    // Without trailing '/'.
1093
88.0k
    SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1094
88.0k
  }
1095
88.0k
1096
88.0k
  HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1097
88.0k
  auto File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
1098
88.0k
  if (!File) {
1099
2
    // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
1100
2
    HeadersFilename = FrameworkName;
1101
2
    HeadersFilename += "PrivateHeaders/";
1102
2
    if (SearchPath) {
1103
2
      SearchPath->clear();
1104
2
      // Without trailing '/'.
1105
2
      SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1106
2
    }
1107
2
1108
2
    HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1109
2
    File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
1110
2
1111
2
    if (!File)
1112
0
      return None;
1113
88.0k
  }
1114
88.0k
1115
88.0k
  // This file is a system header or C++ unfriendly if the old file is.
1116
88.0k
  //
1117
88.0k
  // Note that the temporary 'DirInfo' is required here, as either call to
1118
88.0k
  // getFileInfo could resize the vector and we don't want to rely on order
1119
88.0k
  // of evaluation.
1120
88.0k
  unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
1121
88.0k
  getFileInfo(&File->getFileEntry()).DirInfo = DirInfo;
1122
88.0k
1123
88.0k
  FrameworkName.pop_back(); // remove the trailing '/'
1124
88.0k
  if (!findUsableModuleForFrameworkHeader(&File->getFileEntry(), FrameworkName,
1125
88.0k
                                          RequestingModule, SuggestedModule,
1126
88.0k
                                          /*IsSystem*/ false))
1127
0
    return None;
1128
88.0k
1129
88.0k
  return *File;
1130
88.0k
}
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
68.2k
                                const HeaderFileInfo &OtherHFI) {
1140
68.2k
  assert(OtherHFI.External && "expected to merge external HFI");
1141
68.2k
1142
68.2k
  HFI.isImport |= OtherHFI.isImport;
1143
68.2k
  HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
1144
68.2k
  HFI.isModuleHeader |= OtherHFI.isModuleHeader;
1145
68.2k
  HFI.NumIncludes += OtherHFI.NumIncludes;
1146
68.2k
1147
68.2k
  if (!HFI.ControllingMacro && 
!HFI.ControllingMacroID68.2k
) {
1148
68.2k
    HFI.ControllingMacro = OtherHFI.ControllingMacro;
1149
68.2k
    HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
1150
68.2k
  }
1151
68.2k
1152
68.2k
  HFI.DirInfo = OtherHFI.DirInfo;
1153
68.2k
  HFI.External = (!HFI.IsValid || 
HFI.External64.2k
);
1154
68.2k
  HFI.IsValid = true;
1155
68.2k
  HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
1156
68.2k
1157
68.2k
  if (HFI.Framework.empty())
1158
68.2k
    HFI.Framework = OtherHFI.Framework;
1159
68.2k
}
1160
1161
/// getFileInfo - Return the HeaderFileInfo structure for the specified
1162
/// FileEntry.
1163
8.52M
HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
1164
8.52M
  if (FE->getUID() >= FileInfo.size())
1165
1.72M
    FileInfo.resize(FE->getUID() + 1);
1166
8.52M
1167
8.52M
  HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
1168
8.52M
  // FIXME: Use a generation count to check whether this is really up to date.
1169
8.52M
  if (ExternalSource && 
!HFI->Resolved390k
) {
1170
7.25k
    HFI->Resolved = true;
1171
7.25k
    auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1172
7.25k
1173
7.25k
    HFI = &FileInfo[FE->getUID()];
1174
7.25k
    if (ExternalHFI.External)
1175
4.33k
      mergeHeaderFileInfo(*HFI, ExternalHFI);
1176
7.25k
  }
1177
8.52M
1178
8.52M
  HFI->IsValid = true;
1179
8.52M
  // We have local information about this header file, so it's no longer
1180
8.52M
  // strictly external.
1181
8.52M
  HFI->External = false;
1182
8.52M
  return *HFI;
1183
8.52M
}
1184
1185
const HeaderFileInfo *
1186
HeaderSearch::getExistingFileInfo(const FileEntry *FE,
1187
1.55M
                                  bool WantExternal) const {
1188
1.55M
  // If we have an external source, ensure we have the latest information.
1189
1.55M
  // FIXME: Use a generation count to check whether this is really up to date.
1190
1.55M
  HeaderFileInfo *HFI;
1191
1.55M
  if (ExternalSource) {
1192
469k
    if (FE->getUID() >= FileInfo.size()) {
1193
90.1k
      if (!WantExternal)
1194
0
        return nullptr;
1195
90.1k
      FileInfo.resize(FE->getUID() + 1);
1196
90.1k
    }
1197
469k
1198
469k
    HFI = &FileInfo[FE->getUID()];
1199
469k
    if (!WantExternal && 
(269k
!HFI->IsValid269k
||
HFI->External156k
))
1200
113k
      return nullptr;
1201
356k
    if (!HFI->Resolved) {
1202
232k
      HFI->Resolved = true;
1203
232k
      auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1204
232k
1205
232k
      HFI = &FileInfo[FE->getUID()];
1206
232k
      if (ExternalHFI.External)
1207
63.9k
        mergeHeaderFileInfo(*HFI, ExternalHFI);
1208
232k
    }
1209
1.08M
  } else if (FE->getUID() >= FileInfo.size()) {
1210
942k
    return nullptr;
1211
942k
  } else {
1212
143k
    HFI = &FileInfo[FE->getUID()];
1213
143k
  }
1214
1.55M
1215
1.55M
  
if (499k
!HFI->IsValid499k
||
(283k
HFI->External283k
&&
!WantExternal1.79k
))
1216
215k
    return nullptr;
1217
283k
1218
283k
  return HFI;
1219
283k
}
1220
1221
340
bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1222
340
  // Check if we've ever seen this file as a header.
1223
340
  if (auto *HFI = getExistingFileInfo(File))
1224
340
    return HFI->isPragmaOnce || 
HFI->isImport332
||
HFI->ControllingMacro294
||
1225
340
           
HFI->ControllingMacroID277
;
1226
0
  return false;
1227
0
}
1228
1229
void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
1230
                                        ModuleMap::ModuleHeaderRole Role,
1231
1.21M
                                        bool isCompilingModuleHeader) {
1232
1.21M
  bool isModularHeader = !(Role & ModuleMap::TextualHeader);
1233
1.21M
1234
1.21M
  // Don't mark the file info as non-external if there's nothing to change.
1235
1.21M
  if (!isCompilingModuleHeader) {
1236
1.20M
    if (!isModularHeader)
1237
79.0k
      return;
1238
1.12M
    auto *HFI = getExistingFileInfo(FE);
1239
1.12M
    if (HFI && 
HFI->isModuleHeader3.32k
)
1240
3.08k
      return;
1241
1.13M
  }
1242
1.13M
1243
1.13M
  auto &HFI = getFileInfo(FE);
1244
1.13M
  HFI.isModuleHeader |= isModularHeader;
1245
1.13M
  HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
1246
1.13M
}
1247
1248
bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1249
                                          const FileEntry *File, bool isImport,
1250
2.16M
                                          bool ModulesEnabled, Module *M) {
1251
2.16M
  ++NumIncluded; // Count # of attempted #includes.
1252
2.16M
1253
2.16M
  // Get information about this file.
1254
2.16M
  HeaderFileInfo &FileInfo = getFileInfo(File);
1255
2.16M
1256
2.16M
  // FIXME: this is a workaround for the lack of proper modules-aware support
1257
2.16M
  // for #import / #pragma once
1258
2.16M
  auto TryEnterImported = [&]() -> bool {
1259
293k
    if (!ModulesEnabled)
1260
283k
      return false;
1261
10.5k
    // Ensure FileInfo bits are up to date.
1262
10.5k
    ModMap.resolveHeaderDirectives(File);
1263
10.5k
    // Modules with builtins are special; multiple modules use builtins as
1264
10.5k
    // modular headers, example:
1265
10.5k
    //
1266
10.5k
    //    module stddef { header "stddef.h" export * }
1267
10.5k
    //
1268
10.5k
    // After module map parsing, this expands to:
1269
10.5k
    //
1270
10.5k
    //    module stddef {
1271
10.5k
    //      header "/path_to_builtin_dirs/stddef.h"
1272
10.5k
    //      textual "stddef.h"
1273
10.5k
    //    }
1274
10.5k
    //
1275
10.5k
    // It's common that libc++ and system modules will both define such
1276
10.5k
    // submodules. Make sure cached results for a builtin header won't
1277
10.5k
    // prevent other builtin modules to potentially enter the builtin header.
1278
10.5k
    // Note that builtins are header guarded and the decision to actually
1279
10.5k
    // enter them is postponed to the controlling macros logic below.
1280
10.5k
    bool TryEnterHdr = false;
1281
10.5k
    if (FileInfo.isCompilingModuleHeader && 
FileInfo.isModuleHeader5.66k
)
1282
5.66k
      TryEnterHdr = File->getDir() == ModMap.getBuiltinDir() &&
1283
5.66k
                    ModuleMap::isBuiltinHeader(
1284
499
                        llvm::sys::path::filename(File->getName()));
1285
10.5k
1286
10.5k
    // Textual headers can be #imported from different modules. Since ObjC
1287
10.5k
    // headers find in the wild might rely only on #import and do not contain
1288
10.5k
    // controlling macros, be conservative and only try to enter textual headers
1289
10.5k
    // if such macro is present.
1290
10.5k
    if (!FileInfo.isModuleHeader &&
1291
10.5k
        
FileInfo.getControllingMacro(ExternalLookup)4.85k
)
1292
1.66k
      TryEnterHdr = true;
1293
10.5k
    return TryEnterHdr;
1294
10.5k
  };
1295
2.16M
1296
2.16M
  // If this is a #import directive, check that we have not already imported
1297
2.16M
  // this header.
1298
2.16M
  if (isImport) {
1299
293k
    // If this has already been imported, don't import it again.
1300
293k
    FileInfo.isImport = true;
1301
293k
1302
293k
    // Has this already been #import'ed or #include'd?
1303
293k
    if (FileInfo.NumIncludes && 
!TryEnterImported()192k
)
1304
191k
      return false;
1305
1.86M
  } else {
1306
1.86M
    // Otherwise, if this is a #include of a file that was previously #import'd
1307
1.86M
    // or if this is the second #include of a #pragma once file, ignore it.
1308
1.86M
    if (FileInfo.isImport && 
!TryEnterImported()101k
)
1309
100k
      return false;
1310
1.86M
  }
1311
1.86M
1312
1.86M
  // Next, check to see if the file is wrapped with #ifndef guards.  If so, and
1313
1.86M
  // if the macro that guards it is defined, we know the #include has no effect.
1314
1.86M
  if (const IdentifierInfo *ControllingMacro
1315
1.06M
      = FileInfo.getControllingMacro(ExternalLookup)) {
1316
1.06M
    // If the header corresponds to a module, check whether the macro is already
1317
1.06M
    // defined in that module rather than checking in the current set of visible
1318
1.06M
    // modules.
1319
1.06M
    if (M ? 
PP.isMacroDefinedInLocalModule(ControllingMacro, M)53.0k
1320
1.06M
          : 
PP.isMacroDefined(ControllingMacro)1.01M
) {
1321
1.01M
      ++NumMultiIncludeFileOptzn;
1322
1.01M
      return false;
1323
1.01M
    }
1324
857k
  }
1325
857k
1326
857k
  // Increment the number of times this file has been included.
1327
857k
  ++FileInfo.NumIncludes;
1328
857k
1329
857k
  return true;
1330
857k
}
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.31M
                                bool IsSystem) {
1347
1.31M
  if (!HSOpts->ImplicitModuleMaps)
1348
1.21M
    return false;
1349
92.5k
1350
92.5k
  SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
1351
92.5k
1352
92.5k
  StringRef DirName = FileName;
1353
95.1k
  do {
1354
95.1k
    // Get the parent directory name.
1355
95.1k
    DirName = llvm::sys::path::parent_path(DirName);
1356
95.1k
    if (DirName.empty())
1357
20
      return false;
1358
95.1k
1359
95.1k
    // Determine whether this directory exists.
1360
95.1k
    auto Dir = FileMgr.getDirectory(DirName);
1361
95.1k
    if (!Dir)
1362
0
      return false;
1363
95.1k
1364
95.1k
    // Try to load the module map file in this directory.
1365
95.1k
    switch (loadModuleMapFile(*Dir, IsSystem,
1366
95.1k
                              llvm::sys::path::extension((*Dir)->getName()) ==
1367
95.1k
                                  ".framework")) {
1368
90.3k
    case LMM_NewlyLoaded:
1369
90.3k
    case LMM_AlreadyLoaded:
1370
90.3k
      // Success. All of the directories we stepped through inherit this module
1371
90.3k
      // map file.
1372
92.2k
      for (unsigned I = 0, N = FixUpDirectories.size(); I != N; 
++I1.90k
)
1373
1.90k
        DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1374
90.3k
      return true;
1375
90.3k
1376
90.3k
    case LMM_NoDirectory:
1377
4.83k
    case LMM_InvalidModuleMap:
1378
4.83k
      break;
1379
4.83k
    }
1380
4.83k
1381
4.83k
    // If we hit the top of our search, we're done.
1382
4.83k
    if (*Dir == Root)
1383
2.22k
      return false;
1384
2.60k
1385
2.60k
    // Keep track of all of the directories we checked, so we can mark them as
1386
2.60k
    // having module maps if we eventually do find a module map.
1387
2.60k
    FixUpDirectories.push_back(*Dir);
1388
2.60k
  } while (true);
1389
92.5k
}
1390
1391
ModuleMap::KnownHeader
1392
HeaderSearch::findModuleForHeader(const FileEntry *File,
1393
2.17M
                                  bool AllowTextual) const {
1394
2.17M
  if (ExternalSource) {
1395
95.5k
    // Make sure the external source has handled header info about this file,
1396
95.5k
    // which includes whether the file is part of a module.
1397
95.5k
    (void)getExistingFileInfo(File);
1398
95.5k
  }
1399
2.17M
  return ModMap.findModuleForHeader(File, AllowTextual);
1400
2.17M
}
1401
1402
static bool suggestModule(HeaderSearch &HS, const FileEntry *File,
1403
                          Module *RequestingModule,
1404
2.17M
                          ModuleMap::KnownHeader *SuggestedModule) {
1405
2.17M
  ModuleMap::KnownHeader Module =
1406
2.17M
      HS.findModuleForHeader(File, /*AllowTextual*/true);
1407
2.17M
  if (SuggestedModule)
1408
2.17M
    *SuggestedModule = (Module.getRole() & ModuleMap::TextualHeader)
1409
2.17M
                           ? 
ModuleMap::KnownHeader()1.54k
1410
2.17M
                           : 
Module2.17M
;
1411
2.17M
1412
2.17M
  // If this module specifies [no_undeclared_includes], we cannot find any
1413
2.17M
  // file that's in a non-dependency module.
1414
2.17M
  if (RequestingModule && 
Module100k
&&
RequestingModule->NoUndeclaredIncludes97.3k
) {
1415
57.3k
    HS.getModuleMap().resolveUses(RequestingModule, /*Complain*/false);
1416
57.3k
    if (!RequestingModule->directlyUses(Module.getModule())) {
1417
260
      return false;
1418
260
    }
1419
2.17M
  }
1420
2.17M
1421
2.17M
  return true;
1422
2.17M
}
1423
1424
bool HeaderSearch::findUsableModuleForHeader(
1425
    const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
1426
1.31M
    ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1427
1.31M
  if (File && needModuleLookup(RequestingModule, SuggestedModule)) {
1428
1.31M
    // If there is a module that corresponds to this header, suggest it.
1429
1.31M
    hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
1430
1.31M
    return suggestModule(*this, File, RequestingModule, SuggestedModule);
1431
1.31M
  }
1432
8.79k
  return true;
1433
8.79k
}
1434
1435
bool HeaderSearch::findUsableModuleForFrameworkHeader(
1436
    const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
1437
865k
    ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1438
865k
  // If we're supposed to suggest a module, look for one now.
1439
865k
  if (needModuleLookup(RequestingModule, SuggestedModule)) {
1440
865k
    // Find the top-level framework based on this framework.
1441
865k
    SmallVector<std::string, 4> SubmodulePath;
1442
865k
    const DirectoryEntry *TopFrameworkDir
1443
865k
      = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1444
865k
1445
865k
    // Determine the name of the top-level framework.
1446
865k
    StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1447
865k
1448
865k
    // Load this framework module. If that succeeds, find the suggested module
1449
865k
    // for this header, if any.
1450
865k
    loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1451
865k
1452
865k
    // FIXME: This can find a module not part of ModuleName, which is
1453
865k
    // important so that we're consistent about whether this header
1454
865k
    // corresponds to a module. Possibly we should lock down framework modules
1455
865k
    // so that this is not possible.
1456
865k
    return suggestModule(*this, File, RequestingModule, SuggestedModule);
1457
865k
  }
1458
0
  return true;
1459
0
}
1460
1461
static const FileEntry *getPrivateModuleMap(const FileEntry *File,
1462
18.8k
                                            FileManager &FileMgr) {
1463
18.8k
  StringRef Filename = llvm::sys::path::filename(File->getName());
1464
18.8k
  SmallString<128>  PrivateFilename(File->getDir()->getName());
1465
18.8k
  if (Filename == "module.map")
1466
1.91k
    llvm::sys::path::append(PrivateFilename, "module_private.map");
1467
16.9k
  else if (Filename == "module.modulemap")
1468
16.5k
    llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1469
410
  else
1470
410
    return nullptr;
1471
18.4k
  if (auto File = FileMgr.getFile(PrivateFilename))
1472
142
    return *File;
1473
18.3k
  return nullptr;
1474
18.3k
}
1475
1476
bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
1477
                                     FileID ID, unsigned *Offset,
1478
4.08k
                                     StringRef OriginalModuleMapFile) {
1479
4.08k
  // Find the directory for the module. For frameworks, that may require going
1480
4.08k
  // up from the 'Modules' directory.
1481
4.08k
  const DirectoryEntry *Dir = nullptr;
1482
4.08k
  if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) {
1483
51
    if (auto DirOrErr = FileMgr.getDirectory("."))
1484
51
      Dir = *DirOrErr;
1485
4.03k
  } else {
1486
4.03k
    if (!OriginalModuleMapFile.empty()) {
1487
16
      // We're building a preprocessed module map. Find or invent the directory
1488
16
      // that it originally occupied.
1489
16
      auto DirOrErr = FileMgr.getDirectory(
1490
16
          llvm::sys::path::parent_path(OriginalModuleMapFile));
1491
16
      if (DirOrErr) {
1492
16
        Dir = *DirOrErr;
1493
16
      } else {
1494
0
        auto *FakeFile = FileMgr.getVirtualFile(OriginalModuleMapFile, 0, 0);
1495
0
        Dir = FakeFile->getDir();
1496
0
      }
1497
4.02k
    } else {
1498
4.02k
      Dir = File->getDir();
1499
4.02k
    }
1500
4.03k
1501
4.03k
    StringRef DirName(Dir->getName());
1502
4.03k
    if (llvm::sys::path::filename(DirName) == "Modules") {
1503
198
      DirName = llvm::sys::path::parent_path(DirName);
1504
198
      if (DirName.endswith(".framework"))
1505
177
        if (auto DirOrErr = FileMgr.getDirectory(DirName))
1506
177
          Dir = *DirOrErr;
1507
198
      // FIXME: This assert can fail if there's a race between the above check
1508
198
      // and the removal of the directory.
1509
198
      assert(Dir && "parent must exist");
1510
198
    }
1511
4.03k
  }
1512
4.08k
1513
4.08k
  switch (loadModuleMapFileImpl(File, IsSystem, Dir, ID, Offset)) {
1514
4.08k
  case LMM_AlreadyLoaded:
1515
4.08k
  case LMM_NewlyLoaded:
1516
4.08k
    return false;
1517
4.08k
  case LMM_NoDirectory:
1518
2
  case LMM_InvalidModuleMap:
1519
2
    return true;
1520
0
  }
1521
0
  llvm_unreachable("Unknown load module map result");
1522
0
}
1523
1524
HeaderSearch::LoadModuleMapResult
1525
HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1526
                                    const DirectoryEntry *Dir, FileID ID,
1527
48.1k
                                    unsigned *Offset) {
1528
48.1k
  assert(File && "expected FileEntry");
1529
48.1k
1530
48.1k
  // Check whether we've already loaded this module map, and mark it as being
1531
48.1k
  // loaded in case we recursively try to load it from itself.
1532
48.1k
  auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1533
48.1k
  if (!AddResult.second)
1534
29.1k
    return AddResult.first->second ? LMM_AlreadyLoaded : 
LMM_InvalidModuleMap0
;
1535
18.9k
1536
18.9k
  if (ModMap.parseModuleMapFile(File, IsSystem, Dir, ID, Offset)) {
1537
89
    LoadedModuleMaps[File] = false;
1538
89
    return LMM_InvalidModuleMap;
1539
89
  }
1540
18.8k
1541
18.8k
  // Try to load a corresponding private module map.
1542
18.8k
  if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1543
142
    if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
1544
0
      LoadedModuleMaps[File] = false;
1545
0
      return LMM_InvalidModuleMap;
1546
0
    }
1547
18.8k
  }
1548
18.8k
1549
18.8k
  // This directory has a module map.
1550
18.8k
  return LMM_NewlyLoaded;
1551
18.8k
}
1552
1553
const FileEntry *
1554
948k
HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
1555
948k
  if (!HSOpts->ImplicitModuleMaps)
1556
849k
    return nullptr;
1557
98.8k
  // For frameworks, the preferred spelling is Modules/module.modulemap, but
1558
98.8k
  // module.map at the framework root is also accepted.
1559
98.8k
  SmallString<128> ModuleMapFileName(Dir->getName());
1560
98.8k
  if (IsFramework)
1561
6.17k
    llvm::sys::path::append(ModuleMapFileName, "Modules");
1562
98.8k
  llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1563
98.8k
  if (auto F = FileMgr.getFile(ModuleMapFileName))
1564
43.6k
    return *F;
1565
55.1k
1566
55.1k
  // Continue to allow module.map
1567
55.1k
  ModuleMapFileName = Dir->getName();
1568
55.1k
  llvm::sys::path::append(ModuleMapFileName, "module.map");
1569
55.1k
  if (auto F = FileMgr.getFile(ModuleMapFileName))
1570
2.88k
    return *F;
1571
52.2k
  return nullptr;
1572
52.2k
}
1573
1574
Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1575
                                          const DirectoryEntry *Dir,
1576
870k
                                          bool IsSystem) {
1577
870k
  if (Module *Module = ModMap.findModule(Name))
1578
15.8k
    return Module;
1579
854k
1580
854k
  // Try to load a module map file.
1581
854k
  switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
1582
849k
  case LMM_InvalidModuleMap:
1583
849k
    // Try to infer a module map from the framework directory.
1584
849k
    if (HSOpts->ImplicitModuleMaps)
1585
600
      ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
1586
849k
    break;
1587
0
1588
3
  case LMM_AlreadyLoaded:
1589
3
  case LMM_NoDirectory:
1590
3
    return nullptr;
1591
3
1592
4.84k
  case LMM_NewlyLoaded:
1593
4.84k
    break;
1594
854k
  }
1595
854k
1596
854k
  return ModMap.findModule(Name);
1597
854k
}
1598
1599
HeaderSearch::LoadModuleMapResult
1600
HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1601
511k
                                bool IsFramework) {
1602
511k
  if (auto Dir = FileMgr.getDirectory(DirName))
1603
39.9k
    return loadModuleMapFile(*Dir, IsSystem, IsFramework);
1604
471k
1605
471k
  return LMM_NoDirectory;
1606
471k
}
1607
1608
HeaderSearch::LoadModuleMapResult
1609
HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1610
1.01M
                                bool IsFramework) {
1611
1.01M
  auto KnownDir = DirectoryHasModuleMap.find(Dir);
1612
1.01M
  if (KnownDir != DirectoryHasModuleMap.end())
1613
73.6k
    return KnownDir->second ? LMM_AlreadyLoaded : 
LMM_InvalidModuleMap0
;
1614
945k
1615
945k
  if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
1616
44.0k
    LoadModuleMapResult Result =
1617
44.0k
        loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
1618
44.0k
    // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1619
44.0k
    // E.g. Foo.framework/Modules/module.modulemap
1620
44.0k
    //      ^Dir                  ^ModuleMapFile
1621
44.0k
    if (Result == LMM_NewlyLoaded)
1622
16.7k
      DirectoryHasModuleMap[Dir] = true;
1623
27.2k
    else if (Result == LMM_InvalidModuleMap)
1624
87
      DirectoryHasModuleMap[Dir] = false;
1625
44.0k
    return Result;
1626
44.0k
  }
1627
901k
  return LMM_InvalidModuleMap;
1628
901k
}
1629
1630
2
void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
1631
2
  Modules.clear();
1632
2
1633
2
  if (HSOpts->ImplicitModuleMaps) {
1634
2
    // Load module maps for each of the header search directories.
1635
10
    for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; 
++Idx8
) {
1636
8
      bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1637
8
      if (SearchDirs[Idx].isFramework()) {
1638
4
        std::error_code EC;
1639
4
        SmallString<128> DirNative;
1640
4
        llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1641
4
                                DirNative);
1642
4
1643
4
        // Search each of the ".framework" directories to load them as modules.
1644
4
        llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1645
4
        for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
1646
4
                                           DirEnd;
1647
298
             Dir != DirEnd && 
!EC294
;
Dir.increment(EC)294
) {
1648
294
          if (llvm::sys::path::extension(Dir->path()) != ".framework")
1649
4
            continue;
1650
290
1651
290
          auto FrameworkDir =
1652
290
              FileMgr.getDirectory(Dir->path());
1653
290
          if (!FrameworkDir)
1654
0
            continue;
1655
290
1656
290
          // Load this framework module.
1657
290
          loadFrameworkModule(llvm::sys::path::stem(Dir->path()), *FrameworkDir,
1658
290
                              IsSystem);
1659
290
        }
1660
4
        continue;
1661
4
      }
1662
4
1663
4
      // FIXME: Deal with header maps.
1664
4
      if (SearchDirs[Idx].isHeaderMap())
1665
0
        continue;
1666
4
1667
4
      // Try to load a module map file for the search directory.
1668
4
      loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1669
4
                        /*IsFramework*/ false);
1670
4
1671
4
      // Try to load module map files for immediate subdirectories of this
1672
4
      // search directory.
1673
4
      loadSubdirectoryModuleMaps(SearchDirs[Idx]);
1674
4
    }
1675
2
  }
1676
2
1677
2
  // Populate the list of modules.
1678
2
  for (ModuleMap::module_iterator M = ModMap.module_begin(),
1679
2
                               MEnd = ModMap.module_end();
1680
336
       M != MEnd; 
++M334
) {
1681
334
    Modules.push_back(M->getValue());
1682
334
  }
1683
2
}
1684
1685
70
void HeaderSearch::loadTopLevelSystemModules() {
1686
70
  if (!HSOpts->ImplicitModuleMaps)
1687
0
    return;
1688
70
1689
70
  // Load module maps for each of the header search directories.
1690
324
  
for (unsigned Idx = 0, N = SearchDirs.size(); 70
Idx != N;
++Idx254
) {
1691
254
    // We only care about normal header directories.
1692
254
    if (!SearchDirs[Idx].isNormalDir()) {
1693
37
      continue;
1694
37
    }
1695
217
1696
217
    // Try to load a module map file for the search directory.
1697
217
    loadModuleMapFile(SearchDirs[Idx].getDir(),
1698
217
                      SearchDirs[Idx].isSystemHeaderDirectory(),
1699
217
                      SearchDirs[Idx].isFramework());
1700
217
  }
1701
70
}
1702
1703
6.68k
void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1704
6.68k
  assert(HSOpts->ImplicitModuleMaps &&
1705
6.68k
         "Should not be loading subdirectory module maps");
1706
6.68k
1707
6.68k
  if (SearchDir.haveSearchedAllModuleMaps())
1708
0
    return;
1709
6.68k
1710
6.68k
  std::error_code EC;
1711
6.68k
  SmallString<128> Dir = SearchDir.getDir()->getName();
1712
6.68k
  FileMgr.makeAbsolutePath(Dir);
1713
6.68k
  SmallString<128> DirNative;
1714
6.68k
  llvm::sys::path::native(Dir, DirNative);
1715
6.68k
  llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1716
6.68k
  for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
1717
491k
       Dir != DirEnd && 
!EC484k
;
Dir.increment(EC)484k
) {
1718
484k
    bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1719
484k
    if (IsFramework == SearchDir.isFramework())
1720
484k
      loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1721
484k
                        SearchDir.isFramework());
1722
484k
  }
1723
6.68k
1724
6.68k
  SearchDir.setSearchedAllModuleMaps(true);
1725
6.68k
}
1726
1727
std::string HeaderSearch::suggestPathToFileForDiagnostics(
1728
195
    const FileEntry *File, llvm::StringRef MainFile, bool *IsSystem) {
1729
195
  // FIXME: We assume that the path name currently cached in the FileEntry is
1730
195
  // the most appropriate one for this analysis (and that it's spelled the
1731
195
  // same way as the corresponding header search path).
1732
195
  return suggestPathToFileForDiagnostics(File->getName(), /*WorkingDir=*/"",
1733
195
                                         MainFile, IsSystem);
1734
195
}
1735
1736
std::string HeaderSearch::suggestPathToFileForDiagnostics(
1737
    llvm::StringRef File, llvm::StringRef WorkingDir, llvm::StringRef MainFile,
1738
204
    bool *IsSystem) {
1739
204
  using namespace llvm::sys;
1740
204
1741
204
  unsigned BestPrefixLength = 0;
1742
204
  // Checks whether Dir and File shares a common prefix, if they do and that's
1743
204
  // the longest prefix we've seen so for it returns true and updates the
1744
204
  // BestPrefixLength accordingly.
1745
403
  auto CheckDir = [&](llvm::StringRef Dir) -> bool {
1746
403
    llvm::SmallString<32> DirPath(Dir.begin(), Dir.end());
1747
403
    if (!WorkingDir.empty() && 
!path::is_absolute(Dir)3
)
1748
2
      fs::make_absolute(WorkingDir, DirPath);
1749
403
    path::remove_dots(DirPath, /*remove_dot_dot=*/true);
1750
403
    Dir = DirPath;
1751
403
    for (auto NI = path::begin(File), NE = path::end(File),
1752
403
              DI = path::begin(Dir), DE = path::end(Dir);
1753
3.96k
         /*termination condition in loop*/; 
++NI, ++DI3.56k
) {
1754
3.96k
      // '.' components in File are ignored.
1755
3.97k
      while (NI != NE && *NI == ".")
1756
2
        ++NI;
1757
3.96k
      if (NI == NE)
1758
0
        break;
1759
3.96k
1760
3.96k
      // '.' components in Dir are ignored.
1761
3.96k
      while (DI != DE && 
*DI == "."3.76k
)
1762
0
        ++DI;
1763
3.96k
      if (DI == DE) {
1764
205
        // Dir is a prefix of File, up to '.' components and choice of path
1765
205
        // separators.
1766
205
        unsigned PrefixLength = NI - path::begin(File);
1767
205
        if (PrefixLength > BestPrefixLength) {
1768
204
          BestPrefixLength = PrefixLength;
1769
204
          return true;
1770
204
        }
1771
1
        break;
1772
1
      }
1773
3.76k
1774
3.76k
      // Consider all path separators equal.
1775
3.76k
      if (NI->size() == 1 && 
DI->size() == 1422
&&
1776
3.76k
          
path::is_separator(NI->front())422
&&
path::is_separator(DI->front())402
)
1777
402
        continue;
1778
3.36k
1779
3.36k
      if (*NI != *DI)
1780
198
        break;
1781
3.36k
    }
1782
403
    
return false199
;
1783
403
  };
1784
204
1785
605
  for (unsigned I = 0; I != SearchDirs.size(); 
++I401
) {
1786
401
    // FIXME: Support this search within frameworks and header maps.
1787
401
    if (!SearchDirs[I].isNormalDir())
1788
0
      continue;
1789
401
1790
401
    StringRef Dir = SearchDirs[I].getDir()->getName();
1791
401
    if (CheckDir(Dir) && 
IsSystem203
)
1792
195
      *IsSystem = BestPrefixLength ? I >= SystemDirIdx : 
false0
;
1793
401
  }
1794
204
1795
204
  // Try to shorten include path using TUs directory, if we couldn't find any
1796
204
  // suitable prefix in include search paths.
1797
204
  if (!BestPrefixLength && 
CheckDir(path::parent_path(MainFile))2
&&
IsSystem1
)
1798
0
    *IsSystem = false;
1799
204
1800
204
1801
204
  return path::convert_to_slash(File.drop_front(BestPrefixLength));
1802
204
}