Skip to content

Commit

Permalink
[clang-format] Fix a bug in annotating angles containing FatArrow
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca committed Sep 14, 2024
1 parent 9469836 commit 90a2015
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ class AnnotatingParser {
next();
}

for (bool SeenTernaryOperator = false; CurrentToken;) {
for (bool SeenTernaryOperator = false, SeenFatArrow = false;
CurrentToken;) {
const bool InExpr = Contexts[Contexts.size() - 2].IsExpression;
if (CurrentToken->is(tok::greater)) {
const auto *Next = CurrentToken->Next;
Expand Down Expand Up @@ -243,14 +244,16 @@ class AnnotatingParser {
// operator that was misinterpreted because we are parsing template
// parameters.
// FIXME: This is getting out of hand, write a decent parser.
if (InExpr && !Line.startsWith(tok::kw_template) &&
if (InExpr && !SeenFatArrow && !Line.startsWith(tok::kw_template) &&
Prev.is(TT_BinaryOperator)) {
const auto Precedence = Prev.getPrecedence();
if (Precedence > prec::Conditional && Precedence < prec::Relational)
return false;
}
if (Prev.isOneOf(tok::question, tok::colon) && !Style.isProto())
SeenTernaryOperator = true;
else if (Prev.is(TT_FatArrow))
SeenFatArrow = true;
updateParameterCount(Left, CurrentToken);
if (Style.Language == FormatStyle::LK_Proto) {
if (FormatToken *Previous = CurrentToken->getPreviousNonComment()) {
Expand All @@ -260,8 +263,7 @@ class AnnotatingParser {
Previous->setType(TT_SelectorName);
}
}
}
if (Style.isTableGen()) {
} else if (Style.isTableGen()) {
if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
// They appear as separators. Unless they are not in class definition.
next();
Expand Down
9 changes: 9 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsTernaryInTemplate) {
EXPECT_TOKEN(Tokens[8], tok::greater, TT_TemplateCloser);
}

TEST_F(TokenAnnotatorTest, FatArrowInAngleBrackets) {
auto Tokens = annotate("foo = new Bar<(id: int) => X | Y>();",
getLLVMStyle(FormatStyle::LK_JavaScript));
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
EXPECT_TOKEN(Tokens[4], tok::less, TT_TemplateOpener);
EXPECT_TOKEN(Tokens[10], tok::equal, TT_FatArrow);
EXPECT_TOKEN(Tokens[14], tok::greater, TT_TemplateCloser);
}

TEST_F(TokenAnnotatorTest, UnderstandsNonTemplateAngleBrackets) {
auto Tokens = annotate("return a < b && c > d;");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
Expand Down

0 comments on commit 90a2015

Please sign in to comment.