Skip to content

Commit 7590b76

Browse files
committed
Revert "[clang-format] Annotate constructor/destructor names"
This reverts commit 0e63f1a. clang-format started to crash with contents like: a.h: ``` ``` $ clang-format a.h ``` PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace. Stack dump: 0. Program arguments: ../llvm/build/bin/clang-format a.h #0 0x0000560b689fe177 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Unix/Signals.inc:723:13 #1 0x0000560b689fbfbe llvm::sys::RunSignalHandlers() /usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Signals.cpp:106:18 #2 0x0000560b689feaca SignalHandler(int) /usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Unix/Signals.inc:413:1 #3 0x00007f030405a540 (/lib/x86_64-linux-gnu/libc.so.6+0x3c540) llvm#4 0x0000560b68a9a980 is /usr/local/google/home/kadircet/repos/llvm/clang/include/clang/Lex/Token.h:98:44 llvm#5 0x0000560b68a9a980 is /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/FormatToken.h:562:51 llvm#6 0x0000560b68a9a980 startsSequenceInternal<clang::tok::TokenKind, clang::tok::TokenKind> /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/FormatToken.h:831:9 llvm#7 0x0000560b68a9a980 startsSequence<clang::tok::TokenKind, clang::tok::TokenKind> /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/FormatToken.h:600:12 llvm#8 0x0000560b68a9a980 getFunctionName /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/TokenAnnotator.cpp:3131:17 llvm#9 0x0000560b68a9a980 clang::format::TokenAnnotator::annotate(clang::format::AnnotatedLine&) /usr/local/google/home/kadircet/repos/llvm/clang/lib/Format/TokenAnnotator.cpp:3191:17 Segmentation fault ```
1 parent 475a93a commit 7590b76

File tree

3 files changed

+19
-217
lines changed

3 files changed

+19
-217
lines changed

clang/lib/Format/TokenAnnotator.cpp

+3-85
Original file line numberDiff line numberDiff line change
@@ -3097,76 +3097,6 @@ static unsigned maxNestingDepth(const AnnotatedLine &Line) {
30973097
return Result;
30983098
}
30993099

