Skip to content

Commit

Permalink
objective-c modern translator: buildit objc bool
Browse files Browse the repository at this point in the history
type for rewriter project will be BoolTy.
// rdar://11231426. 

llvm-svn: 154861
  • Loading branch information
Fariborz Jahanian committed Apr 16, 2012
1 parent 1e8ac36 commit 29898f4
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 37 deletions.
3 changes: 0 additions & 3 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -1547,9 +1547,6 @@ def warn_objc_redundant_literal_use : Warning<
"using %0 with a literal is redundant">, InGroup<ObjCRedundantLiteralUse>;
}

def warn_bool_for_boolean_literal : Warning<
"BOOL of type %0 is non-intergal and unsuitable for a "
"boolean literal - ignored">, InGroup<DiagGroup<"numeric-literals">>;
def err_only_annotate_after_access_spec : Error<
"access specifier can only have annotation attributes">;
def err_attribute_section_invalid_for_target : Error<
Expand Down
15 changes: 15 additions & 0 deletions clang/include/clang/Basic/TargetInfo.h
Expand Up @@ -132,6 +132,11 @@ class TargetInfo : public RefCountedBase<TargetInfo> {
IntType SizeType, IntMaxType, UIntMaxType, PtrDiffType, IntPtrType, WCharType,
WIntType, Char16Type, Char32Type, Int64Type, SigAtomicType;

/// Flag whether the Objective-C built-in boolean type should be signed char.
/// Otherwise, when this flag is not set, the normal built-in boolean type is
/// used.
unsigned UseSignedCharForObjCBool : 1;

/// Control whether the alignment of bit-field types is respected when laying
/// out structures. If true, then the alignment of the bit-field type will be
/// used to (a) impact the alignment of the containing structure, and (b)
Expand Down Expand Up @@ -299,6 +304,16 @@ class TargetInfo : public RefCountedBase<TargetInfo> {
return MCountName;
}

/// useSignedCharForObjCBool - Check if the Objective-C built-in boolean
/// type should be signed char. Otherwise, if this returns false, the
/// normal built-in boolean type should also be used for Objective-C.
bool useSignedCharForObjCBool() const {
return UseSignedCharForObjCBool;
}
void noSignedCharForObjCBool() {
UseSignedCharForObjCBool = false;
}

/// useBitFieldTypeAlignment() - Check whether the alignment of bit-field
/// types is respected when laying out structures.
bool useBitFieldTypeAlignment() const {
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/AST/ASTContext.cpp
Expand Up @@ -481,7 +481,8 @@ void ASTContext::InitBuiltinTypes(const TargetInfo &Target) {
InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);

// Builtin type for __objc_yes and __objc_no
ObjCBuiltinBoolTy = SignedCharTy;
ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ?
SignedCharTy : BoolTy);

ObjCConstantStringType = QualType();

Expand Down
1 change: 1 addition & 0 deletions clang/lib/Basic/TargetInfo.cpp
Expand Up @@ -58,6 +58,7 @@ TargetInfo::TargetInfo(const std::string &T) : Triple(T) {
Char32Type = UnsignedInt;
Int64Type = SignedLongLong;
SigAtomicType = SignedInt;
UseSignedCharForObjCBool = true;
UseBitFieldTypeAlignment = true;
UseZeroLengthBitfieldAlignment = false;
ZeroLengthBitfieldBoundary = 0;
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Frontend/CompilerInstance.cpp
Expand Up @@ -651,6 +651,10 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) {
// created. This complexity should be lifted elsewhere.
getTarget().setForcedLangOptions(getLangOpts());

// rewriter project will change target built-in bool type from its default.
if (getFrontendOpts().ProgramAction == frontend::RewriteObjC)
getTarget().noSignedCharForObjCBool();

// Validate/process some options.
if (getHeaderSearchOpts().Verbose)
OS << "clang -cc1 version " CLANG_VERSION_STRING
Expand Down
18 changes: 1 addition & 17 deletions clang/lib/Sema/SemaExpr.cpp
Expand Up @@ -11268,22 +11268,6 @@ 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 (TypedefDecl *BoolTD = dyn_cast_or_null<TypedefDecl>(TD)) {
QualType QT = BoolTD->getUnderlyingType();
if (!QT->isIntegralOrUnscopedEnumerationType()) {
Diag(OpLoc, diag::warn_bool_for_boolean_literal) << QT;
Diag(BoolTD->getLocation(), diag::note_previous_declaration);
}
else
ObjCBoolLiteralQT = QT;
}

return Owned(new (Context) ObjCBoolLiteralExpr(Kind == tok::kw___objc_yes,
ObjCBoolLiteralQT, OpLoc));
Context.ObjCBuiltinBoolTy, OpLoc));
}
12 changes: 6 additions & 6 deletions clang/test/Rewriter/objc-bool-literal-check-modern.mm
Expand Up @@ -2,7 +2,7 @@
// RUN: %clang_cc1 -x objective-c++ -fblocks -fms-extensions -rewrite-objc %t.mm -o - | FileCheck %s
// rdar://11124775

