ymandel created this revision. ymandel added a reviewer: gribozavr. Herald added a project: clang.
Currently, `buildAST[WithArgs]` either succeeds or fails. This patch adds support for the caller to pass a `DiagnosticConsumer` to receive all relevant diagnostics. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D74763 Files: clang/include/clang/Tooling/Tooling.h clang/lib/Tooling/Tooling.cpp clang/unittests/Tooling/ToolingTest.cpp Index: clang/unittests/Tooling/ToolingTest.cpp =================================================================== --- clang/unittests/Tooling/ToolingTest.cpp +++ 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<ASTUnit> AST = buildASTFromCodeWithArgs( + "int x = \"A\";", {}, "input.cc", "clang-tool", + std::make_shared<PCHContainerOperations>(), + getClangStripDependencyFileAdjuster(), FileContentMappings(), &Consumer); + EXPECT_TRUE(AST.get()); + EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen); +} + TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) { std::unique_ptr<FrontendActionFactory> Factory( newFrontendActionFactory<SyntaxOnlyAction>()); @@ -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<std::string>()); ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc")); Index: clang/lib/Tooling/Tooling.cpp =================================================================== --- clang/lib/Tooling/Tooling.cpp +++ clang/lib/Tooling/Tooling.cpp @@ -619,7 +619,8 @@ 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) { + ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles, + DiagnosticConsumer *DiagConsumer) { std::vector<std::unique_ptr<ASTUnit>> ASTs; ASTBuilderAction Action(ASTs); llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> 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)); Index: clang/include/clang/Tooling/Tooling.h =================================================================== --- clang/include/clang/Tooling/Tooling.h +++ clang/include/clang/Tooling/Tooling.h @@ -225,7 +225,8 @@ std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>(), 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 {
Index: clang/unittests/Tooling/ToolingTest.cpp =================================================================== --- clang/unittests/Tooling/ToolingTest.cpp +++ 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<ASTUnit> AST = buildASTFromCodeWithArgs( + "int x = \"A\";", {}, "input.cc", "clang-tool", + std::make_shared<PCHContainerOperations>(), + getClangStripDependencyFileAdjuster(), FileContentMappings(), &Consumer); + EXPECT_TRUE(AST.get()); + EXPECT_EQ(1u, Consumer.NumDiagnosticsSeen); +} + TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) { std::unique_ptr<FrontendActionFactory> Factory( newFrontendActionFactory<SyntaxOnlyAction>()); @@ -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<std::string>()); ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc")); Index: clang/lib/Tooling/Tooling.cpp =================================================================== --- clang/lib/Tooling/Tooling.cpp +++ clang/lib/Tooling/Tooling.cpp @@ -619,7 +619,8 @@ 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) { + ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles, + DiagnosticConsumer *DiagConsumer) { std::vector<std::unique_ptr<ASTUnit>> ASTs; ASTBuilderAction Action(ASTs); llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> 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)); Index: clang/include/clang/Tooling/Tooling.h =================================================================== --- clang/include/clang/Tooling/Tooling.h +++ clang/include/clang/Tooling/Tooling.h @@ -225,7 +225,8 @@ std::shared_ptr<PCHContainerOperations> PCHContainerOps = std::make_shared<PCHContainerOperations>(), 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 {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits