Skip to content

Commit d08fcc8

Browse files
committed
Revert "[clang-format] Annotate ctors/dtors as CtorDtorDeclName instead (llvm#67955)"
This reverts commit 6a621ed as it caused buildbots to fail.
1 parent 5fc28e7 commit d08fcc8

File tree

6 files changed

+43
-56
lines changed

6 files changed

+43
-56
lines changed

clang/lib/Format/FormatToken.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ namespace format {
6161
TYPE(CSharpStringLiteral) \
6262
TYPE(CtorInitializerColon) \
6363
TYPE(CtorInitializerComma) \
64-
TYPE(CtorDtorDeclName) \
6564
TYPE(DesignatedInitializerLSquare) \
6665
TYPE(DesignatedInitializerPeriod) \
6766
TYPE(DictLiteral) \

clang/lib/Format/TokenAnnotator.cpp

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

32123212
void TokenAnnotator::annotate(AnnotatedLine &Line) {
3213+
for (auto &Child : Line.Children)
3214+
annotate(*Child);
3215+
32133216
AnnotatingParser Parser(Style, Line, Keywords, Scopes);
32143217
Line.Type = Parser.parseLine();
32153218

@@ -3230,7 +3233,7 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
32303233
auto *Tok = getFunctionName(Line);
32313234
if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
32323235
Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
3233-
Tok->setFinalizedType(TT_CtorDtorDeclName);
3236+
Tok->setFinalizedType(TT_FunctionDeclarationName);
32343237
}
32353238
}
32363239

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

32443247
Line.First->SpacesRequiredBefore = 1;
32453248
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,13 +3447,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
34473447
Tok = Tok->Next) {
34483448
if (Tok->Previous->EndsCppAttributeGroup)
34493449
AfterLastAttribute = Tok;
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-
}
3450+
if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
3451+
LineIsFunctionDeclaration = true;
3452+
Tok->setFinalizedType(TT_FunctionDeclarationName);
34573453
if (AfterLastAttribute &&
34583454
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
34593455
AfterLastAttribute->MustBreakBefore = true;

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,11 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
975975
AlignTokens(
976976
Style,
977977
[](Change const &C) {
978-
if (C.Tok->isOneOf(TT_FunctionDeclarationName, TT_FunctionTypeLParen))
978+
if (C.Tok->is(TT_FunctionDeclarationName) && C.Tok->Previous &&
979+
C.Tok->Previous->isNot(tok::tilde)) {
980+
return true;
981+
}
982+
if (C.Tok->is(TT_FunctionTypeLParen))
979983
return true;
980984
if (C.Tok->isNot(TT_StartOfName))
981985
return false;

clang/unittests/Format/FormatTest.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10622,12 +10622,6 @@ 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() {}");
1063110625
}
1063210626

1063310627
TEST_F(FormatTest, UnderstandsTemplateParameters) {
@@ -16345,7 +16339,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1634516339

1634616340
verifyFormat("int f();", SpaceFuncDef);
1634716341
verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
16348-
verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
16342+
verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
1634916343
verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
1635016344
verifyFormat("#define A(x) x", SpaceFuncDef);
1635116345
verifyFormat("#define A (x) x", SpaceFuncDef);
@@ -16370,7 +16364,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1637016364
// verifyFormat("T A::operator() () {}", SpaceFuncDef);
1637116365
verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
1637216366
verifyFormat("int x = int(y);", SpaceFuncDef);
16373-
verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
16367+
verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
1637416368
SpaceFuncDef);
1637516369

1637616370
FormatStyle SpaceIfMacros = getLLVMStyle();

clang/unittests/Format/FormatTestMacroExpansion.cpp

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

5256
verifyFormat("ID(CALL(CALL(return a * b;)));", Style);
5357

@@ -247,7 +251,9 @@ TEST_F(FormatTestMacroExpansion,
247251
ContinueFormattingAfterUnclosedParensAfterObjectLikeMacro) {
248252
FormatStyle Style = getLLVMStyle();
249253
Style.Macros.push_back("O=class {");
250-
verifyIncompleteFormat("O(auto x = [](){f();}", Style);
254+
verifyIncompleteFormat("O(auto x = [](){\n"
255+
" f();}",
256+
Style);
251257
}
252258

253259
} // namespace

clang/unittests/Format/TokenAnnotatorTest.cpp

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

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(); };");
1634+
Tokens = annotate("class Foo { public: Foo(); };");
16491635
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1650-
EXPECT_TOKEN(Tokens[5], tok::identifier, TT_CtorDtorDeclName);
1636+
EXPECT_TOKEN(Tokens[5], tok::identifier, TT_FunctionDeclarationName);
16511637

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

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

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

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

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

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

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

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

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

1696-
Tokens = annotate("struct Test {\n"
1697-
" Test()\n"
1698-
" : l([] {\n"
1699-
" Short::foo();\n"
1700-
" }) {}\n"
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"
17011689
"};");
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);
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);
17051693
}
17061694

17071695
TEST_F(TokenAnnotatorTest, UnderstandsC11GenericSelection) {

0 commit comments

Comments
 (0)