From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.145.221.124]) by sourceware.org (Postfix) with ESMTPS id 288CA3858C2F for ; Fri, 23 Jun 2023 22:01:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 288CA3858C2F Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687557671; 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: in-reply-to:in-reply-to:references:references; bh=5AHfDbqaOQrI7K/1XOwWhxCZd2L5jfLBTKPsZAaulS8=; b=ONY24vuFtLvzWaWG0BCNhkXKm1f325b85U2R0AC39BNOtj682PjCaI/9am2mP6V+nzZ7Ll uRHXusFuwkwiSod4wNTOReHZEjikkTyQKjObaltk8swi58oK4ATL0Slxw+1dCSIfTmtlDq 2m/FNze6RQehz1QQB2+S6NxgsHqFeto= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-2-fR5YroLNMpOYaNS31jZ5dQ-1; Fri, 23 Jun 2023 18:01:10 -0400 X-MC-Unique: fR5YroLNMpOYaNS31jZ5dQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1BC0A3C01C26 for ; Fri, 23 Jun 2023 22:01:10 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.9.37]) by smtp.corp.redhat.com (Postfix) with ESMTP id CC02D492B01; Fri, 23 Jun 2023 22:01:09 +0000 (UTC) From: David Malcolm To: Jason Merrill , Marek Polacek Cc: gcc-patches@gcc.gnu.org, David Malcolm Subject: [pushed] c++: provide #include hint for missing includes [PR110164] Date: Fri, 23 Jun 2023 18:01:06 -0400 Message-Id: <20230623220106.117866-1-dmalcolm@redhat.com> In-Reply-To: <97f85e9c-28a2-5e7b-16d5-b622d2eaa6b3@redhat.com> References: <97f85e9c-28a2-5e7b-16d5-b622d2eaa6b3@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: > On Fri, 2023-06-23 at 12:08 -0400, Jason Merrill wrote: > > On 6/22/23 11:50, Marek Polacek wrote: > > > On Wed, Jun 21, 2023 at 04:44:00PM -0400, David Malcolm via Gcc-patches wrote: > > > I'd like to ping this C++ FE patch for review: > > > https://gcc.gnu.org/pipermail/gcc-patches/2023-June/621779.html > > Not an approval, but LGTM, though some nits below: > OK with the tweaks Marek suggests. Thanks Marek and Jason. For reference, here's what I pushed (as r14-2055-g13709b518aa976): PR c++/110164 notes that in cases where we have a forward decl of a std library type such as: std::array x; we emit this diagnostic: error: aggregate ‘std::array x’ has incomplete type and cannot be defined This patch adds this hint to the diagnostic: note: ‘std::array’ is defined in header ‘’; this is probably fixable by adding ‘#include ’ gcc/cp/ChangeLog: PR c++/110164 * cp-name-hint.h (maybe_suggest_missing_header): New decl. * decl.cc: Define INCLUDE_MEMORY. Add include of "cp/cp-name-hint.h". (start_decl_1): Call maybe_suggest_missing_header. * name-lookup.cc (maybe_suggest_missing_header): Remove "static". gcc/testsuite/ChangeLog: PR c++/110164 * g++.dg/diagnostic/missing-header-pr110164.C: New test. --- gcc/cp/cp-name-hint.h | 1 + gcc/cp/decl.cc | 10 ++++++++++ gcc/cp/name-lookup.cc | 2 +- .../g++.dg/diagnostic/missing-header-pr110164.C | 10 ++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/diagnostic/missing-header-pr110164.C diff --git a/gcc/cp/cp-name-hint.h b/gcc/cp/cp-name-hint.h index bfa7c53c8f6..7693980138a 100644 --- a/gcc/cp/cp-name-hint.h +++ b/gcc/cp/cp-name-hint.h @@ -32,6 +32,7 @@ along with GCC; see the file COPYING3. If not see extern name_hint suggest_alternatives_for (location_t, tree, bool); extern name_hint suggest_alternatives_in_other_namespaces (location_t, tree); +extern name_hint maybe_suggest_missing_header (location_t, tree, tree); extern name_hint suggest_alternative_in_explicit_scope (location_t, tree, tree); extern name_hint suggest_alternative_in_scoped_enum (tree, tree); diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index c07a4a8d58d..60f107d50c4 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -27,6 +27,7 @@ along with GCC; see the file COPYING3. If not see line numbers. For example, the CONST_DECLs for enum values. */ #include "config.h" +#define INCLUDE_MEMORY #include "system.h" #include "coretypes.h" #include "target.h" @@ -46,6 +47,7 @@ along with GCC; see the file COPYING3. If not see #include "c-family/c-objc.h" #include "c-family/c-pragma.h" #include "c-family/c-ubsan.h" +#include "cp/cp-name-hint.h" #include "debug.h" #include "plugin.h" #include "builtins.h" @@ -5995,7 +5997,11 @@ start_decl_1 (tree decl, bool initialized) ; /* An auto type is ok. */ else if (TREE_CODE (type) != ARRAY_TYPE) { + auto_diagnostic_group d; error ("variable %q#D has initializer but incomplete type", decl); + maybe_suggest_missing_header (input_location, + TYPE_IDENTIFIER (type), + CP_TYPE_CONTEXT (type)); type = TREE_TYPE (decl) = error_mark_node; } else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) @@ -6011,8 +6017,12 @@ start_decl_1 (tree decl, bool initialized) gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type)); else { + auto_diagnostic_group d; error ("aggregate %q#D has incomplete type and cannot be defined", decl); + maybe_suggest_missing_header (input_location, + TYPE_IDENTIFIER (type), + CP_TYPE_CONTEXT (type)); /* Change the type so that assemble_variable will give DECL an rtl we can live with: (mem (const_int 0)). */ type = TREE_TYPE (decl) = error_mark_node; diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 53b6870f067..74565184403 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -6796,7 +6796,7 @@ maybe_suggest_missing_std_header (location_t location, tree name) for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't applicable. */ -static name_hint +name_hint maybe_suggest_missing_header (location_t location, tree name, tree scope) { if (scope == NULL_TREE) diff --git a/gcc/testsuite/g++.dg/diagnostic/missing-header-pr110164.C b/gcc/testsuite/g++.dg/diagnostic/missing-header-pr110164.C new file mode 100644 index 00000000000..15980071c38 --- /dev/null +++ b/gcc/testsuite/g++.dg/diagnostic/missing-header-pr110164.C @@ -0,0 +1,10 @@ +// { dg-require-effective-target c++11 } + +#include + +std::array a1; /* { dg-error "incomplete type" } */ +/* { dg-message "'std::array' is defined in header ''; this is probably fixable by adding '#include '" "hint" { target *-*-* } .-1 } */ + +std::array a2 {5}; /* { dg-error "incomplete type" } */ +/* { dg-message "'std::array' is defined in header ''; this is probably fixable by adding '#include '" "hint" { target *-*-* } .-1 } */ + -- 2.26.3