Skip to content

Commit 83b1270

Browse files
committed
Address review comments
1 parent 46cde1b commit 83b1270

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,10 @@ struct FormatToken {
746746
return isOneOf(tok::star, tok::amp, tok::ampamp);
747747
}
748748

749+
bool isPlacementOperator() const {
750+
return isOneOf(tok::kw_new, tok::kw_delete);
751+
}
752+
749753
bool isUnaryOperator() const {
750754
switch (Tok.getKind()) {
751755
case tok::plus:

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,9 @@ bool FormatTokenLexer::precedesOperand(FormatToken *Tok) {
610610
tok::r_brace, tok::l_square, tok::semi, tok::exclaim,
611611
tok::colon, tok::question, tok::tilde) ||
612612
Tok->isOneOf(tok::kw_return, tok::kw_do, tok::kw_case, tok::kw_throw,
613-
tok::kw_else, tok::kw_new, tok::kw_delete, tok::kw_void,
614-
tok::kw_typeof, Keywords.kw_instanceof, Keywords.kw_in) ||
615-
Tok->isBinaryOperator();
613+
tok::kw_else, tok::kw_void, tok::kw_typeof,
614+
Keywords.kw_instanceof, Keywords.kw_in) ||
615+
Tok->isPlacementOperator() || Tok->isBinaryOperator();
616616
}
617617

618618
bool FormatTokenLexer::canPrecedeRegexLiteral(FormatToken *Prev) {

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,23 +1639,29 @@ class AnnotatingParser {
16391639
case tok::kw_operator:
16401640
if (Style.isProto())
16411641
break;
1642-
// C++ user-defined conversion function.
1643-
if (IsCpp && CurrentToken &&
1644-
(CurrentToken->is(tok::kw_auto) ||
1645-
CurrentToken->isTypeName(LangOpts))) {
1646-
FormatToken *LParen;
1647-
if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1648-
tok::kw_auto, tok::r_paren)) {
1649-
LParen = CurrentToken->Next->Next->Next->Next;
1650-
} else {
1651-
for (LParen = CurrentToken->Next;
1652-
LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1642+
// Handle C++ user-defined conversion function.
1643+
if (IsCpp && CurrentToken) {
1644+
const auto *Info = CurrentToken->Tok.getIdentifierInfo();
1645+
// What follows Tok is an identifier or a non-operator keyword.
1646+
if (Info && !(Info->isCPlusPlusOperatorKeyword() ||
1647+
CurrentToken->isPlacementOperator() ||
1648+
CurrentToken->is(tok::kw_co_await))) {
1649+
FormatToken *LParen;
1650+
if (CurrentToken->startsSequence(tok::kw_decltype, tok::l_paren,
1651+
tok::kw_auto, tok::r_paren)) {
1652+
// Skip `decltype(auto)`.
1653+
LParen = CurrentToken->Next->Next->Next->Next;
1654+
} else {
1655+
// Skip to l_paren.
1656+
for (LParen = CurrentToken->Next;
1657+
LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
1658+
}
1659+
}
1660+
if (LParen && LParen->is(tok::l_paren)) {
1661+
Tok->setFinalizedType(TT_FunctionDeclarationName);
1662+
LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1663+
break;
16531664
}
1654-
}
1655-
if (LParen && LParen->startsSequence(tok::l_paren, tok::r_paren)) {
1656-
Tok->setFinalizedType(TT_FunctionDeclarationName);
1657-
LParen->setFinalizedType(TT_FunctionDeclarationLParen);
1658-
break;
16591665
}
16601666
}
16611667
while (CurrentToken &&
@@ -3018,7 +3024,7 @@ class AnnotatingParser {
30183024
return TT_UnaryOperator;
30193025
if (PrevToken->is(TT_TypeName))
30203026
return TT_PointerOrReference;
3021-
if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
3027+
if (PrevToken->isPlacementOperator() && Tok.is(tok::ampamp))
30223028
return TT_BinaryOperator;
30233029

30243030
const FormatToken *NextToken = Tok.getNextNonComment();
@@ -3808,7 +3814,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
38083814
return Next;
38093815
if (Next->is(TT_OverloadedOperator))
38103816
continue;
3811-
if (Next->isOneOf(tok::kw_new, tok::kw_delete, tok::kw_co_await)) {
3817+
if (Next->isPlacementOperator() || Next->is(tok::kw_co_await)) {
38123818
// For 'new[]' and 'delete[]'.
38133819
if (Next->Next &&
38143820
Next->Next->startsSequence(tok::l_square, tok::r_square)) {
@@ -4819,7 +4825,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
48194825
spaceRequiredBeforeParens(Right);
48204826
}
48214827
if (Style.SpaceBeforeParens == FormatStyle::SBPO_Custom &&
4822-
Left.isOneOf(tok::kw_new, tok::kw_delete) &&
4828+
Left.isPlacementOperator() &&
48234829
Right.isNot(TT_OverloadedOperatorLParen) &&
48244830
!(Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
48254831
const auto *RParen = Right.MatchingParen;
@@ -4862,7 +4868,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
48624868
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
48634869
spaceRequiredBeforeParens(Right);
48644870
}
4865-
if (Left.isOneOf(tok::kw_new, tok::kw_delete) ||
4871+
if (Left.isPlacementOperator() ||
48664872
(Left.is(tok::r_square) && Left.MatchingParen &&
48674873
Left.MatchingParen->Previous &&
48684874
Left.MatchingParen->Previous->is(tok::kw_delete))) {

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,8 +3857,8 @@ TEST_F(TokenAnnotatorTest, AfterPPDirective) {
38573857
}
38583858

38593859
TEST_F(TokenAnnotatorTest, UserDefinedConversionFunction) {
3860-
auto Tokens = annotate("operator int();");
3861-
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
3860+
auto Tokens = annotate("operator int(void);");
3861+
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
38623862
EXPECT_TOKEN(Tokens[0], tok::kw_operator, TT_FunctionDeclarationName);
38633863
EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
38643864

@@ -3889,21 +3889,18 @@ TEST_F(TokenAnnotatorTest, UserDefinedConversionFunction) {
38893889
EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_FunctionDeclarationLParen);
38903890
EXPECT_TOKEN(Tokens[7], tok::kw_const, TT_TrailingAnnotation);
38913891

3892-
auto Style = getLLVMStyle();
3893-
Style.TypeNames.push_back("Foo");
3894-
3895-
Tokens = annotate("virtual operator Foo() = 0;", Style);
3892+
Tokens = annotate("virtual operator Foo() = 0;");
38963893
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
38973894
EXPECT_TOKEN(Tokens[1], tok::kw_operator, TT_FunctionDeclarationName);
38983895
EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionDeclarationLParen);
38993896

3900-
Tokens = annotate("operator Foo() override { return Foo(); }", Style);
3897+
Tokens = annotate("operator Foo() override { return Foo(); }");
39013898
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
39023899
EXPECT_TOKEN(Tokens[0], tok::kw_operator, TT_FunctionDeclarationName);
39033900
EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_FunctionDeclarationLParen);
39043901
EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_FunctionLBrace);
39053902

3906-
Tokens = annotate("friend Bar::operator Foo();", Style);
3903+
Tokens = annotate("friend Bar::operator Foo();");
39073904
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
39083905
EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_FunctionDeclarationName);
39093906
EXPECT_TOKEN(Tokens[5], tok::l_paren, TT_FunctionDeclarationLParen);

0 commit comments

Comments
 (0)