Our next gnulib update will automatically turn on -Wformat=2, which overrides our attempt to avoid -Wformat-nonliteral. But for newer gcc, we don't need to blindly ignore the warning; instead, we can target just the audited code while leaving the warning to apply to the rest of the code. And for older gcc, we just don't use the warning (that is, warnings are a moving target, and it is fine if we only target newer gcc for full warnings).
* configure.ac (M4_cv_gcc_pragma_push_works): New test. (-Wformat-nonliteral): Disable for older gcc. * src/format.c (expand_format): Mark our only uses of nonliteral format as being safe. Signed-off-by: Eric Blake <[email protected]> --- ChangeLog | 6 ++++++ configure.ac | 21 ++++++++++++++++++++- src/format.c | 9 +++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a1dfb77..1d2400a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2012-09-01 Eric Blake <[email protected]> + build: silence gcc warning + * configure.ac (M4_cv_gcc_pragma_push_works): New test. + (-Wformat-nonliteral): Disable for older gcc. + * src/format.c (expand_format): Mark our only uses of nonliteral + format as being safe. + doc: fix misuse of @xref * doc/m4.texinfo (Inhibiting Invocation): Reword. diff --git a/configure.ac b/configure.ac index 8eecef3..cedb922 100644 --- a/configure.ac +++ b/configure.ac @@ -57,6 +57,20 @@ if test "$gl_gcc_warnings" = yes; then gl_WARN_ADD([-Werror], [WERROR_CFLAGS]) AC_SUBST([WERROR_CFLAGS]) + # We use '#pragma GCC diagnostic push' to silence some + # warnings, but older gcc doesn't support this. + AC_CACHE_CHECK([whether pragma GCC diagnostic push works], + [M4_cv_gcc_pragma_push_works], [ + save_CFLAGS=$CFLAGS + CFLAGS='-Wunknown-pragmas -Werror' + AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ + #pragma GCC diagnostic push + #pragma GCC diagnostic pop + ]])], + [M4_cv_gcc_pragma_push_works=yes], + [M4_cv_gcc_pragma_push_works=no]) + CFLAGS=$save_CFLAGS]) + nw= nw="$nw -Waggregate-return" # C90 is anachronistic nw="$nw -Wlong-long" # C90 is anachronistic @@ -65,7 +79,6 @@ if test "$gl_gcc_warnings" = yes; then nw="$nw -Wsystem-headers" # Don't let system headers trigger warnings nw="$nw -Wpadded" # Our structs are not packed nw="$nw -Wredundant-decls" # Gnulib has multiple decls - nw="$nw -Wformat-nonliteral" # Needed in builtin.c nw="$nw -Wunreachable-code" # Needed in output.c nw="$nw -Wconversion" # Too many warnings for now nw="$nw -Wsign-conversion" # Too many warnings for now @@ -80,6 +93,12 @@ if test "$gl_gcc_warnings" = yes; then gl_WARN_ADD([$w]) done + # Gnulib turns on -Wformat=2, which implies -Wformat-nonliteral, but + # we temporarily override it in format.c if possible. + if test $M4_cv_gcc_pragma_push_works = no; then + gl_WARN_ADD([-Wno-format-nonliteral]) + fi + gl_WARN_ADD([-fdiagnostics-show-option]) gl_WARN_ADD([-funit-at-a-time]) diff --git a/src/format.c b/src/format.c index 4b7f022..940021a 100644 --- a/src/format.c +++ b/src/format.c @@ -348,6 +348,12 @@ expand_format (struct obstack *obs, int argc, token_data **argv) *p++ = c; *p = '\0'; + /* Our constructed format string in fstart is safe. */ +#if 4 < __GNUC__ + (6 <= __GNUC_MINOR__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wformat-nonliteral" +#endif + switch (datatype) { case CHAR: @@ -373,6 +379,9 @@ expand_format (struct obstack *obs, int argc, token_data **argv) default: abort(); } +#if 4 < __GNUC__ + (6 <= __GNUC_MINOR__) +# pragma GCC diagnostic pop +#endif /* NULL was returned on failure, such as invalid format string. For now, just silently ignore that bad specifier. */ -- 1.7.11.4 _______________________________________________ M4-patches mailing list [email protected] https://lists.gnu.org/mailman/listinfo/m4-patches
