ClangFormatスタイルオプション

概要:インデントスタイル(ソースコード整形)ツールであるclang-formatの、スタイル(設定項目)の日本語の概要

ベーススタイル

概要:基本とするコーディングスタイル

具体的な内容がよくわからない場合の参照先

参考

ウィキペディアの 字下げスタイル
Wikipedia の Indent style

注釈

Artistic Style(AStyle) の スタイルオプション(Bracket Style) にも簡単なサンプルあり。

スタイル ClangFormat スタイルオプション コーディング規約
LLVM BasedOnStyle: LLVM LLVM coding standards
google BasedOnStyle: Google Google’s C++ style guide
Chromium BasedOnStyle: Chromium Chromium’s style guide
Mozilla BasedOnStyle: Mozilla Mozilla’s style guide
WebKit BasedOnStyle: WebKit WebKit’s style guide
linux
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
Linux kernel coding style
Visual Studio
UseTab: Never
IndentWidth: 4
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 0
 

スタイルオプション(設定項目)の概要

概要:スタイルオプションの概要と ベーススタイル(LLVM Google Chromium Mozilla WebKit) の設定項目の初期値の一覧表
詳細:詳細は Clang ドキュメントClangClang-Format Style Options を参照

注釈

ベーススタイル名をクリックすると、その列を非表示にします。(JavaScript)

Clang 3.9
# BasedOnStyle LLVM Google Chromium Mozilla WebKit 概要
Language (LanguageKind) Cpp

対象としているフォーマットスタイルの言語

注釈

設定ファイルでは、設定値のprefix LK_ は不要

