xref: /llvm-project/clang/test/Sema/warn-compare-enum-types-mismatch.c (revision f8fab2126ffab713f4ab4619360b6941be6d4e35)
1 // RUN: %clang_cc1 -x c -fsyntax-only -verify -Wenum-compare -Wno-unused-comparison %s
2 // RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wenum-compare -Wno-unused-comparison %s
3 
4 // In C enumerators (i.e enumeration constants) have type int (until C23). In
5 // order to support diagnostics such as -Wenum-compare we pretend they have the
6 // type of their enumeration.
7 
8 typedef enum EnumA {
9   A
10 } EnumA;
11 
12 enum EnumB {
13   B,
14   B1 = 1,
15   // In C++ this comparison doesnt warn as enumerators dont have the type of
16   // their enumeration before the closing brace. We mantain the same behavior
17   // in C.
18   B2 = A == B1
19 };
20 
21 enum {
22   C
23 };
24 
foo(void)25 void foo(void) {
26   enum EnumA a = A;
27   enum EnumB b = B;
28   A == B;
29   // expected-warning@-1 {{comparison of different enumeration types}}
30   a == (B);
31   // expected-warning@-1 {{comparison of different enumeration types}}
32   a == b;
33   // expected-warning@-1 {{comparison of different enumeration types}}
34   A > B;
35   // expected-warning@-1 {{comparison of different enumeration types}}
36   A >= b;
37   // expected-warning@-1 {{comparison of different enumeration types}}
38   a > b;
39   // expected-warning@-1 {{comparison of different enumeration types}}
40   (A) <= ((B));
41   // expected-warning@-1 {{comparison of different enumeration types}}
42   a < B;
43   // expected-warning@-1 {{comparison of different enumeration types}}
44   a < b;
45   // expected-warning@-1 {{comparison of different enumeration types}}
46 
47   // In the following cases we purposefully differ from GCC and dont warn
48   a == C;
49   A < C;
50   b >= C;
51 }
52