Skip to content

Commit 2ea7ec9

Browse files
committed
[clang][Interp][NFC] Expand pointer unittests
Test integral pointers as well.
1 parent 8c3cb6b commit 2ea7ec9

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

clang/unittests/AST/Interp/toAPValue.cpp

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ TEST(ToAPValue, Pointers) {
2020
" A a[3];\n"
2121
"};\n"
2222
"constexpr S d = {{{true, false}, {false, true}, {false, false}}};\n"
23-
"constexpr const bool *b = &d.a[1].z;\n";
23+
"constexpr const bool *b = &d.a[1].z;\n"
24+
"const void *p = (void*)12;\n"
25+
"const void *nullp = (void*)0;\n";
2426

2527
auto AST = tooling::buildASTFromCodeWithArgs(
2628
Code, {"-fexperimental-new-constant-interpreter"});
@@ -41,15 +43,49 @@ TEST(ToAPValue, Pointers) {
4143
return Prog.getPtrGlobal(*Prog.getGlobal(D));
4244
};
4345

44-
const Pointer &GP = getGlobalPtr("b");
45-
const Pointer &P = GP.deref<Pointer>();
46-
ASSERT_TRUE(P.isLive());
47-
APValue A = P.toAPValue();
48-
ASSERT_TRUE(A.isLValue());
49-
ASSERT_TRUE(A.hasLValuePath());
50-
const auto &Path = A.getLValuePath();
51-
ASSERT_EQ(Path.size(), 3u);
52-
ASSERT_EQ(A.getLValueBase(), getDecl("d"));
46+
{
47+
const Pointer &GP = getGlobalPtr("b");
48+
const Pointer &P = GP.deref<Pointer>();
49+
ASSERT_TRUE(P.isLive());
50+
APValue A = P.toAPValue();
51+
ASSERT_TRUE(A.isLValue());
52+
ASSERT_TRUE(A.hasLValuePath());
53+
const auto &Path = A.getLValuePath();
54+
ASSERT_EQ(Path.size(), 3u);
55+
ASSERT_EQ(A.getLValueBase(), getDecl("d"));
56+
// FIXME: Also test all path elements.
57+
}
58+
59+
{
60+
const ValueDecl *D = getDecl("p");
61+
ASSERT_NE(D, nullptr);
62+
const Pointer &GP = getGlobalPtr("p");
63+
const Pointer &P = GP.deref<Pointer>();
64+
ASSERT_TRUE(P.isIntegralPointer());
65+
APValue A = P.toAPValue();
66+
ASSERT_TRUE(A.isLValue());
67+
ASSERT_TRUE(A.getLValueBase().isNull());
68+
APSInt I;
69+
bool Success = A.toIntegralConstant(I, D->getType(), AST->getASTContext());
70+
ASSERT_TRUE(Success);
71+
ASSERT_EQ(I, 12);
72+
}
73+
74+
{
75+
const ValueDecl *D = getDecl("nullp");
76+
ASSERT_NE(D, nullptr);
77+
const Pointer &GP = getGlobalPtr("nullp");
78+
const Pointer &P = GP.deref<Pointer>();
79+
ASSERT_TRUE(P.isIntegralPointer());
80+
APValue A = P.toAPValue();
81+
ASSERT_TRUE(A.isLValue());
82+
ASSERT_TRUE(A.getLValueBase().isNull());
83+
ASSERT_TRUE(A.isNullPointer());
84+
APSInt I;
85+
bool Success = A.toIntegralConstant(I, D->getType(), AST->getASTContext());
86+
ASSERT_TRUE(Success);
87+
ASSERT_EQ(I, 0);
88+
}
5389
}
5490

5591
TEST(ToAPValue, FunctionPointers) {

0 commit comments

Comments
 (0)