xref: /llvm-project/clang/test/Sema/warn-int-in-bool-context.c (revision a18e92d020b895b712175a3b13a3d021608115a7)
1 // RUN: %clang_cc1 -x c -fsyntax-only -verify -Wint-in-bool-context %s
2 // RUN: %clang_cc1 -x c -fsyntax-only -verify -Wall %s
3 // RUN: %clang_cc1 -x c -std=c23 -fsyntax-only -verify -Wall %s
4 // RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wint-in-bool-context %s
5 // RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wall %s
6 
7 #define ONE 1
8 #define TWO 2
9 
10 #define SHIFT(l, r) l << r
11 #define MM a << a
12 #define AF 1 << 7
13 
14 #ifdef __cplusplus
15 typedef bool boolean;
16 #else
17 typedef _Bool boolean;
18 #endif
19 
20 enum num {
21   zero,
22   one,
23   two,
24 };
25 
test(int a,unsigned b,enum num n)26 int test(int a, unsigned b, enum num n) {
27   boolean r;
28   r = a << a;    // expected-warning {{converting the result of '<<' to a boolean; did you mean '(a << a) != 0'?}}
29   r = MM;        // expected-warning {{converting the result of '<<' to a boolean; did you mean '(a << a) != 0'?}}
30   r = (1 << 7);  // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}}
31   r = 2UL << 2;  // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}}
32   r = 0 << a;    // expected-warning {{converting the result of '<<' to a boolean always evaluates to false}}
33   r = 0 << 2;    // expected-warning {{converting the result of '<<' to a boolean always evaluates to false}}
34   r = 1 << 0;    // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}}
35   r = 1 << 2;    // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}}
36   r = 1ULL << 2; // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}}
37   r = 2 << b;    // expected-warning {{converting the result of '<<' to a boolean; did you mean '(2 << b) != 0'?}}
38   r = (unsigned)(2 << b);
39   r = b << 7;
40   r = (1 << a); // expected-warning {{converting the result of '<<' to a boolean; did you mean '(1 << a) != 0'?}}
41   r = TWO << a; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(2 << a) != 0'?}}
42   r = a << 7;   // expected-warning {{converting the result of '<<' to a boolean; did you mean '(a << 7) != 0'?}}
43   r = ONE << a; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(1 << a) != 0'?}}
44   if (TWO << a) // expected-warning {{converting the result of '<<' to a boolean; did you mean '(2 << a) != 0'?}}
45     return a;
46 
47   a = 1 << 2 ? 0: 1; // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}}
48   a = 1 << a ? 0: 1; // expected-warning {{converting the result of '<<' to a boolean; did you mean '(1 << a) != 0'?}}
49 
50   for (a = 0; 1 << a; a++) // expected-warning {{converting the result of '<<' to a boolean; did you mean '(1 << a) != 0'?}}
51     ;
52 
53   if (a << TWO) // expected-warning {{converting the result of '<<' to a boolean; did you mean '(a << 2) != 0'?}}
54     return a;
55 
56   if (n || two)
57     // expected-warning@-1 {{converting the enum constant to a boolean}}
58     return a;
59 
60   if (n == one || two)
61     // expected-warning@-1 {{converting the enum constant to a boolean}}
62     return a;
63 
64   if (r && two)
65     // expected-warning@-1 {{converting the enum constant to a boolean}}
66     return a;
67 
68   if (two && r)
69     // expected-warning@-1 {{converting the enum constant to a boolean}}
70     return a;
71 
72   if (n == one && two)
73     // expected-warning@-1 {{converting the enum constant to a boolean}}
74     return a;
75 
76   if(1 << 5) // expected-warning {{converting the result of '<<' to a boolean always evaluates to true}}
77     return a;
78 
79   // Don't warn in macros.
80   return SHIFT(1, a);
81 }
82 
GH64356(int arg)83 int GH64356(int arg) {
84   if ((arg == 1) && (1 == 1)) return 1;
85     return 0;
86 
87   if ((64 > 32) && (32 < 64))
88     return 2;
89 
90   if ((1 == 1) && (arg == 1)) return 1;
91     return 0;
92 }
93