Re: Any basic cpp capability m4 cannot do?

Daniel Goldman <[email protected]> Thu, 08 May 2014 11:32:25 -0700
Newsgroups gmane.comp.gnu.m4.general
Message-ID <[email protected]>
Hi Raphael,

Thanks, you're right. I looked into this. For stringifying an argument, 
if the sequence does not contain strings, they work the same:

$ cat stringify-01.h
#define DEBUG(var, txt) debug (var, #var ": " txt)
DEBUG(var_1, "Description of var 1")

$ cat stringify-01.m4
m4_define(DEBUG, `debug ($1, "$1" ": " $2)')
DEBUG(var_1, "Description of var 1")

$ cpp -P stringify-01.h
debug (var_1, "var_1" ": " "Description of var 1")

$ m4 -P stringify-01.m4 | grep -v "^ *$"
debug (var_1, "var_1" ": " "Description of var 1")

--------

And both can stringify the contents of other macros:

$ cat stringify-03.h
#define STRINGIFY(txt) HIDDEN_STRINGIFY(txt)
#define HIDDEN_STRINGIFY(txt) #txt
STRINGIFY(XXX)
#define XXX YYY
STRINGIFY(XXX)

$ cat stringify-03.m4
m4_define(STRINGIFY, "$1")
STRINGIFY(XXX)
m4_define(XXX, YYY)
STRINGIFY(XXX)

$ cpp -P stringify-03.h
"XXX"
"YYY"

$ m4 -P stringify-03.m4 | grep -v "^ *$"
"XXX"
"YYY"

-----------------

But if the sequence already contains strings, cpp "knows" to escape the 
internal double quotes, m4 apparently does not:

$ cat stringify-04.h
#define STRINGIFY(txt) HIDDEN_STRINGIFY(txt)
#define HIDDEN_STRINGIFY(txt) #txt
#define XXX The "right stuff"
STRINGIFY(XXX)

$ cat stringify-04.m4
m4_define(STRINGIFY, "$1")
m4_define(XXX, The "right stuff")
STRINGIFY(XXX)

$ cpp -P stringify-04.h
"The \"right stuff\""

$ m4 -P stringify-04.m4 | grep -v "^ *$"
"The "right stuff""

Luckily, I have never had to stringify any sequences that contain 
strings. For some reason, this has never come up in my code, so this 
difference will not be a barrier for me. I suppose if this comes up I 
might have to resort to single quotes. :(

Thanks again. Any other differences anyone might suggest, basic 
capabilities cpp can do that m4 cannot do?

Daniel

On 5/7/2014 11:36 PM, Raphael 'kena' Poss wrote:
> Hi Daniel,
>
> You may want to check cpp's ability with # to turn an arbitrary enclosed
> sequence of C tokens into a single C string that is properly quoted.
> Especially if said sequence already contains strings. This I found
> relatively difficult to imitate with m4.
>
> Best,
>