1
0
mirror of https://github.com/gryf/wmaker.git synced 2026-02-15 13:35:53 +01:00

wmaker: work around compilers that do not support nested functions

There are a few cases in which nested functions are an helpful way to write
code, as this is explained in 'script/nested-func-to-macro.sh'. However,
some compiler do not support them (like clang), so this patch proposes an
elegant solution, where developers can get the benefit of them, but for
users they are automatically converted to C macro if needed.

The advantage of this solution is that we keep the code simple, there is no
hack in the source (like #ifdef and code duplication), yet still having the
full advantages.

The translation is done according to what have been detected by configure
(see the WM_PROG_CC_NESTEDFUNC macro) so that user has nothing to do.

Signed-off-by: Christophe CURIS <christophe.curis@free.fr>
This commit is contained in:
Christophe CURIS
2014-12-05 18:02:40 +01:00
committed by Carlos R. Mafra
parent a24efec61f
commit 10371836ed
6 changed files with 285 additions and 5 deletions

View File

@@ -46,3 +46,41 @@ AS_CASE([$wm_cv_prog_cc_c11],
[no|native], [],
[CFLAGS="$CFLAGS $wm_cv_prog_cc_c11"])
])
# WM_PROG_CC_NESTEDFUNC
# ---------------------
#
# Check if the compiler support declaring Nested Functions (that means
# declaring a function inside another function).
#
# If the compiler does not support them, then the Automake conditional
# USE_NESTED_FUNC will be set to false, in which case the Makefile will
# use the script 'scripts/nested-func-to-macro.sh' to generate a modified
# source with the nested function transformed into a Preprocessor Macro.
AC_DEFUN_ONCE([WM_PROG_CC_NESTEDFUNC],
[AC_CACHE_CHECK([if compiler supports nested functions], [wm_cv_prog_cc_nestedfunc],
[AC_COMPILE_IFELSE(
[AC_LANG_SOURCE([[
int main(int narg, char **argv)
{
int local_variable;
int nested_function(int argument)
{
/* Checking we have access to upper level's scope, otherwise it is of no use */
return local_variable + argument;
}
/* To avoid a warning for unused parameter, that may falsely fail */
(void) argv;
/* Initialise using the parameter to main so the compiler won't be tempted to optimise too much */
local_variable = narg + 1;
return nested_function(2);
}]]) ],
[wm_cv_prog_cc_nestedfunc=yes],
[wm_cv_prog_cc_nestedfunc=no]) ])
AM_CONDITIONAL([USE_NESTED_FUNC], [test "x$wm_cv_prog_cc_nestedfunc" != "xno"])dnl
])