summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcc/cp/name-lookup.c39
-rw-r--r--gcc/testsuite/g++.dg/lookup/empty.h1
-rw-r--r--gcc/testsuite/g++.dg/lookup/missing-std-include-2.C11
3 files changed, 29 insertions, 22 deletions
diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c
index cd7428a4ea4..49c4dea881a 100644
--- a/gcc/cp/name-lookup.c
+++ b/gcc/cp/name-lookup.c
@@ -4838,34 +4838,34 @@ get_std_name_hint (const char *name)
return NULL;
}
-/* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no
- suggestions to offer.
- If SCOPE is the "std" namespace, then suggest pertinent header
- files for NAME. */
+/* If SCOPE is the "std" namespace, then suggest pertinent header
+ files for NAME at LOCATION.
+ Return true iff a suggestion was offered. */
-static void
+static bool
maybe_suggest_missing_header (location_t location, tree name, tree scope)
{
if (scope == NULL_TREE)
- return;
+ return false;
if (TREE_CODE (scope) != NAMESPACE_DECL)
- return;
+ return false;
/* We only offer suggestions for the "std" namespace. */
if (scope != std_node)
- return;
+ return false;
gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
const char *name_str = IDENTIFIER_POINTER (name);
const char *header_hint = get_std_name_hint (name_str);
- if (header_hint)
- {
- gcc_rich_location richloc (location);
- maybe_add_include_fixit (&richloc, header_hint);
- inform_at_rich_loc (&richloc,
- "%<std::%s%> is defined in header %qs;"
- " did you forget to %<#include %s%>?",
- name_str, header_hint, header_hint);
- }
+ if (!header_hint)
+ return false;
+
+ gcc_rich_location richloc (location);
+ maybe_add_include_fixit (&richloc, header_hint);
+ inform_at_rich_loc (&richloc,
+ "%<std::%s%> is defined in header %qs;"
+ " did you forget to %<#include %s%>?",
+ name_str, header_hint, header_hint);
+ return true;
}
/* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
@@ -4880,6 +4880,9 @@ suggest_alternative_in_explicit_scope (location_t location, tree name,
/* Resolve any namespace aliases. */
scope = ORIGINAL_NAMESPACE (scope);
+ if (maybe_suggest_missing_header (location, name, scope))
+ return true;
+
cp_binding_level *level = NAMESPACE_LEVEL (scope);
best_match <tree, const char *> bm (name);
@@ -4895,8 +4898,6 @@ suggest_alternative_in_explicit_scope (location_t location, tree name,
fuzzy_name);
return true;
}
- else
- maybe_suggest_missing_header (location, name, scope);
return false;
}
diff --git a/gcc/testsuite/g++.dg/lookup/empty.h b/gcc/testsuite/g++.dg/lookup/empty.h
new file mode 100644
index 00000000000..a057418050e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/lookup/empty.h
@@ -0,0 +1 @@
+/* empty file for use by missing-std-include-2.C. */
diff --git a/gcc/testsuite/g++.dg/lookup/missing-std-include-2.C b/gcc/testsuite/g++.dg/lookup/missing-std-include-2.C
index ae918f869a7..51c604a9f1e 100644
--- a/gcc/testsuite/g++.dg/lookup/missing-std-include-2.C
+++ b/gcc/testsuite/g++.dg/lookup/missing-std-include-2.C
@@ -6,7 +6,12 @@
/* This is padding (to avoid the generated patch containing DejaGnu
directives). */
-#include <stdio.h>
+#include "empty.h"
+
+namespace std
+{
+ extern int sprintf (char *dst, const char *format, ...);
+};
void test (void)
{
@@ -45,11 +50,11 @@ void test_2 (void)
@@ -7,6 +7,8 @@
directives). */
- #include <stdio.h>
+ #include "empty.h"
+#include <string>
+#include <iostream>
- void test (void)
+ namespace std
{
{ dg-end-multiline-output "" }
#endif