[clang] 7766648 - [clang][Interp] PointerToBoolean casts

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 24 23:08:20 PDT 2023


Author: Timm Bäder
Date: 2023-04-25T08:04:08+02:00
New Revision: 7766648c3aff43408b5d17214fc67c8521952558

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

LOG: [clang][Interp] PointerToBoolean casts

Just emit e != nullptr for these.

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

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 e28532b288871..536438b347a20 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -150,6 +150,17 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
     return this->emitCast(*FromT, *ToT, CE);
   }
 
+  case CK_PointerToBoolean: {
+    // Just emit p != nullptr for this.
+    if (!this->visit(SubExpr))
+      return false;
+
+    if (!this->emitNullPtr(CE))
+      return false;
+
+    return this->emitNEPtr(CE);
+  }
+
   case CK_ToVoid:
     return discard(SubExpr);
 

diff  --git a/clang/test/AST/Interp/literals.cpp b/clang/test/AST/Interp/literals.cpp
index f31a49088dc6c..54f3aefcef2b1 100644
--- a/clang/test/AST/Interp/literals.cpp
+++ b/clang/test/AST/Interp/literals.cpp
@@ -101,6 +101,19 @@ constexpr int gimme(int k) {
 }
 static_assert(gimme(5) == 5, "");
 
+namespace PointerToBool {
+
+  constexpr void *N = nullptr;
+  constexpr bool B = N;
+  static_assert(!B, "");
+  static_assert(!N, "");
+
+  constexpr float F = 1.0;
+  constexpr const float *FP = &F;
+  static_assert(FP, "");
+  static_assert(!!FP, "");
+}
+
 namespace SizeOf {
   constexpr int soint = sizeof(int);
   constexpr int souint = sizeof(unsigned int);


        


More information about the cfe-commits mailing list