From 53aaf8974f3bf60983f6b57c22a0a33adbfca4de Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Marchal?= Date: Wed, 9 Feb 2011 15:23:29 +0000 Subject: [PATCH] Check for empty body in source code Enable a warning in gcc to stop the compilation if an empty body is found after some control structures. It should detect stray semi-colons at the end of the control structures such as if, for, while,... --- CMakeLists.txt | 7 +++++++ configure.in | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 133cf0b..8b2cc7c 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -329,11 +329,18 @@ OPTION(ENABLE_EXTRA_PROTECT "Enable compile and runtime extra protections" OFF) IF(ENABLE_EXTRA_PROTECT) GET_TARGET_PROPERTY(TMPCFLAGS sarg COMPILE_FLAGS) SET_TARGET_PROPERTIES(sarg PROPERTIES COMPILE_FLAGS "${TMPCFLAGS} -fstack-protector -D_FORTIFY_SOURCE=2 -Werror") + CHECK_C_COMPILER_FLAG("-Wformat -Werror=format-security" HAVE_WERROR_FORMAT_SECURITY) IF(HAVE_WERROR_FORMAT_SECURITY) GET_TARGET_PROPERTY(TMPCFLAGS sarg COMPILE_FLAGS) SET_TARGET_PROPERTIES(sarg PROPERTIES COMPILE_FLAGS "${TMPCFLAGS} -Wformat -Werror=format-security") ENDIF(HAVE_WERROR_FORMAT_SECURITY) + + CHECK_C_COMPILER_FLAG("-Wempty-body" HAVE_EMPTY_BODY) + IF(HAVE_EMPTY_BODY) + GET_TARGET_PROPERTY(TMPCFLAGS sarg COMPILE_FLAGS) + SET_TARGET_PROPERTIES(sarg PROPERTIES COMPILE_FLAGS "${TMPCFLAGS} -Wempty-body") + ENDIF(HAVE_EMPTY_BODY) ENDIF(ENABLE_EXTRA_PROTECT) # Compile sarg for debugging diff --git a/configure.in b/configure.in index e15aba9..48b32a0 100644 --- a/configure.in +++ b/configure.in @@ -215,6 +215,15 @@ AC_ARG_ENABLE(extraprotection, if test "$have_format_security" == "no" ; then CFLAGS="${saved_CFLAGS}" fi + + AC_MSG_CHECKING([for empty body flag in $CC]) + saved_CFLAGS="${CFLAGS}" + CFLAGS="${CFLAGS} -Wempty-body" + AC_COMPILE_IFELSE([AC_LANG_SOURCE([])],[have_empty_body="yes"],[have_empty_body="no"]) + AC_MSG_RESULT($have_empty_body) + if test "$have_empty_body" == "no" ; then + CFLAGS="${saved_CFLAGS}" + fi fi ]) -- 2.47.2