aboutsummaryrefslogtreecommitdiff
path: root/test/Sema
diff options
context:
space:
mode:
authorSam Panzer <espanz@gmail.com>2013-03-28 19:07:11 +0000
committerSam Panzer <espanz@gmail.com>2013-03-28 19:07:11 +0000
commit25ffbef84450f0c666957a2d00cdf928c61b44d7 (patch)
tree86e16039dce641e2df92c0276e93bf126283ef5b /test/Sema
parent577bb0a2335295958b3b0f88bc9cdedf6551c17f (diff)
Implemented a warning when an input several bitwise operations are
likely be implicitly truncated: * All forms of Bitwise-and, bitwise-or, and integer multiplication. * The assignment form of integer addition, subtraction, and exclusive-or * The RHS of the comma operator * The LHS of left shifts. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@178273 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Sema')
-rw-r--r--test/Sema/constant-conversion.c42
1 files changed, 41 insertions, 1 deletions
diff --git a/test/Sema/constant-conversion.c b/test/Sema/constant-conversion.c
index 1376333967..91e428bc5b 100644
--- a/test/Sema/constant-conversion.c
+++ b/test/Sema/constant-conversion.c
@@ -66,13 +66,18 @@ void test7() {
struct {
unsigned int twoBits1:2;
unsigned int twoBits2:2;
- unsigned int reserved:28;
+ unsigned int twoBits3:2;
+ unsigned int reserved:26;
} 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
+ f.twoBits3 |= 4; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 4 to 0}}
+ f.twoBits3 += 4; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 4 to 0}}
+ f.twoBits3 *= 4; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 4 to 0}}
+ f.twoBits3 |= 1; // no-warning
}
void test8() {
@@ -80,3 +85,38 @@ void test8() {
struct { enum E x : 1; } f;
f.x = C; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 2 to 0}}
}
+
+int func(int);
+
+void test9() {
+ unsigned char x = 0;
+ unsigned char y = 0;
+ x = y | 0x1ff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
+ x = y | 0xff; // no-warning
+ x = y & 0xdff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 3583 to 255 if converted before operation}}
+ x = y & 0xff; // no-warning
+ x = y & ~1; // no-warning
+ x = 0x1ff | y; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
+ x = 0xff | y; // no-warning
+ x = (y | 0x1ff); // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
+ x = (y | 0xff); // no-warning
+ x = 0xff + y; // no-warning
+ x += 0x1ff; // expected-warning {{implicit conversion from 'int' to 'unsigned char' changes value from 511 to 255}}
+ x = 0xff - y; // no-warning
+ x -= 0x1ff; // expected-warning {{implicit conversion from 'int' to 'unsigned char' changes value from 511 to 255}}
+ x = y * 0x1ff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
+ x = y * 0xff; // no-warning
+ x *= 0x1ff; // expected-warning {{implicit conversion from 'int' to 'unsigned char' changes value from 511 to 255}}
+ x = y ^ 0xff; // no-warning
+ x ^= 0x1ff; // expected-warning {{implicit conversion from 'int' to 'unsigned char' changes value from 511 to 255}}
+ x = (func(1), 0x1ff); // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
+ x = (func(1), 0xff); // no-warning
+ x = 0xff << y; // no-warning
+ x = 0x1ff << y; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
+
+
+ // These next two tests make sure that both LHS and RHS are checked for
+ // narrowing operations.
+ x = 0x1ff | 0xff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
+ x = 0xff | 0x1ff; // expected-warning {{implicit conversion of binary operation from 'int' to 'unsigned char' may change its value; value of operand would be changed from 511 to 255 if converted before operation}}
+}