[clang] 92f67dc - [clang][Interp] Handle DiscardResult for DeclRef- and ParenExprs

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Wed May 3 23:30:52 PDT 2023


Author: Timm Bäder
Date: 2023-05-04T08:22:06+02:00
New Revision: 92f67dc27f41b555fa4b93f3d679fb895e51d795

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

LOG: [clang][Interp] Handle DiscardResult for DeclRef- and ParenExprs

Fixes https://github.com/llvm/llvm-project/issues/62004

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

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 ee59fc372a8b5..c3939996919e2 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -188,7 +188,12 @@ bool ByteCodeExprGen<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) {
 
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::VisitParenExpr(const ParenExpr *PE) {
-  return this->visit(PE->getSubExpr());
+  const Expr *SubExpr = PE->getSubExpr();
+
+  if (DiscardResult)
+    return this->discard(SubExpr);
+
+  return this->visit(SubExpr);
 }
 
 template <class Emitter>
@@ -1872,6 +1877,9 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
 
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
+  if (DiscardResult)
+    return true;
+
   const auto *D = E->getDecl();
 
   if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {

diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index acad760bc43ee..5f4c0485ba772 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -814,4 +814,18 @@ constexpr int ignoredDecls() {
   return F{12}.a;
 }
 static_assert(ignoredDecls() == 12, "");
+
+struct A{};
+constexpr int ignoredExprs() {
+  (void)(1 / 2);
+  A a;
+  a; // expected-warning {{unused}} \
+     // ref-warning {{unused}}
+  (void)a;
+  (a); // expected-warning {{unused}} \
+       // ref-warning {{unused}}
+
+  return 0;
+}
+
 #endif


        


More information about the cfe-commits mailing list