Next: , Previous: , Up: M4 Quotation   [Contents][Index]

F.1.4 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 result of qar is always a literal string, the only time a user can use nested macros is if she relies on an unquoted macro call:

qar(active)
⇒ACT
qar([active])
⇒active

leaving no way for her 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.

Next: , Previous: , Up: M4 Quotation   [Contents][Index]