Clang-format: align asterisk (*) pointer declarations with variable name

I use the following parameters in the .clang-format file:

 AlignConsecutiveDeclarations: true PointerAlignment: Right 

The current formatting result is as follows:

 char * var1; SomeOtherType *var2; int var3; 

Expected Result:

 char *var1; //note the changed position of * SomeOtherType *var2; int var3; 

How to configure clang-format to align an asterisk (*) with a variable name and not with a type when I use the AlignConsecutiveDeclarations parameter?

+9
clang clang-format
source share
1 answer

PointerAlignment: Right , unfortunately, is not yet implemented.

See https://github.com/llvm-mirror/clang/blob/release_39/lib/Format/WhitespaceManager.cpp#L327-L340

 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: // const char* const* v1; // float const* v2; // SomeVeryLongType const& v3; AlignTokens(Style, [](Change const &C) { return C.IsStartOfDeclName; }, Changes); } 
+9
source share

All Articles