libdrgn: add autoconf option to enable compiler warnings

This adds an --enable-compiler-warnings flag that:

* Defines a canonical list of warnings that we enforce. For now, this is
  -Wall -Wformat-overflow=2 -Wformat-truncation=2, but we can add to it
  going forward.
* Enables warnings by default.
* Allows erroring on warnings. We recommend that developers use this and
  use it for the CI.

Signed-off-by: Omar Sandoval <osandov@osandov.com>
This commit is contained in:
Omar Sandoval 2022-03-01 15:38:05 -08:00
parent 36277e22f3
commit 24609a3a2e
9 changed files with 166 additions and 6 deletions

View File

@ -12,7 +12,6 @@ jobs:
fail-fast: false
env:
CC: ${{ matrix.cc }}
CFLAGS: -Wall -Werror -g -O2
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
@ -29,7 +28,7 @@ jobs:
- name: Check with mypy
run: pre-commit run --all-files mypy
- name: Build and test with ${{ matrix.cc }}
run: python setup.py test -K
run: CONFIGURE_FLAGS="--enable-compiler-warnings=error" python setup.py test -K
lint:
runs-on: ubuntu-latest

View File

@ -15,7 +15,7 @@ instructions <README.rst#from-source>`_, then run:
$ git clone https://github.com/osandov/drgn.git
$ cd drgn
$ CFLAGS="-Wall -Werror -g -O2" python3 setup.py build_ext -i
$ CONFIGURE_FLAGS="--enable-compiler-warnings=error" python3 setup.py build_ext -i
$ python3 -m drgn --help
Testing

View File

@ -6,7 +6,7 @@ ACLOCAL_AMFLAGS = -I m4
.DELETE_ON_ERROR:
AM_CPPFLAGS = -I $(top_srcdir)/include -D_GNU_SOURCE
AM_CFLAGS = $(SANITIZER_CFLAGS)
AM_CFLAGS = $(WARN_CFLAGS) $(SANITIZER_CFLAGS)
AM_LDFLAGS= $(SANITIZER_LDFLAGS)
noinst_HEADERS = include/dwarf.h \

View File

