John McCall | ecda6fb | 2010-07-13 06:26:23 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -verify %s |
David Blaikie | b136049 | 2012-03-16 20:30:12 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion %s 2>&1 | FileCheck %s |
John McCall | ecda6fb | 2010-07-13 06:26:23 +0000 | [diff] [blame] | 3 | |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 4 | #include <stddef.h> |
| 5 | |
John McCall | ecda6fb | 2010-07-13 06:26:23 +0000 | [diff] [blame] | 6 | typedef signed char int8_t; |
| 7 | typedef signed short int16_t; |
| 8 | typedef signed int int32_t; |
| 9 | typedef signed long int64_t; |
| 10 | |
| 11 | typedef unsigned char uint8_t; |
| 12 | typedef unsigned short uint16_t; |
| 13 | typedef unsigned int uint32_t; |
| 14 | typedef unsigned long uint64_t; |
| 15 | |
| 16 | // <rdar://problem/7909130> |
| 17 | namespace 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 | |
| 35 | namespace 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 McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 49 | |
| 50 | namespace 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 Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 56 | |
David Blaikie | 95187bd | 2012-03-15 04:50:32 +0000 | [diff] [blame] | 57 | // This file tests -Wnull-conversion, a subcategory of -Wconversion |
| 58 | // which is on by default. |
| 59 | |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 60 | void test3() { |
David Blaikie | 2b2bbee | 2012-03-15 20:48:26 +0000 | [diff] [blame] | 61 | int a = NULL; // expected-warning {{implicit conversion of NULL constant to 'int'}} |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 62 | int b; |
David Blaikie | 2b2bbee | 2012-03-15 20:48:26 +0000 | [diff] [blame] | 63 | b = NULL; // expected-warning {{implicit conversion of NULL constant to 'int'}} |
David Blaikie | 95187bd | 2012-03-15 04:50:32 +0000 | [diff] [blame] | 64 | long l = NULL; // FIXME: this should also warn, but currently does not if sizeof(NULL)==sizeof(inttype) |
David Blaikie | 2b2bbee | 2012-03-15 20:48:26 +0000 | [diff] [blame] | 65 | int c = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}} |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 66 | int d; |
David Blaikie | 2b2bbee | 2012-03-15 20:48:26 +0000 | [diff] [blame] | 67 | d = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}} |
| 68 | bool bl = NULL; // FIXME: this should warn but we currently suppress a bunch of conversion-to-bool warnings including this one |
| 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'}} |
David Blaikie | b136049 | 2012-03-16 20:30:12 +0000 | [diff] [blame] | 72 | |
| 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) |
| 75 | // CHECK-NOT: note: |
| 76 | // CHECK: note: expanded from macro 'FNULL' |
| 77 | #define FNULL NULL |
| 78 | int a2 = FNULL; // expected-warning {{implicit conversion of NULL constant to 'int'}} |
| 79 | // CHECK-NOT: note: |
| 80 | // CHECK: note: expanded from macro 'FINIT' |
| 81 | #define FINIT int a3 = NULL; |
| 82 | FINIT // expected-warning {{implicit conversion of NULL constant to 'int'}} |
Richard Trieu | 1838ca5 | 2011-05-29 19:59:02 +0000 | [diff] [blame] | 83 | } |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 84 | |
| 85 | namespace test4 { |
David Blaikie | 5729672 | 2012-05-01 06:05:57 +0000 | [diff] [blame] | 86 | // FIXME: We should warn for non-dependent args (only when the param type is also non-dependent) only once |
| 87 | // not once for the template + once for every instantiation |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 88 | template<typename T> |
David Blaikie | ca2e1b7 | 2012-05-01 20:28:45 +0000 | [diff] [blame] | 89 | void tmpl(char c = NULL, // expected-warning 4 {{implicit conversion of NULL constant to 'char'}} |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 90 | T a = NULL, // expected-warning {{implicit conversion of NULL constant to 'char'}} \ |
David Blaikie | ca2e1b7 | 2012-05-01 20:28:45 +0000 | [diff] [blame] | 91 | expected-warning 2 {{implicit conversion of NULL constant to 'int'}} |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 92 | T b = 1024) { // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1024 to 0}} |
| 93 | } |
| 94 | |
| 95 | template<typename T> |
| 96 | void tmpl2(T t = NULL) { |
| 97 | } |
| 98 | |
| 99 | void func() { |
David Blaikie | 5729672 | 2012-05-01 06:05:57 +0000 | [diff] [blame] | 100 | tmpl<char>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<char>' required here}} |
| 101 | tmpl<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}} |
David Blaikie | ca2e1b7 | 2012-05-01 20:28:45 +0000 | [diff] [blame] | 102 | // FIXME: We should warn only once for each template instantiation - not once for each call |
| 103 | tmpl<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}} |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 104 | tmpl2<int*>(); |
| 105 | } |
| 106 | } |