-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Add support for passing FileSystem to buildASTFromCodeWithArgs() #123042
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
Conversation
@llvm/pr-subscribers-clang Author: Boaz Brickner (bricknerb) ChangesThis would allow tools that don't use the real file system to use this function. Full diff: https://github.com/llvm/llvm-project/pull/123042.diff 3 Files Affected:
diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h
index 070706e8fa6d11..30ee02d2cf5f12 100644
--- a/clang/include/clang/Tooling/Tooling.h
+++ b/clang/include/clang/Tooling/Tooling.h
@@ -225,6 +225,9 @@ buildASTFromCode(StringRef Code, StringRef FileName = "input.cc",
///
/// \param Adjuster A function to filter the command line arguments as specified.
///
+/// \param FileSystem FileSystem for managing and looking up files.
+///
+///
/// \return The resulting AST or null if an error occurred.
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
StringRef Code, const std::vector<std::string> &Args,
@@ -233,7 +236,9 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
std::make_shared<PCHContainerOperations>(),
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(),
const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
- DiagnosticConsumer *DiagConsumer = nullptr);
+ DiagnosticConsumer *DiagConsumer = nullptr,
+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem =
+ llvm::vfs::getRealFileSystem());
/// Utility to run a FrontendAction in a single clang invocation.
class ToolInvocation {
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 88b7349ce8fed6..385811d3dab9e1 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -692,11 +692,12 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
- DiagnosticConsumer *DiagConsumer) {
+ DiagnosticConsumer *DiagConsumer,
+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem) {
std::vector<std::unique_ptr<ASTUnit>> ASTs;
ASTBuilderAction Action(ASTs);
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
- new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
+ new llvm::vfs::OverlayFileSystem(FileSystem));
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
new llvm::vfs::InMemoryFileSystem);
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp
index 0b65577a05193f..8cdfffb54390e0 100644
--- a/clang/unittests/Tooling/ToolingTest.cpp
+++ b/clang/unittests/Tooling/ToolingTest.cpp
@@ -152,6 +152,20 @@ TEST(buildASTFromCode, ReportsErrors) {
EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
}
+TEST(buildASTFromCode, FileSystem) {
+ llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
+ new llvm::vfs::InMemoryFileSystem);
+ InMemoryFileSystem->addFile("included_file.h", 0,
+ llvm::MemoryBuffer::getMemBufferCopy("class X;"));
+ std::unique_ptr<ASTUnit> AST = buildASTFromCodeWithArgs(
+ R"(#include "included_file.h")", {}, "input.cc", "clang-tool",
+ std::make_shared<PCHContainerOperations>(),
+ getClangStripDependencyFileAdjuster(), FileContentMappings(), nullptr,
+ InMemoryFileSystem);
+ ASSERT_TRUE(AST.get());
+ EXPECT_TRUE(FindClassDeclX(AST.get()));
+}
+
TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
std::unique_ptr<FrontendActionFactory> Factory(
newFrontendActionFactory<SyntaxOnlyAction>());
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/162/builds/14147 Here is the relevant piece of the build log for the reference
|
This would allow tools that don't use the real file system to use this function.