1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -std=c++11 -verify %s
2f4a2713aSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -std=c++11 %s 2>&1 | FileCheck %s
3f4a2713aSLionel Sambuc
4f4a2713aSLionel Sambuc #include <stddef.h>
5f4a2713aSLionel Sambuc
6f4a2713aSLionel Sambuc typedef signed char int8_t;
7f4a2713aSLionel Sambuc typedef signed short int16_t;
8f4a2713aSLionel Sambuc typedef signed int int32_t;
9f4a2713aSLionel Sambuc typedef signed long int64_t;
10f4a2713aSLionel Sambuc
11f4a2713aSLionel Sambuc typedef unsigned char uint8_t;
12f4a2713aSLionel Sambuc typedef unsigned short uint16_t;
13f4a2713aSLionel Sambuc typedef unsigned int uint32_t;
14f4a2713aSLionel Sambuc typedef unsigned long uint64_t;
15f4a2713aSLionel Sambuc
16f4a2713aSLionel Sambuc // <rdar://problem/7909130>
17f4a2713aSLionel Sambuc namespace test0 {
test1_positive(char * I,char * E)18f4a2713aSLionel Sambuc int32_t test1_positive(char *I, char *E) {
19f4a2713aSLionel Sambuc return (E - I); // expected-warning {{implicit conversion loses integer precision}}
20f4a2713aSLionel Sambuc }
21f4a2713aSLionel Sambuc
test1_negative(char * I,char * E)22f4a2713aSLionel Sambuc int32_t test1_negative(char *I, char *E) {
23f4a2713aSLionel Sambuc return static_cast<int32_t>(E - I);
24f4a2713aSLionel Sambuc }
25f4a2713aSLionel Sambuc
test2_positive(uint64_t x)26f4a2713aSLionel Sambuc uint32_t test2_positive(uint64_t x) {
27f4a2713aSLionel Sambuc return x; // expected-warning {{implicit conversion loses integer precision}}
28f4a2713aSLionel Sambuc }
29f4a2713aSLionel Sambuc
test2_negative(uint64_t x)30f4a2713aSLionel Sambuc uint32_t test2_negative(uint64_t x) {
31f4a2713aSLionel Sambuc return (uint32_t) x;
32f4a2713aSLionel Sambuc }
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc namespace test1 {
test1(int x,unsigned y)36f4a2713aSLionel Sambuc uint64_t test1(int x, unsigned y) {
37f4a2713aSLionel Sambuc return sizeof(x == y);
38f4a2713aSLionel Sambuc }
39f4a2713aSLionel Sambuc
test2(int x,unsigned y)40f4a2713aSLionel Sambuc uint64_t test2(int x, unsigned y) {
41f4a2713aSLionel Sambuc return __alignof(x == y);
42f4a2713aSLionel Sambuc }
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc void * const foo();
test2(void * p)45f4a2713aSLionel Sambuc bool test2(void *p) {
46f4a2713aSLionel Sambuc return p == foo();
47f4a2713aSLionel Sambuc }
48f4a2713aSLionel Sambuc }
49f4a2713aSLionel Sambuc
50f4a2713aSLionel Sambuc namespace test2 {
51f4a2713aSLionel Sambuc struct A {
52f4a2713aSLionel Sambuc unsigned int x : 2;
Atest2::A53f4a2713aSLionel Sambuc A() : x(10) {} // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
54f4a2713aSLionel Sambuc };
55f4a2713aSLionel Sambuc }
56f4a2713aSLionel Sambuc
57f4a2713aSLionel Sambuc // This file tests -Wnull-conversion, a subcategory of -Wconversion
58f4a2713aSLionel Sambuc // which is on by default.
59f4a2713aSLionel Sambuc
test3()60f4a2713aSLionel Sambuc void test3() {
61f4a2713aSLionel Sambuc int a = NULL; // expected-warning {{implicit conversion of NULL constant to 'int'}}
62f4a2713aSLionel Sambuc int b;
63f4a2713aSLionel Sambuc b = NULL; // expected-warning {{implicit conversion of NULL constant to 'int'}}
64f4a2713aSLionel Sambuc long l = NULL; // FIXME: this should also warn, but currently does not if sizeof(NULL)==sizeof(inttype)
65f4a2713aSLionel Sambuc int c = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
66f4a2713aSLionel Sambuc int d;
67f4a2713aSLionel Sambuc d = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
68f4a2713aSLionel Sambuc bool bl = NULL; // expected-warning {{implicit conversion of NULL constant to 'bool'}}
69f4a2713aSLionel Sambuc char ch = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
70f4a2713aSLionel Sambuc unsigned char uch = NULL; // expected-warning {{implicit conversion of NULL constant to 'unsigned char'}}
71f4a2713aSLionel Sambuc short sh = NULL; // expected-warning {{implicit conversion of NULL constant to 'short'}}
72f4a2713aSLionel Sambuc double dbl = NULL; // expected-warning {{implicit conversion of NULL constant to 'double'}}
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc // Use FileCheck to ensure we don't get any unnecessary macro-expansion notes
75f4a2713aSLionel Sambuc // (that don't appear as 'real' notes & can't be seen/tested by -verify)
76f4a2713aSLionel Sambuc // CHECK-NOT: note:
77f4a2713aSLionel Sambuc // CHECK: note: expanded from macro 'FINIT'
78f4a2713aSLionel Sambuc #define FINIT int a3 = NULL;
79f4a2713aSLionel Sambuc FINIT // expected-warning {{implicit conversion of NULL constant to 'int'}}
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc // we don't catch the case of #define FOO NULL ... int i = FOO; but that seems a bit narrow anyway
82f4a2713aSLionel Sambuc // and avoiding that helps us skip these cases:
83f4a2713aSLionel Sambuc #define NULL_COND(cond) ((cond) ? &a : NULL)
84f4a2713aSLionel Sambuc bool bl2 = NULL_COND(true); // don't warn on NULL conversion through the conditional operator across a macro boundary
85f4a2713aSLionel Sambuc if (NULL_COND(true))
86f4a2713aSLionel Sambuc ;
87f4a2713aSLionel Sambuc while (NULL_COND(true))
88f4a2713aSLionel Sambuc ;
89f4a2713aSLionel Sambuc for (; NULL_COND(true); )
90f4a2713aSLionel Sambuc ;
91f4a2713aSLionel Sambuc do ;
92f4a2713aSLionel Sambuc while(NULL_COND(true));
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc #define NULL_WRAPPER NULL_COND(false)
95*0a6a1f1dSLionel Sambuc if (NULL_WRAPPER)
96*0a6a1f1dSLionel Sambuc ;
97*0a6a1f1dSLionel Sambuc while (NULL_WRAPPER)
98*0a6a1f1dSLionel Sambuc ;
99*0a6a1f1dSLionel Sambuc for (; NULL_WRAPPER;)
100*0a6a1f1dSLionel Sambuc ;
101*0a6a1f1dSLionel Sambuc do
102*0a6a1f1dSLionel Sambuc ;
103*0a6a1f1dSLionel Sambuc while (NULL_WRAPPER);
104*0a6a1f1dSLionel Sambuc
105f4a2713aSLionel Sambuc int *ip = NULL;
106f4a2713aSLionel Sambuc int (*fp)() = NULL;
107f4a2713aSLionel Sambuc struct foo {
108f4a2713aSLionel Sambuc int n;
109f4a2713aSLionel Sambuc void func();
110f4a2713aSLionel Sambuc };
111f4a2713aSLionel Sambuc int foo::*datamem = NULL;
112f4a2713aSLionel Sambuc int (foo::*funmem)() = NULL;
113f4a2713aSLionel Sambuc }
114f4a2713aSLionel Sambuc
115f4a2713aSLionel Sambuc namespace test4 {
116f4a2713aSLionel Sambuc // FIXME: We should warn for non-dependent args (only when the param type is also non-dependent) only once
117f4a2713aSLionel Sambuc // not once for the template + once for every instantiation
118f4a2713aSLionel Sambuc template<typename T>
tmpl(char c=NULL,T a=NULL,T b=1024)119f4a2713aSLionel Sambuc void tmpl(char c = NULL, // expected-warning 4 {{implicit conversion of NULL constant to 'char'}}
120f4a2713aSLionel Sambuc T a = NULL, // expected-warning {{implicit conversion of NULL constant to 'char'}} \
121f4a2713aSLionel Sambuc expected-warning 2 {{implicit conversion of NULL constant to 'int'}}
122f4a2713aSLionel Sambuc T b = 1024) { // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1024 to 0}}
123f4a2713aSLionel Sambuc }
124f4a2713aSLionel Sambuc
125f4a2713aSLionel Sambuc template<typename T>
tmpl2(T t=NULL)126f4a2713aSLionel Sambuc void tmpl2(T t = NULL) {
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc
func()129f4a2713aSLionel Sambuc void func() {
130f4a2713aSLionel Sambuc tmpl<char>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<char>' required here}}
131f4a2713aSLionel Sambuc tmpl<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}}
132f4a2713aSLionel Sambuc // FIXME: We should warn only once for each template instantiation - not once for each call
133f4a2713aSLionel Sambuc tmpl<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}}
134f4a2713aSLionel Sambuc tmpl2<int*>();
135f4a2713aSLionel Sambuc }
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc namespace test5 {
139f4a2713aSLionel Sambuc template<int I>
func()140f4a2713aSLionel Sambuc void func() {
141f4a2713aSLionel Sambuc bool b = I;
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc
144f4a2713aSLionel Sambuc template void func<3>();
145f4a2713aSLionel Sambuc }
146f4a2713aSLionel Sambuc
147f4a2713aSLionel Sambuc namespace test6 {
func()148f4a2713aSLionel Sambuc decltype(nullptr) func() {
149f4a2713aSLionel Sambuc return NULL;
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc }
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc namespace test7 {
fun()154*0a6a1f1dSLionel Sambuc bool fun() {
155*0a6a1f1dSLionel Sambuc bool x = nullptr; // expected-warning {{implicit conversion of nullptr constant to 'bool'}}
156*0a6a1f1dSLionel Sambuc if (nullptr) {} // expected-warning {{implicit conversion of nullptr constant to 'bool'}}
157*0a6a1f1dSLionel Sambuc return nullptr; // expected-warning {{implicit conversion of nullptr constant to 'bool'}}
158*0a6a1f1dSLionel Sambuc }
159*0a6a1f1dSLionel Sambuc }
160