From: Daniel Jasper Date: Fri, 5 Feb 2016 14:17:16 +0000 (+0000) Subject: clang-format: Fix corner case in template detection. X-Git-Tag: llvmorg-3.9.0-rc1~15023 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0c9772e8742645f65b6e657e9ec77d1c3b27798f;p=platform%2Fupstream%2Fllvm.git clang-format: Fix corner case in template detection. Before: f(a.operator() < A > ()); After: f(a.operator()()); llvm-svn: 259884 --- diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index b65c0e0..1e2946f 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -250,7 +250,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { // If the return type spans multiple lines, wrap before the function name. if ((Current.is(TT_FunctionDeclarationName) || (Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) && - State.Stack.back().BreakBeforeParameter) + !Previous.is(tok::kw_template) && State.Stack.back().BreakBeforeParameter) return true; if (startsSegmentOfBuilderTypeCall(Current) && diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 371c6a3..2089d9d 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -42,8 +42,21 @@ public: private: bool parseAngle() { - if (!CurrentToken) + if (!CurrentToken || !CurrentToken->Previous) + return false; + if (NonTemplateLess.count(CurrentToken->Previous)) return false; + + const FormatToken& Previous = *CurrentToken->Previous; + if (Previous.Previous) { + if (Previous.Previous->Tok.isLiteral()) + return false; + if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 && + (!Previous.Previous->MatchingParen || + !Previous.Previous->MatchingParen->is(TT_OverloadedOperatorLParen))) + return false; + } + FormatToken *Left = CurrentToken->Previous; Left->ParentBracket = Contexts.back().ContextKind; ScopedContextCreator ContextCreator(*this, tok::less, 10); @@ -550,11 +563,7 @@ private: return false; break; case tok::less: - if (!NonTemplateLess.count(Tok) && - (!Tok->Previous || - (!Tok->Previous->Tok.isLiteral() && - !(Tok->Previous->is(tok::r_paren) && Contexts.size() > 1))) && - parseAngle()) { + if (parseAngle()) { Tok->Type = TT_TemplateOpener; } else { Tok->Type = TT_BinaryOperator; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index e03a484..0db5d9c 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5394,6 +5394,10 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) { verifyFormat("struct A::type>;"); verifyFormat("template struct S{}> {};"); + verifyFormat("f(a.operator()());"); + verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " .template operator()());", + getLLVMStyleWithColumns(35)); // Not template parameters. verifyFormat("return a < b && c > d;");