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