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

Klemens Baum klemensbaum at gmail.com
Mon Aug 12 18:58:50 PDT 2013


  Fixed the issues that were noted.

Hi djasper, klimek,

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1360?vs=3359&id=3407#toc

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

Index: ../tools/clang/include/clang/Format/Format.h
===================================================================
--- ../tools/clang/include/clang/Format/Format.h
+++ ../tools/clang/include/clang/Format/Format.h
@@ -141,6 +141,10 @@
   /// \brief The number of characters to use for indentation.
   unsigned IndentWidth;
 
+  /// \brief The number of characters to use for indentation of constructor
+  /// initializer lists.
+  int ConstructorInitializerIndentWidth;
+
   /// \brief If \c true, always break after the \c template<...> of a template
   /// declaration.
   bool AlwaysBreakTemplateDeclarations;
@@ -192,6 +196,8 @@
 
   bool operator==(const FormatStyle &R) const {
     return AccessModifierOffset == R.AccessModifierOffset &&
+           ConstructorInitializerIndentWidth ==
+               R.ConstructorInitializerIndentWidth &&
            AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
            AlignTrailingComments == R.AlignTrailingComments &&
            AllowAllParametersOfDeclarationOnNextLine ==
Index: ../tools/clang/lib/Format/Format.cpp
===================================================================
--- ../tools/clang/lib/Format/Format.cpp
+++ ../tools/clang/lib/Format/Format.cpp
@@ -91,6 +91,8 @@
     }
 
     IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
+    IO.mapOptional("ConstructorInitializerIndentWidth",
+                   Style.ConstructorInitializerIndentWidth);
     IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
     IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
     IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
@@ -173,6 +175,7 @@
   LLVMStyle.IndentCaseLabels = false;
   LLVMStyle.IndentFunctionDeclarationAfterType = false;
   LLVMStyle.IndentWidth = 2;
+  LLVMStyle.ConstructorInitializerIndentWidth = 4;
   LLVMStyle.MaxEmptyLinesToKeep = 1;
   LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
   LLVMStyle.ObjCSpaceBeforeProtocolList = true;
@@ -209,6 +212,7 @@
   GoogleStyle.IndentCaseLabels = true;
   GoogleStyle.IndentFunctionDeclarationAfterType = true;
   GoogleStyle.IndentWidth = 2;
+  GoogleStyle.ConstructorInitializerIndentWidth = 4;
   GoogleStyle.MaxEmptyLinesToKeep = 1;
   GoogleStyle.NamespaceIndentation = FormatStyle::NI_None;
   GoogleStyle.ObjCSpaceBeforeProtocolList = false;
@@ -645,6 +649,12 @@
                  Previous.isOneOf(tok::coloncolon, tok::equal) ||
                  Previous.Type == TT_ObjCMethodExpr) {
         State.Column = ContinuationIndent;
+      } else if (Current.Type == TT_CtorInitializerColon) {
+        State.Column = FirstIndent + Style.ConstructorInitializerIndentWidth;
+        State.Stack.back().Indent = State.Column;
+      } else if (Current.Type == TT_CtorInitializerComma) {
+        assert(Style.BreakConstructorInitializersBeforeComma);
+        State.Column = State.Stack.back().Indent;
       } else {
         State.Column = State.Stack.back().Indent;
         // Ensure that we fall back to indenting 4 spaces instead of just
Index: ../tools/clang/unittests/Format/FormatTest.cpp
===================================================================
--- ../tools/clang/unittests/Format/FormatTest.cpp
+++ ../tools/clang/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,53 @@
             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
 }
 
+TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
+  FormatStyle Style = getLLVMStyle();
+
+  Style.ConstructorInitializerIndentWidth = 4;
+  verifyFormat(
+      "SomeClass::Constructor()\n"
+      "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
+      "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
+      Style);
+
+  Style.ConstructorInitializerIndentWidth = 2;
+  verifyFormat(
+      "SomeClass::Constructor()\n"
+      "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
+      "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
+      Style);
+
+  Style.ConstructorInitializerIndentWidth = 0;
+  verifyFormat(
+      "SomeClass::Constructor()\n"
+      ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
+      "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
+      Style);
+
+  Style.BreakConstructorInitializersBeforeComma = true;
+  Style.ConstructorInitializerIndentWidth = 4;
+  verifyFormat("SomeClass::Constructor()\n"
+               "    : a(a)\n"
+               "    , b(b)\n"
+               "    , c(c) {}",
+               Style);
+
+  Style.ConstructorInitializerIndentWidth = 2;
+  verifyFormat("SomeClass::Constructor()\n"
+               "  : a(a)\n"
+               "  , b(b)\n"
+               "  , c(c) {}",
+               Style);
+
+  Style.ConstructorInitializerIndentWidth = 0;
+  verifyFormat("SomeClass::Constructor()\n"
+               ": a(a)\n"
+               ", b(b)\n"
+               ", c(c) {}",
+               Style);
+}
+
 #endif
 
 } // end namespace tooling
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1360.2.patch
Type: text/x-patch
Size: 5210 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130812/e40172f6/attachment.bin>


More information about the cfe-commits mailing list