From ec08b369ec1b348d8eeeffd3d48cb7138a918437 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sun, 20 Jan 2019 08:23:41 +0100 Subject: [PATCH] c++: address -Wweak-vtables warnings Reported by Derek Clegg http://lists.gnu.org/archive/html/bug-bison/2019-01/msg00021.html aux/parser-internal.h:429:12: error: 'syntax_error' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables] struct syntax_error : std::runtime_error To avoid this warning, we need syntax_error to have a virtual function defined in a compilation unit. Let it be the destructor. To comply with C++98, this dtor should be 'throw()'. Merely making YY_NOEXCEPT be 'throw()' in C++98 triggers errors (http://lists.gnu.org/archive/html/bug-bison/2019-01/msg00022.html), so let's introduce YY_NOTHROW and flag only ~syntax_error with it. Also, since we now have an explicit dtor, we need to provide an copy ctor. * configure.ac (warn_cxx): Add -Wweak-vtables. * data/skeletons/c++.m4 (YY_NOTHROW): New. (syntax_error): Declare the dtor, and define the copy ctor. * data/skeletons/glr.cc, data/skeletons/lalr1.cc (~syntax_error): Define. --- configure.ac | 2 +- data/skeletons/c++.m4 | 11 ++++++++++- data/skeletons/glr.cc | 3 +++ data/skeletons/lalr1.cc | 2 ++ tests/headers.at | 1 + 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index a3a471af..1b5343e7 100644 --- a/configure.ac +++ b/configure.ac @@ -100,7 +100,7 @@ if test "$enable_gcc_warnings" = yes; then -Wpointer-arith -Wshadow -Wwrite-strings' warn_c='-Wbad-function-cast -Wstrict-prototypes' - warn_cxx='-Wextra-semi -Wnoexcept -Wundefined-func-template' + warn_cxx='-Wextra-semi -Wnoexcept -Wundefined-func-template -Wweak-vtables' # Warnings for the test suite only. # # -fno-color-diagnostics: Clang's use of colors in the error diff --git a/data/skeletons/c++.m4 b/data/skeletons/c++.m4 index f6f95c4c..acc35b62 100644 --- a/data/skeletons/c++.m4 +++ b/data/skeletons/c++.m4 @@ -77,8 +77,10 @@ m4_define([b4_cxx_portability], // Support noexcept when possible. #if 201103L <= YY_CPLUSPLUS # define YY_NOEXCEPT noexcept +# define YY_NOTHROW #else # define YY_NOEXCEPT +# define YY_NOTHROW throw () #endif // Support noexcept when possible. @@ -217,7 +219,14 @@ m4_define([b4_public_types_declare], syntax_error (]b4_locations_if([const location_type& l, ])[const std::string& m) : std::runtime_error (m)]b4_locations_if([ , location (l)])[ - {}]b4_locations_if([ + {} + + syntax_error (const syntax_error& s) + : std::runtime_error (s.what ())]b4_locations_if([ + , location (s.location)])[ + {} + + ~syntax_error () YY_NOEXCEPT YY_NOTHROW;]b4_locations_if([ location_type location;])[ }; diff --git a/data/skeletons/glr.cc b/data/skeletons/glr.cc index 778c65b8..56217341 100644 --- a/data/skeletons/glr.cc +++ b/data/skeletons/glr.cc @@ -160,6 +160,9 @@ m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl ]b4_parser_class::~b4_parser_class[ () {} + ]b4_parser_class[::syntax_error::~syntax_error () YY_NOEXCEPT YY_NOTHROW + {} + int ]b4_parser_class[::operator() () { diff --git a/data/skeletons/lalr1.cc b/data/skeletons/lalr1.cc index 053ba3be..1dc2851d 100644 --- a/data/skeletons/lalr1.cc +++ b/data/skeletons/lalr1.cc @@ -562,6 +562,8 @@ m4_if(b4_prefix, [yy], [], ]b4_parser_class::~b4_parser_class[ () {} + ]b4_parser_class[::syntax_error::~syntax_error () YY_NOEXCEPT YY_NOTHROW + {} /*---------------. | Symbol types. | diff --git a/tests/headers.at b/tests/headers.at index 87b765dd..bcf8525f 100644 --- a/tests/headers.at +++ b/tests/headers.at @@ -330,6 +330,7 @@ AT_CHECK([[$PERL -n -0777 -e ' |YY_MOVE_OR_COPY |YY_MOVE_REF |YY_NOEXCEPT + |YY_NOTHROW |YY_NULLPTR |YY_RVREF |YY_\w+_INCLUDED