From: Paolo Bonzini Date: Tue, 28 Oct 2008 10:27:31 +0000 (+0100) Subject: add m4_stack_foreach and m4_stack_foreach_lifo X-Git-Tag: v2.63b~179 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1415907c2d8a008fa2e6e443c491006029f43bde;p=thirdparty%2Fautoconf.git add m4_stack_foreach and m4_stack_foreach_lifo * lib/m4sugar/m4sugar.m4 (_m4_stack_reverse): New from _m4_copy. (m4_stack_foreach, m4_stack_foreach_lifo): New. (m4_copy): Use m4_stack_foreach and m4_curry. (_m4_dumpdefs_down, _m4_dumpdefs_up): Remove. (m4_dumpdefs): Rewrite using m4_stack_foreach_lifo. * tests/m4sugar.at (m4_stack_foreach): New test. --- diff --git a/ChangeLog b/ChangeLog index 4e8fdd1b8..d0296c06c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2008-10-28 Paolo Bonzini + + Add m4_stack_foreach and m4_stack_foreach_lifo. + * lib/m4sugar/m4sugar.m4 (_m4_stack_reverse): New from _m4_copy. + (m4_stack_foreach, m4_stack_foreach_lifo): New. + (m4_copy): Use m4_stack_foreach and m4_curry. + (_m4_dumpdefs_down, _m4_dumpdefs_up): Remove. + (m4_dumpdefs): Rewrite using m4_stack_foreach_lifo. + * tests/m4sugar.at (m4_stack_foreach): New test. + 2008-10-28 Paolo Bonzini use a shell function for AC_TYPE_INTx_T diff --git a/lib/m4sugar/m4sugar.m4 b/lib/m4sugar/m4sugar.m4 index 22c38e8d0..eb5d4260c 100644 --- a/lib/m4sugar/m4sugar.m4 +++ b/lib/m4sugar/m4sugar.m4 @@ -535,19 +535,12 @@ m4_define([_m4_bpatsubsts], # includes one-shot initialization that is later popped to the normal # definition. # -# The recursive worker destructively swaps the order of a stack. We -# use a temporary stack, and swap directions twice, using the third -# argument to restore the original stack. -# -# Some macros simply can't be renamed with this method: namely, -# anything involved in the implementation of _m4_copy. +# Some macros simply can't be renamed with this method: namely, anything +# involved in the implementation of m4_stack_foreach and m4_curry. m4_define([m4_copy], [m4_ifdef([$2], [m4_fatal([$0: won't overwrite defined macro: $2])], - [_$0([$1], [m4_tmp])_$0([m4_tmp], [$2], - [m4_pushdef([$1], _m4_defn([m4_tmp]))])m4_ifdef([m4_location($1)], - [m4_define([m4_location($2)], m4_location)])])]) -m4_define([_m4_copy], -[m4_ifdef([$1], [m4_pushdef([$2], _m4_defn([$1]))$3[]_m4_popdef([$1])$0($@)])]) + [m4_stack_foreach([$1], [m4_curry([m4_pushdef], [$2])])m4_ifdef([m4_location($1)], +[m4_define([m4_location($2)], m4_location)])])]) # m4_define_default(MACRO, VALUE) @@ -603,32 +596,17 @@ m4_define([m4_defn], [m4_foreach([_m4_macro], [$@], [$0(_m4_defn([_m4_macro]))])])]) -# _m4_dumpdefs_up(NAME) -# --------------------- -m4_define([_m4_dumpdefs_up], -[m4_ifdef([$1], - [m4_pushdef([_m4_dumpdefs], _m4_defn([$1]))dnl -m4_dumpdef([$1])dnl -_m4_popdef([$1])dnl -_m4_dumpdefs_up([$1])])]) - - -# _m4_dumpdefs_down(NAME) -# ----------------------- -m4_define([_m4_dumpdefs_down], -[m4_ifdef([_m4_dumpdefs], - [m4_pushdef([$1], _m4_defn([_m4_dumpdefs]))dnl -_m4_popdef([_m4_dumpdefs])dnl -_m4_dumpdefs_down([$1])])]) - - # m4_dumpdefs(NAME) # ----------------- # Similar to `m4_dumpdef(NAME)', but if NAME was m4_pushdef'ed, display its # value stack (most recent displayed first). +# +# This macro cheats, because it relies on the current definition of NAME +# while the second argument of m4_stack_foreach_lifo is evaluated (which +# would be undefined according to the API). If m4_dumpdef is ever rewritten +# not to use the builtin, revisit this. m4_define([m4_dumpdefs], -[_m4_dumpdefs_up([$1])dnl -_m4_dumpdefs_down([$1])]) +[m4_stack_foreach_lifo([$1], [m4_dumpdef([$1])m4_ignore])]) # m4_popdef(NAME) @@ -1189,6 +1167,32 @@ m4_define([m4_map_args_pair], [$1([$3], [$4])[]$0([$1], [$2], m4_shift(m4_shift3($@)))])]) +# m4_stack_foreach(MACRO, FUNC) +# m4_stack_foreach_lifo(MACRO, FUNC) +# ---------------------------------- +# Pass each stacked definition of MACRO to the one-argument macro FUNC. +# m4_stack_foreach proceeds in FIFO order, while m4_stack_foreach_lifo +# processes the topmost definitions first. In addition, FUNC should +# not push or pop definitions of MACRO, and should not expect anything about +# the active definition of MACRO (it will not be the topmost, and may not +# be the one passed to FUNC either). +# +# The recursive worker _m4_stack_reverse destructively swaps the order of a +# stack. We use a temporary stack, and swap directions twice. Some macros +# simply can't be examined with this method: namely, anything involved +# in the implementation of _m4_stack_reverse. +m4_define([_m4_stack_reverse], +[m4_ifdef([$1], [m4_pushdef([$2], _m4_defn([$1]))$3[]_m4_popdef([$1])$0($@)])]) + +m4_define([m4_stack_foreach], +[_m4_stack_reverse([$1], [m4_tmp-$1])]dnl +[_m4_stack_reverse([m4_tmp-$1], [$1], [$2(_m4_defn([m4_tmp-$1]))])]) + +m4_define([m4_stack_foreach_lifo], +[_m4_stack_reverse([$1], [m4_tmp-$1], [$2(_m4_defn([m4_tmp-$1]))])]dnl +[_m4_stack_reverse([m4_tmp-$1], [$1])]) + + ## --------------------------- ## ## 9. More diversion support. ## ## --------------------------- ## diff --git a/tests/m4sugar.at b/tests/m4sugar.at index 959b209da..10ebbd8be 100644 --- a/tests/m4sugar.at +++ b/tests/m4sugar.at @@ -37,6 +37,60 @@ AT_CHECK_M4SUGAR([-o-],, [$2], [$3]) ])# AT_CHECK_M4SUGAR_TEXT +## ------------------ ## +## m4_stack_foreach. ## +## ------------------ ## + +AT_SETUP([m4@&t@_stack_foreach]) + +AT_KEYWORDS([m4@&t@_stack_foreach_lifo m4@&t@_copy m4@&t@_n]) + +# Test the semantics of macros to walk stacked macro definitions. +AT_CHECK_M4SUGAR_TEXT([[dnl +m4_pushdef([abc], [def])dnl +m4_pushdef([abc], [ghi])dnl +m4_pushdef([abc], [jkl])dnl +m4_stack_foreach([abc], [m4_n]) +abc +m4_stack_foreach_lifo([abc], [m4_n]) +m4_stack_foreach([abc], [m4_n]) +m4_copy([abc], [foo])dnl +m4_stack_foreach([foo], [m4_n]) +m4_stack_foreach_lifo([foo], [m4_n]) +m4_pushdef([xyz], [123])dnl +m4_pushdef([xyz], [456])dnl +m4_define([doit], [[$1](m4_shift(m4_stack_foreach([xyz], [,m4_echo]))) +])dnl +m4_stack_foreach([abc], [doit])]], +[[def +ghi +jkl + +jkl +jkl +ghi +def + +def +ghi +jkl + +def +ghi +jkl + +jkl +ghi +def + +def(123,456) +ghi(123,456) +jkl(123,456) +]]) + +AT_CLEANUP + + ## --------- ## ## m4_defn. ## ## --------- ##