[clang] [clang][Interp] Handle CXXScalarValueInitExprs (PR #67147)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 23 23:07:31 PDT 2023
Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>
Message-ID:
In-Reply-To: <llvm/llvm-project/pull/67147/clang at github.com>
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/67147
>From 9c0cc7fde8473c9b02733720149343c4b2dcddae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 23 Sep 2023 11:41:52 +0200
Subject: [PATCH 1/2] [clang][Interp] Fix returning nullptr from functions
isLive() is false for null pointers, so we need to special-case
this here.
---
clang/lib/AST/Interp/Interp.h | 2 +-
clang/test/AST/Interp/functions.cpp | 7 +++++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/Interp/Interp.h b/clang/lib/AST/Interp/Interp.h
index 8453856e526a6b2..71d49a0894f4e35 100644
--- a/clang/lib/AST/Interp/Interp.h
+++ b/clang/lib/AST/Interp/Interp.h
@@ -209,7 +209,7 @@ bool Ret(InterpState &S, CodePtr &PC, APValue &Result) {
// FIXME: We could be calling isLive() here, but the emitted diagnostics
// seem a little weird, at least if the returned expression is of
// pointer type.
- if (!Ret.isLive())
+ if (!Ret.isZero() && !Ret.isLive())
return false;
}
diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 331df74d50b3d62..7f03271d152db5c 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -332,3 +332,10 @@ namespace InvalidReclRefs {
}
#endif
}
+
+namespace PtrReturn {
+ constexpr void *a() {
+ return nullptr;
+ }
+ static_assert(a() == nullptr, "");
+}
>From 163e0d978cd8b217ffe518a44221f512e9d5d524 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 22 Sep 2023 16:27:11 +0200
Subject: [PATCH 2/2] [clang][Interp] Handle CXXScalarValueInitExprs
---
clang/lib/AST/Interp/ByteCodeExprGen.cpp | 6 ++++++
clang/lib/AST/Interp/ByteCodeExprGen.h | 1 +
clang/test/AST/Interp/literals.cpp | 27 ++++++++++++++++++++++++
3 files changed, 34 insertions(+)
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index e813d4fa651ceaf..c804bab7ce567d0 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1488,6 +1488,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;
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.h b/clang/lib/AST/Interp/ByteCodeExprGen.h
index 47a3f75f13459d0..7cfe4d9251c5f05 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.h
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -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;
diff --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index aabc909b3328e48..5b1e14f80dbbf9f 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -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;
More information about the cfe-commits
mailing list