30

I am using the following options in my .clang-format file:

AlignConsecutiveDeclarations: true
PointerAlignment: Right

The current formatting result is the following:

char *         var1;
SomeOtherType *var2;
int            var3;

The result I was expecting would be:

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

How can I configure clang-format to align the asterix (*) with the variable name rather then with the type when I am using the AlignConsecutiveDeclarations option?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
lanoxx
  • 12,249
  • 13
  • 87
  • 142

2 Answers2

28

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:
  //   const char* const* v1;
  //   float const* v2;
  //   SomeVeryLongType const& v3;

  AlignTokens(Style, [](Change const &C) { return C.IsStartOfDeclName; },
              Changes);
}
lanoxx
  • 12,249
  • 13
  • 87
  • 142
bjori
  • 2,064
  • 1
  • 15
  • 15
7

It's fixed now!

The review https://reviews.llvm.org/D27651 has been re-applied in https://reviews.llvm.org/D103245 and committed in https://reviews.llvm.org/rG3e333cc82e42e1e2ecc974d896489eebe1a5edc2.

This change will be included in LLVM 13 release.

Marek Kurdej
  • 1,459
  • 1
  • 17
  • 36
  • Hi Marek, It doesn't work for me. I am using clang-format 13.0.0. I use "PointerAlignment: Right", but the asterisk is still aligned to the left - to the type name. – Alexey Polonsky Dec 30 '21 at 10:12
  • Alexey, you should report a bug then. https://github.com/llvm/llvm-project/labels/clang-format – Marek Kurdej Jan 01 '22 at 18:49
  • Yes. Ubuntu Focal with Clang-format 12 does not do "Right" but Emacs with `lsp-mode` with `clangd` and `clang-format` 13 does. – thoni56 Jul 11 '22 at 08:49