From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) by sourceware.org (Postfix) with ESMTP id 8A2203858C1F for ; Thu, 15 Jun 2023 00:46:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8A2203858C1F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gentoo.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gentoo.org References: <20230615002814.967814-1-dmalcolm@redhat.com> User-agent: mu4e 1.10.3; emacs 29.0.91 From: Sam James To: Eric Gallager Cc: David Malcolm , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++: provide #include hint for missing includes [PR110164] Date: Thu, 15 Jun 2023 01:43:18 +0100 In-reply-to: Message-ID: <87o7lhv9h8.fsf@gentoo.org> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,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: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Eric Gallager via Gcc-patches writes: > 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 = type 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"? > Right now, our fixit says: ``` /tmp/foo.c:1:1: note: =E2=80=98time_t=E2=80=99 is defined in header =E2=80= =98=E2=80=99; did you forget to =E2=80=98#include =E2=80=99? ``` We should probably use the same phrasing for consistency? >> 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, = tree); >> +extern name_hint maybe_suggest_missing_header (location_t location, >> + tree name, >> + tree scope); >> extern name_hint suggest_alternative_in_explicit_scope (location_t, tre= e, 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", de= cl); >> + 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 defin= ed", >> 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 locat= ion, 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 scop= e) >> { >> if (scope =3D=3D NULL_TREE) >> diff --git a/gcc/testsuite/g++.dg/missing-header-pr110164.C b/gcc/testsu= ite/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 p= robably 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 p= robably fixable by adding '#include '" "hint" { target *-*-* } .-1 }= */ >> + >> -- >> 2.26.3 >> --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iOUEARYKAI0WIQQlpruI3Zt2TGtVQcJzhAn1IN+RkAUCZIpfQ18UgAAAAAAuAChp c3N1ZXItZnByQG5vdGF0aW9ucy5vcGVucGdwLmZpZnRoaG9yc2VtYW4ubmV0MjVB NkJCODhERDlCNzY0QzZCNTU0MUMyNzM4NDA5RjUyMERGOTE5MA8cc2FtQGdlbnRv by5vcmcACgkQc4QJ9SDfkZBagQEAsSPbnYLoaM2QWhm+w2/wade7YYUrzOt0sT6o 6sRSkyEA/R/FVNjc3O6H7VSShvFrdoScljMIFChvz5BaPzeg6IkI =vOnA -----END PGP SIGNATURE----- --=-=-=--