From 99850388609234e01901e316bca6ca6ffbd09337 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Thu, 12 Apr 2012 17:49:18 +0000 Subject: [PATCH] objective-c numeric literal: type of boolean is that of typedef BOOL if found. // rdar://11231426 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154595 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExpr.cpp | 14 +++++++++- test/Rewriter/objc-bool-literal-modern-1.mm | 31 +++++++++++++++++++++ test/SemaObjCXX/literals.mm | 11 +++++++- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 test/Rewriter/objc-bool-literal-modern-1.mm diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 1711f356c6..7748b8c8a6 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -11259,6 +11259,18 @@ ExprResult Sema::ActOnObjCBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { assert((Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) && "Unknown Objective-C Boolean value!"); + QualType ObjCBoolLiteralQT = Context.ObjCBuiltinBoolTy; + // signed char is the default type for boolean literals. Use 'BOOL' + // instead, if BOOL typedef is visible in its scope instead. + Decl *TD = + LookupSingleName(TUScope, &Context.Idents.get("BOOL"), + SourceLocation(), LookupOrdinaryName); + if (TypeDecl *BoolTD = dyn_cast_or_null(TD)) { + QualType QT = QualType(BoolTD->getTypeForDecl(), 0); + if (!QT.isNull()) + ObjCBoolLiteralQT = QT; + } + return Owned(new (Context) ObjCBoolLiteralExpr(Kind == tok::kw___objc_yes, - Context.ObjCBuiltinBoolTy, OpLoc)); + ObjCBoolLiteralQT, OpLoc)); } diff --git a/test/Rewriter/objc-bool-literal-modern-1.mm b/test/Rewriter/objc-bool-literal-modern-1.mm new file mode 100644 index 0000000000..782517242e --- /dev/null +++ b/test/Rewriter/objc-bool-literal-modern-1.mm @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -x objective-c++ -fblocks -fms-extensions -rewrite-objc %s -o %t-rw.cpp +// RUN: %clang_cc1 -fsyntax-only -Wno-address-of-temporary -D"__declspec(X)=" %t-rw.cpp +// rdar://11231426 + +typedef bool BOOL; + +BOOL yes() { + return __objc_yes; +} + +BOOL no() { + return __objc_no; +} + +BOOL which (int flag) { + return flag ? yes() : no(); +} + +int main() { + which (__objc_yes); + which (__objc_no); + return __objc_yes; +} + +void y(BOOL (^foo)()); + +void x() { + y(^{ + return __objc_yes; + }); +} diff --git a/test/SemaObjCXX/literals.mm b/test/SemaObjCXX/literals.mm index 1f6782abbe..eed67652d2 100644 --- a/test/SemaObjCXX/literals.mm +++ b/test/SemaObjCXX/literals.mm @@ -1,6 +1,15 @@ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -fblocks %s -typedef unsigned char BOOL; +// rdar://11231426 +typedef bool BOOL; + +void y(BOOL (^foo)()); + +void x() { + y(^{ + return __objc_yes; + }); +} @protocol NSCopying - copy; -- 2.40.0