Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Fix a regression on annotating template angles #132885

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
@@ -253,8 +253,7 @@ class AnnotatingParser {
// FIXME: This is getting out of hand, write a decent parser.
if (MaybeAngles && InExpr && !Line.startsWith(tok::kw_template) &&
Prev.is(TT_BinaryOperator) &&
(Prev.isOneOf(tok::pipepipe, tok::ampamp) ||
Prev.getPrecedence() == prec::Equality)) {
Prev.isOneOf(tok::pipepipe, tok::ampamp)) {
MaybeAngles = false;
}
if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
15 changes: 13 additions & 2 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
@@ -720,8 +720,9 @@ TEST_F(TokenAnnotatorTest, UnderstandsNonTemplateAngleBrackets) {

Tokens = annotate("return A < B != A > B;");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
EXPECT_TOKEN(Tokens[6], tok::greater, TT_BinaryOperator);
// FIXME:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought, could we check the token after greater? If it is an identifier it can never be a template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be a template closer e.g. in declarations with implicit instantiated template types? IMO we should rework parseAngle() instead of adding more heuristics for special cases such as A<B != C> D that must be an expression if preceded by return.

// EXPECT_TOKEN(Tokens[2], tok::less, TT_BinaryOperator);
// EXPECT_TOKEN(Tokens[6], tok::greater, TT_BinaryOperator);

Tokens = annotate("ratio{-1, 2} < ratio{-1, 3} == -1 / 3 > -1 / 2;");
ASSERT_EQ(Tokens.size(), 27u) << Tokens;
@@ -3827,6 +3828,16 @@ TEST_F(TokenAnnotatorTest, TemplateInstantiation) {
ASSERT_EQ(Tokens.size(), 24u) << Tokens;
EXPECT_TOKEN(Tokens[6], tok::less, TT_TemplateOpener);
EXPECT_TOKEN(Tokens[18], tok::greater, TT_TemplateCloser);

Tokens = annotate(
"std::uint16_t kMTU = std::conditional<\n"
" kTypeKind == KindA,\n"
" std::integral_constant<std::uint16_t, kIoSockMtu>>::type::value;");
ASSERT_EQ(Tokens.size(), 30u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::less, TT_TemplateOpener);
EXPECT_TOKEN(Tokens[16], tok::less, TT_TemplateOpener);
EXPECT_TOKEN(Tokens[22], tok::greater, TT_TemplateCloser);
EXPECT_TOKEN(Tokens[23], tok::greater, TT_TemplateCloser);
}

TEST_F(TokenAnnotatorTest, VariableTemplate) {