Skip to content

[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

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions clang/include/clang/Tooling/Tooling.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ buildASTFromCode(StringRef Code, StringRef FileName = "input.cc",
/// \param PCHContainerOps The PCHContainerOperations for loading and creating
/// clang modules.
///
/// \param Adjuster A function to filter the command line arguments as specified.
/// \param Adjuster A function to filter the command line arguments as
/// specified.
///
/// \param BaseFS FileSystem for managing and looking up files.
/// VirtualMappedFiles takes precedence.
///
/// \return The resulting AST or null if an error occurred.
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
Expand All @@ -233,7 +237,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> BaseFS =
llvm::vfs::getRealFileSystem());

/// Utility to run a FrontendAction in a single clang invocation.
class ToolInvocation {
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Tooling/Tooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> BaseFS) {
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(std::move(BaseFS)));
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
new llvm::vfs::InMemoryFileSystem);
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
Expand Down
14 changes: 14 additions & 0 deletions clang/unittests/Tooling/ToolingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>());
Expand Down
Loading