Skip to content

Commit cda542d

Browse files
authored
[clang] Pass VFS into ASTUnit::LoadFromASTFile() (#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 b59af5c commit cda542d

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
@@ -729,16 +729,15 @@ class ASTUnit {
729729
/// \returns - The initialized ASTUnit or null if the AST failed to load.
730730
static std::unique_ptr<ASTUnit> LoadFromASTFile(
731731
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
732-
WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts,
732+
WhatToLoad ToLoad, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
733+
std::shared_ptr<DiagnosticOptions> DiagOpts,
733734
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
734735
const FileSystemOptions &FileSystemOpts,
735736
const HeaderSearchOptions &HSOpts, const LangOptions *LangOpts = nullptr,
736737
bool OnlyLocalDecls = false,
737738
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
738739
bool AllowASTWithCompilerErrors = false,
739-
bool UserFilesAreVolatile = false,
740-
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
741-
llvm::vfs::getRealFileSystem());
740+
bool UserFilesAreVolatile = false);
742741

743742
private:
744743
/// 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
@@ -577,8 +577,8 @@ CrossTranslationUnitContext::ASTLoader::loadFromDump(StringRef ASTDumpPath) {
577577
DiagnosticIDs::create(), *DiagOpts, DiagClient);
578578
return ASTUnit::LoadFromASTFile(
579579
ASTDumpPath, CI.getPCHContainerOperations()->getRawReader(),
580-
ASTUnit::LoadEverything, DiagOpts, Diags, CI.getFileSystemOpts(),
581-
CI.getHeaderSearchOpts());
580+
ASTUnit::LoadEverything, CI.getVirtualFileSystemPtr(), DiagOpts, Diags,
581+
CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
582582
}
583583

584584
/// 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
@@ -808,12 +808,13 @@ void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
808808

809809
std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
810810
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
811-
WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts,
811+
WhatToLoad ToLoad, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
812+
std::shared_ptr<DiagnosticOptions> DiagOpts,
812813
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
813814
const FileSystemOptions &FileSystemOpts, const HeaderSearchOptions &HSOpts,
814815
const LangOptions *LangOpts, bool OnlyLocalDecls,
815816
CaptureDiagsKind CaptureDiagnostics, bool AllowASTWithCompilerErrors,
816-
bool UserFilesAreVolatile, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
817+
bool UserFilesAreVolatile) {
817818
std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
818819

819820
// 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
@@ -864,7 +864,8 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
864864

865865
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
866866
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly,
867-
nullptr, ASTDiags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
867+
CI.getVirtualFileSystemPtr(), nullptr, ASTDiags, CI.getFileSystemOpts(),
868+
CI.getHeaderSearchOpts());
868869
if (!AST)
869870
return false;
870871

@@ -931,9 +932,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
931932
StringRef InputFile = Input.getFile();
932933

933934
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
934-
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, nullptr,
935-
Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts(),
936-
&CI.getLangOpts());
935+
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything,
936+
CI.getVirtualFileSystemPtr(), nullptr, Diags, CI.getFileSystemOpts(),
937+
CI.getHeaderSearchOpts(), &CI.getLangOpts());
937938

938939
if (!AST)
939940
return false;

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

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

276276
HeaderSearchOptions HSOpts;
277277

278+
auto VFS = llvm::vfs::getRealFileSystem();
279+
278280
auto DiagOpts = std::make_shared<DiagnosticOptions>();
279281
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
280-
CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
281-
*DiagOpts);
282+
CompilerInstance::createDiagnostics(*VFS, *DiagOpts);
282283
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
283-
modulePath, *pchRdr, ASTUnit::LoadASTOnly, DiagOpts, Diags,
284+
modulePath, *pchRdr, ASTUnit::LoadASTOnly, VFS, DiagOpts, Diags,
284285
FileSystemOpts, HSOpts, /*LangOpts=*/nullptr,
285286
/*OnlyLocalDecls=*/true, CaptureDiagsKind::None,
286287
/*AllowASTWithCompilerErrors=*/true,

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

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

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

163163
if (!Unit)
164164
return false;

clang/tools/libclang/CIndex.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,13 +4180,14 @@ enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
41804180
FileSystemOptions FileSystemOpts;
41814181
HeaderSearchOptions HSOpts;
41824182

4183+
auto VFS = llvm::vfs::getRealFileSystem();
4184+
41834185
auto DiagOpts = std::make_shared<DiagnosticOptions>();
41844186
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
4185-
CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
4186-
*DiagOpts);
4187+
CompilerInstance::createDiagnostics(*VFS, *DiagOpts);
41874188
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
41884189
ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(),
4189-
ASTUnit::LoadEverything, DiagOpts, Diags, FileSystemOpts, HSOpts,
4190+
ASTUnit::LoadEverything, VFS, DiagOpts, Diags, FileSystemOpts, HSOpts,
41904191
/*LangOpts=*/nullptr, CXXIdx->getOnlyLocalDecls(), CaptureDiagsKind::All,
41914192
/*AllowASTWithCompilerErrors=*/true,
41924193
/*UserFilesAreVolatile=*/true);

clang/unittests/Frontend/ASTUnitTest.cpp

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

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

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

0 commit comments

Comments
 (0)