clang-format Style Options 日本語翻訳 & サンプル集 #clang #llvm
随時更新予定
概要
clangにはclang-formatと言うフォーマッタが存在しておりC言語,C++,Objective Cのインデントなどのファイルフォーマットを自動的に揃えることができます。(vim,emacs,SublimeText向けプラグインも用意されています。)
これらのフォーマットスタイルを詳細に設定可能なのですが、各設定項目が具体的にどういうものかイメージできないので、実際に試して確認していきたいと思います。
いずれはすべてのオプションの意味を理解したいのですが、まずは標準で用意されている LLVM, Google, Chromium, Mozilla, WebKitのフォーマットの差分から特徴的なオプションから日本語訳していきたいと思います。 またC++だけに適用されるフォーマットの優先順位は下げています。
デフォルトオプションの確認方法
以下のようなコマンドを実行すると、styleに指定したオプションが具体的にどのようなものかわかります。
clang-format -style=llvm -dump-config
clang-format Style Options
AccessModifierOffset
intのためマイナス指定可能 publicのindent幅? よくわからずAlignEscapedNewlinesLeft
よくわからずAllowAllParametersOfDeclarationOnNextLine
Allow putting all parameters of a function declaration onto the next line even if BinPackParameters is false.
???BinPackParameters
If false, a function call's or function definition's parameters will either all be on the same line or will have one line each.
???AllowShortIfStatementsOnASingleLine
true or false
括弧なしのif文を1行にするかどうか
- if (a) - printf("sample"); + if (a) printf("sample");
- AllowShortLoopsOnASingleLine
true or false
括弧なしのwhile文を1行にするかどうか
- while (a) - printf("sample"); + while (a) printf("sample");
AlwaysBreakTemplateDeclarations
for C++
If true, always break after the template<...> of a template declaration.AlwaysBreakBeforeMultilineStrings
???
If true, always break before multiline string literals.ConstructorInitializerAllOnOneLineOrOnePerLine
for C++ ?SpacesInParentheses
true or false
"("の後と")"の前にスペースを入れる
-switch (a) { +switch ( a ) {
- SpaceInEmptyParentheses
true or false
空白括弧()の中にスペースを入れる
- int test() { + int test( ) {
- SpacesInCStyleCastParentheses キャスト時の括弧()の中にスペースを入れる。
- int abc = (int)def; + int abc = ( int )def;
- SpaceAfterControlStatementKeyword 制御文の後にスペースを入れる。
- switch(a) { + switch (a) {
- SpaceBeforeAssignmentOperators 演算子の前にスペースを入れる。
- int a= 1; + int a = 1;
- IndentCaseLabels
true or false
caseラベルをswitch文からインデントするかどうか。
switch (a) { - case 1: - printf("1"); - break; + case 1: + printf("1"); + break; }
- BreakBeforeBraces
中括弧{}のスタイル設定
Attach, Linux, Stroustrup, Allman or GNU
Attach
-int test12345(int a, int b, int c) -{ - if (a == 1) - { - printf("abc"); - } - else { +int test12345(int a, int b, int c) { + if (a == 1) { + printf("abc"); + } else { printf("def"); } return 1; } -int test1111(int a){ - if (a == 1){ +int test1111(int a) { + if (a == 1) { printf("a"); - }else{ + } else { printf("d"); } return 0; }
Linux
int test12345(int a, int b, int c) { - if (a == 1) - { - printf("abc"); - } - else { + if (a == 1) { + printf("abc"); + } else { printf("def"); } return 1; } -int test1111(int a){ - if (a == 1){ +int test1111(int a) +{ + if (a == 1) { printf("a"); - }else{ + } else { printf("d"); } return 0; }
Stroustrup
int test12345(int a, int b, int c) { - if (a == 1) - { - printf("abc"); - } - else { + if (a == 1) { + printf("abc"); + } else { printf("def"); } return 1; } -int test1111(int a){ - if (a == 1){ +int test1111(int a) +{ + if (a == 1) { printf("a"); - }else{ + } else { printf("d"); } return 0; }
Allman
int test12345(int a, int b, int c) { if (a == 1) - { - printf("abc"); - } - else { + { + printf("abc"); + } + else + { printf("def"); } return 1; } -int test1111(int a){ - if (a == 1){ +int test1111(int a) +{ + if (a == 1) + { printf("a"); - }else{ + } + else + { printf("d"); } return 0; }
GNU
いわゆるGNUスタイルとは少し違うような?
-int test12345(int a, int b, int c) -{ - if (a == 1) - { - printf("abc"); - } - else { +int test12345(int a, int b, int c) { + if (a == 1) { + printf("abc"); + } else { printf("def"); } return 1; } -int test1111(int a){ - if (a == 1){ +int test1111(int a) { + if (a == 1) { printf("a"); - }else{ + } else { printf("d"); } return 0; }
BreakBeforeBinaryOperators
2項演算子の改行に関する設定、詳細未確認BreakBeforeTernaryOperators
3項演算子の改行に関する設定、詳細未確認BreakConstructorInitializersBeforeComma
???PenaltyReturnTypeOnItsOwnLine ? 関数定義の返り値の方指定を1行に書くかどうか? けどint指定だから別の意味のような気がする。 よくわからず
PointerBindsToType
*や&を型の右に配置(true),変数の左に配置(false) true
- int *tset; + int* test; int* abc;
false
int *tset; -int* abc; +int *abc;
DerivePointerBinding
ファイルを解析して最も多い配置に従って、*や&の位置を決定する。 PointerBindsToTypeを自動判定するようなもの?SpacesBeforeTrailingComments
命令とコメントの間のスペース幅。1行内にコメントを記述する場合。
1を指定した場合
-int *tset;/* test value */ +int *tset; /* test value */
- AlignTrailingComments
true or false
命令の後に続くコメントが複数行続く場合にアラインを整える。
int *tset; /* test value */ -int *abc; /* abc value */ +int *abc; /* abc value */
IndentFunctionDeclarationAfterType
If true, indent when breaking function declarations which are not also definitions after the type.
詳細不明 関数宣言に関するインデント?Standard サポート言語
Cpp03, Cpp11 or Auto
C89とかはないようです。IndentWidth
インデント幅ContinuationIndentWidth
行継続のためのインデント幅
Indent width for line continuations. 詳細不明TabWidth
タブの幅UseTab
タブを使うかどうか
Never, Always or ForIndentationColumnLimit
行の最大文字数
ここで指定した文字数を1行で超えると改行される。MaxEmptyLinesToKeep
最大空白行数
指定前
printf("test"); return 1;
1指定
printf("test"); return 1;
参考
ClangFormat — Clang 3.5 documentation
Clang-Format Style Options — Clang 3.5 documentation