Index: clang/lib/Format/ContinuationIndenter.cpp =================================================================== --- clang/lib/Format/ContinuationIndenter.cpp +++ clang/lib/Format/ContinuationIndenter.cpp @@ -1056,6 +1056,13 @@ return ContinuationIndent; if (Current.is(TT_ProtoExtensionLSquare)) return State.Stack.back().Indent; + // If the previous token is a Constructor initializer comma, and the + // ConstructorInitializerIndentWidth is 0 then the Indent will equal the + // FirstIndent, and it would default to the ContinuationIndentWidth and not + // the ConstructorInitializerIndentWidth width. + if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment && + PreviousNonComment->is(TT_CtorInitializerComma)) + return State.FirstIndent + Style.ConstructorInitializerIndentWidth; if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment && PreviousNonComment->isNot(tok::r_brace)) // Ensure that we fall back to the continuation indent width instead of Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -70,10 +70,11 @@ } void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, - const FormatStyle &Style = getLLVMStyle()) { + const FormatStyle &Style = getLLVMStyle(), + int line = __LINE__) { EXPECT_EQ(Expected.str(), format(Expected, Style)) - << "Expected code is not stable"; - EXPECT_EQ(Expected.str(), format(Code, Style)); + << "Expected code is not stable on line " << line; + EXPECT_EQ(Expected.str(), format(Code, Style)) << " on line " << line; if (Style.Language == FormatStyle::LK_Cpp) { // Objective-C++ is a superset of C++, so everything checked for C++ // needs to be checked for Objective-C++ as well. @@ -88,6 +89,9 @@ verifyFormat(Code, test::messUp(Code), Style); } +#define VERIFYFORMAT(code, style) \ + verifyFormat(code, test::messUp(code), style, __LINE__) + void verifyIncompleteFormat(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { EXPECT_EQ(Code.str(), @@ -11946,6 +11950,21 @@ "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", Style); + + Style = getLLVMStyle(); + Style.ColumnLimit = 0; + Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon; + Style.ConstructorInitializerIndentWidth = 2; + Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true; + Style.AllowAllConstructorInitializersOnNextLine = false; + VERIFYFORMAT("SomeClass::Constructor() :\n a(a),\n b(b),\n c(c) {}", + Style); + + Style.ConstructorInitializerIndentWidth = 1; + VERIFYFORMAT("SomeClass::Constructor() :\n a(a),\n b(b),\n c(c) {}", Style); + + Style.ConstructorInitializerIndentWidth = 0; + VERIFYFORMAT("SomeClass::Constructor() :\na(a),\nb(b),\nc(c) {}", Style); } TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {