From ff2bddb1113c9c0b584fd529231ddda638575229 Mon Sep 17 00:00:00 2001 From: Anastasiia Lukianenko Date: Wed, 18 Nov 2020 18:37:34 +0200 Subject: [PATCH 1/2] [clang-format] Add BreakBeforeStructInitialization configuration If ``true``, struct left brace will be placed after line breaks. true: struct new_struct struct_name = {...}; false: struct new_struct struct_name = { ...}; Signed-off-by: Anastasiia Lukianenko --- clang/include/clang/Format/Format.h | 18 ++++++++++++++++++ clang/lib/Format/ContinuationIndenter.cpp | 2 ++ clang/lib/Format/Format.cpp | 3 +++ clang/lib/Format/TokenAnnotator.cpp | 3 +++ 4 files changed, 26 insertions(+) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 587e588525dfa..a3e04e1ee6f28 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1160,6 +1160,23 @@ struct FormatStyle { /// \endcode BraceWrappingFlags BraceWrapping; + /// 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: @@ -2431,6 +2448,7 @@ struct FormatStyle { BinPackParameters == R.BinPackParameters && BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators && BreakBeforeBraces == R.BreakBeforeBraces && + BreakBeforeStructInitialization == R.BreakBeforeStructInitialization && BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators && BreakConstructorInitializers == R.BreakConstructorInitializers && CompactNamespaces == R.CompactNamespaces && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index d99107cb8b2c0..af6be432b5f4e 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -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; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 1c566c9ea49d4..eb4d64aa91926 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -513,6 +513,9 @@ template <> struct MappingTraits { Style.BreakInheritanceList == FormatStyle::BILS_BeforeColon) Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; + IO.mapOptional("BreakBeforeStructInitialization", + Style.BreakBeforeStructInitialization); + IO.mapOptional("BreakBeforeTernaryOperators", Style.BreakBeforeTernaryOperators); diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 0fdcca867e3d8..37844ca845eaa 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -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; From c4c2c8997ff6598a65da4c4fcf7fed4a055c2af0 Mon Sep 17 00:00:00 2001 From: Anastasiia Lukianenko Date: Tue, 10 Nov 2020 13:39:28 +0200 Subject: [PATCH 2/2] [clang-format] Add BreakBeforeInlineASMColon configuration If ``true``, colons in ASM parameters will be placed after line breaks. true: asm volatile("loooooooooooooooooooooooooooooooooooooooooooooong", : : val); false: asm volatile("loooooooooooooooooooooooooooooooooooooooooooooong", : : val); Signed-off-by: Anastasiia Lukianenko --- clang/include/clang/Format/Format.h | 14 ++++++++++++++ clang/lib/Format/ContinuationIndenter.cpp | 2 +- clang/lib/Format/Format.cpp | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index a3e04e1ee6f28..c3290949b6b3c 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1160,6 +1160,19 @@ 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: @@ -2448,6 +2461,7 @@ struct FormatStyle { BinPackParameters == R.BinPackParameters && BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators && BreakBeforeBraces == R.BreakBeforeBraces && + BreakBeforeInlineASMColon == R.BreakBeforeInlineASMColon && BreakBeforeStructInitialization == R.BreakBeforeStructInitialization && BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators && BreakConstructorInitializers == R.BreakConstructorInitializers && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index af6be432b5f4e..bb5d547550582 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -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)) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index eb4d64aa91926..1ec2110714ef2 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -513,6 +513,9 @@ template <> struct MappingTraits { Style.BreakInheritanceList == FormatStyle::BILS_BeforeColon) Style.BreakInheritanceList = FormatStyle::BILS_BeforeComma; + IO.mapOptional("BreakBeforeInlineASMColon", + Style.BreakBeforeInlineASMColon); + IO.mapOptional("BreakBeforeStructInitialization", Style.BreakBeforeStructInitialization);