[clang] [clang-format] Macro list options now accept regular expressions (PR #242355)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 07:55:02 PST 2026
t-a-james wrote:
> Couldn't you just write a small script which greps for your regexes and produces the actual list, which can then be put into the .clang-format. Same regexes, no manual maintaining (maybe manual invokes, if you still add new macros)
That's what we have at the moment, but it feels like a work-around for missing functionality in the formatter. Whenever a developer adds or removes one of these macros, their code is formatted incorrectly until they fix `.clang-format`. This happens relatively regularly, so there _is_ manual maintenance.
We think it's easier to ask them to re-run the script rather than manually edit our (1500+ line) `.clang-format` file. This is an extra step in the developer workflow that I feel exposes more of our plumbing than seems right.
> maybe OneLineFormatOffRegex can be used?
This is an interesting suggestion, and one that I'd not thought of. It might be a cleaner work-around than what we currently have, but it still feels like a work-around.
> others have to pay the price [of the] exploding Macros container
In the current implementation, the `Macros` container only gets populated if `!MacroPatterns.empty()`. Only people using regexes to catch macros pay the price - you only pay for what you use.
@matthew-j-code, you showed me some data on cache sizes earlier, which to my eyes looked very reasonable. Could you please tidy that up and post it here, with a short explanation of how you calculated it?
@HazardyKnusperkeks , I notice that the main branch currently has this:
```
if (MacroBlockBeginRegex.match(Text))
FormatTok->setType(TT_MacroBlockBegin);
else if (MacroBlockEndRegex.match(Text))
FormatTok->setType(TT_MacroBlockEnd);
```
If we weren't caching, this is exactly how we'd handle our regexes (with the `!MacroPatterns.empty()` guard, of course). Given that calling `match` without a cache is already used in clang-format, would you prefer that solution?
https://github.com/llvm/llvm-project/pull/242355
More information about the cfe-commits
mailing list