[clang] fb4d590 - Fix a unittest file after D108695 when Z3 is enabled
Kristóf Umann via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 14 07:11:23 PDT 2021
Author: Kristóf Umann
Date: 2021-09-14T16:11:11+02:00
New Revision: fb4d590a622f4031900516360c07ee6ace01c5e6
URL: https://github.com/llvm/llvm-project/commit/fb4d590a622f4031900516360c07ee6ace01c5e6
DIFF: https://github.com/llvm/llvm-project/commit/fb4d590a622f4031900516360c07ee6ace01c5e6.diff
LOG: Fix a unittest file after D108695 when Z3 is enabled
Added:
Modified:
clang/unittests/StaticAnalyzer/CheckerRegistration.h
clang/unittests/StaticAnalyzer/FalsePositiveRefutationBRVisitorTest.cpp
Removed:
################################################################################
diff --git a/clang/unittests/StaticAnalyzer/CheckerRegistration.h b/clang/unittests/StaticAnalyzer/CheckerRegistration.h
index de804236229ad..bccdb94d29383 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<const PathDiagnostic *> &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<const PathDiagnostic *> &Diags,
FilesMade *filesMade) override {
for (const auto *PD : Diags) {
@@ -65,18 +81,24 @@ void addChecker(AnalysisASTConsumer &AnalysisConsumer,
Fn1(AnalysisConsumer, AnOpts);
}
-template <AddCheckerFn... Fns>
-class TestAction : public ASTFrontendAction {
+template <AddCheckerFn... Fns> 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<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
StringRef File) override {
std::unique_ptr<AnalysisASTConsumer> AnalysisConsumer =
CreateAnalysisConsumer(Compiler);
- AnalysisConsumer->AddDiagnosticConsumer(new DiagConsumer(DiagsOutput));
+ if (OnlyEmitWarnings)
+ AnalysisConsumer->AddDiagnosticConsumer(
+ new OnlyWarningsDiagConsumer(DiagsOutput));
+ else
+ AnalysisConsumer->AddDiagnosticConsumer(
+ new PathDiagConsumer(DiagsOutput));
addChecker<Fns...>(*AnalysisConsumer, *Compiler.getAnalyzerOpts());
return std::move(AnalysisConsumer);
}
@@ -92,15 +114,16 @@ inline SmallString<80> getCurrentTestNameAsFileName() {
}
template <AddCheckerFn... Fns>
-bool runCheckerOnCode(const std::string &Code, std::string &Diags) {
+bool runCheckerOnCode(const std::string &Code, std::string &Diags,
+ bool OnlyEmitWarnings = false) {
const SmallVectorImpl<char> &FileName = getCurrentTestNameAsFileName();
llvm::raw_string_ostream OS(Diags);
- return tooling::runToolOnCode(std::make_unique<TestAction<Fns...>>(OS), Code,
- FileName);
+ return tooling::runToolOnCode(
+ std::make_unique<TestAction<Fns...>>(OS, OnlyEmitWarnings), Code,
+ FileName);
}
-template <AddCheckerFn... Fns>
-bool runCheckerOnCode(const std::string &Code) {
+template <AddCheckerFn... Fns> bool runCheckerOnCode(const std::string &Code) {
std::string Diags;
return runCheckerOnCode<Fns...>(Code, Diags);
}
@@ -108,11 +131,13 @@ bool runCheckerOnCode(const std::string &Code) {
template <AddCheckerFn... Fns>
bool runCheckerOnCodeWithArgs(const std::string &Code,
const std::vector<std::string> &Args,
- std::string &Diags) {
+ std::string &Diags,
+ bool OnlyEmitWarnings = false) {
const SmallVectorImpl<char> &FileName = getCurrentTestNameAsFileName();
llvm::raw_string_ostream OS(Diags);
return tooling::runToolOnCodeWithArgs(
- std::make_unique<TestAction<Fns...>>(OS), Code, Args, FileName);
+ std::make_unique<TestAction<Fns...>>(OS, OnlyEmitWarnings), Code, Args,
+ FileName);
}
template <AddCheckerFn... Fns>
diff --git a/clang/unittests/StaticAnalyzer/FalsePositiveRefutationBRVisitorTest.cpp b/clang/unittests/StaticAnalyzer/FalsePositiveRefutationBRVisitorTest.cpp
index beaaebdd36cf9..28dad31f54f3e 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<addFalsePositiveGenerator>(
- 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<addFalsePositiveGenerator>(
- 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<addFalsePositiveGenerator>(
- 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<addFalsePositiveGenerator>(
- 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<addFalsePositiveGenerator>(
- 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<addFalsePositiveGenerator>(
- Code, LazyAssumeArgs, Diags2));
+ Code, LazyAssumeArgs, Diags2, /*OnlyEmitWarnings=*/ true));
EXPECT_EQ(Diags2,
"test.FalsePositiveGenerator: REACHED_WITH_NO_CONTRADICTION\n"
"test.FalsePositiveGenerator: CAN_BE_TRUE\n");
More information about the cfe-commits
mailing list