Index: clang/lib/Format/ContinuationIndenter.cpp =================================================================== --- clang/lib/Format/ContinuationIndenter.cpp +++ clang/lib/Format/ContinuationIndenter.cpp @@ -404,6 +404,7 @@ (State.Column + State.Line->Last->TotalLength - Previous.TotalLength > getColumnLimit(State) || CurrentState.BreakBeforeParameter) && + (!Current.isTrailingComment() || Current.NewlinesBefore > 0) && (Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All || Style.BreakConstructorInitializers != FormatStyle::BCIS_BeforeColon || Style.ColumnLimit != 0)) { @@ -793,6 +794,7 @@ (Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) { CurrentState.LastSpace = State.Column; } else if (Previous.is(TT_CtorInitializerColon) && + (!Current.isTrailingComment() || Current.NewlinesBefore > 0) && Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) { CurrentState.Indent = State.Column; @@ -1032,7 +1034,7 @@ // be considered bin packing unless the relevant AllowAll option is false or // this is a dict/object literal. bool PreviousIsBreakingCtorInitializerColon = - Previous.is(TT_CtorInitializerColon) && + PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) && Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon; if (!(Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) || PreviousIsBreakingCtorInitializerColon) || Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -4734,7 +4734,7 @@ // the first list element. Otherwise, it should be placed outside of the // list. return Left.is(BK_BracedInit) || - (Left.is(TT_CtorInitializerColon) && + (Left.is(TT_CtorInitializerColon) && Right.NewlinesBefore > 0 && Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon); } if (Left.is(tok::question) && Right.is(tok::colon)) @@ -4894,8 +4894,10 @@ if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral)) return true; - if (Left.is(TT_CtorInitializerColon)) - return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon; + if (Left.is(TT_CtorInitializerColon)) { + return Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon && + (!Right.isTrailingComment() || Right.NewlinesBefore > 0); + } if (Right.is(TT_CtorInitializerColon)) return Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon; if (Left.is(TT_CtorInitializerComma) && Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -7034,6 +7034,10 @@ "SomeClass::Constructor() :\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", Style); + verifyFormat( + "SomeClass::Constructor() : // NOLINT\n" + " aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}", + Style); Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack; verifyFormat( @@ -7101,6 +7105,10 @@ " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", OnePerLine); + verifyFormat("Foo::Foo(int i, int j) : // NOLINT\n" + " i(i), // comment\n" + " j(j) {}", + OnePerLine); verifyFormat("MyClass::MyClass(int var) :\n" " some_var_(var), // 4 space indent\n" " some_other_var_(var + 1) { // lined up\n" @@ -7133,6 +7141,17 @@ " // Comment forcing unwanted break.\n" " aaaa(aaaa) {}", Style); + verifyFormat("Constructor() : // NOLINT\n" + " aaaa(aaaa) {}", + Style); + verifyFormat("Constructor() : // A very long trailing comment that cannot fit" + " on a single\n" + " // line.\n" + " aaaa(aaaa) {}", + "Constructor() : // A very long trailing comment that cannot fit" + " on a single line.\n" + " aaaa(aaaa) {}", + Style); Style.ColumnLimit = 0; verifyFormat("SomeClass::Constructor() :\n"