diff options
author | John McCall <rjmccall@apple.com> | 2009-11-05 09:23:39 +0000 |
---|---|---|
committer | John McCall <rjmccall@apple.com> | 2009-11-05 09:23:39 +0000 |
commit | 1fa36b7cabfa24ec4f61e2fb3807af3891dc9ee4 (patch) | |
tree | 9f98284ebc313a2b081c8eb18d794dab954ceba2 /clang/test | |
parent | c92ff053e9e5997d385182d509288cea438f969f (diff) | |
download | bcm5719-llvm-1fa36b7cabfa24ec4f61e2fb3807af3891dc9ee4.tar.gz bcm5719-llvm-1fa36b7cabfa24ec4f61e2fb3807af3891dc9ee4.zip |
Implement the conditional-operator part of -Wsign-compare. Turn
DiagnoseSignCompare into Sema::CheckSignCompare and call it from more places.
Add some enumerator tests. These seem to expose some oddities in the
types we're converting C++ enumerators to; in particular, they're converting
to unsigned before int, which seems to contradict 4.5 [conv.prom] p2.
Note to self: stop baiting Doug in my commit messages.
llvm-svn: 86128
Diffstat (limited to 'clang/test')
-rw-r--r-- | clang/test/Sema/compare.c | 12 | ||||
-rw-r--r-- | clang/test/Sema/conditional-expr.c | 19 | ||||
-rw-r--r-- | clang/test/SemaCXX/compare.cpp | 15 | ||||
-rw-r--r-- | clang/test/SemaCXX/conditional-expr.cpp | 22 |
4 files changed, 66 insertions, 2 deletions
diff --git a/clang/test/Sema/compare.c b/clang/test/Sema/compare.c index 50b40e459c3..9cbbfba935b 100644 --- a/clang/test/Sema/compare.c +++ b/clang/test/Sema/compare.c @@ -13,6 +13,18 @@ int ints(long a, unsigned long b) { ((short)a == b) + // expected-warning {{comparison of integers of different signs}} (a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}} (a == (unsigned short) b); // expected-warning {{comparison of integers of different signs}} + + enum Enum {B}; + return (a == B) + + ((int)a == B) + + ((short)a == B) + + (a == (unsigned int) B) + // expected-warning {{comparison of integers of different signs}} + (a == (unsigned short) B); // expected-warning {{comparison of integers of different signs}} + + // Should be able to prove all of these are non-negative. + return (b == (long) B) + + (b == (int) B) + + (b == (short) B); } int equal(char *a, const char *b) { diff --git a/clang/test/Sema/conditional-expr.c b/clang/test/Sema/conditional-expr.c index 1f0a9deb5e4..3bfeae5d4c5 100644 --- a/clang/test/Sema/conditional-expr.c +++ b/clang/test/Sema/conditional-expr.c @@ -34,6 +34,25 @@ void foo() { typedef void *asdf; *(0 ? (asdf) 0 : &x) = 10; + + unsigned long test0 = 5; + test0 = test0 ? (long) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? (int) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? (short) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? test0 : (long) test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? test0 : (int) test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? test0 : (short) test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? test0 : (long) 10; + test0 = test0 ? test0 : (int) 10; + test0 = test0 ? test0 : (short) 10; + test0 = test0 ? (long) 10 : test0; + test0 = test0 ? (int) 10 : test0; + test0 = test0 ? (short) 10 : test0; + + enum Enum { EVal }; + test0 = test0 ? EVal : test0; + test0 = test0 ? EVal : (int) test0; // okay: EVal is an int + test0 = test0 ? (unsigned) EVal : (int) test0; // expected-warning {{operands of ? are integers of different signs}} } int Postgresql() { diff --git a/clang/test/SemaCXX/compare.cpp b/clang/test/SemaCXX/compare.cpp new file mode 100644 index 00000000000..806b078e8df --- /dev/null +++ b/clang/test/SemaCXX/compare.cpp @@ -0,0 +1,15 @@ +// RUN: clang-cc -fsyntax-only -pedantic -verify %s + +int test0(long a, unsigned long b) { + enum Enum {B}; + return (a == B) + // expected-warning {{comparison of integers of different signs}} + ((int)a == B) + // expected-warning {{comparison of integers of different signs}} + ((short)a == B) + // expected-warning {{comparison of integers of different signs}} + (a == (unsigned int) B) + // expected-warning {{comparison of integers of different signs}} + (a == (unsigned short) B); // expected-warning {{comparison of integers of different signs}} + + // Should be able to prove all of these are non-negative. + return (b == (long) B) + + (b == (int) B) + + (b == (short) B); +} diff --git a/clang/test/SemaCXX/conditional-expr.cpp b/clang/test/SemaCXX/conditional-expr.cpp index fea3324b5fd..da2dd67d061 100644 --- a/clang/test/SemaCXX/conditional-expr.cpp +++ b/clang/test/SemaCXX/conditional-expr.cpp @@ -156,8 +156,8 @@ void test() i1 = i1 ? i1 : ir1; int *pi1 = i1 ? &i1 : 0; pi1 = i1 ? 0 : &i1; - i1 = i1 ? i1 : EVal; - i1 = i1 ? EVal : i1; + i1 = i1 ? i1 : EVal; // expected-warning {{operands of ? are integers of different signs}} ?? + i1 = i1 ? EVal : i1; // expected-warning {{operands of ? are integers of different signs}} ?? d1 = i1 ? 'c' : 4.0; d1 = i1 ? 4.0 : 'c'; Base *pb = i1 ? (Base*)0 : (Derived*)0; @@ -177,6 +177,24 @@ void test() (void)&(i1 ? flds.b1 : flds.i1); // expected-error {{address of bit-field requested}} (void)&(i1 ? flds.i1 : flds.b1); // expected-error {{address of bit-field requested}} + + unsigned long test0 = 5; + test0 = test0 ? (long) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? (int) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? (short) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? test0 : (long) test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? test0 : (int) test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? test0 : (short) test0; // expected-warning {{operands of ? are integers of different signs}} + test0 = test0 ? test0 : (long) 10; + test0 = test0 ? test0 : (int) 10; + test0 = test0 ? test0 : (short) 10; + test0 = test0 ? (long) 10 : test0; + test0 = test0 ? (int) 10 : test0; + test0 = test0 ? (short) 10 : test0; + + test0 = test0 ? EVal : test0; + test0 = test0 ? EVal : (int) test0; // expected-warning {{operands of ? are integers of different signs}} + // Note the thing that this does not test: since DR446, various situations // *must* create a separate temporary copy of class objects. This can only // be properly tested at runtime, though. |