Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -119,7 +119,16 @@ (Style.Language == FormatStyle::LK_Proto && Left->Previous && Left->Previous->isOneOf(TT_SelectorName, TT_DictLiteral))) CurrentToken->Type = TT_DictLiteral; - else + // In if/for/while (i < x >> 1) ensure we don't see the <> as + // TemplateOpener/Closer + else if (CurrentToken->Next && CurrentToken->Next->is(tok::greater) && + CurrentToken->Next->Next && + !CurrentToken->Next->Next->isOneOf(tok::l_paren, + tok::coloncolon) && + Line.First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_while)) { + Left->Type = TT_BinaryOperator; + return false; + } else CurrentToken->Type = TT_TemplateCloser; next(); return true; @@ -1522,8 +1531,8 @@ if (Style.Language == FormatStyle::LK_JavaScript) { if (Current.is(tok::exclaim)) { if (Current.Previous && - (Keywords.IsJavaScriptIdentifier( - *Current.Previous, /* AcceptIdentifierName= */ true) || + (Keywords.IsJavaScriptIdentifier(*Current.Previous, + /*AcceptIdentifierName=*/true) || Current.Previous->isOneOf( tok::kw_namespace, tok::r_paren, tok::r_square, tok::r_brace, Keywords.kw_type, Keywords.kw_get, Keywords.kw_set) || @@ -2174,7 +2183,7 @@ FormatToken *Current; }; -} // end anonymous namespace +} // namespace void TokenAnnotator::setCommentLineLevels( SmallVectorImpl &Lines) { Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -7065,6 +7065,21 @@ verifyFormat("static_assert(is_convertible::value, \"AAA\");"); verifyFormat("Constructor(A... a) : a_(X{std::forward(a)}...) {}"); verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <"); + + verifyFormat("some_templated_type"); +} + +TEST_F(FormatTest, UnderstandsShiftOperators) { + verifyFormat("if (i < x >> 1)"); + verifyFormat("while (i < x >> 1)"); + verifyFormat("for (unsigned i = 0; i < i; ++i, v = v >> 1)"); + verifyFormat("for (unsigned i = 0; i < x >> 1; ++i, v = v >> 1)"); + verifyFormat( + "for (std::vector::iterator i = 0; i < x >> 1; ++i, v = v >> 1)"); + verifyFormat("Foo.call>()"); + verifyFormat("if (Foo.call>() == 0)"); + verifyFormat("for (std::vector>::iterator i = 0; i < x >> 1; " + "++i, v = v >> 1)"); } TEST_F(FormatTest, BitshiftOperatorWidth) {