xref: /llvm-project/clang/test/Sema/warn-outof-range-assign-enum.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wassign-enum %s
2 
3 typedef enum CCTestEnum
4 {
5   One,
6   Two=4,
7   Three
8 } CCTestEnum;
9 
10 CCTestEnum test = 50; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
11 CCTestEnum test1 = -50; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
12 
13 // Explicit cast should silence the warning.
14 static const CCTestEnum SilenceWithCast1 = 51; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
15 static const CCTestEnum SilenceWithCast2 = (CCTestEnum) 51; // no-warning
16 static const CCTestEnum SilenceWithCast3 = (const CCTestEnum) 51; // no-warning
17 static const CCTestEnum SilenceWithCast4 = (const volatile CCTestEnum) 51; // no-warning
18 
SilenceWithCastLocalVar(void)19 void SilenceWithCastLocalVar(void) {
20   CCTestEnum SilenceWithCast1 = 51; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
21   CCTestEnum SilenceWithCast2 = (CCTestEnum) 51; // no-warning
22   CCTestEnum SilenceWithCast3 = (const CCTestEnum) 51; // no-warning
23   CCTestEnum SilenceWithCast4 = (const volatile CCTestEnum) 51; // no-warning
24 
25   const CCTestEnum SilenceWithCast1c = 51; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
26   const CCTestEnum SilenceWithCast2c = (CCTestEnum) 51; // no-warning
27   const CCTestEnum SilenceWithCast3c = (const CCTestEnum) 51; // no-warning
28   const CCTestEnum SilenceWithCast4c = (const volatile CCTestEnum) 51; // no-warning
29 }
30 
foo(CCTestEnum r)31 CCTestEnum foo(CCTestEnum r) {
32   return 20; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
33 }
34 
35 enum Test2 { K_zero, K_one };
test2(enum Test2 * t)36 enum Test2 test2(enum Test2 *t) {
37   *t = 20; // expected-warning {{integer constant not in range of enumerated type 'enum Test2'}}
38   return 10; // expected-warning {{integer constant not in range of enumerated type 'enum Test2'}}
39 }
40 
41 // PR15069
42 typedef enum
43 {
44   a = 0
45 } T;
46 
f(void)47 void f(void)
48 {
49   T x = a;
50   x += 1; // expected-warning {{integer constant not in range of enumerated type}}
51 }
52 
main(void)53 int main(void) {
54   CCTestEnum test = 1; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
55   test = 600; // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
56   foo(2); // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
57   foo(-1); // expected-warning {{integer constant not in range of enumerated type 'CCTestEnum'}}
58   foo(4);
59   foo(Two+1);
60 }
61 
62