[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / expressions.cpp
blob75136083dfc66c3a1ee27fe2118270273932848b
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion -std=c++98 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-constant-conversion -std=c++11 %s
5 void choice(int);
6 int choice(bool);
8 void test() {
9 // Result of ! must be type bool.
10 int i = choice(!1);
13 #if __cplusplus < 201703L
14 void f0() {
15 extern void f0_1(int*);
16 register int x;
17 #if __cplusplus >= 201103L // C++11 or later
18 // expected-warning@-2 {{'register' storage class specifier is deprecated}}
19 #endif
20 f0_1(&x);
22 #endif
24 namespace test1 {
25 template <class T> void bar(T &x) { T::fail(); }
26 template <class T> void bar(volatile T &x) {}
28 void test_ints() {
29 volatile int x;
30 bar(x = 5);
31 bar(x += 5);
34 enum E { E_zero };
35 void test_enums() {
36 volatile E x;
37 bar(x = E_zero);
38 bar(x += E_zero); // expected-error {{incompatible type}}
42 int test2(int x) {
43 return x && 4; // expected-warning {{use of logical '&&' with constant operand}} \
44 // expected-note {{use '&' for a bitwise operation}} \
45 // expected-note {{remove constant to silence this warning}}
47 return x && sizeof(int) == 4; // no warning, RHS is logical op.
48 return x && true;
49 return x && false;
50 return x || true;
51 return x || false;
53 return x && (unsigned)0; // expected-warning {{use of logical '&&' with constant operand}} \
54 // expected-note {{use '&' for a bitwise operation}} \
55 // expected-note {{remove constant to silence this warning}}
57 return x || (unsigned)1; // expected-warning {{use of logical '||' with constant operand}} \
58 // expected-note {{use '|' for a bitwise operation}}
60 return x || 0; // expected-warning {{use of logical '||' with constant operand}} \
61 // expected-note {{use '|' for a bitwise operation}}
62 return x || 1; // expected-warning {{use of logical '||' with constant operand}} \
63 // expected-note {{use '|' for a bitwise operation}}
64 return x || -1; // expected-warning {{use of logical '||' with constant operand}} \
65 // expected-note {{use '|' for a bitwise operation}}
66 return x || 5; // expected-warning {{use of logical '||' with constant operand}} \
67 // expected-note {{use '|' for a bitwise operation}}
68 return x && 0; // expected-warning {{use of logical '&&' with constant operand}} \
69 // expected-note {{use '&' for a bitwise operation}} \
70 // expected-note {{remove constant to silence this warning}}
71 return x && 1; // expected-warning {{use of logical '&&' with constant operand}} \
72 // expected-note {{use '&' for a bitwise operation}} \
73 // expected-note {{remove constant to silence this warning}}
74 return x && -1; // expected-warning {{use of logical '&&' with constant operand}} \
75 // expected-note {{use '&' for a bitwise operation}} \
76 // expected-note {{remove constant to silence this warning}}
77 return x && 5; // expected-warning {{use of logical '&&' with constant operand}} \
78 // expected-note {{use '&' for a bitwise operation}} \
79 // expected-note {{remove constant to silence this warning}}
80 return x || (0); // expected-warning {{use of logical '||' with constant operand}} \
81 // expected-note {{use '|' for a bitwise operation}}
82 return x || (1); // expected-warning {{use of logical '||' with constant operand}} \
83 // expected-note {{use '|' for a bitwise operation}}
84 return x || (-1); // expected-warning {{use of logical '||' with constant operand}} \
85 // expected-note {{use '|' for a bitwise operation}}
86 return x || (5); // expected-warning {{use of logical '||' with constant operand}} \
87 // expected-note {{use '|' for a bitwise operation}}
88 return x && (0); // expected-warning {{use of logical '&&' with constant operand}} \
89 // expected-note {{use '&' for a bitwise operation}} \
90 // expected-note {{remove constant to silence this warning}}
91 return x && (1); // expected-warning {{use of logical '&&' with constant operand}} \
92 // expected-note {{use '&' for a bitwise operation}} \
93 // expected-note {{remove constant to silence this warning}}
94 return x && (-1); // expected-warning {{use of logical '&&' with constant operand}} \
95 // expected-note {{use '&' for a bitwise operation}} \
96 // expected-note {{remove constant to silence this warning}}
97 return x && (5); // expected-warning {{use of logical '&&' with constant operand}} \
98 // expected-note {{use '&' for a bitwise operation}} \
99 // expected-note {{remove constant to silence this warning}}
102 template<unsigned int A, unsigned int B> struct S
104 enum {
105 e1 = A && B,
106 e2 = A && 7 // expected-warning {{use of logical '&&' with constant operand}} \
107 // expected-note {{use '&' for a bitwise operation}} \
108 // expected-note {{remove constant to silence this warning}}
111 int foo() {
112 int x = A && B;
113 int y = B && 3; // expected-warning {{use of logical '&&' with constant operand}} \
114 // expected-note {{use '&' for a bitwise operation}} \
115 // expected-note {{remove constant to silence this warning}}
117 return x + y;
121 void test3() {
122 S<5, 8> s1;
123 S<2, 7> s2;
124 (void)s1.foo();
125 (void)s2.foo();
128 namespace pr16992 {
129 typedef int T;
130 unsigned getsz() {
131 return (sizeof T());
135 void test4() {
136 #define X 0
137 #define Y 1
138 bool r1 = X || Y;
140 #define Y2 2
141 bool r2 = X || Y2; // expected-warning {{use of logical '||' with constant operand}} \
142 // expected-note {{use '|' for a bitwise operation}}