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