blob: 9cbbfba935bd048da83f7eafdb05ca4a09be2b30 [file] [log] [blame]
Daniel Dunbard7d5f022009-03-24 02:24:46 +00001// RUN: clang-cc -fsyntax-only -pedantic -verify %s
Chris Lattnerd28f8152007-08-26 01:10:14 +00002
3int test(char *C) { // nothing here should warn.
4 return C != ((void*)0);
5 return C != (void*)0;
Chris Lattner06c0f5b2009-08-23 00:03:44 +00006 return C != 0;
7 return C != 1; // expected-warning {{comparison between pointer and integer ('char *' and 'int')}}
Chris Lattnerd28f8152007-08-26 01:10:14 +00008}
9
John McCall45aa4552009-11-05 00:40:04 +000010int ints(long a, unsigned long b) {
11 return (a == b) + // expected-warning {{comparison of integers of different signs}}
12 ((int)a == b) + // expected-warning {{comparison of integers of different signs}}
13 ((short)a == b) + // expected-warning {{comparison of integers of different signs}}
14 (a == (unsigned int) b) + // expected-warning {{comparison of integers of different signs}}
15 (a == (unsigned short) b); // expected-warning {{comparison of integers of different signs}}
John McCallb13c87f2009-11-05 09:23:39 +000016
17 enum Enum {B};
18 return (a == B) +
19 ((int)a == B) +
20 ((short)a == B) +
21 (a == (unsigned int) B) + // expected-warning {{comparison of integers of different signs}}
22 (a == (unsigned short) B); // expected-warning {{comparison of integers of different signs}}
23
24 // Should be able to prove all of these are non-negative.
25 return (b == (long) B) +
26 (b == (int) B) +
27 (b == (short) B);
John McCall45aa4552009-11-05 00:40:04 +000028}
29
Chris Lattner6365e3e2009-08-22 18:58:31 +000030int equal(char *a, const char *b) {
Steve Naroff77878cc2007-08-27 04:08:11 +000031 return a == b;
32}
Eli Friedman4e92acf2008-02-06 04:53:22 +000033
34int arrays(char (*a)[5], char(*b)[10], char(*c)[5]) {
35 int d = (a == c);
36 return a == b; // expected-warning {{comparison of distinct pointer types}}
37}
Chris Lattner149f1382009-06-30 06:24:05 +000038
Chris Lattner6365e3e2009-08-22 18:58:31 +000039int pointers(int *a) {
Chris Lattner06c0f5b2009-08-23 00:03:44 +000040 return a > 0; // expected-warning {{ordered comparison between pointer and zero ('int *' and 'int') is an extension}}
41 return a > 42; // expected-warning {{ordered comparison between pointer and integer ('int *' and 'int')}}
Chris Lattner149f1382009-06-30 06:24:05 +000042 return a > (void *)0; // expected-warning {{comparison of distinct pointer types}}
43}
44
Eli Friedman3075e762009-08-23 00:27:47 +000045int function_pointers(int (*a)(int), int (*b)(int), void (*c)(int)) {
Chris Lattner149f1382009-06-30 06:24:05 +000046 return a > b; // expected-warning {{ordered comparison of function pointers}}
47 return function_pointers > function_pointers; // expected-warning {{ordered comparison of function pointers}}
Eli Friedman3075e762009-08-23 00:27:47 +000048 return a > c; // expected-warning {{comparison of distinct pointer types}}
Chris Lattner149f1382009-06-30 06:24:05 +000049 return a == (void *) 0;
Eli Friedman3075e762009-08-23 00:27:47 +000050 return a == (void *) 1; // expected-warning {{equality comparison between function pointer and void pointer}}
Chris Lattner149f1382009-06-30 06:24:05 +000051}
Douglas Gregorf9334372009-07-06 20:14:23 +000052
Eli Friedman3075e762009-08-23 00:27:47 +000053int void_pointers(void* foo) {
54 return foo == (void*) 0;
55 return foo == (void*) 1;
Douglas Gregorf9334372009-07-06 20:14:23 +000056}