annotate clang/test/Sema/tautological-unsigned-enum-zero-compare.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 1d019706d866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-linux-gnu -fsyntax-only \
anatofuz
parents:
diff changeset
2 // RUN: -Wtautological-unsigned-enum-zero-compare \
anatofuz
parents:
diff changeset
3 // RUN: -verify=unsigned,unsigned-signed %s
anatofuz
parents:
diff changeset
4 // RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-win32 -fsyntax-only \
anatofuz
parents:
diff changeset
5 // RUN: -Wtautological-unsigned-enum-zero-compare \
anatofuz
parents:
diff changeset
6 // RUN: -verify=unsigned-signed %s
anatofuz
parents:
diff changeset
7 // RUN: %clang_cc1 -std=c++11 -triple=x86_64-pc-win32 -fsyntax-only \
anatofuz
parents:
diff changeset
8 // RUN: -verify=silence %s
anatofuz
parents:
diff changeset
9
anatofuz
parents:
diff changeset
10 // silence-no-diagnostics
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 int main() {
anatofuz
parents:
diff changeset
13 // On Windows, all enumerations have a fixed underlying type, which is 'int'
anatofuz
parents:
diff changeset
14 // if not otherwise specified, so A is identical to C on Windows. Otherwise,
anatofuz
parents:
diff changeset
15 // we follow the C++ rules, which say that the only valid values of A are 0
anatofuz
parents:
diff changeset
16 // and 1.
anatofuz
parents:
diff changeset
17 enum A { A_foo = 0, A_bar, };
anatofuz
parents:
diff changeset
18 enum A a;
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 enum B : unsigned { B_foo = 0, B_bar, };
anatofuz
parents:
diff changeset
21 enum B b;
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 enum C : signed { C_foo = 0, C_bar, };
anatofuz
parents:
diff changeset
24 enum C c;
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 if (a < 0) // unsigned-warning {{comparison of unsigned enum expression < 0 is always false}}
anatofuz
parents:
diff changeset
27 return 0;
anatofuz
parents:
diff changeset
28 if (0 >= a)
anatofuz
parents:
diff changeset
29 return 0;
anatofuz
parents:
diff changeset
30 if (a > 0)
anatofuz
parents:
diff changeset
31 return 0;
anatofuz
parents:
diff changeset
32 if (0 <= a) // unsigned-warning {{comparison of 0 <= unsigned enum expression is always true}}
anatofuz
parents:
diff changeset
33 return 0;
anatofuz
parents:
diff changeset
34 if (a <= 0)
anatofuz
parents:
diff changeset
35 return 0;
anatofuz
parents:
diff changeset
36 if (0 > a) // unsigned-warning {{comparison of 0 > unsigned enum expression is always false}}
anatofuz
parents:
diff changeset
37 return 0;
anatofuz
parents:
diff changeset
38 if (a >= 0) // unsigned-warning {{comparison of unsigned enum expression >= 0 is always true}}
anatofuz
parents:
diff changeset
39 return 0;
anatofuz
parents:
diff changeset
40 if (0 < a)
anatofuz
parents:
diff changeset
41 return 0;
anatofuz
parents:
diff changeset
42
anatofuz
parents:
diff changeset
43 // FIXME: As below, the issue here is that the enumeration is promoted to
anatofuz
parents:
diff changeset
44 // unsigned.
anatofuz
parents:
diff changeset
45 if (a < 0U) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
anatofuz
parents:
diff changeset
46 return 0;
anatofuz
parents:
diff changeset
47 if (0U >= a)
anatofuz
parents:
diff changeset
48 return 0;
anatofuz
parents:
diff changeset
49 if (a > 0U)
anatofuz
parents:
diff changeset
50 return 0;
anatofuz
parents:
diff changeset
51 if (0U <= a) // unsigned-signed-warning {{comparison of 0 <= unsigned enum expression is always true}}
anatofuz
parents:
diff changeset
52 return 0;
anatofuz
parents:
diff changeset
53 if (a <= 0U)
anatofuz
parents:
diff changeset
54 return 0;
anatofuz
parents:
diff changeset
55 if (0U > a) // unsigned-signed-warning {{comparison of 0 > unsigned enum expression is always false}}
anatofuz
parents:
diff changeset
56 return 0;
anatofuz
parents:
diff changeset
57 if (a >= 0U) // unsigned-signed-warning {{comparison of unsigned enum expression >= 0 is always true}}
anatofuz
parents:
diff changeset
58 return 0;
anatofuz
parents:
diff changeset
59 if (0U < a)
anatofuz
parents:
diff changeset
60 return 0;
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 if (b < 0) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
anatofuz
parents:
diff changeset
63 return 0;
anatofuz
parents:
diff changeset
64 if (0 >= b)
anatofuz
parents:
diff changeset
65 return 0;
anatofuz
parents:
diff changeset
66 if (b > 0)
anatofuz
parents:
diff changeset
67 return 0;
anatofuz
parents:
diff changeset
68 if (0 <= b) // unsigned-signed-warning {{comparison of 0 <= unsigned enum expression is always true}}
anatofuz
parents:
diff changeset
69 return 0;
anatofuz
parents:
diff changeset
70 if (b <= 0)
anatofuz
parents:
diff changeset
71 return 0;
anatofuz
parents:
diff changeset
72 if (0 > b) // unsigned-signed-warning {{comparison of 0 > unsigned enum expression is always false}}
anatofuz
parents:
diff changeset
73 return 0;
anatofuz
parents:
diff changeset
74 if (b >= 0) // unsigned-signed-warning {{comparison of unsigned enum expression >= 0 is always true}}
anatofuz
parents:
diff changeset
75 return 0;
anatofuz
parents:
diff changeset
76 if (0 < b)
anatofuz
parents:
diff changeset
77 return 0;
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 if (b < 0U) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
anatofuz
parents:
diff changeset
80 return 0;
anatofuz
parents:
diff changeset
81 if (0U >= b)
anatofuz
parents:
diff changeset
82 return 0;
anatofuz
parents:
diff changeset
83 if (b > 0U)
anatofuz
parents:
diff changeset
84 return 0;
anatofuz
parents:
diff changeset
85 if (0U <= b) // unsigned-signed-warning {{comparison of 0 <= unsigned enum expression is always true}}
anatofuz
parents:
diff changeset
86 return 0;
anatofuz
parents:
diff changeset
87 if (b <= 0U)
anatofuz
parents:
diff changeset
88 return 0;
anatofuz
parents:
diff changeset
89 if (0U > b) // unsigned-signed-warning {{comparison of 0 > unsigned enum expression is always false}}
anatofuz
parents:
diff changeset
90 return 0;
anatofuz
parents:
diff changeset
91 if (b >= 0U) // unsigned-signed-warning {{comparison of unsigned enum expression >= 0 is always true}}
anatofuz
parents:
diff changeset
92 return 0;
anatofuz
parents:
diff changeset
93 if (0U < b)
anatofuz
parents:
diff changeset
94 return 0;
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 if (c < 0)
anatofuz
parents:
diff changeset
97 return 0;
anatofuz
parents:
diff changeset
98 if (0 >= c)
anatofuz
parents:
diff changeset
99 return 0;
anatofuz
parents:
diff changeset
100 if (c > 0)
anatofuz
parents:
diff changeset
101 return 0;
anatofuz
parents:
diff changeset
102 if (0 <= c)
anatofuz
parents:
diff changeset
103 return 0;
anatofuz
parents:
diff changeset
104 if (c <= 0)
anatofuz
parents:
diff changeset
105 return 0;
anatofuz
parents:
diff changeset
106 if (0 > c)
anatofuz
parents:
diff changeset
107 return 0;
anatofuz
parents:
diff changeset
108 if (c >= 0)
anatofuz
parents:
diff changeset
109 return 0;
anatofuz
parents:
diff changeset
110 if (0 < c)
anatofuz
parents:
diff changeset
111 return 0;
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 // FIXME: These diagnostics are terrible. The issue here is that the signed
anatofuz
parents:
diff changeset
114 // enumeration value was promoted to an unsigned type.
anatofuz
parents:
diff changeset
115 if (c < 0U) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
anatofuz
parents:
diff changeset
116 return 0;
anatofuz
parents:
diff changeset
117 if (0U >= c)
anatofuz
parents:
diff changeset
118 return 0;
anatofuz
parents:
diff changeset
119 if (c > 0U)
anatofuz
parents:
diff changeset
120 return 0;
anatofuz
parents:
diff changeset
121 if (0U <= c) // unsigned-signed-warning {{comparison of 0 <= unsigned enum expression is always true}}
anatofuz
parents:
diff changeset
122 return 0;
anatofuz
parents:
diff changeset
123 if (c <= 0U)
anatofuz
parents:
diff changeset
124 return 0;
anatofuz
parents:
diff changeset
125 if (0U > c) // unsigned-signed-warning {{comparison of 0 > unsigned enum expression is always false}}
anatofuz
parents:
diff changeset
126 return 0;
anatofuz
parents:
diff changeset
127 if (c >= 0U) // unsigned-signed-warning {{comparison of unsigned enum expression >= 0 is always true}}
anatofuz
parents:
diff changeset
128 return 0;
anatofuz
parents:
diff changeset
129 if (0U < c)
anatofuz
parents:
diff changeset
130 return 0;
anatofuz
parents:
diff changeset
131
anatofuz
parents:
diff changeset
132 return 1;
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 namespace crash_enum_zero_width {
anatofuz
parents:
diff changeset
136 int test() {
anatofuz
parents:
diff changeset
137 enum A : unsigned {
anatofuz
parents:
diff changeset
138 A_foo = 0
anatofuz
parents:
diff changeset
139 };
anatofuz
parents:
diff changeset
140 enum A a;
anatofuz
parents:
diff changeset
141
anatofuz
parents:
diff changeset
142 // used to crash in llvm::APSInt::getMaxValue()
anatofuz
parents:
diff changeset
143 if (a < 0) // unsigned-signed-warning {{comparison of unsigned enum expression < 0 is always false}}
anatofuz
parents:
diff changeset
144 return 0;
anatofuz
parents:
diff changeset
145
anatofuz
parents:
diff changeset
146 return 1;
anatofuz
parents:
diff changeset
147 }
anatofuz
parents:
diff changeset
148 } // namespace crash_enum_zero_width