mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
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:
@@ -227,10 +227,13 @@ m4_define([b4_value_type_declare],
|
|||||||
# Define the public types: token, semantic value, location, and so forth.
|
# 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.
|
# Depending on %define token_lex, may be output in the header or source file.
|
||||||
m4_define([b4_public_types_declare],
|
m4_define([b4_public_types_declare],
|
||||||
[[#ifndef ]b4_api_PREFIX[STYPE
|
[[#ifdef ]b4_api_PREFIX[STYPE
|
||||||
]b4_value_type_declare[
|
# ifdef __GNUC__
|
||||||
#else
|
# 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;
|
typedef ]b4_api_PREFIX[STYPE value_type;
|
||||||
|
#else
|
||||||
|
]b4_value_type_declare[
|
||||||
#endif
|
#endif
|
||||||
/// Backward compatibility (Bison 3.8).
|
/// Backward compatibility (Bison 3.8).
|
||||||
typedef value_type semantic_type;]b4_locations_if([
|
typedef value_type semantic_type;]b4_locations_if([
|
||||||
|
|||||||
@@ -1777,8 +1777,6 @@ AT_DATA_GRAMMAR([[input.y]],
|
|||||||
float fval;
|
float fval;
|
||||||
} sem_type;
|
} sem_type;
|
||||||
|
|
||||||
# define YYSTYPE sem_type
|
|
||||||
|
|
||||||
]AT_CXX_IF([[
|
]AT_CXX_IF([[
|
||||||
# include <cstdio> // EOF.
|
# include <cstdio> // EOF.
|
||||||
# include <iostream>
|
# include <iostream>
|
||||||
@@ -1800,6 +1798,8 @@ AT_DATA_GRAMMAR([[input.y]],
|
|||||||
]])[
|
]])[
|
||||||
}
|
}
|
||||||
|
|
||||||
|
%define api.value.type {sem_type}
|
||||||
|
|
||||||
%code
|
%code
|
||||||
{
|
{
|
||||||
]AT_YYERROR_DECLARE[
|
]AT_YYERROR_DECLARE[
|
||||||
|
|||||||
@@ -328,15 +328,18 @@ AT_TEST([xb], [%locations %define api.location.file none %language "c++" %define
|
|||||||
# C++ output relies on namespaces and still uses yy a lot.
|
# C++ output relies on namespaces and still uses yy a lot.
|
||||||
#
|
#
|
||||||
# - no 'YY' left.
|
# - no 'YY' left.
|
||||||
# Ignore comments, YYChar (template parameter), YYPUSH_MORE(_DEFINED)?
|
# Ignore YYPUSH_MORE(_DEFINED)? (constant definition),
|
||||||
# (constant definition), YY_\w+_INCLUDED (header guards).
|
|
||||||
# YYDEBUG (not renamed) can be read, but not changed.
|
# YYDEBUG (not renamed) can be read, but not changed.
|
||||||
AT_PERL_CHECK([[-n -0777 -e '
|
AT_PERL_CHECK([[-n -0777 -e '
|
||||||
|
# Ignore comments.
|
||||||
s{/\*.*?\*/}{}gs;
|
s{/\*.*?\*/}{}gs;
|
||||||
s{//.*}{}g;
|
s{//.*}{}g;
|
||||||
|
# Ignore warnings.
|
||||||
|
s{# *pragma .* message ".*"}{}g;
|
||||||
|
|
||||||
s{\b((defined|if)\ YYDEBUG
|
s{\b((defined|if)\ YYDEBUG
|
||||||
|YYChar
|
|YYChar # Template parameter.
|
||||||
|YYNTOKENS # This is actual scoped in a C++ class.
|
|YYNTOKENS # This is actually scoped in a C++ class.
|
||||||
|YYPUSH_MORE(?:_DEFINED)?
|
|YYPUSH_MORE(?:_DEFINED)?
|
||||||
|S_(YY(ACCEPT|EMPTY|EOF|error|UNDEF)) # These guys are scoped.
|
|S_(YY(ACCEPT|EMPTY|EOF|error|UNDEF)) # These guys are scoped.
|
||||||
|YY(?:_REINTERPRET)?_CAST
|
|YY(?:_REINTERPRET)?_CAST
|
||||||
@@ -354,9 +357,9 @@ AT_PERL_CHECK([[-n -0777 -e '
|
|||||||
|YY_NULLPTR
|
|YY_NULLPTR
|
||||||
|YY_RVREF
|
|YY_RVREF
|
||||||
|YY_USE
|
|YY_USE
|
||||||
|YY_\w+_INCLUDED
|
|YY_\w+_INCLUDED # Header guards.
|
||||||
|FILE\ \*yyo # Function argument.
|
|FILE\ \*yyo # Function argument.
|
||||||
|const\ yylocp # Function argument.
|
|const\ yylocp # Function argument.
|
||||||
)\b}{}gx;
|
)\b}{}gx;
|
||||||
while (/^(.*YY.*)$/gm)
|
while (/^(.*YY.*)$/gm)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user