Skip to content

Commit f757fdd

Browse files
committed
[clang] Pass VFS into ASTUnit::LoadFromASTFile() (llvm#159166)
This PR makes the `VFS` parameter to `ASTUnit::LoadFromASTFile()` required and explicit, rather than silently defaulting to the real file system. This makes it easy to correctly propagate the fully-configured VFS and load any input files like the rest of the compiler does.
1 parent 3f94fe0 commit f757fdd

File tree

9 files changed

+27
-22
lines changed

9 files changed

+27
-22
lines changed

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -706,16 +706,15 @@ class ASTUnit {
706706
/// \returns - The initialized ASTUnit or null if the AST failed to load.
707707
static std::unique_ptr<ASTUnit> LoadFromASTFile(
708708
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
709-
WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts,
709+
WhatToLoad ToLoad, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
710+
std::shared_ptr<DiagnosticOptions> DiagOpts,
710711
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
711712
const FileSystemOptions &FileSystemOpts,
712713
const HeaderSearchOptions &HSOpts, const LangOptions *LangOpts = nullptr,
713714
bool OnlyLocalDecls = false,
714715
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
715716
bool AllowASTWithCompilerErrors = false,
716-
bool UserFilesAreVolatile = false,
717-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
718-
llvm::vfs::getRealFileSystem());
717+
bool UserFilesAreVolatile = false);
719718

720719
private:
721720
/// Helper function for \c LoadFromCompilerInvocation() and

clang/lib/CrossTU/CrossTranslationUnit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,8 @@ CrossTranslationUnitContext::ASTLoader::loadFromDump(StringRef ASTDumpPath) {
578578
new DiagnosticsEngine(DiagID, *DiagOpts, DiagClient));
579579
return ASTUnit::LoadFromASTFile(
580580
ASTDumpPath, CI.getPCHContainerOperations()->getRawReader(),
581-
ASTUnit::LoadEverything, DiagOpts, Diags, CI.getFileSystemOpts(),
582-
CI.getHeaderSearchOpts());
581+
ASTUnit::LoadEverything, CI.getVirtualFileSystemPtr(), DiagOpts, Diags,
582+
CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
583583
}
584584

585585
/// Load the AST from a source-file, which is supposed to be located inside the

clang/lib/Frontend/ASTMerge.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ void ASTMergeAction::ExecuteAction() {
4747
/*ShouldOwnClient=*/true));
4848
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
4949
ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything,
50-
nullptr, Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
50+
CI.getVirtualFileSystemPtr(), nullptr, Diags, CI.getFileSystemOpts(),
51+
CI.getHeaderSearchOpts());
5152

5253
if (!Unit)
5354
continue;

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,12 +799,13 @@ void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
799799

800800
std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
801801
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
802-
WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts,
802+
WhatToLoad ToLoad, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
803+
std::shared_ptr<DiagnosticOptions> DiagOpts,
803804
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
804805
const FileSystemOptions &FileSystemOpts, const HeaderSearchOptions &HSOpts,
805806
const LangOptions *LangOpts, bool OnlyLocalDecls,
806807
CaptureDiagsKind CaptureDiagnostics, bool AllowASTWithCompilerErrors,
807-
bool UserFilesAreVolatile, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
808+
bool UserFilesAreVolatile) {
808809
std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
809810

810811
// Recover resources if we crash before exiting this method.

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,8 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
904904

905905
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
906906
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly,
907-
nullptr, ASTDiags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
907+
CI.getVirtualFileSystemPtr(), nullptr, ASTDiags, CI.getFileSystemOpts(),
908+
CI.getHeaderSearchOpts());
908909
if (!AST)
909910
return false;
910911

@@ -971,9 +972,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
971972
StringRef InputFile = Input.getFile();
972973

973974
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
974-
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, nullptr,
975-
Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts(),
976-
&CI.getLangOpts());
975+
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything,
976+
CI.getVirtualFileSystemPtr(), nullptr, Diags, CI.getFileSystemOpts(),
977+
CI.getHeaderSearchOpts(), &CI.getLangOpts());
977978

978979
if (!AST)
979980
return false;

clang/tools/c-index-test/core_main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,13 @@ static bool printSourceSymbolsFromModule(StringRef modulePath,
369369

370370
HeaderSearchOptions HSOpts;
371371

372+
auto VFS = llvm::vfs::getRealFileSystem();
373+
372374
auto DiagOpts = std::make_shared<DiagnosticOptions>();
373375
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
374-
CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
375-
*DiagOpts);
376+
CompilerInstance::createDiagnostics(*VFS, *DiagOpts);
376377
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
377-
modulePath, *pchRdr, ASTUnit::LoadASTOnly, DiagOpts, Diags,
378+
modulePath, *pchRdr, ASTUnit::LoadASTOnly, VFS, DiagOpts, Diags,
378379
FileSystemOpts, HSOpts, /*LangOpts=*/nullptr,
379380
/*OnlyLocalDecls=*/true, CaptureDiagsKind::None,
380381
/*AllowASTWithCompilerErrors=*/true,

clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ static bool HandleAST(StringRef AstPath) {
158158

159159
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
160160
AstPath, CI->getPCHContainerOperations()->getRawReader(),
161-
ASTUnit::LoadASTOnly, DiagOpts, DiagEngine, CI->getFileSystemOpts(),
162-
CI->getHeaderSearchOpts());
161+
ASTUnit::LoadASTOnly, CI->getVirtualFileSystemPtr(), DiagOpts, DiagEngine,
162+
CI->getFileSystemOpts(), CI->getHeaderSearchOpts());
163163

164164
if (!Unit)
165165
return false;

clang/tools/libclang/CIndex.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4243,13 +4243,14 @@ enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
42434243
FileSystemOptions FileSystemOpts;
42444244
HeaderSearchOptions HSOpts;
42454245

4246+
auto VFS = llvm::vfs::getRealFileSystem();
4247+
42464248
auto DiagOpts = std::make_shared<DiagnosticOptions>();
42474249
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
4248-
CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
4249-
*DiagOpts);
4250+
CompilerInstance::createDiagnostics(*VFS, *DiagOpts);
42504251
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
42514252
ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(),
4252-
ASTUnit::LoadEverything, DiagOpts, Diags, FileSystemOpts, HSOpts,
4253+
ASTUnit::LoadEverything, VFS, DiagOpts, Diags, FileSystemOpts, HSOpts,
42534254
/*LangOpts=*/nullptr, CXXIdx->getOnlyLocalDecls(), CaptureDiagsKind::All,
42544255
/*AllowASTWithCompilerErrors=*/true,
42554256
/*UserFilesAreVolatile=*/true);

clang/unittests/Frontend/ASTUnitTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ TEST_F(ASTUnitTest, SaveLoadPreservesLangOptionsInPrintingPolicy) {
9797

9898
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
9999
ASTFileName, PCHContainerOps->getRawReader(), ASTUnit::LoadEverything,
100-
DiagOpts, Diags, FileSystemOptions(), HSOpts);
100+
llvm::vfs::getRealFileSystem(), DiagOpts, Diags, FileSystemOptions(),
101+
HSOpts);
101102

102103
if (!AU)
103104
FAIL() << "failed to load ASTUnit";

0 commit comments

Comments
 (0)