blob: edb3a6a4c8a5fa169155f3ad6601e8621dabb287 [file] [log] [blame]
Daniel Dunbar3573b2c2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00002
3int foo(int x) {
Douglas Gregor5d8e67e2010-06-08 19:50:34 +00004 return x == x; // expected-warning {{self-comparison always evaluates to true}}
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00005}
6
7int foo2(int x) {
Douglas Gregor5d8e67e2010-06-08 19:50:34 +00008 return (x) != (((x))); // expected-warning {{self-comparison always evaluates to false}}
9}
10
11void foo3(short s, short t) {
12 if (s == s) {} // expected-warning {{self-comparison always evaluates to true}}
13 if (s == t) {} // no-warning
14}
15
16void foo4(void* v, void* w) {
17 if (v == v) {} // expected-warning {{self-comparison always evaluates to true}}
18 if (v == w) {} // no-warning
Ted Kremenekcf8b77d2007-10-29 16:58:49 +000019}
20
Ted Kremenek4926e102007-10-29 17:02:56 +000021int qux(int x) {
22 return x < x; // expected-warning {{self-comparison}}
23}
24
25int qux2(int x) {
26 return x > x; // expected-warning {{self-comparison}}
27}
28
Ted Kremenekcf8b77d2007-10-29 16:58:49 +000029int bar(float x) {
30 return x == x; // no-warning
31}
32
33int bar2(float x) {
34 return x != x; // no-warning
Ted Kremenek4926e102007-10-29 17:02:56 +000035}
Ted Kremenekf042dc62009-03-20 18:35:45 +000036
Chandler Carruth0737f2e2010-07-12 06:23:38 +000037#define IS_THE_ANSWER(x) (x == 42)
38
39int macro_comparison() {
40 return IS_THE_ANSWER(42);
Ted Kremenekf042dc62009-03-20 18:35:45 +000041}
Douglas Gregor03d4a752010-01-12 23:18:54 +000042
43// Don't complain in unevaluated contexts.
44int compare_sizeof(int x) {
45 return sizeof(x == x); // no-warning
46}
Douglas Gregor5d8e67e2010-06-08 19:50:34 +000047
48int array_comparisons() {
49 int array1[2];
50 int array2[2];
51
52 //
53 // compare same array
54 //
55 return array1 == array1; // expected-warning{{self-comparison always evaluates to true}}
56 return array1 != array1; // expected-warning{{self-comparison always evaluates to false}}
57 return array1 < array1; // expected-warning{{self-comparison always evaluates to false}}
58 return array1 <= array1; // expected-warning{{self-comparison always evaluates to true}}
59 return array1 > array1; // expected-warning{{self-comparison always evaluates to false}}
60 return array1 >= array1; // expected-warning{{self-comparison always evaluates to true}}
61
62 //
63 // compare differrent arrays
64 //
65 return array1 == array2; // expected-warning{{array comparison always evaluates to false}}
66 return array1 != array2; // expected-warning{{array comparison always evaluates to true}}
67
68 //
69 // we don't know what these are going to be
70 //
71 return array1 < array2; // expected-warning{{array comparison always evaluates to a constant}}
72 return array1 <= array2; // expected-warning{{array comparison always evaluates to a constant}}
73 return array1 > array2; // expected-warning{{array comparison always evaluates to a constant}}
74 return array1 >= array2; // expected-warning{{array comparison always evaluates to a constant}}
75
76}
77
Ted Kremenek657ca292010-09-16 00:03:01 +000078// Don't issue a warning when either the left or right side of the comparison
79// results from a macro expansion. <rdar://problem/8435950>
80#define R8435950_A i
81#define R8435950_B i
82
83int R8435950(int i) {
84 if (R8435950_A == R8435950_B) // no-warning
85 return 0;
86 return 1;
87}
88