diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1175,15 +1175,29 @@ if (CurrentToken->isOneOf(tok::star, tok::amp)) CurrentToken->setType(TT_PointerOrReference); consumeToken(); - if (CurrentToken && CurrentToken->is(tok::comma) && + if (!CurrentToken) + continue; + if (CurrentToken->is(tok::comma) && CurrentToken->Previous->isNot(tok::kw_operator)) { break; } - if (CurrentToken && CurrentToken->Previous->isOneOf( - TT_BinaryOperator, TT_UnaryOperator, tok::comma, - tok::star, tok::arrow, tok::amp, tok::ampamp)) { + if (CurrentToken->Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, + tok::comma, tok::star, tok::arrow, + tok::amp, tok::ampamp)) { CurrentToken->Previous->setType(TT_OverloadedOperator); } + // User defined literal without a space. + if (CurrentToken->Previous->is(tok::string_literal) && + CurrentToken->Previous->TokenText.startswith("\"\"")) { + CurrentToken->Previous->setType(TT_OverloadedOperator); + } + // User defined literal with a space. + if (CurrentToken->Previous->is(tok::string_literal) && + CurrentToken->Previous->TokenText == "\"\"" && + CurrentToken->is(tok::identifier)) { + CurrentToken->Previous->setType(TT_OverloadedOperator); + CurrentToken->setType(TT_OverloadedOperator); + } } if (CurrentToken && CurrentToken->is(tok::l_paren)) CurrentToken->setType(TT_OverloadedOperatorLParen); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -10153,6 +10153,14 @@ // verifyFormat("void f() { operator*(a & a); }"); // verifyFormat("void f() { operator&(a, b * b); }"); + verifyFormat("void f() { return operator()(x) * b; }"); + verifyFormat("void f() { return operator[](x) * b; }"); + verifyFormat("void f() { return operator\"\"_a(x) * b; }"); + verifyFormat("void f() { return operator\"\" _a(x) * b; }"); + verifyFormat("void f() { return operator\"\"s(x) * b; }"); + verifyFormat("void f() { return operator\"\" s(x) * b; }"); + verifyFormat("void f() { return operator\"\"if(x) * b; }"); + verifyFormat("::operator delete(foo);"); verifyFormat("::operator new(n * sizeof(foo));"); verifyFormat("foo() { ::operator delete(foo); }"); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -376,6 +376,66 @@ EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference); } +TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) { + auto Tokens = annotate("x.operator+()"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::plus, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator=()"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::equal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator+=()"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::plusequal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator,()"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::comma, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator()()"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator[]()"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + // EXPECT_TOKEN(Tokens[3], tok::l_square, TT_OverloadedOperator); + // EXPECT_TOKEN(Tokens[4], tok::r_square, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator\"\"_a()"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator\"\" _a()"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::identifier, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator\"\"_long_literal()"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator\"\" _long_literal()"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::identifier, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator\"\"if()"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator\"\"s()"); + ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("x.operator\"\" s()"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::identifier, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen); +} + TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) { auto Tokens = annotate("template \n" "concept C = (Foo && Bar) && (Bar && Baz);");