1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
//===- unittests/Analysis/FlowSensitive/DataflowAnalysisContextTest.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/DataflowAnalysisContext.h"
#include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <memory>
namespace {
using namespace clang;
using namespace dataflow;
class DataflowAnalysisContextTest : public ::testing::Test {
protected:
DataflowAnalysisContextTest()
: Context(std::make_unique<WatchedLiteralsSolver>()), A(Context.arena()) {
}
DataflowAnalysisContext Context;
Arena &A;
};
TEST_F(DataflowAnalysisContextTest, DistinctTopsNotEquivalent) {
auto &X = A.makeTopValue();
auto &Y = A.makeTopValue();
EXPECT_FALSE(Context.equivalentFormulas(X.formula(), Y.formula()));
}
TEST_F(DataflowAnalysisContextTest, EmptyFlowCondition) {
Atom FC = A.makeFlowConditionToken();
auto &C = A.makeAtomRef(A.makeAtom());
EXPECT_FALSE(Context.flowConditionImplies(FC, C));
}
TEST_F(DataflowAnalysisContextTest, AddFlowConditionConstraint) {
Atom FC = A.makeFlowConditionToken();
auto &C = A.makeAtomRef(A.makeAtom());
Context.addFlowConditionConstraint(FC, C);
EXPECT_TRUE(Context.flowConditionImplies(FC, C));
}
TEST_F(DataflowAnalysisContextTest, ForkFlowCondition) {
Atom FC1 = A.makeFlowConditionToken();
auto &C1 = A.makeAtomRef(A.makeAtom());
Context.addFlowConditionConstraint(FC1, C1);
// Forked flow condition inherits the constraints of its parent flow
// condition.
Atom FC2 = Context.forkFlowCondition(FC1);
EXPECT_TRUE(Context.flowConditionImplies(FC2, C1));
// Adding a new constraint to the forked flow condition does not affect its
// parent flow condition.
auto &C2 = A.makeAtomRef(A.makeAtom());
Context.addFlowConditionConstraint(FC2, C2);
EXPECT_TRUE(Context.flowConditionImplies(FC2, C2));
EXPECT_FALSE(Context.flowConditionImplies(FC1, C2));
}
TEST_F(DataflowAnalysisContextTest, JoinFlowConditions) {
auto &C1 = A.makeAtomRef(A.makeAtom());
auto &C2 = A.makeAtomRef(A.makeAtom());
auto &C3 = A.makeAtomRef(A.makeAtom());
Atom FC1 = A.makeFlowConditionToken();
Context.addFlowConditionConstraint(FC1, C1);
Context.addFlowConditionConstraint(FC1, C3);
Atom FC2 = A.makeFlowConditionToken();
Context.addFlowConditionConstraint(FC2, C2);
Context.addFlowConditionConstraint(FC2, C3);
Atom FC3 = Context.joinFlowConditions(FC1, FC2);
EXPECT_FALSE(Context.flowConditionImplies(FC3, C1));
EXPECT_FALSE(Context.flowConditionImplies(FC3, C2));
EXPECT_TRUE(Context.flowConditionImplies(FC3, C3));
}
TEST_F(DataflowAnalysisContextTest, FlowConditionTautologies) {
// Fresh flow condition with empty/no constraints is always true.
Atom FC1 = A.makeFlowConditionToken();
EXPECT_TRUE(Context.flowConditionIsTautology(FC1));
// Literal `true` is always true.
Atom FC2 = A.makeFlowConditionToken();
Context.addFlowConditionConstraint(FC2, A.makeLiteral(true));
EXPECT_TRUE(Context.flowConditionIsTautology(FC2));
// Literal `false` is never true.
Atom FC3 = A.makeFlowConditionToken();
Context.addFlowConditionConstraint(FC3, A.makeLiteral(false));
EXPECT_FALSE(Context.flowConditionIsTautology(FC3));
// We can't prove that an arbitrary bool A is always true...
auto &C1 = A.makeAtomRef(A.makeAtom());
Atom FC4 = A.makeFlowConditionToken();
Context.addFlowConditionConstraint(FC4, C1);
EXPECT_FALSE(Context.flowConditionIsTautology(FC4));
// ... but we can prove A || !A is true.
Atom FC5 = A.makeFlowConditionToken();
Context.addFlowConditionConstraint(FC5, A.makeOr(C1, A.makeNot(C1)));
EXPECT_TRUE(Context.flowConditionIsTautology(FC5));
}
TEST_F(DataflowAnalysisContextTest, EquivBoolVals) {
auto &X = A.makeAtomRef(A.makeAtom());
auto &Y = A.makeAtomRef(A.makeAtom());
auto &Z = A.makeAtomRef(A.makeAtom());
auto &True = A.makeLiteral(true);
auto &False = A.makeLiteral(false);
// X == X
EXPECT_TRUE(Context.equivalentFormulas(X, X));
// X != Y
EXPECT_FALSE(Context.equivalentFormulas(X, Y));
// !X != X
EXPECT_FALSE(Context.equivalentFormulas(A.makeNot(X), X));
// !(!X) = X
EXPECT_TRUE(Context.equivalentFormulas(A.makeNot(A.makeNot(X)), X));
// (X || X) == X
EXPECT_TRUE(Context.equivalentFormulas(A.makeOr(X, X), X));
// (X || Y) != X
EXPECT_FALSE(Context.equivalentFormulas(A.makeOr(X, Y), X));
// (X || True) == True
EXPECT_TRUE(Context.equivalentFormulas(A.makeOr(X, True), True));
// (X || False) == X
EXPECT_TRUE(Context.equivalentFormulas(A.makeOr(X, False), X));
// (X && X) == X
EXPECT_TRUE(Context.equivalentFormulas(A.makeAnd(X, X), X));
// (X && Y) != X
EXPECT_FALSE(Context.equivalentFormulas(A.makeAnd(X, Y), X));
// (X && True) == X
EXPECT_TRUE(Context.equivalentFormulas(A.makeAnd(X, True), X));
// (X && False) == False
EXPECT_TRUE(Context.equivalentFormulas(A.makeAnd(X, False), False));
// (X || Y) == (Y || X)
EXPECT_TRUE(Context.equivalentFormulas(A.makeOr(X, Y), A.makeOr(Y, X)));
// (X && Y) == (Y && X)
EXPECT_TRUE(Context.equivalentFormulas(A.makeAnd(X, Y), A.makeAnd(Y, X)));
// ((X || Y) || Z) == (X || (Y || Z))
EXPECT_TRUE(Context.equivalentFormulas(A.makeOr(A.makeOr(X, Y), Z),
A.makeOr(X, A.makeOr(Y, Z))));
// ((X && Y) && Z) == (X && (Y && Z))
EXPECT_TRUE(Context.equivalentFormulas(A.makeAnd(A.makeAnd(X, Y), Z),
A.makeAnd(X, A.makeAnd(Y, Z))));
}
} // namespace
|