r197515 - Objective-C. Make diagnostics and fix-its consistent
Fariborz Jahanian
fjahanian at apple.com
Tue Dec 17 11:33:44 PST 2013
Author: fjahanian
Date: Tue Dec 17 13:33:43 2013
New Revision: 197515
URL: http://llvm.org/viewvc/llvm-project?rev=197515&view=rev
Log:
Objective-C. Make diagnostics and fix-its consistent
when diagnosing casting of a cstring literal to
NSString in default and -fobjc-arc mode.
// rdar://14106083
Added:
cfe/trunk/test/FixIt/fixit-objc-arc.m
Modified:
cfe/trunk/include/clang/Basic/DiagnosticGroups.td
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/include/clang/Sema/Sema.h
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/lib/Sema/SemaExprObjC.cpp
cfe/trunk/test/FixIt/fixit-objc.m
Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=197515&r1=197514&r2=197515&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Dec 17 13:33:43 2013
@@ -609,8 +609,6 @@ def ObjCLiteralComparison : DiagGroup<"o
ObjCStringComparison
]>;
-def ObjCLiteralMissingAtSign : DiagGroup<"objc-literal-missing-atsign">;
-
// Inline ASM warnings.
def ASMOperandWidths : DiagGroup<"asm-operand-widths">;
def ASM : DiagGroup<"asm", [
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=197515&r1=197514&r2=197515&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Dec 17 13:33:43 2013
@@ -1911,8 +1911,8 @@ def warn_objc_literal_comparison : Warni
"direct comparison of %select{an array literal|a dictionary literal|"
"a numeric literal|a boxed expression|}0 has undefined behavior">,
InGroup<ObjCLiteralComparison>;
-def warn_missing_atsign_prefix : Warning<
- "string literal must be prefixed by '@' ">, InGroup<ObjCLiteralMissingAtSign>;
+def err_missing_atsign_prefix : Error<
+ "string literal must be prefixed by '@' ">;
def warn_objc_string_literal_comparison : Warning<
"direct comparison of a string literal has undefined behavior">,
InGroup<ObjCStringComparison>;
Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=197515&r1=197514&r2=197515&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Tue Dec 17 13:33:43 2013
@@ -6900,6 +6900,10 @@ public:
QualType DestType, QualType SrcType,
Expr *&SrcExpr);
+ StringLiteral * ConversionToObjCStringLiteralCheck(QualType DstType,
+ Expr *SrcExpr, FixItHint &Hint,
+ bool &IsNSString);
+
bool checkInitMethod(ObjCMethodDecl *method, QualType receiverTypeIfCall);
/// \brief Check whether the given new method is a valid override of the
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=197515&r1=197514&r2=197515&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Dec 17 13:33:43 2013
@@ -10587,22 +10587,23 @@ ExprResult Sema::ActOnGNUNullExpr(Source
return Owned(new (Context) GNUNullExpr(Ty, TokenLoc));
}
-static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType,
- Expr *SrcExpr, FixItHint &Hint,
- bool &IsNSString) {
- if (!SemaRef.getLangOpts().ObjC1)
- return;
+StringLiteral *
+Sema::ConversionToObjCStringLiteralCheck(QualType DstType,
+ Expr *SrcExpr, FixItHint &Hint,
+ bool &IsNSString) {
+ if (!getLangOpts().ObjC1)
+ return 0;
const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>();
if (!PT)
- return;
+ return 0;
// Check if the destination is of type 'id'.
if (!PT->isObjCIdType()) {
// Check if the destination is the 'NSString' interface.
const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
if (!ID || !ID->getIdentifier()->isStr("NSString"))
- return;
+ return 0;
IsNSString = true;
}
@@ -10616,9 +10617,9 @@ static void MakeObjCStringLiteralFixItHi
StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr);
if (!SL || !SL->isAscii())
- return;
-
+ return 0;
Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@");
+ return SL;
}
bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
@@ -10655,7 +10656,7 @@ bool Sema::DiagnoseAssignmentResult(Assi
MayHaveConvFixit = true;
break;
case IncompatiblePointer:
- MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint, IsNSString);
+ ConversionToObjCStringLiteralCheck(DstType, SrcExpr, Hint, IsNSString);
DiagKind =
(Action == AA_Passing_CFAudited ?
diag::err_arc_typecheck_convert_incompatible_pointer :
@@ -10670,7 +10671,7 @@ bool Sema::DiagnoseAssignmentResult(Assi
DstType = DstType.getUnqualifiedType();
}
else if (IsNSString && !Hint.isNull())
- DiagKind = diag::warn_missing_atsign_prefix;
+ DiagKind = diag::err_missing_atsign_prefix;
MayHaveConvFixit = true;
break;
case IncompatiblePointerSign:
Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=197515&r1=197514&r2=197515&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Tue Dec 17 13:33:43 2013
@@ -3612,6 +3612,17 @@ Sema::CheckObjCARCConversion(SourceRange
CCK != CCK_ImplicitConversion)
return ACR_unbridged;
+ // Do not issue bridge cast" diagnostic when implicit casting a cstring
+ // to 'NSString *'. Let caller issue a normal mismatched diagnostic with
+ // suitable fix-it.
+ if (castACTC == ACTC_retainable && exprACTC == ACTC_none) {
+ bool IsNSString = false;
+ FixItHint Hint;
+ if (ConversionToObjCStringLiteralCheck(
+ castType, castExpr, Hint, IsNSString) && IsNSString)
+ return ACR_okay;
+ }
+
// Do not issue "bridge cast" diagnostic when implicit casting
// a retainable object to a CF type parameter belonging to an audited
// CF API function. Let caller issue a normal type mismatched diagnostic
Added: cfe/trunk/test/FixIt/fixit-objc-arc.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/FixIt/fixit-objc-arc.m?rev=197515&view=auto
==============================================================================
--- cfe/trunk/test/FixIt/fixit-objc-arc.m (added)
+++ cfe/trunk/test/FixIt/fixit-objc-arc.m Tue Dec 17 13:33:43 2013
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -pedantic -verify %s
+// RUN: cp %s %t
+// RUN: not %clang_cc1 -pedantic -fobjc-arc -fixit -x objective-c %t
+// RUN: %clang_cc1 -pedantic -fobjc-arc -Werror -x objective-c %t
+// rdar://14106083
+
+ at class A;
+ at class NSString;
+
+ at interface Test
+- (void)test:(NSString *)string; // expected-note{{passing argument to parameter 'string' here}}
+
+ at property (copy) NSString *property;
+ at end
+
+void g(NSString *a); // expected-note{{passing argument to parameter 'a' here}}
+void h(id a);
+
+void f(Test *t) {
+ NSString *a = "Foo"; // expected-error {{string literal must be prefixed by '@'}}
+ g("Foo"); // expected-error {{string literal must be prefixed by '@'}}
+ [t test:"Foo"]; // expected-error {{string literal must be prefixed by '@'}}
+ t.property = "Foo"; // expected-error {{string literal must be prefixed by '@'}}
+}
Modified: cfe/trunk/test/FixIt/fixit-objc.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/FixIt/fixit-objc.m?rev=197515&r1=197514&r2=197515&view=diff
==============================================================================
--- cfe/trunk/test/FixIt/fixit-objc.m (original)
+++ cfe/trunk/test/FixIt/fixit-objc.m Tue Dec 17 13:33:43 2013
@@ -27,13 +27,13 @@ void g(NSString *a); // expected-note{{p
void h(id a); // expected-note 2{{passing argument to parameter 'a' here}}
void f(Test *t) {
- NSString *a = "Foo"; // expected-warning {{string literal must be prefixed by '@'}}
+ NSString *a = "Foo"; // expected-error {{string literal must be prefixed by '@'}}
id b = "Foo"; // expected-warning {{incompatible pointer types initializing 'id' with an expression of type 'char [4]'}}
- g("Foo"); // expected-warning {{string literal must be prefixed by '@'}}
+ g("Foo"); // expected-error {{string literal must be prefixed by '@'}}
h("Foo"); // expected-warning{{incompatible pointer types passing 'char [4]' to parameter of type 'id'}}
h(("Foo")); // expected-warning{{incompatible pointer types passing 'char [4]' to parameter of type 'id'}}
- [t test:"Foo"]; // expected-warning {{string literal must be prefixed by '@'}}
- t.property = "Foo"; // expected-warning {{string literal must be prefixed by '@'}}
+ [t test:"Foo"]; // expected-error {{string literal must be prefixed by '@'}}
+ t.property = "Foo"; // expected-error {{string literal must be prefixed by '@'}}
// <rdar://problem/6896493>
[t test:@"Foo"]]; // expected-error{{extraneous ']' before ';'}}
More information about the cfe-commits
mailing list