LLVM Discussion Forums

Clang-format macros formatting

First off, I apologize, since I am new to this community and may be making silly mistakes in how, where and what I post.

Having experimented with clang-format for a bit, I notice that support for various kinds of macro-related special cases seems missing, maybe because it would require lots one-off work from the maintainers. For example, you have a way to say that a macro behaves like ‘namespace X’ but to specify that a macro behaves like ‘class A {’ from the formatting standpoint seems impossible (I hope someone will correct me here). There are a few discussions floating around on that topic, but nowhere did I find anything that suggests this might change in the near future.

There are some cases of this in my codebase, and I think there is a simple solution that would actually solve this issue once and for all. Wondering if there is a way to make a proposal on this somewhere, or if this even makes sense.

The idea is simple. If you have a macro, say

#define DEF_TEMPLATE_CHILD(a,b,c) template class a: public c {

why can’t we also define a macro for the purpose of clang-format, something like:

DefineMacros:

  • Name: DEF_TEMPLATE_CHILD
    Args: true
    Equivalent: ‘class A {’

The ‘Equivalent’ part can be simple and static, it would just be enough to control the context of formatting. Then every time clang-format encounters DEF_TEMPLATE_CHILD(a,b,c) it would treat it as if it were ‘class A {’. Seems like this could solve the problem for all kinds of special cases in a way where no additional work would ever be required.

Again, apologies if I’m missing something.