r318456 - Issue -Wempty-body warnings for else blocks

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 16 15:32:11 PST 2017


This is kicking up false positives on code that does the following:

#define USED(x) if(x);else
// ...
assert(x);
USED(x);

It's a bit of a weird pattern, but it occurs in some open-source code and
seems easy enough for us to detect (if the semicolon and the preceding
token aren't from the same FileID, we shouldn't be diagnosing, because they
aren't actually adjacent).

On 16 November 2017 at 13:26, Reid Kleckner via cfe-commits <
cfe-commits at lists.llvm.org> wrote:

> Author: rnk
> Date: Thu Nov 16 13:26:18 2017
> New Revision: 318456
>
> URL: http://llvm.org/viewvc/llvm-project?rev=318456&view=rev
> Log:
> Issue -Wempty-body warnings for else blocks
>
> This looks like it was just an oversight.
>
> Fixes http://llvm.org/pr35319
>
> Modified:
>     cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>     cfe/trunk/lib/Sema/SemaStmt.cpp
>     cfe/trunk/test/SemaCXX/warn-empty-body.cpp
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/
> DiagnosticSemaKinds.td?rev=318456&r1=318455&r2=318456&view=diff
> ============================================================
> ==================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Nov 16
> 13:26:18 2017
> @@ -8090,6 +8090,8 @@ def err_switch_incomplete_class_type : E
>
>  def warn_empty_if_body : Warning<
>    "if statement has empty body">, InGroup<EmptyBody>;
> +def warn_empty_else_body : Warning<
> +  "else clause has empty body">, InGroup<EmptyBody>;
>  def warn_empty_for_body : Warning<
>    "for loop has empty body">, InGroup<EmptyBody>;
>  def warn_empty_range_based_for_body : Warning<
>
> Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/
> SemaStmt.cpp?rev=318456&r1=318455&r2=318456&view=diff
> ============================================================
> ==================
> --- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaStmt.cpp Thu Nov 16 13:26:18 2017
> @@ -527,7 +527,9 @@ Sema::ActOnIfStmt(SourceLocation IfLoc,
>                         CondExpr->getExprLoc()))
>      CommaVisitor(*this).Visit(CondExpr);
>
> -  if (!elseStmt)
> +  if (elseStmt)
> +    DiagnoseEmptyStmtBody(ElseLoc, elseStmt, diag::warn_empty_else_body);
> +  else
>      DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), thenStmt,
>                            diag::warn_empty_if_body);
>
>
> Modified: cfe/trunk/test/SemaCXX/warn-empty-body.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/
> SemaCXX/warn-empty-body.cpp?rev=318456&r1=318455&r2=318456&view=diff
> ============================================================
> ==================
> --- cfe/trunk/test/SemaCXX/warn-empty-body.cpp (original)
> +++ cfe/trunk/test/SemaCXX/warn-empty-body.cpp Thu Nov 16 13:26:18 2017
> @@ -238,6 +238,26 @@ void test6(int x, int y) {
>    }
>  }
>
> +void test_if_else(int x) {
> +  if (x); // expected-warning{{if statement has empty body}}
> expected-note{{separate line}}
> +
> +  if (x)
> +    ; // no-warning
> +
> +  if (x)
> +    ; // no-warning
> +  else
> +    ; // no-warning
> +
> +  if (x)
> +    ; // no-warning
> +  else; // expected-warning{{else clause has empty body}}
> expected-note{{separate line}}
> +
> +  if (x)
> +    ; // no-warning
> +  else EMPTY(x); // no-warning
> +}
> +
>  void test_errors(int x) {
>    if (1)
>      aa; // expected-error{{use of undeclared identifier}}
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171116/739949fb/attachment.html>


More information about the cfe-commits mailing list