rymiel created this revision. rymiel added reviewers: HazardyKnusperkeks, owenpan, MyDeveloperDay. rymiel added a project: clang-format. Herald added a project: All. rymiel requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
There should not be any cases where the angle brackets of template parameters are directly followed by a literal. It is more likely that a comparison is taking place instead. This patch makes the TokenAnnotator prefer to annotate < and > as operators when directly followed by a literal. A similar check already exists for literals directly *before* potential template args. Fixes https://github.com/llvm/llvm-project/issues/60140 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D142139 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -10345,6 +10345,7 @@ // Not template parameters. verifyFormat("return a < b && c > d;"); + verifyFormat("a < 0 ? b : a > 0 ? c : d;"); verifyFormat("void f() {\n" " while (a < b && c > d) {\n" " }\n" Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -125,19 +125,18 @@ if (NonTemplateLess.count(CurrentToken->Previous)) return false; - const FormatToken &Previous = *CurrentToken->Previous; // The '<'. - if (Previous.Previous) { - if (Previous.Previous->Tok.isLiteral()) + FormatToken *Left = CurrentToken->Previous; // The '<'. + if (Left->Previous) { + if (Left->Previous->Tok.isLiteral()) return false; - if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 && - (!Previous.Previous->MatchingParen || - !Previous.Previous->MatchingParen->is( + if (Left->Previous->is(tok::r_paren) && Contexts.size() > 1 && + (!Left->Previous->MatchingParen || + !Left->Previous->MatchingParen->is( TT_OverloadedOperatorLParen))) { return false; } } - FormatToken *Left = CurrentToken->Previous; Left->ParentBracket = Contexts.back().ContextKind; ScopedContextCreator ContextCreator(*this, tok::less, 12); @@ -185,6 +184,8 @@ } else { CurrentToken->setType(TT_TemplateCloser); } + if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral()) + return false; next(); return true; }
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -10345,6 +10345,7 @@ // Not template parameters. verifyFormat("return a < b && c > d;"); + verifyFormat("a < 0 ? b : a > 0 ? c : d;"); verifyFormat("void f() {\n" " while (a < b && c > d) {\n" " }\n" Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -125,19 +125,18 @@ if (NonTemplateLess.count(CurrentToken->Previous)) return false; - const FormatToken &Previous = *CurrentToken->Previous; // The '<'. - if (Previous.Previous) { - if (Previous.Previous->Tok.isLiteral()) + FormatToken *Left = CurrentToken->Previous; // The '<'. + if (Left->Previous) { + if (Left->Previous->Tok.isLiteral()) return false; - if (Previous.Previous->is(tok::r_paren) && Contexts.size() > 1 && - (!Previous.Previous->MatchingParen || - !Previous.Previous->MatchingParen->is( + if (Left->Previous->is(tok::r_paren) && Contexts.size() > 1 && + (!Left->Previous->MatchingParen || + !Left->Previous->MatchingParen->is( TT_OverloadedOperatorLParen))) { return false; } } - FormatToken *Left = CurrentToken->Previous; Left->ParentBracket = Contexts.back().ContextKind; ScopedContextCreator ContextCreator(*this, tok::less, 12); @@ -185,6 +184,8 @@ } else { CurrentToken->setType(TT_TemplateCloser); } + if (CurrentToken->Next && CurrentToken->Next->Tok.isLiteral()) + return false; next(); return true; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits