From bea1ab46d9ffdfc50108580c712596a54323a94c Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Sun, 1 Mar 2015 18:55:26 +0000 Subject: [PATCH] clang-format: Always align */& in multi-var DeclStmts. Seems like the most consistent thing to do and in multi-var DeclStmts, it is especially important to point out that the */& bind to the identifier. llvm-svn: 230903 --- clang/lib/Format/TokenAnnotator.cpp | 16 ++++++++++++---- clang/lib/Format/TokenAnnotator.h | 5 +++-- clang/unittests/Format/FormatTest.cpp | 21 +++++++++++---------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 7c35545..853a56b 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -526,8 +526,10 @@ private: Tok->Type = TT_ObjCForIn; break; case tok::comma: - if (Contexts.back().FirstStartOfName) + if (Contexts.back().FirstStartOfName && Contexts.size() == 1) { Contexts.back().FirstStartOfName->PartOfMultiVariableDeclStmt = true; + Line.IsMultiVariableDeclStmt = true; + } if (Contexts.back().InCtorInitializer) Tok->Type = TT_CtorInitializerComma; if (Contexts.back().IsForEachMacro) @@ -1671,15 +1673,19 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line, TT_FunctionDeclarationName)))) && (Left.Tok.isLiteral() || (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) && - Style.PointerAlignment != FormatStyle::PAS_Left)); + (Style.PointerAlignment != FormatStyle::PAS_Left || + Line.IsMultiVariableDeclStmt))); if (Right.is(TT_FunctionTypeLParen) && Left.isNot(tok::l_paren) && (!Left.is(TT_PointerOrReference) || - Style.PointerAlignment != FormatStyle::PAS_Right)) + (Style.PointerAlignment != FormatStyle::PAS_Right && + !Line.IsMultiVariableDeclStmt))) return true; if (Left.is(TT_PointerOrReference)) return Right.Tok.isLiteral() || Right.is(TT_BlockComment) || (!Right.isOneOf(TT_PointerOrReference, tok::l_paren) && - Style.PointerAlignment != FormatStyle::PAS_Right && Left.Previous && + (Style.PointerAlignment != FormatStyle::PAS_Right && + !Line.IsMultiVariableDeclStmt) && + Left.Previous && !Left.Previous->isOneOf(tok::l_paren, tok::coloncolon)); if (Right.is(tok::star) && Left.is(tok::l_paren)) return false; @@ -2006,6 +2012,8 @@ 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) || Left.is(tok::kw_operator)) diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h index 94ed1d1..a948cdb 100644 --- a/clang/lib/Format/TokenAnnotator.h +++ b/clang/lib/Format/TokenAnnotator.h @@ -42,8 +42,8 @@ public: : First(Line.Tokens.front().Tok), Level(Line.Level), InPPDirective(Line.InPPDirective), MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false), - Affected(false), LeadingEmptyLinesAffected(false), - ChildrenAffected(false) { + IsMultiVariableDeclStmt(false), Affected(false), + LeadingEmptyLinesAffected(false), ChildrenAffected(false) { assert(!Line.Tokens.empty()); // Calculate Next and Previous for all tokens. Note that we must overwrite @@ -90,6 +90,7 @@ public: bool InPPDirective; bool MustBeDeclaration; bool MightBeFunctionDecl; + bool IsMultiVariableDeclStmt; /// \c True if this line should be formatted, i.e. intersects directly or /// indirectly with one of the input ranges. diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 2028798..e763356 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -4495,17 +4495,18 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) { " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); - // FIXME: If multiple variables are defined, the "*" needs to move to the new - // line. Also fix indent for breaking after the type, this looks bad. - verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" - " * b = bbbbbbbbbbbbbbbbbbb;", - getGoogleStyle()); - // Not ideal, but pointer-with-type does not allow much here. - verifyGoogleFormat( - "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, * b = bbbbbbbbbbbbbbbbbbb,\n" - " * b = bbbbbbbbbbbbbbbbbbb, * d = ddddddddddddddddddd;"); + FormatStyle Style = getGoogleStyle(); + Style.PointerAlignment = FormatStyle::PAS_Left; + Style.DerivePointerAlignment = false; + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" + " *b = bbbbbbbbbbbbbbbbbbb;", + Style); + verifyFormat( + "aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" + " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;", + Style); } TEST_F(FormatTest, ConditionalExpressionsInBrackets) { -- 2.7.4