clang-format:将指针声明的星号 (*) 与变量名对齐

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

我在 .clang-format 文件中使用了以下选项:

AlignConsecutiveDeclarations: true
PointerAlignment: Right

当前格式化结果如下:

char *         var1;
SomeOtherType *var2;
int            var3;

我期待的结果是:

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

如何配置 clang-format 使星号 (*) 与变量名对齐,而不是与类型对齐 使用 AlignConsecutiveDeclarations 选项?

PointerAlignment: Right 遗憾的是尚未实现。

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);
}

现已修复!

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

此更改将包含在 LLVM 13 版本中。