From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 3EBDA3858C42; Fri, 26 Apr 2024 12:29:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3EBDA3858C42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1714134573; bh=DzQqLt0ueH2hg+RnlnT+VuQFbQHaleUKybUf2SqusNM=; h=From:To:Subject:Date:From; b=a33SoVnp4L+FpKrYrNhaumJWikP0n1giHQnkSgY4Oknx+6XLUh0V+JwlLmoyWMepx YO4hzAMsPFU2fMmOMxvflDaFhqzzNu7PQMsxXnxyfFuEpCv6pAnQ/8XY4LK4psB9uu snGYAVbbbJNRwyEYnhafx7uY++88hPdJusYxE/6I= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-10140] c++: fix source printing for "required from here" message X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/releases/gcc-14 X-Git-Oldrev: 5e5f33a067d03d11f67eba719d3ef9f37b23886d X-Git-Newrev: c014cfd8853240827feb3a4cef92403e83cd4265 Message-Id: <20240426122933.3EBDA3858C42@sourceware.org> Date: Fri, 26 Apr 2024 12:29:33 +0000 (GMT) List-Id: https://gcc.gnu.org/g:c014cfd8853240827feb3a4cef92403e83cd4265 commit r14-10140-gc014cfd8853240827feb3a4cef92403e83cd4265 Author: Patrick Palka Date: Fri Apr 26 07:44:25 2024 -0400 c++: fix source printing for "required from here" message It seems the diagnostic machinery's source line printing respects the pretty printer prefix, but this is undesirable for the call to diagnostic_show_locus in print_instantiation_partial_context_line (added in r14-4388-g1c45319b66edc9) since the prefix may have been set when issuing an earlier, unrelated diagnostic and we just want to print an unprefixed source line. This patch naively fixes this by clearing the prefix before calling diagnostic_show_locus. Before this patch, for error60a.C below we'd print gcc/testsuite/g++.dg/template/error60a.C: In function ‘void usage()’: gcc/testsuite/g++.dg/template/error60a.C:24:3: error: ‘unrelated_error’ was not declared in this scope 24 | unrelated_error; // { dg-error "not declared" } | ^~~~~~~~~~~~~~~ gcc/testsuite/g++.dg/template/error60a.C: In instantiation of ‘void test(Foo) [with Foo = int]’: gcc/testsuite/g++.dg/template/error60a.C:25:13: required from here gcc/testsuite/g++.dg/template/error60a.C:24:3: error: 25 | test (42); // { dg-message " required from here" } gcc/testsuite/g++.dg/template/error60a.C:24:3: error: | ~~~~~~~~~~^~~~ gcc/testsuite/g++.dg/template/error60a.C:19:24: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive] 19 | my_pointer ptr (val); // { dg-error "invalid conversion from 'int' to 'int\\*'" } | ^~~ | | | int gcc/testsuite/g++.dg/template/error60a.C:9:20: note: initializing argument 1 of ‘my_pointer::my_pointer(Foo*) [with Foo = int]’ 9 | my_pointer (Foo *ptr) // { dg-message " initializing argument 1" } | ~~~~~^~~ and afterward we print gcc/testsuite/g++.dg/template/error60a.C: In function ‘void usage()’: gcc/testsuite/g++.dg/template/error60a.C:24:3: error: ‘unrelated_error’ was not declared in this scope 24 | unrelated_error; // { dg-error "not declared" } | ^~~~~~~~~~~~~~~ gcc/testsuite/g++.dg/template/error60a.C: In instantiation of ‘void test(Foo) [with Foo = int]’: gcc/testsuite/g++.dg/template/error60a.C:25:13: required from here 25 | test (42); // { dg-message " required from here" } | ~~~~~~~~~~^~~~ gcc/testsuite/g++.dg/template/error60a.C:19:24: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive] 19 | my_pointer ptr (val); // { dg-error "invalid conversion from 'int' to 'int\\*'" } | ^~~ | | | int gcc/testsuite/g++.dg/template/error60a.C:9:20: note: initializing argument 1 of ‘my_pointer::my_pointer(Foo*) [with Foo = int]’ 9 | my_pointer (Foo *ptr) // { dg-message " initializing argument 1" } | ~~~~~^~~ gcc/cp/ChangeLog: * error.cc (print_instantiation_partial_context_line): Clear the pretty printer prefix around the call to diagnostic_show_locus. gcc/testsuite/ChangeLog: * g++.dg/concepts/diagnostic2.C: Expect source line printed for the "required from here" message. * g++.dg/template/error60a.C: New test. (cherry picked from commit 7d5479a2ecf6309281de10b747a7423169a2ff95) Diff: --- gcc/cp/error.cc | 2 ++ gcc/testsuite/g++.dg/concepts/diagnostic2.C | 6 +++- gcc/testsuite/g++.dg/template/error60a.C | 46 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc index 7074845154e..37987ccb570 100644 --- a/gcc/cp/error.cc +++ b/gcc/cp/error.cc @@ -3793,7 +3793,9 @@ print_instantiation_partial_context_line (diagnostic_context *context, : _("required from here\n")); } gcc_rich_location rich_loc (loc); + char *saved_prefix = pp_take_prefix (context->printer); diagnostic_show_locus (context, &rich_loc, DK_NOTE); + pp_set_prefix (context->printer, saved_prefix); } /* Same as print_instantiation_full_context but less verbose. */ diff --git a/gcc/testsuite/g++.dg/concepts/diagnostic2.C b/gcc/testsuite/g++.dg/concepts/diagnostic2.C index 6550ed6b3bd..d6f5872de2c 100644 --- a/gcc/testsuite/g++.dg/concepts/diagnostic2.C +++ b/gcc/testsuite/g++.dg/concepts/diagnostic2.C @@ -23,7 +23,11 @@ void baz() { bar(); // { dg-error "no match" } -/* { dg-begin-multiline-output "" } +/* { dg-begin-multiline-output "for no match error" } + bar(); + ~~~~~~~~^~ + { dg-end-multiline-output "" } */ +/* { dg-begin-multiline-output "for required from here message" } bar(); ~~~~~~~~^~ { dg-end-multiline-output "" } */ diff --git a/gcc/testsuite/g++.dg/template/error60a.C b/gcc/testsuite/g++.dg/template/error60a.C new file mode 100644 index 00000000000..9d0f170c18b --- /dev/null +++ b/gcc/testsuite/g++.dg/template/error60a.C @@ -0,0 +1,46 @@ +// A version of error60.C that first issues an unrelated error +// to cause the pretty printer prefix to get set, verifying we +// still print the source line for the "required from here" +// message correctly in that case. +// { dg-options "-fdiagnostics-show-caret" } + +template +struct my_pointer +{ + my_pointer (Foo *ptr) // { dg-message " initializing argument 1" } + : m_ptr (ptr) + {} + + Foo *m_ptr; +}; + +template +void test (Foo val) +{ + my_pointer ptr (val); // { dg-error "invalid conversion from 'int' to 'int\\*'" } +} + +void usage () +{ + unrelated_error; // { dg-error "not declared" } + test (42); // { dg-message " required from here" } + /* { dg-begin-multiline-output "" } + unrelated_error; + ^~~~~~~~~~~~~~~ + { dg-end-multiline-output "" } */ + /* { dg-begin-multiline-output "" } + test (42); + ~~~~~~~~~~^~~~ + { dg-end-multiline-output "" } */ +} + + /* { dg-begin-multiline-output "" } + my_pointer (Foo *ptr) + ~~~~~^~~ + { dg-end-multiline-output "" } */ + /* { dg-begin-multiline-output "" } + my_pointer ptr (val); + ^~~ + | + int + { dg-end-multiline-output "" } */