Add support for the clang compiler; update RAII_VAR to use BlocksRuntime

RAII_VAR, which is used extensively in Asterisk to manage reference counted
resources, uses a GCC extension to automatically invoke a cleanup function
when a variable loses scope. While this functionality is incredibly useful
and has prevented a large number of memory leaks, it also prevents Asterisk
from being compiled with clang.

This patch updates the RAII_VAR macro such that it can be compiled with clang.
It makes use of the BlocksRuntime, which allows for a closure to be created
that performs the actual cleanup.

Note that this does not attempt to address the numerous warnings that the clang
compiler catches in Asterisk.

Much thanks for this patch goes to:
* The folks on StackOverflow who asked this question and Leushenko for
  providing the answer that formed the basis of this code:
  http://stackoverflow.com/questions/24959440/rewrite-gcc-cleanup-macro-with-nested-function-for-clang
* Diederik de Groot, who has been extremely patient in working on getting this
  patch into Asterisk.

Review: https://reviewboard.asterisk.org/r/4370/

ASTERISK-24133
ASTERISK-23666
ASTERISK-20399
ASTERISK-20850 #close
Reported by: Diederik de Groot
patches:
  RAII_CLANG.patch uploaded by Diederik de Groot (License 6600)
........

Merged revisions 432807 from http://svn.asterisk.org/svn/asterisk/branches/11


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@432808 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Matthew Jordan
2015-03-12 12:39:26 +00:00
parent bd029688cd
commit f5bc032567
7 changed files with 127 additions and 27 deletions

View File

@@ -1078,7 +1078,7 @@ fi
AC_SUBST(AST_DECLARATION_AFTER_STATEMENT)
AC_MSG_CHECKING(for -Wtrampolines support)
if $(${CC} -Wtrampolines -S -o /dev/null -xc /dev/null > /dev/null 2>&1); then
if $(${CC} -Wtrampolines -Werror -S -o /dev/null -xc /dev/null > /dev/null 2>&1); then
AC_MSG_RESULT(yes)
AST_TRAMPOLINES=-Wtrampolines
else
@@ -1133,16 +1133,41 @@ fi
AC_SUBST(AST_NATIVE_ARCH)
dnl Nested functions required for RAII implementation
AC_MSG_CHECKING(for -fnested-functions)
AC_COMPILE_IFELSE(
dnl Prototype needed due to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36774
[AC_LANG_PROGRAM([], [auto void foo(void); void foo(void) {}])],
AC_MSG_RESULT(no)
[AST_NESTED_FUNCTIONS=],
AC_MSG_RESULT(required)
[AST_NESTED_FUNCTIONS=-fnested-functions]
AC_LINK_IFELSE(
[AC_LANG_PROGRAM([], [
#if defined(__clang__)
choke
#endif
])
],[
dnl Nested functions required for RAII implementation
AC_MSG_CHECKING(for gcc -fnested-functions)
AC_COMPILE_IFELSE(
dnl Prototype needed due to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36774
[AC_LANG_PROGRAM([], [auto void foo(void); void foo(void) {}])],
AC_MSG_RESULT(no)
[AST_NESTED_FUNCTIONS=],
AC_MSG_RESULT(yes)
[AST_NESTED_FUNCTIONS=-fnested-functions]
)
AC_SUBST(AST_NESTED_FUNCTIONS)
],[
AC_MSG_CHECKING(for clang -fblocks)
if test "`echo "int main(){return ^{return 42;}();}" | ${CC} -o /dev/null -fblocks -x c - 2>&1`" = ""; then
[AST_CLANG_BLOCKS_LIBS=""]
[AST_CLANG_BLOCKS="-Wno-unknown-warning-option -fblocks"]
AC_MSG_RESULT(yes)
elif test "`echo "int main(){return ^{return 42;}();}" | ${CC} -o /dev/null -fblocks -x c -lBlocksRuntime - 2>&1`" = ""; then
[AST_CLANG_BLOCKS_LIBS="-lBlocksRuntime"]
[AST_CLANG_BLOCKS="-fblocks"]
AC_MSG_RESULT(yes)
else
AC_MSG_ERROR("BlocksRuntime is required for clang")
fi
AC_SUBST(AST_CLANG_BLOCKS_LIBS)
AC_SUBST(AST_CLANG_BLOCKS)
]
)
AC_SUBST(AST_NESTED_FUNCTIONS)
dnl Check to see if rpath should be set in LDFLAGS
AC_ARG_ENABLE(rpath,