Skip to content

Commit 15d3707

Browse files
committed
[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead
After annotating constructors/destructors as FunctionDeclarationName in commit 0863051, we have seen several issues because ctors/dtors had been treated differently than functions in aligning, wrapping, and indenting. This patch annotates ctors/dtors as CtorDtorDeclName instead and would effectively revert commit 0468fa0, which is obsolete now. Fixed #67903. Fixed #67907.
1 parent 451255b commit 15d3707

File tree

6 files changed

+56
-41
lines changed

6 files changed

+56
-41
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace format {
6161
TYPE(CSharpStringLiteral) \
6262
TYPE(CtorInitializerColon) \
6363
TYPE(CtorInitializerComma) \
64+
TYPE(CtorDtorDeclName) \
6465
TYPE(DesignatedInitializerLSquare) \
6566
TYPE(DesignatedInitializerPeriod) \
6667
TYPE(DictLiteral) \

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,9 +3210,6 @@ static bool isCtorOrDtorName(const FormatToken *Tok) {
32103210
}
32113211

32123212
void TokenAnnotator::annotate(AnnotatedLine &Line) {
3213-
for (auto &Child : Line.Children)
3214-
annotate(*Child);
3215-
32163213
AnnotatingParser Parser(Style, Line, Keywords, Scopes);
32173214
Line.Type = Parser.parseLine();
32183215

@@ -3233,7 +3230,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
32333230
auto *Tok = getFunctionName(Line);
32343231
if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
32353232
Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
3236-
Tok->setFinalizedType(TT_FunctionDeclarationName);
3233+
Tok->setFinalizedType(TT_CtorDtorDeclName);
32373234
}
32383235
}
32393236

@@ -3246,6 +3243,9 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
32463243

32473244
Line.First->SpacesRequiredBefore = 1;
32483245
Line.First->CanBreakBefore = Line.First->MustBreakBefore;
3246+
3247+
for (auto &Child : Line.Children)
3248+
annotate(*Child);
32493249
}
32503250

32513251
// This function heuristically determines whether 'Current' starts the name of a
@@ -3447,9 +3447,13 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
34473447
Tok = Tok->Next) {
34483448
if (Tok->Previous->EndsCppAttributeGroup)
34493449
AfterLastAttribute = Tok;
3450-
if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
3451-
LineIsFunctionDeclaration = true;
3452-
Tok->setFinalizedType(TT_FunctionDeclarationName);
3450+
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
3451+
IsCtorOrDtor ||
3452+
isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
3453+
if (!IsCtorOrDtor) {
3454+
LineIsFunctionDeclaration = true;
3455+
Tok->setFinalizedType(TT_FunctionDeclarationName);
3456+
}
34533457
if (AfterLastAttribute &&
34543458
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
34553459
AfterLastAttribute->MustBreakBefore = true;

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -974,11 +974,7 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
974974
AlignTokens(
975975
Style,
976976
[](Change const &C) {
977-
if (C.Tok->is(TT_FunctionDeclarationName) && C.Tok->Previous &&
978-
C.Tok->Previous->isNot(tok::tilde)) {
979-
return true;
980-
}
981-
if (C.Tok->is(TT_FunctionTypeLParen))
977+
if (C.Tok->isOneOf(TT_FunctionDeclarationName, TT_FunctionTypeLParen))
982978
return true;
983979
if (C.Tok->isNot(TT_StartOfName))
984980
return false;

clang/unittests/Format/FormatTest.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10622,6 +10622,12 @@ TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
1062210622
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
1062310623
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1062410624
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
10625+
10626+
verifyFormat(
10627+
"LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
10628+
" AndAnotherLongClassNameToShowTheIssue() {}\n"
10629+
"LongClassNameToShowTheIssue::AndAnotherLongClassNameToShowTheIssue::\n"
10630+
" ~AndAnotherLongClassNameToShowTheIssue() {}");
1062510631
}
1062610632

1062710633
TEST_F(FormatTest, UnderstandsTemplateParameters) {
@@ -16339,7 +16345,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1633916345

1634016346
verifyFormat("int f();", SpaceFuncDef);
1634116347
verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
16342-
verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
16348+
verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
1634316349
verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
1634416350
verifyFormat("#define A(x) x", SpaceFuncDef);
1634516351
verifyFormat("#define A (x) x", SpaceFuncDef);
@@ -16364,7 +16370,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1636416370
// verifyFormat("T A::operator() () {}", SpaceFuncDef);
1636516371
verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
1636616372
verifyFormat("int x = int(y);", SpaceFuncDef);
16367-
verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
16373+
verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
1636816374
SpaceFuncDef);
1636916375

1637016376
FormatStyle SpaceIfMacros = getLLVMStyle();

clang/unittests/Format/FormatTestMacroExpansion.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ TEST_F(FormatTestMacroExpansion, UnexpandConfiguredMacros) {
4747
{ ID(a *b); });
4848
)",
4949
Style);
50-
verifyIncompleteFormat(R"(ID3({, ID(a *b),
51-
;
52-
});
50+
verifyIncompleteFormat(R"(ID3({, ID(a *b), ; });
5351
)",
5452
Style);
5553

