aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family/known-headers.h
AgeCommit message (Collapse)AuthorFilesLines
2020-06-04Provide diagnostic hints for missing C++ cinttypes string constants.Mark Wielaard1-0/+1
When reporting an error in cp_parser and we notice a string literal followed by an unknown name check whether there is a known standard header containing a string macro with the same name, then add a hint to the error message to include that header. gcc/c-family/ChangeLog: * known-headers.cc (get_cp_stdlib_header_for_string_macro_name): New function. * known-headers.h (get_cp_stdlib_header_for_string_macro_name): New function declaration. gcc/cp/ChangeLog: * parser.c (cp_lexer_safe_previous_token): New function. (cp_parser_error_1): Add name_hint if the previous token is a string literal and next token is a CPP_NAME and we have a missing header suggestion for the name. gcc/testsuite/ChangeLog: * g++.dg/spellcheck-inttypes.C: Add string-literal testcases.
2020-06-04Provide diagnostic hints for missing C inttypes.h string constants.Mark Wielaard1-0/+2
This adds a flag to c_parser so we know when we were trying to construct a string literal. If there is a parse error and we were constructing a string literal, and the next token is an unknown identifier name, and we know there is a standard header that defines that name as a string literal, then add a missing header hint to the error messsage. The list of macro names are also used when providing a hint for missing identifiers. gcc/c-family/ChangeLog: * known-headers.cc (get_string_macro_hint): New function. (get_stdlib_header_for_name): Use get_string_macro_hint. (get_c_stdlib_header_for_string_macro_name): New function. * known-headers.h (get_c_stdlib_header_for_string_macro_name): New function declaration. gcc/c/ChangeLog: * c-parser.c (struct c_parser): Add seen_string_literal bitfield. (c_parser_consume_token): Reset seen_string_literal. (c_parser_error_richloc): Add name_hint if seen_string_literal and next token is a CPP_NAME and we have a missing header suggestion for the name. (c_parser_string_literal): Set seen_string_literal. gcc/testsuite/ChangeLog: * gcc.dg/spellcheck-inttypes.c: New test. * g++.dg/spellcheck-inttypes.C: Likewise.
2020-01-01Update copyright years.Jakub Jelinek1-1/+1
From-SVN: r279813
2019-01-01Update copyright years.Jakub Jelinek1-1/+1
From-SVN: r267494
2018-01-03Update copyright years.Jakub Jelinek1-1/+1
From-SVN: r256169
2017-11-21C/C++: more stdlib header hints (PR c/81404)David Malcolm1-0/+41
This patch extends the C frontend's "knowledge" of the C stdlib within get_c_name_hint to cover some more macros and functions, covering a case reported in PR c/81404 ("INT_MAX"), so that rather than printing: t.c:5:12: error: 'INT_MAX' undeclared here (not in a function); did you mean '__INT_MAX__'? int test = INT_MAX; ^~~~~~~ __INT_MAX__ we instead print: t.c:5:12: error: 'INT_MAX' undeclared here (not in a function) int test = INT_MAX; ^~~~~~~ t.c:5:12: note: 'INT_MAX' is defined in header '<limits.h>'; did you forget to '#include <limits.h>'? t.c:1:1: +#include <limits.h> t.c:5:12: int test = INT_MAX; ^~~~~~~ It also adds generalizes some of the code for this (and for the "std::" namespace hints in the C++ frontend), moving it to a new c-family/known-headers.cc and .h, and introducing a class known_headers. This currently just works by scanning a hardcoded array of known name/header associations, but perhaps in the future could be turned into some kind of symbol database so that the compiler could record API uses and use that to offer suggestions e.g. foo.cc: error: 'myapi::foo' was not declared in this scope foo.cc: note: 'myapi::foo" was declared in header 'myapi/private.h' (included via 'myapi/public.h') when compiling 'bar.cc'; did you forget to '#include "myapi/public.h"'? or somesuch. In any case, moving this to a class gives an easier way to locate the hardcoded knowledge about the stdlib. The patch also adds similar code to the C++ frontend covering unqualified names in the standard library, so that rather than just e.g.: t.cc:19:13: error: 'NULL' was not declared in this scope void *ptr = NULL; ^~~~ we can emit: t.cc:19:13: error: 'NULL' was not declared in this scope void *ptr = NULL; ^~~~ t.cc:19:13: note: 'NULL' is defined in header '<cstddef>'; did you forget to '#include <cstddef>'? t.cc:1:1: +#include <cstddef> t.cc:19:13: void *ptr = NULL; ^~~~ (Also XFAIL for PR c++/80567 added for the C++ testcase; this is a separate pre-existing bug exposed by the testcase for PR 81404). gcc/ChangeLog: PR c/81404 * Makefile.in (C_COMMON_OBJS): Add c-family/known-headers.o. gcc/c-family/ChangeLog: PR c/81404 * known-headers.cc: New file, based on material from c/c-decl.c. (suggest_missing_header): Copied as-is. (get_stdlib_header_for_name): New, based on get_c_name_hint but heavily edited to add C++ support. Add some knowledge about <limits.h>, <stdint.h>, and <wchar.h>. * known-headers.h: Likewise. gcc/c/ChangeLog: PR c/81404 * c-decl.c: Include "c-family/known-headers.h". (get_c_name_hint): Rename to get_stdlib_header_for_name and move to known-headers.cc. (class suggest_missing_header): Move to known-header.h. (lookup_name_fuzzy): Call get_c_stdlib_header_for_name rather than get_c_name_hint. gcc/cp/ChangeLog: PR c/81404 * name-lookup.c: Include "c-family/known-headers.h" (lookup_name_fuzzy): Call get_cp_stdlib_header_for_name and potentially return a new suggest_missing_header hint. gcc/testsuite/ChangeLog: PR c/81404 * g++.dg/spellcheck-stdlib.C: New. * gcc.dg/spellcheck-stdlib.c (test_INT_MAX): New. From-SVN: r254980