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;
17 int32_t test1_positive(char *I
, char *E
) {
18 return (E
- I
); // expected-warning {{implicit conversion loses integer precision}}
21 int32_t test1_negative(char *I
, char *E
) {
22 return static_cast<int32_t>(E
- I
);
25 uint32_t test2_positive(uint64_t x
) {
26 return x
; // expected-warning {{implicit conversion loses integer precision}}
29 uint32_t test2_negative(uint64_t x
) {
35 uint64_t test1(int x
, unsigned y
) {
36 return sizeof(x
== y
);
39 uint64_t test2(int x
, unsigned y
) {
40 return __alignof(x
== y
);
52 A() : x(10) {} // expected-warning {{implicit truncation from 'int' to bit-field changes value from 10 to 2}}
56 // This file tests -Wnull-conversion, a subcategory of -Wconversion
57 // which is on by default.
60 int a
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'int'}}
62 b
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'int'}}
63 long l
= NULL
; // FIXME: this should also warn, but currently does not if sizeof(NULL)==sizeof(inttype)
64 int c
= ((((NULL
)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
66 d
= ((((NULL
)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
67 bool bl
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'bool'}}
68 char ch
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'char'}}
69 unsigned char uch
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'unsigned char'}}
70 short sh
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'short'}}
71 double dbl
= NULL
; // expected-warning {{implicit conversion of NULL constant to 'double'}}
73 // Use FileCheck to ensure we don't get any unnecessary macro-expansion notes
74 // (that don't appear as 'real' notes & can't be seen/tested by -verify)
76 // CHECK: note: expanded from macro 'FINIT'
77 #define FINIT int a3 = NULL;
78 FINIT
// expected-warning {{implicit conversion of NULL constant to 'int'}}
79 // we don't catch the case of #define FOO NULL ... int i = FOO; but that
80 // seems a bit narrow anyway and avoiding that helps us skip other cases.
88 int foo::*datamem
= NULL
;
89 int (foo::*funmem
)() = NULL
;
93 // FIXME: We should warn for non-dependent args (only when the param type is also non-dependent) only once
94 // not once for the template + once for every instantiation
96 void tmpl(char c
= NULL
, // expected-warning 3 {{implicit conversion of NULL constant to 'char'}}
97 T a
= NULL
, // expected-warning {{implicit conversion of NULL constant to 'char'}} \
98 expected
-warning
{{implicit conversion of NULL constant to
'int'}}
99 T b
= 1024) { // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1024 to 0}}
103 void tmpl2(T t
= NULL
) {
107 tmpl
<char>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<char>' required here}}
108 tmpl
<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}}
120 template void func
<3>();
124 decltype(nullptr) func() {
131 bool x
= nullptr; // expected-error {{cannot initialize}}
132 if (nullptr) {} // expected-warning {{implicit conversion of nullptr constant to 'bool'}}
133 return nullptr; // expected-error {{cannot initialize}}
138 #define NULL_COND(cond) ((cond) ? &num : NULL)
139 #define NULL_WRAPPER NULL_COND(false)
141 // don't warn on NULL conversion through the conditional operator across a
145 bool b
= NULL_COND(true);
146 if (NULL_COND(true)) {}
147 while (NULL_COND(true)) {}
148 for (;NULL_COND(true);) {}
149 do {} while (NULL_COND(true));
152 while (NULL_WRAPPER
) {}
153 for (;NULL_WRAPPER
;) {}
154 do {} while (NULL_WRAPPER
);
157 // Identical to the previous function except with a template argument.
158 // This ensures that template instantiation does not introduce any new
160 template <typename X
>
161 void template_and_macro() {
163 bool b
= NULL_COND(true);
164 if (NULL_COND(true)) {}
165 while (NULL_COND(true)) {}
166 for (;NULL_COND(true);) {}
167 do {} while (NULL_COND(true));
170 while (NULL_WRAPPER
) {}
171 for (;NULL_WRAPPER
;) {}
172 do {} while (NULL_WRAPPER
);
175 // Identical to the previous function except the template argument affects
176 // the conditional statement.
177 template <typename X
>
178 void template_and_macro2() {
180 bool b
= NULL_COND(true);
181 if (NULL_COND(true)) {}
182 while (NULL_COND(true)) {}
183 for (;NULL_COND(true);) {}
184 do {} while (NULL_COND(true));
187 while (NULL_WRAPPER
) {}
188 for (;NULL_WRAPPER
;) {}
189 do {} while (NULL_WRAPPER
);
193 template_and_macro
<int>();
194 template_and_macro
<double>();
195 template_and_macro2
<int>();
196 template_and_macro2
<double>();
201 typedef decltype(nullptr) nullptr_t
;
205 return EXIT(); // expected-error {{cannot initialize}}
209 // Test NULL macro inside a macro has same warnings nullptr inside a macro.
211 #define test1(cond) \
212 ((cond) ? nullptr : NULL)
213 #define test2(cond) \
214 ((cond) ? NULL : nullptr)
216 #define assert(cond) \
217 ((cond) ? foo() : bar())
231 #define assert11(expr) ((expr) ? 0 : 0)
233 // The whitespace in macro run1 are important to trigger the macro being split
234 // over multiple SLocEntry's.
235 #define run1() (dostuff() ? \
237 #define run2() (dostuff() ? NULL : NULL)
240 void test(const char * content_type
) {
249 #define x return NULL;
252 x
// expected-warning{{}}
257 // More tests with macros. Specficially, test function-like macros that either
258 // have a pointer return type or take pointer arguments. Basically, if the
259 // macro was changed into a function and Clang doesn't warn, then it shouldn't
260 // warn for the macro either.
262 #define check_str_nullptr_13(str) ((str) ? str : nullptr)
263 #define check_str_null_13(str) ((str) ? str : NULL)
264 #define test13(condition) if (condition) return;
265 #define identity13(arg) arg
266 #define CHECK13(condition) test13(identity13(!(condition)))
268 void function1(const char* str
) {
269 CHECK13(check_str_nullptr_13(str
));
270 CHECK13(check_str_null_13(str
));
273 bool some_bool_function(bool); // expected-note {{no known conversion}}
275 CHECK13(some_bool_function(nullptr)); // expected-error {{no matching function}}
276 CHECK13(some_bool_function(NULL
)); // expected-warning {{implicit conversion of NULL constant to 'bool'}}
279 #define run_check_nullptr_13(str) \
280 if (check_str_nullptr_13(str)) return;
281 #define run_check_null_13(str) \
282 if (check_str_null_13(str)) return;
283 void function3(const char* str
) {
284 run_check_nullptr_13(str
)
285 run_check_null_13(str
)
286 if (check_str_nullptr_13(str
)) return;
287 if (check_str_null_13(str
)) return;
291 #define conditional_run_13(ptr) \
294 conditional_run_13(nullptr);
295 conditional_run_13(NULL
);