From 62e98ef1a23b06c939d5fde19ac65d77a5922ccd Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 29 Mar 2018 20:43:48 +0000 Subject: [PATCH] More #include suggestions (PR c++/84269) PR c++/84269 reports a number of names in the C and C++ standard libraries for which we don't yet offer #include fix-it hints. This patch adds them (up to comment #9). gcc/c-family/ChangeLog: PR c++/84269 * known-headers.cc (get_stdlib_header_for_name): Add various names from , , and ; add more names from . gcc/cp/ChangeLog: PR c++/84269 * name-lookup.c (get_std_name_hint): Add names from , , and . gcc/testsuite/ChangeLog: PR c++/84269 * g++.dg/lookup/missing-std-include-6.C: New test. * g++.dg/lookup/missing-std-include.C: Add std::pair and std::tuple tests. * g++.dg/spellcheck-reswords.C: Expect a hint about . * g++.dg/spellcheck-stdlib.C: Add tests for names in , , , and . From-SVN: r258966 --- gcc/c-family/ChangeLog | 7 ++ gcc/c-family/known-headers.cc | 33 ++++++++- gcc/cp/ChangeLog | 6 ++ gcc/cp/name-lookup.c | 14 ++++ gcc/testsuite/ChangeLog | 10 +++ .../g++.dg/lookup/missing-std-include-6.C | 62 ++++++++++++++++ .../g++.dg/lookup/missing-std-include.C | 8 ++ gcc/testsuite/g++.dg/spellcheck-reswords.C | 1 + gcc/testsuite/g++.dg/spellcheck-stdlib.C | 73 +++++++++++++++++++ 9 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/lookup/missing-std-include-6.C diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index e3558d7332f..27245b7e60d 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,10 @@ +2018-03-29 David Malcolm + + PR c++/84269 + * known-headers.cc (get_stdlib_header_for_name): Add various names + from , , and ; add more names from + . + 2018-03-27 Jakub Jelinek PR c++/85061 diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc index ef23cbea20b..5524d216318 100644 --- a/gcc/c-family/known-headers.cc +++ b/gcc/c-family/known-headers.cc @@ -57,6 +57,9 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib) gcc_assert (lib < NUM_STDLIBS); static const stdlib_hint hints[] = { + /* and . */ + {"assert", {"", ""} }, + /* and . */ {"errno", {"", ""} }, @@ -92,16 +95,44 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib) {"size_t", {"", ""} }, {"wchar_t", {"", NULL /* a keyword in C++ */} }, - /* . */ + /* and . */ {"BUFSIZ", {"", ""} }, {"EOF", {"", ""} }, {"FILE", {"", ""} }, {"FILENAME_MAX", {"", ""} }, + {"fopen", {"", ""} }, {"fpos_t", {"", ""} }, + {"getchar", {"", ""} }, + {"printf", {"", ""} }, + {"snprintf", {"", ""} }, + {"sprintf", {"", ""} }, {"stderr", {"", ""} }, {"stdin", {"", ""} }, {"stdout", {"", ""} }, + /* and . */ + {"free", {"", ""} }, + {"malloc", {"", ""} }, + {"realloc", {"", ""} }, + + /* and . */ + {"memchr", {"", ""} }, + {"memcmp", {"", ""} }, + {"memcpy", {"", ""} }, + {"memmove", {"", ""} }, + {"memset", {"", ""} }, + {"strcat", {"", ""} }, + {"strchr", {"", ""} }, + {"strcmp", {"", ""} }, + {"strcpy", {"", ""} }, + {"strlen", {"", ""} }, + {"strncat", {"", ""} }, + {"strncmp", {"", ""} }, + {"strncpy", {"", ""} }, + {"strrchr", {"", ""} }, + {"strspn", {"", ""} }, + {"strstr", {"", ""} }, + /* . */ {"PTRDIFF_MAX", {"", ""} }, {"PTRDIFF_MIN", {"", ""} }, diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 1536c359fe8..77c729c979e 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2018-03-29 David Malcolm + + PR c++/84269 + * name-lookup.c (get_std_name_hint): Add names from , + , and . + 2018-03-29 Jason Merrill PR c++/85093 - too many template args with pack expansion. diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index e193b3bc1de..061729a989b 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -5453,6 +5453,12 @@ get_std_name_hint (const char *name) /* . */ {"map", ""}, {"multimap", ""}, + /* . */ + {"make_shared", ""}, + {"make_unique", ""}, + {"shared_ptr", ""}, + {"unique_ptr", ""}, + {"weak_ptr", ""}, /* . */ {"queue", ""}, {"priority_queue", ""}, @@ -5472,6 +5478,9 @@ get_std_name_hint (const char *name) {"basic_stringstream", ""}, /* . */ {"stack", ""}, + /* . */ + {"make_tuple", ""}, + {"tuple", ""}, /* . */ {"string", ""}, {"wstring", ""}, @@ -5483,6 +5492,11 @@ get_std_name_hint (const char *name) /* . */ {"unordered_set", ""}, // C++11 {"unordered_multiset", ""}, // C++11 + /* . */ + {"forward", ""}, + {"make_pair", ""}, + {"move", ""}, + {"pair", ""}, /* . */ {"vector", ""}, }; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index bc545f021ed..a8906ddbc98 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,13 @@ +2018-03-29 David Malcolm + + PR c++/84269 + * g++.dg/lookup/missing-std-include-6.C: New test. + * g++.dg/lookup/missing-std-include.C: Add std::pair and + std::tuple tests. + * g++.dg/spellcheck-reswords.C: Expect a hint about . + * g++.dg/spellcheck-stdlib.C: Add tests for names in , + , , and . + 2018-03-29 Vladimir Makarov PR inline-asm/84985 diff --git a/gcc/testsuite/g++.dg/lookup/missing-std-include-6.C b/gcc/testsuite/g++.dg/lookup/missing-std-include-6.C new file mode 100644 index 00000000000..100bcc0aa0a --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/missing-std-include-6.C @@ -0,0 +1,62 @@ +// { dg-do compile { target c++11 } } + +/* . */ + +template +void test_make_shared () +{ + auto p = std::make_shared(); // { dg-error "'make_shared' is not a member of 'std'" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + // { dg-error "expected primary-expression before '>' token" "" { target *-*-* } .-2 } + // { dg-error "expected primary-expression before '\\)' token" "" { target *-*-* } .-3 } +} + +template +void test_make_unique () +{ + auto p = std::make_unique(); // { dg-error "'make_unique' is not a member of 'std'" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + // { dg-error "expected primary-expression before '>' token" "" { target *-*-* } .-2 } + // { dg-error "expected primary-expression before '\\)' token" "" { target *-*-* } .-3 } +} + +std::shared_ptr test_shared_ptr; // { dg-error "'shared_ptr' in namespace 'std' does not name a template type" } +// { dg-message "'#include '" "" { target *-*-* } .-1 } + +std::unique_ptr test_unique_ptr; // { dg-error "'unique_ptr' in namespace 'std' does not name a template type" } +// { dg-message "'#include '" "" { target *-*-* } .-1 } + +std::weak_ptr test_weak_ptr; // { dg-error "'weak_ptr' in namespace 'std' does not name a template type" } +// { dg-message "'#include '" "" { target *-*-* } .-1 } + +/* . */ + +void test_make_tuple (int i, int j, int k) +{ + auto t = std::make_tuple (i, j, k); // { dg-error "'make_tuple' is not a member of 'std'" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } +} + +/* . */ + +template +void test_forward(T&& arg) +{ + std::forward(arg); // { dg-error "'forward' is not a member of 'std'" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + // { dg-error "expected primary-expression before '>' token" "" { target *-*-* } .-2 } +} + +void test_make_pair (int i, int j) +{ + auto p = std::make_pair (i, j); // { dg-error "'make_pair' is not a member of 'std'" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } +} + +template +void test_move(T&& arg) +{ + std::move(arg); // { dg-error "'move' is not a member of 'std'" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + // { dg-error "expected primary-expression before '>' token" "" { 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 82f994f0f99..54527602eb8 100644 --- a/gcc/testsuite/g++.dg/lookup/missing-std-include.C +++ b/gcc/testsuite/g++.dg/lookup/missing-std-include.C @@ -26,4 +26,12 @@ void test (void) std::list lst; // { dg-error ".list. is not a member of .std." } // { dg-message ".std::list. is defined in header ..; did you forget to .#include .?" "" { target *-*-* } .-1 } // { dg-error "expected primary-expression before .int." "" { target *-*-* } .-2 } + + 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 } } diff --git a/gcc/testsuite/g++.dg/spellcheck-reswords.C b/gcc/testsuite/g++.dg/spellcheck-reswords.C index db6104b0671..0687666744a 100644 --- a/gcc/testsuite/g++.dg/spellcheck-reswords.C +++ b/gcc/testsuite/g++.dg/spellcheck-reswords.C @@ -8,4 +8,5 @@ void pr80567 (void *p) { memset (p, 0, 4); // { dg-error "not declared" } // { dg-bogus "'else'" "" { target *-*-*} .-1 } + // { dg-message "'#include '" "" { target *-*-*} .-2 } } diff --git a/gcc/testsuite/g++.dg/spellcheck-stdlib.C b/gcc/testsuite/g++.dg/spellcheck-stdlib.C index c7a66262479..11a4e3e8c41 100644 --- a/gcc/testsuite/g++.dg/spellcheck-stdlib.C +++ b/gcc/testsuite/g++.dg/spellcheck-stdlib.C @@ -35,6 +35,21 @@ void test_cstdio (void) EOF; // { dg-error "'EOF' was not declared" } // { dg-message "'EOF' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } + + fopen ("test.txt"); // { dg-error "'fopen' was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + + printf ("test\n"); // { dg-error "'printf' was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + + char tmp[16]; + sprintf (tmp, "test\n"); // { dg-error "'sprintf' was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + snprintf (tmp, 16, "test\n"); // { dg-error "'snprintf' was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + + getchar (); // { dg-error "'getchar' was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } } /* Missing . */ @@ -62,6 +77,64 @@ int test_INT_MAX (void) // { dg-message "'INT_MAX' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } INT_MAX_line } } +/* Missing . */ + +void test_cstring (char *dest, char *src) +{ + memchr(dest, 'a', 4); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + memcmp(dest, src, 4); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + memcpy(dest, src, 4); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + memmove(dest, src, 4); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + memset(dest, 'a', 4); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strcat(dest, "test"); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strchr("test", 'e'); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strcmp(dest, "test"); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strcpy(dest, "test"); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strlen("test"); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strncat(dest, "test", 3); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strncmp(dest, "test", 3); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strncpy(dest, "test", 3); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strrchr("test", 'e'); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strspn(dest, "test"); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + strstr(dest, "test"); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } +} + +/* Missing . */ + +void test_cassert (int a, int b) +{ + assert (a == b); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } +} + +/* Missing . */ + +void test_cstdlib (void *q) +{ + void *ptr = malloc (64); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + free (ptr); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } + q = realloc (q, 1024); // { dg-error "was not declared" } + // { dg-message "'#include '" "" { target *-*-* } .-1 } +} + /* Verify that we don't offer suggestions to stdlib globals names when there's an explicit namespace. */ -- 2.30.2