-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang] Pass VFS into ASTUnit::LoadFromASTFile()
#159166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang] Pass VFS into ASTUnit::LoadFromASTFile()
#159166
Conversation
@llvm/pr-subscribers-clang Author: Jan Svoboda (jansvoboda11) ChangesThis PR makes the Full diff: https://github.com/llvm/llvm-project/pull/159166.diff 9 Files Affected:
diff --git a/clang/include/clang/Frontend/ASTUnit.h b/clang/include/clang/Frontend/ASTUnit.h
index 27bba8e64859d..f66df89aad904 100644
--- a/clang/include/clang/Frontend/ASTUnit.h
+++ b/clang/include/clang/Frontend/ASTUnit.h
@@ -729,16 +729,15 @@ class ASTUnit {
/// \returns - The initialized ASTUnit or null if the AST failed to load.
static std::unique_ptr<ASTUnit> LoadFromASTFile(
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
- WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts,
+ WhatToLoad ToLoad, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
+ std::shared_ptr<DiagnosticOptions> DiagOpts,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
const FileSystemOptions &FileSystemOpts,
const HeaderSearchOptions &HSOpts, const LangOptions *LangOpts = nullptr,
bool OnlyLocalDecls = false,
CaptureDiagsKind CaptureDiagnostics = CaptureDiagsKind::None,
bool AllowASTWithCompilerErrors = false,
- bool UserFilesAreVolatile = false,
- IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS =
- llvm::vfs::getRealFileSystem());
+ bool UserFilesAreVolatile = false);
private:
/// Helper function for \c LoadFromCompilerInvocation() and
diff --git a/clang/lib/CrossTU/CrossTranslationUnit.cpp b/clang/lib/CrossTU/CrossTranslationUnit.cpp
index 96e28fb10284f..847913d4b03fd 100644
--- a/clang/lib/CrossTU/CrossTranslationUnit.cpp
+++ b/clang/lib/CrossTU/CrossTranslationUnit.cpp
@@ -577,8 +577,8 @@ CrossTranslationUnitContext::ASTLoader::loadFromDump(StringRef ASTDumpPath) {
DiagnosticIDs::create(), *DiagOpts, DiagClient);
return ASTUnit::LoadFromASTFile(
ASTDumpPath, CI.getPCHContainerOperations()->getRawReader(),
- ASTUnit::LoadEverything, DiagOpts, Diags, CI.getFileSystemOpts(),
- CI.getHeaderSearchOpts());
+ ASTUnit::LoadEverything, CI.getVirtualFileSystemPtr(), DiagOpts, Diags,
+ CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
}
/// Load the AST from a source-file, which is supposed to be located inside the
diff --git a/clang/lib/Frontend/ASTMerge.cpp b/clang/lib/Frontend/ASTMerge.cpp
index 10c10458466bc..0dff69d613bcf 100644
--- a/clang/lib/Frontend/ASTMerge.cpp
+++ b/clang/lib/Frontend/ASTMerge.cpp
@@ -47,7 +47,8 @@ void ASTMergeAction::ExecuteAction() {
/*ShouldOwnClient=*/true);
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything,
- nullptr, Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
+ CI.getVirtualFileSystemPtr(), nullptr, Diags, CI.getFileSystemOpts(),
+ CI.getHeaderSearchOpts());
if (!Unit)
continue;
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 8b35af152cbc8..cb445682ac48b 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -808,12 +808,13 @@ void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile(
StringRef Filename, const PCHContainerReader &PCHContainerRdr,
- WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts,
+ WhatToLoad ToLoad, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
+ std::shared_ptr<DiagnosticOptions> DiagOpts,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
const FileSystemOptions &FileSystemOpts, const HeaderSearchOptions &HSOpts,
const LangOptions *LangOpts, bool OnlyLocalDecls,
CaptureDiagsKind CaptureDiagnostics, bool AllowASTWithCompilerErrors,
- bool UserFilesAreVolatile, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
+ bool UserFilesAreVolatile) {
std::unique_ptr<ASTUnit> AST(new ASTUnit(true));
// Recover resources if we crash before exiting this method.
diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp
index ca37e0661476d..6cc3b65a16cb2 100644
--- a/clang/lib/Frontend/FrontendAction.cpp
+++ b/clang/lib/Frontend/FrontendAction.cpp
@@ -864,7 +864,8 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
InputFile, CI.getPCHContainerReader(), ASTUnit::LoadPreprocessorOnly,
- nullptr, ASTDiags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
+ CI.getVirtualFileSystemPtr(), nullptr, ASTDiags, CI.getFileSystemOpts(),
+ CI.getHeaderSearchOpts());
if (!AST)
return false;
@@ -931,9 +932,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
StringRef InputFile = Input.getFile();
std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
- InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything, nullptr,
- Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts(),
- &CI.getLangOpts());
+ InputFile, CI.getPCHContainerReader(), ASTUnit::LoadEverything,
+ CI.getVirtualFileSystemPtr(), nullptr, Diags, CI.getFileSystemOpts(),
+ CI.getHeaderSearchOpts(), &CI.getLangOpts());
if (!AST)
return false;
diff --git a/clang/tools/c-index-test/core_main.cpp b/clang/tools/c-index-test/core_main.cpp
index 25104304322d4..5a3086a7fc08f 100644
--- a/clang/tools/c-index-test/core_main.cpp
+++ b/clang/tools/c-index-test/core_main.cpp
@@ -275,12 +275,13 @@ static bool printSourceSymbolsFromModule(StringRef modulePath,
HeaderSearchOptions HSOpts;
+ auto VFS = llvm::vfs::getRealFileSystem();
+
auto DiagOpts = std::make_shared<DiagnosticOptions>();
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
- CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
- *DiagOpts);
+ CompilerInstance::createDiagnostics(*VFS, *DiagOpts);
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
- modulePath, *pchRdr, ASTUnit::LoadASTOnly, DiagOpts, Diags,
+ modulePath, *pchRdr, ASTUnit::LoadASTOnly, VFS, DiagOpts, Diags,
FileSystemOpts, HSOpts, /*LangOpts=*/nullptr,
/*OnlyLocalDecls=*/true, CaptureDiagsKind::None,
/*AllowASTWithCompilerErrors=*/true,
diff --git a/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp b/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp
index ddb2944e3820b..6d8f86b13fa36 100644
--- a/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp
+++ b/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp
@@ -157,8 +157,8 @@ static bool HandleAST(StringRef AstPath) {
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
AstPath, CI->getPCHContainerOperations()->getRawReader(),
- ASTUnit::LoadASTOnly, DiagOpts, DiagEngine, CI->getFileSystemOpts(),
- CI->getHeaderSearchOpts());
+ ASTUnit::LoadASTOnly, CI->getVirtualFileSystemPtr(), DiagOpts, DiagEngine,
+ CI->getFileSystemOpts(), CI->getHeaderSearchOpts());
if (!Unit)
return false;
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index 9526f629bda42..6518e7d4d530f 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -4180,13 +4180,14 @@ enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
FileSystemOptions FileSystemOpts;
HeaderSearchOptions HSOpts;
+ auto VFS = llvm::vfs::getRealFileSystem();
+
auto DiagOpts = std::make_shared<DiagnosticOptions>();
IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
- CompilerInstance::createDiagnostics(*llvm::vfs::getRealFileSystem(),
- *DiagOpts);
+ CompilerInstance::createDiagnostics(*VFS, *DiagOpts);
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
ast_filename, CXXIdx->getPCHContainerOperations()->getRawReader(),
- ASTUnit::LoadEverything, DiagOpts, Diags, FileSystemOpts, HSOpts,
+ ASTUnit::LoadEverything, VFS, DiagOpts, Diags, FileSystemOpts, HSOpts,
/*LangOpts=*/nullptr, CXXIdx->getOnlyLocalDecls(), CaptureDiagsKind::All,
/*AllowASTWithCompilerErrors=*/true,
/*UserFilesAreVolatile=*/true);
diff --git a/clang/unittests/Frontend/ASTUnitTest.cpp b/clang/unittests/Frontend/ASTUnitTest.cpp
index 7160453b17daa..dfdbe90e72f1f 100644
--- a/clang/unittests/Frontend/ASTUnitTest.cpp
+++ b/clang/unittests/Frontend/ASTUnitTest.cpp
@@ -98,7 +98,8 @@ TEST_F(ASTUnitTest, SaveLoadPreservesLangOptionsInPrintingPolicy) {
std::unique_ptr<ASTUnit> AU = ASTUnit::LoadFromASTFile(
ASTFileName, PCHContainerOps->getRawReader(), ASTUnit::LoadEverything,
- DiagOpts, Diags, FileSystemOptions(), HSOpts);
+ llvm::vfs::getRealFileSystem(), DiagOpts, Diags, FileSystemOptions(),
+ HSOpts);
if (!AU)
FAIL() << "failed to load ASTUnit";
|
static std::unique_ptr<ASTUnit> LoadFromASTFile( | ||
StringRef Filename, const PCHContainerReader &PCHContainerRdr, | ||
WhatToLoad ToLoad, std::shared_ptr<DiagnosticOptions> DiagOpts, | ||
WhatToLoad ToLoad, IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like it is better to pass vfs::FileSystem&
to avoid ref counting? The caller should hold onto VFS during the function call and no ownership is transferred here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree in principle, but ASTUnit
is special when it comes to lifetimes and actually typically does outlive the caller. Moreover, the VFS
is used to create a FileManager
that currently does take the ownership.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/27/builds/16242 Here is the relevant piece of the build log for the reference
|
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. (cherry picked from commit cda542d)
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.
This PR makes the
VFS
parameter toASTUnit::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.