xref: /llvm-project/clang/test/Sema/warn-bitwise-or-bool.c (revision ab28c1de23f3880d7d2becf936fe560abe68e020)
1 // RUN: %clang_cc1 -x c -fsyntax-only -verify -Wbool-operation %s
2 // RUN: %clang_cc1 -x c -fsyntax-only -verify -Wall %s
3 // RUN: %clang_cc1 -x c -fsyntax-only -Wbitwise-instead-of-logical -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
4 // RUN: %clang_cc1 -x c -fsyntax-only -Wbool-operation -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
5 // RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wbool-operation %s
6 // RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wall %s
7 // RUN: %clang_cc1 -x c++ -fsyntax-only -Wbitwise-instead-of-logical -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
8 // RUN: %clang_cc1 -x c++ -fsyntax-only -Wbool-operation -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
9 
10 #ifdef __cplusplus
11 typedef bool boolean;
12 #else
13 typedef _Bool boolean;
14 #endif
15 
16 boolean foo(void);
17 boolean bar(void);
18 boolean baz(void) __attribute__((const));
19 void sink(boolean);
20 
21 #define FOO foo()
22 #define MY_OR |
23 #define My_BITOR bitor
24 
test(boolean a,boolean b,int * p,volatile int * q,int i)25 void test(boolean a, boolean b, int *p, volatile int *q, int i) {
26   b = a | b;
27   b = foo() | a;
28   b = (p != 0) | (*p == 42);   // FIXME: also warn for a non-volatile pointer dereference
29   b = foo() | (*q == 42);      // expected-warning {{use of bitwise '|' with boolean operands}}
30                                // expected-note@-1 {{cast one or both operands to int to silence this warning}}
31   b = foo() | (int)(*q == 42); // OK, no warning expected
32   b = a | foo();
33   b = (int)a | foo();     // OK, no warning expected
34   b = foo() | bar();      // expected-warning {{use of bitwise '|' with boolean operands}}
35                           // expected-note@-1 {{cast one or both operands to int to silence this warning}}
36                           // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:14}:"||"
37   b = foo() | !bar();     // expected-warning {{use of bitwise '|' with boolean operands}}
38                           // expected-note@-1 {{cast one or both operands to int to silence this warning}}
39                           // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:14}:"||"
40   b = foo() | (int)bar(); // OK, no warning expected
41   b = a | baz();
42   b = bar() | FOO;        // expected-warning {{use of bitwise '|' with boolean operands}}
43                           // expected-note@-1 {{cast one or both operands to int to silence this warning}}
44                           // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:14}:"||"
45   b = foo() | (int)FOO;   // OK, no warning expected
46   b = b | foo();
47   b = bar() | (i > 4);
48   b = (i == 7) | foo();
49   b = b MY_OR foo();     // OK, no warning expected
50 #ifdef __cplusplus
51   b = foo() bitor bar();  //Ok, no warning expected
52   b = foo() My_BITOR bar(); // Ok, no warning expected
53 
54 #endif
55 
56   if (foo() | bar())      // expected-warning {{use of bitwise '|' with boolean operands}}
57                           // expected-note@-1 {{cast one or both operands to int to silence this warning}}
58     ;
59 
60   sink(a | b);
61   sink(a | foo());
62   sink(foo() | bar());    // expected-warning {{use of bitwise '|' with boolean operands}}
63                           // expected-note@-1 {{cast one or both operands to int to silence this warning}}
64 
65   int n = i + 10;
66   b = (n | (n - 1));
67 }
68