[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / warn-char-subscripts.c
blobc2f7a3731d72c894ccbf4bf01c90008b4469787d
1 // RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
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'}}
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'}}
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'}}
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'}}
28 char returnsChar(void);
29 void t5(void) {
30 int *array = 0;
31 int val = array[returnsChar()]; // expected-warning{{array subscript is of type 'char'}}
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
40 void t7(void) {
41 int array[1] = { 0 };
42 unsigned char subscript = 0;
43 int val = array[subscript]; // no warning for unsigned char
46 typedef char CharTy;
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'}}
53 typedef signed char SignedCharTy;
54 void t9(void) {
55 int array[1] = { 0 };
56 SignedCharTy subscript = 0;
57 int val = array[subscript]; // no warning for explicit signed char
60 typedef unsigned char UnsignedCharTy;
61 void t10(void) {
62 int array[1] = { 0 };
63 UnsignedCharTy subscript = 0;
64 int val = array[subscript]; // no warning for unsigned char
67 void t11(void) {
68 int array[256] = { 0 };
69 int val = array['a']; // no warning for char with known positive value
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'}}
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'}}
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];