Skip to content

Commit db76577

Browse files
author
Fariborz Jahanian
committed
Objective-C. Provide fixit's for objc_bride_related
attributed CF to ObjC type conversions. // rdar://15499111 llvm-svn: 196935
1 parent 9653eb5 commit db76577

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6922,7 +6922,8 @@ class Sema {
69226922
bool CfToNs);
69236923

69246924
bool CheckObjCBridgeRelatedConversions(SourceLocation Loc,
6925-
QualType DestType, QualType SrcType);
6925+
QualType DestType, QualType SrcType,
6926+
Expr *SrcExpr);
69266927

69276928
bool checkInitMethod(ObjCMethodDecl *method, QualType receiverTypeIfCall);
69286929

clang/lib/Sema/SemaExpr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10640,7 +10640,7 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
1064010640
break;
1064110641
case IncompatiblePointer:
1064210642
if (getLangOpts().ObjC1 &&
10643-
CheckObjCBridgeRelatedConversions(Loc, DstType, SrcType))
10643+
CheckObjCBridgeRelatedConversions(Loc, DstType, SrcType, SrcExpr))
1064410644
return false;
1064510645
MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint, IsNSString);
1064610646
DiagKind =
@@ -10722,7 +10722,7 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
1072210722
break;
1072310723
case Incompatible:
1072410724
if (getLangOpts().ObjC1 &&
10725-
CheckObjCBridgeRelatedConversions(Loc, DstType, SrcType))
10725+
CheckObjCBridgeRelatedConversions(Loc, DstType, SrcType, SrcExpr))
1072610726
return true;
1072710727
DiagKind = diag::err_typecheck_convert_incompatible;
1072810728
ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this);

clang/lib/Sema/SemaExprObjC.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3427,7 +3427,8 @@ bool Sema::checkObjCBridgeRelatedComponents(SourceLocation Loc,
34273427

34283428
bool
34293429
Sema::CheckObjCBridgeRelatedConversions(SourceLocation Loc,
3430-
QualType DestType, QualType SrcType) {
3430+
QualType DestType, QualType SrcType,
3431+
Expr *SrcExpr) {
34313432
ARCConversionTypeClass rhsExprACTC = classifyTypeForARCConversion(SrcType);
34323433
ARCConversionTypeClass lhsExprACTC = classifyTypeForARCConversion(DestType);
34333434
bool CfToNs = (rhsExprACTC == ACTC_coreFoundation && lhsExprACTC == ACTC_retainable);
@@ -3445,9 +3446,18 @@ Sema::CheckObjCBridgeRelatedConversions(SourceLocation Loc,
34453446

34463447
if (CfToNs) {
34473448
// Implicit conversion from CF to ObjC object is needed.
3448-
if (ClassMethod)
3449+
if (ClassMethod) {
3450+
std::string ExpressionString = "[";
3451+
ExpressionString += RelatedClass->getNameAsString();
3452+
ExpressionString += " ";
3453+
ExpressionString += ClassMethod->getSelector().getAsString();
3454+
SourceLocation SrcExprEndLoc = PP.getLocForEndOfToken(SrcExpr->getLocEnd());
3455+
// Provide a fixit: [RelatedClass ClassMethod SrcExpr]
34493456
Diag(Loc, diag::err_objc_bridged_related_known_method)
3450-
<< SrcType << DestType << ClassMethod->getSelector() << 0;
3457+
<< SrcType << DestType << ClassMethod->getSelector() << 0
3458+
<< FixItHint::CreateInsertion(SrcExpr->getLocStart(), ExpressionString)
3459+
<< FixItHint::CreateInsertion(SrcExprEndLoc, "]");
3460+
}
34513461
else
34523462
Diag(Loc, diag::err_objc_bridged_related_unknown_method)
34533463
<< SrcType << DestType << 0;
@@ -3456,9 +3466,18 @@ Sema::CheckObjCBridgeRelatedConversions(SourceLocation Loc,
34563466
}
34573467
else {
34583468
// Implicit conversion from ObjC type to CF object is needed.
3459-
if (InstanceMethod)
3469+
if (InstanceMethod) {
3470+
// Provide a fixit: [ObjectExpr InstanceMethod];
3471+
std::string ExpressionString = " ";
3472+
ExpressionString += InstanceMethod->getSelector().getAsString();
3473+
ExpressionString += "]";
3474+
SourceLocation SrcExprEndLoc = PP.getLocForEndOfToken(SrcExpr->getLocEnd());
3475+
34603476
Diag(Loc, diag::err_objc_bridged_related_known_method)
3461-
<< SrcType << DestType << InstanceMethod->getSelector() << 1;
3477+
<< SrcType << DestType << InstanceMethod->getSelector() << 1
3478+
<< FixItHint::CreateInsertion(SrcExpr->getLocStart(), "[")
3479+
<< FixItHint::CreateInsertion(SrcExprEndLoc, ExpressionString);
3480+
}
34623481
else
34633482
Diag(Loc, diag::err_objc_bridged_related_unknown_method)
34643483
<< SrcType << DestType << 1;

clang/lib/Sema/SemaInit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6484,7 +6484,7 @@ bool InitializationSequence::Diagnose(Sema &S,
64846484
QualType FromType = Args[0]->getType();
64856485
if (S.getLangOpts().ObjC1)
64866486
S.CheckObjCBridgeRelatedConversions(Kind.getLocation(),
6487-
DestType, FromType);
6487+
DestType, FromType, Args[0]);
64886488
PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed)
64896489
<< (int)Entity.getKind()
64906490
<< DestType
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Objective-C recovery
2+
// RUN: not %clang_cc1 -triple x86_64-apple-darwin10 -fdiagnostics-parseable-fixits -x objective-c %s 2>&1 | FileCheck %s
3+
// RUN: not %clang_cc1 -triple x86_64-apple-darwin10 -fobjc-arc -fdiagnostics-parseable-fixits -x objective-c %s 2>&1 | FileCheck %s
4+
// RUN: not %clang_cc1 -triple x86_64-apple-darwin10 -fdiagnostics-parseable-fixits -x objective-c++ %s 2>&1 | FileCheck %s
5+
// rdar://15499111
6+
7+
typedef struct __attribute__((objc_bridge_related(NSColor,colorWithCGColor:,CGColor))) CGColor *CGColorRef;
8+
9+
@interface NSColor
10+
+ (NSColor *)colorWithCGColor:(CGColorRef)cgColor;
11+
- (CGColorRef)CGColor;
12+
@end
13+
14+
@interface NSTextField
15+
- (void)setBackgroundColor:(NSColor *)color;
16+
- (NSColor *)backgroundColor;
17+
@end
18+
19+
NSColor * Test1(NSTextField *textField, CGColorRef newColor) {
20+
textField.backgroundColor = newColor;
21+
return newColor;
22+
}
23+
24+
CGColorRef Test2(NSTextField *textField, CGColorRef newColor) {
25+
newColor = textField.backgroundColor; // [textField.backgroundColor CGColor]
26+
return textField.backgroundColor;
27+
}
28+
// CHECK: {20:30-20:30}:"[NSColor colorWithCGColor:"
29+
// CHECK: {20:38-20:38}:"]"
30+
// CHECK: {21:9-21:9}:"[NSColor colorWithCGColor:"
31+
// CHECK: {21:17-21:17}:"]"
32+
// CHECK: {25:13-25:13}:"["
33+
// CHECK: {25:38-25:38}:" CGColor]"
34+
// CHECK: {26:9-26:9}:"["
35+
// CHECK: {26:34-26:34}:" CGColor]"

0 commit comments

Comments
 (0)