Skip to content

Commit

Permalink
Fix a unittest file after D108695 when Z3 is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristóf Umann committed Sep 14, 2021
1 parent 8401713 commit fb4d590
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
51 changes: 38 additions & 13 deletions clang/unittests/StaticAnalyzer/CheckerRegistration.h
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -92,27 +114,30 @@ 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);
}

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>
Expand Down
Expand Up @@ -126,15 +126,15 @@ 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.

// 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");
Expand All @@ -157,15 +157,15 @@ 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.

// 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");
Expand Down Expand Up @@ -204,15 +204,15 @@ 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.

// 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");
Expand Down

0 comments on commit fb4d590

Please sign in to comment.