diff options
author | Zancanaro; Carlo <czan8762@plang3.cs.usyd.edu.au> | 2012-09-24 09:58:17 +1000 |
---|---|---|
committer | Zancanaro; Carlo <czan8762@plang3.cs.usyd.edu.au> | 2012-09-24 09:58:17 +1000 |
commit | 222e2a7620e6520ffaf4fc4e69d79c18da31542e (patch) | |
tree | 7bfbc05bfa3b41c8f9d2e56d53a0bc3e310df239 /clang/test/Sema/constant-conversion.c | |
parent | 3d206f03985b50beacae843d880bccdc91a9f424 (diff) |
Add the clang library to the repo (with some of my changes, too).
Diffstat (limited to 'clang/test/Sema/constant-conversion.c')
-rw-r--r-- | clang/test/Sema/constant-conversion.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/clang/test/Sema/constant-conversion.c b/clang/test/Sema/constant-conversion.c new file mode 100644 index 0000000..1376333 --- /dev/null +++ b/clang/test/Sema/constant-conversion.c @@ -0,0 +1,82 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s + +// This file tests -Wconstant-conversion, a subcategory of -Wconversion +// which is on by default. + +// rdar://problem/6792488 +void test_6792488(void) { + int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}} +} + +void test_7809123(void) { + struct { int i5 : 5; } a; + + a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 36 to 4}} +} + +void test() { + struct { int bit : 1; } a; + a.bit = 1; // shouldn't warn +} + +enum Test2 { K_zero, K_one }; +enum Test2 test2(enum Test2 *t) { + *t = 20; + return 10; // shouldn't warn +} + +void test3() { + struct A { + unsigned int foo : 2; + int bar : 2; + }; + + struct A a = { 0, 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}} + struct A b[] = { 0, 10, 0, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}} + struct A c[] = {{10, 0}}; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} + struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} + struct A e = { .foo = 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}} +} + +void test4() { + struct A { + char c : 2; + } a; + + a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 257 to 1}} +} + +void test5() { + struct A { + _Bool b : 1; + } a; + + // Don't warn about this implicit conversion to bool, or at least + // don't warn about it just because it's a bitfield. + a.b = 100; +} + +void test6() { + // Test that unreachable code doesn't trigger the truncation warning. + unsigned char x = 0 ? 65535 : 1; // no-warning + unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}} +} + +void test7() { + struct { + unsigned int twoBits1:2; + unsigned int twoBits2:2; + unsigned int reserved:28; + } f; + + f.twoBits1 = ~1; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -2 to 2}} + f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -3 to 1}} + f.twoBits1 &= ~1; // no-warning + f.twoBits2 &= ~2; // no-warning +} + +void test8() { + enum E { A, B, C }; + struct { enum E x : 1; } f; + f.x = C; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 2 to 0}} +} |