Allow putting all parameters of a function declaration onto
the next line even if ``BinPackParameters`` is ``false``.
+**AllowShortFunctionsOnASingleLine** (``bool``)
+ If ``true``, ``int f() { return 0; }`` can be put on a single
+ line.
+
**AllowShortIfStatementsOnASingleLine** (``bool``)
If ``true``, ``if (a) return;`` can be put on a single
line.
Always break before braces.
+**BreakBeforeTernaryOperators** (``bool``)
+ If ``true``, ternary operators will be placed after line breaks.
+
**BreakConstructorInitializersBeforeComma** (``bool``)
Always break constructor initializers before commas and align
the commas with the colon.
A column limit of ``0`` means that there is no column limit. In this case,
clang-format will respect the input's line breaking decisions within
- statements.
+ statements unless they contradict other rules.
**ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``)
If the constructor initializers don't fit on a line, put each
The number of characters to use for indentation of constructor
initializer lists.
+**ContinuationIndentWidth** (``unsigned``)
+ Indent width for line continuations.
+
**Cpp11BracedListStyle** (``bool``)
If ``true``, format braced lists as best suited for C++11 braced
lists.
**IndentWidth** (``unsigned``)
The number of columns to use for indentation.
+**Language** (``LanguageKind``)
+ Language, this format style is targeted at.
+
+ Possible values:
+
+ * ``LK_None`` (in configuration: ``None``)
+ Do not use.
+ * ``LK_Cpp`` (in configuration: ``Cpp``)
+ Should be used for C, C++, ObjectiveC, ObjectiveC++.
+ * ``LK_JavaScript`` (in configuration: ``JavaScript``)
+ Should be used for JavaScript.
+
+
**MaxEmptyLinesToKeep** (``unsigned``)
The maximum number of consecutive empty lines to keep.
Add a space in front of an Objective-C protocol list, i.e. use
``Foo <Protocol>`` instead of ``Foo<Protocol>``.
+**PenaltyBreakBeforeFirstCallParameter** (``unsigned``)
+ The penalty for breaking a function call after "call(".
+
**PenaltyBreakComment** (``unsigned``)
The penalty for each line break introduced inside a comment.
**PointerBindsToType** (``bool``)
Set whether & and * bind to the type as opposed to the variable.
-**SpaceAfterControlStatementKeyword** (``bool``)
- If ``true``, spaces will be inserted between 'for'/'if'/'while'/...
- and '('.
-
**SpaceBeforeAssignmentOperators** (``bool``)
If ``false``, spaces will be removed before assignment operators.
+**SpaceBeforeParens** (``SpaceBeforeParensOptions``)
+ Defines in which cases to put a space before opening parentheses.
+
+ Possible values:
+
+ * ``SBPO_Never`` (in configuration: ``Never``)
+ Never put a space before opening parentheses.
+ * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``)
+ Put a space before opening parentheses only after control statement
+ keywords (``for/if/while...``).
+ * ``SBPO_Always`` (in configuration: ``Always``)
+ Always put a space before opening parentheses, except when it's
+ prohibited by the syntax rules (in function-like macro definitions) or
+ when determined by other style rules (after unary operators, opening
+ parentheses, etc.)
+
+
**SpaceInEmptyParentheses** (``bool``)
If ``false``, spaces may be inserted into '()'.
**SpacesBeforeTrailingComments** (``unsigned``)
The number of spaces to before trailing line comments.
+**SpacesInAngles** (``bool``)
+ If ``true``, spaces will be inserted after '<' and before '>' in
+ template argument lists
+
**SpacesInCStyleCastParentheses** (``bool``)
If ``false``, spaces may be inserted into C style casts.
**SpacesInParentheses** (``bool``)
- If ``true``, spaces will be inserted after every '(' and before
- every ')'.
+ If ``true``, spaces will be inserted after '(' and before ')'.
**Standard** (``LanguageStandard``)
Format compatible with this standard, e.g. use
/// \brief If \c false, spaces may be inserted into C style casts.
bool SpacesInCStyleCastParentheses;
- /// \brief If \c true, spaces will be inserted between 'for'/'if'/'while'/...
- /// and '('.
- bool SpaceAfterControlStatementKeyword;
+ /// \brief Different ways to put a space before opening parentheses.
+ enum SpaceBeforeParensOptions {
+ /// Never put a space before opening parentheses.
+ SBPO_Never,
+ /// Put a space before opening parentheses only after control statement
+ /// keywords (<tt>for/if/while...</tt>).
+ SBPO_ControlStatements,
+ /// Always put a space before opening parentheses, except when it's
+ /// prohibited by the syntax rules (in function-like macro definitions) or
+ /// when determined by other style rules (after unary operators, opening
+ /// parentheses, etc.)
+ SBPO_Always
+ };
+
+ /// \brief Defines in which cases to put a space before opening parentheses.
+ SpaceBeforeParensOptions SpaceBeforeParens;
/// \brief If \c false, spaces will be removed before assignment operators.
bool SpaceBeforeAssignmentOperators;
SpacesInAngles == R.SpacesInAngles &&
SpaceInEmptyParentheses == R.SpaceInEmptyParentheses &&
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
- SpaceAfterControlStatementKeyword ==
- R.SpaceAfterControlStatementKeyword &&
+ SpaceBeforeParens == R.SpaceBeforeParens &&
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
ContinuationIndentWidth == R.ContinuationIndentWidth;
}
}
};
+template <>
+struct ScalarEnumerationTraits<
+ clang::format::FormatStyle::SpaceBeforeParensOptions> {
+ static void
+ enumeration(IO &IO,
+ clang::format::FormatStyle::SpaceBeforeParensOptions &Value) {
+ IO.enumCase(Value, "Never", clang::format::FormatStyle::SBPO_Never);
+ IO.enumCase(Value, "ControlStatements",
+ clang::format::FormatStyle::SBPO_ControlStatements);
+ IO.enumCase(Value, "Always", clang::format::FormatStyle::SBPO_Always);
+
+ // For backward compatibility.
+ IO.enumCase(Value, "false", clang::format::FormatStyle::SBPO_Never);
+ IO.enumCase(Value, "true",
+ clang::format::FormatStyle::SBPO_ControlStatements);
+ }
+};
+
template <> struct MappingTraits<clang::format::FormatStyle> {
static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
if (IO.outputting()) {
IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
IO.mapOptional("SpacesInCStyleCastParentheses",
Style.SpacesInCStyleCastParentheses);
- IO.mapOptional("SpaceAfterControlStatementKeyword",
- Style.SpaceAfterControlStatementKeyword);
IO.mapOptional("SpaceBeforeAssignmentOperators",
Style.SpaceBeforeAssignmentOperators);
IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
+
+ // For backward compatibility.
+ if (!IO.outputting()) {
+ IO.mapOptional("SpaceAfterControlStatementKeyword",
+ Style.SpaceBeforeParens);
+ }
+ IO.mapOptional("SpaceBeforeParens", Style.SpaceBeforeParens);
}
};
LLVMStyle.SpacesInParentheses = false;
LLVMStyle.SpaceInEmptyParentheses = false;
LLVMStyle.SpacesInCStyleCastParentheses = false;
- LLVMStyle.SpaceAfterControlStatementKeyword = true;
+ LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
LLVMStyle.SpaceBeforeAssignmentOperators = true;
LLVMStyle.ContinuationIndentWidth = 4;
LLVMStyle.SpacesInAngles = false;
GoogleStyle.SpacesInParentheses = false;
GoogleStyle.SpaceInEmptyParentheses = false;
GoogleStyle.SpacesInCStyleCastParentheses = false;
- GoogleStyle.SpaceAfterControlStatementKeyword = true;
+ GoogleStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements;
GoogleStyle.SpaceBeforeAssignmentOperators = true;
GoogleStyle.ContinuationIndentWidth = 4;
GoogleStyle.SpacesInAngles = false;
return Line.Type == LT_ObjCDecl ||
Left.isOneOf(tok::kw_return, tok::kw_new, tok::kw_delete,
tok::semi) ||
- (Style.SpaceAfterControlStatementKeyword &&
+ (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while, tok::kw_switch,
- tok::kw_catch));
+ tok::kw_catch)) ||
+ (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
+ Left.isOneOf(tok::identifier, tok::kw___attribute) &&
+ Line.Type != LT_PreprocessorDirective);
}
if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
return false;
}
TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
- verifyFormat("#define A (1)");
+ EXPECT_EQ("#define A (x)", format("#define A (x)"));
+ EXPECT_EQ("#define A(x)", format("#define A(x)"));
}
TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
getLLVMStyle()));
}
-TEST_F(FormatTest, ConfigurableSpaceAfterControlStatementKeyword) {
+TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
FormatStyle NoSpace = getLLVMStyle();
- NoSpace.SpaceAfterControlStatementKeyword = false;
+ NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
verifyFormat("while(true)\n"
" continue;", NoSpace);
"default:\n"
" break;\n"
"}", NoSpace);
+
+ FormatStyle Space = getLLVMStyle();
+ Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
+
+ verifyFormat("int f ();", Space);
+ verifyFormat("void f (int a, T b) {\n"
+ " while (true)\n"
+ " continue;\n"
+ "}",
+ Space);
+ verifyFormat("if (true)\n"
+ " f ();\n"
+ "else if (true)\n"
+ " f ();",
+ Space);
+ verifyFormat("do {\n"
+ " do_something ();\n"
+ "} while (something ());",
+ Space);
+ verifyFormat("switch (x) {\n"
+ "default:\n"
+ " break;\n"
+ "}",
+ Space);
+ verifyFormat("A::A () : a (1) {}", Space);
+ verifyFormat("void f () __attribute__ ((asdf));", Space);
+ verifyFormat("*(&a + 1);\n"
+ "&((&a)[1]);\n"
+ "a[(b + c) * d];\n"
+ "(((a + 1) * 2) + 3) * 4;",
+ Space);
+ verifyFormat("#define A(x) x", Space);
+ verifyFormat("#define A (x) x", Space);
+ verifyFormat("#if defined(x)\n"
+ "#endif",
+ Space);
}
TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
CHECK_PARSE_BOOL(SpacesInAngles);
CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
- CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);
CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
+ Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
+ CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
+ FormatStyle::SBPO_Never);
+ CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
+ FormatStyle::SBPO_Always);
+ CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
+ FormatStyle::SBPO_ControlStatements);
+ // For backward compatibility:
+ CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
+ FormatStyle::SBPO_Never);
+ CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
+ FormatStyle::SBPO_ControlStatements);
+
Style.ColumnLimit = 123;
FormatStyle BaseStyle = getLLVMStyle();
CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);