Provide diagnostic hints for missing C inttypes.h string constants.
authorMark Wielaard <mark@klomp.org>
Thu, 21 May 2020 23:10:50 +0000 (01:10 +0200)
committerMark Wielaard <mark@klomp.org>
Wed, 3 Jun 2020 23:22:52 +0000 (01:22 +0200)
commit9eea5d2ddf73037e21b59bcd8084ba969ae10174
treefc247b15b760343e12c8472d91d08634b9340557
parent34e4962aed08b38f37e37242234bfbbd1b897f39
Provide diagnostic hints for missing C inttypes.h string constants.

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.
gcc/c-family/known-headers.cc
gcc/c-family/known-headers.h
gcc/c/c-parser.c
gcc/testsuite/g++.dg/spellcheck-inttypes.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/spellcheck-inttypes.c [new file with mode: 0644]