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

(cherry picked from commit ef71383)
  • Loading branch information
jackhong12 authored and tru committed Sep 8, 2022
1 parent 4c6350f commit 0595790
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 @@ -2329,25 +2329,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 @@ -86,6 +86,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 0595790

Please sign in to comment.