[clang] [clang] Detect int-to-float narrowing when the back-conversion is unspecified (PR #157174)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Sep 7 04:06:14 PDT 2025
https://github.com/camc updated https://github.com/llvm/llvm-project/pull/157174
>From 73696f81166f958e212c5ea314528791c5201565 Mon Sep 17 00:00:00 2001
From: camc <69519329+camc at users.noreply.github.com>
Date: Fri, 5 Sep 2025 20:47:06 +0000
Subject: [PATCH 1/3] [clang] Detect int-to-float narrowing when backconversion
is unspecified
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaOverload.cpp | 10 ++++++----
.../test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp | 2 ++
3 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8720262c33959..bd1f536f33404 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -308,6 +308,8 @@ Bug Fixes in This Version
- Builtin elementwise operators now accept vector arguments that have different
qualifiers on their elements. For example, vector of 4 ``const float`` values
and vector of 4 ``float`` values. (#GH155405)
+- Fix the check for narrowing int-to-float conversions, so that they are detected in
+ cases where converting the float back to an integer is undefined behaviour.
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 14fa8478fe317..71d23906f2b17 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -412,10 +412,12 @@ NarrowingKind StandardConversionSequence::getNarrowingKind(
// And back.
llvm::APSInt ConvertedValue = *IntConstantValue;
bool ignored;
- Result.convertToInteger(ConvertedValue,
- llvm::APFloat::rmTowardZero, &ignored);
- // If the resulting value is different, this was a narrowing conversion.
- if (*IntConstantValue != ConvertedValue) {
+ llvm::APFloat::opStatus fs = Result.convertToInteger(
+ ConvertedValue, llvm::APFloat::rmTowardZero, &ignored);
+ // If the converted-back integer has unspecified value, or if the
+ // resulting value is different, this was a narrowing conversion.
+ if (fs == llvm::APFloat::opInvalidOp ||
+ *IntConstantValue != ConvertedValue) {
ConstantValue = APValue(*IntConstantValue);
ConstantType = Initializer->getType();
return NK_Constant_Narrowing;
diff --git a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
index 2bceb3e267790..5bb4708f869f8 100644
--- a/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
+++ b/clang/test/CXX/dcl.decl/dcl.init/dcl.init.list/p7-0x.cpp
@@ -137,6 +137,8 @@ void int_to_float() {
Agg<float> f7 = {12345678}; // OK (exactly fits in a float)
Agg<float> f8 = {EnumVal}; // OK
Agg<float> f9 = {123456789}; // expected-error {{ cannot be narrowed }} expected-note {{silence}}
+ Agg<float> f10 = {2147483646}; // expected-error {{constant expression evaluates to 2147483646 which cannot be narrowed to type 'float'}} expected-note {{silence}}
+ Agg<float> f11 = {2147483647}; // expected-error {{constant expression evaluates to 2147483647 which cannot be narrowed to type 'float'}} expected-note {{silence}}
Agg<float> ce1 = { Convert<int>(123456789) }; // expected-error {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{silence}}
Agg<double> ce2 = { ConvertVar<long long>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{silence}}
>From fbdca7670295a662c81a76fb74406e7be338d20c Mon Sep 17 00:00:00 2001
From: camc <69519329+camc at users.noreply.github.com>
Date: Sat, 6 Sep 2025 13:12:30 +0000
Subject: [PATCH 2/3] Address comments
---
clang/docs/ReleaseNotes.rst | 2 +-
clang/lib/Sema/SemaOverload.cpp | 4 ++--
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index bd1f536f33404..dc5f8c59fddc8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -309,7 +309,7 @@ Bug Fixes in This Version
qualifiers on their elements. For example, vector of 4 ``const float`` values
and vector of 4 ``float`` values. (#GH155405)
- Fix the check for narrowing int-to-float conversions, so that they are detected in
- cases where converting the float back to an integer is undefined behaviour.
+ cases where converting the float back to an integer is undefined behaviour (#GH157067).
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 71d23906f2b17..941542247e240 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -412,11 +412,11 @@ NarrowingKind StandardConversionSequence::getNarrowingKind(
// And back.
llvm::APSInt ConvertedValue = *IntConstantValue;
bool ignored;
- llvm::APFloat::opStatus fs = Result.convertToInteger(
+ llvm::APFloat::opStatus Status = Result.convertToInteger(
ConvertedValue, llvm::APFloat::rmTowardZero, &ignored);
// If the converted-back integer has unspecified value, or if the
// resulting value is different, this was a narrowing conversion.
- if (fs == llvm::APFloat::opInvalidOp ||
+ if (Status == llvm::APFloat::opInvalidOp ||
*IntConstantValue != ConvertedValue) {
ConstantValue = APValue(*IntConstantValue);
ConstantType = Initializer->getType();
>From 03f7201fdbbe09a76499ce8f6566f0d56c16bcee Mon Sep 17 00:00:00 2001
From: camc <69519329+camc at users.noreply.github.com>
Date: Sun, 7 Sep 2025 11:06:04 +0000
Subject: [PATCH 3/3] move release note
---
clang/docs/ReleaseNotes.rst | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index dc5f8c59fddc8..1520d8440b4c0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -308,8 +308,6 @@ Bug Fixes in This Version
- Builtin elementwise operators now accept vector arguments that have different
qualifiers on their elements. For example, vector of 4 ``const float`` values
and vector of 4 ``float`` values. (#GH155405)
-- Fix the check for narrowing int-to-float conversions, so that they are detected in
- cases where converting the float back to an integer is undefined behaviour (#GH157067).
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -346,6 +344,8 @@ Bug Fixes to C++ Support
instantiated properly. (#GH154054)
- Fixed a crash when implicit conversions from initialize list to arrays of
unknown bound during constant evaluation. (#GH151716)
+- Fix the check for narrowing int-to-float conversions, so that they are detected in
+ cases where converting the float back to an integer is undefined behaviour (#GH157067).
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
More information about the cfe-commits
mailing list