Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][bytecode] Reject void InitListExpr differently #105802

Merged
merged 1 commit into from
Aug 23, 2024

Conversation

tbaederr
Copy link
Contributor

This reverts c79d1fa and 125aa10

Instead, use the previous approach but allow void-typed InitListExprs with 0 initializers.

This reverts c79d1fa
and 125aa10

Instead, use the previous approach but allow void-typed InitListExprs
with 0 initializers.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 23, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

This reverts c79d1fa and 125aa10

Instead, use the previous approach but allow void-typed InitListExprs with 0 initializers.


Full diff: https://github.com/llvm/llvm-project/pull/105802.diff

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Compiler.cpp (+16-10)
  • (modified) clang/test/AST/ByteCode/c.c (+6-1)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 3a3927a9671345..b70d6cc8567fbe 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1344,6 +1344,16 @@ 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 (QT->isVoidType()) {
+    if (Inits.size() == 0)
+      return true;
+    return this->emitInvalid(E);
+  }
+
   // Handle discarding first.
   if (DiscardResult) {
     for (const Expr *Init : Inits) {
@@ -1353,13 +1363,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 +3278,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 +5177,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;
+}

@tbaederr tbaederr merged commit 7b4b85b into llvm:main Aug 23, 2024
9 of 11 checks passed
cjdb pushed a commit to cjdb/llvm-project that referenced this pull request Aug 23, 2024
This reverts c79d1fa and
125aa10

Instead, use the previous approach but allow void-typed InitListExprs
with 0 initializers.
dmpolukhin pushed a commit to dmpolukhin/llvm-project that referenced this pull request Sep 2, 2024
This reverts c79d1fa and
125aa10

Instead, use the previous approach but allow void-typed InitListExprs
with 0 initializers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants