xref: /llvm-project/clang/test/Sema/warn-char-subscripts.cpp (revision 0031efe6be19735402656a76b64a173d17f1f935)
1 // RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
2 
t1(void)3 void t1(void) {
4   int array[1] = { 0 };
5   char subscript = 0;
6   int val = array[subscript]; // expected-warning{{array subscript is of type 'char'}}
7 }
8 
t2(void)9 void t2(void) {
10   int array[1] = { 0 };
11   char subscript = 0;
12   int val = subscript[array]; // expected-warning{{array subscript is of type 'char'}}
13 }
14 
t3(void)15 void t3(void) {
16   int *array = 0;
17   char subscript = 0;
18   int val = array[subscript]; // expected-warning{{array subscript is of type 'char'}}
19 }
20 
t4(void)21 void t4(void) {
22   int *array = 0;
23   char subscript = 0;
24   int val = subscript[array]; // expected-warning{{array subscript is of type 'char'}}
25 }
26 
27 char returnsChar(void);
t5(void)28 void t5(void) {
29   int *array = 0;
30   int val = array[returnsChar()]; // expected-warning{{array subscript is of type 'char'}}
31 }
32 
t6(void)33 void t6(void) {
34   int array[1] = { 0 };
35   signed char subscript = 0;
36   int val = array[subscript]; // no warning for explicit signed char
37 }
38 
t7(void)39 void t7(void) {
40   int array[1] = { 0 };
41   unsigned char subscript = 0;
42   int val = array[subscript]; // no warning for unsigned char
43 }
44 
45 typedef char CharTy;
t8(void)46 void t8(void) {
47   int array[1] = { 0 };
48   CharTy subscript = 0;
49   int val = array[subscript]; // expected-warning{{array subscript is of type 'char'}}
50 }
51 
52 typedef signed char SignedCharTy;
t9(void)53 void t9(void) {
54   int array[1] = { 0 };
55   SignedCharTy subscript = 0;
56   int val = array[subscript]; // no warning for explicit signed char
57 }
58 
59 typedef unsigned char UnsignedCharTy;
t10(void)60 void t10(void) {
61   int array[1] = { 0 };
62   UnsignedCharTy subscript = 0;
63   int val = array[subscript]; // no warning for unsigned char
64 }
65 
t11(void)66 void t11(void) {
67   int array[256] = { 0 };
68   int val = array['a']; // no warning for char with known positive value
69 }
70 
t12(void)71 void t12(void) {
72   int array[256] = { 0 };
73   char b = 'a';
74   int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
75 }
76 
t13(void)77 void t13(void) {
78   int array[256] = { 0 };
79   const char b = 'a';
80   int val = array[b]; // no warning for char with known positive value
81 }
82 
t14(void)83 void t14(void) {
84   int array[256] = { 0 };
85   constexpr char b = 'a';
86   int val = array[b]; // no warning for char with known positive value
87 }
88 
t15(void)89 void t15(void) {
90   int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
91   const char b = -1;
92   // expected-warning@+2 {{array subscript is of type 'char'}}
93   // expected-warning@+1 {{array index -1 is before the beginning of the array}}
94   int val = array[b];
95 }
96 
t16(void)97 void t16(void) {
98   int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
99   constexpr char b = -1;
100   // expected-warning@+2 {{array subscript is of type 'char'}}
101   // expected-warning@+1 {{array index -1 is before the beginning of the array}}
102   int val = array[b];
103 }
104