[svn-commits] mjordan: branch 13 r432808 - in /branches/13: ./ include/asterisk/ main/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Mar 12 07:39:30 CDT 2015


Author: mjordan
Date: Thu Mar 12 07:39:26 2015
New Revision: 432808

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=432808
Log:
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

Modified:
    branches/13/   (props changed)
    branches/13/Makefile
    branches/13/configure
    branches/13/configure.ac
    branches/13/include/asterisk/inline_api.h
    branches/13/include/asterisk/utils.h
    branches/13/main/Makefile
    branches/13/makeopts.in

Propchange: branches/13/
------------------------------------------------------------------------------
Binary property 'branch-11-merged' - no diff available.

Modified: branches/13/Makefile
URL: http://svnview.digium.com/svn/asterisk/branches/13/Makefile?view=diff&rev=432808&r1=432807&r2=432808
==============================================================================
--- branches/13/Makefile (original)
+++ branches/13/Makefile Thu Mar 12 07:39:26 2015
@@ -184,7 +184,7 @@
   _ASTCFLAGS+=-Wall
 endif
 
-_ASTCFLAGS+=-Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(AST_NESTED_FUNCTIONS) $(DEBUG)
+_ASTCFLAGS+=-Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(AST_NESTED_FUNCTIONS) $(AST_CLANG_BLOCKS) $(DEBUG)
 ADDL_TARGETS=
 
 ifeq ($(AST_DEVMODE),yes)
@@ -931,7 +931,7 @@
 	- at menuselect/nmenuselect menuselect.makeopts && (echo "menuselect changes saved!"; rm -f channels/h323/Makefile.ast main/asterisk) || echo "menuselect changes NOT saved!"
 
 # options for make in menuselect/
-MAKE_MENUSELECT=CC="$(BUILD_CC)" CXX="" LD="" AR="" RANLIB="" \
+MAKE_MENUSELECT=CC="$(CC)" CXX="$(CXX)" LD="" AR="" RANLIB="" \
 		CFLAGS="$(BUILD_CFLAGS)" LDFLAGS="$(BUILD_LDFLAGS)" \
 		$(MAKE) -C menuselect CONFIGURE_SILENT="--silent"
 

Modified: branches/13/configure.ac
URL: http://svnview.digium.com/svn/asterisk/branches/13/configure.ac?view=diff&rev=432808&r1=432807&r2=432808
==============================================================================
--- branches/13/configure.ac (original)
+++ branches/13/configure.ac Thu Mar 12 07:39:26 2015
@@ -1078,7 +1078,7 @@
 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 @@
 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_SUBST(AST_NESTED_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)
+	]
+)
 
 dnl Check to see if rpath should be set in LDFLAGS
 AC_ARG_ENABLE(rpath,

Modified: branches/13/include/asterisk/inline_api.h
URL: http://svnview.digium.com/svn/asterisk/branches/13/include/asterisk/inline_api.h?view=diff&rev=432808&r1=432807&r2=432808
==============================================================================
--- branches/13/include/asterisk/inline_api.h (original)
+++ branches/13/include/asterisk/inline_api.h Thu Mar 12 07:39:26 2015
@@ -25,12 +25,14 @@
   Small API functions that are candidates for inlining need to be specially
   declared and defined, to ensure that the 'right thing' always happens.
   For example:
-  	- there must _always_ be a non-inlined version of the function
+	- there must _always_ be a non-inlined version of the function
 	available for modules compiled out of the tree to link to
 	- references to a function that cannot be inlined (for any
 	reason that the compiler deems proper) must devolve into an
 	'extern' reference, instead of 'static', so that multiple
-	copies of the function body are not built in different modules
+	copies of the function body are not built in different modules.
+	However, since this doesn't work for clang, we go with 'static'
+	anyway and hope for the best!
 	- when LOW_MEMORY is defined, inlining should be disabled
 	completely, even if the compiler is configured to support it
 
@@ -46,8 +48,12 @@
 #if !defined(LOW_MEMORY) && !defined(DISABLE_INLINE)
 
 #if !defined(AST_API_MODULE)
+#if defined(__clang__)
+#define AST_INLINE_API(hdr, body) static hdr; static inline hdr body
+#else /* if defined(__clang__) */
 #define AST_INLINE_API(hdr, body) hdr; extern inline hdr body
-#else
+#endif
+#else /* if !defined(AST_API_MODULE) */
 #define AST_INLINE_API(hdr, body) hdr; hdr body
 #endif
 

Modified: branches/13/include/asterisk/utils.h
URL: http://svnview.digium.com/svn/asterisk/branches/13/include/asterisk/utils.h?view=diff&rev=432808&r1=432807&r2=432808
==============================================================================
--- branches/13/include/asterisk/utils.h (original)
+++ branches/13/include/asterisk/utils.h Thu Mar 12 07:39:26 2015
@@ -1005,11 +1005,32 @@
  * }
  * \endcode
  */
