[cfe-commits] r152047 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/block-missing-return-type-warning.c test/Sema/warn-unused-parameters.c
Douglas Gregor
dgregor at apple.com
Mon Mar 5 20:10:12 PST 2012
On Mar 5, 2012, at 11:34 AM, Fariborz Jahanian wrote:
> Author: fjahanian
> Date: Mon Mar 5 13:34:00 2012
> New Revision: 152047
>
> URL: http://llvm.org/viewvc/llvm-project?rev=152047&view=rev
> Log:
> patch to optionally warn for block implementations without explicit
> return types that return non-void values. // rdar://10735698
This should not be a warning in Clang. It's an opt-in warning (which is a huge red flag) that is more about style than about finding real problems: someone got confused about the types of some C expressions (== returns an int in C, despite there being a _Bool in C99 and a BOOL in Objective-C) which resulted in a mismatch with the inferred return type of a block. The right solution to this problem is to improve the error message we produce when the mismatch occurs, e.g., by adding a note that points where the "-> return type" should be added, so the user knows exactly how to fix the problem.
- Doug
> Added:
> cfe/trunk/test/Sema/block-missing-return-type-warning.c
> Modified:
> cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> cfe/trunk/lib/Sema/SemaExpr.cpp
> cfe/trunk/test/Sema/warn-unused-parameters.c
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=152047&r1=152046&r2=152047&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Mar 5 13:34:00 2012
> @@ -4953,6 +4953,10 @@
> " or pick a deployment target that supports them">;
> def err_block_returning_array_function : Error<
> "block cannot return %select{array|function}0 type %1">;
> +def warn_block_missing_return_type : Warning<
> + "block literal is missing explicit return type and "
> + "returns non-void values">,
> + InGroup<DiagGroup<"block-missing-explicit-return-type">>, DefaultIgnore;
>
> // Builtin annotation string.
> def err_builtin_annotation_not_string_constant : Error<
>
> Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=152047&r1=152046&r2=152047&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Mar 5 13:34:00 2012
> @@ -8902,7 +8902,12 @@
> ExprCleanupObjects.push_back(Result->getBlockDecl());
> ExprNeedsCleanups = true;
> }
> -
> +
> + if (BSI->TheDecl->blockMissingReturnType() &&
> + !RetTy->isDependentType() &&
> + !Context.getCanonicalType(RetTy)->isVoidType())
> + Diag(CaretLoc, diag::warn_block_missing_return_type);
> +
> return Owned(Result);
> }
>
>
> Added: cfe/trunk/test/Sema/block-missing-return-type-warning.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/block-missing-return-type-warning.c?rev=152047&view=auto
> ==============================================================================
> --- cfe/trunk/test/Sema/block-missing-return-type-warning.c (added)
> +++ cfe/trunk/test/Sema/block-missing-return-type-warning.c Mon Mar 5 13:34:00 2012
> @@ -0,0 +1,18 @@
> +// RUN: %clang_cc1 %s -fsyntax-only -verify -fblocks -Wblock-missing-explicit-return-type
> +// rdar://10735698
> +
> +int f;
> +int main() {
> + int (^bar)() = ^{ if (f) return 'a'; // expected-warning {{block literal is missing explicit return type and returns non-void values}}
> + else return 10;
> + };
> +
> + void (^bar1)() = ^{ f = 100; };
> +
> + void (^bar2)() = ^(void){ f = 100; };
> +
> + int (^bar3)() = ^ int { if (f) return 'a';
> + else return 10;
> + };
> +
> +}
>
> Modified: cfe/trunk/test/Sema/warn-unused-parameters.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unused-parameters.c?rev=152047&r1=152046&r2=152047&view=diff
> ==============================================================================
> --- cfe/trunk/test/Sema/warn-unused-parameters.c (original)
> +++ cfe/trunk/test/Sema/warn-unused-parameters.c Mon Mar 5 13:34:00 2012
> @@ -24,7 +24,7 @@
> // RUN: %clang_cc1 -fblocks -fsyntax-only -Weverything %s 2>&1 | FileCheck -check-prefix=CHECK-everything %s
> // RUN: %clang_cc1 -fblocks -fsyntax-only -Weverything -Werror %s 2>&1 | FileCheck -check-prefix=CHECK-everything-error %s
> // RUN: %clang_cc1 -fblocks -fsyntax-only -Weverything -Wno-unused %s 2>&1 | FileCheck -check-prefix=CHECK-everything-no-unused %s
> -// CHECK-everything: 6 warnings generated
> -// CHECK-everything-error: 5 errors generated
> -// CHECK-everything-no-unused: 5 warnings generated
> +// CHECK-everything: 7 warnings generated
> +// CHECK-everything-error: 6 errors generated
> +// CHECK-everything-no-unused: 6 warnings generated
>
>
>
> _______________________________________________
> 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