[clang] aed9646 - [clang][Interp] Fix discarding void-typed comma expressions

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 25 22:45:18 PDT 2023


Author: Timm Bäder
Date: 2023-07-26T07:45:02+02:00
New Revision: aed9646d8e29c1c5c86be65a2fd97029edf628d9

URL: https://github.com/llvm/llvm-project/commit/aed9646d8e29c1c5c86be65a2fd97029edf628d9
DIFF: https://github.com/llvm/llvm-project/commit/aed9646d8e29c1c5c86be65a2fd97029edf628d9.diff

LOG: [clang][Interp] Fix discarding void-typed comma expressions

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.

Differential Revision: https://reviews.llvm.org/D148925

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 40792fa6f4a284..5e42cca6762104 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -223,8 +223,12 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
 
   // 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));
   }
 
@@ -1642,10 +1646,12 @@ bool ByteCodeExprGen<Emitter>::visitExpr(const Expr *Exp) {
   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().

diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index f0513810eb4b7e..cf5e86658e3b38 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -855,9 +855,23 @@ constexpr int ignoredExprs() {
   (a); // expected-warning {{unused}} \
        // ref-warning {{unused}}
 
+  (void)5, (void)6;
+
   return 0;
 }
 
+/// Ignored comma expressions still have their
+/// expressions evaluated.
+constexpr int Comma(int start) {
+    int i = start;
+
+    (void)i++;
+    (void)i++,(void)i++;
+    return i;
+}
+constexpr int Value = Comma(5);
+static_assert(Value == 8, "");
+
 #endif
 
 namespace PredefinedExprs {


        


More information about the cfe-commits mailing list