]> granicus.if.org Git - clang/commitdiff
Warn about implicit conversions between values of different, named
authorDouglas Gregor <dgregor@apple.com>
Tue, 22 Feb 2011 02:45:07 +0000 (02:45 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 22 Feb 2011 02:45:07 +0000 (02:45 +0000)
enumeration types. Fixes <rdar://problem/8559831>.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126183 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaChecking.cpp
test/Sema/conversion.c

index 2c075cb098b50e93ebac5f6d7bbc16d6c8872957..284698222f228c60ff828703d58e52b3074b99df 100644 (file)
@@ -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">,
index 556665483e6490108ed7ef0a44f942158d03f15c..bb5ef7f9b39323befffe9fb7674bac61a315d1d0 100644 (file)
@@ -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;
 }
 
index e78902332eb1b00f37cc1c00f00e0e23fe033809..619d6993251f3101edde17208620b559081188c6 100644 (file)
@@ -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;
+}