Skip to content

Commit 6ca560a

Browse files
authored
[clang] Add support for passing FileSystem to buildASTFromCodeWithArgs() (#123042)
This would allow tools that don't use the real file system to use this function.
1 parent a32c456 commit 6ca560a

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

clang/include/clang/Tooling/Tooling.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ buildASTFromCode(StringRef Code, StringRef FileName = "input.cc",
223223
/// \param PCHContainerOps The PCHContainerOperations for loading and creating
224224
/// clang modules.
225225
///
226-
/// \param Adjuster A function to filter the command line arguments as specified.
226+
/// \param Adjuster A function to filter the command line arguments as
227+
/// specified.
228+
///
229+
/// \param BaseFS FileSystem for managing and looking up files.
230+
/// VirtualMappedFiles takes precedence.
227231
///
228232
/// \return The resulting AST or null if an error occurred.
229233
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
@@ -233,7 +237,9 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
233237
std::make_shared<PCHContainerOperations>(),
234238
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(),
235239
const FileContentMappings &VirtualMappedFiles = FileContentMappings(),
236-
DiagnosticConsumer *DiagConsumer = nullptr);
240+
DiagnosticConsumer *DiagConsumer = nullptr,
241+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS =
242+
llvm::vfs::getRealFileSystem());
237243

238244
/// Utility to run a FrontendAction in a single clang invocation.
239245
class ToolInvocation {

clang/lib/Tooling/Tooling.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,12 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
692692
StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
693693
StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
694694
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
695-
DiagnosticConsumer *DiagConsumer) {
695+
DiagnosticConsumer *DiagConsumer,
696+
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
696697
std::vector<std::unique_ptr<ASTUnit>> ASTs;
697698
ASTBuilderAction Action(ASTs);
698699
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
699-
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
700+
new llvm::vfs::OverlayFileSystem(std::move(BaseFS)));
700701
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
701702
new llvm::vfs::InMemoryFileSystem);
702703
OverlayFileSystem->pushOverlay(InMemoryFileSystem);

clang/unittests/Tooling/ToolingTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ TEST(buildASTFromCode, ReportsErrors) {
152152
EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen);
153153
}
154154

155+
TEST(buildASTFromCode, FileSystem) {
156+
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
157+
new llvm::vfs::InMemoryFileSystem);
158+
InMemoryFileSystem->addFile("included_file.h", 0,
159+
llvm::MemoryBuffer::getMemBufferCopy("class X;"));
160+
std::unique_ptr<ASTUnit> AST = buildASTFromCodeWithArgs(
161+
R"(#include "included_file.h")", {}, "input.cc", "clang-tool",
162+
std::make_shared<PCHContainerOperations>(),
163+
getClangStripDependencyFileAdjuster(), FileContentMappings(), nullptr,
164+
InMemoryFileSystem);
165+
ASSERT_TRUE(AST.get());
166+
EXPECT_TRUE(FindClassDeclX(AST.get()));
167+
}
168+
155169
TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
156170
std::unique_ptr<FrontendActionFactory> Factory(
157171
newFrontendActionFactory<SyntaxOnlyAction>());

0 commit comments

Comments
 (0)