C++: suggest missing headers for implicit use of "std" (PR c++/85021)
We provide fix-it hints for the most common "std" names when an explicit
"std::" prefix is present, however we don't yet provide fix-it hints for
this implicit case:
using namespace std;
void f() { cout << "test"; }
for which we emit:
t.cc: In function 'void f()':
t.cc:2:13: error: 'cout' was not declared in this scope
void f() { cout << "test"; }
^~~~
This patch detects if a "using namespace std;" directive is present
in the current namespace, and if so, offers a suggestion for
unrecognized names that are in our list of common "std" names:
t.cc: In function 'void f()':
t.cc:2:13: error: 'cout' was not declared in this scope
void f() { cout << "test"; }
^~~~
t.cc:2:13: note: 'std::cout' is defined in header '<iostream>'; did you forget to '#include <iostream>'?
+#include <iostream>
using namespace std;
void f() { cout << "test"; }
^~~~
gcc/cp/ChangeLog:
PR c++/85021
* name-lookup.c (using_directives_contain_std_p): New function.
(has_using_namespace_std_directive_p): New function.
(suggest_alternatives_for): Simplify if/else logic using early
returns. If no candidates were found, and there's a
"using namespace std;" directive, call
maybe_suggest_missing_std_header.
(maybe_suggest_missing_header): Split later part of the function
into..
(maybe_suggest_missing_std_header): New.
gcc/testsuite/ChangeLog:
PR c++/85021
* g++.dg/lookup/missing-std-include-7.C: New test.
From-SVN: r259179