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

时间:2016-07-15 09:42:41

标签: clang clang-format

我在.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选项?

2 个答案:

答案 0 :(得分:11)

遗憾的是,

PointerAlignment: Right尚未实施。

请参阅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);
}

答案 1 :(得分:3)

现在已经修复了!

审核 https://reviews.llvm.org/D27651 已在 https://reviews.llvm.org/D103245 重新申请并在 https://reviews.llvm.org/rG3e333cc82e42e1e2ecc974d896489eebe1a5edc2 提交。

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