From 4d9e7388ccdfdab97f92caac5db9b87a530742f4 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Mon, 3 May 2010 18:24:37 +0000 Subject: [PATCH] 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 . git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@102930 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaInit.cpp | 10 +++++++--- test/SemaObjC/compound-init.m | 7 +++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 test/SemaObjC/compound-init.m diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 1d02701879..d654ed513d 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1691,6 +1691,8 @@ def err_empty_scalar_initializer : Error<"scalar initializer cannot be empty">; 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< diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index 5e6aa2c0a6..0aa3446177 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -624,10 +624,14 @@ void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, } 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; } } diff --git a/test/SemaObjC/compound-init.m b/test/SemaObjC/compound-init.m new file mode 100644 index 0000000000..7b288bb3de --- /dev/null +++ b/test/SemaObjC/compound-init.m @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s +@interface A +@end + +void f() { + (A){ 0 }; // expected-error{{cannot initialize Objective-C class type 'A'}} +} -- 2.40.0