[cfe-commits] r99903 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/AST/Expr.cpp lib/Sema/SemaDeclAttr.cpp lib/Sema/SemaStmt.cpp test/SemaObjC/method-warn-unused-attribute.m
Daniel Dunbar
daniel at zuster.org
Tue Mar 30 19:19:19 PDT 2010
On Tue, Mar 30, 2010 at 11:22 AM, Fariborz Jahanian <fjahanian at apple.com> wrote:
> Author: fjahanian
> Date: Tue Mar 30 13:22:15 2010
> New Revision: 99903
>
> URL: http://llvm.org/viewvc/llvm-project?rev=99903&view=rev
> Log:
> Add Support for 'warn_unused_result" attribute on
> objective-c methods. (radar 7418262).
>
>
> Modified:
> cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> cfe/trunk/lib/AST/Expr.cpp
> cfe/trunk/lib/Sema/SemaDeclAttr.cpp
> cfe/trunk/lib/Sema/SemaStmt.cpp
> cfe/trunk/test/SemaObjC/method-warn-unused-attribute.m
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=99903&r1=99902&r2=99903&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Mar 30 13:22:15 2010
> @@ -783,8 +783,9 @@
> def warn_attribute_ignored : Warning<"%0 attribute ignored">;
> def warn_attribute_precede_definition : Warning<
> "attribute declaration must precede definition">;
> -def warn_attribute_void_function : Warning<
> - "attribute %0 cannot be applied to functions without return value">;
> +def warn_attribute_void_function_method : Warning<
> + "attribute %0 cannot be applied to "
> + "%select{functions|Objective-C method}1 without return value">;
Would this read better if it just said "method"; the Objective-Cness
is usually pretty obvious from the context. (This may apply to some
other warnings, although I think we tend to use "method" more than we
use "Objective-C method").
- Daniel
> def warn_attribute_weak_on_field : Warning<
> "__weak attribute cannot be specified on a field declaration">;
> def warn_attribute_weak_on_local : Warning<
>
> Modified: cfe/trunk/lib/AST/Expr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=99903&r1=99902&r2=99903&view=diff
> ==============================================================================
> --- cfe/trunk/lib/AST/Expr.cpp (original)
> +++ cfe/trunk/lib/AST/Expr.cpp Tue Mar 30 13:22:15 2010
> @@ -914,8 +914,15 @@
> case CXXConstructExprClass:
> return false;
>
> - case ObjCMessageExprClass:
> + case ObjCMessageExprClass: {
> + const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
> + const ObjCMethodDecl *MD = ME->getMethodDecl();
> + if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
> + Loc = getExprLoc();
> + return true;
> + }
> return false;
> + }
>
> case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
> #if 0
>
> Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=99903&r1=99902&r2=99903&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Tue Mar 30 13:22:15 2010
> @@ -837,18 +837,24 @@
> return;
> }
>
> - if (!isFunction(D)) {
> + if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
> S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
> << Attr.getName() << 0 /*function*/;
> return;
> }
>
> - if (getFunctionType(D)->getResultType()->isVoidType()) {
> - S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
> - << Attr.getName();
> + if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
> + S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
> + << Attr.getName() << 0;
> return;
> }
> -
> + if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
> + if (MD->getResultType()->isVoidType()) {
> + S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
> + << Attr.getName() << 1;
> + return;
> + }
> +
> D->addAttr(::new (S.Context) WarnUnusedResultAttr());
> }
>
>
> Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=99903&r1=99902&r2=99903&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaStmt.cpp Tue Mar 30 13:22:15 2010
> @@ -119,7 +119,13 @@
> }
> }
> }
> -
> + else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) {
> + const ObjCMethodDecl *MD = ME->getMethodDecl();
> + if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
> + Diag(Loc, diag::warn_unused_call) << R1 << R2 << "warn_unused_result";
> + return;
> + }
> + }
> Diag(Loc, DiagID) << R1 << R2;
> }
>
>
> Modified: cfe/trunk/test/SemaObjC/method-warn-unused-attribute.m
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/method-warn-unused-attribute.m?rev=99903&r1=99902&r2=99903&view=diff
> ==============================================================================
> --- cfe/trunk/test/SemaObjC/method-warn-unused-attribute.m (original)
> +++ cfe/trunk/test/SemaObjC/method-warn-unused-attribute.m Tue Mar 30 13:22:15 2010
> @@ -1,8 +1,16 @@
> // RUN: %clang_cc1 -fsyntax-only -Wunused-value -verify %s
>
> @interface INTF
> -// Currently this is rejected by both GCC and Clang (and Clang was crashing on it).
> -- (id) foo __attribute__((warn_unused_result)); // expected-warning{{warning: 'warn_unused_result' attribute only applies to function types}}
> +- (id) foo __attribute__((warn_unused_result));
> +- (void) garf __attribute__((warn_unused_result)); // expected-warning {{attribute 'warn_unused_result' cannot be applied to Objective-C method without return value}}
> +- (int) fee __attribute__((warn_unused_result));
> ++ (int) c __attribute__((warn_unused_result));
> @end
>
> +void foo(INTF *a) {
> + [a garf];
> + [a fee]; // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
> + [INTF c]; // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
> +}
> +
>
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
More information about the cfe-commits
mailing list