[cfe-commits] r156843 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExprObjC.cpp test/SemaObjC/boxing-illegal-types.m test/SemaObjCXX/boxing-illegal-types.mm

Argyrios Kyrtzidis akyrtzi at gmail.com
Tue May 15 12:17:44 PDT 2012


Author: akirtzidis
Date: Tue May 15 14:17:44 2012
New Revision: 156843

URL: http://llvm.org/viewvc/llvm-project?rev=156843&view=rev
Log:
Allow objc @() syntax for enum types.

Previously we would reject it as illegal using a value of
enum type and on ObjC++ it was illegal to use an enumerator
as well.

rdar://11454917

Added:
    cfe/trunk/test/SemaObjCXX/boxing-illegal-types.mm
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExprObjC.cpp
    cfe/trunk/test/SemaObjC/boxing-illegal-types.m

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=156843&r1=156842&r2=156843&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue May 15 14:17:44 2012
@@ -1540,6 +1540,8 @@
   "cannot box a string value because NSString has not been declared">;
 def err_objc_illegal_boxed_expression_type : Error<
   "illegal type %0 used in a boxed expression">;
+def err_objc_incomplete_boxed_expression_type : Error<
+  "incomplete type %0 used in a boxed expression">;
 def err_undeclared_nsarray : Error<
   "NSArray must be available to use Objective-C array literals">;
 def err_undeclared_nsdictionary : Error<

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=156843&r1=156842&r2=156843&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Tue May 15 14:17:44 2012
@@ -539,6 +539,17 @@
     // Look for the appropriate method within NSNumber.
     BoxingMethod = getNSNumberFactoryMethod(*this, SR.getBegin(), ValueType);
     BoxedType = NSNumberPointer;
+
+  } else if (const EnumType *ET = ValueType->getAs<EnumType>()) {
+    if (!ET->getDecl()->isComplete()) {
+      Diag(SR.getBegin(), diag::err_objc_incomplete_boxed_expression_type)
+        << ValueType << ValueExpr->getSourceRange();
+      return ExprError();
+    }
+
+    BoxingMethod = getNSNumberFactoryMethod(*this, SR.getBegin(),
+                                            ET->getDecl()->getIntegerType());
+    BoxedType = NSNumberPointer;
   }
 
   if (!BoxingMethod) {

Modified: cfe/trunk/test/SemaObjC/boxing-illegal-types.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/boxing-illegal-types.m?rev=156843&r1=156842&r2=156843&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/boxing-illegal-types.m (original)
+++ cfe/trunk/test/SemaObjC/boxing-illegal-types.m Tue May 15 14:17:44 2012
@@ -1,5 +1,29 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -Wattributes %s
 
+typedef long NSInteger;
+typedef unsigned long NSUInteger;
+typedef signed char BOOL;
+
+ at interface NSNumber
+ at end
+ at interface NSNumber (NSNumberCreation)
++ (NSNumber *)numberWithChar:(char)value;
++ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
++ (NSNumber *)numberWithShort:(short)value;
++ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
++ (NSNumber *)numberWithInt:(int)value;
++ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
++ (NSNumber *)numberWithLong:(long)value;
++ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
++ (NSNumber *)numberWithLongLong:(long long)value;
++ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
++ (NSNumber *)numberWithFloat:(float)value;
++ (NSNumber *)numberWithDouble:(double)value;
++ (NSNumber *)numberWithBool:(BOOL)value;
++ (NSNumber *)numberWithInteger:(NSInteger)value;
++ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value;
+ at end
+
 typedef struct {
     int x, y, z;
 } point;
@@ -19,3 +43,16 @@
 void testInvalid() {
   @(not_defined); // expected-error {{use of undeclared identifier 'not_defined'}}
 }
+
+enum MyEnum {
+  ME_foo
+};
+
+enum ForwE;
+
+void testEnum(void *p) {
+  enum MyEnum myen;
+  id box = @(myen);
+  box = @(ME_foo);
+  box = @(*(enum ForwE*)p); // expected-error {{incomplete type 'enum ForwE' used in a boxed expression}}
+}

Added: cfe/trunk/test/SemaObjCXX/boxing-illegal-types.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/boxing-illegal-types.mm?rev=156843&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjCXX/boxing-illegal-types.mm (added)
+++ cfe/trunk/test/SemaObjCXX/boxing-illegal-types.mm Tue May 15 14:17:44 2012
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wattributes %s
+
+typedef long NSInteger;
+typedef unsigned long NSUInteger;
+typedef signed char BOOL;
+
+ at interface NSNumber
+ at end
+ at interface NSNumber (NSNumberCreation)
++ (NSNumber *)numberWithChar:(char)value;
++ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
++ (NSNumber *)numberWithShort:(short)value;
++ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
++ (NSNumber *)numberWithInt:(int)value;
++ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
++ (NSNumber *)numberWithLong:(long)value;
++ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
++ (NSNumber *)numberWithLongLong:(long long)value;
++ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
++ (NSNumber *)numberWithFloat:(float)value;
++ (NSNumber *)numberWithDouble:(double)value;
++ (NSNumber *)numberWithBool:(BOOL)value;
++ (NSNumber *)numberWithInteger:(NSInteger)value;
++ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value;
+ at end
+
+typedef struct {
+    int x, y, z;
+} point;
+
+void testStruct() {
+    point p = { 0, 0, 0 };
+    id boxed = @(p);    // expected-error {{illegal type 'point' used in a boxed expression}}
+}
+
+void testPointers() {
+    void *null = 0;
+    id boxed_null = @(null);        // expected-error {{illegal type 'void *' used in a boxed expression}}
+    int numbers[] = { 0, 1, 2 };
+    id boxed_numbers = @(numbers);  // expected-error {{illegal type 'int *' used in a boxed expression}}
+}
+
+void testInvalid() {
+  @(not_defined); // expected-error {{use of undeclared identifier 'not_defined'}}
+}
+
+enum MyEnum {
+  ME_foo
+};
+
+enum ForwE; // expected-error {{ISO C++ forbids forward references to 'enum' types}}
+
+void testEnum(void *p) {
+  enum MyEnum myen;
+  id box = @(myen);
+  box = @(ME_foo);
+  box = @(*(enum ForwE*)p); // expected-error {{incomplete type 'enum ForwE' used in a boxed expression}}
+}





More information about the cfe-commits mailing list