[PATCH] D136013: [clang][Interp] Fix ignoring unary operators and This exprs

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 15 00:58:40 PDT 2022


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

Randomly noticed this. We need to honor DiscardResult here.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136013

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


Index: clang/test/AST/Interp/records.cpp
===================================================================
--- clang/test/AST/Interp/records.cpp
+++ clang/test/AST/Interp/records.cpp
@@ -204,6 +204,21 @@
                                 // expected-error {{static assertion failed}} \
                                 // expected-note {{evaluates to '12 == 200'}}
 
+
+struct S {
+  constexpr void fo() const {
+    this; // expected-warning {{expression result unused}} \
+          // ref-warning {{expression result unused}}
+  }
+
+  constexpr int m() const {
+    fo();
+    return 1;
+  }
+};
+constexpr S s;
+static_assert(s.m() == 1, "");
+
 namespace MI {
   class A {
   public:
Index: clang/test/AST/Interp/literals.cpp
===================================================================
--- clang/test/AST/Interp/literals.cpp
+++ clang/test/AST/Interp/literals.cpp
@@ -279,6 +279,17 @@
   static_assert((12 | true) == 13, "");
 };
 
+
+#if __cplusplus >= 201402L
+constexpr bool IgnoredUnary() {
+  bool bo = true;
+  !bo; // expected-warning {{expression result unused}} \
+       // ref-warning {{expression result unused}}
+  return bo;
+}
+static_assert(IgnoredUnary(), "");
+#endif
+
 namespace floats {
   constexpr int i = 2;
   constexpr float f = 1.0f;
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1132,11 +1132,16 @@
 
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
+  if (DiscardResult)
+    return true;
   return this->emitThis(E);
 }
 
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
+  if (DiscardResult)
+    return true;
+
   const Expr *SubExpr = E->getSubExpr();
 
   switch (E->getOpcode()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136013.467999.patch
Type: text/x-patch
Size: 1899 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221015/5cb8625c/attachment.bin>


More information about the cfe-commits mailing list