Skip to content

Commit

Permalink
[clang][Interp] Handle CXXScalarValueInitExprs (#67147)
Browse files Browse the repository at this point in the history
Handle those by just zero-initializing the result.
  • Loading branch information
tbaederr committed Oct 10, 2023
1 parent 80fa5a6 commit 1210738
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,12 @@ bool ByteCodeExprGen<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) {
return this->emitOffsetOf(T, E, E);
}

template <class Emitter>
bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
const CXXScalarValueInitExpr *E) {
return this->visitZeroInitializer(E->getType(), E);
}

template <class Emitter> bool ByteCodeExprGen<Emitter>::discard(const Expr *E) {
if (E->containsErrors())
return false;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/Interp/ByteCodeExprGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
bool VisitCXXConstructExpr(const CXXConstructExpr *E);
bool VisitSourceLocExpr(const SourceLocExpr *E);
bool VisitOffsetOfExpr(const OffsetOfExpr *E);
bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E);

protected:
bool visitExpr(const Expr *E) override;
Expand Down
27 changes: 27 additions & 0 deletions clang/test/AST/Interp/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ static_assert(b, "");
constexpr int one = true;
static_assert(one == 1, "");

constexpr bool b2 = bool();
static_assert(!b2, "");

namespace ScalarTypes {
constexpr int ScalarInitInt = int();
static_assert(ScalarInitInt == 0, "");
constexpr float ScalarInitFloat = float();
static_assert(ScalarInitFloat == 0.0f, "");

static_assert(decltype(nullptr)() == nullptr, "");

template<typename T>
constexpr T getScalar() { return T(); }

static_assert(getScalar<const int>() == 0, "");
static_assert(getScalar<const double>() == 0.0, "");

static_assert(getScalar<void*>() == nullptr, "");
static_assert(getScalar<void(*)(void)>() == nullptr, "");

enum E {
First = 0,
};
static_assert(getScalar<E>() == First, "");
/// FIXME: Member pointers.
}

namespace IntegralCasts {
constexpr int i = 12;
constexpr unsigned int ui = i;
Expand Down

0 comments on commit 1210738

Please sign in to comment.