Searching through the Clang-Format Style Options, I can't seem to find a way to control the behavior on the placement of C++ attributes.
As an example, take these two declarations, the first of which does not overflow the column limit and the second of which does:
template <typename TChar>
[[gnu::always_inline]]
static ptr<TChar> within_limit(ptr<TChar> first, ptr<TChar> last);
template <typename TChar, typename FApply, typename... FApplyRest>
[[gnu::always_inline]]
static ptr<TChar> overflow(ptr<TChar> first, ptr<TChar> last, const FApply& apply, const FApplyRest&... apply_rest);
No matter how I tweak my .clang-format
, the output is some variant of this:
[[gnu::always_inline]] static ptr<TChar> within_limit(ptr<TChar> first, ptr<TChar> last);
[[gnu::always_inline]] static ptr<TChar>
overflow(ptr<TChar> first, ptr<TChar> last, const FApply& apply, const FApplyRest&... apply_rest);
Having the attributes on the same line as the type is rather unreadable (to me), so I would prefer clang-format
not do this. Using __attribute__((always_inline))
exhibits the same behavior. Specifying multiple attributes in a single list ([[noreturn, gnu::cold]]
) causes reformatting (to [[ noreturn, gnu::cold ]]
for reasons unclear to me). The formatter has at least some basic understanding of attributes.
SO: Is there a way to get clang-format
to put attributes on their own line (the C++ equivalent to BreakAfterJavaFieldAnnotations
)?
Attempted Workarounds
Use of // clang-format off
/// clang-format on
is an okay stopgap, but it is definitely too ham-handed for a permanent solution. I still want the declaration formatted properly. Aside from that, the project requires the use of a lot of attributes, so having clang-format
comments everywhere is arguably less readable.
Use of CommentPragmas
theoretically would allow me to be more localized in disabling, but the output is still quite odd:
template <typename TChar>
[[gnu::always_inline]] // NO-FORMAT: Attribute
static ptr<TChar>
within_limit(ptr<TChar> first, ptr<TChar> last);
[[nodiscard]]
attribute specifier. Any workarounds using other clang-format properties? – Matthiasclang-format
v12.0.0?! I really want to use[[nodiscard]]
, but this missing feature "destroys" our nicely aligned functions using trailing return types:[[nodiscard]] auto f() -> void;
– Florian Wolters