From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ua1-x92b.google.com (mail-ua1-x92b.google.com [IPv6:2607:f8b0:4864:20::92b]) by sourceware.org (Postfix) with ESMTPS id A09CD3858C2F for ; Thu, 15 Jun 2023 00:37:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A09CD3858C2F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gwmail.gwu.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gwmail.gwu.edu Received: by mail-ua1-x92b.google.com with SMTP id a1e0cc1a2514c-78a065548e3so1179266241.0 for ; Wed, 14 Jun 2023 17:37:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gwmail.gwu.edu; s=google; t=1686789431; x=1689381431; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=nSUb2Tkv2dMDo4ULJGdNMPYcgwNNLkcG/DJ/mz7Epw4=; b=Wfdl1Hvj/Scx00KuqGNr9RYDnTCtoSWumD28uzSts7i2PYdqW8YVK/NK0gV1CSAvPi 6tpKvWdX8H3pplBnHCAr5sxKoqO+xxTNkgsdxUTqBQpKdYyFLjTLZj6F+HIXJqat+RAQ gXHH5DEFU7Sh0YYCW9KPCo4X8XIPmuYxiEYzAgjwx9I5eB+DkyzU9i/WE5VdCo3Ljhyz u1t8Y/EvvXjmDhDDBPGYia0QoDUjWDfcrwWMqmLfiUyD6M0SaWjQr8dr3Q29A2t0ZKCS AzpvnD014tBxrA/Q42cA/8KVd9Zx2cwnK5lZSMTZt5L9lFYN8BYO3lf+L+W1TL0q8EZ3 4w5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686789431; x=1689381431; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nSUb2Tkv2dMDo4ULJGdNMPYcgwNNLkcG/DJ/mz7Epw4=; b=HzlwEIiOV7eg721v0BeDw4ntjlv96/gSosu/SSb8bnS0J2jmfmhtoufkE0jETx/HtW lJZS79S9Hs+4ctQ/aopzV8ltgR4OctpT8mJeAmZv2z2a3Juk93WC+WjewNlSfqbRbh/O V3MUt5+BZ077yGdfz3ZyYyRBi5Fg3ubvbuKCrv74negQaGVC+ns+cmU9U4rk99wj8Lyj Y33s62XNXX9dkt/vP3Q0NHdZ9E/hosAqBhIZ0QCTsG7UY3pLw3J2bmrXYpC/X2qtZm9F gQ7JTdVYbbcAlzXgisB7qgqOkDoNx9iDBhFAtUlxf7DfaAPTjJK0lz8IAmtngiJJ9EWU BvKA== X-Gm-Message-State: AC+VfDyV2onKXOPhPvm0mzblmJdDfunh34J3Gpd045InIXtPT9SY2ksv LiPRE9jtl8n29Bbr2+cgcUtoqMwM8DN+RLT6dv6xmd/8UYDO2UxD X-Google-Smtp-Source: ACHHUZ45f5FqRL51xySUHKJedEWK2aMmnk0RithAkuTKMvlgj9ZXA/9108PuzUV6R13hGS8ZWhyXPpyQoShOeUCLb2k= X-Received: by 2002:a1f:4313:0:b0:46e:7b3c:d680 with SMTP id q19-20020a1f4313000000b0046e7b3cd680mr3601555vka.6.1686789430946; Wed, 14 Jun 2023 17:37:10 -0700 (PDT) MIME-Version: 1.0 References: <20230615002814.967814-1-dmalcolm@redhat.com> In-Reply-To: <20230615002814.967814-1-dmalcolm@redhat.com> From: Eric Gallager Date: Wed, 14 Jun 2023 20:36:59 -0400 Message-ID: Subject: Re: [PATCH] c++: provide #include hint for missing includes [PR110164] To: David Malcolm Cc: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 Wed, Jun 14, 2023 at 8:29=E2=80=AFPM David Malcolm via Gcc-patches wrote: > > PR c++/110164 notes that in cases where we have a forward decl > of a std library type such as: > > std::array x; > > we omit this diagnostic: > > error: aggregate =E2=80=98std::array x=E2=80=99 has incomplete t= ype and cannot be defined > > This patch adds this hint to the diagnostic: > > note: =E2=80=98std::array=E2=80=99 is defined in header =E2=80=98= =E2=80=99; this is probably fixable by adding =E2=80=98#include =E2= =80=99 > ..."probably"? > Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. > OK for trunk? > > 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/missing-header-pr110164.C: New test. > --- > gcc/cp/cp-name-hint.h | 3 +++ > gcc/cp/decl.cc | 10 ++++++++++ > gcc/cp/name-lookup.cc | 2 +- > gcc/testsuite/g++.dg/missing-header-pr110164.C | 10 ++++++++++ > 4 files changed, 24 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/missing-header-pr110164.C > > diff --git a/gcc/cp/cp-name-hint.h b/gcc/cp/cp-name-hint.h > index bfa7c53c8f6..e2387e23d1f 100644 > --- a/gcc/cp/cp-name-hint.h > +++ b/gcc/cp/cp-name-hint.h > @@ -32,6 +32,9 @@ 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, t= ree); > +extern name_hint maybe_suggest_missing_header (location_t location, > + tree name, > + tree scope); > 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 a672e4844f1..504b08ec250 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) !=3D ARRAY_TYPE) > { > + auto_diagnostic_group d; > error ("variable %q#D has initializer but incomplete type", dec= l); > + maybe_suggest_missing_header (input_location, > + TYPE_IDENTIFIER (type), > + TYPE_CONTEXT (type)); > type =3D TREE_TYPE (decl) =3D 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 define= d", > decl); > + maybe_suggest_missing_header (input_location, > + TYPE_IDENTIFIER (type), > + TYPE_CONTEXT (type)); > /* Change the type so that assemble_variable will give > DECL an rtl we can live with: (mem (const_int 0)). */ > type =3D TREE_TYPE (decl) =3D error_mark_node; > diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc > index 6ac58a35b56..917b481c163 100644 > --- a/gcc/cp/name-lookup.cc > +++ b/gcc/cp/name-lookup.cc > @@ -6796,7 +6796,7 @@ maybe_suggest_missing_std_header (location_t locati= on, 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 =3D=3D NULL_TREE) > diff --git a/gcc/testsuite/g++.dg/missing-header-pr110164.C b/gcc/testsui= te/g++.dg/missing-header-pr110164.C > new file mode 100644 > index 00000000000..15980071c38 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/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 pr= obably 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 pr= obably fixable by adding '#include '" "hint" { target *-*-* } .-1 } = */ > + > -- > 2.26.3 >