diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index e5abf833194d4..88e80349ad31c 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1566,7 +1566,8 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { } if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) || - Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon)) { + Previous.isOneOf(TT_PointerOrReference, tok::coloncolon, tok::equal, + TT_JsTypeColon)) { return ContinuationIndent; } if (PreviousNonComment && PreviousNonComment->is(tok::colon) && diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 25971d2497f97..3ff5587ab2232 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -4343,7 +4343,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, return Style.PenaltyReturnTypeOnItsOwnLine; return 200; } - if (Right.is(TT_PointerOrReference)) + if (Left.is(TT_PointerOrReference) || Right.is(TT_PointerOrReference)) return 190; if (Right.is(TT_LambdaArrow)) return 110; @@ -6262,8 +6262,10 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, TT_ClassHeadName, TT_QtProperty, tok::kw_operator)) { return true; } + // It is fine to break the line when the * or & should be separate from the + // name according to the PointerAlignment config. if (Left.is(TT_PointerOrReference)) - return false; + return Right.SpacesRequiredBefore; if (Right.isTrailingComment()) { // We rely on MustBreakBefore being set correctly here as we should not // change the "binding" behavior of a comment. diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index ce68f91bef02a..83b13e805210e 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -8644,6 +8644,28 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}", Style); + Style.ColumnLimit = 70; + verifyFormat( + "void foo(const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n" + " my_super_super_super_super_long_variable_name) {}", + Style); + verifyFormat( + "void foo(const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName*\n" + " const my_super_super_super_super_long_variable_name) {}", + Style); + + Style.PointerAlignment = FormatStyle::PAS_Middle; + verifyFormat( + "void foo(\n" + " const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n" + " my_super_super_super_super_long_variable_name) {}", + Style); + verifyFormat( + "void foo(\n" + " const MySuperSuperSuperSuperSuperSuperSuperSuperLongTypeName *\n" + " const my_super_super_super_super_long_variable_name) {}", + Style); + Style = getLLVMStyleWithColumns(45); Style.PenaltyReturnTypeOnItsOwnLine = 400; verifyFormat("template