c++: Reject UDLs in certain contexts [PR105300]
authorMarek Polacek <polacek@redhat.com>
Fri, 11 Nov 2022 22:59:30 +0000 (17:59 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 26 Jan 2023 15:06:20 +0000 (10:06 -0500)
commit9f353b0c1dc9385ba8b8a64b65d66d5452383c11
tree46eb0c0e167e6da608f961bd74cf4212d62f112f
parentf1eab269288ffa80ba924ddb4c4b36f8f781d613
c++: Reject UDLs in certain contexts [PR105300]

In this PR, we are crashing because we've encountered a UDL where a
string-literal is expected.  This patch makes the parser reject string
and character UDLs in all places where the grammar requires a
string-literal and not a user-defined-string-literal.

I've introduced two new wrappers; the existing cp_parser_string_literal
was renamed to cp_parser_string_literal_common and should not be called
directly.  finish_userdef_string_literal is renamed from
cp_parser_userdef_string_literal.

PR c++/105300

gcc/c-family/ChangeLog:

* c-pragma.cc (handle_pragma_message): Warn for CPP_STRING_USERDEF.

gcc/cp/ChangeLog:

* parser.cc: Remove unnecessary forward declarations.
(cp_parser_string_literal): New wrapper.
(cp_parser_string_literal_common): Renamed from
cp_parser_string_literal.  Add a bool parameter.  Give an error when
UDLs are not permitted.
(cp_parser_userdef_string_literal): New wrapper.
(finish_userdef_string_literal): Renamed from
cp_parser_userdef_string_literal.
(cp_parser_primary_expression): Call cp_parser_userdef_string_literal
instead of cp_parser_string_literal.
(cp_parser_linkage_specification): Move a variable declaration closer
to its first use.
(cp_parser_static_assert): Likewise.
(cp_parser_operator): Call cp_parser_userdef_string_literal instead of
cp_parser_string_literal.
(cp_parser_asm_definition): Move a variable declaration closer to its
first use.
(cp_parser_asm_specification_opt): Move variable declarations closer to
their first use.
(cp_parser_asm_operand_list): Likewise.
(cp_parser_asm_clobber_list): Likewise.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/udlit-error1.C: New test.
gcc/c-family/c-pragma.cc
gcc/cp/parser.cc
gcc/testsuite/g++.dg/cpp0x/udlit-error1.C [new file with mode: 0644]