[clang] cd2f2c1 - [clang][Interp] Visit Logical-not operand as bool

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 15 01:12:52 PDT 2023


Author: Timm Bäder
Date: 2023-09-15T10:12:20+02:00
New Revision: cd2f2c166203cb30f8231c0f3f8d2e28f30f9b8c

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

LOG: [clang][Interp] Visit Logical-not operand as bool

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 3f0deb0a9f9c683..e49dc80a5c9ce7c 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -2378,10 +2378,18 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
     return this->emitStore(*T, E);
   }
   case UO_LNot: // !x
-    if (!this->visit(SubExpr))
+    if (DiscardResult)
+      return this->discard(SubExpr);
+
+    if (!this->visitBool(SubExpr))
       return false;
-    // The Inv doesn't change anything, so skip it if we don't need the result.
-    return DiscardResult ? this->emitPop(*T, E) : this->emitInvBool(E);
+
+    if (!this->emitInvBool(E))
+      return false;
+
+    if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
+      return this->emitCast(PT_Bool, ET, E);
+    return true;
   case UO_Minus: // -x
     if (!this->visit(SubExpr))
       return false;

diff  --git a/clang/test/AST/Interp/c.c b/clang/test/AST/Interp/c.c
index f4adfd189d22ec9..789139362335236 100644
--- a/clang/test/AST/Interp/c.c
+++ b/clang/test/AST/Interp/c.c
@@ -12,6 +12,9 @@ _Static_assert(1.0 == 1.0, ""); // pedantic-ref-warning {{not an integer constan
 _Static_assert(1 && 1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
                               // pedantic-expected-warning {{not an integer constant expression}}
 _Static_assert( (5 > 4) + (3 > 2) == 2, "");
+_Static_assert(!!1.0, ""); // pedantic-ref-warning {{not an integer constant expression}} \
+                           // pedantic-expected-warning {{not an integer constant expression}}
+_Static_assert(!!1, "");
 
 /// FIXME: Should also be rejected in the new interpreter
 int a = (1 == 1 ? 5 : 3);


        


More information about the cfe-commits mailing list