llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Alex (filaka771)

<details>
<summary>Changes</summary>

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.


---
Full diff: https://github.com/llvm/llvm-project/pull/203792.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaChecking.cpp (+10-5) 
- (modified) clang/test/Sema/constant-conversion.c (+10) 


``````````diff
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;
   }
 
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/203792
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to