3100-
// Returns the name of a function with no return type, e.g. a constructor or
3101-
// destructor.
3102-
static FormatToken *getFunctionName(const AnnotatedLine &Line) {
3103-
for (FormatToken *Tok = Line.getFirstNonComment(), *Name = nullptr; Tok;
3104-
Tok = Tok->getNextNonComment()) {
3105-
// Skip C++11 attributes both before and after the function name.
3106-
if (Tok->is(tok::l_square) && Tok->is(TT_AttributeSquare)) {
3107-
Tok = Tok->MatchingParen;
3108-
if (!Tok)
3109-
break;
3110-
continue;
3111-
}
3112-
3113-
// Make sure the name is followed by a pair of parentheses.
3114-
if (Name)
3115-
return Tok->is(tok::l_paren) && Tok->MatchingParen ? Name : nullptr;
3116-
3117-
// Skip keywords that may precede the constructor/destructor name.
3118-
if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
3119-
tok::kw_constexpr, tok::kw_consteval, tok::kw_explicit)) {
3120-
continue;
3121-
}
3122-
3123-
// A qualified name may start from the global namespace.
3124-
if (Tok->is(tok::coloncolon)) {
3125-
Tok = Tok->Next;
3126-
if (!Tok)
3127-
break;
3128-
}
3129-
3130-
// Skip to the unqualified part of the name.
3131-
while (Tok->startsSequence(tok::identifier, tok::coloncolon)) {
3132-
assert(Tok->Next);
3133-
Tok = Tok->Next->Next;
3134-
if (!Tok)
3135-
break;
3136-
}
3137-
3138-
// Skip the `~` if a destructor name.
3139-
if (Tok->is(tok::tilde)) {
3140-
Tok = Tok->Next;
3141-
if (!Tok)
3142-
break;
3143-
}
3144-
3145-
// Make sure the name is not already annotated, e.g. as NamespaceMacro.
3146-
if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
3147-
break;
3148-
3149-
Name = Tok;
3150-
}
3151-
3152-
return nullptr;
3153-
}
3154-
3155-
// Checks if Tok is a constructor/destructor name qualified by its class name.
3156-
static bool isCtorOrDtorName(const FormatToken *Tok) {
3157-
assert(Tok && Tok->is(tok::identifier));
3158-
const auto *Prev = Tok->Previous;
3159-
3160-
if (Prev && Prev->is(tok::tilde))
3161-
Prev = Prev->Previous;
3162-
3163-
if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier))
3164-
return false;
3165-
3166-
assert(Prev->Previous);
3167-
return Prev->Previous->TokenText == Tok->TokenText;
3168-
}
3169-
31703100
void TokenAnnotator::annotate(AnnotatedLine &Line) {
31713101
for (auto &Child : Line.Children)
31723102
annotate(*Child);
@@ -3187,14 +3117,6 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
31873117
ExpressionParser ExprParser(Style, Keywords, Line);
31883118
ExprParser.parse();
31893119

3190-
if (Style.isCpp()) {
3191-
auto *Tok = getFunctionName(Line);
3192-
if (Tok && ((!Scopes.empty() && Scopes.back() == ST_Class) ||
3193-
Line.endsWith(TT_FunctionLBrace) || isCtorOrDtorName(Tok))) {
3194-
Tok->setFinalizedType(TT_FunctionDeclarationName);
3195-
}
3196-
}
3197-
31983120
if (Line.startsWith(TT_ObjCMethodSpecifier))
31993121
Line.Type = LT_ObjCMethodDecl;
32003122
else if (Line.startsWith(TT_ObjCDecl))
@@ -3211,10 +3133,6 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
32113133
static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
32123134
const AnnotatedLine &Line) {
32133135
assert(Current.Previous);
3214-
3215-
if (Current.is(TT_FunctionDeclarationName))
3216-
return true;
3217-
32183136
if (!Current.Tok.getIdentifierInfo())
32193137
return false;
32203138

@@ -3395,18 +3313,18 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
33953313
bool LineIsFunctionDeclaration = false;
33963314
for (FormatToken *Tok = Current, *AfterLastAttribute = nullptr; Tok;
33973315
Tok = Tok->Next) {
3398-
if (Tok->Previous->EndsCppAttributeGroup)
3399-
AfterLastAttribute = Tok;
34003316
if (isFunctionDeclarationName(Style.isCpp(), *Tok, Line)) {
34013317
LineIsFunctionDeclaration = true;
3402-
Tok->setFinalizedType(TT_FunctionDeclarationName);
3318+
Tok->setType(TT_FunctionDeclarationName);
34033319
if (AfterLastAttribute &&
34043320
mustBreakAfterAttributes(*AfterLastAttribute, Style)) {
34053321
AfterLastAttribute->MustBreakBefore = true;
34063322
Line.ReturnTypeWrapped = true;
34073323
}
34083324
break;
34093325
}
3326+
if (Tok->Previous->EndsCppAttributeGroup)
3327+
AfterLastAttribute = Tok;
34103328
}
34113329

34123330
if (Style.isCpp() && !LineIsFunctionDeclaration) {

clang/unittests/Format/FormatTest.cpp

+16-84
Original file line numberDiff line numberDiff line change
@@ -16541,7 +16541,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1654116541

1654216542
verifyFormat("int f();", SpaceFuncDef);
1654316543
verifyFormat("void f (int a, T b) {}", SpaceFuncDef);
16544-
verifyFormat("A::A () : a(1) {}", SpaceFuncDef);
16544+
verifyFormat("A::A() : a(1) {}", SpaceFuncDef);
1654516545
verifyFormat("void f() __attribute__((asdf));", SpaceFuncDef);
1654616546
verifyFormat("#define A(x) x", SpaceFuncDef);
1654716547
verifyFormat("#define A (x) x", SpaceFuncDef);
@@ -16566,7 +16566,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
1656616566
// verifyFormat("T A::operator() () {}", SpaceFuncDef);
1656716567
verifyFormat("auto lambda = [] () { return 0; };", SpaceFuncDef);
1656816568
verifyFormat("int x = int(y);", SpaceFuncDef);
16569-
verifyFormat("M (std::size_t R, std::size_t C) : C(C), data(R) {}",
16569+
verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}",
1657016570
SpaceFuncDef);
1657116571

1657216572
FormatStyle SpaceIfMacros = getLLVMStyle();
@@ -26239,18 +26239,18 @@ TEST_F(FormatTest, BreakAfterAttributes) {
2623926239
FormatStyle Style = getLLVMStyle();
2624026240
EXPECT_EQ(Style.BreakAfterAttributes, FormatStyle::ABS_Never);
2624126241

26242-
constexpr StringRef Code("[[nodiscard]] inline int f(int &i);\n"
26243-
"[[foo([[]])]] [[nodiscard]]\n"
26244-
"int g(int &i);\n"
26245-
"[[nodiscard]]\n"
26246-
"inline int f(int &i) {\n"
26247-
" i = 1;\n"
26248-
" return 0;\n"
26249-
"}\n"
26250-
"[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
26251-
" i = 0;\n"
26252-
" return 1;\n"
26253-
"}");
26242+
const StringRef Code("[[nodiscard]] inline int f(int &i);\n"
26243+
"[[foo([[]])]] [[nodiscard]]\n"
26244+
"int g(int &i);\n"
26245+
"[[nodiscard]]\n"
26246+
"inline int f(int &i) {\n"
26247+
" i = 1;\n"
26248+
" return 0;\n"
26249+
"}\n"
26250+
"[[foo([[]])]] [[nodiscard]] int g(int &i) {\n"
26251+
" i = 0;\n"
26252+
" return 1;\n"
26253+
"}");
2625426254

2625526255
verifyFormat("[[nodiscard]] inline int f(int &i);\n"
2625626256
"[[foo([[]])]] [[nodiscard]] int g(int &i);\n"
@@ -26264,9 +26264,6 @@ TEST_F(FormatTest, BreakAfterAttributes) {
2626426264
"}",
2626526265
Code, Style);
2626626266

26267-
Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
26268-
verifyNoChange(Code, Style);
26269-
2627026267
Style.BreakAfterAttributes = FormatStyle::ABS_Always;
2627126268
verifyFormat("[[nodiscard]]\n"
2627226269
"inline int f(int &i);\n"
@@ -26284,73 +26281,8 @@ TEST_F(FormatTest, BreakAfterAttributes) {
2628426281
"}",
2628526282
Code, Style);
2628626283

26287-
constexpr StringRef CtorDtorCode("struct Foo {\n"
26288-
" [[deprecated]] Foo();\n"
26289-
" [[deprecated]] Foo() {}\n"
26290-
" [[deprecated]] ~Foo();\n"
26291-
" [[deprecated]] ~Foo() {}\n"
26292-
" [[deprecated]] void f();\n"
26293-
" [[deprecated]] void f() {}\n"
26294-
"};\n"
26295-
"[[deprecated]] Bar::Bar() {}\n"
26296-
"[[deprecated]] Bar::~Bar() {}\n"
26297-
"[[deprecated]] void g() {}");
26298-
verifyFormat("struct Foo {\n"
26299-
" [[deprecated]]\n"
26300-
" Foo();\n"
26301-
" [[deprecated]]\n"
26302-
" Foo() {}\n"
26303-
" [[deprecated]]\n"
26304-
" ~Foo();\n"
26305-
" [[deprecated]]\n"
26306-
" ~Foo() {}\n"
26307-
" [[deprecated]]\n"
26308-
" void f();\n"
26309-
" [[deprecated]]\n"
26310-
" void f() {}\n"
26311-
"};\n"
26312-
"[[deprecated]]\n"
26313-
"Bar::Bar() {}\n"
26314-
"[[deprecated]]\n"
26315-
"Bar::~Bar() {}\n"
26316-
"[[deprecated]]\n"
26317-
"void g() {}",
26318-
CtorDtorCode, Style);
26319-
26320-
Style.BreakBeforeBraces = FormatStyle::BS_Linux;
26321-
verifyFormat("struct Foo {\n"
26322-
" [[deprecated]]\n"
26323-
" Foo();\n"
26324-
" [[deprecated]]\n"
26325-
" Foo()\n"
26326-
" {\n"
26327-
" }\n"
26328-
" [[deprecated]]\n"
26329-
" ~Foo();\n"
26330-
" [[deprecated]]\n"
26331-
" ~Foo()\n"
26332-
" {\n"
26333-
" }\n"
26334-
" [[deprecated]]\n"
26335-
" void f();\n"
26336-
" [[deprecated]]\n"
26337-
" void f()\n"
26338-
" {\n"
26339-
" }\n"
26340-
"};\n"
26341-
"[[deprecated]]\n"
26342-
"Bar::Bar()\n"
26343-
"{\n"
26344-
"}\n"
26345-
"[[deprecated]]\n"
26346-
"Bar::~Bar()\n"
26347-
"{\n"
26348-
"}\n"
26349-
"[[deprecated]]\n"
26350-
"void g()\n"
26351-
"{\n"
26352-
"}",
26353-
CtorDtorCode, Style);
26284+
Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
26285+
verifyNoChange(Code, Style);
2635426286
}
2635526287

2635626288
TEST_F(FormatTest, InsertNewlineAtEOF) {

clang/unittests/Format/TokenAnnotatorTest.cpp

-48
Original file line numberDiff line numberDiff line change
@@ -1589,54 +1589,6 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
15891589
Tokens = annotate("void f [[noreturn]] () {}");
15901590
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
15911591
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);
1592-
1593-
Tokens = annotate("class Foo { public: Foo(); };");
1594-
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
1595-
EXPECT_TOKEN(Tokens[5], tok::identifier, TT_FunctionDeclarationName);
1596-
1597-
Tokens = annotate("class Foo { public: ~Foo(); };");
1598-
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
1599-
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
1600-
1601-
Tokens = annotate("struct Foo { [[deprecated]] Foo() {} };");
1602-
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1603-
EXPECT_TOKEN(Tokens[8], tok::identifier, TT_FunctionDeclarationName);
1604-
EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_FunctionLBrace);
1605-
1606-
Tokens = annotate("struct Foo { [[deprecated]] ~Foo() {} };");
1607-
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1608-
EXPECT_TOKEN(Tokens[9], tok::identifier, TT_FunctionDeclarationName);
1609-
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
1610-
1611-
Tokens = annotate("struct Foo { Foo() [[deprecated]] {} };");
1612-
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
1613-
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_FunctionDeclarationName);
1614-
EXPECT_TOKEN(Tokens[11], tok::l_brace, TT_FunctionLBrace);
1615-
1616-
Tokens = annotate("struct Foo { ~Foo() [[deprecated]] {} };");
1617-
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1618-
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_FunctionDeclarationName);
1619-
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
1620-
1621-
Tokens = annotate("struct Foo { [[deprecated]] explicit Foo() {} };");
1622-
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
1623-
EXPECT_TOKEN(Tokens[9], tok::identifier, TT_FunctionDeclarationName);
1624-
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_FunctionLBrace);
1625-
1626-
Tokens = annotate("struct Foo { virtual [[deprecated]] ~Foo() {} };");
1627-
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
1628-
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_FunctionDeclarationName);
1629-
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
1630-
1631-
Tokens = annotate("Foo::Foo() {}");
1632-
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
1633-
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_FunctionDeclarationName);
1634-
EXPECT_TOKEN(Tokens[5], tok::l_brace, TT_FunctionLBrace);
1635-
1636-
Tokens = annotate("Foo::~Foo() {}");
1637-
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
1638-
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_FunctionDeclarationName);
1639-
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
16401592
}
16411593

16421594
TEST_F(TokenAnnotatorTest, UnderstandsC11GenericSelection) {

0 commit comments

Comments
 (0)