clang-format
説明
C++ の著名なフォーマッターである clang-format についてです。
2つの .clang-format ファイルを置いておきます。
clang-format 18 用です。それ以前の clang-format では使用できません。
1つ目は tomolatoon が使っている .clang-format
2つ目は Siv3D に近い形の .clang-format
まだ用意していない
clang-format の導入
Mac/Linux 環境については、筆者の手元に環境がないので割愛します。
Windows 環境は次のようにします。
インストールフォルダから見て.\LLVM\bin\に clang-format.exe があります。
デフォルトではC:\LLVM\Program Files\LLVM\bin
clang-format.exe は適当な場所に移すなど好きなようにします。
clang-format.exe の在り処を Visual Studio なり Visual Studio Code に教えます。
Visual Studio:
「ツール > オプション」
「テキスト エディター > C/C++ > 書式設定 > 全般 > clang-format.exe へのカスタム パスを使用します」
.clang-format ファイルをソリューションフォルダか、それよりも親のフォルダーに置きます。
Visual Stduio Code:
プロジェクトを開いたら、そのプロジェクトの.vscode/settings.jsonを開く(or 追加)。
その JSON ファイルに、C_Cpp.Clang_format_pathとして設定。]
.clang-format ファイルをプロジェクトフォルダに置きます。
.clang-format のオプション一覧に関する事柄
.clang-format ファイルは YAML で書かれています。でもファイル名は .clang-format にします。
本家。
Google 翻訳なり DeepL 翻訳で翻訳しながらで十分読めます。
次にリリース予定のバージョンのオプションも書かれています。
code:tomlatoon_clang-format.yaml
# メタ
DisableFormat: false
Language: Cpp
Standard: Latest
BasedOnStyle: LLVM
# タブ・インデント
TabWidth: 4
UseTab: AlignWithSpaces
IndentWidth: 4
ContinuationIndentWidth: 4
# EOF
LineEnding: DeriveCRLF
InsertNewlineAtEOF: true
# 行・改行
ColumnLimit: 175
AlignEscapedNewlines: Right
MaxEmptyLinesToKeep: 2
KeepEmptyLinesAtEOF: true
KeepEmptyLinesAtTheStartOfBlocks: false
# 宣言
## class
AccessModifierOffset: -4
PackConstructorInitializers: NextLineOnly
ConstructorInitializerIndentWidth: 4
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
EmptyLineAfterAccessModifier: Always
EmptyLineBeforeAccessModifier: Always
IndentAccessModifiers: false
AllowShortCompoundRequirementOnASingleLine: true
## 関数
AllowShortFunctionsOnASingleLine: Empty
IndentWrappedFunctionNames: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllArgumentsOnNextLine: false
BinPackParameters: false
BinPackArguments: false
AllowBreakBeforeNoexceptSpecifier: OnlyWithParen
## ラムダ式
AllowShortLambdasOnASingleLine: Inline
LambdaBodyIndentation: Signature
## 名前空間
CompactNamespaces: false
FixNamespaceComments: true
NamespaceIndentation: All
ShortNamespaceLines: 0
## Template, Concepts
IndentRequiresClause: false
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
BreakBeforeConceptDeclarations: Always
## enum
AllowShortEnumsOnASingleLine: false
# 式・宣言
SeparateDefinitionBlocks: Always
## リスト区切りのもの(初期化リスト・関数引数など)
Cpp11BracedListStyle: true
AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: Right
## リテラル
AlwaysBreakBeforeMultilineStrings: false
BreakAdjacentStringLiterals: true
BreakStringLiterals: true
IntegerLiteralSeparator:
Binary: 8
Decimal: 6
Hex: 4
## 演算子の揃え
AlignOperands: AlignAfterOperator
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
BreakBeforeBinaryOperators: All
BreakBeforeTernaryOperators: true
# Enabled: true
# AcrossEmptyLines: false
# AcrossComments: true
# Enabled: true
# AcrossEmptyLines: false
# AcrossComments: true
## Bit Field
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
BitFieldColonSpacing: After
## Using 宣言
SortUsingDeclarations: LexicographicNumeric
## 修飾子
QualifierAlignment: Custom
## ポインタ・参照
DerivePointerAlignment: false
PointerAlignment: Left
ReferenceAlignment: Pointer
SpaceAroundPointerQualifiers: Default
# ブロック
AllowShortBlocksOnASingleLine: Empty
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
# before-clang-format18; AfterFunction: true (requries 節の扱い)
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentExternBlock: AfterExternBlock
# スペース
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: false
SpaceBeforeInheritanceColon: false
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDeclarationName: false
AfterFunctionDefinitionName: false
AfterIfMacros: true
AfterOverloadedOperator: false
AfterRequiresInClause: true
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParens: Never
SpacesInSquareBrackets: false
# 制御文
## if, loop, goto
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
IndentGotoLabels: false
InsertBraces: false # これで壊れることも稀にある
## switch
AllowShortCaseLabelsOnASingleLine: true
AlignConsecutiveShortCaseStatements:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCaseColons: false
IndentCaseBlocks: true
IndentCaseLabels: false
# 属性・マクロ
SkipMacroDefinitionBody: false # 場合に寄る
BreakAfterAttributes: Always
IndentPPDirectives: AfterHash
PPIndentWidth: 1
AttributeMacros: [
"WINAPI",
"CALLBACK"
]
ForEachMacros: [
"BOOST_FOREACH",
"rep", "rrep", "reps", "rreps",
"TEST_MODULE_INITIALIZE", "TEST_CLASS_INITIALIZE", "TEST_METHOD_INITIALIZE",
"TEST_MODULE_CLEANUP", "TEST_METHOD_CLEANUP", "TEST_CLASS_CLEANUP",
"TEST_CLASS", "TEST_METHOD"
]
MacroBlockBegin: "^_STD_BEGIN|^BOOST_*_BEGIN|^TOMOLATOON_*_BEGIN"
MacroBlockEnd: "^_STD_END|^BOOST_*_END|^TOMOLATOON_*_END"
StatementAttributeLikeMacros: "emit" StatementMacros: [
"TEST_DESCRIPTION", "TEST_OWNER", "TEST_PRIORITY", "TEST_WORKITEM", "TEST_IGNORE",
"TEST_MODULE_ATTRIBUTE", "BEGIN_TEST_CLASS_ATTRIBUTE", "TEST_METHOD_ATTRIBUTE"
]
# include
SortIncludes: CaseSensitive
IncludeBlocks: Regroup
IncludeCategories:
Priority: 1
CaseSensitive: true
Priority: 2
Priority: 3
Priority: 4
Priority: 5
- Regex: '.*'
Priority: 6
# コメント
AlignTrailingComments:
Kind: Always
OverEmptyLines: 1
CommentPragmas: "\\*|/|/<"
ReflowComments: true
# JSON
SpaceBeforeJsonColon: false
BreakArrays: true
# # RawStringFormats
# RawStringFormats:
# - Language: Cpp
# BasedOnStyle: WebKit
# - Language: Json
# BasedOnStyle: WebKit