diff --git a/clang/unittests/StaticAnalyzer/CheckerRegistration.h b/clang/unittests/StaticAnalyzer/CheckerRegistration.h index de804236229ade..bccdb94d293833 100644 --- a/clang/unittests/StaticAnalyzer/CheckerRegistration.h +++ b/clang/unittests/StaticAnalyzer/CheckerRegistration.h @@ -20,11 +20,27 @@ namespace clang { namespace ento { -class DiagConsumer : public PathDiagnosticConsumer { +class OnlyWarningsDiagConsumer : public PathDiagnosticConsumer { llvm::raw_ostream &Output; public: - DiagConsumer(llvm::raw_ostream &Output) : Output(Output) {} + OnlyWarningsDiagConsumer(llvm::raw_ostream &Output) : Output(Output) {} + void FlushDiagnosticsImpl(std::vector &Diags, + FilesMade *filesMade) override { + for (const auto *PD : Diags) { + Output << PD->getCheckerName() << ": "; + Output << PD->getShortDescription() << '\n'; + } + } + + StringRef getName() const override { return "Test"; } +}; + +class PathDiagConsumer : public PathDiagnosticConsumer { + llvm::raw_ostream &Output; + +public: + PathDiagConsumer(llvm::raw_ostream &Output) : Output(Output) {} void FlushDiagnosticsImpl(std::vector &Diags, FilesMade *filesMade) override { for (const auto *PD : Diags) { @@ -65,18 +81,24 @@ void addChecker(AnalysisASTConsumer &AnalysisConsumer, Fn1(AnalysisConsumer, AnOpts); } -template -class TestAction : public ASTFrontendAction { +template class TestAction : public ASTFrontendAction { llvm::raw_ostream &DiagsOutput; + bool OnlyEmitWarnings; public: - TestAction(llvm::raw_ostream &DiagsOutput) : DiagsOutput(DiagsOutput) {} + TestAction(llvm::raw_ostream &DiagsOutput, bool OnlyEmitWarnings) + : DiagsOutput(DiagsOutput), OnlyEmitWarnings(OnlyEmitWarnings) {} std::unique_ptr CreateASTConsumer(CompilerInstance &Compiler, StringRef File) override { std::unique_ptr AnalysisConsumer = CreateAnalysisConsumer(Compiler); - AnalysisConsumer->AddDiagnosticConsumer(new DiagConsumer(DiagsOutput)); + if (OnlyEmitWarnings) + AnalysisConsumer->AddDiagnosticConsumer( + new OnlyWarningsDiagConsumer(DiagsOutput)); + else + AnalysisConsumer->AddDiagnosticConsumer( + new PathDiagConsumer(DiagsOutput)); addChecker(*AnalysisConsumer, *Compiler.getAnalyzerOpts()); return std::move(AnalysisConsumer); } @@ -92,15 +114,16 @@ inline SmallString<80> getCurrentTestNameAsFileName() { } template -bool runCheckerOnCode(const std::string &Code, std::string &Diags) { +bool runCheckerOnCode(const std::string &Code, std::string &Diags, + bool OnlyEmitWarnings = false) { const SmallVectorImpl &FileName = getCurrentTestNameAsFileName(); llvm::raw_string_ostream OS(Diags); - return tooling::runToolOnCode(std::make_unique>(OS), Code, - FileName); + return tooling::runToolOnCode( + std::make_unique>(OS, OnlyEmitWarnings), Code, + FileName); } -template -bool runCheckerOnCode(const std::string &Code) { +template bool runCheckerOnCode(const std::string &Code) { std::string Diags; return runCheckerOnCode(Code, Diags); } @@ -108,11 +131,13 @@ bool runCheckerOnCode(const std::string &Code) { template bool runCheckerOnCodeWithArgs(const std::string &Code, const std::vector &Args, - std::string &Diags) { + std::string &Diags, + bool OnlyEmitWarnings = false) { const SmallVectorImpl &FileName = getCurrentTestNameAsFileName(); llvm::raw_string_ostream OS(Diags); return tooling::runToolOnCodeWithArgs( - std::make_unique>(OS), Code, Args, FileName); + std::make_unique>(OS, OnlyEmitWarnings), Code, Args, + FileName); } template diff --git a/clang/unittests/StaticAnalyzer/FalsePositiveRefutationBRVisitorTest.cpp b/clang/unittests/StaticAnalyzer/FalsePositiveRefutationBRVisitorTest.cpp index beaaebdd36cf99..28dad31f54f3e2 100644 --- a/clang/unittests/StaticAnalyzer/FalsePositiveRefutationBRVisitorTest.cpp +++ b/clang/unittests/StaticAnalyzer/FalsePositiveRefutationBRVisitorTest.cpp @@ -126,7 +126,7 @@ TEST_F(FalsePositiveRefutationBRVisitorTestBase, UnSatInTheMiddleNoReport) { std::string Diags; EXPECT_TRUE(runCheckerOnCodeWithArgs( - Code, LazyAssumeAndCrossCheckArgs, Diags)); + Code, LazyAssumeAndCrossCheckArgs, Diags, /*OnlyEmitWarnings=*/ true)); EXPECT_EQ(Diags, "test.FalsePositiveGenerator: REACHED_WITH_NO_CONTRADICTION\n"); // Single warning. The second report was invalidated by the visitor. @@ -134,7 +134,7 @@ TEST_F(FalsePositiveRefutationBRVisitorTestBase, UnSatInTheMiddleNoReport) { // Without enabling the crosscheck-with-z3 both reports are displayed. std::string Diags2; EXPECT_TRUE(runCheckerOnCodeWithArgs( - Code, LazyAssumeArgs, Diags2)); + Code, LazyAssumeArgs, Diags2, /*OnlyEmitWarnings=*/ true)); EXPECT_EQ(Diags2, "test.FalsePositiveGenerator: REACHED_WITH_NO_CONTRADICTION\n" "test.FalsePositiveGenerator: REACHED_WITH_CONTRADICTION\n"); @@ -157,7 +157,7 @@ TEST_F(FalsePositiveRefutationBRVisitorTestBase, })"; std::string Diags; EXPECT_TRUE(runCheckerOnCodeWithArgs( - Code, LazyAssumeAndCrossCheckArgs, Diags)); + Code, LazyAssumeAndCrossCheckArgs, Diags, /*OnlyEmitWarnings=*/ true)); EXPECT_EQ(Diags, "test.FalsePositiveGenerator: REACHED_WITH_NO_CONTRADICTION\n"); // Single warning. The second report was invalidated by the visitor. @@ -165,7 +165,7 @@ TEST_F(FalsePositiveRefutationBRVisitorTestBase, // Without enabling the crosscheck-with-z3 both reports are displayed. std::string Diags2; EXPECT_TRUE(runCheckerOnCodeWithArgs( - Code, LazyAssumeArgs, Diags2)); + Code, LazyAssumeArgs, Diags2, /*OnlyEmitWarnings=*/ true)); EXPECT_EQ(Diags2, "test.FalsePositiveGenerator: REACHED_WITH_NO_CONTRADICTION\n" "test.FalsePositiveGenerator: CAN_BE_TRUE\n"); @@ -204,7 +204,7 @@ TEST_F(FalsePositiveRefutationBRVisitorTestBase, std::string Diags; EXPECT_TRUE(runCheckerOnCodeWithArgs( - Code, LazyAssumeAndCrossCheckArgs, Diags)); + Code, LazyAssumeAndCrossCheckArgs, Diags, /*OnlyEmitWarnings=*/ true)); EXPECT_EQ(Diags, "test.FalsePositiveGenerator: REACHED_WITH_NO_CONTRADICTION\n"); // Single warning. The second report was invalidated by the visitor. @@ -212,7 +212,7 @@ TEST_F(FalsePositiveRefutationBRVisitorTestBase, // Without enabling the crosscheck-with-z3 both reports are displayed. std::string Diags2; EXPECT_TRUE(runCheckerOnCodeWithArgs( - Code, LazyAssumeArgs, Diags2)); + Code, LazyAssumeArgs, Diags2, /*OnlyEmitWarnings=*/ true)); EXPECT_EQ(Diags2, "test.FalsePositiveGenerator: REACHED_WITH_NO_CONTRADICTION\n" "test.FalsePositiveGenerator: CAN_BE_TRUE\n");