[PATCH] D148925: Fix discarding void-typed comma expressions

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 21 07:49:56 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, tahonermann, shafik, erichkeane.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

First, we need to handle void types in `visitExpr`, so we don't run into an assertion there when we try to pop a return value from the stack that isn't there.

Secondly, we need to handle it when visiting comma expressions so we don't do the same thing there.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148925

Files:
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/test/AST/Interp/literals.cpp


Index: clang/test/AST/Interp/literals.cpp
===================================================================
--- clang/test/AST/Interp/literals.cpp
+++ clang/test/AST/Interp/literals.cpp
@@ -791,6 +791,8 @@
   (a); // expected-warning {{unused}} \
        // ref-warning {{unused}}
 
+  (void)5, (void)6;
+
   return 0;
 }
 
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -264,8 +264,12 @@
 
   // Deal with operations which have composite or void types.
   if (BO->isCommaOp()) {
-    if (!discard(LHS))
+    if (this->discard(LHS))
       return false;
+    if (RHS->getType()->isVoidType())
+      return this->discard(RHS);
+
+    // Otherwise, visit RHS and optionally discard its value.
     return Discard(this->visit(RHS));
   }
 
@@ -1650,10 +1654,12 @@
   if (!visit(Exp))
     return false;
 
+  if (Exp->getType()->isVoidType())
+    return this->emitRetVoid(Exp);
+
   if (std::optional<PrimType> T = classify(Exp))
     return this->emitRet(*T, Exp);
-  else
-    return this->emitRetValue(Exp);
+  return this->emitRetValue(Exp);
 }
 
 /// Toplevel visitDecl().


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148925.515733.patch
Type: text/x-patch
Size: 1240 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230421/315da765/attachment-0001.bin>


More information about the cfe-commits mailing list