I’m trying to create a new preprocessor macro directive called #repeat, the point of this directive to to repeatedly inject tokens into the preprocessor wherever it’s called.
It takes two “arguments”, though that term doesn’t really make sense in this context.
the first is the number of times to inject these tokens into the preprocessor, the second is the actual tokens to inject.
My use case is a little convoluted, but I think this feature will be extremely useful.
#define BitBuffer_NumTests 1
#define BitBuffer_TestNames BitBuffer_Test1
_Pragma(“push_macro(“BitBuffer_TestNames”)”)
_Pragma(“redefine_macro(BitBuffer_TestNames BitBuffer_Test2)”)
_Pragma(“redefine_macro(BitBuffer_NumTests BitBuffer_NumTests + 1)”)
/*
BitBuffer_NumTests now equals 2, and BitBuffer_TestNames now contains a stack of two tests, Bitbuffer_Test1 and BitBuffer_Test2
Using pop macro on BitBuffer_TestNames we can expand every single test name by looping with _Pragma("pop_macro("BitBuffer_TestNames “|)”)
I know that this is very convoluted, and the code will be hard to read, but it’s doable, and I strongly think it’s worth while to add these two features, #repeat and _Pragma(redefine_macro())
*/
My question is technical, I’m already writing these extensions myself, and all I need is a little help understanding Clang’s implementation of the preprocessor
So, I’ve written a new PPKeyword in TokenKinds.def for #repeat, and in Clang’s Lexer I’ve written HandleRepeatDirective, I’ve added it to the IdentifierTable, etc.
My question is, when in HandleRepeatDirective, I Lex and expand the first argument, the number of times to repeat and that part is working fine.
I’m having trouble with the second part, basically the second argument is strange, like the example above the _Pragma(redefine_macro("BitBuffer_NumTests BitBuffer_NumTests + 1"))
part, how should I read these tokens?
Previously I was just doing a raw loop making sure that eod wasn’t seen, but it turns out that macros are created by ReadOptionalMacroParameterListAndBody, so I started using that, but I don’t want to create a new macro, so what do I actually do here? how do I expand the tokens repeatedly when the #repeat directive is seen by clang during compilation?