From fa244bb0804d8766b6296e2eb961fc7058bb944e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= Date: Sat, 26 Apr 2025 06:32:34 +0200 Subject: [PATCH] [clang][bytecode] Allow memory leaks before C++20 The expression allocating the memory wasn't valid in the first place. This matches the diagnostic behavior of the current interpreter. --- clang/lib/AST/ByteCode/InterpState.cpp | 4 +++- clang/test/AST/ByteCode/cxx11-pedantic.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/clang/lib/AST/ByteCode/InterpState.cpp b/clang/lib/AST/ByteCode/InterpState.cpp index d6e6771f0a04f..7848f298cfec8 100644 --- a/clang/lib/AST/ByteCode/InterpState.cpp +++ b/clang/lib/AST/ByteCode/InterpState.cpp @@ -113,7 +113,9 @@ bool InterpState::maybeDiagnoseDanglingAllocations() { << (It.second.size() - 1) << Source->getSourceRange(); } } - return NoAllocationsLeft; + // Keep evaluating before C++20, since the CXXNewExpr wasn't valid there + // in the first place. + return NoAllocationsLeft || !getLangOpts().CPlusPlus20; } StdAllocatorCaller InterpState::getStdAllocatorCaller(StringRef Name) const { diff --git a/clang/test/AST/ByteCode/cxx11-pedantic.cpp b/clang/test/AST/ByteCode/cxx11-pedantic.cpp index a73f20ead1092..247c7ef1c77d8 100644 --- a/clang/test/AST/ByteCode/cxx11-pedantic.cpp +++ b/clang/test/AST/ByteCode/cxx11-pedantic.cpp @@ -20,3 +20,10 @@ namespace DynamicCast { // both-note {{dynamic_cast}} }; } + +namespace NewDelete { + struct T { + int n : *new int(4); // both-warning {{constant expression}} \ + // both-note {{until C++20}} + }; +}