public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: provide #include hint for missing includes [PR110164]
@ 2023-06-15  0:28 David Malcolm
  2023-06-15  0:36 ` Eric Gallager
  2023-06-21 20:44 ` PING: " David Malcolm
  0 siblings, 2 replies; 9+ messages in thread
From: David Malcolm @ 2023-06-15  0:28 UTC (permalink / raw)
  To: gcc-patches; +Cc: David Malcolm

PR c++/110164 notes that in cases where we have a forward decl
of a std library type such as:

std::array<int, 10> x;

we omit this diagnostic:

error: aggregate ‘std::array<int, 10> x’ has incomplete type and cannot be defined

This patch adds this hint to the diagnostic:

note: ‘std::array’ is defined in header ‘<array>’; this is probably fixable by adding ‘#include <array>’

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, 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) != 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),
+					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),
+					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 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 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/missing-header-pr110164.C b/gcc/testsuite/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 <map>
+
+std::array<int, 10> a1; /* { dg-error "incomplete type" } */
+/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
+
+std::array<int, 10> a2 {5}; /* { dg-error "incomplete type" } */
+/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
+
-- 
2.26.3


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] c++: provide #include hint for missing includes [PR110164]
  2023-06-15  0:28 [PATCH] c++: provide #include hint for missing includes [PR110164] David Malcolm
@ 2023-06-15  0:36 ` Eric Gallager
  2023-06-15  0:43   ` Sam James
  2023-06-21 20:44 ` PING: " David Malcolm
  1 sibling, 1 reply; 9+ messages in thread
From: Eric Gallager @ 2023-06-15  0:36 UTC (permalink / raw)
  To: David Malcolm; +Cc: gcc-patches

On Wed, Jun 14, 2023 at 8:29 PM David Malcolm via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> PR c++/110164 notes that in cases where we have a forward decl
> of a std library type such as:
>
> std::array<int, 10> x;
>
> we omit this diagnostic:
>
> error: aggregate ‘std::array<int, 10> x’ has incomplete type and cannot be defined
>
> This patch adds this hint to the diagnostic:
>
> note: ‘std::array’ is defined in header ‘<array>’; this is probably fixable by adding ‘#include <array>’
>

..."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, 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, 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) != 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),
> +                                       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),
> +                                       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 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 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/missing-header-pr110164.C b/gcc/testsuite/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 <map>
> +
> +std::array<int, 10> a1; /* { dg-error "incomplete type" } */
> +/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
> +
> +std::array<int, 10> a2 {5}; /* { dg-error "incomplete type" } */
> +/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
> +
> --
> 2.26.3
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] c++: provide #include hint for missing includes [PR110164]
  2023-06-15  0:36 ` Eric Gallager
@ 2023-06-15  0:43   ` Sam James
  2023-06-15 11:54     ` David Malcolm
  0 siblings, 1 reply; 9+ messages in thread
From: Sam James @ 2023-06-15  0:43 UTC (permalink / raw)
  To: Eric Gallager; +Cc: David Malcolm, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 5995 bytes --]


Eric Gallager via Gcc-patches <gcc-patches@gcc.gnu.org> writes:

> On Wed, Jun 14, 2023 at 8:29 PM David Malcolm via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
>>
>> PR c++/110164 notes that in cases where we have a forward decl
>> of a std library type such as:
>>
>> std::array<int, 10> x;
>>
>> we omit this diagnostic:
>>
>> error: aggregate ‘std::array<int, 10> x’ has incomplete type and cannot be defined
>>
>> This patch adds this hint to the diagnostic:
>>
>> note: ‘std::array’ is defined in header ‘<array>’; this is probably fixable by adding ‘#include <array>’
>>
>
> ..."probably"?
>

