From: Daniel Jasper Date: Thu, 12 Mar 2015 15:04:53 +0000 (+0000) Subject: clang-format: When putting */& next to types, also wrap before them. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=43df2eea67c6fa2f3ee46ecdf270d14cbbf74349;p=clang clang-format: When putting */& next to types, also wrap before them. Before: LoooooooooooongType * loooooooooooongVariable; After: LoooooooooooongType *loooooooooooongVariable; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@232044 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 17a007f8b2..15fc07f62e 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -580,7 +580,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { return State.Stack.back().StartOfArraySubscripts; return ContinuationIndent; } - if (NextNonComment->is(TT_StartOfName) || + if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) || Previous.isOneOf(tok::coloncolon, tok::equal)) { return ContinuationIndent; } diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 74b7de0001..caca6458df 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1537,6 +1537,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, return Style.PenaltyReturnTypeOnItsOwnLine; return 200; } + if (Right.is(TT_PointerOrReference)) + return 200; if (Right.is(TT_TrailingReturnArrow)) return 110; if (Left.is(tok::equal) && Right.is(tok::l_brace)) @@ -1980,6 +1982,14 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return false; if (Left.isOneOf(TT_JavaAnnotation, TT_LeadingJavaAnnotation)) return !Right.is(tok::l_paren); + if (Left.is(TT_PointerOrReference)) + return (!Line.IsMultiVariableDeclStmt && + Style.PointerAlignment != FormatStyle::PAS_Right) || + Right.is(TT_FunctionDeclarationName); + if (Right.is(TT_PointerOrReference)) + return Line.IsMultiVariableDeclStmt || + (Style.PointerAlignment == FormatStyle::PAS_Right && + (!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName))); if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || Right.is(tok::kw_operator)) return true; @@ -2016,10 +2026,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return true; if (Right.is(TT_RangeBasedForLoopColon)) return false; - if (Right.is(TT_PointerOrReference) && Line.IsMultiVariableDeclStmt) - return true; - if (Left.isOneOf(TT_PointerOrReference, TT_TemplateCloser, - TT_UnaryOperator) || + if (Left.isOneOf(TT_TemplateCloser, TT_UnaryOperator) || Left.is(tok::kw_operator)) return false; if (Left.is(tok::equal) && Line.Type == LT_VirtualFunctionDecl) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 3e4806f5a2..34eb4bfe91 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -5648,6 +5648,8 @@ TEST_F(FormatTest, BreaksLongVariableDeclarations) { " LoooooooooooooooooooooooooooooooooooooooongVariable;"); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n" " LoooooooooooooooooooooooooooooooooooooooongVariable;"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" + " *LoooooooooooooooooooooooooooooooooooooooongVariable;"); // Different ways of ()-initializiation. verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" @@ -5665,6 +5667,8 @@ TEST_F(FormatTest, BreaksLongDeclarations) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n" + "LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n" @@ -5703,12 +5707,12 @@ TEST_F(FormatTest, BreaksLongDeclarations) { " SourceLocation L, IdentifierIn *II,\n" " Type *T) {}"); verifyFormat("ReallyLongReturnType\n" - "ReallyReallyLongFunctionName(\n" + "ReallyReaaallyLongFunctionName(\n" " const std::string &SomeParameter,\n" - " const SomeType &\n" - " ReallyReallyLongParameterName,\n" - " const SomeType &\n" - " AnotherLongParameterName) {}"); + " const SomeType\n" + " &ReallyReallyLongParameterName,\n" + " const SomeType\n" + " &AnotherLongParameterName) {}"); verifyFormat("template \n" "SomeLoooooooooooooooooooooongType<\n" " typename some_namespace::SomeOtherType::Type>\n" @@ -5732,8 +5736,8 @@ TEST_F(FormatTest, BreaksLongDeclarations) { " int aaaaaaaaaaaaaaaaaaaaaaa);"); verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n" - " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + " const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); } TEST_F(FormatTest, FormatsArrays) {