Skip to content

Commit 8de2300

Browse files
authored
[clang-format] Correctly annotate braces of empty ctors/dtors (#82097)
Also reformat Polly. Fixes #79834.
1 parent 44a46a0 commit 8de2300

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,10 +3754,11 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
37543754
for (AnnotatedLine *ChildLine : Line.Children)
37553755
calculateFormattingInformation(*ChildLine);
37563756

3757-
Line.First->TotalLength =
3758-
Line.First->IsMultiline ? Style.ColumnLimit
3759-
: Line.FirstStartColumn + Line.First->ColumnWidth;
3760-
FormatToken *Current = Line.First->Next;
3757+
auto *First = Line.First;
3758+
First->TotalLength = First->IsMultiline
3759+
? Style.ColumnLimit
3760+
: Line.FirstStartColumn + First->ColumnWidth;
3761+
FormatToken *Current = First->Next;
37613762
bool InFunctionDecl = Line.MightBeFunctionDecl;
37623763
bool AlignArrayOfStructures =
37633764
(Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
@@ -3779,16 +3780,15 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
37793780
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
37803781
IsCtorOrDtor ||
37813782
isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
3782-
if (!IsCtorOrDtor) {
3783-
LineIsFunctionDeclaration = true;
3783+
if (!IsCtorOrDtor)
37843784
Tok->setFinalizedType(TT_FunctionDeclarationName);
3785-
}
3785+
LineIsFunctionDeclaration = true;
37863786
SeenName = true;
37873787
break;
37883788
}
37893789
}
37903790

3791-
if (IsCpp && LineIsFunctionDeclaration &&
3791+
if (IsCpp && (LineIsFunctionDeclaration || First->is(TT_CtorDtorDeclName)) &&
37923792
Line.endsWith(tok::semi, tok::r_brace)) {
37933793
auto *Tok = Line.Last->Previous;
37943794
while (Tok->isNot(tok::r_brace))
@@ -3811,7 +3811,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
38113811
if (IsCpp) {
38123812
if (!LineIsFunctionDeclaration) {
38133813
// Annotate */&/&& in `operator` function calls as binary operators.
3814-
for (const auto *Tok = Line.First; Tok; Tok = Tok->Next) {
3814+
for (const auto *Tok = First; Tok; Tok = Tok->Next) {
38153815
if (Tok->isNot(tok::kw_operator))
38163816
continue;
38173817
do {
@@ -3948,7 +3948,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
39483948

39493949
calculateUnbreakableTailLengths(Line);
39503950
unsigned IndentLevel = Line.Level;
3951-
for (Current = Line.First; Current; Current = Current->Next) {
3951+
for (Current = First; Current; Current = Current->Next) {
39523952
if (Current->Role)
39533953
Current->Role->precomputeFormattingInfos(Current);
39543954
if (Current->MatchingParen &&

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,20 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
26782678
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
26792679
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
26802680
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
2681+
2682+
Tokens = annotate("struct Foo {\n"
2683+
" Foo() {};\n"
2684+
" ~Foo() {};\n"
2685+
"};");
2686+
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
2687+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
2688+
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
2689+
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
2690+
EXPECT_BRACE_KIND(Tokens[7], BK_Block);
2691+
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName);
2692+
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
2693+
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
2694+
EXPECT_BRACE_KIND(Tokens[14], BK_Block);
26812695
}
26822696

26832697
TEST_F(TokenAnnotatorTest, StreamOperator) {

polly/lib/Exchange/JSONExporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ class JSONImporterPrinterLegacyPass final : public ScopPass {
842842
public:
843843
static char ID;
844844

845-
JSONImporterPrinterLegacyPass() : JSONImporterPrinterLegacyPass(outs()){};
845+
JSONImporterPrinterLegacyPass() : JSONImporterPrinterLegacyPass(outs()) {}
846846
explicit JSONImporterPrinterLegacyPass(llvm::raw_ostream &OS)
847847
: ScopPass(ID), OS(OS) {}
848848

polly/lib/Transform/DeLICM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ class DeLICMPrinterLegacyPass final : public ScopPass {
14631463
public:
14641464
static char ID;
14651465

1466-
DeLICMPrinterLegacyPass() : DeLICMPrinterLegacyPass(outs()){};
1466+
DeLICMPrinterLegacyPass() : DeLICMPrinterLegacyPass(outs()) {}
14671467
explicit DeLICMPrinterLegacyPass(llvm::raw_ostream &OS)
14681468
: ScopPass(ID), OS(OS) {}
14691469

polly/lib/Transform/FlattenSchedule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class FlattenSchedulePrinterLegacyPass final : public ScopPass {
103103
static char ID;
104104

105105
FlattenSchedulePrinterLegacyPass()
106-
: FlattenSchedulePrinterLegacyPass(outs()){};
106+
: FlattenSchedulePrinterLegacyPass(outs()) {}
107107
explicit FlattenSchedulePrinterLegacyPass(llvm::raw_ostream &OS)
108108
: ScopPass(ID), OS(OS) {}
109109

polly/lib/Transform/ForwardOpTree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ class ForwardOpTreePrinterLegacyPass final : public ScopPass {
11491149
public:
11501150
static char ID;
11511151

1152-
ForwardOpTreePrinterLegacyPass() : ForwardOpTreePrinterLegacyPass(outs()){};
1152+
ForwardOpTreePrinterLegacyPass() : ForwardOpTreePrinterLegacyPass(outs()) {}
11531153
explicit ForwardOpTreePrinterLegacyPass(llvm::raw_ostream &OS)
11541154
: ScopPass(ID), OS(OS) {}
11551155

0 commit comments

Comments
 (0)