On Thu, Mar 21, 2024, 17:20 Kaz Kylheku <k...@kylheku.com> wrote: > On 2024-03-20 16:34, rep.dot....@gmail.com wrote: > > On 19 March 2024 18:27:13 CET, Kaz Kylheku <k...@kylheku.com> wrote: > >>On 2024-03-18 00:30, Jonathan Wakely wrote: > >>> I don't have an opinion on the implementation, or the proposal itself, > >>> except that the implementation seems susprisingly simple, which is > >>> nice. > >> > >>Hi Jonathan, > >> > >>Here is an updated patch. > >> > >>It rebased cleanly over more than newer 16000 commits, suggesting > >>that the area in the cpp code is "still waters", which is good. > >> > >>I made the documentation change not to recommend using #if, but > >>#ifdef. > >> > >>I got rid of the ChangeLog changes, and also tried to pay more > >>attention to the log message format, where the ChangeLog pieces > >>are specified. > >> > >>In the first test case, I had to adjust the expected warning text > >>for two lines. > >> > > > > Please forgive the bike shedding, but __EXP_COUNTER__ would lead me into > thinking about exponents or thereabouts. > > __MACRO_EXPANSION_COUNTER__ is more what your patch is about, IMHO? > Maybe you could come up with a more descriptive name, please? > > > > And, while I can see what could possibly be done with that, I'm not > really convinced that it would be a wise idea to (unilaterally) support > that idea. Don't you think that this would encourage producing more > spaghetti code? > > > > Just curious about real world motivating examples I guess. > > cheers > > Hi, (Bernhard?) > > Concerns about naming are very important; not bike shedding at all. > I changed the patch to use __EXPANSION_NUMBER__. I didn't include MACRO > because I hope it's clear that in preprocessing, we are expanding > macros. The parent symbol is now called __PARENT_EXPANSION_NUMBER__. > > I dropped the COUNTER terminology because the existing __COUNTER__ > is a symbol whose value changes each time it is mentioned, > These symbols are not like that; they capture a fixed value in > a scope and behave like ordinary macros. > > In doing the renaming, I noticed that from the beginning I've already > been calling the internal value in the macro context macro->exp_number, > because it's not a counter. > > The focus of this feature isn't to enable some new "earth-shattering" > techniques, but to improve certain situations in existing macros. > > For instance, suppose we have a macro that expands to some block > of code in which there is an internal goto. If we have it > > #define MAC(...) { ... goto _label; ... __label: ; } > > then this cannot be used twice in the same function; labels have > function scope.
In this case why can't you use gcc's already extension of defining a local label? https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Local-Labels.html This extension has been around for over 20 years specifically for that use case. Thanks, Andrew If we make it > > #define MAC(...) { ... goto CAT(__label, __LINE__); ... CAT(__label, > __LINE__): ; } > > we now can use MAC two or more times in the same function, but not in > the same line of code. > > With __EXPANSION_NUMBER__ it is doable. Given this program: > > #define xcat(A, B) A ## B > #define cat(A, B) xcat(A, B) > #define lab(PREFIX) cat(PREFIX, __PARENT_EXPANSION_NUMBER__) > > #define MAC { goto lab(foo); /*...*/ lab(foo): ; } > > MAC MAC MAC > > We get the preprocessed output (with -E): > > { goto foo3; foo3: ; } { goto foo10; foo10: ; } { goto foo17; foo17: ; } > > There are issues with relying on __LINE__ to produce different values > when it is referenced in code generated by a macro. > > The following program prints the same value 12 three times; even though > PRINT seems to be referenced on different physical lines in the > PRINT3 macro replacement text. __LINE__ references the line where > the top-level expansion of PRINT3 occurs, not where PRINT occurs. > > #include <stdio.h> > > #define PRINT (printf("%d\n", __LINE__)) > > #define PRINT3 do { \ > PRINT; \ > PRINT; \ > PRINT; \ > } while (0) > > int main() > { > PRINT3; > return 0; > } >