Right now, our fixit says:
```
/tmp/foo.c:1:1: note: ‘time_t’ is defined in header ‘<ctime>’; did you forget to ‘#include <ctime>’?
```

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, 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) != 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),
>> +                                       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),
>> +                                       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 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 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/missing-header-pr110164.C b/gcc/testsuite/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 <map>
>> +
>> +std::array<int, 10> a1; /* { dg-error "incomplete type" } */
>> +/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
>> +
>> +std::array<int, 10> a2 {5}; /* { dg-error "incomplete type" } */
>> +/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
>> +
>> --
>> 2.26.3
>>


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 377 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] c++: provide #include hint for missing includes [PR110164]
  2023-06-15  0:43   ` Sam James
@ 2023-06-15 11:54     ` David Malcolm
  2023-06-15 12:11       ` Sam James
  0 siblings, 1 reply; 9+ messages in thread
From: David Malcolm @ 2023-06-15 11:54 UTC (permalink / raw)
  To: Sam James, Eric Gallager; +Cc: gcc-patches

On Thu, 2023-06-15 at 01:43 +0100, Sam James wrote:
> 
> Eric Gallager via Gcc-patches <gcc-patches@gcc.gnu.org> writes:
> 
> > On Wed, Jun 14, 2023 at 8:29 PM David Malcolm via Gcc-patches
> > <gcc-patches@gcc.gnu.org> wrote:
> > > 
> > > PR c++/110164 notes that in cases where we have a forward decl
> > > of a std library type such as:
> > > 
> > > std::array<int, 10> x;
> > > 
> > > we omit this diagnostic:
> > > 
> > > error: aggregate ‘std::array<int, 10> x’ has incomplete type and
> > > cannot be defined
> > > 
> > > This patch adds this hint to the diagnostic:
> > > 
> > > note: ‘std::array’ is defined in header ‘<array>’; this is
> > > probably fixable by adding ‘#include <array>’
> > > 
> > 
> > ..."probably"?
> > 
> 
> Right now, our fixit says:
> ```
> /tmp/foo.c:1:1: note: ‘time_t’ is defined in header ‘<ctime>’; did
> you forget to ‘#include <ctime>’?
> ```
> 
> We should probably use the same phrasing for consistency?

It's using the same phrasing (it's calling the same function); I
changed the wording recently, in r14-1798-g7474c46cf2d371:
  https://gcc.gnu.org/pipermail/gcc-patches/2023-June/621607.html

I used "probably" because there's no guarantee it will fix things (e.g.
if the user has non-standard headers).

Dave


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] c++: provide #include hint for missing includes [PR110164]
  2023-06-15 11:54     ` David Malcolm
@ 2023-06-15 12:11       ` Sam James
  0 siblings, 0 replies; 9+ messages in thread
From: Sam James @ 2023-06-15 12:11 UTC (permalink / raw)
  To: David Malcolm; +Cc: Eric Gallager, gcc-patches



> On 15 Jun 2023, at 12:54, David Malcolm <dmalcolm@redhat.com> wrote:
> 
> On Thu, 2023-06-15 at 01:43 +0100, Sam James wrote:
>> 
>> Eric Gallager via Gcc-patches <gcc-patches@gcc.gnu.org> writes:
>> 
>>> On Wed, Jun 14, 2023 at 8:29 PM David Malcolm via Gcc-patches
>>> <gcc-patches@gcc.gnu.org> wrote:
>>>> 
>>>> PR c++/110164 notes that in cases where we have a forward decl
>>>> of a std library type such as:
>>>> 
>>>> std::array<int, 10> x;
>>>> 
>>>> we omit this diagnostic:
>>>> 
>>>> error: aggregate ‘std::array<int, 10> x’ has incomplete type and
>>>> cannot be defined
>>>> 
>>>> This patch adds this hint to the diagnostic:
>>>> 
>>>> note: ‘std::array’ is defined in header ‘<array>’; this is
>>>> probably fixable by adding ‘#include <array>’
>>>> 
>>> 
>>> ..."probably"?
>>> 
>> 
>> Right now, our fixit says:
>> ```
>> /tmp/foo.c:1:1: note: ‘time_t’ is defined in header ‘<ctime>’; did
>> you forget to ‘#include <ctime>’?
>> ```
>> 
>> We should probably use the same phrasing for consistency?
> 
> It's using the same phrasing (it's calling the same function); I
> changed the wording recently, in r14-1798-g7474c46cf2d371:
>  https://gcc.gnu.org/pipermail/gcc-patches/2023-June/621607.html
> 
> I used "probably" because there's no guarantee it will fix things (e.g.
> if the user has non-standard headers).

Ah, sorry Dave, shame on me for not git pulling first :)

No objection then - I like the new phrasing, was just worried about consistency.

> 
> Dave
> 

Thank you!

^ permalink raw reply	[flat|nested] 9+ messages in thread

* PING: Re: [PATCH] c++: provide #include hint for missing includes [PR110164]
  2023-06-15  0:28 [PATCH] c++: provide #include hint for missing includes [PR110164] David Malcolm
  2023-06-15  0:36 ` Eric Gallager
@ 2023-06-21 20:44 ` David Malcolm
  2023-06-22 15:50   ` Marek Polacek
  1 sibling, 1 reply; 9+ messages in thread
From: David Malcolm @ 2023-06-21 20:44 UTC (permalink / raw)
  To: gcc-patches, Jason Merrill

I'd like to ping this C++ FE patch for review:
    https://gcc.gnu.org/pipermail/gcc-patches/2023-June/621779.html

Thanks
Dave

On Wed, 2023-06-14 at 20:28 -0400, David Malcolm wrote:
> PR c++/110164 notes that in cases where we have a forward decl
> of a std library type such as:
> 
> std::array<int, 10> x;
> 
> we omit this diagnostic:
> 
> error: aggregate ‘std::array<int, 10> x’ has incomplete type and cannot be defined
> 
> This patch adds this hint to the diagnostic:
> 
> note: ‘std::array’ is defined in header ‘<array>’; this is probably fixable by adding ‘#include <array>’
> 
> 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, 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) != 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),
> +                                       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),
> +                                       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 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 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/missing-header-pr110164.C b/gcc/testsuite/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 <map>
> +
> +std::array<int, 10> a1; /* { dg-error "incomplete type" } */
> +/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
> +
> +std::array<int, 10> a2 {5}; /* { dg-error "incomplete type" } */
> +/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
> +


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PING: Re: [PATCH] c++: provide #include hint for missing includes [PR110164]
  2023-06-21 20:44 ` PING: " David Malcolm
@ 2023-06-22 15:50   ` Marek Polacek
  2023-06-23 16:08     ` Jason Merrill
  0 siblings, 1 reply; 9+ messages in thread
From: Marek Polacek @ 2023-06-22 15:50 UTC (permalink / raw)
  To: David Malcolm; +Cc: gcc-patches, Jason Merrill

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:
 
> On Wed, 2023-06-14 at 20:28 -0400, David Malcolm wrote:
> > PR c++/110164 notes that in cases where we have a forward decl
> > of a std library type such as:
> > 
> > std::array<int, 10> x;
> > 
> > we omit this diagnostic:
> > 
> > error: aggregate ‘std::array<int, 10> x’ has incomplete type and cannot be defined
> > 
> > This patch adds this hint to the diagnostic:
> > 
> > note: ‘std::array’ is defined in header ‘<array>’; this is probably fixable by adding ‘#include <array>’
> > 
> > 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);

The enclosing decls omit the parameter names; if you do that, it may
fit on one line.

> >  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) != 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),
> > +                                       TYPE_CONTEXT (type));

Maybe CP_TYPE_CONTEXT?

> >           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),
> > +                                       TYPE_CONTEXT (type));

Here as well.

> >           /* 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 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 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/missing-header-pr110164.C b/gcc/testsuite/g++.dg/missing-header-pr110164.C
> > new file mode 100644
> > index 00000000000..15980071c38
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/missing-header-pr110164.C

Can we put the new test into g++.dg/diagnostic/?

Thanks,

> > @@ -0,0 +1,10 @@
> > +// { dg-require-effective-target c++11 }
> > +
> > +#include <map>
> > +
> > +std::array<int, 10> a1; /* { dg-error "incomplete type" } */
> > +/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
> > +
> > +std::array<int, 10> a2 {5}; /* { dg-error "incomplete type" } */
> > +/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
> > +
> 

Marek


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: PING: Re: [PATCH] c++: provide #include hint for missing includes [PR110164]
  2023-06-22 15:50   ` Marek Polacek
@ 2023-06-23 16:08     ` Jason Merrill
  2023-06-23 22:01       ` [pushed] " David Malcolm
  0 siblings, 1 reply; 9+ messages in thread
From: Jason Merrill @ 2023-06-23 16:08 UTC (permalink / raw)
  To: Marek Polacek, David Malcolm; +Cc: gcc-patches

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:
>   
>> On Wed, 2023-06-14 at 20:28 -0400, David Malcolm wrote:
>>> PR c++/110164 notes that in cases where we have a forward decl
>>> of a std library type such as:
>>>
>>> std::array<int, 10> x;
>>>
>>> we omit this diagnostic:
>>>
>>> error: aggregate ‘std::array<int, 10> x’ has incomplete type and cannot be defined
>>>
>>> This patch adds this hint to the diagnostic:
>>>
>>> note: ‘std::array’ is defined in header ‘<array>’; this is probably fixable by adding ‘#include <array>’
>>>
>>> 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);
> 
> The enclosing decls omit the parameter names; if you do that, it may
> fit on one line.
> 
>>>   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) != 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),
>>> +                                       TYPE_CONTEXT (type));
> 
> Maybe CP_TYPE_CONTEXT?
> 
>>>            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),
>>> +                                       TYPE_CONTEXT (type));
> 
> Here as well.
> 
>>>            /* 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 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 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/missing-header-pr110164.C b/gcc/testsuite/g++.dg/missing-header-pr110164.C
>>> new file mode 100644
>>> index 00000000000..15980071c38
>>> --- /dev/null
>>> +++ b/gcc/testsuite/g++.dg/missing-header-pr110164.C
> 
> Can we put the new test into g++.dg/diagnostic/?

OK with the tweaks Marek suggests.

Jason


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [pushed] c++: provide #include hint for missing includes [PR110164]
  2023-06-23 16:08     ` Jason Merrill
@ 2023-06-23 22:01       ` David Malcolm
  0 siblings, 0 replies; 9+ messages in thread
From: David Malcolm @ 2023-06-23 22:01 UTC (permalink / raw)
  To: Jason Merrill, Marek Polacek; +Cc: gcc-patches, David Malcolm

> 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<int, 10> x;

we emit this diagnostic:

error: aggregate ‘std::array<int, 10> x’ has incomplete type and cannot be defined

This patch adds this hint to the diagnostic:

note: ‘std::array’ is defined in header ‘<array>’; this is probably fixable by adding ‘#include <array>’

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 <map>
+
+std::array<int, 10> a1; /* { dg-error "incomplete type" } */
+/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
+
+std::array<int, 10> a2 {5}; /* { dg-error "incomplete type" } */
+/* { dg-message "'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'" "hint" { target *-*-* } .-1 } */
+
-- 
2.26.3


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-06-23 22:01 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-15  0:28 [PATCH] c++: provide #include hint for missing includes [PR110164] David Malcolm
2023-06-15  0:36 ` Eric Gallager
2023-06-15  0:43   ` Sam James
2023-06-15 11:54     ` David Malcolm
2023-06-15 12:11       ` Sam James
2023-06-21 20:44 ` PING: " David Malcolm
2023-06-22 15:50   ` Marek Polacek
2023-06-23 16:08     ` Jason Merrill
2023-06-23 22:01       ` [pushed] " David Malcolm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).