Clang 22.0.0git documentation
Clang-Format Style Options
« ClangFormat :: Contents :: Clang Linker Wrapper »
Clang-Format Style Options ¶
Clang-Format Style Options describes configurable formatting style options supported by LibFormat and ClangFormat.
When using clang-format command line utility or clang::format::reformat(...) functions from code, one can either use one of the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft) or create a custom style by configuring specific style options.
Configuring Style with clang-format ¶
clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the .clang-format or _clang-format file in the project directory.
When using -style=file, clang-format for each input file will try to find the .clang-format file located in the closest parent directory of the input file. When the standard input is used, the search is started from the current directory.
When using -style=file:<format_file_path>, clang-format for each input file will use the format file located at . The path may be absolute or relative to the working directory.
The .clang-format file uses YAML format:
key1: value1
key2: value2
# A comment.
The configuration file can consist of several sections each having different Language: parameter denoting the programming language this section of the configuration is targeted at. See the description of the Language option below for the list of supported languages. The first section may have no language set, it will set the default style options for all languages. Configuration sections for specific language will override options set in the default section.
When clang-format formats a file, it auto-detects the language using the file name. When formatting standard input or a file that doesn’t have the extension corresponding to its language, -assume-filename= option can be used to override the file name clang-format uses to detect the language.
An example of a configuration file for multiple languages:
---
# We'll use defaults from the LLVM style, but with 4 columns indentation.
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Left
---
Language: JavaScript
# Use 100 columns for JS.
ColumnLimit: 100
---
Language: Proto
# Don't format .proto files.
DisableFormat: true
---
Language: CSharp
# Use 100 columns for C#.
ColumnLimit: 100
An easy way to get a valid .clang-format file containing all configuration options of a certain predefined style is:
clang-format -style=llvm -dump-config > .clang-format
When specifying configuration in the -style= option, the same configuration is applied for all input files. The format of the configuration is:
-style='{key1: value1, key2: value2, ...}'
Disabling Formatting on a Piece of Code ¶
Clang-format understands also special comments that switch formatting in a delimited range. The code between a comment // clang-format off or /* clang-format off */ up to a comment // clang-format on or /* clang-format on */ will not be formatted. The comments themselves will be formatted (aligned) normally. Also, a colon ( :) and additional text may follow // clang-format off or // clang-format on to explain why clang-format is turned off or back on.
int formatted_code;
// clang-format off
void unformatted_code ;
// clang-format on
void formatted_code_again;
In addition, the OneLineFormatOffRegex option gives you a concise way to disable formatting for all of the lines that match the regular expression.
Configuring Style in Code ¶
When using clang::format::reformat(...) functions, the format is specified by supplying the clang::format::FormatStyle structure.
Configurable Format Style Options ¶
This section lists the supported style options. Value type is specified for each option. For enumeration types possible values are specified both as a C++ enumeration member (with a prefix, e.g. LS_Auto), and as a value usable in the configuration (without a prefix: Auto).
BasedOnStyle ( String) ¶
The style used for all options not specifically set in the configuration.
This option is supported only in the clang-format configuration (both within -style='{...}' and the .clang-format file).
Possible values:
LLVMA style complying with the LLVM coding standardsGoogleA style complying with Google’s C++ style guideChromiumA style complying with Chromium’s style guideMozillaA style complying with Mozilla’s style guideWebKitA style complying with WebKit’s style guideMicrosoftA style complying with Microsoft’s style guideGNUA style complying with the GNU coding standardsInheritParentConfigNot a real style, but allows to use the.clang-formatfile from the parent directory (or its parent if there is none). If there is no parent file found it falls back to thefallbackstyle, and applies the changes to that.
With this option you can overwrite some parts of your main style for your subdirectories. This is also possible through the command line, e.g.: --style={BasedOnStyle: InheritParentConfig, ColumnLimit: 20}
AccessModifierOffset ( Integer) clang-format 3.3 ¶
The extra indent or outdent of access modifiers, e.g. public:.
AlignAfterOpenBracket ( BracketAlignmentStyle) clang-format 3.8 ¶
If true, horizontally aligns arguments after an open bracket.
This applies to round brackets (parentheses), angle brackets and square brackets.
Possible values:
BAS_Align(in configuration:Align) Align parameters on the open bracket, e.g.:
someLongFunction(argument1,
argument2);
BAS_DontAlign(in configuration:DontAlign) Don’t align, instead useContinuationIndentWidth, e.g.:
someLongFunction(argument1,
argument2);
BAS_AlwaysBreak(in configuration:AlwaysBreak) Always break after an open bracket, if the parameters don’t fit on a single line, e.g.:
someLongFunction(
argument1, argument2);
BAS_BlockIndent(in configuration:BlockIndent) Always break after an open bracket, if the parameters don’t fit on a single line. Closing brackets will be placed on a new line. E.g.:
someLongFunction(
argument1, argument2
)
Note
This currently only applies to braced initializer lists (when Cpp11BracedListStyle is not Block) and parentheses.
AlignArrayOfStructures ( ArrayInitializerAlignmentStyle) clang-format 13 ¶
If not None, when using initialization for an array of structs aligns the fields into columns.
Note
As of clang-format 15 this option only applied to arrays with equal number of columns per row.
Possible values:
AIAS_Left(in configuration:Left) Align array column and left justify the columns e.g.:
struct test demo[] =
{
{56, 23, "hello"},
{-1, 93463, "world"},
{7, 5, "!!" }
};
AIAS_Right(in configuration:Right) Align array column and right justify the columns e.g.:
struct test demo[] =
{
{56, 23, "hello"},
{-1, 93463, "world"},
{ 7, 5, "!!"}
};
AIAS_None(in configuration:None) Don’t align array initializer columns.
AlignConsecutiveAssignments ( AlignConsecutiveStyle) clang-format 3.8 ¶
Style of aligning consecutive assignments.
Consecutive will result in formattings like:
int a = 1;
int somelongname = 2;
double c = 3;
Nested configuration flags:
Alignment options.
They can also be read as a whole for compatibility. The choices are:
NoneConsecutiveAcrossEmptyLinesAcrossCommentsAcrossEmptyLinesAndComments
For example, to align across empty lines and not across comments, either of these work.
AlignConsecutiveAssignments: AcrossEmptyLines
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false
bool EnabledWhether aligning is enabled.
#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)
int a = 1;
int somelongname = 2;
double c = 3;
int aaaa : 1;
int b : 12;
int ccc : 8;
int aaaa = 12;
float b = 23;
std::string ccc;
bool AcrossEmptyLinesWhether to align across empty lines.
true:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
false:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
bool AcrossCommentsWhether to align across comments.
true:
int d = 3;
/* A comment. */
double e = 4;
false:
int d = 3;
/* A comment. */
double e = 4;
bool AlignCompoundOnly forAlignConsecutiveAssignments. Whether compound assignments like+=are aligned along with=.
true:
a &= 2;
bbb = 2;
false:
a &= 2;
bbb = 2;
bool AlignFunctionDeclarationsOnly forAlignConsecutiveDeclarations. Whether function declarations are aligned.
true:
unsigned int f1(void);
void f2(void);
size_t f3(void);
false:
unsigned int f1(void);
void f2(void);
size_t f3(void);
bool AlignFunctionPointersOnly forAlignConsecutiveDeclarations. Whether function pointers are aligned.
true:
unsigned i;
int &r;
int *p;
int (*f)();
false:
unsigned i;
int &r;
int *p;
int (*f)();
bool PadOperatorsOnly forAlignConsecutiveAssignments. Whether short assignment operators are left-padded to the same length as long ones in order to put all assignment operators to the right of the left hand side.
true:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
false:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
AlignConsecutiveBitFields ( AlignConsecutiveStyle) clang-format 11 ¶
Style of aligning consecutive bit fields.
Consecutive will align the bitfield separators of consecutive lines. This will result in formattings like:
int aaaa : 1;
int b : 12;
int ccc : 8;
Nested configuration flags:
Alignment options.
They can also be read as a whole for compatibility. The choices are:
NoneConsecutiveAcrossEmptyLinesAcrossCommentsAcrossEmptyLinesAndComments
For example, to align across empty lines and not across comments, either of these work.
AlignConsecutiveBitFields: AcrossEmptyLines
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false
bool EnabledWhether aligning is enabled.
#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)
int a = 1;
int somelongname = 2;
double c = 3;
int aaaa : 1;
int b : 12;
int ccc : 8;
int aaaa = 12;
float b = 23;
std::string ccc;
bool AcrossEmptyLinesWhether to align across empty lines.
true:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
false:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
bool AcrossCommentsWhether to align across comments.
true:
int d = 3;
/* A comment. */
double e = 4;
false:
int d = 3;
/* A comment. */
double e = 4;
bool AlignCompoundOnly forAlignConsecutiveAssignments. Whether compound assignments like+=are aligned along with=.
true:
a &= 2;
bbb = 2;
false:
a &= 2;
bbb = 2;
bool AlignFunctionDeclarationsOnly forAlignConsecutiveDeclarations. Whether function declarations are aligned.
true:
unsigned int f1(void);
void f2(void);
size_t f3(void);
false:
unsigned int f1(void);
void f2(void);
size_t f3(void);
bool AlignFunctionPointersOnly forAlignConsecutiveDeclarations. Whether function pointers are aligned.
true:
unsigned i;
int &r;
int *p;
int (*f)();
false:
unsigned i;
int &r;
int *p;
int (*f)();
bool PadOperatorsOnly forAlignConsecutiveAssignments. Whether short assignment operators are left-padded to the same length as long ones in order to put all assignment operators to the right of the left hand side.
true:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
false:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
AlignConsecutiveDeclarations ( AlignConsecutiveStyle) clang-format 3.8 ¶
Style of aligning consecutive declarations.
Consecutive will align the declaration names of consecutive lines. This will result in formattings like:
int aaaa = 12;
float b = 23;
std::string ccc;
Nested configuration flags:
Alignment options.
They can also be read as a whole for compatibility. The choices are:
NoneConsecutiveAcrossEmptyLinesAcrossCommentsAcrossEmptyLinesAndComments
For example, to align across empty lines and not across comments, either of these work.
AlignConsecutiveDeclarations: AcrossEmptyLines
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false
bool EnabledWhether aligning is enabled.
#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)
int a = 1;
int somelongname = 2;
double c = 3;
int aaaa : 1;
int b : 12;
int ccc : 8;
int aaaa = 12;
float b = 23;
std::string ccc;
bool AcrossEmptyLinesWhether to align across empty lines.
true:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
false:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
bool AcrossCommentsWhether to align across comments.
true:
int d = 3;
/* A comment. */
double e = 4;
false:
int d = 3;
/* A comment. */
double e = 4;
bool AlignCompoundOnly forAlignConsecutiveAssignments. Whether compound assignments like+=are aligned along with=.
true:
a &= 2;
bbb = 2;
false:
a &= 2;
bbb = 2;
bool AlignFunctionDeclarationsOnly forAlignConsecutiveDeclarations. Whether function declarations are aligned.
true:
unsigned int f1(void);
void f2(void);
size_t f3(void);
false:
unsigned int f1(void);
void f2(void);
size_t f3(void);
bool AlignFunctionPointersOnly forAlignConsecutiveDeclarations. Whether function pointers are aligned.
true:
unsigned i;
int &r;
int *p;
int (*f)();
false:
unsigned i;
int &r;
int *p;
int (*f)();
bool PadOperatorsOnly forAlignConsecutiveAssignments. Whether short assignment operators are left-padded to the same length as long ones in order to put all assignment operators to the right of the left hand side.
true:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
false:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
AlignConsecutiveMacros ( AlignConsecutiveStyle) clang-format 9 ¶
Style of aligning consecutive macro definitions.
Consecutive will result in formattings like:
#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)
Nested configuration flags:
Alignment options.
They can also be read as a whole for compatibility. The choices are:
NoneConsecutiveAcrossEmptyLinesAcrossCommentsAcrossEmptyLinesAndComments
For example, to align across empty lines and not across comments, either of these work.
AlignConsecutiveMacros: AcrossEmptyLines
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false
bool EnabledWhether aligning is enabled.
#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)
int a = 1;
int somelongname = 2;
double c = 3;
int aaaa : 1;
int b : 12;
int ccc : 8;
int aaaa = 12;
float b = 23;
std::string ccc;
bool AcrossEmptyLinesWhether to align across empty lines.
true:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
false:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
bool AcrossCommentsWhether to align across comments.
true:
int d = 3;
/* A comment. */
double e = 4;
false:
int d = 3;
/* A comment. */
double e = 4;
bool AlignCompoundOnly forAlignConsecutiveAssignments. Whether compound assignments like+=are aligned along with=.
true:
a &= 2;
bbb = 2;
false:
a &= 2;
bbb = 2;
bool AlignFunctionDeclarationsOnly forAlignConsecutiveDeclarations. Whether function declarations are aligned.
true:
unsigned int f1(void);
void f2(void);
size_t f3(void);
false:
unsigned int f1(void);
void f2(void);
size_t f3(void);
bool AlignFunctionPointersOnly forAlignConsecutiveDeclarations. Whether function pointers are aligned.
true:
unsigned i;
int &r;
int *p;
int (*f)();
false:
unsigned i;
int &r;
int *p;
int (*f)();
bool PadOperatorsOnly forAlignConsecutiveAssignments. Whether short assignment operators are left-padded to the same length as long ones in order to put all assignment operators to the right of the left hand side.
true:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
false:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
AlignConsecutiveShortCaseStatements ( ShortCaseStatementsAlignmentStyle) clang-format 17 ¶
Style of aligning consecutive short case labels. Only applies if AllowShortCaseExpressionOnASingleLine or AllowShortCaseLabelsOnASingleLine is true.
# Example of usage:
AlignConsecutiveShortCaseStatements:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCaseColons: false
Nested configuration flags:
Alignment options.
bool EnabledWhether aligning is enabled.
true:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
default: return "";
}
false:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
default: return "";
}
bool AcrossEmptyLinesWhether to align across empty lines.
true:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
default: return "";
}
false:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
default: return "";
}
bool AcrossCommentsWhether to align across comments.
true:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
/* A comment. */
default: return "";
}
false:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
/* A comment. */
default: return "";
}
bool AlignCaseArrowsWhether to align the case arrows when aligning short case expressions.
true:
i = switch (day) {
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
default -> 0;
};
false:
i = switch (day) {
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
default -> 0;
};
bool AlignCaseColonsWhether aligned case labels are aligned on the colon, or on the tokens after the colon.
true:
switch (level) {
case log::info : return "info:";
case log::warning: return "warning:";
default : return "";
}
false:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
default: return "";
}
AlignConsecutiveTableGenBreakingDAGArgColons ( AlignConsecutiveStyle) clang-format 19 ¶
Style of aligning consecutive TableGen DAGArg operator colons. If enabled, align the colon inside DAGArg which have line break inside. This works only when TableGenBreakInsideDAGArg is BreakElements or BreakAll and the DAGArg is not excepted by TableGenBreakingDAGArgOperators’s effect.
let dagarg = (ins
a :$src1,
aa :$src2,
aaa:$src3
)
Nested configuration flags:
Alignment options.
They can also be read as a whole for compatibility. The choices are:
NoneConsecutiveAcrossEmptyLinesAcrossCommentsAcrossEmptyLinesAndComments
For example, to align across empty lines and not across comments, either of these work.
AlignConsecutiveTableGenBreakingDAGArgColons: AcrossEmptyLines
AlignConsecutiveTableGenBreakingDAGArgColons:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false
bool EnabledWhether aligning is enabled.
#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)
int a = 1;
int somelongname = 2;
double c = 3;
int aaaa : 1;
int b : 12;
int ccc : 8;
int aaaa = 12;
float b = 23;
std::string ccc;
bool AcrossEmptyLinesWhether to align across empty lines.
true:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
false:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
bool AcrossCommentsWhether to align across comments.
true:
int d = 3;
/* A comment. */
double e = 4;
false:
int d = 3;
/* A comment. */
double e = 4;
bool AlignCompoundOnly forAlignConsecutiveAssignments. Whether compound assignments like+=are aligned along with=.
true:
a &= 2;
bbb = 2;
false:
a &= 2;
bbb = 2;
bool AlignFunctionDeclarationsOnly forAlignConsecutiveDeclarations. Whether function declarations are aligned.
true:
unsigned int f1(void);
void f2(void);
size_t f3(void);
false:
unsigned int f1(void);
void f2(void);
size_t f3(void);
bool AlignFunctionPointersOnly forAlignConsecutiveDeclarations. Whether function pointers are aligned.
true:
unsigned i;
int &r;
int *p;
int (*f)();
false:
unsigned i;
int &r;
int *p;
int (*f)();
bool PadOperatorsOnly forAlignConsecutiveAssignments. Whether short assignment operators are left-padded to the same length as long ones in order to put all assignment operators to the right of the left hand side.
true:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
false:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
AlignConsecutiveTableGenCondOperatorColons ( AlignConsecutiveStyle) clang-format 19 ¶
Style of aligning consecutive TableGen cond operator colons. Align the colons of cases inside !cond operators.
!cond(!eq(size, 1) : 1,
!eq(size, 16): 1,
true : 0)
Nested configuration flags:
Alignment options.
They can also be read as a whole for compatibility. The choices are:
NoneConsecutiveAcrossEmptyLinesAcrossCommentsAcrossEmptyLinesAndComments
For example, to align across empty lines and not across comments, either of these work.
AlignConsecutiveTableGenCondOperatorColons: AcrossEmptyLines
AlignConsecutiveTableGenCondOperatorColons:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false
bool EnabledWhether aligning is enabled.
#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)
int a = 1;
int somelongname = 2;
double c = 3;
int aaaa : 1;
int b : 12;
int ccc : 8;
int aaaa = 12;
float b = 23;
std::string ccc;
bool AcrossEmptyLinesWhether to align across empty lines.
true:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
false:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
bool AcrossCommentsWhether to align across comments.
true:
int d = 3;
/* A comment. */
double e = 4;
false:
int d = 3;
/* A comment. */
double e = 4;
bool AlignCompoundOnly forAlignConsecutiveAssignments. Whether compound assignments like+=are aligned along with=.
true:
a &= 2;
bbb = 2;
false:
a &= 2;
bbb = 2;
bool AlignFunctionDeclarationsOnly forAlignConsecutiveDeclarations. Whether function declarations are aligned.
true:
unsigned int f1(void);
void f2(void);
size_t f3(void);
false:
unsigned int f1(void);
void f2(void);
size_t f3(void);
bool AlignFunctionPointersOnly forAlignConsecutiveDeclarations. Whether function pointers are aligned.
true:
unsigned i;
int &r;
int *p;
int (*f)();
false:
unsigned i;
int &r;
int *p;
int (*f)();
bool PadOperatorsOnly forAlignConsecutiveAssignments. Whether short assignment operators are left-padded to the same length as long ones in order to put all assignment operators to the right of the left hand side.
true:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
false:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
AlignConsecutiveTableGenDefinitionColons ( AlignConsecutiveStyle) clang-format 19 ¶
Style of aligning consecutive TableGen definition colons. This aligns the inheritance colons of consecutive definitions.
def Def : Parent {}
def DefDef : Parent {}
def DefDefDef : Parent {}
Nested configuration flags:
Alignment options.
They can also be read as a whole for compatibility. The choices are:
NoneConsecutiveAcrossEmptyLinesAcrossCommentsAcrossEmptyLinesAndComments
For example, to align across empty lines and not across comments, either of these work.
AlignConsecutiveTableGenDefinitionColons: AcrossEmptyLines
AlignConsecutiveTableGenDefinitionColons:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false
bool EnabledWhether aligning is enabled.
#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)
int a = 1;
int somelongname = 2;
double c = 3;
int aaaa : 1;
int b : 12;
int ccc : 8;
int aaaa = 12;
float b = 23;
std::string ccc;
bool AcrossEmptyLinesWhether to align across empty lines.
true:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
false:
int a = 1;
int somelongname = 2;
double c = 3;
int d = 3;
bool AcrossCommentsWhether to align across comments.
true:
int d = 3;
/* A comment. */
double e = 4;
false:
int d = 3;
/* A comment. */
double e = 4;
bool AlignCompoundOnly forAlignConsecutiveAssignments. Whether compound assignments like+=are aligned along with=.
true:
a &= 2;
bbb = 2;
false:
a &= 2;
bbb = 2;
bool AlignFunctionDeclarationsOnly forAlignConsecutiveDeclarations. Whether function declarations are aligned.
true:
unsigned int f1(void);
void f2(void);
size_t f3(void);
false:
unsigned int f1(void);
void f2(void);
size_t f3(void);
bool AlignFunctionPointersOnly forAlignConsecutiveDeclarations. Whether function pointers are aligned.
true:
unsigned i;
int &r;
int *p;
int (*f)();
false:
unsigned i;
int &r;
int *p;
int (*f)();
bool PadOperatorsOnly forAlignConsecutiveAssignments. Whether short assignment operators are left-padded to the same length as long ones in order to put all assignment operators to the right of the left hand side.
true:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
false:
a >>= 2;
bbb = 2;
a = 2;
bbb >>= 2;
AlignEscapedNewlines ( EscapedNewlineAlignmentStyle) clang-format 5 ¶
Options for aligning backslashes in escaped newlines.
Possible values:
ENAS_DontAlign(in configuration:DontAlign) Don’t align escaped newlines.
#define A
int aaaa;
int b;
int dddddddddd;
ENAS_Left(in configuration:Left) Align escaped newlines as far left as possible.
#define A
int aaaa;
int b;
int dddddddddd;
ENAS_LeftWithLastLine(in configuration:LeftWithLastLine) Align escaped newlines as far left as possible, using the last line of the preprocessor directive as the reference if it’s the longest.
#define A
int aaaa;
int b;
int dddddddddd;
ENAS_Right(in configuration:Right) Align escaped newlines in the right-most column.
#define A
int aaaa;
int b;
int dddddddddd;
AlignOperands ( OperandAlignmentStyle) clang-format 3.5 ¶
If true, horizontally align operands of binary and ternary expressions.
Possible values:
OAS_DontAlign(in configuration:DontAlign) Do not align operands of binary and ternary expressions. The wrapped lines are indentedContinuationIndentWidthspaces from the start of the line.OAS_Align(in configuration:Align) Horizontally align operands of binary and ternary expressions.
Specifically, this aligns operands of a single expression that needs to be split over multiple lines, e.g.:
int aaa = bbbbbbbbbbbbbbb +
ccccccccccccccc;
When BreakBeforeBinaryOperators is set, the wrapped operator is aligned with the operand on the first line.
int aaa = bbbbbbbbbbbbbbb
+ ccccccccccccccc;
OAS_AlignAfterOperator(in configuration:AlignAfterOperator) Horizontally align operands of binary and ternary expressions.
This is similar to OAS_Align, except when BreakBeforeBinaryOperators is set, the operator is un-indented so that the wrapped operand is aligned with the operand on the first line.
int aaa = bbbbbbbbbbbbbbb
+ ccccccccccccccc;
AlignTrailingComments ( TrailingCommentsAlignmentStyle) clang-format 3.7 ¶
Control of trailing comments.
The alignment stops at closing braces after a line break, and only followed by other closing braces, a ( do-) while, a lambda call, or a semicolon.
Note
As of clang-format 16 this option is not a bool but can be set to the options. Conventional bool options still can be parsed as before.
# Example of usage:
AlignTrailingComments:
Kind: Always
OverEmptyLines: 2
Nested configuration flags:
Alignment options
TrailingCommentsAlignmentKinds KindSpecifies the way to align trailing comments.
Possible values:
TCAS_Leave(in configuration:Leave) Leave trailing comments as they are.
int a; // comment
int ab; // comment
int abc; // comment
int abcd; // comment
```
- `TCAS_Always` (in configuration: `Always`)
Align trailing comments.
```cpp
int a; // comment
int ab; // comment
int abc; // comment
int abcd; // comment
```
- `TCAS_Never` (in configuration: `Never`)
Don’t align trailing comments but other formatter applies.
```cpp
int a; // comment
int ab; // comment
int abc; // comment
int abcd; // comment
```
- `unsigned OverEmptyLines` How many empty lines to apply alignment.
When both `MaxEmptyLinesToKeep` and `OverEmptyLines` are set to 2,
it formats like below.
```cpp
int a; // all these
int ab; // comments are
int abcdef; // aligned
When MaxEmptyLinesToKeep is set to 2 and OverEmptyLines is set to 1, it formats like below.
int a; // these are
int ab; // aligned
int abcdef; // but this isn't
AllowAllArgumentsOnNextLine ( Boolean) clang-format 9 ¶
If a function call or braced initializer list doesn’t fit on a line, allow putting all arguments onto the next line, even if BinPackArguments is false.
true:
callFunction(
a, b, c, d);
false:
callFunction(a,
b,
c,
d);
AllowAllConstructorInitializersOnNextLine ( Boolean) clang-format 9 ¶
This option is deprecated. See NextLine of PackConstructorInitializers.
AllowAllParametersOfDeclarationOnNextLine ( Boolean) clang-format 3.3 ¶
If the function declaration doesn’t fit on a line, allow putting all parameters of a function declaration onto the next line even if BinPackParameters is OnePerLine.
true:
void myFunction(
int a, int b, int c, int d, int e);
false:
void myFunction(int a,
int b,
int c,
int d,
int e);
AllowBreakBeforeNoexceptSpecifier ( BreakBeforeNoexceptSpecifierStyle) clang-format 18 ¶
Controls if there could be a line break before a noexcept specifier.
Possible values:
BBNSS_Never(in configuration:Never) No line break allowed.
void foo(int arg1,
double arg2) noexcept;
void bar(int arg1, double arg2) noexcept(
noexcept(baz(arg1)) &&
noexcept(baz(arg2)));
BBNSS_OnlyWithParen(in configuration:OnlyWithParen) For a simplenoexceptthere is no line break allowed, but when we have a condition it is.
void foo(int arg1,
double arg2) noexcept;
void bar(int arg1, double arg2)
noexcept(noexcept(baz(arg1)) &&
noexcept(baz(arg2)));
BBNSS_Always(in configuration:Always) Line breaks are allowed. But note that because of the associated penaltiesclang-formatoften prefers not to break before thenoexcept.
void foo(int arg1,
double arg2) noexcept;
void bar(int arg1, double arg2)
noexcept(noexcept(baz(arg1)) &&
noexcept(baz(arg2)));
AllowBreakBeforeQtProperty ( Boolean) clang-format 22 ¶
Allow breaking before Q_Property keywords READ, WRITE, etc. as if they were preceded by a comma ( ,). This allows them to be formatted according to BinPackParameters.
AllowShortBlocksOnASingleLine ( ShortBlockStyle) clang-format 3.5 ¶
Dependent on the value, while (true) { continue; } can be put on a single line.
Possible values:
SBS_Never(in configuration:Never) Never merge blocks into a single line.
while (true) {
}
while (true) {
continue;
}
SBS_Empty(in configuration:Empty) Only merge empty blocks.
while (true) {}
while (true) {
continue;
}
SBS_Always(in configuration:Always) Always merge short blocks into a single line.
while (true) {}
while (true) { continue; }
AllowShortCaseExpressionOnASingleLine ( Boolean) clang-format 19 ¶
Whether to merge a short switch labeled rule into a single line.
true: false:
switch (a) { vs. switch (a) {
case 1 -> 1; case 1 ->
default -> 0; 1;
}; default ->
0;
};
AllowShortCaseLabelsOnASingleLine ( Boolean) clang-format 3.6 ¶
If true, short case labels will be contracted to a single line.
true: false:
switch (a) { vs. switch (a) {
case 1: x = 1; break; case 1:
case 2: return; x = 1;
} break;
case 2:
return;
}
AllowShortCompoundRequirementOnASingleLine ( Boolean) clang-format 18 ¶
Allow short compound requirement on a single line.
true:
template <typename T>
concept c = requires(T x) {
{ x + 1 } -> std::same_as<int>;
};
false:
template <typename T>
concept c = requires(T x)
…(truncated)