[clang] [clang-format] Support fine-grained alignment of variable declarations (PR #160270)

James Henderson via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 7 02:54:32 PDT 2025


jh7370 wrote:

> > This allows disabling variable alignment declarations while still having function alignment. It also distinguishes between class member variables and other variables (e.g. function local variables).
> 
> See https://clang.llvm.org/docs/ClangFormatStyleOptions.html#adding-additional-style-options. I'm more interested in the first two bullets there.

Thanks, I wasn't aware of that documentation. I'll take a look and try to identify existing examples of this style in open source - my own specific use case is an entirely proprietary project, so there won't be a public style guide to go on.

> If we decide to go forward with enabling finer controls of aligning consecutive declarations of variables, we probably want to add an enum/struct option to `AlignConsecutiveDeclarations`.
> 
> An enum example:
> 
> ```
> AlignConsecutiveDeclarations:
>   # excluding function pointers?
>   AlignVariableDeclarations: Always # or DataMembers or Never
> ```
> 
> A struct example:
> 
> ```
> AlignConsecutiveDeclarations:
>   AlignVariableDeclarations: # including function pointers?
>     GlobalVariables: true
>     LocalVariables: true
>     DataMembers: true
> ```

Fair enough. I did originally consider an enum approach, but found it a bit messy at first, but that was largely due to lack familiarity with clang-format. When I have some more time to continue working on this PR, I'll look at how practical that approach is. My soft feeling is that the struct approach may be nicer overall.

https://github.com/llvm/llvm-project/pull/160270


More information about the cfe-commits mailing list