clang-format: Align asterisk (*) of pointer declaration with variable name

PointerAlignment: Right is unfortunately not implemented yet. See https://github.com/llvm/llvm-project/blob/master/clang/lib/Format/WhitespaceManager.cpp#L643 void WhitespaceManager::alignConsecutiveDeclarations() { if (!Style.AlignConsecutiveDeclarations) return; // FIXME: Currently we don’t handle properly the PointerAlignment: Right // The * and & are not aligned and are left dangling. Something has to be done // about it, but it raises the question of alignment of code like: … Read more

How to indent after access modifiers with clang-format

Where I work, we’ve stumbled upon the same problem. Since the IndentWidth parameter controls the indentation everywhere (classes, functions, etc.) what you’re trying to achieve seems impossible. The next best thing, in my opinion, is to keep IndentWidth=4 and set AccessModifierOffset=-2. That way you get: class Foo { public: Foo() = default; }; bool foo() … Read more

Can clang-format align a block of #defines for me?

[UPDATE] The op’s pull request finally went through and as of clang version 9.0.0 is live. The functionality is enabled by the AlignConsecutiveMacros: true option. [ORIGINAL] Weirdly enough this feature is yet to be implemented in clang; the formatting option for consecutive macros is currently missing. Many developers are interested and there is a working … Read more

Can clang-format break my code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code. Since the corresponding SortIncludes option is set to true by several of the built-in styles, it might not be obvious that clang-format is going to reorder … Read more

How to auto indent a C++ class with 4 spaces using clang-format?

As near as I can tell, clang-format offers no option for indenting function contents differently from non-access-modifier class contents. That is, consider the following code: class A { public: void foo() {} } void bar() { int a; } In this code, the line “void foo() {}” will always be indented the same amount as … Read more