1 // RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-linux-gnu -fsyntax-only -DALL_WARN -verify %s
2 // RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-win32 -fsyntax-only -DSIGN_WARN -verify %s
3 // RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-win32 -fsyntax-only -Wno-tautological-unsigned-enum-zero-compare -verify %s
4 
5 // Okay, this is where it gets complicated.
6 // Then default enum sigdness is target-specific.
7 // On windows, it is signed by default. We do not want to warn in that case.
8 
9 int main() {
10   enum A { A_foo, A_bar };
11   enum A a;
12 
13   enum B : unsigned { B_foo, B_bar };
14   enum B b;
15 
16   enum C : signed { c_foo, c_bar };
17   enum C c;
18 
19 #ifdef ALL_WARN
20   if (a < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
21     return 0;
22   if (a >= 0) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
23     return 0;
24   if (0 <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
25     return 0;
26   if (0 > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
27     return 0;
28   if (a < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
29     return 0;
30   if (a >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
31     return 0;
32   if (0U <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
33     return 0;
34   if (0U > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
35     return 0;
36 
37   if (b < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
38     return 0;
39   if (b >= 0) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
40     return 0;
41   if (0 <= b) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
42     return 0;
43   if (0 > b) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
44     return 0;
45   if (b < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
46     return 0;
47   if (b >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
48     return 0;
49   if (0U <= b) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
50     return 0;
51   if (0U > b) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
52     return 0;
53 
54   if (c < 0) // ok
55     return 0;
56   if (c >= 0) // ok
57     return 0;
58   if (0 <= c) // ok
59     return 0;
60   if (0 > c) // ok
61     return 0;
62   if (c < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
63     return 0;
64   if (c >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
65     return 0;
66   if (0U <= c) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
67     return 0;
68   if (0U > c) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
69     return 0;
70 #elif defined(SIGN_WARN)
71   if (a < 0) // ok
72     return 0;
73   if (a >= 0) // ok
74     return 0;
75   if (0 <= a) // ok
76     return 0;
77   if (0 > a) // ok
78     return 0;
79   if (a < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
80     return 0;
81   if (a >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
82     return 0;
83   if (0U <= a) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
84     return 0;
85   if (0U > a) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
86     return 0;
87 
88   if (b < 0) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
89     return 0;
90   if (b >= 0) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
91     return 0;
92   if (0 <= b) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
93     return 0;
94   if (0 > b) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
95     return 0;
96   if (b < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
97     return 0;
98   if (b >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
99     return 0;
100   if (0U <= b) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
101     return 0;
102   if (0U > b) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
103     return 0;
104 
105   if (c < 0) // ok
106     return 0;
107   if (c >= 0) // ok
108     return 0;
109   if (0 <= c) // ok
110     return 0;
111   if (0 > c) // ok
112     return 0;
113   if (c < 0U) // expected-warning {{comparison of unsigned enum expression < 0 is always false}}
114     return 0;
115   if (c >= 0U) // expected-warning {{comparison of unsigned enum expression >= 0 is always true}}
116     return 0;
117   if (0U <= c) // expected-warning {{comparison of 0 <= unsigned enum expression is always true}}
118     return 0;
119   if (0U > c) // expected-warning {{comparison of 0 > unsigned enum expression is always false}}
120     return 0;
121 #else
122   // expected-no-diagnostics
123   if (a < 0)
124     return 0;
125   if (a >= 0)
126     return 0;
127   if (0 <= a)
128     return 0;
129   if (0 > a)
130     return 0;
131   if (a < 0U)
132     return 0;
133   if (a >= 0U)
134     return 0;
135   if (0U <= a)
136     return 0;
137   if (0U > a)
138     return 0;
139 
140   if (b < 0)
141     return 0;
142   if (b >= 0)
143     return 0;
144   if (0 <= b)
145     return 0;
146   if (0 > b)
147     return 0;
148   if (b < 0U)
149     return 0;
150   if (b >= 0U)
151     return 0;
152   if (0U <= b)
153     return 0;
154   if (0U > b)
155     return 0;
156 
157   if (c < 0)
158     return 0;
159   if (c >= 0)
160     return 0;
161   if (0 <= c)
162     return 0;
163   if (0 > c)
164     return 0;
165   if (c < 0U)
166     return 0;
167   if (c >= 0U)
168     return 0;
169   if (0U <= c)
170     return 0;
171   if (0U > c)
172     return 0;
173 #endif
174 
175   return 1;
176 }
177