-#define RAII_VAR(vartype, varname, initval, dtor) \
-    /* Prototype needed due to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36774 */ \
-    auto void _dtor_ ## varname (vartype * v); \
-    void _dtor_ ## varname (vartype * v) { dtor(*v); } \
+
+#if defined(__clang__)
+
+#if defined(__has_feature) && __has_feature(blocks)
+typedef void (^_raii_cleanup_block_t)(void);
+static inline void _raii_cleanup_block(_raii_cleanup_block_t *b) { (*b)(); }
+
+#define RAII_VAR(vartype, varname, initval, dtor)                                                                \
+    _raii_cleanup_block_t _raii_cleanup_ ## varname __attribute__((cleanup(_raii_cleanup_block),unused)) = NULL; \
+    vartype varname = initval;                                                                                   \
+    _raii_cleanup_ ## varname = ^{ dtor(varname); }
+
+#else
+	#error "CLANG must support the 'blocks' feature to compile Asterisk."
+#endif /* #if defined(__has_feature) && __has_feature(blocks) */
+
+#elif defined(__GNUC__)
+
+#define RAII_VAR(vartype, varname, initval, dtor)                              \
+    auto void _dtor_ ## varname (vartype * v);                                 \
+    void _dtor_ ## varname (vartype * v) { dtor(*v); }                         \
     vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
+
+#else
+    #error "Cannot compile Asterisk: unknown and unsupported compiler."
+#endif /* #if __GNUC__ */
 
 /*!
  * \brief Asterisk wrapper around crypt(3).

Modified: branches/13/main/Makefile
URL: http://svnview.digium.com/svn/asterisk/branches/13/main/Makefile?view=diff&rev=432808&r1=432807&r2=432808
==============================================================================
--- branches/13/main/Makefile (original)
+++ branches/13/main/Makefile Thu Mar 12 07:39:26 2015
@@ -40,6 +40,7 @@
 AST_LIBS+=$(URIPARSER_LIB)
 AST_LIBS+=$(UUID_LIB)
 AST_LIBS+=$(CRYPT_LIB)
+AST_LIBS+=$(AST_CLANG_BLOCKS_LIBS)
 
 ifneq ($(findstring $(OSARCH), linux-gnu uclinux linux-uclibc kfreebsd-gnu),)
   ifneq ($(findstring LOADABLE_MODULES,$(MENUSELECT_CFLAGS)),)

Modified: branches/13/makeopts.in
URL: http://svnview.digium.com/svn/asterisk/branches/13/makeopts.in?view=diff&rev=432808&r1=432807&r2=432808
==============================================================================
--- branches/13/makeopts.in (original)
+++ branches/13/makeopts.in Thu Mar 12 07:39:26 2015
@@ -109,6 +109,8 @@
 AST_NO_STRICT_OVERFLOW=@AST_NO_STRICT_OVERFLOW@
 AST_SHADOW_WARNINGS=@AST_SHADOW_WARNINGS@
 AST_NESTED_FUNCTIONS=@AST_NESTED_FUNCTIONS@
+AST_CLANG_BLOCKS=@AST_CLANG_BLOCKS@
+AST_CLANG_BLOCKS_LIBS=@AST_CLANG_BLOCKS_LIBS@
 AST_RPATH=@AST_RPATH@
 AST_FORTIFY_SOURCE=@AST_FORTIFY_SOURCE@
 AST_MARCH_NATIVE=@AST_MARCH_NATIVE@




More information about the svn-commits mailing list