typedef signed char BOOL;
typedef bool BOOL;

BOOL yes() {
return __objc_yes;
Expand All @@ -22,8 +22,8 @@ int main() {
return __objc_yes;
}

// CHECK: return ((signed char)1);
// CHECK: return ((signed char)0);
// CHECK: which (((signed char)1));
// CHECK: which (((signed char)0));
// CHECK: return ((signed char)1);
// CHECK: return ((bool)1);
// CHECK: return ((bool)0);
// CHECK: which (((bool)1));
// CHECK: which (((bool)0));
// CHECK: return ((bool)1);
2 changes: 1 addition & 1 deletion clang/test/Rewriter/objc-bool-literal-modern.mm
Expand Up @@ -2,7 +2,7 @@
// RUN: %clang_cc1 -fsyntax-only -D"__declspec(X)=" %t-rw.cpp
// rdar://11124775

typedef signed char BOOL;
typedef bool BOOL;

BOOL yes() {
return __objc_yes;
Expand Down
10 changes: 5 additions & 5 deletions clang/test/Rewriter/objc-modern-numeric-literal.mm
Expand Up @@ -4,7 +4,7 @@

extern "C" void *sel_registerName(const char *);

typedef signed char BOOL;
typedef bool BOOL;
typedef long NSInteger;
typedef unsigned long NSUInteger;

Expand Down Expand Up @@ -63,7 +63,7 @@ int main(int argc, const char *argv[]) {
// CHECK: NSNumber *fortyTwoLongLong = ((NSNumber *(*)(id, SEL, long long))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithLongLong:"), 42LL);
// CHECK: NSNumber *piFloat = ((NSNumber *(*)(id, SEL, float))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithFloat:"), 3.1415927);
// CHECK: NSNumber *piDouble = ((NSNumber *(*)(id, SEL, double))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithDouble:"), 3.1415926535);
// CHECK: NSNumber *yesNumber = ((NSNumber *(*)(id, SEL, BOOL))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithBool:"), (BOOL)true);
// CHECK: NSNumber *noNumber = ((NSNumber *(*)(id, SEL, BOOL))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithBool:"), (BOOL)false);
// CHECK: NSNumber *trueNumber = ((NSNumber *(*)(id, SEL, BOOL))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithBool:"), (BOOL)true);
// CHECK: NSNumber *falseNumber = ((NSNumber *(*)(id, SEL, BOOL))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithBool:"), (BOOL)false);
// CHECK: NSNumber *yesNumber = ((NSNumber *(*)(id, SEL, BOOL))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithBool:"), true);
// CHECK: NSNumber *noNumber = ((NSNumber *(*)(id, SEL, BOOL))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithBool:"), false);
// CHECK: NSNumber *trueNumber = ((NSNumber *(*)(id, SEL, BOOL))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithBool:"), true);
// CHECK: NSNumber *falseNumber = ((NSNumber *(*)(id, SEL, BOOL))(void *)objc_msgSend)(objc_getClass("NSNumber"), sel_registerName("numberWithBool:"), false);
2 changes: 1 addition & 1 deletion clang/test/Rewriter/rewrite-modern-container-literal.mm
Expand Up @@ -5,7 +5,7 @@
void *sel_registerName(const char *);
typedef unsigned long NSUInteger;
typedef long NSInteger;
typedef signed char BOOL;
typedef bool BOOL;

@interface NSNumber
+ (NSNumber *)numberWithChar:(char)value;
Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaObjC/objc-literal-nsnumber.m
Expand Up @@ -78,8 +78,8 @@ @interface NSString<NSCopying>
}

// rdar:// 11231426
typedef float BOOL; // expected-note {{previous declaration is here}}
typedef float BOOL;

BOOL radar11231426() {
return __objc_yes; // expected-warning {{BOOL of type 'float' is non-intergal and unsuitable for a boolean literal - ignored}}
return __objc_yes;
}
2 changes: 1 addition & 1 deletion clang/test/SemaObjCXX/literals.mm
@@ -1,7 +1,7 @@
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -fblocks %s

// rdar://11231426
typedef bool BOOL;
typedef signed char BOOL;

void y(BOOL (^foo)());

Expand Down

0 comments on commit 29898f4

Please sign in to comment.