https://github.com/filaka771 created
https://github.com/llvm/llvm-project/pull/203792
This resolves #181730: fixes a missing `-Wconstant-conversion`
diagnostic for `signed char` array initialization.
Before this change, Clang warned for `signed char foo = 255;` but did not
warn for `signed char bar[] = {255};`.
The warning suppression helper did not distinguish between `char`,
`unsigned char`, and `signed char` in brace-initialized arrays. Now it does.
>From 6f95e295bf3987eca0dff7c6a72db951658d98b6 Mon Sep 17 00:00:00 2001
From: Alex Filak <[email protected]>
Date: Sun, 14 Jun 2026 00:40:00 +0300
Subject: [PATCH 1/2] [clang] Fix char-array constant-conversion suppression
---
clang/lib/Sema/SemaChecking.cpp | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 1c1cd0f292753..884aafa15a3b6 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 (CC.isValid() && T->isCharType()) {
+ // 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()) {
+ const auto *BT =
+ dyn_cast<BuiltinType>(S.Context.getCanonicalType(T).getTypePtr());
const char FirstContextCharacter =
S.getSourceManager().getCharacterData(CC)[0];
- if (FirstContextCharacter == '{')
+ if (BT && BT->isCharType() && BT->getKind() != BuiltinType::SChar &&
+ FirstContextCharacter == '{')
return false;
}
>From 08e95303df9b096003822fbc9fece8965f18dc35 Mon Sep 17 00:00:00 2001
From: Alex Filak <[email protected]>
Date: Sun, 14 Jun 2026 00:40:12 +0300
Subject: [PATCH 2/2] [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
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits