[clang] [clang] Add support for passing FileSystem to buildASTFromCodeWithArgs() (PR #123042)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 15 03:16:24 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Boaz Brickner (bricknerb)
<details>
<summary>Changes</summary>
This 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:
- (modified) clang/include/clang/Tooling/Tooling.h (+6-1)
- (modified) clang/lib/Tooling/Tooling.cpp (+3-2)
- (modified) clang/unittests/Tooling/ToolingTest.cpp (+14)
``````````diff
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>());
``````````
</details>
https://github.com/llvm/llvm-project/pull/123042
More information about the cfe-commits
mailing list