1 //===- unittests/Analysis/FlowSensitive/DataflowEnvironmentTest.cpp -------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" 10 #include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h" 11 #include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h" 12 #include "gmock/gmock.h" 13 #include "gtest/gtest.h" 14 #include <memory> 15 16 namespace { 17 18 using namespace clang; 19 using namespace dataflow; 20 21 class EnvironmentTest : public ::testing::Test { 22 DataflowAnalysisContext Context; 23 24 protected: 25 EnvironmentTest() 26 : Context(std::make_unique<WatchedLiteralsSolver>()), Env(Context) {} 27 28 Environment Env; 29 }; 30 31 TEST_F(EnvironmentTest, MakeImplicationReturnsTrueGivenSameArgs) { 32 auto &X = Env.makeAtomicBoolValue(); 33 auto &XEqX = Env.makeImplication(X, X); 34 EXPECT_EQ(&XEqX, &Env.getBoolLiteralValue(true)); 35 } 36 37 TEST_F(EnvironmentTest, MakeIffReturnsTrueGivenSameArgs) { 38 auto &X = Env.makeAtomicBoolValue(); 39 auto &XEqX = Env.makeIff(X, X); 40 EXPECT_EQ(&XEqX, &Env.getBoolLiteralValue(true)); 41 } 42 43 TEST_F(EnvironmentTest, FlowCondition) { 44 EXPECT_TRUE(Env.flowConditionImplies(Env.getBoolLiteralValue(true))); 45 EXPECT_FALSE(Env.flowConditionImplies(Env.getBoolLiteralValue(false))); 46 47 auto &X = Env.makeAtomicBoolValue(); 48 EXPECT_FALSE(Env.flowConditionImplies(X)); 49 50 Env.addToFlowCondition(X); 51 EXPECT_TRUE(Env.flowConditionImplies(X)); 52 53 auto &NotX = Env.makeNot(X); 54 EXPECT_FALSE(Env.flowConditionImplies(NotX)); 55 } 56 57 } // namespace 58