xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/self-comparison.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
foo(int x)3*f4a2713aSLionel Sambuc int foo(int x) {
4*f4a2713aSLionel Sambuc   return x == x; // expected-warning {{self-comparison always evaluates to true}}
5*f4a2713aSLionel Sambuc }
6*f4a2713aSLionel Sambuc 
foo2(int x)7*f4a2713aSLionel Sambuc int foo2(int x) {
8*f4a2713aSLionel Sambuc   return (x) != (((x))); // expected-warning {{self-comparison always evaluates to false}}
9*f4a2713aSLionel Sambuc }
10*f4a2713aSLionel Sambuc 
foo3(short s,short t)11*f4a2713aSLionel Sambuc void foo3(short s, short t) {
12*f4a2713aSLionel Sambuc   if (s == s) {} // expected-warning {{self-comparison always evaluates to true}}
13*f4a2713aSLionel Sambuc   if (s == t) {} // no-warning
14*f4a2713aSLionel Sambuc }
15*f4a2713aSLionel Sambuc 
foo4(void * v,void * w)16*f4a2713aSLionel Sambuc void foo4(void* v, void* w) {
17*f4a2713aSLionel Sambuc   if (v == v) {} // expected-warning {{self-comparison always evaluates to true}}
18*f4a2713aSLionel Sambuc   if (v == w) {} // no-warning
19*f4a2713aSLionel Sambuc }
20*f4a2713aSLionel Sambuc 
qux(int x)21*f4a2713aSLionel Sambuc int qux(int x) {
22*f4a2713aSLionel Sambuc    return x < x; // expected-warning {{self-comparison}}
23*f4a2713aSLionel Sambuc }
24*f4a2713aSLionel Sambuc 
qux2(int x)25*f4a2713aSLionel Sambuc int qux2(int x) {
26*f4a2713aSLionel Sambuc    return x > x; // expected-warning {{self-comparison}}
27*f4a2713aSLionel Sambuc }
28*f4a2713aSLionel Sambuc 
bar(float x)29*f4a2713aSLionel Sambuc int bar(float x) {
30*f4a2713aSLionel Sambuc   return x == x; // no-warning
31*f4a2713aSLionel Sambuc }
32*f4a2713aSLionel Sambuc 
bar2(float x)33*f4a2713aSLionel Sambuc int bar2(float x) {
34*f4a2713aSLionel Sambuc   return x != x; // no-warning
35*f4a2713aSLionel Sambuc }
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc #define IS_THE_ANSWER(x) (x == 42)
38*f4a2713aSLionel Sambuc 
macro_comparison()39*f4a2713aSLionel Sambuc int macro_comparison() {
40*f4a2713aSLionel Sambuc   return IS_THE_ANSWER(42);
41*f4a2713aSLionel Sambuc }
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc // Don't complain in unevaluated contexts.
compare_sizeof(int x)44*f4a2713aSLionel Sambuc int compare_sizeof(int x) {
45*f4a2713aSLionel Sambuc   return sizeof(x == x); // no-warning
46*f4a2713aSLionel Sambuc }
47*f4a2713aSLionel Sambuc 
array_comparisons()48*f4a2713aSLionel Sambuc int array_comparisons() {
49*f4a2713aSLionel Sambuc   int array1[2];
50*f4a2713aSLionel Sambuc   int array2[2];
51*f4a2713aSLionel Sambuc 
52*f4a2713aSLionel Sambuc   //
53*f4a2713aSLionel Sambuc   // compare same array
54*f4a2713aSLionel Sambuc   //
55*f4a2713aSLionel Sambuc   return array1 == array1; // expected-warning{{self-comparison always evaluates to true}}
56*f4a2713aSLionel Sambuc   return array1 != array1; // expected-warning{{self-comparison always evaluates to false}}
57*f4a2713aSLionel Sambuc   return array1 < array1; // expected-warning{{self-comparison always evaluates to false}}
58*f4a2713aSLionel Sambuc   return array1 <= array1; // expected-warning{{self-comparison always evaluates to true}}
59*f4a2713aSLionel Sambuc   return array1 > array1; // expected-warning{{self-comparison always evaluates to false}}
60*f4a2713aSLionel Sambuc   return array1 >= array1; // expected-warning{{self-comparison always evaluates to true}}
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc   //
63*f4a2713aSLionel Sambuc   // compare differrent arrays
64*f4a2713aSLionel Sambuc   //
65*f4a2713aSLionel Sambuc   return array1 == array2; // expected-warning{{array comparison always evaluates to false}}
66*f4a2713aSLionel Sambuc   return array1 != array2; // expected-warning{{array comparison always evaluates to true}}
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc   //
69*f4a2713aSLionel Sambuc   // we don't know what these are going to be
70*f4a2713aSLionel Sambuc   //
71*f4a2713aSLionel Sambuc   return array1 < array2; // expected-warning{{array comparison always evaluates to a constant}}
72*f4a2713aSLionel Sambuc   return array1 <= array2; // expected-warning{{array comparison always evaluates to a constant}}
73*f4a2713aSLionel Sambuc   return array1 > array2; // expected-warning{{array comparison always evaluates to a constant}}
74*f4a2713aSLionel Sambuc   return array1 >= array2; // expected-warning{{array comparison always evaluates to a constant}}
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc }
77*f4a2713aSLionel Sambuc 
78*f4a2713aSLionel Sambuc // Don't issue a warning when either the left or right side of the comparison
79*f4a2713aSLionel Sambuc // results from a macro expansion.  <rdar://problem/8435950>
80*f4a2713aSLionel Sambuc #define R8435950_A i
81*f4a2713aSLionel Sambuc #define R8435950_B i
82*f4a2713aSLionel Sambuc 
R8435950(int i)83*f4a2713aSLionel Sambuc int R8435950(int i) {
84*f4a2713aSLionel Sambuc   if (R8435950_A == R8435950_B) // no-warning
85*f4a2713aSLionel Sambuc    return 0;
86*f4a2713aSLionel Sambuc   return 1;
87*f4a2713aSLionel Sambuc }
88*f4a2713aSLionel Sambuc 
89