[clang] 65dca4c - [clang][Interp] Fix tests for ignored expressions

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 1 05:59:16 PDT 2023


Author: Timm Bäder
Date: 2023-08-01T14:58:46+02:00
New Revision: 65dca4cbcbfc5c4447207eff7d223005089e9fed

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

LOG: [clang][Interp] Fix tests for ignored expressions

We didn't call the function explicitly in a static_assert() statement
and the checkPotentialConstantExpression() invocation quietly aborted
early because of the missing initializer for A::a.

Fix this by calling ignoredExprs() explicitly.

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

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 40a06b3b80ac0e..82f1e7a74b22e6 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -144,6 +144,8 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
   case CK_NoOp:
   case CK_UserDefinedConversion:
   case CK_BitCast:
+    if (DiscardResult)
+      return this->discard(SubExpr);
     return this->visit(SubExpr);
 
   case CK_IntegralToBoolean:
@@ -528,6 +530,9 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryExprOrTypeTraitExpr(
       Size = ASTCtx.getTypeSizeInChars(ArgType);
     }
 
+    if (DiscardResult)
+      return true;
+
     return this->emitConst(Size.getQuantity(), E);
   }
 
@@ -558,6 +563,9 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryExprOrTypeTraitExpr(
         Size = AlignOfType(Arg->getType(), ASTCtx, Kind);
     }
 
+    if (DiscardResult)
+      return true;
+
     return this->emitConst(Size.getQuantity(), E);
   }
 

diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index 64ba381057c458..d514ba9b654bef 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -193,6 +193,21 @@ namespace SizeOf {
                                             // ref-error {{must be initialized by a constant expression}}
   }
 
+#if __cplusplus >= 201402L
+  constexpr int IgnoredRejected() { // ref-error {{never produces a constant expression}}
+    int n = 0;
+    sizeof(int[n++]); // expected-warning {{expression result unused}} \
+                      // ref-warning {{expression result unused}} \
+                      // ref-note 2{{subexpression not valid in a constant expression}}
+    return n;
+  }
+  /// FIXME: This is rejected because the parameter so sizeof() is not constant.
+  ///   produce a proper diagnostic.
+  static_assert(IgnoredRejected() == 0, ""); // expected-error {{not an integral constant expression}} \
+                                             // ref-error {{not an integral constant expression}} \
+                                             // ref-note {{in call to 'IgnoredRejected()'}}
+#endif
+
 
 #if __cplusplus >= 202002L
   /// FIXME: The following code should be accepted.
@@ -883,7 +898,7 @@ namespace DiscardExprs {
   struct A{ int a; };
   constexpr int ignoredExprs() {
     (void)(1 / 2);
-    A a;
+    A a{12};
     a;
     (void)a;
     (a);
@@ -903,9 +918,12 @@ namespace DiscardExprs {
     arr[0];
     "a";
     'b';
+    sizeof(int);
+    alignof(int);
 
     return 0;
   }
+  static_assert(ignoredExprs() == 0, "");
 
   constexpr int oh_my(int x) {
     (int){ x++ };


        


More information about the cfe-commits mailing list