1 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -std=c++11 -verify %s
2 // RUN: not %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -std=c++11 %s 2>&1 | FileCheck %s
6 typedef signed char int8_t;
7 typedef signed short int16_t;
8 typedef signed int int32_t;
9 typedef signed long int64_t;
11 typedef unsigned char uint8_t;
12 typedef unsigned short uint16_t;
13 typedef unsigned int uint32_t;
14 typedef unsigned long uint64_t;
16 // <rdar://problem/7909130>
18 int32_t test1_positive(char *I
, char *E
) {
19 return (E
- I
); // expected-warning {{implicit conversion loses integer precision}}
22 int32_t test1_negative(char *I
, char *E
) {
23 return static_cast<int32_t>(E
- I
);
26 uint32_t test2_positive(uint64_t x
) {
27 return x
; // expected-warning {{implicit conversion loses integer precision}}
30 uint32_t test2_negative(uint64_t x
) {
36 uint64_t test1(int x
, unsigned y
) {
37 return sizeof(x
== y
);
40 uint64_t test2(int x
, unsigned y
) {
41 return __alignof(x
== y
);
53 A() : x(10) {} // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}}
57 // This file tests -Wnull-conversion, a subcategory of -Wconversion
58 // which is on by default.
61 int a
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'int'}}
63 b
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'int'}}
64 long l
= NULL
; // FIXME: this should also warn, but currently does not if sizeof(NULL)==sizeof(inttype)
65 int c
= ((((NULL
)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
67 d
= ((((NULL
)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
68 bool bl
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'bool'}}
69 char ch
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'char'}}
70 unsigned char uch
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'unsigned char'}}
71 short sh
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'short'}}
72 double dbl
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'double'}}
74 // Use FileCheck to ensure we don't get any unnecessary macro-expansion notes
75 // (that don't appear as 'real' notes & can't be seen/tested by -verify)
77 // CHECK: note: expanded from macro 'FINIT'
78 #define FINIT int a3 = NULL;
79 FINIT
// expected-warning {{implicit conversion of NULL constant to 'int'}}
80 // we don't catch the case of #define FOO NULL ... int i = FOO; but that
81 // seems a bit narrow anyway and avoiding that helps us skip other cases.
89 int foo::*datamem
= NULL
;
90 int (foo::*funmem
)() = NULL
;
94 // FIXME: We should warn for non-dependent args (only when the param type is also non-dependent) only once
95 // not once for the template + once for every instantiation
97 void tmpl(char c
= NULL
, // expected-warning 3 {{implicit conversion of NULL constant to 'char'}}
98 T a
= NULL
, // expected-warning {{implicit conversion of NULL constant to 'char'}} \
99 expected
-warning
{{implicit conversion of NULL constant to
'int'}}
100 T b
= 1024) { // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1024 to 0}}
104 void tmpl2(T t
= NULL
) {
108 tmpl
<char>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<char>' required here}}
109 tmpl
<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}}
121 template void func
<3>();
125 decltype(nullptr) func() {
132 bool x
= nullptr; // expected-error {{cannot initialize}}
133 if (nullptr) {} // expected-warning {{implicit conversion of nullptr constant to 'bool'}}
134 return nullptr; // expected-error {{cannot initialize}}
139 #define NULL_COND(cond) ((cond) ? &num : NULL)
140 #define NULL_WRAPPER NULL_COND(false)
142 // don't warn on NULL conversion through the conditional operator across a
146 bool b
= NULL_COND(true);
147 if (NULL_COND(true)) {}
148 while (NULL_COND(true)) {}
149 for (;NULL_COND(true);) {}
150 do {} while (NULL_COND(true));
153 while (NULL_WRAPPER
) {}
154 for (;NULL_WRAPPER
;) {}
155 do {} while (NULL_WRAPPER
);
158 // Identical to the previous function except with a template argument.
159 // This ensures that template instantiation does not introduce any new
161 template <typename X
>
162 void template_and_macro() {
164 bool b
= NULL_COND(true);
165 if (NULL_COND(true)) {}
166 while (NULL_COND(true)) {}
167 for (;NULL_COND(true);) {}
168 do {} while (NULL_COND(true));
171 while (NULL_WRAPPER
) {}
172 for (;NULL_WRAPPER
;) {}
173 do {} while (NULL_WRAPPER
);
176 // Identical to the previous function except the template argument affects
177 // the conditional statement.
178 template <typename X
>
179 void template_and_macro2() {
181 bool b
= NULL_COND(true);
182 if (NULL_COND(true)) {}
183 while (NULL_COND(true)) {}
184 for (;NULL_COND(true);) {}
185 do {} while (NULL_COND(true));
188 while (NULL_WRAPPER
) {}
189 for (;NULL_WRAPPER
;) {}
190 do {} while (NULL_WRAPPER
);
194 template_and_macro
<int>();
195 template_and_macro
<double>();
196 template_and_macro2
<int>();
197 template_and_macro2
<double>();
202 typedef decltype(nullptr) nullptr_t
;
206 return EXIT(); // expected-error {{cannot initialize}}
210 // Test NULL macro inside a macro has same warnings nullptr inside a macro.
212 #define test1(cond) \
213 ((cond) ? nullptr : NULL)
214 #define test2(cond) \
215 ((cond) ? NULL : nullptr)
217 #define assert(cond) \
218 ((cond) ? foo() : bar())
232 #define assert11(expr) ((expr) ? 0 : 0)
234 // The whitespace in macro run1 are important to trigger the macro being split
235 // over multiple SLocEntry's.
236 #define run1() (dostuff() ? \
238 #define run2() (dostuff() ? NULL : NULL)
241 void test(const char * content_type
) {
250 #define x return NULL;
253 x
// expected-warning{{}}
258 // More tests with macros. Specficially, test function-like macros that either
259 // have a pointer return type or take pointer arguments. Basically, if the
260 // macro was changed into a function and Clang doesn't warn, then it shouldn't
261 // warn for the macro either.
263 #define check_str_nullptr_13(str) ((str) ? str : nullptr)
264 #define check_str_null_13(str) ((str) ? str : NULL)
265 #define test13(condition) if (condition) return;
266 #define identity13(arg) arg
267 #define CHECK13(condition) test13(identity13(!(condition)))
269 void function1(const char* str
) {
270 CHECK13(check_str_nullptr_13(str
));
271 CHECK13(check_str_null_13(str
));
274 bool some_bool_function(bool); // expected-note {{no known conversion}}
276 CHECK13(some_bool_function(nullptr)); // expected-error {{no matching function}}
277 CHECK13(some_bool_function(NULL
)); // expected-warning {{implicit conversion of NULL constant to 'bool'}}
280 #define run_check_nullptr_13(str) \
281 if (check_str_nullptr_13(str)) return;
282 #define run_check_null_13(str) \
283 if (check_str_null_13(str)) return;
284 void function3(const char* str
) {
285 run_check_nullptr_13(str
)
286 run_check_null_13(str
)
287 if (check_str_nullptr_13(str
)) return;
288 if (check_str_null_13(str
)) return;
292 #define conditional_run_13(ptr) \
295 conditional_run_13(nullptr);
296 conditional_run_13(NULL
);