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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 2 09:46:46 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];
+}
----------------
AaronBallman wrote:

These two both surprise me in a C file; `b` is not an integer constant expression in either case, so I would have expected no diagnostic here. The diagnostic is defensible, just a surprise.

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


More information about the cfe-commits mailing list