@ -89,6 +89,30 @@ AS_CASE(["x$with_libkdumpfile"],
AM_CONDITIONAL([WITH_LIBKDUMPFILE], [test "x$with_libkdumpfile" = xyes])
AM_COND_IF([WITH_LIBKDUMPFILE], [AC_DEFINE(WITH_LIBKDUMPFILE)])
AC_ARG_ENABLE([compiler-warnings],
[AS_HELP_STRING([--enable-compiler-warnings@<:@=no|yes|error@:>@],
[enable compiler warnings. If no, then only the
default compiler warnings are enabled. If yes,
then additional warnings required by the package
are enabled. If error, then warnings are treated
as errors (this is only intended for
developers). @<:@default=yes@:>@])],
[], [enable_compiler_warnings=no])
dnl Make Clang error instead of warn for unknown warning options.
AX_CHECK_COMPILE_FLAG([-Werror=unknown-warning-option],
[compiler_flags_test="-Werror=unknown-warning-option"],
[compiler_flags_test=""])
AS_IF([test "x$enable_compiler_warnings" != xno],
[AX_APPEND_COMPILE_FLAGS([ dnl
-Wall dnl
-Wformat-overflow=2 dnl
-Wformat-truncation=2 dnl
], [WARN_CFLAGS], [$compiler_flags_test])])
AS_IF([test "x$enable_compiler_warnings" = xerror],
[AX_APPEND_FLAG([-Werror], [WARN_CFLAGS])])
AC_SUBST(WARN_CFLAGS)
AC_ARG_ENABLE([asan],
[AS_HELP_STRING([--enable-asan], [enable AddressSanitizer])],
[], [enable_asan=no])

View File

@ -1,3 +1,6 @@
*
!/.gitignore
!/ax_append_compile_flags.m4
!/ax_append_flag.m4
!/ax_check_compile_flag.m4
!/ax_require_defined.m4

View File

@ -0,0 +1,46 @@
# ============================================================================
# https://www.gnu.org/software/autoconf-archive/ax_append_compile_flags.html
# ============================================================================
#
# SYNOPSIS
#
# AX_APPEND_COMPILE_FLAGS([FLAG1 FLAG2 ...], [FLAGS-VARIABLE], [EXTRA-FLAGS], [INPUT])
#
# DESCRIPTION
#
# For every FLAG1, FLAG2 it is checked whether the compiler works with the
# flag. If it does, the flag is added FLAGS-VARIABLE
#
# If FLAGS-VARIABLE is not specified, the current language's flags (e.g.
# CFLAGS) is used. During the check the flag is always added to the
# current language's flags.
#
# If EXTRA-FLAGS is defined, it is added to the current language's default
# flags (e.g. CFLAGS) when the check is done. The check is thus made with
# the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to
# force the compiler to issue an error when a bad flag is given.
#
# INPUT gives an alternative input source to AC_COMPILE_IFELSE.
#
# NOTE: This macro depends on the AX_APPEND_FLAG and
# AX_CHECK_COMPILE_FLAG. Please keep this macro in sync with
# AX_APPEND_LINK_FLAGS.
#
# LICENSE
#
# Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 7
AC_DEFUN([AX_APPEND_COMPILE_FLAGS],
[AX_REQUIRE_DEFINED([AX_CHECK_COMPILE_FLAG])
AX_REQUIRE_DEFINED([AX_APPEND_FLAG])
for flag in $1; do
AX_CHECK_COMPILE_FLAG([$flag], [AX_APPEND_FLAG([$flag], [$2])], [], [$3], [$4])
done
])dnl AX_APPEND_COMPILE_FLAGS

View File

@ -0,0 +1,53 @@
# ===========================================================================
# https://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT])
#
# DESCRIPTION
#
# Check whether the given FLAG works with the current language's compiler
# or gives an error. (Warnings, however, are ignored)
#
# ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on
# success/failure.
#
# If EXTRA-FLAGS is defined, it is added to the current language's default
# flags (e.g. CFLAGS) when the check is done. The check is thus made with
# the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to
# force the compiler to issue an error when a bad flag is given.
#
# INPUT gives an alternative input source to AC_COMPILE_IFELSE.
#
# NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this
# macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG.
#
# LICENSE
#
# Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>
# Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 6
AC_DEFUN([AX_CHECK_COMPILE_FLAG],
[AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF
AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl
AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [
ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS
_AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1"
AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])],
[AS_VAR_SET(CACHEVAR,[yes])],
[AS_VAR_SET(CACHEVAR,[no])])
_AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags])
AS_VAR_IF(CACHEVAR,yes,
[m4_default([$2], :)],
[m4_default([$3], :)])
AS_VAR_POPDEF([CACHEVAR])dnl
])dnl AX_CHECK_COMPILE_FLAGS

View File

@ -0,0 +1,37 @@
# ===========================================================================
# https://www.gnu.org/software/autoconf-archive/ax_require_defined.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_REQUIRE_DEFINED(MACRO)
#
# DESCRIPTION
#
# AX_REQUIRE_DEFINED is a simple helper for making sure other macros have
# been defined and thus are available for use. This avoids random issues
# where a macro isn't expanded. Instead the configure script emits a
# non-fatal:
#
# ./configure: line 1673: AX_CFLAGS_WARN_ALL: command not found
#
# It's like AC_REQUIRE except it doesn't expand the required macro.
#
# Here's an example:
#
# AX_REQUIRE_DEFINED([AX_CHECK_LINK_FLAG])
#
# LICENSE
#
# Copyright (c) 2014 Mike Frysinger <vapier@gentoo.org>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 2
AC_DEFUN([AX_REQUIRE_DEFINED], [dnl
m4_ifndef([$1], [m4_fatal([macro ]$1[ is not defined; is a m4 file missing?])])
])dnl AX_REQUIRE_DEFINED

View File

@ -169,8 +169,6 @@ def ignore_line(path, state, line):
def main():
if "CFLAGS" not in os.environ:
os.environ["CFLAGS"] = "-Wall"
parser = argparse.ArgumentParser(description="run include-what-you-use on drgn")
parser.add_argument(
"source", nargs="*", help="run on given file instead of all source files"