blob: ac235cc7feafd833e14762516c9d7727ee7157a0 [file] [log] [blame]
John McCalla15eb732010-07-13 06:26:23 +00001// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -verify %s
David Blaikieae12b182012-03-16 20:30:12 +00002// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion %s 2>&1 | FileCheck %s
John McCalla15eb732010-07-13 06:26:23 +00003
Richard Trieubeaf3452011-05-29 19:59:02 +00004#include <stddef.h>
5
John McCalla15eb732010-07-13 06:26:23 +00006typedef signed char int8_t;
7typedef signed short int16_t;
8typedef signed int int32_t;
9typedef signed long int64_t;
10
11typedef unsigned char uint8_t;
12typedef unsigned short uint16_t;
13typedef unsigned int uint32_t;
14typedef unsigned long uint64_t;
15
16// <rdar://problem/7909130>
17namespace test0 {
18 int32_t test1_positive(char *I, char *E) {
19 return (E - I); // expected-warning {{implicit conversion loses integer precision}}
20 }
21
22 int32_t test1_negative(char *I, char *E) {
23 return static_cast<int32_t>(E - I);
24 }
25
26 uint32_t test2_positive(uint64_t x) {
27 return x; // expected-warning {{implicit conversion loses integer precision}}
28 }
29
30 uint32_t test2_negative(uint64_t x) {
31 return (uint32_t) x;
32 }
33}
34
35namespace test1 {
36 uint64_t test1(int x, unsigned y) {
37 return sizeof(x == y);
38 }
39
40 uint64_t test2(int x, unsigned y) {
41 return __alignof(x == y);
42 }
43
44 void * const foo();
45 bool test2(void *p) {
46 return p == foo();
47 }
48}
John McCall1f425642010-11-11 03:21:53 +000049
50namespace test2 {
51 struct A {
52 unsigned int x : 2;
53 A() : x(10) {} // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
54 };
55}
Richard Trieubeaf3452011-05-29 19:59:02 +000056
David Blaikieebcbe4b2012-03-15 04:50:32 +000057// This file tests -Wnull-conversion, a subcategory of -Wconversion
58// which is on by default.
59
Richard Trieubeaf3452011-05-29 19:59:02 +000060void test3() {
David Blaikiee7fd5802012-03-15 20:48:26 +000061 int a = NULL; // expected-warning {{implicit conversion of NULL constant to 'int'}}
Richard Trieubeaf3452011-05-29 19:59:02 +000062 int b;
David Blaikiee7fd5802012-03-15 20:48:26 +000063 b = NULL; // expected-warning {{implicit conversion of NULL constant to 'int'}}
David Blaikieebcbe4b2012-03-15 04:50:32 +000064 long l = NULL; // FIXME: this should also warn, but currently does not if sizeof(NULL)==sizeof(inttype)
David Blaikiee7fd5802012-03-15 20:48:26 +000065 int c = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
Richard Trieubeaf3452011-05-29 19:59:02 +000066 int d;
David Blaikiee7fd5802012-03-15 20:48:26 +000067 d = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
David Blaikie7555b6a2012-05-15 16:56:36 +000068 bool bl = NULL; // expected-warning {{implicit conversion of NULL constant to 'bool'}}
David Blaikiee7fd5802012-03-15 20:48:26 +000069 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'}}
David Blaikie9366d2b2012-06-19 21:19:06 +000072 double dbl = NULL; // expected-warning {{implicit conversion of NULL constant to 'double'}}
David Blaikieae12b182012-03-16 20:30:12 +000073
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)
76 // CHECK-NOT: note:
David Blaikieae12b182012-03-16 20:30:12 +000077 // CHECK: note: expanded from macro 'FINIT'
78#define FINIT int a3 = NULL;
79 FINIT // expected-warning {{implicit conversion of NULL constant to 'int'}}
David Blaikie18e9ac72012-05-15 21:57:38 +000080
81 // we don't catch the case of #define FOO NULL ... int i = FOO; but that seems a bit narrow anyway
82 // and avoiding that helps us skip these cases:
83#define NULL_COND(cond) ((cond) ? &a : NULL)
84 bool bl2 = NULL_COND(true); // don't warn on NULL conversion through the conditional operator across a macro boundary
David Blaikiea5696df2012-05-16 04:20:04 +000085 if (NULL_COND(true))
86 ;
87 while (NULL_COND(true))
88 ;
89 for (; NULL_COND(true); )
90 ;
91 do ;
92 while(NULL_COND(true));
David Blaikie8cf439f2012-06-21 18:51:10 +000093 int *ip = NULL;
94 int (*fp)() = NULL;
95 struct foo {
96 int n;
97 void func();
98 };
99 int foo::*datamem = NULL;
100 int (foo::*funmem)() = NULL;
Richard Trieubeaf3452011-05-29 19:59:02 +0000101}
David Blaikief68e8092012-04-30 18:21:31 +0000102
103namespace test4 {
David Blaikie7afed5e2012-05-01 06:05:57 +0000104 // FIXME: We should warn for non-dependent args (only when the param type is also non-dependent) only once
105 // not once for the template + once for every instantiation
David Blaikief68e8092012-04-30 18:21:31 +0000106 template<typename T>
David Blaikie3890d682012-05-01 20:28:45 +0000107 void tmpl(char c = NULL, // expected-warning 4 {{implicit conversion of NULL constant to 'char'}}
David Blaikief68e8092012-04-30 18:21:31 +0000108 T a = NULL, // expected-warning {{implicit conversion of NULL constant to 'char'}} \
David Blaikie3890d682012-05-01 20:28:45 +0000109 expected-warning 2 {{implicit conversion of NULL constant to 'int'}}
David Blaikief68e8092012-04-30 18:21:31 +0000110 T b = 1024) { // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1024 to 0}}
111 }
112
113 template<typename T>
114 void tmpl2(T t = NULL) {
115 }
116
117 void func() {
David Blaikie7afed5e2012-05-01 06:05:57 +0000118 tmpl<char>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<char>' required here}}
119 tmpl<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}}
David Blaikie3890d682012-05-01 20:28:45 +0000120 // FIXME: We should warn only once for each template instantiation - not once for each call
121 tmpl<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}}
David Blaikief68e8092012-04-30 18:21:31 +0000122 tmpl2<int*>();
123 }
124}
David Blaikie7555b6a2012-05-15 16:56:36 +0000125
126namespace test5 {
127 template<int I>
128 void func() {
129 bool b = I;
130 }
131
132 template void func<3>();
133}