Sam McCall 2fd614efc1 [dataflow] Add dedicated representation of boolean formulas
This is the first step in untangling the two current jobs of BoolValue.

=== Desired end-state: ===

- BoolValue will model C++ booleans e.g. held in StorageLocations.
  this includes describing uncertainty (e.g. "top" is a Value concern)
- Formula describes analysis-level assertions in terms of SAT atoms.

These can still be linked together: a BoolValue may have a corresponding
SAT atom which is constrained by formulas.

=== Done in this patch: ===

BoolValue is left intact, Formula is just the input type to the
SAT solver, and we build formulas as needed to invoke the solver.

=== Incidental changes to debug string printing: ===

- variables renamed from B0 etc to V0 etc
  B0 collides with the names of basic blocks, which is confusing when
  debugging flow conditions.
- debug printing of formulas (Formula and Atom) uses operator<<
  rather than debugString(), so works with gtest.
  Therefore moved out of DebugSupport.h
- Did the same to Solver::Result, and some helper changes to SolverTest,
  so that we get useful messages on unit test failures
- formulas are now printed as infix expressions on one line, rather than
  wrapped/indented S-exprs. My experience is that this is easier to scan
  FCs for small examples, and large ones are unreadable either way.
- most of the several debugString() functions for constraints/results
  are unused, so removed them rather than updating tests.
  Inlined the one that was actually used into its callsite.

Differential Revision: https://reviews.llvm.org/D153366
2023-07-04 12:19:44 +02:00

152 lines
4.3 KiB
C++

//===- ArenaTest.cpp ------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/Analysis/FlowSensitive/Arena.h"
#include "llvm/Support/ScopedPrinter.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang::dataflow {
namespace {
class ArenaTest : public ::testing::Test {
protected:
Arena A;
};
TEST_F(ArenaTest, CreateAtomicBoolValueReturnsDistinctValues) {
auto &X = A.create<AtomicBoolValue>();
auto &Y = A.create<AtomicBoolValue>();
EXPECT_NE(&X, &Y);
}
TEST_F(ArenaTest, CreateTopBoolValueReturnsDistinctValues) {
auto &X = A.create<TopBoolValue>();
auto &Y = A.create<TopBoolValue>();
EXPECT_NE(&X, &Y);
}
TEST_F(ArenaTest, GetOrCreateConjunctionReturnsSameExprGivenSameArgs) {
auto &X = A.create<AtomicBoolValue>();
auto &XAndX = A.makeAnd(X, X);
EXPECT_EQ(&XAndX, &X);
}
TEST_F(ArenaTest, GetOrCreateConjunctionReturnsSameExprOnSubsequentCalls) {
auto &X = A.create<AtomicBoolValue>();
auto &Y = A.create<AtomicBoolValue>();
auto &XAndY1 = A.makeAnd(X, Y);
auto &XAndY2 = A.makeAnd(X, Y);
EXPECT_EQ(&XAndY1, &XAndY2);
auto &YAndX = A.makeAnd(Y, X);
EXPECT_EQ(&XAndY1, &YAndX);
auto &Z = A.create<AtomicBoolValue>();
auto &XAndZ = A.makeAnd(X, Z);
EXPECT_NE(&XAndY1, &XAndZ);
}
TEST_F(ArenaTest, GetOrCreateDisjunctionReturnsSameExprGivenSameArgs) {
auto &X = A.create<AtomicBoolValue>();
auto &XOrX = A.makeOr(X, X);
EXPECT_EQ(&XOrX, &X);
}
TEST_F(ArenaTest, GetOrCreateDisjunctionReturnsSameExprOnSubsequentCalls) {
auto &X = A.create<AtomicBoolValue>();
auto &Y = A.create<AtomicBoolValue>();
auto &XOrY1 = A.makeOr(X, Y);
auto &XOrY2 = A.makeOr(X, Y);
EXPECT_EQ(&XOrY1, &XOrY2);
auto &YOrX = A.makeOr(Y, X);
EXPECT_EQ(&XOrY1, &YOrX);
auto &Z = A.create<AtomicBoolValue>();
auto &XOrZ = A.makeOr(X, Z);
EXPECT_NE(&XOrY1, &XOrZ);
}
TEST_F(ArenaTest, GetOrCreateNegationReturnsSameExprOnSubsequentCalls) {
auto &X = A.create<AtomicBoolValue>();
auto &NotX1 = A.makeNot(X);
auto &NotX2 = A.makeNot(X);
EXPECT_EQ(&NotX1, &NotX2);
auto &Y = A.create<AtomicBoolValue>();
auto &NotY = A.makeNot(Y);
EXPECT_NE(&NotX1, &NotY);
}
TEST_F(ArenaTest, GetOrCreateImplicationReturnsTrueGivenSameArgs) {
auto &X = A.create<AtomicBoolValue>();
auto &XImpliesX = A.makeImplies(X, X);
EXPECT_EQ(&XImpliesX, &A.makeLiteral(true));
}
TEST_F(ArenaTest, GetOrCreateImplicationReturnsSameExprOnSubsequentCalls) {
auto &X = A.create<AtomicBoolValue>();
auto &Y = A.create<AtomicBoolValue>();
auto &XImpliesY1 = A.makeImplies(X, Y);
auto &XImpliesY2 = A.makeImplies(X, Y);
EXPECT_EQ(&XImpliesY1, &XImpliesY2);
auto &YImpliesX = A.makeImplies(Y, X);
EXPECT_NE(&XImpliesY1, &YImpliesX);
auto &Z = A.create<AtomicBoolValue>();
auto &XImpliesZ = A.makeImplies(X, Z);
EXPECT_NE(&XImpliesY1, &XImpliesZ);
}
TEST_F(ArenaTest, GetOrCreateIffReturnsTrueGivenSameArgs) {
auto &X = A.create<AtomicBoolValue>();
auto &XIffX = A.makeEquals(X, X);
EXPECT_EQ(&XIffX, &A.makeLiteral(true));
}
TEST_F(ArenaTest, GetOrCreateIffReturnsSameExprOnSubsequentCalls) {
auto &X = A.create<AtomicBoolValue>();
auto &Y = A.create<AtomicBoolValue>();
auto &XIffY1 = A.makeEquals(X, Y);
auto &XIffY2 = A.makeEquals(X, Y);
EXPECT_EQ(&XIffY1, &XIffY2);
auto &YIffX = A.makeEquals(Y, X);
EXPECT_EQ(&XIffY1, &YIffX);
auto &Z = A.create<AtomicBoolValue>();
auto &XIffZ = A.makeEquals(X, Z);
EXPECT_NE(&XIffY1, &XIffZ);
}
TEST_F(ArenaTest, ValueToFormula) {
auto &X = A.create<AtomicBoolValue>();
auto &Y = A.create<AtomicBoolValue>();
auto &XIffY = A.makeEquals(X, Y);
auto &XOrNotY = A.makeOr(X, A.makeNot(Y));
auto &Implies = A.makeImplies(XIffY, XOrNotY);
EXPECT_EQ(llvm::to_string(A.getFormula(Implies)),
"((V0 = V1) => (V0 | !V1))");
}
TEST_F(ArenaTest, ValueToFormulaCached) {
auto &X = A.create<AtomicBoolValue>();
auto &Y = A.create<AtomicBoolValue>();
auto &XIffY = A.makeEquals(X, Y);
auto &Formula1 = A.getFormula(XIffY);
auto &Formula2 = A.getFormula(XIffY);
EXPECT_EQ(&Formula1, &Formula2);
}
} // namespace
} // namespace clang::dataflow