diff options
author | Fariborz Jahanian <fjahanian@apple.com> | 2013-06-10 23:51:51 +0000 |
---|---|---|
committer | Fariborz Jahanian <fjahanian@apple.com> | 2013-06-10 23:51:51 +0000 |
commit | 286fcf67979f905e3932fb5779a7a9b5ea24b9fc (patch) | |
tree | 1216aff7675a54d99b3ba6713ef5143168ca8547 | |
parent | 536a10cc2d8a64eea4761234ea696f0e7fc697db (diff) | |
download | bcm5719-llvm-286fcf67979f905e3932fb5779a7a9b5ea24b9fc.tar.gz bcm5719-llvm-286fcf67979f905e3932fb5779a7a9b5ea24b9fc.zip |
Objective-C [qoi]: Issue better warning when nsstring literal is missing
the '@'. PR16287 and // rdar://14106083
llvm-svn: 183713
-rw-r--r-- | clang/include/clang/Basic/DiagnosticGroups.td | 4 | ||||
-rw-r--r-- | clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 9 | ||||
-rw-r--r-- | clang/test/FixIt/fixit-objc.m | 8 |
4 files changed, 16 insertions, 7 deletions
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index 99b342454c4..5d9b9c93e68 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -539,8 +539,10 @@ def ObjCCocoaAPI : DiagGroup<"objc-cocoa-api", [ ]>; def ObjCStringComparison : DiagGroup<"objc-string-compare">; +def ObjCLiteralMissingAtSign : DiagGroup<"objc-literal-missing-atsign">; def ObjCLiteralComparison : DiagGroup<"objc-literal-compare", [ - ObjCStringComparison + ObjCStringComparison, + ObjCLiteralMissingAtSign ]>; // Inline ASM warnings. diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index b9c0518dece..c476ac906ee 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -1879,6 +1879,8 @@ def warn_objc_literal_comparison : Warning< "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 warn_objc_string_literal_comparison : Warning< "direct comparison of a string literal has undefined behavior">, InGroup<ObjCStringComparison>; diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index fcfe17a007b..66598992d7f 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -10195,7 +10195,8 @@ ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { } static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, - Expr *SrcExpr, FixItHint &Hint) { + Expr *SrcExpr, FixItHint &Hint, + bool &IsNSString) { if (!SemaRef.getLangOpts().ObjC1) return; @@ -10209,6 +10210,7 @@ static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); if (!ID || !ID->getIdentifier()->isStr("NSString")) return; + IsNSString = true; } // Ignore any parens, implicit casts (should only be @@ -10242,6 +10244,7 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, ConversionFixItGenerator ConvHints; bool MayHaveConvFixit = false; bool MayHaveFunctionDiff = false; + bool IsNSString = false; switch (ConvTy) { case Compatible: @@ -10259,7 +10262,7 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, MayHaveConvFixit = true; break; case IncompatiblePointer: - MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); + MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint, IsNSString); DiagKind = diag::ext_typecheck_convert_incompatible_pointer; CheckInferredResultType = DstType->isObjCObjectPointerType() && SrcType->isObjCObjectPointerType(); @@ -10270,6 +10273,8 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, SrcType = SrcType.getUnqualifiedType(); DstType = DstType.getUnqualifiedType(); } + else if (IsNSString && !Hint.isNull()) + DiagKind = diag::warn_missing_atsign_prefix; MayHaveConvFixit = true; break; case IncompatiblePointerSign: diff --git a/clang/test/FixIt/fixit-objc.m b/clang/test/FixIt/fixit-objc.m index ea57fe671b9..7c4776ae71e 100644 --- a/clang/test/FixIt/fixit-objc.m +++ b/clang/test/FixIt/fixit-objc.m @@ -27,13 +27,13 @@ void g(NSString *a); // expected-note{{passing argument to parameter 'a' here}} void h(id a); // expected-note 2{{passing argument to parameter 'a' here}} void f(Test *t) { - NSString *a = "Foo"; // expected-warning {{incompatible pointer types initializing 'NSString *' with an expression of type 'char [4]'}} + NSString *a = "Foo"; // expected-warning {{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{{incompatible pointer types passing 'char [4]' to parameter of type 'NSString *'}} + g("Foo"); // expected-warning {{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{{incompatible pointer types sending 'char [4]' to parameter of type 'NSString *'}} - t.property = "Foo"; // expected-warning{{incompatible pointer types assigning to 'NSString *' from 'char [4]'}} + [t test:"Foo"]; // expected-warning {{string literal must be prefixed by '@'}} + t.property = "Foo"; // expected-warning {{string literal must be prefixed by '@'}} // <rdar://problem/6896493> [t test:@"Foo"]]; // expected-error{{extraneous ']' before ';'}} |