From 69bd8fb79a44f9173f98ae478735f58d7bb940f4 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 27 Sep 2013 07:49:08 +0000 Subject: [PATCH] clang-format: Fix formatting bug with comment in weird place. Before: template // T should be one of {A, B}. void f() {} After: template // T should be one of {A, B}. void f() {} llvm-svn: 191492 --- clang/lib/Format/ContinuationIndenter.cpp | 5 ++++- clang/unittests/Format/FormatTest.cpp | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 87c022294e47..8b0baead67af 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -192,6 +192,8 @@ unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline, unsigned ExtraSpaces) { const FormatToken &Current = *State.NextToken; const FormatToken &Previous = *State.NextToken->Previous; + const FormatToken *PreviousNonComment = + State.NextToken->getPreviousNonComment(); // Extra penalty that needs to be added because of the way certain line // breaks are chosen. @@ -253,7 +255,8 @@ unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline, State.Column = State.Stack.back().QuestionColumn; } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) { State.Column = State.Stack.back().VariablePos; - } else if (Previous.ClosesTemplateDeclaration || + } else if ((PreviousNonComment && + PreviousNonComment->ClosesTemplateDeclaration) || ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) && State.ParenLevel == 0 && diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 0175413a50c4..be2a45d1996b 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3500,6 +3500,9 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { TEST_F(FormatTest, WrapsTemplateDeclarations) { verifyFormat("template \n" "virtual void loooooooooooongFunction(int Param1, int Param2);"); + verifyFormat("template \n" + "// T should be one of {A, B}.\n" + "virtual void loooooooooooongFunction(int Param1, int Param2);"); verifyFormat( "template \n" "using comment_to_xml_conversion = comment_to_xml_conversion;");