https://github.com/bricknerb created https://github.com/llvm/llvm-project/pull/123042
This would allow tools that don't use the real file system to use this function. >From 197fa433e56c875b4098806576f14b33a37a4300 Mon Sep 17 00:00:00 2001 From: Boaz Brickner <brick...@google.com> Date: Wed, 15 Jan 2025 11:48:20 +0100 Subject: [PATCH] [clang] Add support for passing FileSystem to buildASTFromCodeWithArgs() --- clang/include/clang/Tooling/Tooling.h | 7 ++++++- clang/lib/Tooling/Tooling.cpp | 5 +++-- clang/unittests/Tooling/ToolingTest.cpp | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) 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>()); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits