Debian Codegraph @

clang/unittests/AST/Interp/toAPValue.cpp

raw
1 #include "../../../lib/AST/Interp/Context.h"
2 #include "../../../lib/AST/Interp/Descriptor.h"
3 #include "../../../lib/AST/Interp/Program.h"
4 #include "clang/AST/ASTContext.h"
5 #include "clang/AST/Decl.h"
6 #include "clang/ASTMatchers/ASTMatchFinder.h"
7 #include "clang/ASTMatchers/ASTMatchers.h"
8 #include "clang/Tooling/Tooling.h"
9 #include "gtest/gtest.h"
10
11 using namespace clang;
12 using namespace clang::interp;
13 using namespace clang::ast_matchers;
14
15 /// Test the various toAPValue implementations.
16 TEST(ToAPValue, Pointers) {
17 constexpr char Code[] =
18 "struct A { bool a; bool z; };\n"
19 "struct S {\n"
20 " A a[3];\n"
21 "};\n"
22 "constexpr S d = {{{true, false}, {false, true}, {false, false}}};\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";
26
27 auto AST = tooling::buildASTFromCodeWithArgs(
28 Code, {"-fexperimental-new-constant-interpreter"});
29
30 auto &ASTCtx = AST->getASTContext();
31 auto &Ctx = AST->getASTContext().getInterpContext();
32 Program &Prog = Ctx.getProgram();
33
34 auto getDecl = [&](const char *Name) -> const ValueDecl * {
35 auto Nodes =
36 match(valueDecl(hasName(Name)).bind("var"), AST->getASTContext());
37 assert(Nodes.size() == 1);
38 const auto *D = Nodes[0].getNodeAs<ValueDecl>("var");
39 assert(D);
40 return D;
41 };
42 auto getGlobalPtr = [&](const char *Name) -> Pointer {
43 const VarDecl *D = cast<VarDecl>(getDecl(Name));
44 return Prog.getPtrGlobal(*Prog.getGlobal(D));
45 };
46
47 {
48 const Pointer &GP = getGlobalPtr("b");
49 const Pointer &P = GP.deref<Pointer>();
50 ASSERT_TRUE(P.isLive());
51 APValue A = P.toAPValue(ASTCtx);
52 ASSERT_TRUE(A.isLValue());
53 ASSERT_TRUE(A.hasLValuePath());
54 const auto &Path = A.getLValuePath();
55 ASSERT_EQ(Path.size(), 3u);
56 ASSERT_EQ(A.getLValueBase(), getDecl("d"));
57 // FIXME: Also test all path elements.
58 }
59
60 {
61 const ValueDecl *D = getDecl("p");
62 ASSERT_NE(D, nullptr);
63 const Pointer &GP = getGlobalPtr("p");
64 const Pointer &P = GP.deref<Pointer>();
65 ASSERT_TRUE(P.isIntegralPointer());
66 APValue A = P.toAPValue(ASTCtx);
67 ASSERT_TRUE(A.isLValue());
68 ASSERT_TRUE(A.getLValueBase().isNull());
69 APSInt I;
70 bool Success = A.toIntegralConstant(I, D->getType(), AST->getASTContext());
71 ASSERT_TRUE(Success);
72 ASSERT_EQ(I, 12);
73 }
74
75 {
76 const ValueDecl *D = getDecl("nullp");
77 ASSERT_NE(D, nullptr);
78 const Pointer &GP = getGlobalPtr("nullp");
79 const Pointer &P = GP.deref<Pointer>();
80 ASSERT_TRUE(P.isIntegralPointer());
81 APValue A = P.toAPValue(ASTCtx);
82 ASSERT_TRUE(A.isLValue());
83 ASSERT_TRUE(A.getLValueBase().isNull());
84 ASSERT_TRUE(A.isNullPointer());
85 APSInt I;
86 bool Success = A.toIntegralConstant(I, D->getType(), AST->getASTContext());
87 ASSERT_TRUE(Success);
88 ASSERT_EQ(I, 0);
89 }
90 }
91
92 TEST(ToAPValue, FunctionPointers) {
93 constexpr char Code[] = " constexpr bool foo() { return true; }\n"
94 " constexpr bool (*func)() = foo;\n"
95 " constexpr bool (*nullp)() = nullptr;\n";
96
97 auto AST = tooling::buildASTFromCodeWithArgs(
98 Code, {"-fexperimental-new-constant-interpreter"});
99
100 auto &ASTCtx = AST->getASTContext();
101 auto &Ctx = AST->getASTContext().getInterpContext();
102 Program &Prog = Ctx.getProgram();
103
104 auto getDecl = [&](const char *Name) -> const ValueDecl * {
105 auto Nodes =
106 match(valueDecl(hasName(Name)).bind("var"), AST->getASTContext());
107 assert(Nodes.size() == 1);
108 const auto *D = Nodes[0].getNodeAs<ValueDecl>("var");
109 assert(D);
110 return D;
111 };
112
113 auto getGlobalPtr = [&](const char *Name) -> Pointer {
114 const VarDecl *D = cast<VarDecl>(getDecl(Name));
115 return Prog.getPtrGlobal(*Prog.getGlobal(D));
116 };
117
118 {
119 const Pointer &GP = getGlobalPtr("func");
120 const FunctionPointer &FP = GP.deref<FunctionPointer>();
121 ASSERT_FALSE(FP.isZero());
122 APValue A = FP.toAPValue(ASTCtx);
123 ASSERT_TRUE(A.hasValue());
124 ASSERT_TRUE(A.isLValue());
125 ASSERT_TRUE(A.hasLValuePath());
126 const auto &Path = A.getLValuePath();
127 ASSERT_EQ(Path.size(), 0u);
128 ASSERT_FALSE(A.getLValueBase().isNull());
129 ASSERT_EQ(A.getLValueBase().dyn_cast<const ValueDecl *>(), getDecl("foo"));
130 }
131
132 {
133 const ValueDecl *D = getDecl("nullp");
134 ASSERT_NE(D, nullptr);
135 const Pointer &GP = getGlobalPtr("nullp");
136 const auto &P = GP.deref<FunctionPointer>();
137 APValue A = P.toAPValue(ASTCtx);
138 ASSERT_TRUE(A.isLValue());
139 ASSERT_TRUE(A.getLValueBase().isNull());
140 ASSERT_TRUE(A.isNullPointer());
141 APSInt I;
142 bool Success = A.toIntegralConstant(I, D->getType(), AST->getASTContext());
143 ASSERT_TRUE(Success);
144 ASSERT_EQ(I, 0);
145 }
146 }
147
148 TEST(ToAPValue, FunctionPointersC) {
149 // NB: The declaration of func2 is useless, but it makes us register a global
150 // variable for func.
151 constexpr char Code[] = "const int (* const func)(int *) = (void*)17;\n"
152 "const int (*func2)(int *) = func;\n";
153 auto AST = tooling::buildASTFromCodeWithArgs(
154 Code, {"-x", "c", "-fexperimental-new-constant-interpreter"});
155
156 auto &ASTCtx = AST->getASTContext();
157 auto &Ctx = AST->getASTContext().getInterpContext();
158 Program &Prog = Ctx.getProgram();
159
160 auto getDecl = [&](const char *Name) -> const ValueDecl * {
161 auto Nodes =
162 match(valueDecl(hasName(Name)).bind("var"), AST->getASTContext());
163 assert(Nodes.size() == 1);
164 const auto *D = Nodes[0].getNodeAs<ValueDecl>("var");
165 assert(D);
166 return D;
167 };
168
169 auto getGlobalPtr = [&](const char *Name) -> Pointer {
170 const VarDecl *D = cast<VarDecl>(getDecl(Name));
171 return Prog.getPtrGlobal(*Prog.getGlobal(D));
172 };
173
174 {
175 const ValueDecl *D = getDecl("func");
176 const Pointer &GP = getGlobalPtr("func");
177 ASSERT_TRUE(GP.isLive());
178 const FunctionPointer &FP = GP.deref<FunctionPointer>();
179 ASSERT_FALSE(FP.isZero());
180 APValue A = FP.toAPValue(ASTCtx);
181 ASSERT_TRUE(A.hasValue());
182 ASSERT_TRUE(A.isLValue());
183 const auto &Path = A.getLValuePath();
184 ASSERT_EQ(Path.size(), 0u);
185 ASSERT_TRUE(A.getLValueBase().isNull());
186 APSInt I;
187 bool Success = A.toIntegralConstant(I, D->getType(), AST->getASTContext());
188 ASSERT_TRUE(Success);
189 ASSERT_EQ(I, 17);
190 }
191 }
192
193 TEST(ToAPValue, MemberPointers) {
194 constexpr char Code[] = "struct S {\n"
195 " int m, n;\n"
196 "};\n"
197 "constexpr int S::*pm = &S::m;\n"
198 "constexpr int S::*nn = nullptr;\n";
199
200 auto AST = tooling::buildASTFromCodeWithArgs(
201 Code, {"-fexperimental-new-constant-interpreter"});
202
203 auto &ASTCtx = AST->getASTContext();
204 auto &Ctx = AST->getASTContext().getInterpContext();
205 Program &Prog = Ctx.getProgram();
206
207 auto getDecl = [&](const char *Name) -> const ValueDecl * {
208 auto Nodes =
209 match(valueDecl(hasName(Name)).bind("var"), AST->getASTContext());
210 assert(Nodes.size() == 1);
211 const auto *D = Nodes[0].getNodeAs<ValueDecl>("var");
212 assert(D);
213 return D;
214 };
215
216 auto getGlobalPtr = [&](const char *Name) -> Pointer {
217 const VarDecl *D = cast<VarDecl>(getDecl(Name));
218 return Prog.getPtrGlobal(*Prog.getGlobal(D));
219 };
220
221 {
222 const Pointer &GP = getGlobalPtr("pm");
223 ASSERT_TRUE(GP.isLive());
224 const MemberPointer &FP = GP.deref<MemberPointer>();
225 APValue A = FP.toAPValue(ASTCtx);
226 ASSERT_EQ(A.getMemberPointerDecl(), getDecl("m"));
227 ASSERT_EQ(A.getKind(), APValue::MemberPointer);
228 }
229
230 {
231 const Pointer &GP = getGlobalPtr("nn");
232 ASSERT_TRUE(GP.isLive());
233 const MemberPointer &NP = GP.deref<MemberPointer>();
234 ASSERT_TRUE(NP.isZero());
235 APValue A = NP.toAPValue(ASTCtx);
236 ASSERT_EQ(A.getKind(), APValue::MemberPointer);
237 }
238 }