[llvm-dev] [PATCH 1/2] [clang-format] Add BreakBeforeStructInitialization configuration

Anastasiia Lukianenko via llvm-dev llvm-dev at lists.llvm.org
Thu Nov 19 04:40:13 PST 2020


From: Anastasiia Lukianenko <anastasiia_lukianenko at epam.com>

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 <anastasiia_lukianenko at epam.com>
---
 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 587e588525d..a3e04e1ee6f 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 d99107cb8b2..af6be432b5f 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 1c566c9ea49..eb4d64aa919 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -513,6 +513,9 @@ template <> struct MappingTraits<FormatStyle> {
         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 0fdcca867e3..37844ca845e 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;
 
-- 
2.17.1



More information about the llvm-dev mailing list