From patchwork Tue Jan 5 08:43:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 562992 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id C4079140271 for ; Tue, 5 Jan 2016 19:43:21 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=QMKYAG5J; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; q=dns; s=default; b=wGnptwtdEqU/ NpTkng+fAugAaYuL+VLEn/bA4LiPFhViM2Me0FKyhkHqPCWtpeUxSn5LQACakuYV +K7gE6vIrb2zq8daSE23sVH+FPqmrE0ntI9MFT0b6jGi3T1ixk8y8cVJ1bMifv3W O/GXza0rR3onqCUVsStP/ggIHfQlOcY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; s=default; bh=+qKopkmbry0qRNoJin Zse1/rGVA=; b=QMKYAG5JCVmZbPf00HiIWu5mSSOLrYqLcHNoakxvv1VIbdNlMy KbxaHZiYh5pa9HCNej7+MbcbkSG8q98CLOaQa1u7KJZaP1nRkE0pw2BsBQx+V7yB J/aOynbSXU0oX1xwU1JCsrFgIE8sTqRe+gZy3lCVhJ3HUkzZv27zbNqwo= Received: (qmail 40972 invoked by alias); 5 Jan 2016 08:43:14 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 40958 invoked by uid 89); 5 Jan 2016 08:43:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.3 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=no version=3.3.2 spammy=cindentationc, UD:c-indentation.c, c-indentation.c, complain X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 05 Jan 2016 08:43:12 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 53C1C19EE03 for ; Tue, 5 Jan 2016 08:43:11 +0000 (UTC) Received: from surprise.redhat.com (vpn-224-195.phx2.redhat.com [10.3.224.195]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u058hAoR023907; Tue, 5 Jan 2016 03:43:10 -0500 From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH] Fix PR c/69122 (-Wmisleading-indentation false positive with empty macros) Date: Tue, 5 Jan 2016 03:43:02 -0500 Message-Id: <1451983382-12886-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes Successfully bootstrapped®rtested on x86_64-pc-linux-gnu. OK for trunk? gcc/c-family/ChangeLog: PR c/69122 * c-indentation.c (get_visual_column): Remove default argument. (should_warn_for_misleading_indentation): For the multiline case, update call to get_visual_column for next_stmt_exploc so that it captures the location of the first non-whitespace character in the relevant line. Don't issue warnings if there is non-whitespace before the next statement. gcc/testsuite/ChangeLog: PR c/69122 * c-c++-common/Wmisleading-indentation.c (pr69122): New function. --- gcc/c-family/c-indentation.c | 24 ++++++++++++++++++++-- .../c-c++-common/Wmisleading-indentation.c | 10 +++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/gcc/c-family/c-indentation.c b/gcc/c-family/c-indentation.c index 2f857f7..3c09336 100644 --- a/gcc/c-family/c-indentation.c +++ b/gcc/c-family/c-indentation.c @@ -38,7 +38,7 @@ extern cpp_options *cpp_opts; static bool get_visual_column (expanded_location exploc, unsigned int *out, - unsigned int *first_nws = NULL) + unsigned int *first_nws) { int line_len; const char *line = location_get_source_line (exploc.file, exploc.line, @@ -329,12 +329,20 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo, ; foo (); ^ DON'T WARN HERE + + #define emit + if (flag) + foo (); + emit bar (); + ^ DON'T WARN HERE + */ if (next_stmt_exploc.line > body_exploc.line) { /* Determine if GUARD_LOC and NEXT_STMT_LOC are aligned on the same "visual column"... */ unsigned int next_stmt_vis_column; + unsigned int next_stmt_line_first_nws; unsigned int body_vis_column; unsigned int body_line_first_nws; unsigned int guard_vis_column; @@ -343,7 +351,8 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo, the case for input files containing #line directives, and these are often for autogenerated sources (e.g. from .md files), where it's not clear that it's meaningful to look at indentation. */ - if (!get_visual_column (next_stmt_exploc, &next_stmt_vis_column)) + if (!get_visual_column (next_stmt_exploc, &next_stmt_vis_column, + &next_stmt_line_first_nws)) return false; if (!get_visual_column (body_exploc, &body_vis_column, @@ -354,6 +363,17 @@ should_warn_for_misleading_indentation (const token_indent_info &guard_tinfo, &guard_line_first_nws)) return false; + /* If the line where the next stmt starts has non-whitespace + on it before the stmt, then don't warn: + #define emit + if (flag) + foo (); + emit bar (); + ^ DON'T WARN HERE + (PR c/69122). */ + if (next_stmt_line_first_nws < next_stmt_vis_column) + return false; + if ((body_type != CPP_SEMICOLON && next_stmt_vis_column == body_vis_column) /* As a special case handle the case where the body is a semicolon diff --git a/gcc/testsuite/c-c++-common/Wmisleading-indentation.c b/gcc/testsuite/c-c++-common/Wmisleading-indentation.c index a3f5acd..491291c 100644 --- a/gcc/testsuite/c-c++-common/Wmisleading-indentation.c +++ b/gcc/testsuite/c-c++-common/Wmisleading-indentation.c @@ -891,3 +891,13 @@ fn_39 (void) i++); foo (i); } + +/* We shouldn't complain about the following function. */ +#define emit +void pr69122 (void) +{ + if (flagA) + foo (0); + emit foo (1); +} +#undef emit