[clang] [clang][bytecode] Fix calling operator new with nothrow/align parameter (PR #144271)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 15 10:35:23 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/144271
Discard all the parameters we don't care about.
>From 56ef41ba741fc71d797b1d6768d6fb8211c71bef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 15 Jun 2025 19:26:16 +0200
Subject: [PATCH] [clang][bytecode] Fix calling operator new with nothrow/align
parameter
Discard all the parameters we don't care about.
---
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 20 +++++++++++++++++++-
clang/test/AST/ByteCode/new-delete.cpp | 22 ++++++++++++++++------
2 files changed, 35 insertions(+), 7 deletions(-)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 5fc5034569597..d01e3d042a8bf 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1423,7 +1423,6 @@ static bool interp__builtin_operator_new(InterpState &S, CodePtr OpPC,
// Walk up the call stack to find the appropriate caller and get the
// element type from it.
auto [NewCall, ElemType] = S.getStdAllocatorCaller("allocate");
- APSInt Bytes = popToAPSInt(S.Stk, *S.getContext().classify(Call->getArg(0)));
if (ElemType.isNull()) {
S.FFDiag(Call, S.getLangOpts().CPlusPlus20
@@ -1439,6 +1438,25 @@ static bool interp__builtin_operator_new(InterpState &S, CodePtr OpPC,
return false;
}
+ // We only care about the first parameter (the size), so discard all the
+ // others.
+ {
+ unsigned NumArgs = Call->getNumArgs();
+ assert(NumArgs >= 1);
+
+ // The std::nothrow_t arg never gets put on the stack.
+ if (Call->getArg(NumArgs - 1)->getType()->isNothrowT())
+ --NumArgs;
+ auto Args = llvm::ArrayRef(Call->getArgs(), Call->getNumArgs());
+ // First arg is needed.
+ Args = Args.drop_front();
+
+ // Discard the rest.
+ for (const Expr *Arg : Args)
+ discard(S.Stk, *S.getContext().classify(Arg));
+ }
+
+ APSInt Bytes = popToAPSInt(S.Stk, *S.getContext().classify(Call->getArg(0)));
CharUnits ElemSize = S.getASTContext().getTypeSizeInChars(ElemType);
assert(!ElemSize.isZero());
// Divide the number of bytes by sizeof(ElemType), so we get the number of
diff --git a/clang/test/AST/ByteCode/new-delete.cpp b/clang/test/AST/ByteCode/new-delete.cpp
index 1ee41a98e13bb..9c293e5d15fc8 100644
--- a/clang/test/AST/ByteCode/new-delete.cpp
+++ b/clang/test/AST/ByteCode/new-delete.cpp
@@ -1,9 +1,9 @@
-// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=expected,both %s
-// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
-// RUN: %clang_cc1 -triple=i686-linux-gnu -std=c++20 -fexperimental-new-constant-interpreter -verify=expected,both %s
-// RUN: %clang_cc1 -verify=ref,both %s
-// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
-// RUN: %clang_cc1 -triple=i686-linux-gnu -std=c++20 -verify=ref,both %s
+// RUN: %clang_cc1 -verify=expected,both -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -std=c++20 -verify=expected,both -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -std=c++20 -verify=expected,both -triple=i686-linux-gnu -fexperimental-new-constant-interpreter %s
+// RUN: %clang_cc1 -verify=ref,both %s
+// RUN: %clang_cc1 -std=c++20 -verify=ref,both %s
+// RUN: %clang_cc1 -std=c++20 -verify=ref,both -triple=i686-linux-gnu %s
#if __cplusplus >= 202002L
@@ -1012,6 +1012,16 @@ constexpr int no_deallocate_nonalloc = (std::allocator<int>().deallocate((int*)&
// both-note {{in call}} \
// both-note {{declared here}}
+namespace OpNewNothrow {
+ constexpr int f() {
+ int *v = (int*)operator new(sizeof(int), std::align_val_t(2), std::nothrow); // both-note {{cannot allocate untyped memory in a constant expression; use 'std::allocator<T>::allocate' to allocate memory of type 'T'}}
+ operator delete(v, std::align_val_t(2), std::nothrow);
+ return 1;
+ }
+ static_assert(f()); // both-error {{not an integral constant expression}} \
+ // both-note {{in call to}}
+}
+
#else
/// Make sure we reject this prior to C++20
constexpr int a() { // both-error {{never produces a constant expression}}
More information about the cfe-commits
mailing list