diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h --- a/clang/include/clang/Tooling/Tooling.h +++ b/clang/include/clang/Tooling/Tooling.h @@ -225,7 +225,8 @@ std::shared_ptr PCHContainerOps = std::make_shared(), ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(), - const FileContentMappings &VirtualMappedFiles = FileContentMappings()); + const FileContentMappings &VirtualMappedFiles = FileContentMappings(), + DiagnosticConsumer *DiagConsumer = nullptr); /// 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 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -619,7 +619,8 @@ std::unique_ptr buildASTFromCodeWithArgs( StringRef Code, const std::vector &Args, StringRef FileName, StringRef ToolName, std::shared_ptr PCHContainerOps, - ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles) { + ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles, + DiagnosticConsumer *DiagConsumer) { std::vector> ASTs; ASTBuilderAction Action(ASTs); llvm::IntrusiveRefCntPtr OverlayFileSystem( @@ -633,6 +634,7 @@ ToolInvocation Invocation( getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileName), FileName), &Action, Files.get(), std::move(PCHContainerOps)); + Invocation.setDiagnosticConsumer(DiagConsumer); InMemoryFileSystem->addFile(FileName, 0, llvm::MemoryBuffer::getMemBufferCopy(Code)); diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp --- a/clang/unittests/Tooling/ToolingTest.cpp +++ b/clang/unittests/Tooling/ToolingTest.cpp @@ -101,6 +101,15 @@ } return false; } + +struct TestDiagnosticConsumer : public DiagnosticConsumer { + TestDiagnosticConsumer() : NumDiagnosticsSeen(0) {} + void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, + const Diagnostic &Info) override { + ++NumDiagnosticsSeen; + } + unsigned NumDiagnosticsSeen; +}; } // end namespace TEST(runToolOnCode, FindsClassDecl) { @@ -129,6 +138,16 @@ EXPECT_FALSE(FindClassDeclX(AST.get())); } +TEST(buildASTFromCode, ReportsErrors) { + TestDiagnosticConsumer Consumer; + std::unique_ptr AST = buildASTFromCodeWithArgs( + "int x = \"A\";", {}, "input.cc", "clang-tool", + std::make_shared(), + getClangStripDependencyFileAdjuster(), FileContentMappings(), &Consumer); + EXPECT_TRUE(AST.get()); + EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen); +} + TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) { std::unique_ptr Factory( newFrontendActionFactory()); @@ -639,15 +658,6 @@ EXPECT_EQ(2u, ASTs.size()); } -struct TestDiagnosticConsumer : public DiagnosticConsumer { - TestDiagnosticConsumer() : NumDiagnosticsSeen(0) {} - void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, - const Diagnostic &Info) override { - ++NumDiagnosticsSeen; - } - unsigned NumDiagnosticsSeen; -}; - TEST(ClangToolTest, InjectDiagnosticConsumer) { FixedCompilationDatabase Compilations("/", std::vector()); ClangTool Tool(Compilations, std::vector(1, "/a.cc"));