@@ -251,9 +249,7 @@ TEST_F(FormatTestMacroExpansion,
251249
ContinueFormattingAfterUnclosedParensAfterObjectLikeMacro) {
252250
FormatStyle Style = getLLVMStyle();
253251
Style.Macros.push_back("O=class {");
254-
verifyIncompleteFormat("O(auto x = [](){\n"
255-
" f();}",
256-
Style);
252+
verifyIncompleteFormat("O(auto x = [](){f();}", Style);
257253
}
258254

259255
} // namespace

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,65 +1631,77 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
16311631
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
16321632
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
16331633

1634-
Tokens = annotate("class Foo { public: Foo(); };");
1634+
Tokens = annotate("#define FOO Foo::\n"
1635+
"FOO Foo();");
1636+
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1637+
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
1638+
1639+
Tokens = annotate("struct Foo {\n"
1640+
" Bar (*func)();\n"
1641+
"};");
1642+
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
1643+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
1644+
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);
1645+
}
1646+
1647+
TEST_F(TokenAnnotatorTest, UnderstandsCtorAndDtorDeclNames) {
1648+
auto Tokens = annotate("class Foo { public: Foo(); };");
16351649
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1636-
EXPECT_TOKEN(Tokens[5], tok::identifier, TT_FunctionDeclarationName);
1650+
EXPECT_TOKEN(Tokens[5], tok::identifier, TT_CtorDtorDeclName);
16371651

16381652
Tokens = annotate("class Foo { public: ~Foo(); };");
16391653
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
1640-
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
1654+
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_CtorDtorDeclName);
16411655

16421656
Tokens = annotate("struct Foo { [[deprecated]] Foo() {} };");
16431657
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1644-
EXPECT_TOKEN(Tokens[8], tok::identifier, TT_FunctionDeclarationName);
1658+
EXPECT_TOKEN(Tokens[8], tok::identifier, TT_CtorDtorDeclName);
16451659
EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_FunctionLBrace);
16461660

16471661
Tokens = annotate("struct Foo { [[deprecated]] ~Foo() {} };");
16481662
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1649-
EXPECT_TOKEN(Tokens[9], tok::identifier, TT_FunctionDeclarationName);
1663+
EXPECT_TOKEN(Tokens[9], tok::identifier, TT_CtorDtorDeclName);
16501664
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
16511665

16521666
Tokens = annotate("struct Foo { Foo() [[deprecated]] {} };");
16531667
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1654-
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_FunctionDeclarationName);
1668+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
16551669
EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_FunctionLBrace);
16561670

16571671
Tokens = annotate("struct Foo { ~Foo() [[deprecated]] {} };");
16581672
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1659-
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_FunctionDeclarationName);
1673+
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_CtorDtorDeclName);
16601674
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
16611675

16621676
Tokens = annotate("struct Foo { [[deprecated]] explicit Foo() {} };");
16631677
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1664-
EXPECT_TOKEN(Tokens[9], tok::identifier, TT_FunctionDeclarationName);
1678+
EXPECT_TOKEN(Tokens[9], tok::identifier, TT_CtorDtorDeclName);
16651679
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
16661680

16671681
Tokens = annotate("struct Foo { virtual [[deprecated]] ~Foo() {} };");
16681682
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
1669-
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_FunctionDeclarationName);
1683+
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName);
16701684
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
16711685

16721686
Tokens = annotate("Foo::Foo() {}");
16731687
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
1674-
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_FunctionDeclarationName);
1688+
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_CtorDtorDeclName);
16751689
EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_FunctionLBrace);
16761690

16771691
Tokens = annotate("Foo::~Foo() {}");
16781692
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
1679-
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_FunctionDeclarationName);
1693+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
16801694
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
16811695

1682-
Tokens = annotate("#define FOO Foo::\n"
1683-
"FOO Foo();");
1684-
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
1685-
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
1686-
1687-
Tokens = annotate("struct Foo {\n"
1688-
" Bar (*func)();\n"
1696+
Tokens = annotate("struct Test {\n"
1697+
" Test()\n"
1698+
" : l([] {\n"
1699+
" Short::foo();\n"
1700+
" }) {}\n"
16891701
"};");
1690-
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
1691-
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
1692-
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);
1702+
ASSERT_EQ(Tokens.size(), 25u) << Tokens;
1703+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
1704+
EXPECT_TOKEN(Tokens[14], tok::identifier, TT_Unknown);
16931705
}
16941706

16951707
TEST_F(TokenAnnotatorTest, UnderstandsC11GenericSelection) {

0 commit comments

Comments
 (0)