[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 2 12:50:49 PDT 2023


================
@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning at +2 {{array subscript is of type 'char'}}
+  // expected-warning at +1 {{array index -1 is before the beginning of the array}}
+  int val = array[b];
+}
----------------
wheatman wrote:

This behavior was not changed https://godbolt.org/z/4drrx6h9M since in `c`  mode `b` is not considered a constant expression.  These tests were added to since they were added in the `c++` file and I wanted to show the expected behavior in both languages. 

https://github.com/llvm/llvm-project/pull/69061


More information about the cfe-commits mailing list