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