[PATCH] NoReturn Warning: Removed Warning for Unreachable Return
Aaron Ballman
aaron at aaronballman.com
Fri Jan 3 11:49:30 PST 2014
On Fri, Jan 3, 2014 at 2:18 PM, Michael Bao <mike.h.bao at gmail.com> wrote:
> From Bug 10642 (http://llvm.org/bugs/show_bug.cgi?id=10642).
>
> If a return statement is unreachable, do not emit the warning stating that the function that is declared 'noreturn' should not return.
>
> http://llvm-reviews.chandlerc.com/D2508
>
> Files:
> lib/Sema/SemaStmt.cpp
> test/Sema/return-noreturn.c
>
> Index: lib/Sema/SemaStmt.cpp
> ===================================================================
> --- lib/Sema/SemaStmt.cpp
> +++ lib/Sema/SemaStmt.cpp
> @@ -2776,9 +2776,6 @@
> QualType RelatedRetType;
> if (const FunctionDecl *FD = getCurFunctionDecl()) {
> FnRetType = FD->getResultType();
> - if (FD->isNoReturn())
> - Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
> - << FD->getDeclName();
> } else if (ObjCMethodDecl *MD = getCurMethodDecl()) {
> FnRetType = MD->getResultType();
> if (MD->hasRelatedResultType() && MD->getClassInterface()) {
> @@ -2953,6 +2950,12 @@
> !CurContext->isDependentContext())
> FunctionScopes.back()->Returns.push_back(Result);
>
> + if (const FunctionDecl *FD = getCurFunctionDecl()) {
> + if (FD->isNoReturn())
> + DiagRuntimeBehavior(ReturnLoc, Result,
> + PDiag(diag::warn_noreturn_function_has_return_expr) << FD->getDeclName());
No need to call getDeclName() here, can just pass in FD directly.
Also, why has this code moved ~200 lines further down? I think this is
changing the semantics of the diagnostic.
> + }
> +
> return Owned(Result);
> }
>
> Index: test/Sema/return-noreturn.c
> ===================================================================
> --- test/Sema/return-noreturn.c
> +++ test/Sema/return-noreturn.c
> @@ -1,4 +1,4 @@
> -// RUN: %clang_cc1 %s -fsyntax-only -verify -fblocks -Wmissing-noreturn -Wno-unreachable-code
> +// RUN: %clang_cc1 %s -fsyntax-only -verify -fblocks -Wmissing-noreturn -Wno-unreachable-code -Winvalid-noreturn
>
> int j;
> void test1() { // expected-warning {{function 'test1' could be declared with attribute 'noreturn'}}
> @@ -40,3 +40,11 @@
> _Noreturn void test5() {
> test2_positive();
> }
> +
> +// Should not warn about an invalid return even if the function is
> +// marked 'noreturn' and a return statement is present
> +// in the case that the return statement is unreachable.
> +__attribute__((noreturn)) void test6() {
> + while (1) {}
> + return;
> +}
~Aaron
More information about the cfe-commits
mailing list