[clang] [clang][bytecode] Reject void InitListExpr differently (PR #105697)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 22 10:01:26 PDT 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/105697

This reverts c79d1fa540390f6e37e1ea326153559eeadd0de6 and 125aa10b3d645bd26523a1bc321bb2e6b1cf04e1

Instead, just only use emitInvalid() for void InitListExprs if we're not discarding them.

>From 719be4236d30a7ed41a8828deb6f82533882bfd1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 22 Aug 2024 18:52:27 +0200
Subject: [PATCH] [clang][bytecode] Reject void InitListExpr differently

This reverts c79d1fa540390f6e37e1ea326153559eeadd0de6
and 125aa10b3d645bd26523a1bc321bb2e6b1cf04e1

Instead, just only use emitInvalid() for void InitListExprs if
we're not discarding them.
---
 clang/lib/AST/ByteCode/Compiler.cpp | 24 ++++++++++++++----------
 clang/test/AST/ByteCode/c.c         |  7 ++++++-
 2 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 3a3927a9671345..1182762cb9bb4c 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1344,6 +1344,14 @@ bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
 template <class Emitter>
 bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
                                       const Expr *ArrayFiller, const Expr *E) {
+
+  QualType QT = E->getType();
+  if (const auto *AT = QT->getAs<AtomicType>())
+    QT = AT->getValueType();
+
+  if (!DiscardResult && QT->isVoidType())
+    return this->emitInvalid(E);
+
   // Handle discarding first.
   if (DiscardResult) {
     for (const Expr *Init : Inits) {
@@ -1353,13 +1361,6 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
     return true;
   }
 
-  QualType QT = E->getType();
-  if (const auto *AT = QT->getAs<AtomicType>())
-    QT = AT->getValueType();
-
-  if (QT->isVoidType())
-    return this->emitInvalid(E);
-
   // Primitive values.
   if (std::optional<PrimType> T = classify(QT)) {
     assert(!DiscardResult);
@@ -3275,9 +3276,12 @@ template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
   if (E->getType().isNull())
     return false;
 
+  if (E->getType()->isVoidType())
+    return this->discard(E);
+
   // Create local variable to hold the return value.
-  if (!E->getType()->isVoidType() && !E->isGLValue() &&
-      !E->getType()->isAnyComplexType() && !classify(E->getType())) {
+  if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
+      !classify(E->getType())) {
     std::optional<unsigned> LocalIndex = allocateLocal(E);
     if (!LocalIndex)
       return false;
@@ -5171,7 +5175,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
     // We should already have a pointer when we get here.
     return this->delegate(SubExpr);
   case UO_Deref: // *x
-    if (DiscardResult || E->getType()->isVoidType())
+    if (DiscardResult)
       return this->discard(SubExpr);
     return this->visit(SubExpr);
   case UO_Not: // ~x
diff --git a/clang/test/AST/ByteCode/c.c b/clang/test/AST/ByteCode/c.c
index 60f4d6ad1b2967..7cb7f96049f2de 100644
--- a/clang/test/AST/ByteCode/c.c
+++ b/clang/test/AST/ByteCode/c.c
@@ -298,7 +298,6 @@ void T1(void) {
 enum teste1 test1f(void), (*test1)(void) = test1f; // pedantic-warning {{ISO C forbids forward references to 'enum' types}}
 enum teste1 { TEST1 };
 
-
 void func(void) {
   _Static_assert(func + 1 - func == 1, ""); // pedantic-warning {{arithmetic on a pointer to the function type}} \
                                             // pedantic-warning {{arithmetic on pointers to the function type}} \
@@ -313,3 +312,9 @@ void func(void) {
   func - 0xdead000000000000UL; // all-warning {{expression result unused}} \
                                // pedantic-warning {{arithmetic on a pointer to the function type}}
 }
+
+void foo3 (void)
+{
+ void* x = 0;
+ void* y = &*x;
+}



More information about the cfe-commits mailing list