104 lines · cpp
1// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s2 3void 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 9void 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 15void 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 21void 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 27char returnsChar(void);28void t5(void) {29 int *array = 0;30 int val = array[returnsChar()]; // expected-warning{{array subscript is of type 'char'}}31}32 33void t6(void) {34 int array[1] = { 0 };35 signed char subscript = 0;36 int val = array[subscript]; // no warning for explicit signed char37}38 39void t7(void) {40 int array[1] = { 0 };41 unsigned char subscript = 0;42 int val = array[subscript]; // no warning for unsigned char43}44 45typedef char CharTy;46void 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 52typedef signed char SignedCharTy;53void t9(void) {54 int array[1] = { 0 };55 SignedCharTy subscript = 0;56 int val = array[subscript]; // no warning for explicit signed char57}58 59typedef unsigned char UnsignedCharTy;60void t10(void) {61 int array[1] = { 0 };62 UnsignedCharTy subscript = 0;63 int val = array[subscript]; // no warning for unsigned char64}65 66void t11(void) {67 int array[256] = { 0 };68 int val = array['a']; // no warning for char with known positive value69}70 71void 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 77void t13(void) {78 int array[256] = { 0 };79 const char b = 'a';80 int val = array[b]; // no warning for char with known positive value81}82 83void t14(void) {84 int array[256] = { 0 };85 constexpr char b = 'a';86 int val = array[b]; // no warning for char with known positive value87}88 89void 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 97void 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