This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. rymiel marked an inline comment as done. Closed by commit rGdce5bb9a6f89: [clang-format] Correctly annotate UDLs as OverloadedOperator (authored by rymiel).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D134853/new/ https://reviews.llvm.org/D134853 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp clang/unittests/Format/TokenAnnotatorTest.cpp
Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -390,6 +390,55 @@ 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[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[5], tok::l_paren, TT_OverloadedOperatorLParen); +} + TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) { auto Tokens = annotate("template <typename T>\n" "concept C = (Foo && Bar) && (Bar && Baz);"); Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ 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); }"); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -1177,13 +1177,17 @@ 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) || + // User defined literal. + CurrentToken->Previous->TokenText.startswith("\"\"")) { CurrentToken->Previous->setType(TT_OverloadedOperator); } } @@ -2114,6 +2118,9 @@ if (Tok.Previous == Tok.MatchingParen || !Tok.Next || !Tok.MatchingParen) return false; + if (Tok.MatchingParen->is(TT_OverloadedOperatorLParen)) + return false; + FormatToken *LeftOfParens = Tok.MatchingParen->getPreviousNonComment(); if (LeftOfParens) { // If there is a closing parenthesis left of the current
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits