Skip to content

.clang-format: リポジトリ実態に合わせてリファイン - #75

Merged
thawk105 merged 1 commit into
masterfrom
refine-clang-format
May 14, 2026
Merged

.clang-format: リポジトリ実態に合わせてリファイン#75
thawk105 merged 1 commit into
masterfrom
refine-clang-format

Conversation

@thawk105

Copy link
Copy Markdown
Owner

概要

リポジトリ直下の .clang-format は CLion が生成したままで、ccbench の実コードスタイルと乖離していた。実態に合わせてリファインし、現代的でメンテしやすい設定に更新する。

背景: 設定が実態と合っていなかった

cc/ include/ common/ の C++ ソースは 全面的に 2-space indent だが、旧 .clang-formatIndentWidth: 4 だった。このため設定どおりに format すると全ファイルが大量に書き換わる状態になっていた (leading-space ヒストグラムでも 2 の倍数が圧倒的多数)。

変更したオプションと理由

オプション before → after 理由
IndentWidth 42 実コードは 2-space indent。4 のままだと全ファイルが reformat 対象
TabWidth 42 indent 幅に整合
ContinuationIndentWidth 84 2-space ベースに整合 (実測でも diff 最小)
AccessModifierOffset -4-2 public:/private: はメンバから 1 段外。実測で -2 が diff 最小
AllowShortIfStatementsOnASingleLine AlwaysWithoutElse else 付きは展開する方が実態に近い (実測で diff 減)
Standard (なし) → c++20 本リポは C++20。明示してパース挙動を固定
DerivePointerAlignment (なし) → false PointerAlignment: Left をファイル間で固定
SortIncludes (なし) → false include 順は一部 load-bearing (include/common.hh を先頭に置く protocol .cc など)。並べ替えは別途対応
その他 CLion 由来の ObjC 系・冗長/デフォルト同値オプションを削除し、残したものにコメントを付与

clang-format 14 (devcontainer / ubuntu:24.04 同梱バージョン) で有効なオプションのみ使用。clang-format --dump-config がエラーなくパースできることを確認済み。

方針: 「設定ファイルの質を上げる」ことが目的であり、「コードを大量 reformat させる」ことではない。今回の変更はむしろ設定を実態に寄せることで reformat 量を 大幅に削減 する。

before/after の計測

対象: find cc include common -type f \( -name '*.cc' -o -name '*.hh' -o -name '*.cpp' \) (242 ファイル)

dry-run 違反ファイル数

違反ファイル数
before 222 / 242
after 215 / 242

違反ファイル数は増えていない (むしろ微減)。

format 時の総変更行数 (設定が実態にどれだけ合っているかの指標)

clang-format 適用時の変更行数
before 43,231 行
after 20,311 行

IndentWidth の修正だけで reformat 量が半分以下になり、設定が実態に大きく近づいたことを示す。

test plan

  • clang-format --dump-config がエラーなくパースできる
  • clang-format --dry-run --Werrorcc/ include/ common/ 全ソース (242 ファイル) に対して実行し、違反ファイル数を計測 (before 222 → after 215、増加なし)
  • before/after の format 総変更行数を計測 (43,231 → 20,311)
  • IndentWidth / AccessModifierOffset / ContinuationIndentWidth / AllowShortIfStatementsOnASingleLine の候補値を実測比較し、diff 最小の組み合わせを採用

関連

CLion 生成のままだった設定を ccbench の実コードスタイルに合わせて更新。

- IndentWidth を 4 から 2 へ (実コードは全面的に 2-space indent。
  4 のままだと全ファイルが大量 reformat 対象になっていた)
- TabWidth / AccessModifierOffset / ContinuationIndentWidth を 2-space
  ベースに整合
- Standard: c++20 を明示 (本リポは C++20)
- DerivePointerAlignment: false を明示し PointerAlignment: Left を固定
- SortIncludes: false を明示 (include 順は一部 load-bearing なため別途対応)
- 非デフォルト/曖昧なオプションを整理しコメントを付与

clang-format 14 想定。clang-format --dump-config でパース可能なことを確認。
このリポの clang-format 自体への準拠は #63 で CI 化を検討中。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant