glr2.cc: disable incorrect warnings from GCC6 to 9

For instance with GCC8:

    616. regression.at:1560: testing Lex and parse params: glr2.cc ...
    tests/regression.at:1560: COLUMNS=1000; export COLUMNS; NO_TERM_HYPERLINKS=1; export NO_TERM_HYPERLINKS;  bison --color=no -fno-caret  -o input.cc input.y
    tests/regression.at:1560: $CXX $CXXFLAGS $CPPFLAGS  $LDFLAGS -o input input.cc $LIBS
    stderr:
    input.cc: In member function 'YYRESULTTAG glr_stack::yyresolveValue(glr_state*)':
    input.cc:1796:10: error: potential null pointer dereference [-Werror=null-dereference]
       return yypred ? &(asItem(this) - yypred)->getState() : YY_NULLPTR;
              ^~~~~~
    input.cc:1796:10: error: potential null pointer dereference [-Werror=null-dereference]
       return yypred ? &(asItem(this) - yypred)->getState() : YY_NULLPTR;
              ^~~~~~
    cc1plus: all warnings being treated as errors

It complains that the implicit this in yypred might be null.  It fears
it because of loops such as

    for (glr_state* yys = firstTopState();
         yys != yystateStack.yysplitPoint;
         yys = yys->pred())
      yyn += 1;

that could possibly set yys to null, since yys->pred might return
null.  However, the warning is incorrect, since in C++ `this` cannot
be null.  GCC 10 no longer emits this warning.

GCC 7 also complains many times about glr_stack::yyresolveLocations
when NDEBUG is enabled (when it is not, YYASSERT (yyoption !=
YY_NULLPTR) is probably enough to pacify GCC):

    616. regression.at:1560: testing Lex and parse params: glr2.cc ...
    tests/regression.at:1560: COLUMNS=1000; export COLUMNS; NO_TERM_HYPERLINKS=1; export NO_TERM_HYPERLINKS;  bison --color=no -fno-caret  -o input.cc input.y
    tests/regression.at:1560: $CXX $CXXFLAGS $CPPFLAGS  $LDFLAGS -o input input.cc $LIBS
    stderr:
    input.cc: In member function 'void glr_stack::yyresolveLocations(glr_state*, int)':
    input.cc:3061:46: error: potential null pointer dereference [-Werror=null-dereference]
                     yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
    input.cc:3061:46: error: potential null pointer dereference [-Werror=null-dereference]
                     yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
    input.cc:3061:46: error: potential null pointer dereference [-Werror=null-dereference]
                     yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
    input.cc: In member function 'YYRESULTTAG glr_stack::yyresolveValue(glr_state*)':
    input.cc:3061:46: error: potential null pointer dereference [-Werror=null-dereference]
                     yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
    input.cc:3061:46: error: potential null pointer dereference [-Werror=null-dereference]
                     yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
    input.cc:3061:46: error: potential null pointer dereference [-Werror=null-dereference]
                     yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
    input.cc:3061:46: error: potential null pointer dereference [-Werror=null-dereference]
                     yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
    input.cc:3061:46: error: potential null pointer dereference [-Werror=null-dereference]
                     yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
    input.cc:3061:46: error: potential null pointer dereference [-Werror=null-dereference]
                     yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~

* data/skeletons/glr2.cc (YY_IGNORE_NULL_DEREFERENCE_BEGIN)
(YY_IGNORE_NULL_DEREFERENCE_BEGIN): New.
(glr_state::pred, glr_stack::yyresolveLocations): Use them.
This commit is contained in:
Akim Demaille
2020-09-13 09:52:53 +02:00
parent 2bc886dc02
commit cf8f805d36
2 changed files with 33 additions and 7 deletions

View File

@@ -126,6 +126,17 @@ b4_percent_code_get([[requires]])[
]b4_cast_define[
]b4_null_define[
#if defined __GNUC__ && ! defined __ICC && 6 <= __GNUC__ && __GNUC__ <= 9
# define YY_IGNORE_NULL_DEREFERENCE_BEGIN \
_Pragma ("GCC diagnostic push") \
_Pragma ("GCC diagnostic ignored \"-Wnull-dereference\"")
# define YY_IGNORE_NULL_DEREFERENCE_END \
_Pragma ("GCC diagnostic pop")
#else
# define YY_IGNORE_NULL_DEREFERENCE_BEGIN
# define YY_IGNORE_NULL_DEREFERENCE_END
#endif
// Whether we are compiled with exception support.
#ifndef YY_EXCEPTIONS
# if defined __GNUC__ && !defined __EXCEPTIONS
@@ -1271,12 +1282,18 @@ public:
bool is_state_;
};
glr_state* glr_state::pred() {
return yypred ? &(asItem(this) - yypred)->getState() : YY_NULLPTR;
glr_state* glr_state::pred ()
{
YY_IGNORE_NULL_DEREFERENCE_BEGIN
return yypred ? &(asItem (this) - yypred)->getState () : YY_NULLPTR;
YY_IGNORE_NULL_DEREFERENCE_END
}
const glr_state* glr_state::pred() const {
return yypred ? &(asItem(this) - yypred)->getState() : YY_NULLPTR;
const glr_state* glr_state::pred () const
{
YY_IGNORE_NULL_DEREFERENCE_BEGIN
return yypred ? &(asItem (this) - yypred)->getState () : YY_NULLPTR;
YY_IGNORE_NULL_DEREFERENCE_END
}
void glr_state::setPred(const glr_state* state) {
@@ -2606,7 +2623,9 @@ class glr_stack {
detected. Thus the location of the previous state (but not
necessarily the previous state itself) is guaranteed to be
resolved already. */
YY_IGNORE_NULL_DEREFERENCE_BEGIN
yyrhsloc[0].getState().yyloc = yyoption->state()->yyloc;
YY_IGNORE_NULL_DEREFERENCE_END
}
YYLLOC_DEFAULT ((yys1->yyloc), yyrhsloc, yynrhs);
}