annotate clang/test/SemaCXX/warn-bool-conversion.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 79ff65ed7e25
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -fsyntax-only -verify=expected,expected-cxx11 %s
anatofuz
parents:
diff changeset
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
anatofuz
parents:
diff changeset
3 // RUN: %clang_cc1 -fsyntax-only -verify=expected,expected-cxx11 -std=c++11 %s
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 namespace BooleanFalse {
anatofuz
parents:
diff changeset
6 int* j = false;
anatofuz
parents:
diff changeset
7 #if __cplusplus <= 199711L
anatofuz
parents:
diff changeset
8 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
anatofuz
parents:
diff changeset
9 #else
anatofuz
parents:
diff changeset
10 // expected-error@-4 {{cannot initialize a variable of type 'int *' with an rvalue of type 'bool'}}
anatofuz
parents:
diff changeset
11 #endif
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #if __cplusplus <= 199711L
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
14 // expected-warning@+5 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
150
anatofuz
parents:
diff changeset
15 #else
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
16 // expected-error@+3 {{cannot initialize a parameter of type 'int *' with an rvalue of type 'bool'}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
17 // expected-note@+2 {{passing argument to parameter 'j' here}}
150
anatofuz
parents:
diff changeset
18 #endif
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
19 void bar(int *j = false);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
20
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
21 #if __cplusplus > 199711L
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
22 // expected-note@+2 4{{candidate function not viable: no known conversion}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
23 #endif
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
24 void foo(int *i)
150
anatofuz
parents:
diff changeset
25 {
anatofuz
parents:
diff changeset
26 foo(false);
anatofuz
parents:
diff changeset
27 #if __cplusplus <= 199711L
anatofuz
parents:
diff changeset
28 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
anatofuz
parents:
diff changeset
29 #else
anatofuz
parents:
diff changeset
30 // expected-error@-4 {{no matching function for call to 'foo'}}
anatofuz
parents:
diff changeset
31 #endif
anatofuz
parents:
diff changeset
32
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
33 foo((int*)false); // OK: explicit cast
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
34 foo(0); // OK: not a bool, even though it's convertible to bool
150
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 foo(false == true);
anatofuz
parents:
diff changeset
37 #if __cplusplus <= 199711L
anatofuz
parents:
diff changeset
38 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
anatofuz
parents:
diff changeset
39 #else
anatofuz
parents:
diff changeset
40 // expected-error@-4 {{no matching function for call to 'foo'}}
anatofuz
parents:
diff changeset
41 #endif
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 foo((42 + 24) < 32);
anatofuz
parents:
diff changeset
44 #if __cplusplus <= 199711L
anatofuz
parents:
diff changeset
45 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
anatofuz
parents:
diff changeset
46 #else
anatofuz
parents:
diff changeset
47 // expected-error@-4 {{no matching function for call to 'foo'}}
anatofuz
parents:
diff changeset
48 #endif
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 const bool kFlag = false;
anatofuz
parents:
diff changeset
51 foo(kFlag);
anatofuz
parents:
diff changeset
52 #if __cplusplus <= 199711L
anatofuz
parents:
diff changeset
53 // expected-warning@-2 {{initialization of pointer of type 'int *' to null from a constant boolean expression}}
anatofuz
parents:
diff changeset
54 #else
anatofuz
parents:
diff changeset
55 // expected-error@-4 {{no matching function for call to 'foo'}}
anatofuz
parents:
diff changeset
56 #endif
anatofuz
parents:
diff changeset
57 }
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 char f(struct Undefined*);
anatofuz
parents:
diff changeset
60 double f(...);
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 // Ensure that when using false in metaprogramming machinery its conversion
anatofuz
parents:
diff changeset
63 // isn't flagged.
anatofuz
parents:
diff changeset
64 template <int N> struct S {};
anatofuz
parents:
diff changeset
65 S<sizeof(f(false))> s;
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 namespace Function {
anatofuz
parents:
diff changeset
70 void f1();
anatofuz
parents:
diff changeset
71
anatofuz
parents:
diff changeset
72 struct S {
anatofuz
parents:
diff changeset
73 static void f2();
anatofuz
parents:
diff changeset
74 };
anatofuz
parents:
diff changeset
75
anatofuz
parents:
diff changeset
76 extern void f3() __attribute__((weak_import));
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 struct S2 {
anatofuz
parents:
diff changeset
79 static void f4() __attribute__((weak_import));
anatofuz
parents:
diff changeset
80 };
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 bool f5();
anatofuz
parents:
diff changeset
83 bool f6(int);
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 void bar() {
anatofuz
parents:
diff changeset
86 bool b;
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 b = f1; // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
anatofuz
parents:
diff changeset
89 expected-note {{prefix with the address-of operator to silence this warning}}
anatofuz
parents:
diff changeset
90 if (f1) {} // expected-warning {{address of function 'f1' will always evaluate to 'true'}} \
anatofuz
parents:
diff changeset
91 expected-note {{prefix with the address-of operator to silence this warning}}
anatofuz
parents:
diff changeset
92 b = S::f2; // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
anatofuz
parents:
diff changeset
93 expected-note {{prefix with the address-of operator to silence this warning}}
anatofuz
parents:
diff changeset
94 if (S::f2) {} // expected-warning {{address of function 'S::f2' will always evaluate to 'true'}} \
anatofuz
parents:
diff changeset
95 expected-note {{prefix with the address-of operator to silence this warning}}
anatofuz
parents:
diff changeset
96 b = f5; // expected-warning {{address of function 'f5' will always evaluate to 'true'}} \
anatofuz
parents:
diff changeset
97 expected-note {{prefix with the address-of operator to silence this warning}} \
anatofuz
parents:
diff changeset
98 expected-note {{suffix with parentheses to turn this into a function call}}
anatofuz
parents:
diff changeset
99 b = f6; // expected-warning {{address of function 'f6' will always evaluate to 'true'}} \
anatofuz
parents:
diff changeset
100 expected-note {{prefix with the address-of operator to silence this warning}}
anatofuz
parents:
diff changeset
101
anatofuz
parents:
diff changeset
102 // implicit casts of weakly imported symbols are ok:
anatofuz
parents:
diff changeset
103 b = f3;
anatofuz
parents:
diff changeset
104 if (f3) {}
anatofuz
parents:
diff changeset
105 b = S2::f4;
anatofuz
parents:
diff changeset
106 if (S2::f4) {}
anatofuz
parents:
diff changeset
107 }
anatofuz
parents:
diff changeset
108 }
anatofuz
parents:
diff changeset
109
anatofuz
parents:
diff changeset
110 namespace Array {
anatofuz
parents:
diff changeset
111 #define GetValue(ptr) ((ptr) ? ptr[0] : 0)
anatofuz
parents:
diff changeset
112 extern int a[] __attribute__((weak));
anatofuz
parents:
diff changeset
113 int b[] = {8,13,21};
anatofuz
parents:
diff changeset
114 struct {
anatofuz
parents:
diff changeset
115 int x[10];
anatofuz
parents:
diff changeset
116 } c;
anatofuz
parents:
diff changeset
117 const char str[] = "text";
anatofuz
parents:
diff changeset
118 void ignore() {
anatofuz
parents:
diff changeset
119 if (a) {}
anatofuz
parents:
diff changeset
120 if (a) {}
anatofuz
parents:
diff changeset
121 (void)GetValue(b);
anatofuz
parents:
diff changeset
122 }
anatofuz
parents:
diff changeset
123 void test() {
anatofuz
parents:
diff changeset
124 if (b) {}
anatofuz
parents:
diff changeset
125 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
126 if (b) {}
anatofuz
parents:
diff changeset
127 // expected-warning@-1{{address of array 'b' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
128 if (c.x) {}
anatofuz
parents:
diff changeset
129 // expected-warning@-1{{address of array 'c.x' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
130 if (str) {}
anatofuz
parents:
diff changeset
131 // expected-warning@-1{{address of array 'str' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
132 }
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 namespace Pointer {
anatofuz
parents:
diff changeset
136 extern int a __attribute__((weak));
anatofuz
parents:
diff changeset
137 int b;
anatofuz
parents:
diff changeset
138 static int c;
anatofuz
parents:
diff changeset
139 class S {
anatofuz
parents:
diff changeset
140 public:
anatofuz
parents:
diff changeset
141 static int a;
anatofuz
parents:
diff changeset
142 int b;
anatofuz
parents:
diff changeset
143 };
anatofuz
parents:
diff changeset
144 void ignored() {
anatofuz
parents:
diff changeset
145 if (&a) {}
anatofuz
parents:
diff changeset
146 }
anatofuz
parents:
diff changeset
147 void test() {
anatofuz
parents:
diff changeset
148 S s;
anatofuz
parents:
diff changeset
149 if (&b) {}
anatofuz
parents:
diff changeset
150 // expected-warning@-1{{address of 'b' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
151 if (&c) {}
anatofuz
parents:
diff changeset
152 // expected-warning@-1{{address of 'c' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
153 if (&s.a) {}
anatofuz
parents:
diff changeset
154 // expected-warning@-1{{address of 's.a' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
155 if (&s.b) {}
anatofuz
parents:
diff changeset
156 // expected-warning@-1{{address of 's.b' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
157 if (&S::a) {}
anatofuz
parents:
diff changeset
158 // expected-warning@-1{{address of 'S::a' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
159 }
anatofuz
parents:
diff changeset
160 }
anatofuz
parents:
diff changeset
161
anatofuz
parents:
diff changeset
162 namespace macros {
anatofuz
parents:
diff changeset
163 #define assert(x) if (x) {}
anatofuz
parents:
diff changeset
164 #define zero_on_null(x) ((x) ? *(x) : 0)
anatofuz
parents:
diff changeset
165
anatofuz
parents:
diff changeset
166 int array[5];
anatofuz
parents:
diff changeset
167 void fun();
anatofuz
parents:
diff changeset
168 int x;
anatofuz
parents:
diff changeset
169
anatofuz
parents:
diff changeset
170 void test() {
anatofuz
parents:
diff changeset
171 assert(array);
anatofuz
parents:
diff changeset
172 assert(array && "expecting null pointer");
anatofuz
parents:
diff changeset
173 // expected-warning@-1{{address of array 'array' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
174
anatofuz
parents:
diff changeset
175 assert(fun);
anatofuz
parents:
diff changeset
176 assert(fun && "expecting null pointer");
anatofuz
parents:
diff changeset
177 // expected-warning@-1{{address of function 'fun' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
178 // expected-note@-2 {{prefix with the address-of operator to silence this warning}}
anatofuz
parents:
diff changeset
179
anatofuz
parents:
diff changeset
180 // TODO: warn on assert(&x) while not warning on zero_on_null(&x)
anatofuz
parents:
diff changeset
181 zero_on_null(&x);
anatofuz
parents:
diff changeset
182 assert(zero_on_null(&x));
anatofuz
parents:
diff changeset
183 assert(&x);
anatofuz
parents:
diff changeset
184 assert(&x && "expecting null pointer");
anatofuz
parents:
diff changeset
185 // expected-warning@-1{{address of 'x' will always evaluate to 'true'}}
anatofuz
parents:
diff changeset
186 }
anatofuz
parents:
diff changeset
187 }
anatofuz
parents:
diff changeset
188
anatofuz
parents:
diff changeset
189 namespace Template {
anatofuz
parents:
diff changeset
190 // FIXME: These cases should not warn.
anatofuz
parents:
diff changeset
191 template<int *p> void f() { if (p) {} } // expected-warning 2{{will always evaluate to 'true'}} expected-cxx11-warning {{implicit conversion of nullptr}}
anatofuz
parents:
diff changeset
192 template<int (*p)[3]> void g() { if (p) {} } // expected-warning 2{{will always evaluate to 'true'}} expected-cxx11-warning {{implicit conversion of nullptr}}
anatofuz
parents:
diff changeset
193 template<int (*p)()> void h() { if (p) {} }
anatofuz
parents:
diff changeset
194
anatofuz
parents:
diff changeset
195 int a, b[3], c[3][3], d();
anatofuz
parents:
diff changeset
196 template void f<&a>(); // expected-note {{instantiation of}}
anatofuz
parents:
diff changeset
197 template void f<b>(); // expected-note {{instantiation of}}
anatofuz
parents:
diff changeset
198 #if __cplusplus >= 201103L
anatofuz
parents:
diff changeset
199 template void f<(int*)nullptr>(); // expected-note {{instantiation of}}
anatofuz
parents:
diff changeset
200 #endif
anatofuz
parents:
diff changeset
201 template void g<&b>(); // expected-note {{instantiation of}}
anatofuz
parents:
diff changeset
202 template void g<c>(); // expected-note {{instantiation of}}
anatofuz
parents:
diff changeset
203 #if __cplusplus >= 201103L
anatofuz
parents:
diff changeset
204 template void g<(int(*)[3])nullptr>(); // expected-note {{instantiation of}}
anatofuz
parents:
diff changeset
205 #endif
anatofuz
parents:
diff changeset
206 template void h<d>();
anatofuz
parents:
diff changeset
207 }