clang-format: ポインタ宣言のアスタリスク (*) を変数名に揃える 質問する

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?

ベストアンサー1

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

おすすめ記事