[cfe-commits] r102930 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaInit.cpp test/SemaObjC/compound-init.m

Douglas Gregor dgregor at apple.com
Mon May 3 11:24:38 PDT 2010


Author: dgregor
Date: Mon May  3 13:24:37 2010
New Revision: 102930

URL: http://llvm.org/viewvc/llvm-project?rev=102930&view=rev
Log:
Complain when we try to initialize an object of Objective-C class type
(which is ill-formed) with an initializer list. Also, change the
fallback from an assertion to a generic error message, which is far
friendlier. Fixes <rdar://problem/7730948>.

Added:
    cfe/trunk/test/SemaObjC/compound-init.m
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaInit.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=102930&r1=102929&r2=102930&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon May  3 13:24:37 2010
@@ -1691,6 +1691,8 @@
 def err_illegal_initializer : Error<
   "illegal initializer (only variables can be initialized)">;
 def err_illegal_initializer_type : Error<"illegal initializer type %0">;
+def err_init_objc_class : Error<
+  "cannot initialize Objective-C class type %0">;
 def err_implicit_empty_initializer : Error<
   "initializer for aggregate with no elements requires explicit braces">;
 def err_bitfield_has_negative_width : Error<

Modified: cfe/trunk/lib/Sema/SemaInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=102930&r1=102929&r2=102930&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)
+++ cfe/trunk/lib/Sema/SemaInit.cpp Mon May  3 13:24:37 2010
@@ -624,10 +624,14 @@
   } else if (DeclType->isReferenceType()) {
     CheckReferenceType(Entity, IList, DeclType, Index,
                        StructuredList, StructuredIndex);
+  } else if (DeclType->isObjCInterfaceType()) {
+    SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class)
+      << DeclType;
+    hadError = true;
   } else {
-    // In C, all types are either scalars or aggregates, but
-    // additional handling is needed here for C++ (and possibly others?).
-    assert(0 && "Unsupported initializer type");
+    SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
+      << DeclType;
+    hadError = true;
   }
 }
 

Added: cfe/trunk/test/SemaObjC/compound-init.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/compound-init.m?rev=102930&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/compound-init.m (added)
+++ cfe/trunk/test/SemaObjC/compound-init.m Mon May  3 13:24:37 2010
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+ at interface A
+ at end
+
+void f() {
+  (A){ 0 }; // expected-error{{cannot initialize Objective-C class type 'A'}}
+}





More information about the cfe-commits mailing list