GNU Info

Info Node: (autoconf.info)Quotation and Nested Macros

(autoconf.info)Quotation and Nested Macros


Next: Changequote is Evil Prev: One Macro Call Up: M4 Quotation
Enter node , (file) or (file)node

Quotation and Nested Macros
---------------------------

   The examples below use the following macros:

     define([car], [$1])
     define([active], [ACT, IVE])
     define([array], [int tab[10]])

   Each additional embedded macro call introduces other possible
interesting quotations:

     car(active)
     =>ACT
     car([active])
     =>ACT, IVE
     car([[active]])
     =>active

   In the first case, the top level looks for the arguments of `car',
and finds `active'.  Because `m4' evaluates its arguments before
applying the macro, `active' is expanded, which results in:

     car(ACT, IVE)
     =>ACT

In the second case, the top level gives `active' as first and only
argument of `car', which results in:

     active
     =>ACT, IVE

i.e., the argument is evaluated _after_ the macro that invokes it.  In
the third case, `car' receives `[active]', which results in:

     [active]
     =>active

exactly as we already saw above.

   The example above, applied to a more realistic example, gives:

     car(int tab[10];)
     =>int tab10;
     car([int tab[10];])
     =>int tab10;
     car([[int tab[10];]])
     =>int tab[10];

Huh?  The first case is easily understood, but why is the second wrong,
and the third right?  To understand that, you must know that after `m4'
expands a macro, the resulting text is immediately subjected to macro
expansion and quote removal.  This means that the quote removal occurs
twice--first before the argument is passed to the `car' macro, and
second after the `car' macro expands to the first argument.

   As the author of the Autoconf macro `car', you then consider it to
be incorrect that your users have to double-quote the arguments of
`car', so you "fix" your macro.  Let's call it `qar' for quoted car:

     define([qar], [[$1]])

and check that `qar' is properly fixed:

     qar([int tab[10];])
     =>int tab[10];

Ahhh!  That's much better.

   But note what you've done: now that the arguments are literal
strings, if the user wants to use the results of expansions as
arguments, she has to use an _unquoted_ macro call:

     qar(active)
     =>ACT

where she wanted to reproduce what she used to do with `car':

     car([active])
     =>ACT, IVE

Worse yet: she wants to use a macro that produces a set of `cpp' macros:

     define([my_includes], [#include <stdio.h>])
     car([my_includes])
     =>#include <stdio.h>
     qar(my_includes)
     error-->EOF in argument list

   This macro, `qar', because it double quotes its arguments, forces
its users to leave their macro calls unquoted, which is dangerous.
Commas and other active symbols are interpreted by `m4' before they are
given to the macro, often not in the way the users expect.  Also,
because `qar' behaves differently from the other macros, it's an
exception that should be avoided in Autoconf.


automatically generated by info2www version 1.2.2.9