| John McCall | 18a2c2c | 2010-11-09 22:22:12 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s | 
|  | 2 |  | 
|  | 3 | // This file tests -Wconstant-conversion, a subcategory of -Wconversion | 
|  | 4 | // which is on by default. | 
|  | 5 |  | 
|  | 6 | // rdar://problem/6792488 | 
|  | 7 | void test_6792488(void) { | 
|  | 8 | int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}} | 
|  | 9 | } | 
| John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 10 |  | 
|  | 11 | void test_7809123(void) { | 
|  | 12 | struct { int i5 : 5; } a; | 
|  | 13 |  | 
| John McCall | 0e6cfaf | 2010-11-09 23:36:43 +0000 | [diff] [blame] | 14 | a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 36 to 4}} | 
| John McCall | d2a5312 | 2010-11-09 23:24:47 +0000 | [diff] [blame] | 15 | } | 
| John McCall | fd81c52 | 2010-11-10 00:26:50 +0000 | [diff] [blame] | 16 |  | 
|  | 17 | void test() { | 
|  | 18 | struct { int bit : 1; } a; | 
|  | 19 | a.bit = 1; // shouldn't warn | 
|  | 20 | } | 
| John McCall | 817d4af | 2010-11-10 23:38:19 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | enum Test2 { K_zero, K_one }; | 
|  | 23 | enum Test2 test2(enum Test2 *t) { | 
|  | 24 | *t = 20; | 
|  | 25 | return 10; // shouldn't warn | 
|  | 26 | } | 
| John McCall | 1f42564 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 27 |  | 
|  | 28 | void test3() { | 
|  | 29 | struct A { | 
|  | 30 | unsigned int foo : 2; | 
|  | 31 | int bar : 2; | 
|  | 32 | }; | 
|  | 33 |  | 
|  | 34 | struct A a = { 0, 10 };            // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} | 
|  | 35 | struct A b[] = { 0, 10, 0, 0 };    // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} | 
|  | 36 | struct A c[] = {{10, 0}};          // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} | 
|  | 37 | struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} | 
|  | 38 | struct A e = { .foo = 10 };        // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} | 
|  | 39 | } | 
| John McCall | deebbcf | 2010-11-11 05:33:51 +0000 | [diff] [blame] | 40 |  | 
|  | 41 | void test4() { | 
|  | 42 | struct A { | 
|  | 43 | char c : 2; | 
|  | 44 | } a; | 
|  | 45 |  | 
|  | 46 | a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 257 to 1}} | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | void test5() { | 
|  | 50 | struct A { | 
|  | 51 | _Bool b : 1; | 
|  | 52 | } a; | 
|  | 53 |  | 
|  | 54 | // Don't warn about this implicit conversion to bool, or at least | 
|  | 55 | // don't warn about it just because it's a bitfield. | 
|  | 56 | a.b = 100; | 
|  | 57 | } |