diff options
author | Douglas Gregor <dgregor@apple.com> | 2011-02-22 02:45:07 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2011-02-22 02:45:07 +0000 |
commit | 284cc8d8a90ae6558e0a4b60b7dc1ddcfd220758 (patch) | |
tree | 8fd92888a11513a2ffb869c8b44f4436e8835c7c | |
parent | e9b801f7633b11b18d357a71442bd003435784e8 (diff) |
Warn about implicit conversions between values of different, named
enumeration types. Fixes <rdar://problem/8559831>.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126183 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 3 | ||||
-rw-r--r-- | lib/Sema/SemaChecking.cpp | 11 | ||||
-rw-r--r-- | test/Sema/conversion.c | 21 |
3 files changed, 35 insertions, 0 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 2c075cb098..284698222f 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1154,6 +1154,9 @@ def warn_impcast_literal_float_to_integer : Warning< "implicit conversion turns literal floating-point number into integer: " "%0 to %1">, InGroup<DiagGroup<"literal-conversion">>, DefaultIgnore; +def warn_impcast_different_enum_types : Warning< + "implicit conversion from enumeration type %0 to different enumeration type " + "%1">, InGroup<DiagGroup<"conversion">>; def warn_cast_align : Warning< "cast from %0 to %1 increases required alignment from %2 to %3">, diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index 556665483e..bb5ef7f9b3 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -2861,6 +2861,17 @@ void CheckImplicitConversion(Sema &S, Expr *E, QualType T, return DiagnoseImpCast(S, E, T, CC, DiagID); } + // Diagnose conversions between different enumeration types. + if (const EnumType *SourceEnum = Source->getAs<EnumType>()) + if (const EnumType *TargetEnum = Target->getAs<EnumType>()) + if ((SourceEnum->getDecl()->getIdentifier() || + SourceEnum->getDecl()->getTypedefForAnonDecl()) && + (TargetEnum->getDecl()->getIdentifier() || + TargetEnum->getDecl()->getTypedefForAnonDecl()) && + SourceEnum != TargetEnum) + return DiagnoseImpCast(S, E, T, CC, + diag::warn_impcast_different_enum_types); + return; } diff --git a/test/Sema/conversion.c b/test/Sema/conversion.c index e78902332e..619d699325 100644 --- a/test/Sema/conversion.c +++ b/test/Sema/conversion.c @@ -310,3 +310,24 @@ void test_8232669(void) { #define USER_SETBIT(set,bit) do { int i = bit; set[i/(8*sizeof(set[0]))] |= (1 << (i%(8*sizeof(set)))); } while(0) USER_SETBIT(bitset, 0); // expected-warning 2 {{implicit conversion changes signedness}} } + +// <rdar://problem/8559831> +enum E8559831a { E8559831a_val }; +enum E8559831b { E8559831b_val }; +typedef enum { E8559831c_val } E8559831c; +enum { E8559831d_val } value_d; + +void test_8559831_a(enum E8559831a value); +void test_8559831(enum E8559831b value_a, E8559831c value_c) { + test_8559831_a(value_a); // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}} + enum E8559831a a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}} + a1 = value_a; // expected-warning{{implicit conversion from enumeration type 'enum E8559831b' to different enumeration type 'enum E8559831a'}} + + test_8559831_a(value_c); // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}} + enum E8559831a a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}} + a2 = value_c; // expected-warning{{implicit conversion from enumeration type 'E8559831c' to different enumeration type 'enum E8559831a'}} + + test_8559831_a(value_d); + enum E8559831a a3 = value_d; + a3 = value_d; +} |