From e1c7971b81abab836f2fc4f0b8d0f7964f8a6e13 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 6 Apr 2018 17:36:33 +0000 Subject: [PATCH] C++: more std header hints; filter on C++ dialect (PR c++/84269) This patch adds more suggestions as per: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84269#c10 some of which need C++14 and C++17, and some of which use headers that exist in earlier standards. For example, exists in C++98, but if the user attempts to use std::make_shared with -std=c++98, they are suggested to include , even if they've already included it. This patch adds the missing names, and fixes the nonsensical suggestions by detecting if the name isn't available yet, based on the user's dialect, and reporting things more intelligently: t.cc: In function 'void test_make_shared()': t.cc:5:8: error: 'make_shared' is not a member of 'std' std::make_shared(); ^~~~~~~~~~~ t.cc:5:8: note: 'std::make_shared' is only available from C++11 onwards gcc/cp/ChangeLog: PR c++/84269 * name-lookup.c (struct std_name_hint): Move out of get_std_name_hint; add field "min_dialect". (get_std_name_hint): Add min_dialect values to all initializers. Add , , , , , , , , , , , , , , and . Add fstream, ifstream, and ofstream to . Add istringstream, ostringstream, and stringstream to . Add basic_string to . Add tuple_element and tuple_size to . Add declval to . Fix ordering of and . Return a std_name_hint, rather than a const char *. (get_cxx_dialect_name): New function. (maybe_suggest_missing_std_header): Detect names that aren't yet available in the current dialect, and instead of suggesting a missing #include, warn about the dialect. gcc/testsuite/ChangeLog: PR c++/84269 * g++.dg/lookup/missing-std-include-6.C: Move std::array and std::tuple here since they need C++11. * g++.dg/lookup/missing-std-include-8.C: New test. * g++.dg/lookup/missing-std-include.C: Move std::array and std::tuple test to missing-std-include-6.C to avoid failures with C++98. From-SVN: r259184 --- gcc/cp/ChangeLog | 21 ++ gcc/cp/name-lookup.c | 262 +++++++++++++++------ gcc/testsuite/ChangeLog | 10 + .../g++.dg/lookup/missing-std-include-6.C | 13 + .../g++.dg/lookup/missing-std-include-8.C | 44 ++++ gcc/testsuite/g++.dg/lookup/missing-std-include.C | 7 - 6 files changed, 279 insertions(+), 78 deletions(-) create mode 100644 gcc/testsuite/g++.dg/lookup/missing-std-include-8.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 119741aa994..e71b4fbe9d8 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,24 @@ +2018-04-06 David Malcolm + + PR c++/84269 + * name-lookup.c (struct std_name_hint): Move out of + get_std_name_hint; add field "min_dialect". + (get_std_name_hint): Add min_dialect values to all initializers. + Add , , , , , + , , , , , , + , , , and . + Add fstream, ifstream, and ofstream to . + Add istringstream, ostringstream, and stringstream to . + Add basic_string to . + Add tuple_element and tuple_size to . + Add declval to . + Fix ordering of and . + Return a std_name_hint, rather than a const char *. + (get_cxx_dialect_name): New function. + (maybe_suggest_missing_std_header): Detect names that aren't yet + available in the current dialect, and instead of suggesting a + missing #include, warn about the dialect. + 2018-04-06 Jakub Jelinek PR c++/85210 diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index dc7699d5366..ca776844c9c 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -5469,104 +5469,214 @@ suggest_alternatives_for (location_t location, tree name, } } +/* A well-known name within the C++ standard library, returned by + get_std_name_hint. */ + +struct std_name_hint +{ + /* A name within "std::". */ + const char *name; + + /* The header name defining it within the C++ Standard Library + (with '<' and '>'). */ + const char *header; + + /* The dialect of C++ in which this was added. */ + enum cxx_dialect min_dialect; +}; + /* Subroutine of maybe_suggest_missing_header for handling unrecognized names for some of the most common names within "std::". - Given non-NULL NAME, a name for lookup within "std::", return the header - name defining it within the C++ Standard Library (with '<' and '>'), - or NULL. */ + Given non-NULL NAME, return the std_name_hint for it, or NULL. */ -static const char * +static const std_name_hint * get_std_name_hint (const char *name) { - struct std_name_hint - { - const char *name; - const char *header; - }; static const std_name_hint hints[] = { + /* . */ + {"any", "", cxx17}, + {"any_cast", "", cxx17}, + {"make_any", "", cxx17}, /* . */ - {"array", ""}, // C++11 + {"array", "", cxx11}, + /* . */ + {"atomic", "", cxx11}, + {"atomic_flag", "", cxx11}, + /* . */ + {"bitset", "", cxx11}, /* . */ - {"complex", ""}, - {"complex_literals", ""}, + {"complex", "", cxx98}, + {"complex_literals", "", cxx98}, + /* . */ + {"condition_variable", "", cxx11}, + {"condition_variable_any", "", cxx11}, /* . */ - {"deque", ""}, + {"deque", "", cxx98}, /* . */ - {"forward_list", ""}, // C++11 + {"forward_list", "", cxx11}, /* . */ - {"basic_filebuf", ""}, - {"basic_ifstream", ""}, - {"basic_ofstream", ""}, - {"basic_fstream", ""}, + {"basic_filebuf", "", cxx98}, + {"basic_ifstream", "", cxx98}, + {"basic_ofstream", "", cxx98}, + {"basic_fstream", "", cxx98}, + {"fstream", "", cxx98}, + {"ifstream", "", cxx98}, + {"ofstream", "", cxx98}, + /* . */ + {"bind", "", cxx11}, + {"function", "", cxx11}, + {"hash", "", cxx11}, + {"mem_fn", "", cxx11}, + /* . */ + {"async", "", cxx11}, + {"future", "", cxx11}, + {"packaged_task", "", cxx11}, + {"promise", "", cxx11}, /* . */ - {"cin", ""}, - {"cout", ""}, - {"cerr", ""}, - {"clog", ""}, - {"wcin", ""}, - {"wcout", ""}, - {"wclog", ""}, + {"cin", "", cxx98}, + {"cout", "", cxx98}, + {"cerr", "", cxx98}, + {"clog", "", cxx98}, + {"wcin", "", cxx98}, + {"wcout", "", cxx98}, + {"wclog", "", cxx98}, + /* . */ + {"istream", "", cxx98}, + /* . */ + {"advance", "", cxx98}, + {"back_inserter", "", cxx98}, + {"begin", "", cxx11}, + {"distance", "", cxx98}, + {"end", "", cxx11}, + {"front_inserter", "", cxx98}, + {"inserter", "", cxx98}, + {"istream_iterator", "", cxx98}, + {"istreambuf_iterator", "", cxx98}, + {"iterator_traits", "", cxx98}, + {"move_iterator", "", cxx11}, + {"next", "", cxx11}, + {"ostream_iterator", "", cxx98}, + {"ostreambuf_iterator", "", cxx98}, + {"prev", "", cxx11}, + {"reverse_iterator", "", cxx98}, + /* . */ + {"ostream", "", cxx98}, /* . */ - {"list", ""}, + {"list", "", cxx98}, /* . */ - {"map", ""}, - {"multimap", ""}, + {"map", "", cxx98}, + {"multimap", "", cxx98}, /* . */ - {"make_shared", ""}, - {"make_unique", ""}, - {"shared_ptr", ""}, - {"unique_ptr", ""}, - {"weak_ptr", ""}, - /* . */ - {"queue", ""}, - {"priority_queue", ""}, + {"make_shared", "", cxx11}, + {"make_unique", "", cxx11}, + {"shared_ptr", "", cxx11}, + {"unique_ptr", "", cxx11}, + {"weak_ptr", "", cxx11}, + /* . */ + {"mutex", "", cxx11}, + {"timed_mutex", "", cxx11}, + {"recursive_mutex", "", cxx11}, + {"recursive_timed_mutex", "", cxx11}, + {"once_flag", "", cxx11}, + {"call_once,", "", cxx11}, + {"lock", "", cxx11}, + {"scoped_lock", "", cxx17}, + {"try_lock", "", cxx11}, + {"lock_guard", "", cxx11}, + {"unique_lock", "", cxx11}, + /* . */ + {"optional", "", cxx17}, + {"make_optional", "", cxx17}, /* . */ - {"ostream", ""}, - {"wostream", ""}, - {"ends", ""}, - {"flush", ""}, - {"endl", ""}, + {"ostream", "", cxx98}, + {"wostream", "", cxx98}, + {"ends", "", cxx98}, + {"flush", "", cxx98}, + {"endl", "", cxx98}, + /* . */ + {"queue", "", cxx98}, + {"priority_queue", "", cxx98}, /* . */ - {"set", ""}, - {"multiset", ""}, + {"set", "", cxx98}, + {"multiset", "", cxx98}, + /* . */ + {"shared_lock", "", cxx14}, + {"shared_mutex", "", cxx17}, + {"shared_timed_mutex", "", cxx14}, /* . */ - {"basic_stringbuf", ""}, - {"basic_istringstream", ""}, - {"basic_ostringstream", ""}, - {"basic_stringstream", ""}, + {"basic_stringbuf", "", cxx98}, + {"basic_istringstream", "", cxx98}, + {"basic_ostringstream", "", cxx98}, + {"basic_stringstream", "", cxx98}, + {"istringstream", "", cxx98}, + {"ostringstream", "", cxx98}, + {"stringstream", "", cxx98}, /* . */ - {"stack", ""}, - /* . */ - {"make_tuple", ""}, - {"tuple", ""}, + {"stack", "", cxx98}, /* . */ - {"string", ""}, - {"wstring", ""}, - {"u16string", ""}, - {"u32string", ""}, + {"basic_string", "", cxx98}, + {"string", "", cxx98}, + {"wstring", "", cxx98}, + {"u16string", "", cxx11}, + {"u32string", "", cxx11}, + /* . */ + {"string_view", "", cxx17}, + /* . */ + {"thread", "", cxx11}, + /* . */ + {"make_tuple", "", cxx11}, + {"tuple", "", cxx11}, + {"tuple_element", "", cxx11}, + {"tuple_size", "", cxx11}, /* . */ - {"unordered_map", ""}, // C++11 - {"unordered_multimap", ""}, // C++11 + {"unordered_map", "", cxx11}, + {"unordered_multimap", "", cxx11}, /* . */ - {"unordered_set", ""}, // C++11 - {"unordered_multiset", ""}, // C++11 + {"unordered_set", "", cxx11}, + {"unordered_multiset", "", cxx11}, /* . */ - {"forward", ""}, - {"make_pair", ""}, - {"move", ""}, - {"pair", ""}, + {"declval", "", cxx11}, + {"forward", "", cxx11}, + {"make_pair", "", cxx98}, + {"move", "", cxx11}, + {"pair", "", cxx98}, + /* . */ + {"variant", "", cxx17}, + {"visit", "", cxx17}, /* . */ - {"vector", ""}, + {"vector", "", cxx98}, }; const size_t num_hints = sizeof (hints) / sizeof (hints[0]); for (size_t i = 0; i < num_hints; i++) { if (strcmp (name, hints[i].name) == 0) - return hints[i].header; + return &hints[i]; } return NULL; } +/* Describe DIALECT. */ + +static const char * +get_cxx_dialect_name (enum cxx_dialect dialect) +{ + switch (dialect) + { + default: + gcc_unreachable (); + case cxx98: + return "C++98"; + case cxx11: + return "C++11"; + case cxx14: + return "C++14"; + case cxx17: + return "C++17"; + case cxx2a: + return "C++2a"; + } +} + /* Suggest pertinent header files for NAME at LOCATION, for common names within the "std" namespace. Return true iff a suggestion was offered. */ @@ -5577,16 +5687,26 @@ maybe_suggest_missing_std_header (location_t location, tree name) gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); const char *name_str = IDENTIFIER_POINTER (name); - const char *header_hint = get_std_name_hint (name_str); + const std_name_hint *header_hint = get_std_name_hint (name_str); if (!header_hint) return false; gcc_rich_location richloc (location); - maybe_add_include_fixit (&richloc, header_hint); - inform (&richloc, - "% is defined in header %qs;" - " did you forget to %<#include %s%>?", - name_str, header_hint, header_hint); + if (cxx_dialect >= header_hint->min_dialect) + { + const char *header = header_hint->header; + maybe_add_include_fixit (&richloc, header); + inform (&richloc, + "% is defined in header %qs;" + " did you forget to %<#include %s%>?", + name_str, header, header); + } + else + { + inform (&richloc, + "% is only available from %s onwards", + name_str, get_cxx_dialect_name (header_hint->min_dialect)); + } return true; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 4ab768d2fc4..e92ad3c241a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,13 @@ +2018-04-06 David Malcolm + + PR c++/84269 + * g++.dg/lookup/missing-std-include-6.C: Move std::array and + std::tuple here since they need C++11. + * g++.dg/lookup/missing-std-include-8.C: New test. + * g++.dg/lookup/missing-std-include.C: Move std::array and + std::tuple test to missing-std-include-6.C to avoid failures + with C++98. + 2018-04-06 Jakub Jelinek PR debug/85252 diff --git a/gcc/testsuite/g++.dg/lookup/missing-std-include-6.C b/gcc/testsuite/g++.dg/lookup/missing-std-include-6.C index 100bcc0aa0a..d9eeb4284e8 100644 --- a/gcc/testsuite/g++.dg/lookup/missing-std-include-6.C +++ b/gcc/testsuite/g++.dg/lookup/missing-std-include-6.C @@ -60,3 +60,16 @@ void test_move(T&& arg) // { dg-message "'#include '" "" { target *-*-* } .-1 } // { dg-error "expected primary-expression before '>' token" "" { target *-*-* } .-2 } } + +void test_array () +{ + std::array a; // { dg-error ".array. is not a member of .std." } + // { dg-message ".std::array. is defined in header ..; did you forget to .#include .?" "" { target *-*-* } .-1 } +} + +void test_tuple () +{ + std::tuple p; // { dg-error ".tuple. is not a member of .std." } + // { dg-message ".std::tuple. is defined in header ..; did you forget to .#include .?" "" { target *-*-* } .-1 } + // { dg-error "expected primary-expression before .int." "" { target *-*-* } .-2 } +} diff --git a/gcc/testsuite/g++.dg/lookup/missing-std-include-8.C b/gcc/testsuite/g++.dg/lookup/missing-std-include-8.C new file mode 100644 index 00000000000..68b208299f2 --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/missing-std-include-8.C @@ -0,0 +1,44 @@ +/* Verify that we don't offer #include suggestions for things that + aren't yet available due to the C++ dialect in use. */ +// { dg-do compile { target c++98_only } } + +#include + +template +void test_make_shared () +{ + std::make_shared(); // { dg-error "'make_shared' is not a member of 'std'" } + // { dg-message "'std::make_shared' is only available from C\\+\\+11 onwards" "" { target *-*-* } .-1 } + // { dg-error "expected primary-expression before '>' token" "" { target *-*-* } .-2 } + // { dg-error "expected primary-expression before '\\)' token" "" { target *-*-* } .-3 } +} + +void test_array () +{ + std::array a; // { dg-error "'array' is not a member of 'std'" } + // { dg-message "'std::array' is only available from C\\+\\+11 onwards" "" { target *-*-* } .-1 } +} + +void test_tuple () +{ + std::tuple p; // { dg-error "'tuple' is not a member of 'std'" } + // { dg-message "'std::tuple' is only available from C\\+\\+11 onwards" "" { target *-*-* } .-1 } + // { dg-error "expected primary-expression before 'int'" "" { target *-*-* } .-2 } +} + +/* Since C++14. */ +std::shared_timed_mutex m; // { dg-error "'shared_timed_mutex' in namespace 'std' does not name a type" } +// { dg-message "'std::shared_timed_mutex' is only available from C\\+\\+14 onwards" "" { target *-*-* } .-1 } + +/* Since C++17: */ +std::string_view sv; // { dg-error "'string_view' in namespace 'std' does not name a type" } +// { dg-message "'std::string_view' is only available from C\\+\\+17 onwards" "" { target *-*-* } .-1 } + +/* Verify interaction with "using namespace std;". */ +using namespace std; +void test_via_using_directive () +{ + shared_ptr p; // { dg-error "'shared_ptr' was not declared in this scope" } + // { dg-message "'std::shared_ptr' is only available from C\\+\\+11 onwards" "" { target *-*-* } .-1 } + // { dg-error "expected primary-expression before 'int'" "" { target *-*-* } .-2 } +} diff --git a/gcc/testsuite/g++.dg/lookup/missing-std-include.C b/gcc/testsuite/g++.dg/lookup/missing-std-include.C index 54527602eb8..0fcc72b7d30 100644 --- a/gcc/testsuite/g++.dg/lookup/missing-std-include.C +++ b/gcc/testsuite/g++.dg/lookup/missing-std-include.C @@ -13,9 +13,6 @@ void test (void) std::cin >> i; // { dg-error ".cin. is not a member of .std." } // { dg-message ".std::cin. is defined in header ..; did you forget to .#include .?" "" { target *-*-* } .-1 } - std::array a; // { dg-error ".array. is not a member of .std." } - // { dg-message ".std::array. is defined in header ..; did you forget to .#include .?" "" { target *-*-* } .-1 } - std::deque a; // { dg-error ".deque. is not a member of .std." } // { dg-message ".std::deque. is defined in header ..; did you forget to .#include .?" "" { target *-*-* } .-1 } @@ -30,8 +27,4 @@ void test (void) std::pair p; // { dg-error ".pair. is not a member of .std." } // { dg-message ".std::pair. is defined in header ..; did you forget to .#include .?" "" { target *-*-* } .-1 } // { dg-error "expected primary-expression before .int." "" { target *-*-* } .-2 } - - std::tuple p; // { dg-error ".tuple. is not a member of .std." } - // { dg-message ".std::tuple. is defined in header ..; did you forget to .#include .?" "" { target *-*-* } .-1 } - // { dg-error "expected primary-expression before .int." "" { target *-*-* } .-2 } } -- 2.11.4.GIT