Author: Emilia Dreamer Date: 2022-11-17T11:38:28+02:00 New Revision: 48a932e13e04a21f4422d962bf1bb4667a670e41
URL: https://github.com/llvm/llvm-project/commit/48a932e13e04a21f4422d962bf1bb4667a670e41 DIFF: https://github.com/llvm/llvm-project/commit/48a932e13e04a21f4422d962bf1bb4667a670e41.diff LOG: [clang-format] Remove special case for kw_operator when aligning decls This change breaks no existing tests but does fix the linked issue. Declarations of operator overloads are annotated with `TT_FunctionDeclarationName` on the `operator` keyword, which is already being checked for when aligning, so the extra `kw_operator` doesn't seem to be necessary. (just for reference, it was added in rG92b397fb9d55ccdf4632c2b1b15b4a0ee417cf74 / 92b397fb9d55ccdf4632c2b1b15b4a0ee417cf74) Fixes https://github.com/llvm/llvm-project/issues/55733 Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D137223 Added: Modified: clang/lib/Format/WhitespaceManager.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 1f29f7ab917c..cd9773921dae 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -870,9 +870,7 @@ void WhitespaceManager::alignConsecutiveDeclarations() { AlignTokens( Style, [](Change const &C) { - // tok::kw_operator is necessary for aligning operator overload - // definitions. - if (C.Tok->isOneOf(TT_FunctionDeclarationName, tok::kw_operator)) + if (C.Tok->is(TT_FunctionDeclarationName)) return true; if (C.Tok->isNot(TT_StartOfName)) return false; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index adacb9761ca3..2e3bbd7e60b5 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -423,50 +423,80 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionRefQualifiers) { TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) { auto Tokens = annotate("x.operator+()"); ASSERT_EQ(Tokens.size(), 7u) << Tokens; + EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_FunctionDeclarationName); 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[2], tok::kw_operator, TT_FunctionDeclarationName); 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[2], tok::kw_operator, TT_FunctionDeclarationName); 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[2], tok::kw_operator, TT_FunctionDeclarationName); 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[2], tok::kw_operator, TT_FunctionDeclarationName); 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[2], tok::kw_operator, TT_FunctionDeclarationName); // 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[2], tok::kw_operator, TT_FunctionDeclarationName); 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; + // FIXME + // EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_FunctionDeclarationName); 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[2], tok::kw_operator, TT_FunctionDeclarationName); 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[2], tok::kw_operator, TT_FunctionDeclarationName); 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; + // FIXME + // EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_FunctionDeclarationName); EXPECT_TOKEN(Tokens[3], tok::string_literal, TT_OverloadedOperator); EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_OverloadedOperatorLParen); + + Tokens = annotate("int operator+(int);"); + ASSERT_EQ(Tokens.size(), 8u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::kw_operator, TT_FunctionDeclarationName); + EXPECT_TOKEN(Tokens[2], tok::plus, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("auto operator=(T&) {}"); + ASSERT_EQ(Tokens.size(), 10u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::kw_operator, TT_FunctionDeclarationName); + EXPECT_TOKEN(Tokens[2], tok::equal, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_OverloadedOperatorLParen); + Tokens = annotate("auto operator()() {}"); + ASSERT_EQ(Tokens.size(), 9u) << Tokens; + EXPECT_TOKEN(Tokens[1], tok::kw_operator, TT_FunctionDeclarationName); + EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_OverloadedOperator); + EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen); } TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits