xref: /llvm-project/clang/unittests/Analysis/FlowSensitive/DebugSupportTest.cpp (revision 7338eb561c48803ec244cd6116154163f56e9717)
1c9666d23SWei Yi Tee //===- unittests/Analysis/FlowSensitive/DebugSupportTest.cpp --------------===//
2c9666d23SWei Yi Tee //
3c9666d23SWei Yi Tee // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c9666d23SWei Yi Tee // See https://llvm.org/LICENSE.txt for license information.
5c9666d23SWei Yi Tee // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c9666d23SWei Yi Tee //
7c9666d23SWei Yi Tee //===----------------------------------------------------------------------===//
8c9666d23SWei Yi Tee 
9c9666d23SWei Yi Tee #include "clang/Analysis/FlowSensitive/DebugSupport.h"
10c9666d23SWei Yi Tee #include "TestingSupport.h"
11fc9821a8SSam McCall #include "clang/Analysis/FlowSensitive/Formula.h"
12fc9821a8SSam McCall #include "llvm/Support/ScopedPrinter.h"
13fc9821a8SSam McCall #include "llvm/Support/raw_ostream.h"
14c9666d23SWei Yi Tee #include "gmock/gmock.h"
15c9666d23SWei Yi Tee #include "gtest/gtest.h"
16c9666d23SWei Yi Tee 
17c9666d23SWei Yi Tee namespace {
18c9666d23SWei Yi Tee 
19c9666d23SWei Yi Tee using namespace clang;
20c9666d23SWei Yi Tee using namespace dataflow;
21c9666d23SWei Yi Tee 
22c9666d23SWei Yi Tee using test::ConstraintContext;
23c9666d23SWei Yi Tee using testing::StrEq;
24c9666d23SWei Yi Tee 
TEST(BoolValueDebugStringTest,AtomicBoolean)25c9666d23SWei Yi Tee TEST(BoolValueDebugStringTest, AtomicBoolean) {
26c9666d23SWei Yi Tee   ConstraintContext Ctx;
27c9666d23SWei Yi Tee   auto B = Ctx.atom();
28c9666d23SWei Yi Tee 
29fc9821a8SSam McCall   auto Expected = "V0";
30fc9821a8SSam McCall   EXPECT_THAT(llvm::to_string(*B), StrEq(Expected));
3171579569SSam McCall }
3271579569SSam McCall 
TEST(BoolValueDebugStringTest,Literal)33*7338eb56SSam McCall TEST(BoolValueDebugStringTest, Literal) {
34*7338eb56SSam McCall   ConstraintContext Ctx;
35*7338eb56SSam McCall   EXPECT_EQ("true", llvm::to_string(*Ctx.literal(true)));
36*7338eb56SSam McCall   EXPECT_EQ("false", llvm::to_string(*Ctx.literal(false)));
37*7338eb56SSam McCall }
38*7338eb56SSam McCall 
TEST(BoolValueDebugStringTest,Negation)39c9666d23SWei Yi Tee TEST(BoolValueDebugStringTest, Negation) {
40c9666d23SWei Yi Tee   ConstraintContext Ctx;
41c9666d23SWei Yi Tee   auto B = Ctx.neg(Ctx.atom());
42c9666d23SWei Yi Tee 
43fc9821a8SSam McCall   auto Expected = "!V0";
44fc9821a8SSam McCall   EXPECT_THAT(llvm::to_string(*B), StrEq(Expected));
45c9666d23SWei Yi Tee }
46c9666d23SWei Yi Tee 
TEST(BoolValueDebugStringTest,Conjunction)47c9666d23SWei Yi Tee TEST(BoolValueDebugStringTest, Conjunction) {
48c9666d23SWei Yi Tee   ConstraintContext Ctx;
49fc9821a8SSam McCall   auto *V0 = Ctx.atom();
50fc9821a8SSam McCall   auto *V1 = Ctx.atom();
51fc9821a8SSam McCall   EXPECT_EQ("(V0 & V1)", llvm::to_string(*Ctx.conj(V0, V1)));
52c9666d23SWei Yi Tee }
53c9666d23SWei Yi Tee 
TEST(BoolValueDebugStringTest,Disjunction)54c9666d23SWei Yi Tee TEST(BoolValueDebugStringTest, Disjunction) {
55c9666d23SWei Yi Tee   ConstraintContext Ctx;
56fc9821a8SSam McCall   auto *V0 = Ctx.atom();
57fc9821a8SSam McCall   auto *V1 = Ctx.atom();
58fc9821a8SSam McCall   EXPECT_EQ("(V0 | V1)", llvm::to_string(*Ctx.disj(V0, V1)));
59c9666d23SWei Yi Tee }
60c9666d23SWei Yi Tee 
TEST(BoolValueDebugStringTest,Implication)61c9666d23SWei Yi Tee TEST(BoolValueDebugStringTest, Implication) {
62c9666d23SWei Yi Tee   ConstraintContext Ctx;
63fc9821a8SSam McCall   auto *V0 = Ctx.atom();
64fc9821a8SSam McCall   auto *V1 = Ctx.atom();
65fc9821a8SSam McCall   EXPECT_EQ("(V0 => V1)", llvm::to_string(*Ctx.impl(V0, V1)));
66c9666d23SWei Yi Tee }
67c9666d23SWei Yi Tee 
TEST(BoolValueDebugStringTest,Iff)68c9666d23SWei Yi Tee TEST(BoolValueDebugStringTest, Iff) {
69c9666d23SWei Yi Tee   ConstraintContext Ctx;
70fc9821a8SSam McCall   auto *V0 = Ctx.atom();
71fc9821a8SSam McCall   auto *V1 = Ctx.atom();
72fc9821a8SSam McCall   EXPECT_EQ("(V0 = V1)", llvm::to_string(*Ctx.iff(V0, V1)));
73c9666d23SWei Yi Tee }
74c9666d23SWei Yi Tee 
TEST(BoolValueDebugStringTest,Xor)75c9666d23SWei Yi Tee TEST(BoolValueDebugStringTest, Xor) {
76c9666d23SWei Yi Tee   ConstraintContext Ctx;
77fc9821a8SSam McCall   auto V0 = Ctx.atom();
78fc9821a8SSam McCall   auto V1 = Ctx.atom();
79fc9821a8SSam McCall   auto B = Ctx.disj(Ctx.conj(V0, Ctx.neg(V1)), Ctx.conj(Ctx.neg(V0), V1));
80c9666d23SWei Yi Tee 
81fc9821a8SSam McCall   auto Expected = "((V0 & !V1) | (!V0 & V1))";
82fc9821a8SSam McCall   EXPECT_THAT(llvm::to_string(*B), StrEq(Expected));
83c9666d23SWei Yi Tee }
84c9666d23SWei Yi Tee 
TEST(BoolValueDebugStringTest,NestedBoolean)85c9666d23SWei Yi Tee TEST(BoolValueDebugStringTest, NestedBoolean) {
86c9666d23SWei Yi Tee   ConstraintContext Ctx;
87fc9821a8SSam McCall   auto V0 = Ctx.atom();
88fc9821a8SSam McCall   auto V1 = Ctx.atom();
89fc9821a8SSam McCall   auto V2 = Ctx.atom();
90fc9821a8SSam McCall   auto V3 = Ctx.atom();
91fc9821a8SSam McCall   auto V4 = Ctx.atom();
92fc9821a8SSam McCall   auto B = Ctx.conj(V0, Ctx.disj(V1, Ctx.conj(V2, Ctx.disj(V3, V4))));
93c9666d23SWei Yi Tee 
94fc9821a8SSam McCall   auto Expected = "(V0 & (V1 | (V2 & (V3 | V4))))";
95fc9821a8SSam McCall   EXPECT_THAT(llvm::to_string(*B), StrEq(Expected));
96c9666d23SWei Yi Tee }
97c9666d23SWei Yi Tee 
TEST(BoolValueDebugStringTest,ComplexBooleanWithSomeNames)98c9666d23SWei Yi Tee TEST(BoolValueDebugStringTest, ComplexBooleanWithSomeNames) {
99c9666d23SWei Yi Tee   ConstraintContext Ctx;
100*7338eb56SSam McCall   auto X = Ctx.atom();
101*7338eb56SSam McCall   auto Y = Ctx.atom();
102*7338eb56SSam McCall   Formula::AtomNames Names;
103*7338eb56SSam McCall   Names[X->getAtom()] = "X";
104*7338eb56SSam McCall   Names[Y->getAtom()] = "Y";
1053353f7ddSDouglas Yung   auto V2 = Ctx.atom();
1063353f7ddSDouglas Yung   auto V3 = Ctx.atom();
107*7338eb56SSam McCall   auto B = Ctx.disj(Ctx.conj(Y, V2), Ctx.disj(X, V3));
108c9666d23SWei Yi Tee 
109*7338eb56SSam McCall   auto Expected = R"(((Y & V2) | (X | V3)))";
110fc9821a8SSam McCall   std::string Actual;
111fc9821a8SSam McCall   llvm::raw_string_ostream OS(Actual);
112fc9821a8SSam McCall   B->print(OS, &Names);
113fc9821a8SSam McCall   EXPECT_THAT(Actual, StrEq(Expected));
114c9666d23SWei Yi Tee }
115c9666d23SWei Yi Tee 
116c9666d23SWei Yi Tee } // namespace
117