blob: a733b6edfc03427ca1469ea38738cefab84497ed [file] [log] [blame]
Hal Finkel05e46482017-12-16 02:23:22 +00001// RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-linux-gnu -fsyntax-only \
2// RUN: -verify=unsigned,unsigned-signed %s
3// RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-win32 -fsyntax-only \
4// RUN: -verify=unsigned-signed %s
5// RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-win32 -fsyntax-only \
6// RUN: -Wno-tautological-unsigned-enum-zero-compare \
7// RUN: -verify=silence %s
8
9// silence-no-diagnostics
Roman Lebedev30d26082017-09-20 13:50:01 +000010
Roman Lebedev30d26082017-09-20 13:50:01 +000011int main() {
Richard Smith371e9e8a2017-12-06 03:00:51 +000012 // On Windows, all enumerations have a fixed underlying type, which is 'int'
13 // if not otherwise specified, so A is identical to C on Windows. Otherwise,
14 // we follow the C++ rules, which say that the only valid values of A are 0
15 // and 1.
Roman Lebedevca1aaac2017-10-21 16:44:03 +000016 enum A { A_foo = 0, A_bar, };
Roman Lebedev30d26082017-09-20 13:50:01 +000017 enum A a;
18
Roman Lebedevca1aaac2017-10-21 16:44:03 +000019 enum B : unsigned { B_foo = 0, B_bar, };
Roman Lebedev30d26082017-09-20 13:50:01 +000020 enum B b;
21
Roman Lebedevca1aaac2017-10-21 16:44:03 +000022 enum C : signed { C_foo = 0, C_bar, };
Roman Lebedev30d26082017-09-20 13:50:01 +000023 enum C c;
24
Hal Finkel05e46482017-12-16 02:23:22 +000025 if (a < 0) // unsigned-warning {{comparison of unsigned enum expression < 0 is always false}}
Roman Lebedev30d26082017-09-20 13:50:01 +000026 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000027 if (0 >= a)
28 return 0;
29 if (a > 0)
Roman Lebedev30d26082017-09-20 13:50:01 +000030 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000031 if (0 <= a) // unsigned-warning {{comparison of 0 <= unsigned enum expression is always true}}
Roman Lebedev30d26082017-09-20 13:50:01 +000032 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000033 if (a <= 0)
34 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000035 if (0 > a) // unsigned-warning {{comparison of 0 > unsigned enum expression is always false}}
Roman Lebedev30d26082017-09-20 13:50:01 +000036 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000037 if (a >= 0) // unsigned-warning {{comparison of unsigned enum expression >= 0 is always true}}
Roman Lebedevca1aaac2017-10-21 16:44:03 +000038 return 0;
39 if (0 < a)
40 return 0;
41
Hal Finkel05e46482017-12-16 02:23:22 +000042 // FIXME: As below, the issue here is that the enumeration is promoted to
43 // unsigned.
44 if (a < 0U) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
Roman Lebedev30d26082017-09-20 13:50:01 +000045 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000046 if (0U >= a)
47 return 0;
48 if (a > 0U)
Roman Lebedev30d26082017-09-20 13:50:01 +000049 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000050 if (0U <= a) // unsigned-signed-warning {{comparison of 0 <= unsigned enum expression is always true}}
Roman Lebedev30d26082017-09-20 13:50:01 +000051 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000052 if (a <= 0U)
53 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000054 if (0U > a) // unsigned-signed-warning {{comparison of 0 > unsigned enum expression is always false}}
Roman Lebedev30d26082017-09-20 13:50:01 +000055 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000056 if (a >= 0U) // unsigned-signed-warning {{comparison of unsigned enum expression >= 0 is always true}}
Roman Lebedevca1aaac2017-10-21 16:44:03 +000057 return 0;
58 if (0U < a)
59 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +000060
Hal Finkel05e46482017-12-16 02:23:22 +000061 if (b < 0) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
Roman Lebedev30d26082017-09-20 13:50:01 +000062 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000063 if (0 >= b)
64 return 0;
65 if (b > 0)
Roman Lebedev30d26082017-09-20 13:50:01 +000066 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000067 if (0 <= b) // unsigned-signed-warning {{comparison of 0 <= unsigned enum expression is always true}}
Roman Lebedev30d26082017-09-20 13:50:01 +000068 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000069 if (b <= 0)
70 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000071 if (0 > b) // unsigned-signed-warning {{comparison of 0 > unsigned enum expression is always false}}
Roman Lebedev30d26082017-09-20 13:50:01 +000072 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000073 if (b >= 0) // unsigned-signed-warning {{comparison of unsigned enum expression >= 0 is always true}}
Roman Lebedev30d26082017-09-20 13:50:01 +000074 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000075 if (0 < b)
Roman Lebedev30d26082017-09-20 13:50:01 +000076 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000077
Hal Finkel05e46482017-12-16 02:23:22 +000078 if (b < 0U) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
Roman Lebedev30d26082017-09-20 13:50:01 +000079 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000080 if (0U >= b)
81 return 0;
82 if (b > 0U)
Roman Lebedev30d26082017-09-20 13:50:01 +000083 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000084 if (0U <= b) // unsigned-signed-warning {{comparison of 0 <= unsigned enum expression is always true}}
Roman Lebedev30d26082017-09-20 13:50:01 +000085 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000086 if (b <= 0U)
87 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000088 if (0U > b) // unsigned-signed-warning {{comparison of 0 > unsigned enum expression is always false}}
Roman Lebedev30d26082017-09-20 13:50:01 +000089 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +000090 if (b >= 0U) // unsigned-signed-warning {{comparison of unsigned enum expression >= 0 is always true}}
Roman Lebedev30d26082017-09-20 13:50:01 +000091 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +000092 if (0U < b)
Roman Lebedev30d26082017-09-20 13:50:01 +000093 return 0;
94
95 if (c < 0)
96 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +000097 if (0 >= c)
Roman Lebedevca1aaac2017-10-21 16:44:03 +000098 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +000099 if (c > 0)
Roman Lebedev30d26082017-09-20 13:50:01 +0000100 return 0;
101 if (0 <= c)
102 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000103 if (c <= 0)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000104 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +0000105 if (0 > c)
106 return 0;
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000107 if (c >= 0)
108 return 0;
Richard Smith371e9e8a2017-12-06 03:00:51 +0000109 if (0 < c)
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000110 return 0;
111
Richard Smith371e9e8a2017-12-06 03:00:51 +0000112 // FIXME: These diagnostics are terrible. The issue here is that the signed
113 // enumeration value was promoted to an unsigned type.
Hal Finkel05e46482017-12-16 02:23:22 +0000114 if (c < 0U) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000115 return 0;
116 if (0U >= c)
117 return 0;
118 if (c > 0U)
119 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +0000120 if (0U <= c) // unsigned-signed-warning {{comparison of 0 <= unsigned enum expression is always true}}
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000121 return 0;
122 if (c <= 0U)
123 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +0000124 if (0U > c) // unsigned-signed-warning {{comparison of 0 > unsigned enum expression is always false}}
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000125 return 0;
Hal Finkel05e46482017-12-16 02:23:22 +0000126 if (c >= 0U) // unsigned-signed-warning {{comparison of unsigned enum expression >= 0 is always true}}
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000127 return 0;
128 if (0U < c)
129 return 0;
Roman Lebedev30d26082017-09-20 13:50:01 +0000130
131 return 1;
132}
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000133
134namespace crash_enum_zero_width {
135int test() {
136 enum A : unsigned {
137 A_foo = 0
138 };
139 enum A a;
140
141 // used to crash in llvm::APSInt::getMaxValue()
Hal Finkel05e46482017-12-16 02:23:22 +0000142 if (a < 0) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
Roman Lebedevca1aaac2017-10-21 16:44:03 +0000143 return 0;
144
145 return 1;
146}
147} // namespace crash_enum_zero_width