From patchwork Mon Aug 27 14:34:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 962518 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-484501-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="JeQMlQ1z"; dkim-atps=neutral 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 41zYB02Y0Lz9s1c for ; Mon, 27 Aug 2018 23:48:06 +1000 (AEST) 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=oqSjxQsh6Qv1 cL98xdElhD6cMnp6bYSMB08+vP4Q6/IQagDaxn4kKFAMqf6zeG8n5rFGgW5jmYKX YL6HVK5tXqhdCbrTcpHctob2tuRKS28kovihtzA9a8gjK58puBToMOV9F8LVARna 0PLPd3yHEdXr8aU6COCAf9sAulN0u1A= 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=6jUZ+8rLPBHSLimE7g JDwrd0sUI=; b=JeQMlQ1zWjph0FP4ywHa1e/2MTa7RP0sWFmAPP1jxwC80WcNIx EiKouSAyAfhCWyzFpMgnj3FnOoYeeEDIVoWmLNrbE4ViBDI6F5NVSQFTQFzBNFNu oAdTTZvl/WnPI8yryLroBPmUTSK7fID3a1rTxttrf0JHnPosZsyHE8DvA= Received: (qmail 88814 invoked by alias); 27 Aug 2018 13:47:59 -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 88804 invoked by uid 89); 27 Aug 2018 13:47:58 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=32m, zi, rich_location, sk:test_di 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 ESMTP; Mon, 27 Aug 2018 13:47:56 +0000 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9639B308AA13 for ; Mon, 27 Aug 2018 13:47:55 +0000 (UTC) Received: from c64.redhat.com (ovpn-112-25.phx2.redhat.com [10.3.112.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9710B5B682; Mon, 27 Aug 2018 13:47:54 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [committed] diagnostics: show an extra line of context in line-insertion fix-it hints (PR 87091) Date: Mon, 27 Aug 2018 10:34:11 -0400 Message-Id: <1535380451-37896-1-git-send-email-dmalcolm@redhat.com> X-IsSubscribed: yes This patch tweaks how we print line-insertion fix-it hints, so that the line before the insertion point is also printed, to give the user more context on the proposed change. For example, it changes: ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22: note: message +++ |+#include 74 | #endif .... 87 | using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>; | ^~~ to: ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:87:22: note: message 73 | # include +++ |+#include 74 | #endif .... 87 | using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>; | ^~~ Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Committed to trunk as r263884. gcc/ChangeLog: PR 87091 * diagnostic-show-locus.c (get_line_span_for_fixit_hint): Show the line above for line-insertion fix-it hints. (selftest::test_fixit_insert_containing_newline): Update the expected results, and add a test with line-numbering enabled. gcc/testsuite/ChangeLog: PR 87091 * g++.dg/pr85523.C: Extend expected output to show line before line-insertion fix-it hint. * gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c (test_fixit_insert_newline): Add previous line to expected output. * gcc.dg/plugin/diagnostic-test-show-locus-bw.c: Likewise. * gcc.dg/plugin/diagnostic-test-show-locus-color.c: Likewise. --- gcc/diagnostic-show-locus.c | 43 ++++++++++++++++++---- gcc/testsuite/g++.dg/pr85523.C | 3 ++ .../diagnostic-test-show-locus-bw-line-numbers.c | 1 + .../gcc.dg/plugin/diagnostic-test-show-locus-bw.c | 1 + .../plugin/diagnostic-test-show-locus-color.c | 1 + 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/gcc/diagnostic-show-locus.c b/gcc/diagnostic-show-locus.c index 1e7f969..d305ada 100644 --- a/gcc/diagnostic-show-locus.c +++ b/gcc/diagnostic-show-locus.c @@ -1159,7 +1159,16 @@ static line_span get_line_span_for_fixit_hint (const fixit_hint *hint) { gcc_assert (hint); - return line_span (LOCATION_LINE (hint->get_start_loc ()), + + int start_line = LOCATION_LINE (hint->get_start_loc ()); + + /* For line-insertion fix-it hints, add the previous line to the + span, to give the user more context on the proposed change. */ + if (hint->ends_with_newline_p ()) + if (start_line > 1) + start_line--; + + return line_span (start_line, LOCATION_LINE (hint->get_next_loc ())); } @@ -3479,13 +3488,31 @@ test_fixit_insert_containing_newline (const line_table_case &case_) { rich_location richloc (line_table, case_loc); richloc.add_fixit_insert_before (line_start, " break;\n"); - test_diagnostic_context dc; - diagnostic_show_locus (&dc, &richloc, DK_ERROR); - ASSERT_STREQ ("\n" - "+ break;\n" - " case 'b':\n" - " ^~~~~~~~~\n", - pp_formatted_text (dc.printer)); + + /* Without line numbers. */ + { + test_diagnostic_context dc; + diagnostic_show_locus (&dc, &richloc, DK_ERROR); + ASSERT_STREQ ("\n" + " x = a;\n" + "+ break;\n" + " case 'b':\n" + " ^~~~~~~~~\n", + pp_formatted_text (dc.printer)); + } + + /* With line numbers. */ + { + test_diagnostic_context dc; + dc.show_line_numbers_p = true; + diagnostic_show_locus (&dc, &richloc, DK_ERROR); + ASSERT_STREQ ("\n" + "2 | x = a;\n" + "+ |+ break;\n" + "3 | case 'b':\n" + " | ^~~~~~~~~\n", + pp_formatted_text (dc.printer)); + } } /* Verify that attempts to add text with a newline fail when the diff --git a/gcc/testsuite/g++.dg/pr85523.C b/gcc/testsuite/g++.dg/pr85523.C index 9cd939b..0ed16ff 100644 --- a/gcc/testsuite/g++.dg/pr85523.C +++ b/gcc/testsuite/g++.dg/pr85523.C @@ -47,6 +47,7 @@ struct s5 { i = z.i; } // { dg-warning "no return statement in function returning non-void" } /* { dg-begin-multiline-output "" } + i = z.i; + return *this; } ^ @@ -63,6 +64,7 @@ struct s6 { i = z.i; } // { dg-warning "no return statement in function returning non-void" } /* { dg-begin-multiline-output "" } + i = z.i; + return *this; } ^ @@ -81,6 +83,7 @@ struct s7 { i = z.i; } // { dg-warning "no return statement in function returning non-void" } /* { dg-begin-multiline-output "" } + i = z.i; + return *this; } ^ diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c index f2bbc58..63e5855 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw-line-numbers.c @@ -111,6 +111,7 @@ void test_fixit_insert_newline (void) x = b; } /* { dg-begin-multiline-output "" } +109 | x = a; +++ |+ break; 110 | case 'b': | ^~~~~~~~ diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c index bdfa420..be6f103 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-bw.c @@ -332,6 +332,7 @@ void test_fixit_insert_newline (void) x = b; } /* { dg-begin-multiline-output "" } + x = a; + break; case 'b': ^~~~~~~~ diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c index 094bc65..7ae3801 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-show-locus-color.c @@ -217,6 +217,7 @@ void test_fixit_insert_newline (void) x = b; } /* { dg-begin-multiline-output "" } + x = a; + break; case 'b': ^~~~~~~~