1 // RUN: %clang_cc1 -x c -fsyntax-only -verify -Wxor-used-as-pow %s
2 // RUN: %clang_cc1 -x c -fsyntax-only -verify %s
3 // RUN: %clang_cc1 -x c -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
4 
5 // RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wxor-used-as-pow %s
6 // RUN: %clang_cc1 -x c++ -fsyntax-only -verify %s
7 // RUN: %clang_cc1 -x c++ -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
8 
9 #define FOOBAR(x, y) (x * y)
10 #define XOR(x, y) (x ^ y)
11 #define TWO 2
12 #define TEN 10
13 #define IOP 64
14 #define TWO_ULL 2ULL
15 #define EPSILON 10 ^ -300
16 #define ALPHA_OFFSET 3
17 #define EXP 3
18 
19 #define flexor 7
20 
21 #ifdef __cplusplus
operator ""_xor(unsigned long long v)22 constexpr long long operator"" _xor(unsigned long long v) { return v; }
23 
operator ""_0b(unsigned long long v)24 constexpr long long operator"" _0b(unsigned long long v) { return v; }
operator ""_0X(unsigned long long v)25 constexpr long long operator"" _0X(unsigned long long v) { return v; }
26 #else
27 #define xor ^ // iso646.h
28 #endif
29 
test(unsigned a,unsigned b)30 void test(unsigned a, unsigned b) {
31   unsigned res;
32   res = a ^ 5;
33   res = 2 ^ b;
34   res = a ^ b;
35   res = 2 ^ -1;
36   res = 2 ^ 0; // expected-warning {{result of '2 ^ 0' is 2; did you mean '1 << 0' (1)?}}
37                // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:14}:"1"
38                // expected-note@-2 {{replace expression with '0x2 ^ 0' or use 'xor' instead of '^' to silence this warning}}
39   res = 2 ^ 1; // expected-warning {{result of '2 ^ 1' is 3; did you mean '1 << 1' (2)?}}
40                // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:14}:"1 << 1"
41                // expected-note@-2 {{replace expression with '0x2 ^ 1' or use 'xor' instead of '^' to silence this warning}}
42   res = 2 ^ 2; // expected-warning {{result of '2 ^ 2' is 0; did you mean '1 << 2' (4)?}}
43   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:14}:"1 << 2"
44   // expected-note@-2 {{replace expression with '0x2 ^ 2' or use 'xor' instead of '^' to silence this warning}}
45   res = 2 ^ 8; // expected-warning {{result of '2 ^ 8' is 10; did you mean '1 << 8' (256)?}}
46   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:14}:"1 << 8"
47   // expected-note@-2 {{replace expression with '0x2 ^ 8' or use 'xor' instead of '^' to silence this warning}}
48   res = 2 ^ +8; // expected-warning {{result of '2 ^ +8' is 10; did you mean '1 << +8' (256)?}}
49   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:15}:"1 << +8"
50   // expected-note@-2 {{replace expression with '0x2 ^ +8' or use 'xor' instead of '^' to silence this warning}}
51   res = TWO ^ 8; // expected-warning {{result of 'TWO ^ 8' is 10; did you mean '1 << 8' (256)?}}
52   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:16}:"1 << 8"
53   // expected-note@-2 {{replace expression with '0x2 ^ 8' or use 'xor' instead of '^' to silence this warning}}
54   res = 2 ^ 16; // expected-warning {{result of '2 ^ 16' is 18; did you mean '1 << 16' (65536)?}}
55   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:15}:"1 << 16"
56   // expected-note@-2 {{replace expression with '0x2 ^ 16' or use 'xor' instead of '^' to silence this warning}}
57   res = 2 ^ TEN; // expected-warning {{result of '2 ^ TEN' is 8; did you mean '1 << TEN' (1024)?}}
58   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:16}:"1 << TEN"
59   // expected-note@-2 {{replace expression with '0x2 ^ TEN' or use 'xor' instead of '^' to silence this warning}}
60   res = res + (2 ^ ALPHA_OFFSET); // expected-warning {{result of '2 ^ ALPHA_OFFSET' is 1; did you mean '1 << ALPHA_OFFSET' (8)?}}
61   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:32}:"1 << ALPHA_OFFSET"
62   // expected-note@-2 {{replace expression with '0x2 ^ ALPHA_OFFSET' or use 'xor' instead of '^' to silence this warning}}
63   res = 0x2 ^ 16;
64   res = 2 xor 16;
65 
66   res = 2 ^ 0x4;
67   res = 2 ^ 04;
68   res = TWO ^ TEN;
69   res = 0x2 ^ 10;
70   res = 0X2 ^ 10;
71   res = 02 ^ 10;
72   res = FOOBAR(2, 16);
73   res = 0b10 ^ 16;
74   res = 0B10 ^ 16;
75   res = 2 ^ 0b100;
76   res = XOR(2, 16);
77   unsigned char two = 2;
78   res = two ^ 16;
79   res = TWO_ULL ^ 16;
80   res = 2 ^ 32; // expected-warning {{result of '2 ^ 32' is 34; did you mean '1LL << 32'?}}
81   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:15}:"1LL << 32"
82   // expected-note@-2 {{replace expression with '0x2 ^ 32' or use 'xor' instead of '^' to silence this warning}}
83   res = 2 ^ 64; // expected-warning {{result of '2 ^ 64' is 66; did you mean exponentiation?}}
84   // expected-note@-1 {{replace expression with '0x2 ^ 64' or use 'xor' instead of '^' to silence this warning}}
85   res = 2 ^ 65;
86 
87   res = EPSILON;
88   res = 10 ^ 0; // expected-warning {{result of '10 ^ 0' is 10; did you mean '1e0'?}}
89   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:15}:"1e0"
90   // expected-note@-2 {{replace expression with '0xA ^ 0' or use 'xor' instead of '^' to silence this warning}}
91   res = 10 ^ 1; // expected-warning {{result of '10 ^ 1' is 11; did you mean '1e1'?}}
92   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:15}:"1e1"
93   // expected-note@-2 {{replace expression with '0xA ^ 1' or use 'xor' instead of '^' to silence this warning}}
94   res = 10 ^ 2; // expected-warning {{result of '10 ^ 2' is 8; did you mean '1e2'?}}
95   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:15}:"1e2"
96   // expected-note@-2 {{replace expression with '0xA ^ 2' or use 'xor' instead of '^' to silence this warning}}
97   res = 10 ^ 4; // expected-warning {{result of '10 ^ 4' is 14; did you mean '1e4'?}}
98   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:15}:"1e4"
99   // expected-note@-2 {{replace expression with '0xA ^ 4' or use 'xor' instead of '^' to silence this warning}}
100   res = 10 ^ +4; // expected-warning {{result of '10 ^ +4' is 14; did you mean '1e4'?}}
101   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:16}:"1e4"
102   // expected-note@-2 {{replace expression with '0xA ^ +4' or use 'xor' instead of '^' to silence this warning}}
103   res = 10 ^ 10; // expected-warning {{result of '10 ^ 10' is 0; did you mean '1e10'?}}
104   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:16}:"1e10"
105   // expected-note@-2 {{replace expression with '0xA ^ 10' or use 'xor' instead of '^' to silence this warning}}
106   res = TEN ^ 10; // expected-warning {{result of 'TEN ^ 10' is 0; did you mean '1e10'?}}
107   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:17}:"1e10"
108   // expected-note@-2 {{replace expression with '0xA ^ 10' or use 'xor' instead of '^' to silence this warning}}
109   res = 10 ^ TEN; // expected-warning {{result of '10 ^ TEN' is 0; did you mean '1e10'?}}
110   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:17}:"1e10"
111   // expected-note@-2 {{replace expression with '0xA ^ TEN' or use 'xor' instead of '^' to silence this warning}}
112   res = 10 ^ 100; // expected-warning {{result of '10 ^ 100' is 110; did you mean '1e100'?}}
113   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:17}:"1e100"
114   // expected-note@-2 {{replace expression with '0xA ^ 100' or use 'xor' instead of '^' to silence this warning}}
115   res = 0xA ^ 10;
116   res = 10 ^ -EXP; // expected-warning {{result of '10 ^ -EXP' is -9; did you mean '1e-3'?}}
117   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:18}:"1e-3"
118   // expected-note@-2 {{replace expression with '0xA ^ -EXP' or use 'xor' instead of '^' to silence this warning}}
119   res = 10 ^ +EXP; // expected-warning {{result of '10 ^ +EXP' is 9; did you mean '1e3'?}}
120   // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:18}:"1e3"
121   // expected-note@-2 {{replace expression with '0xA ^ +EXP' or use 'xor' instead of '^' to silence this warning}}
122   res = 10 xor 10;
123 #ifdef __cplusplus
124   res = 10 ^ 5_xor;
125   res = 10_xor ^ 5;
126   res = 10 ^ 5_0b;
127   res = 10_0X ^ 5;
128   res = 2 ^ 2'000;
129   res = 2 ^ 0b0110'1001;
130   res = 10 ^ 2'000;
131 #else
132 #undef xor
133   res = 10 ^ 1; // expected-warning {{result of '10 ^ 1' is 11; did you mean '1e1'?}}
134   // expected-note@-1 {{replace expression with '0xA ^ 1' to silence this warning}}
135   res = 2 ^ 1; // expected-warning {{result of '2 ^ 1' is 3; did you mean '1 << 1' (2)?}}
136   // expected-note@-1 {{replace expression with '0x2 ^ 1' to silence this warning}}
137 #endif
138 }
139