thieta created this revision. thieta added reviewers: MyDeveloperDay, curdeius, owenpan. Herald added a project: All. thieta requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D137327 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1035,6 +1035,12 @@ EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown); } +TEST_F(TokenAnnotatorTest, UnderstandIfInitializer) { + auto Tokens = annotate("if (Class* obj {getObj()}) "); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference); +} + TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) { auto Annotate = [this](llvm::StringRef Code) { return annotate(Code, getLLVMStyle(FormatStyle::LK_Verilog)); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -362,7 +362,8 @@ 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; } @@ -2385,6 +2386,12 @@ 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;
Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -1035,6 +1035,12 @@ EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown); } +TEST_F(TokenAnnotatorTest, UnderstandIfInitializer) { + auto Tokens = annotate("if (Class* obj {getObj()}) "); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference); +} + TEST_F(TokenAnnotatorTest, UnderstandsVerilogOperators) { auto Annotate = [this](llvm::StringRef Code) { return annotate(Code, getLLVMStyle(FormatStyle::LK_Verilog)); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -362,7 +362,8 @@ 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; } @@ -2385,6 +2386,12 @@ 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;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits