]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: provide #include hint for missing includes [PR110164]
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 23 Jun 2023 21:56:14 +0000 (17:56 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Fri, 23 Jun 2023 21:56:14 +0000 (17:56 -0400)
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.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/cp/cp-name-hint.h
gcc/cp/decl.cc
gcc/cp/name-lookup.cc
gcc/testsuite/g++.dg/diagnostic/missing-header-pr110164.C [new file with mode: 0644]

index bfa7c53c8f69991101dee0b3031710db2e6ff5ed..7693980138a5f804c4ec308ca2f986bdec88dd4d 100644 (file)
@@ -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);
 
index c07a4a8d58d5bb4272eb91636eeb64296e544e8e..60f107d50c4c546fdf9bff09fedcfa2f2b3c7aca 100644 (file)
@@ -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;
index 53b6870f067b8fc637d8c7bc4bf99e84cdf94746..74565184403c1b4a378614a77a38cbb2f5786464 100644 (file)
@@ -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 (file)
index 0000000..1598007
--- /dev/null
@@ -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 } */
+