[clang] [clang][bytecode] Fix discarded pointer subtractions (PR #118477)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 3 04:01:51 PST 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/118477

We need to pop the value.

>From 409fa110d7eeb4a91f62b64d59aecb30db700ebd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 3 Dec 2024 13:00:29 +0100
Subject: [PATCH] [clang][bytecode] Fix discarded pointer subtractions

We need to pop the value.
---
 clang/lib/AST/ByteCode/Compiler.cpp  | 5 ++++-
 clang/test/AST/ByteCode/literals.cpp | 2 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index a95353fd2943c9..705ab1ec8e8ab6 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -1000,7 +1000,10 @@ bool Compiler<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) {
     if (!visitAsPointer(RHS, *RT) || !visitAsPointer(LHS, *LT))
       return false;
 
-    return this->emitSubPtr(classifyPrim(E->getType()), E);
+    PrimType IntT = classifyPrim(E->getType());
+    if (!this->emitSubPtr(IntT, E))
+      return false;
+    return DiscardResult ? this->emitPop(IntT, E) : true;
   }
 
   PrimType OffsetType;
diff --git a/clang/test/AST/ByteCode/literals.cpp b/clang/test/AST/ByteCode/literals.cpp
index 13d6c4feb35002..662823c49cd4a0 100644
--- a/clang/test/AST/ByteCode/literals.cpp
+++ b/clang/test/AST/ByteCode/literals.cpp
@@ -980,6 +980,8 @@ namespace DiscardExprs {
     __uuidof(number); // both-error {{cannot call operator __uuidof on a type with no GUID}}
 
     requires{false;};
+    constexpr int *p = nullptr;
+    p - p;
 
     return 0;
   }



More information about the cfe-commits mailing list