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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156843 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index b1b4044..7436f8b 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -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<
diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp
index 7ade400..9a5a279 100644
--- a/lib/Sema/SemaExprObjC.cpp
+++ b/lib/Sema/SemaExprObjC.cpp
@@ -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) {
diff --git a/test/SemaObjC/boxing-illegal-types.m b/test/SemaObjC/boxing-illegal-types.m
index 7771094..ad45b11 100644
--- a/test/SemaObjC/boxing-illegal-types.m
+++ b/test/SemaObjC/boxing-illegal-types.m
@@ -1,5 +1,29 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -Wattributes %s
 
+typedef long NSInteger;
+typedef unsigned long NSUInteger;
+typedef signed char BOOL;
+
+@interface NSNumber
+@end
+@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;
+@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}}
+}
diff --git a/test/SemaObjCXX/boxing-illegal-types.mm b/test/SemaObjCXX/boxing-illegal-types.mm
new file mode 100644
index 0000000..7729753
--- /dev/null
+++ b/test/SemaObjCXX/boxing-illegal-types.mm
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wattributes %s
+
+typedef long NSInteger;
+typedef unsigned long NSUInteger;
+typedef signed char BOOL;
+
+@interface NSNumber
+@end
+@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;
+@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}}
+}