diff options
Diffstat (limited to 'test/SemaCXX/compare.cpp')
-rw-r--r-- | test/SemaCXX/compare.cpp | 150 |
1 files changed, 141 insertions, 9 deletions
diff --git a/test/SemaCXX/compare.cpp b/test/SemaCXX/compare.cpp index 432069f..feb1ccb 100644 --- a/test/SemaCXX/compare.cpp +++ b/test/SemaCXX/compare.cpp @@ -1,7 +1,7 @@ // Force x86-64 because some of our heuristics are actually based // on integer sizes. -// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -pedantic -verify -Wsign-compare -std=c++11 %s int test0(long a, unsigned long b) { enum EnumA {A}; @@ -89,8 +89,8 @@ int test0(long a, unsigned long b) { // (C,b) (C == (unsigned long) b) + (C == (unsigned int) b) + - (C == (unsigned short) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned short' is always false}} - (C == (unsigned char) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned char' is always false}} + (C == (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}} + (C == (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}} ((long) C == b) + ((int) C == b) + ((short) C == b) + @@ -101,8 +101,8 @@ int test0(long a, unsigned long b) { ((signed char) C == (unsigned char) b) + (C < (unsigned long) b) + (C < (unsigned int) b) + - (C < (unsigned short) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned short' is always false}} - (C < (unsigned char) b) + // expected-warning {{comparison of constant 65536 with expression of type 'unsigned char' is always false}} + (C < (unsigned short) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always false}} + (C < (unsigned char) b) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always false}} ((long) C < b) + ((int) C < b) + ((short) C < b) + @@ -119,8 +119,8 @@ int test0(long a, unsigned long b) { (a == (unsigned char) C) + ((long) a == C) + ((int) a == C) + - ((short) a == C) + // expected-warning {{comparison of constant 65536 with expression of type 'short' is always false}} - ((signed char) a == C) + // expected-warning {{comparison of constant 65536 with expression of type 'signed char' is always false}} + ((short) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always false}} + ((signed char) a == C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always false}} ((long) a == (unsigned long) C) + ((int) a == (unsigned int) C) + ((short) a == (unsigned short) C) + @@ -131,8 +131,8 @@ int test0(long a, unsigned long b) { (a < (unsigned char) C) + ((long) a < C) + ((int) a < C) + - ((short) a < C) + // expected-warning {{comparison of constant 65536 with expression of type 'short' is always true}} - ((signed char) a < C) + // expected-warning {{comparison of constant 65536 with expression of type 'signed char' is always true}} + ((short) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always true}} + ((signed char) a < C) + // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always true}} ((long) a < (unsigned long) C) + // expected-warning {{comparison of integers of different signs}} ((int) a < (unsigned int) C) + // expected-warning {{comparison of integers of different signs}} ((short) a < (unsigned short) C) + @@ -223,3 +223,135 @@ void test3() { (void) (true ? b : a); (void) (true ? (unsigned char)b : (signed char)a); } + +// Test comparison of short to unsigned. If tautological compare does not +// trigger, then the signed comparision warning will. +void test4(short s) { + // A is max short plus 1. All zero and positive shorts are smaller than it. + // All negative shorts are cast towards the max unsigned range. Relation + // comparisons are possible, but equality comparisons are tautological. + const unsigned A = 32768; + void (s < A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + void (s > A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + void (s <= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + void (s >= A); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + + void (s == A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always false}} + void (s != A); // expected-warning{{comparison of constant 32768 with expression of type 'short' is always true}} + + // When negative one is converted to an unsigned value, it becomes the max + // unsigned. Likewise, a negative one short can also be converted to max + // unsigned. + const unsigned B = -1; + void (s < B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + void (s > B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + void (s <= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + void (s >= B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + void (s == B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + void (s != B); // expected-warning{{comparison of integers of different signs: 'short' and 'const unsigned int'}} + +} + +void test5(bool b) { + (void) (b < -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}} + (void) (b > -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}} + (void) (b == -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}} + (void) (b != -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}} + (void) (b <= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always false}} + (void) (b >= -1); // expected-warning{{comparison of constant -1 with expression of type 'bool' is always true}} + + (void) (b < -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}} + (void) (b > -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}} + (void) (b == -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}} + (void) (b != -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}} + (void) (b <= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always false}} + (void) (b >= -10); // expected-warning{{comparison of constant -10 with expression of type 'bool' is always true}} + + (void) (b < 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}} + (void) (b > 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}} + (void) (b == 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}} + (void) (b != 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}} + (void) (b <= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always true}} + (void) (b >= 2); // expected-warning{{comparison of constant 2 with expression of type 'bool' is always false}} + + (void) (b < 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}} + (void) (b > 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}} + (void) (b == 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}} + (void) (b != 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}} + (void) (b <= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always true}} + (void) (b >= 10); // expected-warning{{comparison of constant 10 with expression of type 'bool' is always false}} +} + +void test6(signed char sc) { + (void)(sc < 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} + (void)(sc > 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} + (void)(sc <= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} + (void)(sc >= 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} + (void)(sc == 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} + (void)(sc != 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} + + (void)(200 < sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} + (void)(200 > sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} + (void)(200 <= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} + (void)(200 >= sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} + (void)(200 == sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always false}} + (void)(200 != sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always true}} +} + +// Test many signedness combinations. +void test7(unsigned long other) { + // Common unsigned, other unsigned, constant unsigned + (void)((unsigned)other != (unsigned long)(0x1ffffffff)); // expected-warning{{true}} + (void)((unsigned)other != (unsigned long)(0xffffffff)); + (void)((unsigned long)other != (unsigned)(0x1ffffffff)); + (void)((unsigned long)other != (unsigned)(0xffffffff)); + + // Common unsigned, other signed, constant unsigned + (void)((int)other != (unsigned long)(0xffffffffffffffff)); // expected-warning{{different signs}} + (void)((int)other != (unsigned long)(0x00000000ffffffff)); // expected-warning{{true}} + (void)((int)other != (unsigned long)(0x000000000fffffff)); + (void)((int)other < (unsigned long)(0x00000000ffffffff)); // expected-warning{{different signs}} + (void)((int)other == (unsigned)(0x800000000)); + + // Common unsigned, other unsigned, constant signed + (void)((unsigned long)other != (int)(0xffffffff)); // expected-warning{{different signs}} + + // Common unsigned, other signed, constant signed + // Should not be possible as the common type should also be signed. + + // Common signed, other signed, constant signed + (void)((int)other != (long)(0xffffffff)); // expected-warning{{true}} + (void)((int)other != (long)(0xffffffff00000000)); // expected-warning{{true}} + (void)((int)other != (long)(0xfffffff)); + (void)((int)other != (long)(0xfffffffff0000000)); + + // Common signed, other signed, constant unsigned + (void)((int)other != (unsigned char)(0xffff)); + (void)((int)other != (unsigned char)(0xff)); + + // Common signed, other unsigned, constant signed + (void)((unsigned char)other != (int)(0xff)); + (void)((unsigned char)other != (int)(0xffff)); // expected-warning{{true}} + + // Common signed, other unsigned, constant unsigned + (void)((unsigned char)other != (unsigned short)(0xff)); + (void)((unsigned char)other != (unsigned short)(0x100)); // expected-warning{{true}} + (void)((unsigned short)other != (unsigned char)(0xff)); +} + +void test8(int x) { + enum E { + Negative = -1, + Positive = 1 + }; + + (void)((E)x == 1); + (void)((E)x == -1); +} + +void test9(int x) { + enum E : int { + Positive = 1 + }; + (void)((E)x == 1); +} |