From patchwork Wed Apr 24 20:22:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1927369 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=XO/tEtUH; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VPr571TF0z1yP2 for ; Thu, 25 Apr 2024 06:22:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ABD8F384642D for ; Wed, 24 Apr 2024 20:22:31 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id D829D3846401 for ; Wed, 24 Apr 2024 20:22:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D829D3846401 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D829D3846401 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713990135; cv=none; b=LhspW0094wGT3Xp7O/G9FERxLnKjua2kpBeyHiOvOeeZ6E1bNW1WnK8rgSlFWvVa0tsfbbPpP5N+8RDlbZXLFsgkJxo8LAQ4v5dwBRZy9mTJwC1wrBf6d4SG1CJdBSLyjafND/74XJKtIvhh27uDoqAU4sPofuVkLq9B7dJOt34= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713990135; c=relaxed/simple; bh=zJK4nX8aimv0w7ZKY74VlAP9gm9HpzV8/Hns5/sW+Hg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VTiu7BlIo9jIVZzmT6mEpYDfb8Chooct4a3s0fz+mFs6yvb2XCNsYRYns4uw5TyEehf9+xoKAMocEsw6tfwDj8nWXh70NMetY6Cm98vPgNSYXBkrNaSCsIaZbKM0K4xMBi5wRZhvASY1YW/bVS4+vIomK4NKJfD8Xoec3Y9HugU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713990133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AGw+CAmHj9eWP9J+PiWl3engGmAoXSKgvmcsV7it4v8=; b=XO/tEtUHPxhQBWKmx15l99rclDRIPCPOVB5CfEFBktYcm3U3KihjuqgA95ICOtO0vBxEU5 rzsPqphdo7ZuEEQCsZXYgDeyknBwC1q8BZICmCttBahgwZPspcAhoJBJBkr+b+Yuu6fY8H X68wmX9y+4+GdyulXakKfGgfrtDTiH0= Received: from mail-ot1-f72.google.com (mail-ot1-f72.google.com [209.85.210.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-668-8UV0u_RbO9i-cXqWCKCZfA-1; Wed, 24 Apr 2024 16:22:12 -0400 X-MC-Unique: 8UV0u_RbO9i-cXqWCKCZfA-1 Received: by mail-ot1-f72.google.com with SMTP id 46e09a7af769-6eb7d6b370dso347917a34.3 for ; Wed, 24 Apr 2024 13:22:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713990129; x=1714594929; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AGw+CAmHj9eWP9J+PiWl3engGmAoXSKgvmcsV7it4v8=; b=N2x6Y25EP2C2iPRe7/nC5c6SqysrTeL6RzgKBxHTw5MSWevzKUUqtPSH1HC2bOrbfs LGD8XkMZ351BKKwY9z8BzgqHuLSd2BbwEFp5QgCAS79ZztgrrYOuryCJOyRdX8KxotpJ JrtyMqjCQSEaejrESz48Oq0vUFo/abVK3xiY6aVNvTa/2Ijbh8iCMOqt/1ZoP9cTY0qI sUHpDoIxDswRb0rqM6v1/V0xJWepUkD2ViDhuFy90+A6zN/vbia23dR8FNdlQ7aSKw7M kjHfUIxC2HAIQPE8Vp4a9PFkmqO7ND57eY6vib6WyMgth4msdTYW80NSrEqTZycagSiB R22w== X-Gm-Message-State: AOJu0Yz59yYuI6itAXjmz5YrqxfowOsjDuonbkVVuM3G3Te+Fd116vY6 7J/MsB0+CKUNTRJYSIDHCUrA+IxF1x8m9FYsm12T9ze1ib2oSjPD8KMpy6DpQfR/lcpWBr4rS/t u8SObftptdszQFIApP7Y4mnFsTAZaAe7TOJSGi7NM9BQi0h5R8bW9lXMss5B0quFAfZHsYEnfMu M3t0hKINkD7NBLg/Uk4LXvy/7D2MFvXUjkQh/8 X-Received: by 2002:a05:6358:16d1:b0:186:436e:a217 with SMTP id r17-20020a05635816d100b00186436ea217mr4298883rwl.25.1713990129409; Wed, 24 Apr 2024 13:22:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGWq79+S3uPHUJ5N5nI3M25LrmpSUoBug7MKzYD6eLKD+SrAOcnVQFoIfOdjwxHzMmTD1yaLw== X-Received: by 2002:a05:6358:16d1:b0:186:436e:a217 with SMTP id r17-20020a05635816d100b00186436ea217mr4298852rwl.25.1713990128928; Wed, 24 Apr 2024 13:22:08 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id m11-20020a0cac4b000000b006a0503a5217sm1638076qvb.108.2024.04.24.13.22.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Apr 2024 13:22:08 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, dmalcolm@redhat.com, Patrick Palka Subject: [PATCH] c++: fix source printing for "required from here" message Date: Wed, 24 Apr 2024 16:22:06 -0400 Message-ID: <20240424202206.173103-1-ppalka@redhat.com> X-Mailer: git-send-email 2.45.0.rc1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested on x86_64-pc-linux-gnu, full bootstrap+regtest in progress, does this look OK if successful? -- >8 -- 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 context->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. --- 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(-) create mode 100644 gcc/testsuite/g++.dg/template/error60a.C diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc index 7074845154e..a7067d4d2ed 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); + context->printer->prefix = 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..7c2f8188d16 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/error60a.C @@ -0,0 +1,46 @@ +// Like error60.C but first issues an unrelated error that +// causes 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 "" } */