c++: issue a compile-time warning when #define YYSTYPE is used

Using #define YYSTYPE has always been strongly discouraged in C++.
Macros are dangerous and can result in subtle bugs.
https://lists.gnu.org/r/bug-bison/2020-12/msg00007.html

Maybe some people are currently using #define YYSTYPE.  Instead of
dropping support right now, first issue a warning.  Bison can "see" if
YYDEBUG is defined (it could even be on the command line), only the
compiler knows.  Unfortunately `#warning` is non-portable, and
actually GCC even dies on it when `-pedantic` is enabled.  So we need
to use `#pragma message`.  We must make it conditional as some
compilers might not support it, but it doesn't matter if only _some_
compilers emit the warning: it should be enough to catch the attention
of the developers.

* data/skeletons/c++.m4: Issue a warning when the user defined
YYSTYPE.
* tests/actions.at: Don't #define YYSTYPE.
* tests/headers.at (Several parsers): Ignore the YYSTYPE in the
warning.
This commit is contained in:
Akim Demaille
2021-01-31 16:05:13 +01:00
parent dc8fe0678a
commit 9acc9d2aee
3 changed files with 18 additions and 12 deletions

View File

@@ -227,10 +227,13 @@ m4_define([b4_value_type_declare],
# Define the public types: token, semantic value, location, and so forth.
# Depending on %define token_lex, may be output in the header or source file.
m4_define([b4_public_types_declare],
[[#ifndef ]b4_api_PREFIX[STYPE
]b4_value_type_declare[
#else
[[#ifdef ]b4_api_PREFIX[STYPE
# ifdef __GNUC__
# pragma GCC message "bison: do not #define ]b4_api_PREFIX[STYPE in C++, use %define api.value.type"
# endif
typedef ]b4_api_PREFIX[STYPE value_type;
#else
]b4_value_type_declare[
#endif
/// Backward compatibility (Bison 3.8).
typedef value_type semantic_type;]b4_locations_if([