From 5dc94b3356bd861d304e1ab61f7bb9ef33bf46b3 Mon Sep 17 00:00:00 2001 From: Emilia Dreamer Date: Wed, 12 Apr 2023 00:33:12 +0300 Subject: [PATCH] [clang-format] Don't modify template arguments on the LHS of assignment 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 https://github.com/llvm/llvm-project/issues/62055 Reviewed By: owenpan, MyDeveloperDay, HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D148024 --- clang/lib/Format/TokenAnnotator.cpp | 2 +- clang/unittests/Format/TokenAnnotatorTest.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index e7de8fd..18fccca 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1819,7 +1819,7 @@ private: 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; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 0fc6442..facd006 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -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 = {};"); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator); + + Tokens = annotate("Foo a = {};"); + ASSERT_EQ(Tokens.size(), 11u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_PointerOrReference); + + Tokens = annotate("template * = nullptr> void f();"); + ASSERT_EQ(Tokens.size(), 19u) << Tokens; + EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_BinaryOperator); } TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) { -- 2.7.4