-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang-format] add an option to insert a space only for non-code block empty braces, not for empty parentheses #93634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4668,13 +4668,11 @@ struct FormatStyle { | |
bool SpaceBeforeRangeBasedForLoopColon; | ||
|
||
/// If ``true``, spaces will be inserted into ``{}``. | ||
/// \code | ||
/// true: false: | ||
/// void f() { } vs. void f() {} | ||
/// while (true) { } while (true) {} | ||
/// \endcode | ||
/// This option is **deprecated**. The previous behavior is preserved by using | ||
/// ``SpaceInEmptyBraces`` with ``Custom`` and by setting ``Block`` in | ||
/// ``SpaceInEmptyBracesOptions`` to ``true``. | ||
/// \version 10 | ||
bool SpaceInEmptyBlock; | ||
// bool SpaceInEmptyBlock; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry I don't understand are you removing this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was reviewing this PR and running into some issues related to https://reviews.llvm.org/D68415. I felt that we had to bite the bullet and add a new boolean option like the author did initially. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, we might be able to support the following enumerated values:
|
||
|
||
/// If ``true``, spaces may be inserted into ``()``. | ||
/// This option is **deprecated**. See ``InEmptyParentheses`` of | ||
|
@@ -4913,6 +4911,107 @@ struct FormatStyle { | |
/// \version 17 | ||
SpacesInParensCustom SpacesInParensOptions; | ||
|
||
/// Different ways to put a space in empty braces. | ||
enum SpaceInEmptyBracesStyle : int8_t { | ||
/// Never put a space in empty braces. | ||
/// \code | ||
/// void f() {} | ||
/// T x{}; | ||
/// while (true) {} | ||
/// struct U1 {}; | ||
/// union U2 {}; | ||
/// class U3 {}; | ||
/// enum U4 {}; | ||
/// \endcode | ||
SIEBO_Never, | ||
/// Always put a space in empty braces. | ||
SIEBO_Always, | ||
/// Configure each individual space in empty braces in | ||
/// `SpacesInEmptyBracesOptions`. | ||
SIEBO_Custom, | ||
}; | ||
|
||
/// Defines in which cases spaces will be inserted in empty braces. | ||
/// \version 20 | ||
SpaceInEmptyBracesStyle SpaceInEmptyBraces; | ||
|
||
/// Precise control over the spacing in empty braces. | ||
/// \code | ||
/// # Should be declared this way: | ||
/// SpaceInEmptyBraces: Custom | ||
/// SpaceInEmptyBracesOptions: | ||
/// Function: true | ||
/// Record: false | ||
/// InitList: true | ||
/// Block: false | ||
/// \endcode | ||
struct SpaceInEmptyBracesCustom { | ||
/// Put a space in empty braces of function definition. | ||
/// \code | ||
/// true: false: | ||
/// void f() { } vs. void f() {} | ||
/// \endcode | ||
bool Function; | ||
/// Put a space in empty braces of record/struct definition. | ||
/// \code | ||
/// true: false: | ||
/// struct U1 { }; vs. struct U1 {}; | ||
/// union U2 { }; union U2 {}; | ||
/// class U3 { }; class U3 {}; | ||
/// enum U4 { }; enum U4 {}; | ||
/// \endcode | ||
bool Record; | ||
/// Put a space in empty braces of initializer list. | ||
/// \code | ||
/// true: false: | ||
/// T x{ }; vs. T x{}; | ||
/// \endcode | ||
bool InitList; | ||
/// Put a space in empty braces of other blocks, including functions and | ||
/// record, compatible with ``SpaceInEmptyBlock``. | ||
/// \code | ||
/// true: false: | ||
/// void f() { } vs. void f() {} | ||
/// enum Unit { }; enum Unit {}; | ||
/// while (true) { } while (true) {} | ||
/// \endcode | ||
bool Block; | ||
|
||
SpaceInEmptyBracesCustom() | ||
: Function(false), Record(false), InitList(false), Block(false) {} | ||
|
||
SpaceInEmptyBracesCustom(bool Function, bool Record, bool InitList, | ||
bool Block) | ||
: Function(Function), Record(Record), InitList(InitList), Block(Block) { | ||
} | ||
|
||
bool operator==(const SpaceInEmptyBracesCustom &R) const { | ||
return Function == R.Function && Record == R.Record && | ||
InitList == R.InitList && Block == R.Block; | ||
} | ||
|
||
bool operator!=(const SpaceInEmptyBracesCustom &R) const { | ||
return !(*this == R); | ||
} | ||
}; | ||
|
||
/// Control of individual spaces in empty braces. | ||
/// | ||
/// If ``SpaceInEmptyBraces`` is set to ``Custom``, use this to specify | ||
/// how each individual space in empty braces case should be handled. | ||
/// Otherwise, this is ignored. | ||
/// \code{.yaml} | ||
/// # Example of usage: | ||
/// SpaceInEmptyBraces: Custom | ||
/// SpaceInEmptyBracesOptions: | ||
/// Function: true | ||
/// Record: false | ||
/// InitList: true | ||
/// Block: false | ||
/// \endcode | ||
/// \version 20 | ||
SpaceInEmptyBracesCustom SpaceInEmptyBracesOptions; | ||
|
||
/// If ``true``, spaces will be inserted after ``[`` and before ``]``. | ||
/// Lambdas without arguments or unspecified size array declarations will not | ||
/// be affected. | ||
|
@@ -5339,7 +5438,8 @@ struct FormatStyle { | |
SpaceBeforeRangeBasedForLoopColon == | ||
R.SpaceBeforeRangeBasedForLoopColon && | ||
SpaceBeforeSquareBrackets == R.SpaceBeforeSquareBrackets && | ||
SpaceInEmptyBlock == R.SpaceInEmptyBlock && | ||
SpaceInEmptyBraces == R.SpaceInEmptyBraces && | ||
SpaceInEmptyBracesOptions == R.SpaceInEmptyBracesOptions && | ||
SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments && | ||
SpacesInAngles == R.SpacesInAngles && | ||
SpacesInContainerLiterals == R.SpacesInContainerLiterals && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -761,6 +761,24 @@ template <> struct MappingTraits<FormatStyle::SpacesInLineComment> { | |
} | ||
}; | ||
|
||
template <> struct MappingTraits<FormatStyle::SpaceInEmptyBracesCustom> { | ||
static void mapping(IO &IO, FormatStyle::SpaceInEmptyBracesCustom &Space) { | ||
IO.mapOptional("Function", Space.Function); | ||
IO.mapOptional("Record", Space.Record); | ||
IO.mapOptional("InitList", Space.InitList); | ||
IO.mapOptional("Block", Space.Block); | ||
} | ||
}; | ||
|
||
template <> | ||
struct ScalarEnumerationTraits<FormatStyle::SpaceInEmptyBracesStyle> { | ||
static void enumeration(IO &IO, FormatStyle::SpaceInEmptyBracesStyle &Value) { | ||
IO.enumCase(Value, "Never", FormatStyle::SIEBO_Never); | ||
khei4 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
IO.enumCase(Value, "Always", FormatStyle::SIEBO_Always); | ||
IO.enumCase(Value, "Custom", FormatStyle::SIEBO_Custom); | ||
} | ||
}; | ||
|
||
template <> struct MappingTraits<FormatStyle::SpacesInParensCustom> { | ||
static void mapping(IO &IO, FormatStyle::SpacesInParensCustom &Spaces) { | ||
IO.mapOptional("ExceptDoubleParentheses", Spaces.ExceptDoubleParentheses); | ||
|
@@ -905,6 +923,7 @@ template <> struct MappingTraits<FormatStyle> { | |
bool UseCRLF = false; | ||
|
||
bool SpaceInEmptyParentheses = false; | ||
bool SpaceInEmptyBlock = false; | ||
bool SpacesInConditionalStatement = false; | ||
bool SpacesInCStyleCastParentheses = false; | ||
bool SpacesInParentheses = false; | ||
|
@@ -934,6 +953,7 @@ template <> struct MappingTraits<FormatStyle> { | |
IO.mapOptional("SpaceAfterControlStatementKeyword", | ||
Style.SpaceBeforeParens); | ||
IO.mapOptional("SpaceInEmptyParentheses", SpaceInEmptyParentheses); | ||
IO.mapOptional("SpaceInEmptyBlock", SpaceInEmptyBlock); | ||
IO.mapOptional("SpacesInConditionalStatement", | ||
SpacesInConditionalStatement); | ||
IO.mapOptional("SpacesInCStyleCastParentheses", | ||
|
@@ -1154,14 +1174,16 @@ template <> struct MappingTraits<FormatStyle> { | |
Style.SpaceBeforeRangeBasedForLoopColon); | ||
IO.mapOptional("SpaceBeforeSquareBrackets", | ||
Style.SpaceBeforeSquareBrackets); | ||
IO.mapOptional("SpaceInEmptyBlock", Style.SpaceInEmptyBlock); | ||
IO.mapOptional("SpacesBeforeTrailingComments", | ||
Style.SpacesBeforeTrailingComments); | ||
IO.mapOptional("SpacesInAngles", Style.SpacesInAngles); | ||
IO.mapOptional("SpacesInContainerLiterals", | ||
Style.SpacesInContainerLiterals); | ||
IO.mapOptional("SpacesInLineCommentPrefix", | ||
Style.SpacesInLineCommentPrefix); | ||
IO.mapOptional("SpaceInEmptyBraces", Style.SpaceInEmptyBraces); | ||
IO.mapOptional("SpaceInEmptyBracesOptions", | ||
Style.SpaceInEmptyBracesOptions); | ||
IO.mapOptional("SpacesInParens", Style.SpacesInParens); | ||
IO.mapOptional("SpacesInParensOptions", Style.SpacesInParensOptions); | ||
IO.mapOptional("SpacesInSquareBrackets", Style.SpacesInSquareBrackets); | ||
|
@@ -1260,6 +1282,15 @@ template <> struct MappingTraits<FormatStyle> { | |
} | ||
Style.SpacesInParens = FormatStyle::SIPO_Custom; | ||
} | ||
|
||
if (Style.SpaceInEmptyBraces != FormatStyle::SIEBO_Custom && | ||
SpaceInEmptyBlock) { | ||
Style.SpaceInEmptyBraces = FormatStyle::SIEBO_Custom; | ||
Style.SpaceInEmptyBracesOptions.Function = true; | ||
Style.SpaceInEmptyBracesOptions.Record = true; | ||
Style.SpaceInEmptyBracesOptions.InitList = false; | ||
Style.SpaceInEmptyBracesOptions.Block = true; | ||
} | ||
} | ||
}; | ||
|
||
|
@@ -1632,7 +1663,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { | |
LLVMStyle.SpaceBeforeParensOptions.AfterIfMacros = true; | ||
LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true; | ||
LLVMStyle.SpaceBeforeSquareBrackets = false; | ||
LLVMStyle.SpaceInEmptyBlock = false; | ||
LLVMStyle.SpaceInEmptyBraces = FormatStyle::SIEBO_Never; | ||
LLVMStyle.SpacesBeforeTrailingComments = 1; | ||
LLVMStyle.SpacesInAngles = FormatStyle::SIAS_Never; | ||
LLVMStyle.SpacesInContainerLiterals = true; | ||
|
@@ -1934,7 +1965,9 @@ FormatStyle getWebKitStyle() { | |
Style.ObjCSpaceAfterProperty = true; | ||
Style.PointerAlignment = FormatStyle::PAS_Left; | ||
Style.SpaceBeforeCpp11BracedList = true; | ||
Style.SpaceInEmptyBlock = true; | ||
Style.SpaceInEmptyBraces = FormatStyle::SIEBO_Always; | ||
Style.SpacesInParensOptions.InEmptyParentheses = false; | ||
|
||
Style.SpacesInParensOptions.Other = false; | ||
return Style; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can these be Never/Always/Leave