[clang] [clang] Warn on signed char array constant conversion (PR #203792)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 25 21:13:51 PDT 2026
https://github.com/filaka771 updated https://github.com/llvm/llvm-project/pull/203792
>From 0fd336cfa52938b1af10eced6b71d6895f063afd Mon Sep 17 00:00:00 2001
From: Alex Filak <filaka771 at gmail.com>
Date: Sun, 14 Jun 2026 00:40:00 +0300
Subject: [PATCH 1/4] [clang] Fix char-array constant-conversion suppression
---
clang/lib/Sema/SemaChecking.cpp | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index b8a3f48a32f24..80e8f6cc7e540 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -13080,7 +13080,8 @@ static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T,
}
// Helper function to filter out cases for constant width constant conversion.
-// Don't warn on char array initialization or for non-decimal values.
+// Don't warn on char / unsigned char array initialization or for non-decimal
+// values.
static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T,
SourceLocation CC) {
// If initializing from a constant, and the constant starts with '0',
@@ -13093,12 +13094,16 @@ static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T,
return false;
}
- // If the CC location points to a '{', and the type is char, then assume
- // assume it is an array initialization.
+ // If the CC location points to a '{', and the destination type is char or
+ // unsigned char, then assume this is an array initialization. Keep warning
+ // for signed char arrays, where values such as 255 change sign.
if (CC.isValid() && T->isCharType()) {
+ const auto *BT =
+ dyn_cast<BuiltinType>(S.Context.getCanonicalType(T).getTypePtr());
const char FirstContextCharacter =
S.getSourceManager().getCharacterData(CC)[0];
- if (FirstContextCharacter == '{')
+ if (BT && BT->getKind() != BuiltinType::SChar &&
+ FirstContextCharacter == '{')
return false;
}
>From c6d583a1589e031e9e3850ebaff17f6df48f5a93 Mon Sep 17 00:00:00 2001
From: Alex Filak <filaka771 at gmail.com>
Date: Sun, 14 Jun 2026 00:40:12 +0300
Subject: [PATCH 2/4] [clang] Add char-array constant-conversion tests
---
clang/test/Sema/constant-conversion.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/clang/test/Sema/constant-conversion.c b/clang/test/Sema/constant-conversion.c
index ffc25b9cc4978..b40379e2180ce 100644
--- a/clang/test/Sema/constant-conversion.c
+++ b/clang/test/Sema/constant-conversion.c
@@ -125,6 +125,16 @@ void test9(void) {
char macro_char_dec = CHAR_MACRO_DEC; // expected-warning {{implicit conversion from 'int' to 'char' changes value from 255 to -1}}
char array_init[] = { 255, 127, 128, 129, 0 };
+ unsigned char unsigned_array_init[] = { 255 };
+ unsigned char unsigned_array_init_multi[] = { 255, 127, 128, 129, 0 };
+ signed char signed_array_init[] = { 255 }; // expected-warning {{implicit conversion from 'int' to 'signed char' changes value from 255 to -1}}
+ signed char signed_array_init_multi[] = {
+ 255, // expected-warning {{implicit conversion from 'int' to 'signed char' changes value from 255 to -1}}
+ 127,
+ 128, // expected-warning {{implicit conversion from 'int' to 'signed char' changes value from 128 to -128}}
+ 129, // expected-warning {{implicit conversion from 'int' to 'signed char' changes value from 129 to -127}}
+ 0
+ };
}
#define A 1
>From 4ee5bb5751469133b4790888b39c04e7348537db Mon Sep 17 00:00:00 2001
From: Alex Filak <filaka771 at gmail.com>
Date: Wed, 17 Jun 2026 21:53:22 +0300
Subject: [PATCH 3/4] [clang] Add release note for signed-char array warning
---
clang/docs/ReleaseNotes.rst | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 11656d560ee34..b33faad2ab6b9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -646,6 +646,8 @@ Improvements to Clang's diagnostics
- Diagnostics for the C++11 range-based for statement now report the correct
iterator type in notes for invalid iterator types.
+- Fixed a missing ``-Wconstant-conversion`` diagnostic for ``signed char`` array initialization.
+
Improvements to Clang's time-trace
----------------------------------
>From 4e26791ee041bb35f9b72f0ad3abb181ea3ec5ee Mon Sep 17 00:00:00 2001
From: Alex Filak <filaka771 at gmail.com>
Date: Fri, 26 Jun 2026 07:02:08 +0300
Subject: [PATCH 4/4] [clang] Handle plain char signedness in array conversion
warnings
---
clang/docs/ReleaseNotes.rst | 3 ++-
clang/lib/Sema/SemaChecking.cpp | 13 +++++++------
clang/test/Sema/constant-conversion.c | 17 ++++++++++++-----
3 files changed, 21 insertions(+), 12 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b33faad2ab6b9..5c97d00d084f0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -646,7 +646,8 @@ Improvements to Clang's diagnostics
- Diagnostics for the C++11 range-based for statement now report the correct
iterator type in notes for invalid iterator types.
-- Fixed a missing ``-Wconstant-conversion`` diagnostic for ``signed char`` array initialization.
+- Fixed a missing ``-Wconstant-conversion`` diagnostic for ``signed char``
+ array initialization. (#GH181730)
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 80e8f6cc7e540..b381975cd45ab 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -13080,8 +13080,8 @@ static void DiagnoseNullConversion(Sema &S, Expr *E, QualType T,
}
// Helper function to filter out cases for constant width constant conversion.
-// Don't warn on char / unsigned char array initialization or for non-decimal
-// values.
+// Don't warn on unsigned char / unsigned plain char array initialization or for
+// non-decimal values.
static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T,
SourceLocation CC) {
// If initializing from a constant, and the constant starts with '0',
@@ -13094,16 +13094,17 @@ static bool isSameWidthConstantConversion(Sema &S, Expr *E, QualType T,
return false;
}
- // If the CC location points to a '{', and the destination type is char or
- // unsigned char, then assume this is an array initialization. Keep warning
- // for signed char arrays, where values such as 255 change sign.
+ // If the CC location points to a '{', and the destination type is plain char
+ // with unsigned signedness or unsigned char, then assume this is an array
+ // initialization. Keep warning for signed char arrays and plain char arrays
+ // with signed signedness, where values such as 255 change sign.
if (CC.isValid() && T->isCharType()) {
const auto *BT =
dyn_cast<BuiltinType>(S.Context.getCanonicalType(T).getTypePtr());
const char FirstContextCharacter =
S.getSourceManager().getCharacterData(CC)[0];
if (BT && BT->getKind() != BuiltinType::SChar &&
- FirstContextCharacter == '{')
+ BT->getKind() != BuiltinType::Char_S && FirstContextCharacter == '{')
return false;
}
diff --git a/clang/test/Sema/constant-conversion.c b/clang/test/Sema/constant-conversion.c
index b40379e2180ce..31e378eb707cc 100644
--- a/clang/test/Sema/constant-conversion.c
+++ b/clang/test/Sema/constant-conversion.c
@@ -1,5 +1,6 @@
-// RUN: %clang_cc1 -fsyntax-only -ffreestanding -verify=expected,one-bit -triple x86_64-apple-darwin %s
-// RUN: %clang_cc1 -fsyntax-only -ffreestanding -Wno-single-bit-bitfield-constant-conversion -verify -triple x86_64-apple-darwin %s
+// RUN: %clang_cc1 -fsyntax-only -ffreestanding -verify=expected,signed-plain-char,one-bit -triple x86_64-apple-darwin %s
+// RUN: %clang_cc1 -fsyntax-only -ffreestanding -Wno-single-bit-bitfield-constant-conversion -verify=expected,signed-plain-char -triple x86_64-apple-darwin %s
+// RUN: %clang_cc1 -fsyntax-only -ffreestanding -Wno-single-bit-bitfield-constant-conversion -verify=expected,unsigned-plain-char -triple x86_64-apple-darwin -fno-signed-char %s
#include <stdbool.h>
@@ -103,7 +104,7 @@ void test9(void) {
const int max_short_plus_one = (int)max_short + 1;
const long max_int_plus_one = (long)max_int + 1;
- char new_char = max_char_plus_one; // expected-warning {{implicit conversion from 'const short' to 'char' changes value from 128 to -128}}
+ char new_char = max_char_plus_one; // signed-plain-char-warning {{implicit conversion from 'const short' to 'char' changes value from 128 to -128}}
short new_short = max_short_plus_one; // expected-warning {{implicit conversion from 'const int' to 'short' changes value from 32768 to -32768}}
int new_int = max_int_plus_one; // expected-warning {{implicit conversion from 'const long' to 'int' changes value from 2147483648 to -2147483648}}
@@ -122,9 +123,15 @@ void test9(void) {
#define CHAR_MACRO_HEX 0xff
char macro_char_hex = CHAR_MACRO_HEX;
#define CHAR_MACRO_DEC 255
- char macro_char_dec = CHAR_MACRO_DEC; // expected-warning {{implicit conversion from 'int' to 'char' changes value from 255 to -1}}
+ char macro_char_dec = CHAR_MACRO_DEC; // signed-plain-char-warning {{implicit conversion from 'int' to 'char' changes value from 255 to -1}}
- char array_init[] = { 255, 127, 128, 129, 0 };
+ char array_init[] = {
+ 255, // signed-plain-char-warning {{implicit conversion from 'int' to 'char' changes value from 255 to -1}}
+ 127,
+ 128, // signed-plain-char-warning {{implicit conversion from 'int' to 'char' changes value from 128 to -128}}
+ 129, // signed-plain-char-warning {{implicit conversion from 'int' to 'char' changes value from 129 to -127}}
+ 0
+ };
unsigned char unsigned_array_init[] = { 255 };
unsigned char unsigned_array_init_multi[] = { 255, 127, 128, 129, 0 };
signed char signed_array_init[] = { 255 }; // expected-warning {{implicit conversion from 'int' to 'signed char' changes value from 255 to -1}}
More information about the cfe-commits
mailing list