https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/183761
>From a42fc169d00aa2bb7adae380790042474f409607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Fri, 27 Feb 2026 16:54:14 +0100 Subject: [PATCH] obt --- clang/lib/AST/ByteCode/Context.cpp | 3 +++ clang/lib/AST/ByteCode/Interp.h | 12 ++++++++++++ clang/test/Sema/attr-overflow-behavior-constexpr.cpp | 4 ++++ clang/test/Sema/attr-overflow-behavior-templates.cpp | 1 + clang/test/Sema/attr-overflow-behavior.c | 1 + clang/test/Sema/attr-overflow-behavior.cpp | 1 + 6 files changed, 22 insertions(+) diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp index 879d51e6a2c3e..c811fac384fd4 100644 --- a/clang/lib/AST/ByteCode/Context.cpp +++ b/clang/lib/AST/ByteCode/Context.cpp @@ -470,6 +470,9 @@ OptPrimType Context::classify(QualType T) const { if (const auto *DT = dyn_cast<DecltypeType>(T)) return classify(DT->getUnderlyingType()); + if (const auto *OBT = T.getCanonicalType()->getAs<OverflowBehaviorType>()) + return classify(OBT->getUnderlyingType()); + if (T->isObjCObjectPointerType() || T->isBlockPointerType()) return PT_Ptr; diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index 7f30def20cc36..bec6560a4020c 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -296,6 +296,10 @@ bool AddSubMulHelper(InterpState &S, CodePtr OpPC, unsigned Bits, const T &LHS, if constexpr (std::is_same_v<T, FixedPoint>) return handleFixedPointOverflow(S, OpPC, Result); + // If wrapping is enabled, the new value is fine. + if (S.Current->getExpr(OpPC)->getType().isWrapType()) + return true; + // Slow path - compute the result using another bit of precision. APSInt Value = OpAP<APSInt>()(LHS.toAPSInt(Bits), RHS.toAPSInt(Bits)); @@ -708,6 +712,9 @@ bool Neg(InterpState &S, CodePtr OpPC) { "don't expect other types to fail at constexpr negation"); S.Stk.push<T>(Result); + if (S.Current->getExpr(OpPC)->getType().isWrapType()) + return true; + APSInt NegatedValue = -Value.toAPSInt(Value.bitWidth() + 1); if (S.checkingForUndefinedBehavior()) { const Expr *E = S.Current->getExpr(OpPC); @@ -776,6 +783,11 @@ bool IncDecHelper(InterpState &S, CodePtr OpPC, const Pointer &Ptr, } assert(CanOverflow); + if (S.Current->getExpr(OpPC)->getType().isWrapType()) { + Ptr.deref<T>() = Result; + return true; + } + // Something went wrong with the previous operation. Compute the // result with another bit of precision. unsigned Bits = Value.bitWidth() + 1; diff --git a/clang/test/Sema/attr-overflow-behavior-constexpr.cpp b/clang/test/Sema/attr-overflow-behavior-constexpr.cpp index 1d81d3a243c1e..403ea319a61ae 100644 --- a/clang/test/Sema/attr-overflow-behavior-constexpr.cpp +++ b/clang/test/Sema/attr-overflow-behavior-constexpr.cpp @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -triple x86_64-linux-gnu %s -fexperimental-overflow-behavior-types -verify -fsyntax-only -std=c++14 +// RUN: %clang_cc1 -triple x86_64-linux-gnu %s -fexperimental-overflow-behavior-types -verify -fsyntax-only -std=c++14 -fexperimental-new-constant-interpreter #define __wrap __attribute__((overflow_behavior(wrap))) #define __no_trap __attribute__((overflow_behavior(trap))) @@ -35,6 +36,9 @@ void constexpr_test() { static_assert(preinc(max) == -2147483648, "preinc wrapping failed"); static_assert(postinc(max) == -2147483648, "postinc wrapping failed"); + + constexpr wrap_int M = -2147483648; + constexpr wrap_int A = -M; } template <typename T> diff --git a/clang/test/Sema/attr-overflow-behavior-templates.cpp b/clang/test/Sema/attr-overflow-behavior-templates.cpp index 09242f6f9ab3a..40c7310b78e53 100644 --- a/clang/test/Sema/attr-overflow-behavior-templates.cpp +++ b/clang/test/Sema/attr-overflow-behavior-templates.cpp @@ -1,4 +1,5 @@ // RUN: %clang_cc1 %s -fexperimental-overflow-behavior-types -verify -fsyntax-only -Wimplicit-overflow-behavior-conversion +// RUN: %clang_cc1 %s -fexperimental-overflow-behavior-types -verify -fsyntax-only -Wimplicit-overflow-behavior-conversion -fexperimental-new-constant-interpreter template<typename T> constexpr int template_overload_test(T) { diff --git a/clang/test/Sema/attr-overflow-behavior.c b/clang/test/Sema/attr-overflow-behavior.c index 6ce876f09d802..7f2312537516d 100644 --- a/clang/test/Sema/attr-overflow-behavior.c +++ b/clang/test/Sema/attr-overflow-behavior.c @@ -1,4 +1,5 @@ // RUN: %clang_cc1 %s -Winteger-overflow -Wno-unused-value -fexperimental-overflow-behavior-types -Woverflow-behavior-conversion -Wconstant-conversion -verify -fsyntax-only -std=c11 -Wno-pointer-sign +// RUN: %clang_cc1 %s -Winteger-overflow -Wno-unused-value -fexperimental-overflow-behavior-types -Woverflow-behavior-conversion -Wconstant-conversion -verify -fsyntax-only -std=c11 -Wno-pointer-sign -fexperimental-new-constant-interpreter typedef int __attribute__((overflow_behavior)) bad_arg_count; // expected-error {{'overflow_behavior' attribute takes one argument}} typedef int __attribute__((overflow_behavior(not_real))) bad_arg_spec; // expected-error {{'not_real' is not a valid argument to attribute 'overflow_behavior'}} diff --git a/clang/test/Sema/attr-overflow-behavior.cpp b/clang/test/Sema/attr-overflow-behavior.cpp index 1dfe00ab47ae2..a69eb83e591f5 100644 --- a/clang/test/Sema/attr-overflow-behavior.cpp +++ b/clang/test/Sema/attr-overflow-behavior.cpp @@ -1,4 +1,5 @@ // RUN: %clang_cc1 %s -Winteger-overflow -Wno-unused-value -fexperimental-overflow-behavior-types -Wconstant-conversion -Woverflow-behavior-conversion -verify -fsyntax-only +// RUN: %clang_cc1 %s -Winteger-overflow -Wno-unused-value -fexperimental-overflow-behavior-types -Wconstant-conversion -Woverflow-behavior-conversion -verify -fsyntax-only -fexperimental-new-constant-interpreter typedef int __attribute__((overflow_behavior)) bad_arg_count; // expected-error {{'overflow_behavior' attribute takes one argument}} typedef int __attribute__((overflow_behavior(not_real))) bad_arg_spec; // expected-error {{'not_real' is not a valid argument to attribute 'overflow_behavior'}} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
