4 ms·
I wrote a useful extension to the C preprocessor for GCC, and submitted it to the gcc-patches mailing list in April. This went unnoticed, as have my subsequent
by kazinator 4y ago
I wrote a useful extension to the C preprocessor for GCC, and submitted it to the gcc-patches mailing list in April. This went unnoticed, as have my subsequent pings since. I'm planning to ping once a month from now on until the rest of 2022, and then switch to quarterly.
https://gcc.gnu.org/pipermail/gcc-patches/2022-April/593473.html https://gcc.gnu.org/pipermail/gcc-patches/2022-April/593473....
__EXP_COUNTER__ gives a macro expansion to a numeric value which uniquely enumerates that expansion.
The sister macro __UEXP_COUNTER__ allows a macro expansion to access the parent's value: if a macro is being expanded in the body of another macro, one level up, it provides the __EXP_COUNTER__ value of that parent macro.
This feature solves the problem of producing unique names in a macro. (Unique within a translation unit.)
The __LINE__ symbol gets abused for this. The problem is that it's not unique. A macro can be called two or more times in the same line of code. Moreover, the same line number like 42 can occur multiple times in the same translation unit due to #include; a line number is not unique within a translation unit.
__COUNTER__ is next to useless because on each access, its value changes. It's useful in a situation in which a name is needed syntactically, and has to be unique, but is otherwise never referenced: just mentioned once and that's it.
Multiple references to __EXP_COUNTER__ in the same macro expansion context produce the same value.
- gpderetta 4y agoI'm probably missing something, but can't you use counter to generate a unique name once and then forward to to another macro so that it can be used multiple times?
- kazinator 4y agoPossibly. Whereas definitions are lazily evaluated, so that if we have: #define COUNT __COUNTER__ each occurrence of COUNT behaves like an invocation of __COUNTER__. But, this is not true of parameters because they get expanded before substitution. $ gcc -E - #define MAC(PARAM) PARAM PARAM PARAM MAC(__COUNTER__) # 1 "<stdin>" # 1 "<built-in>" # 1 "<command-line>" # 31 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 32 "<command-line>" 2 # 1 "<stdin>" 0 0 0 Thus perhaps you may be able to get something like __EXP_COUNTER__ by splitting your macros into interface and implementation: #define MAC_IMPL(A, B, EXP_COUNTER) #define MAC(A, B) MAC_IMPL(A, B, __COUNTER__) I'm guessing this is what you mean by forwarding. This could be a pretty major inconvenience, if you have to do it in the middle of a situation that is already stuffed with preprocessing contortions. Like say you had to define 32 macros that are similar to each other, for whatever reason, and you want this hack: now you have 64.
- gpderetta 4y agoYes the interface/impl split is what I had in mind. Even only moderately complex PP metaprogramming requires multiple rounds of wrapping, so I'm not sure it is such a big burden.
- kazinator 4y agoBy the way, I'm also interested in solving the "no recursive macro" problem hinted at in this submission. While working on __EXP_COUNTER__, I looked into it a bit. The big issue is that the GNU C preprocessor uses global state for tracking expansion. In effect, it takes advantage of the no-recursion rule and says that during a macro's expansion, only one context for that expansion needs to exist. That context is patched into the macro definition, or something like that. (I don't have the code in front of me and it's been a few months.) The preprocessor knows that there is a current macro being expanded, and there is a stack of those; but that is referenced by its static definition, which has a 1:1 relationship to expansion state, like parameters, location and whatnot. That might have to turn into a stack, perhaps; there is a refactoring job there, and the code is a bit of a hornet's nest. In terms of syntax/deployment, it would be easy. I envision that there could be a #defrec directive that is like #define, but which creates a macro that is blessed for recursive expansion. Or other possibilities: #pragma rec(names, of, macros, ...) which is better for code that has to work without the extension, since it uses #define.
- jstimpfle 4y agoAs someone who understands C macros relatively well and who makes frequent use of them, I don't think I understand what __EXP_COUNTER__ does, and how it is different from __COUNTER__. I would have to experiment each time before using it, and would then quickly forget again the intricate details about expansion order, etc., similar to how every time I do some kind of STRINGIFY macro I have to make sure to use the right number of forwarding macro calls. Is there a concrete use case for this that really can't be solved by __LINE__? I've used __LINE__ in the past to generate unique identifiers used in macro-generated code chunks. I don't see that non-uniqueness thing you mentioned causing any problems except for global variables (so not really an issue in my book). As much as I love the C preprocessor as a crude tool that can solve many practical issues that are solved in other languages with a magnitude more complexity (besides solving problems that other languages don't have), I think the value doesn't come from its unintelligible execution model. And if __EXP_COUNTER__ is so difficult to understand, I personally don't like it.
- scratcheee 4y agoThe parent comment explained pretty well the advantages over __LINE__ imo. Crafting macros is often black magic, but using them shouldn't be (if they're well crafted). Having an implicit rule in your macro that it cannot be used twice in the same line is surprising and potentially dangerous. Another example is where you have a macro doing lots of work, if it needs to use a submacro multiple times that itself needs a unique identifier, then __LINE__ is no longer sufficient. __UEXP_COUNTER__ is a little more difficult to imagine a use-case for, I'll admit (I can see it allows passing counters around, but I can't see why a parameter couldn't do the same). Again, preprocessor macros are black magic, these additions seem a lot simpler to understand than `__VA_OPT__` (and its predecessor `##__VA_ARGS__`) or MSVCs awful stringify problems, as you brought up.
- kazinator 4y ago__UEXP_COUNTER__ is required so that you can abstract creating unique symbols: #define MAC(...) WHATEVER UNIQ(X) WHATEVER without __UEXP_COUNTER__, you would have to pass __EXP_COUNTER__ as an extra argument to UNIQ(X). You cannot make a nickname for __EXP_COUNTER__; e.g. #define EC __EXP_COUNTER__ because the expansion of EC itself has its own counter! Using __UEXP_COUNTER__, UNIQ(X) can just do everything necessary: obtain MAC's counter, and combine it with the prefix X.
- aaaaaaaaaaab 4y ago>I'm planning to ping once a month from now on until the rest of 2022, and then switch to quarterly. I would do the opposite: keep halving the ping interval until they notice.
- rwmj 4y agoWe use this (trick taken from stackoverflow) to make __COUNTER__ usefully provide unique, reusable names: https://gitlab.com/nbdkit/nbdkit/-/blob/master/common/include/unique-name.h https://gitlab.com/nbdkit/nbdkit/-/blob/master/common/includ... Example use for MIN and MAX macros which evaluate the parameters once and can be nested to any depth: https://gitlab.com/nbdkit/nbdkit/-/blob/master/common/include/minmax.h https://gitlab.com/nbdkit/nbdkit/-/blob/master/common/includ... I don't know what __EXP_COUNTER__ would add.
- kazinator 4y agoYour NBDKIT_UNIQUE_NAME(name) cannot produce the same name twice because it doesn't take a counter as a parameter. __EXP_COUNTER__ adds the ability for a macro expansion to have its own counter for that expansion instance, without some other macro having to hand it one as a an extra, visible parameter.