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

标签 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 选项?

最佳答案

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

关于clang-format:将指针声明的星号 (*) 与变量名对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38392889/

相关文章:

c++ - 有没有办法使用 clang-format 来做 "indentation only"?

kivy - 尝试在 Kivy Complete VM 上生成简单的 apk 时出错

c - 警告 : implicit declaration of function 'fsync' is invalid in C99

c++ - 有没有办法强制使用 "this->"用于 clang-format/clang-tidy 中的类成员/方法?

c++ - 如何标记一个区域以使 clang-format 不会触及它?

c++ - 有没有办法禁用 clang 格式的 "SpacesInBraces"?

Python 性能 : Clang vs gcc

clang - 获取llvm中局部变量的实际值

c++ - 通过 GNU 扩展 asm 约束加载 64 位整数常量?

c++ - C++ 中结构数组的 clang 格式奇怪缩进