summaryrefslogtreecommitdiffstats
path: root/clang/test/Sema/compare.c
blob: 45a100be1e85a460acc29cc51eb8b2c5a2ff1e3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// RUN: clang-cc -fsyntax-only -pedantic -verify -Wsign-compare %s

int test(char *C) { // nothing here should warn.
  return C != ((void*)0);
  return C != (void*)0;
  return C != 0;  
  return C != 1;  // expected-warning {{comparison between pointer and integer ('char *' and 'int')}}
}

int ints(long a, unsigned long 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}}

  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) {
    return a == b;
}

int arrays(char (*a)[5], char(*b)[10], char(*c)[5]) {
  int d = (a == c);
  return a == b; // expected-warning {{comparison of distinct pointer types}}
}

int pointers(int *a) {
  return a > 0; // expected-warning {{ordered comparison between pointer and zero ('int *' and 'int') is an extension}}
  return a > 42; // expected-warning {{ordered comparison between pointer and integer ('int *' and 'int')}}
  return a > (void *)0; // expected-warning {{comparison of distinct pointer types}}
}

int function_pointers(int (*a)(int), int (*b)(int), void (*c)(int)) {
  return a > b; // expected-warning {{ordered comparison of function pointers}}
  return function_pointers > function_pointers; // expected-warning {{ordered comparison of function pointers}}
  return a > c; // expected-warning {{comparison of distinct pointer types}}
  return a == (void *) 0;
  return a == (void *) 1; // expected-warning {{equality comparison between function pointer and void pointer}}
}

int void_pointers(void* foo) {
  return foo == (void*) 0;
  return foo == (void*) 1;
}
OpenPOWER on IntegriCloud