[PATCH] [ClangFormat] Add ConstructorInitializerOffset to control initializer list indentation

Klemens Baum klemensbaum at gmail.com
Sun Aug 11 19:44:27 PDT 2013


Some coding styles don't use different indent widths for initializer lists to visually separate them from blocks.

Boost.Spirit for instance uses 2 spaces in initializer lists and 4 spaces elsewhere.

This patch adds the ConstructorInitializerOffset option, that can be used to specify the relative indentation of initializer lists.

http://llvm-reviews.chandlerc.com/D1360

Files:
  include/clang/Format/Format.h
  lib/Format/Format.cpp
  unittests/Format/FormatTest.cpp

Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -61,6 +61,9 @@
   /// \brief The extra indent or outdent of access modifiers (e.g.: public:).
   int AccessModifierOffset;
 
+  /// \brief The extra indent or outdent of constructor initializer lists.
+  int ConstructorInitializerOffset;
+
   enum LanguageStandard {
     LS_Cpp03,
     LS_Cpp11,
@@ -192,6 +195,7 @@
 
   bool operator==(const FormatStyle &R) const {
     return AccessModifierOffset == R.AccessModifierOffset &&
+           ConstructorInitializerOffset == R.ConstructorInitializerOffset &&
            AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
            AlignTrailingComments == R.AlignTrailingComments &&
            AllowAllParametersOfDeclarationOnNextLine ==
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -91,6 +91,8 @@
     }
 
     IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
+    IO.mapOptional("ConstructorInitializerOffset",
+                   Style.ConstructorInitializerOffset);
     IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
     IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
     IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
@@ -167,6 +169,7 @@
   LLVMStyle.BreakConstructorInitializersBeforeComma = false;
   LLVMStyle.ColumnLimit = 80;
   LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
+  LLVMStyle.ConstructorInitializerOffset = 0;
   LLVMStyle.Cpp11BracedListStyle = false;
   LLVMStyle.DerivePointerBinding = false;
   LLVMStyle.ExperimentalAutoDetectBinPacking = false;
@@ -203,6 +206,7 @@
   GoogleStyle.BreakConstructorInitializersBeforeComma = false;
   GoogleStyle.ColumnLimit = 80;
   GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
+  GoogleStyle.ConstructorInitializerOffset = 0;
   GoogleStyle.Cpp11BracedListStyle = true;
   GoogleStyle.DerivePointerBinding = true;
   GoogleStyle.ExperimentalAutoDetectBinPacking = false;
@@ -651,6 +655,11 @@
         // flushing continuations left.
         if (State.Column == FirstIndent)
           State.Column += 4;
+        if (Current.Type == TT_CtorInitializerColon ||
+            (Style.BreakConstructorInitializersBeforeComma ? Current : Previous)
+                    .Type == TT_CtorInitializerComma) {
+          State.Column += Style.ConstructorInitializerOffset;
+        }
       }
 
       if (Current.is(tok::question))
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -4175,7 +4175,7 @@
                "  f();\n");
 
   // This is simply incomplete. Formatting is not important, but must not crash.
-  verifyFormat("class A:"); 
+  verifyFormat("class A:");
 }
 
 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
@@ -5847,6 +5847,43 @@
             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
 }
 
+TEST_F(FormatTest, ConstructorInitializerOffset) {
+  FormatStyle BreakBeforeCommas = getLLVMStyle();
+  BreakBeforeCommas.BreakConstructorInitializersBeforeComma = true;
+  BreakBeforeCommas.ConstructorInitializerOffset = -2;
+  BreakBeforeCommas.BreakBeforeBraces = BS_Allman;
+
+  verifyFormat("struct foo\n"
+               "{\n"
+               "    foo()\n"
+               "      : a(a)\n"
+               "      , b(b)\n"
+               "      , c(c)\n"
+               "    {\n"
+               "    }\n"
+               "    int a, b, c;\n"
+               "};\n",
+               BreakBeforeCommas);
+
+  FormatStyle BreakAt80Chars = getLLVMStyle();
+  verifyFormat(
+      "SomeClass::Constructor()\n"
+      "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
+      "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
+
+  BreakAt80Chars.ConstructorInitializerOffset = -2;
+  verifyFormat(
+      "SomeClass::Constructor()\n"
+      "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
+      "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
+
+  BreakAt80Chars.ConstructorInitializerOffset = -3;
+  verifyFormat(
+      "SomeClass::Constructor()\n"
+      " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
+      "   aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
+}
+
 #endif
 
 } // end namespace tooling
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1360.1.patch
Type: text/x-patch
Size: 4553 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130811/78285cb8/attachment.bin>


More information about the cfe-commits mailing list