Skip to content

Commit

Permalink
[clang-format] Don't modify template arguments on the LHS of assignment
Browse files Browse the repository at this point in the history
After clang-format has determined that an equals sign starts an
expression, it will also go backwards and modify any star/amp/ampamp
binary operators on the left side of the assignment to be
pointers/references instead.

There already exists logic to skip over contents of parentheses and
square brackets, but this patch also expands that logic to apply to
angle brackets. This is so that binary operators inside of template
arguments would not be touched, primary arguments to non-type template
parameters.

Fixes #62055

Reviewed By: owenpan, MyDeveloperDay, HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D148024
  • Loading branch information
rymiel committed Apr 11, 2023
1 parent 8af5756 commit 5dc94b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,7 @@ class AnnotatingParser {
Previous && Previous->Previous &&
!Previous->Previous->isOneOf(tok::comma, tok::semi);
Previous = Previous->Previous) {
if (Previous->isOneOf(tok::r_square, tok::r_paren)) {
if (Previous->isOneOf(tok::r_square, tok::r_paren, tok::greater)) {
Previous = Previous->MatchingParen;
if (!Previous)
break;
Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,18 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionTypeLParen);
EXPECT_TOKEN(Tokens[7], tok::star, TT_PointerOrReference);

Tokens = annotate("Foo<A && B> a = {};");
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator);

Tokens = annotate("Foo<A &&> a = {};");
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference);

Tokens = annotate("template <enable_if_t<foo && !bar>* = nullptr> void f();");
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_BinaryOperator);
}

TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Expand Down

0 comments on commit 5dc94b3

Please sign in to comment.