Author: Tobias Hieta Date: 2022-11-07T08:34:57+01:00 New Revision: 70de684d44135b4025d92b2b36ad387cf5ab8b5a
URL: https://github.com/llvm/llvm-project/commit/70de684d44135b4025d92b2b36ad387cf5ab8b5a DIFF: https://github.com/llvm/llvm-project/commit/70de684d44135b4025d92b2b36ad387cf5ab8b5a.diff LOG: [clang-format] Handle object instansiation in if-statements Before this patch code like this: ``` if (Class* obj{getObject()}) { } ``` would be mis-formated since the * would be annotated as a binaryoperator. This patch changes the * to become a PointerOrReference instead and fixes the formatting issues. Reviewed By: HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D137327 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 3d76cc171b0dc..dbfe88c531322 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -362,7 +362,8 @@ class AnnotatingParser { FormatToken *Next = CurrentToken->Next; if (PrevPrev && PrevPrev->is(tok::identifier) && Prev->isOneOf(tok::star, tok::amp, tok::ampamp) && - CurrentToken->is(tok::identifier) && Next->isNot(tok::equal)) { + CurrentToken->is(tok::identifier) && + !Next->isOneOf(tok::equal, tok::l_brace)) { Prev->setType(TT_BinaryOperator); LookForDecls = false; } @@ -2387,6 +2388,12 @@ class AnnotatingParser { return TT_PointerOrReference; } + // if (Class* obj { function() }) + if (PrevToken->Tok.isAnyIdentifier() && NextToken->Tok.isAnyIdentifier() && + NextToken->Next && NextToken->Next->is(tok::l_brace)) { + return TT_PointerOrReference; + } + if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete)) return TT_UnaryOperator; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index fa95f6845f077..65ecb12c46cd7 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -145,6 +145,18 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionTypeLParen); EXPECT_TOKEN(Tokens[7], tok::star, TT_UnaryOperator); EXPECT_TOKEN(Tokens[12], tok::star, TT_PointerOrReference); + + Tokens = annotate("if (Foo * Bar / Test)"); + ASSERT_EQ(Tokens.size(), 9u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::star, TT_BinaryOperator); + + Tokens = annotate("if (Class* obj {getObj()})"); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference); + + Tokens = annotate("if (Foo* Bar = getObj())"); + ASSERT_EQ(Tokens.size(), 11u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference); } TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits