Skip to content

[clang-format] Add BreakBeforeInlineASMColon and BreakBeforeStructInitialization configurations #273

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,36 @@ struct FormatStyle {
/// \endcode
BraceWrappingFlags BraceWrapping;

/// If ``true``, colons in ASM parameters will be placed after line breaks.
/// \code
/// true:
/// asm volatile("loooooooooooooooooooooooooooooooooooooooooooooong",
/// :
/// : val);
///
/// false:
/// asm volatile("loooooooooooooooooooooooooooooooooooooooooooooong",
/// : : val);
/// \endcode
bool BreakBeforeInlineASMColon;

/// If ``true``, struct left brace will be placed after line breaks.
/// \code
/// true:
/// struct new_struct struct_name =
/// {
/// a = 1,
/// b = 2,
/// };
///
/// false:
/// struct new_struct struct_name = {
/// a = 1,
/// b = 2,
/// };
/// \endcode
bool BreakBeforeStructInitialization;

/// If ``true``, ternary operators will be placed after line breaks.
/// \code
/// true:
Expand Down Expand Up @@ -2431,6 +2461,8 @@ struct FormatStyle {
BinPackParameters == R.BinPackParameters &&
BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators &&
BreakBeforeBraces == R.BreakBeforeBraces &&
BreakBeforeInlineASMColon == R.BreakBeforeInlineASMColon &&
BreakBeforeStructInitialization == R.BreakBeforeStructInitialization &&
BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators &&
BreakConstructorInitializers == R.BreakConstructorInitializers &&
CompactNamespaces == R.CompactNamespaces &&
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack);
return (LambdaBodyLength > getColumnLimit(State));
}
if (Current.MustBreakBefore || Current.is(TT_InlineASMColon))
if (Current.MustBreakBefore || (Current.is(TT_InlineASMColon) && Style.BreakBeforeInlineASMColon))
return true;
if (State.Stack.back().BreakBeforeClosingBrace &&
Current.closesBlockOrBlockTypeList(Style))
Expand Down Expand Up @@ -953,6 +953,8 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {

const FormatToken &Previous = *Current.Previous;
// If we are continuing an expression, we want to use the continuation indent.
if (Style.BreakBeforeStructInitialization)
Style.ContinuationIndentWidth = 0;
unsigned ContinuationIndent =
std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
Style.ContinuationIndentWidth;
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,12 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakInheritanceList == FormatStyle::BILS_BeforeColon)
Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma;

IO.mapOptional("BreakBeforeInlineASMColon",
Style.BreakBeforeInlineASMColon);

IO.mapOptional("BreakBeforeStructInitialization",
Style.BreakBeforeStructInitialization);

IO.mapOptional("BreakBeforeTernaryOperators",
Style.BreakBeforeTernaryOperators);

Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3489,6 +3489,9 @@ static bool isAllmanBraceIncludedBreakableLambda(
bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
const FormatToken &Right) {
const FormatToken &Left = *Right.Previous;
if (Style.BreakBeforeStructInitialization && Right.is(tok::l_brace) &&
(Right.is(BK_BracedInit) || Left.is(tok::equal)))
return true;
if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
return true;

Expand Down