[clang] [clang] Warn on signed char array constant conversion (PR #203792)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 17 11:55:03 PDT 2026
https://github.com/filaka771 updated https://github.com/llvm/llvm-project/pull/203792
>From cac4b9f0b32bcce99483513fdde5a236a27666e4 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/3] [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 1c1cd0f292753..25a25d2459ec2 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12792,7 +12792,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',
@@ -12805,12 +12806,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 1314f866214db537354866c523e58d186faa33c7 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/3] [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 61ca10e2f1067805d91830293f2bf1a6c5cff106 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/3] [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 1601be699a604..5c16b66032d23 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -360,6 +360,8 @@ Improvements to Clang's diagnostics
- Clang now emits an error when implicitly casting a complex type to a built-in vector type. (#GH186805)
+- Fixed a missing ``-Wconstant-conversion`` diagnostic for ``signed char`` array initialization.
+
Improvements to Clang's time-trace
----------------------------------
More information about the cfe-commits
mailing list