Skip to content

Commit

Permalink
[clang-format] Distinguish logical and after bracket from reference
Browse files Browse the repository at this point in the history
Fix commit `b646f0955574` and remove redundant code.

Differential Revision: https://reviews.llvm.org/D131750
  • Loading branch information
jackhong12 committed Aug 13, 2022
1 parent b12aa49 commit ef71383
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 19 deletions.
19 changes: 0 additions & 19 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2365,25 +2365,6 @@ class AnnotatingParser {
!PrevToken->MatchingParen)
return TT_PointerOrReference;

// For "} &&"
if (PrevToken->is(tok::r_brace) && Tok.is(tok::ampamp)) {
const FormatToken *MatchingLBrace = PrevToken->MatchingParen;

// We check whether there is a TemplateCloser(">") to indicate it's a
// template or not. If it's not a template, "&&" is likely a reference
// operator.
// struct {} &&ref = {};
if (!MatchingLBrace)
return TT_PointerOrReference;
FormatToken *BeforeLBrace = MatchingLBrace->getPreviousNonComment();
if (!BeforeLBrace || BeforeLBrace->isNot(TT_TemplateCloser))
return TT_PointerOrReference;

// If it is a template, "&&" is a binary operator.
// enable_if<>{} && ...
return TT_BinaryOperator;
}

if (PrevToken->Tok.isLiteral() ||
PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
tok::kw_false, tok::r_brace)) {
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10474,6 +10474,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
verifyFormat("class {\n"
"}&& ptr = {};",
Style);
verifyFormat("bool b = 3 == int{3} && true;");

Style.PointerAlignment = FormatStyle::PAS_Middle;
verifyFormat("struct {\n"
Expand Down
4 changes: 4 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
EXPECT_EQ(Tokens.size(), 5u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::amp, TT_UnaryOperator);

Tokens = annotate("bool b = 3 == int{3} && true;\n");
EXPECT_EQ(Tokens.size(), 13u) << Tokens;
EXPECT_TOKEN(Tokens[9], tok::ampamp, TT_BinaryOperator);

Tokens = annotate("struct {\n"
"} *ptr;");
EXPECT_EQ(Tokens.size(), 7u) << Tokens;
Expand Down

0 comments on commit ef71383

Please sign in to comment.