Live data from Hacker News

Recursive macros in C, demystified (once the ugly crying stops)

h4x0r.org

41–50 of 90 posts

Re: Recursive macros in C, demystified (once the ugly crying stops)

#42
post #35

Mildly related, sort of, one can prevent expansion of variadic macros as follows: #define printf(...) int (printf)(const char *, ...); I keep on seeing many random code bases just resort to #undef instead...

Doesn't this trigger warnings?

Re: Recursive macros in C, demystified (once the ugly crying stops)

#43

Imagine trying to implement the C preprocessor. I had to write it from scratch 3 times before it worked 100%.

microsoft fixed their broken c preprocessor implementation just a few years ago

They could have just asked me :-)

Re: Recursive macros in C, demystified (once the ugly crying stops)

#44

In many ways being limited ends up being a feature. Even limited as it is, you get some crimes against humanity like the bourne shell source, but at least most people agree it is a bad idea If it allowed more unlimited metaprogramming, building big complex things as macros might well have become popular

The CPP does allow quite advanced metaprogramming, it's just so obtuse to use and requires insane hacks so almost nobody does. See one of my favorite projects https://github.com/hirrolot/metalang99

Re: Recursive macros in C, demystified (once the ugly crying stops)

#45
post #35

Mildly related, sort of, one can prevent expansion of variadic macros as follows: #define printf(...) int (printf)(const char *, ...); I keep on seeing many random code bases just resort to #undef instead...

Doesn't this trigger warnings?

Function like macros literally requires name( , i.e name followed directly by open paren, otherwise no macro substitution occurs. so (name)() will always suppress function like macros (but not non-function ones, i.e regular #define name xxx)

Re: Recursive macros in C, demystified (once the ugly crying stops)

#47
post #28
post #7

The behavior of C macros is actually described by a piece of pseudocode from Dave Prosser and it is not in the standard: * https://www.spinellis.gr/blog/20060626/ * https://www.spinellis.gr/pubs/jrnl/2006-DDJ-Finessing/html/S... * https://gcc.gnu.org/legacy-ml/gcc-prs/2001-q1/msg00495.html

Wow, I'm not sure I've ever seen this (or if I did, it was 20 years ago). And I was definitely looking around for this kind of history when I was searching around when writing. Perhaps my google skills have decayed... or google... or both! Thanks very much.

Oh, are you L33 T.?

Re: Recursive macros in C, demystified (once the ugly crying stops)

#48
I think the C preprocessor was designed after the GPM clone m6, its successor m4, and Ratfor, so I suspect the difficulty in doing things like this is intentional. I guess I should ask McIlroy, who is responsible for pushing m4 to its absolute limits and was present when the C preprocessor was being designed: https://www.cs.dartmouth.edu/~doug/barem4.m4

    _        Pure macros as a programming language
    _
    _ m4 is Turing complete even when stripped to the bare minimum
    _ of one builtin: `define'. This is not news; Christopher
    _ Strachey demonstrated it in his ancestral GPM, described in
    _ "A general- purpose macrogenerator", The Computer Journal 8
    _ (1965) 225-241.
    _
    _ This m4 program more fully illustrates universality by
    _ building familiar programming capabilities: unlimited
    _ precision integer arithmetic, boolean algebra, conditional
    _ execution, case-switching, and some higher-level operators
    _ from functional programming. In support of these normal
    _ facilities, however, the program exploits some unusual
    _ programming idioms:
    _ 
    _ 1. Case-switching via macro names constructed on the fly.
    _ 2. Equality testing by redefining macros.
    _ 3. Representing data structures by nested parenthesized lists.
    _ 4. Using macros as associative memory.
    _ 5. Inserting nested parameter symbols on the fly.
    _
    _ Idioms 2 and 5 are "reflective": the program writes code
    _ for itself.
It's very easy to get into enormous amounts of trouble in m4, m6, or GPM. The C preprocessor is not without its problems, but it is rare that I have difficulty in understanding why a given gcc -E invocation produces the output it does.

Re: Recursive macros in C, demystified (once the ugly crying stops)

#49
post #7

The behavior of C macros is actually described by a piece of pseudocode from Dave Prosser and it is not in the standard: * https://www.spinellis.gr/blog/20060626/ * https://www.spinellis.gr/pubs/jrnl/2006-DDJ-Finessing/html/S... * https://gcc.gnu.org/legacy-ml/gcc-prs/2001-q1/msg00495.html

Thank you very much for providing these links!

Re: Recursive macros in C, demystified (once the ugly crying stops)

#50

Earlier quoted context omitted.

microsoft fixed their broken c preprocessor implementation just a few years ago

They could have just asked me :-)

They were deliberately leaving their C compiler broken because they considered C++ to be the replacement for C.
Post reply on HN