設定値 概要
LK_None 使わない
LK_Cpp C, C++, ObjectiveC, ObjectiveC++ を使用する
LK_Java Java を使用する
LK_JavaScript JavaScript を使用する
LK_Proto Protocol Buffers (https://developers.google.com/protocol-buffers/).
AccessModifierOffset (int) -2 -1 -1 -2 -4
アクセス修飾子(public: protected: private:)のインデント
(IndentWidth オプションと同じ値のマイナスでインデントなし)
AlignAfterOpenBracket (BracketAlignmentStyle) Align Align Align Align DontAlign

true の場合、開始括弧の後で水平方向に引数を整列させる。

注釈

設定ファイルでは、設定値のprefix BAS_ は不要
バージョン2.8で設定値の型が変更
設定値 概要
BAS_Align
someLongFunction(argument1,
                 argument2);
BAS_DontAlign
someLongFunction(argument1,
    argument2);
BAS_AlwaysBreak
someLongFunction(
    argument1, argument2);
AlignConsecutiveAssignments (bool) false

true の場合、連続する行の代入演算子を揃える

int aaaa = 12;
int b    = 23;
int ccc  = 23;

注釈

バージョン 3.7 より追加

AlignConsecutiveDeclarations (bool) false

true の場合、連続する行の宣言を揃える

int         aaaa = 12;
float       b = 23;
std::string ccc = 23;

注釈

バージョン 3.8 より追加

AlignEscapedNewlinesLeft (bool) false true true false false

エスケープされた改行の位置

設定値 概要
true

可能な限り、左端に整列する

#define TEST       \
  "long long test" \
  "string"         \
  "!!"
false

ColumnLimit に配置

#define TEST                           \
  "long long test"                     \
  "string"                             \
  "!!"
AlignOperands (bool) true true true true false

true の場合、水平方向に二項演算子と三項演算子を揃えます。

int aaa = bbbbbbbbbbbbbbb +
          ccccccccccccccc;
AlignTrailingComments (bool) true true true true false コメントを末尾に揃える
AllowAllParametersOfDeclarationOnNextLine (bool) true true false false true BinPackParametersfalse の場合でも、 次の行に関数宣言のすべてのパラメータを置く。
AllowShortBlocksOnASingleLine (bool) false
単一の行に簡単なブレース文を収縮。
例えば、 if (a) { return; } を単一行に置く。
AllowShortCaseLabelsOnASingleLine (bool) false true の場合、短いケースラベルを単一行に置く。
AllowShortFunctionsOnASingleLine (ShortFunctionStyle) All All Inline All All

int f() { return 0; } を単一行に置くか。

注釈

設定ファイルでは、設定値のprefix SFS_ は不要

設定値 概要
SFS_None 単一行にしない
SFS_Inline class内で定義された関数のみ単一行にする
SFS_Empty 空の関数のみ単一行にする
SFS_All 全て単一行にする
AllowShortIfStatementsOnASingleLine (bool) false true false false false true の場合、単一行の if (a) return; をキープ
AllowShortLoopsOnASingleLine (bool) false true false false false true の場合、単一行の while (true) continue; をキープ
AlwaysBreakAfterDefinitionReturnType (DefinitionReturnTypeBreakingStyle) None None None TopLevel None

関数定義の戻り値の型のスタイル。 より正確には「関数定義の型以下の識別子の前に改行」。

警告

このオプションは廃止されており、後方互換性のために残されています。
AlwaysBreakAfterReturnType を参照
設定値 概要
DRTBS_None 自動的に戻り値の型の後で改行。 ペナルティは PenaltyReturnTypeOnItsOwnLine が考慮される。
DRTBS_All 全ての戻り値の型の後で改行
DRTBS_TopLevel トップレベルの関数の戻り値の型の後で改行

注釈

バージョン 3.7 より追加

AlwaysBreakAfterReturnType (ReturnTypeBreakingStyle) None None None TopLevelDefinitions None

関数宣言の戻り値の型のスタイル。 より正確には「関数定義の型以下の識別子の前に改行」。

設定値 概要
RTBS_None 自動的に戻り値の型の後で改行。 ペナルティは PenaltyReturnTypeOnItsOwnLine が考慮される。
RTBS_All 全ての戻り値の型の後で改行
RTBS_TopLevel トップレベルの関数の戻り値の型の後で改行
RTBS_AllDefinitions 全ての関数定義の戻り値の型の後で改行
RTBS_TopLevelDefinitions トップレベルの関数定義の戻り値の型の後で改行

注釈

バージョン 3.8 より追加

AlwaysBreakBeforeMultilineStrings (bool) false true true false false 複数行のリテラル文字列の前で改行する
AlwaysBreakTemplateDeclarations (bool) false true true false false true の場合、テンプレート宣言の template<...> の後に改行
BinPackArguments (bool) true false の場合、関数呼び出しの引数は、全て同じ行か、1行ごとになる。
BinPackParameters (bool) true true false true true 関数呼び出しや関数定義のパラメーターを、一行ごとにするか
BraceWrapping:  

波括弧{}のフォーマットスタイル(個々の設定はネストした設定フラグを参照)

BreakBeforeBraces が、 BS_Custom に設定されていない場合、 ネストした設定フラグは無視

注釈

バージョン 3.8 より追加

BraceWrapping:AfterClass(bool) false false false true false class の後
BraceWrapping:AfterControlStatement(bool) false コントロールステートメント(if/for/while/switch/..)の後
BraceWrapping:AfterEnum(bool) false false false true false enum の後
BraceWrapping:AfterFunction(bool) false false false true true 関数の後
BraceWrapping:AfterNamespace(bool) false namespace の後
BraceWrapping:AfterObjCDeclaration(bool) false ObjC 宣言(@autoreleasepool, interfaces, ..)の後
BraceWrapping:AfterStruct(bool) false false false true false struct の後
BraceWrapping:AfterUnion(bool) false false false true false union の後
BraceWrapping:BeforeCatch(bool) false catch の前
BraceWrapping:BeforeElse(bool) false else の前
BraceWrapping:IndentBraces(bool) false 関数呼び出しや関数定義のパラメーターを、一行ごとにするか
BreakBeforeBinaryOperators (BinaryOperatorStyle) None None None None All

二項演算子の折り返し方法

注釈

設定ファイルでは、設定値のprefix BOS_ は不要

設定値 概要
BOS_None 演算子の後に改行
BOS_NonAssignment 割り当てていない演算子の前で改行
BOS_All 演算子よりも先に改行
BreakBeforeBraces (BraceBreakingStyle) Attach Attach Attach Mozilla (3.6 までは Attach) WebKit (3.7 までは Stroustrup)
波括弧{}のフォーマットスタイル

注釈

設定ファイルでは、設定値のprefix BS_ は不要

設定値 概要
BS_Attach 常に周囲のコンテキストに波括弧{}を付ける
BS_Linux Linux風、但し、関数、namespace、 クラス定義の波括弧{}の前で改行
BS_Mozilla
(3.7 より追加)
列挙型、関数、およびレコード定義の波括弧{}の前で改行
BS_Stroustrup Stroustrup風、但し、関数定義の前で改行
BS_Allman 常に波括弧{}の前で改行
BS_GNU
(3.5 より追加)
常に波括弧{}の前で改行。クラス以外の、関数、 または他の定義の制御文の波括弧{}にインデントレベルを追加。
BS_WebKit
(3.8 より追加)
Attach 風、関数の前に改行
BS_Custom
(3.8 より追加)
BraceWrapping で設定
BreakBeforeTernaryOperators (bool) true 三項演算子を、改行の後に配置する
BreakConstructorInitializersBeforeComma (bool) false false false false true

コンストラクタ初期化子のコンマの前で改行し、コロンでカンマを揃えるか

設定値 概要
true
class Test {
public:
  Test()
      : n(0)
      , c('a') {}

private:
  int n;
  char c;
};
false
class Test {
public:
  Test() : n(0), c('a') {}

private:
  int n;
  char c;
};
BreakAfterJavaFieldAnnotations false

Javaファイル内のフィールドのアノテーションの後に改行。

注釈

バージョン 3.9 より追加

BreakStringLiterals true

フォーマット時に文字列リテラルを改行で調整。

注釈

バージョン 3.9 より追加

ColumnLimit (unsigned) 80 80 80 80 0 最大列(行幅)数
CommentPragmas (std::string) ‘^ IWYU pragma:’

行に分割または変更すべきでない、特別な意味を持つのコメントを記述する正規表現。

CommentPragmas: '\*' で、doxygenコメントを無視

ConstructorInitializerAllOnOneLineOrOnePerLine (bool) false true true true false コンストラクターの初期化子が行に収まらない場合は、改行する
ConstructorInitializerIndentWidth (unsigned) 4 コンストラクタ初期化子リストのインデントに使用する文字数
ContinuationIndentWidth (unsigned) 4 継続行のためのインデント幅
Cpp11BracedListStyle (bool) true true true false false  
DerivePointerAlignment (bool) false true false true false
true の場合、 *& の位置を自動判定。
PointerAlignment は、フォールバックとしてのみ使う
DisableFormat (bool) false 全ての書式設定を無効にする。
ExperimentalAutoDetectBinPacking (bool) false  
ForEachMacros (std::vector<std::string>) [ foreach, Q_FOREACH, BOOST_FOREACH ] For example: BOOST_FOREACH.
IncludeCategories (std::vector<IncludeCategory>) (概要参照)

#include のオーダーに使用されるカテゴリー

LLVM, Mozilla, WebKit

IncludeCategories:
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
  - Regex:           '^(<|"(gtest|isl|json)/)'
    Priority:        3
  - Regex:           '.*'
    Priority:        1

Google, Chromium

IncludeCategories:
  - Regex:           '^<.*\.h>'
    Priority:        1
  - Regex:           '^<.*'
    Priority:        2
  - Regex:           '.*'
    Priority:        3

注釈

バージョン 3.8 より追加

IncludeIsMainRegex (std::string) (概要参照)

file-to-main-include マッピングで許可されている サフィックスの正規表現を指定します。

LLVM, Mozilla, WebKit

IncludeIsMainRegex: '$'

Google, Chromium

IncludeIsMainRegex: '([-_](test|unittest))?$'

注釈

バージョン 3.9 より追加

IndentCaseLabels (bool) false true true true false switch ブロック内の case X: 文をインデント
IndentWidth (unsigned) 2 2 2 2 4 インデントに使用する列数
IndentWrappedFunctionNames (bool) false  
JavaScriptQuotes (JavaScriptQuoteStyle) Leave

JavaScriptの文字列に使用します。

注釈

設定ファイルでは、設定値のprefix JSQS_ は不要

設定値 概要
JSQS_Leave 現状維持
JSQS_Single シングルクォーテーションを使用する
JSQS_Double ダブルクォーテーションを使用する

注釈

バージョン 3.9 より追加

JavaScriptWrapImports (bool) true  
KeepEmptyLinesAtTheStartOfBlocks (bool) true false false true true true の場合、ブロックの開始時の空行を保持する
MacroBlockBegin (std::string) ‘’

ブロック開始マクロにマッチする正規表現

注釈

バージョン 3.7 より追加

MacroBlockEnd (std::string) ‘’

ブロック終了マクロにマッチする正規表現

注釈

バージョン 3.7 より追加

MaxEmptyLinesToKeep (unsigned) 1 最大連続空行数
NamespaceIndentation (NamespaceIndentationKind) None None None None Inner

namespace のインデント

注釈

設定ファイルでは、設定値のprefix NI_ は不要

設定値 概要
NI_None namespace でインデントしない
NI_Inner 他の namespace にネストされたもののみインデント
NI_All 全ての namespace でインデントする
ObjCBlockIndentWidth (unsigned) 2 2 2 2 4 Objective-Cのインデントの文字数
ObjCSpaceAfterProperty (bool) false false false true true
Objective-Cで @property の後にスペースを追加する。
@property(readonly) の代わりに @property (readonly) を使用する。
ObjCSpaceBeforeProtocolList (bool) true false false false true
Objective-Cのプロトコルリストの前にスペースを追加。
Foo<Protocol> の代わりに Foo <Protocol> を使用する。
PenaltyBreakBeforeFirstCallParameter (unsigned) 19 1 1 19 19 “call(” のペナルティー
PenaltyBreakComment (unsigned) 300 コメントのペナルティー
PenaltyBreakFirstLessLess (unsigned) 120 << のペナルティー
PenaltyBreakString (unsigned) 1000 リテラル文字列のペナルティー
PenaltyExcessCharacter (unsigned) 1000000 文字のペナルティー
PenaltyReturnTypeOnItsOwnLine (unsigned) 60 200 200 200 60 独自の行に関数の戻り値の型を置くためのペナルティー
PointerAlignment (PointerAlignmentStyle) Right Left Left Left Left

*& のスタイル(DerivePointerAlignment オプションも参照)

注釈

設定ファイルでは、設定値のprefix PAS_ は不要

設定値 概要
PAS_Left int* n;
PAS_Right int *n;
PAS_Middle int * n;
ReflowComments(bool) true

true の場合、コメントをリフローしようと試みる。

注釈

バージョン 3.8 より追加

SortIncludes(bool) true true false true true

true の場合、 #include をソートする。

注釈

バージョン 3.8 より追加

SpaceAfterCStyleCast (bool) false

Cスタイルのキャスト後のスペース

設定値 概要
true n = (int*) c;
false n = (int*)c;
SpaceBeforeAssignmentOperators (bool) true true の場合、代入演算子の前のスペースを削除しない
SpaceBeforeParens (SpaceBeforeParensOptions) ControlStatements

開始括弧’(‘ の前のスペース

注釈

設定ファイルでは、設定値のprefix SBPO_ は不要

設定値 概要
SBPO_Never 開始括弧’(‘ の前にスペースを入れない
SBPO_ControlStatements 制御文のキーワード(if, for, while ...) と開始括弧’(‘ の間にスペースを挿入する
SBPO_Always 開始括弧’(‘ の前にスペースを挿入する
SpaceInEmptyParentheses (bool) false

空括弧()内のスペース

設定値 概要
true func( );
false func();
SpacesBeforeTrailingComments (unsigned) 1 2 2 1 1
1行コメントの前のスペース幅
int n; ココの幅 //< コメント
SpacesInAngles (bool) false

山括弧<>の内側のスペース(Standard オプションも参照)

設定値 概要
true std::vector< std::vector< int > >
false std::vector<std::vector<int> >
SpacesInContainerLiterals (bool) true true の場合、コンテナリテラル(例. ObjC、 Javascript 配列、 dict リテラル) の内部にスペースを挿入する
SpacesInCStyleCastParentheses (bool) false

Cスタイルのキャストのスペース

設定値 概要
true n = ( int * )c;
false n = (int *)c;
SpacesInParentheses (bool) false

括弧()の内側のスペース

設定値 概要
true if ( a + b ) {}
false if (a + b) {}
SpacesInSquareBrackets (bool) false

括弧[]の内側のスペース

設定値 概要
true a[ 1 ]
false a[1]
Standard (LanguageStandard) Cpp11 Auto Auto Cpp03 Cpp03

規格との互換性

注釈

設定ファイルでは、設定値のprefix LS_ は不要

設定値 概要
LS_Cpp03 C++03互換の構文を使う
LS_Cpp11
C++11の機能を使う
(例 A<A<int> > の代わりに A<A<int>> )
LS_Auto インプットから自動検出
TabWidth (unsigned) 8 タブ幅
UseTab (UseTabStyle) Never

タブの使い方

注釈

設定ファイルでは、設定値のprefix UT_ は不要

設定値 概要
UT_Never タブを使わない
UT_ForIndentation インデントのみにタブを使う
UT_Always 可能な限りタブを使う

Old Clang
# BasedOnStyle LLVM Google Chromium Mozilla WebKit 概要
AlignAfterOpenBracket (bool) true true true true false

true の場合、開始括弧の後で水平方向に引数を整列させる。

丸括弧()、波括弧{}、角括弧[]に適用。

someLongFunction(argument1,
argument2);

警告

(バージョン3.7まで有効。それ以降のバージョンは、設定値が BracketAlignmentStyle へ変更)

AlwaysBreakAfterDefinitionReturnType (bool) false

true の場合、必ず関数定義の戻り値の型の後に改行。 より正確には「関数定義の型以下の識別子の前に改行」。 PenaltyReturnTypeOnItsOwnLine は無関係になる。

警告

(バージョン3.7まで有効。それ以降のバージョンは、設定値が DefinitionReturnTypeBreakingStyle へ変更)

DerivePointerBinding (bool) false true false true false

インプットファイルを解析して最も多い配置に従って、 *& の位置 を自動判定。 fallbackとしてのみ PointerBindsToType を使う

警告

(バージョン3.4まで有効。それ以降のバージョンは、この項目は削除され、 DerivePointerAlignment へ変更)

IndentFunctionDeclarationAfterType (bool) false

警告

バージョン3.4まで有効。

PointerBindsToType (bool) false true true true true

*& の位置(DerivePointerBinding オプションも参照)

設定値 概要
true int* n;
false int *n;

警告

(バージョン3.4まで有効。それ以降のバージョンは、この項目は削除され、 PointerAlignment へ変更)

SpaceAfterControlStatementKeyword (bool) true
true の場合、括弧のヘッダー(例. if, for, while ...)の後に
スペースを挿入する

警告

(バージョン3.4まで有効。それ以降のバージョンは、この項目は削除され、 SpaceBeforeParens: 'ControlStatements' へ変更)