Commit f661e57e by David Malcolm Committed by David Malcolm

C++: fix ordering of missing std #include suggestion (PR c++/81514)

gcc/cp/ChangeLog:
	PR c++/81514
	* name-lookup.c (maybe_suggest_missing_header): Convert return
	type from void to bool; return true iff a suggestion was offered.
	(suggest_alternative_in_explicit_scope): Move call to
	maybe_suggest_missing_header to before use of best_match, and
	return true if the former offers a suggestion.

gcc/testsuite/ChangeLog:
	PR c++/81514
	* g++.dg/lookup/empty.h: New file.
	* g++.dg/lookup/missing-std-include-2.C: Replace include of
	stdio.h with empty.h and a declaration of a "std::sprintf" not based
	on a built-in.

From-SVN: r251186
parent 676c4146
2017-08-18 David Malcolm <dmalcolm@redhat.com>
PR c++/81514
* name-lookup.c (maybe_suggest_missing_header): Convert return
type from void to bool; return true iff a suggestion was offered.
(suggest_alternative_in_explicit_scope): Move call to
maybe_suggest_missing_header to before use of best_match, and
return true if the former offers a suggestion.
2017-08-18 H.J. Lu <hongjiu.lu@intel.com> 2017-08-18 H.J. Lu <hongjiu.lu@intel.com>
PR c/53037 PR c/53037
......
...@@ -4838,34 +4838,34 @@ get_std_name_hint (const char *name) ...@@ -4838,34 +4838,34 @@ get_std_name_hint (const char *name)
return NULL; return NULL;
} }
/* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no /* If SCOPE is the "std" namespace, then suggest pertinent header
suggestions to offer. files for NAME at LOCATION.
If SCOPE is the "std" namespace, then suggest pertinent header Return true iff a suggestion was offered. */
files for NAME. */
static void static bool
maybe_suggest_missing_header (location_t location, tree name, tree scope) maybe_suggest_missing_header (location_t location, tree name, tree scope)
{ {
if (scope == NULL_TREE) if (scope == NULL_TREE)
return; return false;
if (TREE_CODE (scope) != NAMESPACE_DECL) if (TREE_CODE (scope) != NAMESPACE_DECL)
return; return false;
/* We only offer suggestions for the "std" namespace. */ /* We only offer suggestions for the "std" namespace. */
if (scope != std_node) if (scope != std_node)
return; return false;
gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
const char *name_str = IDENTIFIER_POINTER (name); const char *name_str = IDENTIFIER_POINTER (name);
const char *header_hint = get_std_name_hint (name_str); const char *header_hint = get_std_name_hint (name_str);
if (header_hint) if (!header_hint)
{ return false;
gcc_rich_location richloc (location);
maybe_add_include_fixit (&richloc, header_hint); gcc_rich_location richloc (location);
inform_at_rich_loc (&richloc, maybe_add_include_fixit (&richloc, header_hint);
"%<std::%s%> is defined in header %qs;" inform_at_rich_loc (&richloc,
" did you forget to %<#include %s%>?", "%<std::%s%> is defined in header %qs;"
name_str, header_hint, header_hint); " 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 /* 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, ...@@ -4880,6 +4880,9 @@ suggest_alternative_in_explicit_scope (location_t location, tree name,
/* Resolve any namespace aliases. */ /* Resolve any namespace aliases. */
scope = ORIGINAL_NAMESPACE (scope); scope = ORIGINAL_NAMESPACE (scope);
if (maybe_suggest_missing_header (location, name, scope))
return true;
cp_binding_level *level = NAMESPACE_LEVEL (scope); cp_binding_level *level = NAMESPACE_LEVEL (scope);
best_match <tree, const char *> bm (name); best_match <tree, const char *> bm (name);
...@@ -4895,8 +4898,6 @@ suggest_alternative_in_explicit_scope (location_t location, tree name, ...@@ -4895,8 +4898,6 @@ suggest_alternative_in_explicit_scope (location_t location, tree name,
fuzzy_name); fuzzy_name);
return true; return true;
} }
else
maybe_suggest_missing_header (location, name, scope);
return false; return false;
} }
......
2017-08-18 David Malcolm <dmalcolm@redhat.com>
PR c++/81514
* g++.dg/lookup/empty.h: New file.
* g++.dg/lookup/missing-std-include-2.C: Replace include of
stdio.h with empty.h and a declaration of a "std::sprintf" not based
on a built-in.
2017-08-18 H.J. Lu <hongjiu.lu@intel.com> 2017-08-18 H.J. Lu <hongjiu.lu@intel.com>
PR c/53037 PR c/53037
......
/* empty file for use by missing-std-include-2.C. */
...@@ -6,7 +6,12 @@ ...@@ -6,7 +6,12 @@
/* This is padding (to avoid the generated patch containing DejaGnu /* This is padding (to avoid the generated patch containing DejaGnu
directives). */ directives). */
#include <stdio.h> #include "empty.h"
namespace std
{
extern int sprintf (char *dst, const char *format, ...);
};
void test (void) void test (void)
{ {
...@@ -45,11 +50,11 @@ void test_2 (void) ...@@ -45,11 +50,11 @@ void test_2 (void)
@@ -7,6 +7,8 @@ @@ -7,6 +7,8 @@
directives). */ directives). */
#include <stdio.h> #include "empty.h"
+#include <string> +#include <string>
+#include <iostream> +#include <iostream>
void test (void) namespace std
{ {
{ dg-end-multiline-output "" } { dg-end-multiline-output "" }
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment