From 433c2e64f851849737f108371ebddc97e582ee8e Mon Sep 17 00:00:00 2001 From: John McCall Date: Thu, 21 Mar 2013 00:10:07 +0000 Subject: [PATCH] Further weaken block conversion rules to permit blocks with enum return type to be converted to blocks with any integer type of the same size. rdar://13463504 llvm-svn: 177613 --- clang/lib/AST/ASTContext.cpp | 35 +++++++++++++++++++++++++---------- clang/test/Sema/block-return.c | 11 +++++++++++ clang/test/SemaObjC/blocks.m | 4 ++-- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index b55a926..eee7ed2 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -6844,6 +6844,27 @@ QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, return getFunctionNoProtoType(retType, einfo); } +/// Given that we have an enum type and a non-enum type, try to merge them. +static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, + QualType other, bool isBlockReturnType) { + // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, + // a signed integer type, or an unsigned integer type. + // Compatibility is based on the underlying type, not the promotion + // type. + QualType underlyingType = ET->getDecl()->getIntegerType(); + if (underlyingType.isNull()) return QualType(); + if (Context.hasSameType(underlyingType, other)) + return other; + + // In block return types, we're more permissive and accept any + // integral type of the same size. + if (isBlockReturnType && other->isIntegerType() && + Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) + return other; + + return QualType(); +} + QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, bool OfBlockPointer, bool Unqualified, bool BlockReturnType) { @@ -6925,19 +6946,13 @@ QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, // If the canonical type classes don't match. if (LHSClass != RHSClass) { - // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, - // a signed integer type, or an unsigned integer type. - // Compatibility is based on the underlying type, not the promotion - // type. + // Note that we only have special rules for turning block enum + // returns into block int returns, not vice-versa. if (const EnumType* ETy = LHS->getAs()) { - QualType TINT = ETy->getDecl()->getIntegerType(); - if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType())) - return RHS; + return mergeEnumWithInteger(*this, ETy, RHS, false); } if (const EnumType* ETy = RHS->getAs()) { - QualType TINT = ETy->getDecl()->getIntegerType(); - if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType())) - return LHS; + return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); } // allow block pointer type to match an 'id' type. if (OfBlockPointer && !BlockReturnType) { diff --git a/clang/test/Sema/block-return.c b/clang/test/Sema/block-return.c index 2ea4d81..6b4d998 100644 --- a/clang/test/Sema/block-return.c +++ b/clang/test/Sema/block-return.c @@ -134,3 +134,14 @@ void foo7() void (^blk)(void) = ^{ return (void)0; // expected-warning {{void block literal should not return void expression}} }; + +// rdar://13463504 +enum Test8 { T8_a, T8_b, T8_c }; +void test8(void) { + extern void test8_helper(int (^)(int)); + test8_helper(^(int flag) { if (flag) return T8_a; return T8_b; }); +} +void test8b(void) { + extern void test8_helper2(char (^)(int)); // expected-note {{here}} + test8_helper2(^(int flag) { if (flag) return T8_a; return T8_b; }); // expected-error {{passing 'enum Test8 (^)(int)' to parameter of type 'char (^)(int)'}} +} diff --git a/clang/test/SemaObjC/blocks.m b/clang/test/SemaObjC/blocks.m index dd659ad..b523e4c 100644 --- a/clang/test/SemaObjC/blocks.m +++ b/clang/test/SemaObjC/blocks.m @@ -196,8 +196,8 @@ typedef short (^short_block_t)(); void testAnonymousEnumTypes(int arg) { int_block_t IB; IB = ^{ return AnonymousValue; }; - IB = ^{ if (arg) return TDE_Value; else return getTDE(); }; // expected-error {{incompatible block pointer}} - IB = ^{ if (arg) return getTDE(); else return TDE_Value; }; // expected-error {{incompatible block pointer}} + IB = ^{ if (arg) return TDE_Value; else return getTDE(); }; + IB = ^{ if (arg) return getTDE(); else return TDE_Value; }; // Since we fixed the underlying type of the enum, these are considered // compatible block types anyway. -- 2.7.4