Skip to content

Commit 1210738

Browse files
authored
[clang][Interp] Handle CXXScalarValueInitExprs (#67147)
Handle those by just zero-initializing the result.
1 parent 80fa5a6 commit 1210738

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,12 @@ bool ByteCodeExprGen<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) {
15311531
return this->emitOffsetOf(T, E, E);
15321532
}
15331533

1534+
template <class Emitter>
1535+
bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
1536+
const CXXScalarValueInitExpr *E) {
1537+
return this->visitZeroInitializer(E->getType(), E);
1538+
}
1539+
15341540
template <class Emitter> bool ByteCodeExprGen<Emitter>::discard(const Expr *E) {
15351541
if (E->containsErrors())
15361542
return false;

clang/lib/AST/Interp/ByteCodeExprGen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
106106
bool VisitCXXConstructExpr(const CXXConstructExpr *E);
107107
bool VisitSourceLocExpr(const SourceLocExpr *E);
108108
bool VisitOffsetOfExpr(const OffsetOfExpr *E);
109+
bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);
109110

110111
protected:
111112
bool visitExpr(const Expr *E) override;

clang/test/AST/Interp/literals.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ static_assert(b, "");
3131
constexpr int one = true;
3232
static_assert(one == 1, "");
3333

34+
constexpr bool b2 = bool();
35+
static_assert(!b2, "");
36+
37+
namespace ScalarTypes {
38+
constexpr int ScalarInitInt = int();
39+
static_assert(ScalarInitInt == 0, "");
40+
constexpr float ScalarInitFloat = float();
41+
static_assert(ScalarInitFloat == 0.0f, "");
42+
43+
static_assert(decltype(nullptr)() == nullptr, "");
44+
45+
template<typename T>
46+
constexpr T getScalar() { return T(); }
47+
48+
static_assert(getScalar<const int>() == 0, "");
49+
static_assert(getScalar<const double>() == 0.0, "");
50+
51+
static_assert(getScalar<void*>() == nullptr, "");
52+
static_assert(getScalar<void(*)(void)>() == nullptr, "");
53+
54+
enum E {
55+
First = 0,
56+
};
57+
static_assert(getScalar<E>() == First, "");
58+
/// FIXME: Member pointers.
59+
}
60+
3461
namespace IntegralCasts {
3562
constexpr int i = 12;
3663
constexpr unsigned int ui = i;

0 commit comments

Comments
 (0)