Skip to content

Commit 3ceccbd

Browse files
owencatstellar
authored andcommitted
[clang-format] Correctly annotate braces of empty ctors/dtors (#82097)
Also reformat Polly. Fixes #79834. (cherry picked from commit 8de2300)
1 parent 429d628 commit 3ceccbd

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

clang/lib/Format/TokenAnnotator.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -3450,10 +3450,11 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
34503450
for (AnnotatedLine *ChildLine : Line.Children)
34513451
calculateFormattingInformation(*ChildLine);
34523452

3453-
Line.First->TotalLength =
3454-
Line.First->IsMultiline ? Style.ColumnLimit
3455-
: Line.FirstStartColumn + Line.First->ColumnWidth;
3456-
FormatToken *Current = Line.First->Next;
3453+
auto *First = Line.First;
3454+
First->TotalLength = First->IsMultiline
3455+
? Style.ColumnLimit
3456+
: Line.FirstStartColumn + First->ColumnWidth;
3457+
FormatToken *Current = First->Next;
34573458
bool InFunctionDecl = Line.MightBeFunctionDecl;
34583459
bool AlignArrayOfStructures =
34593460
(Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
@@ -3475,16 +3476,15 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
34753476
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
34763477
IsCtorOrDtor ||
34773478
isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
3478-
if (!IsCtorOrDtor) {
3479-
LineIsFunctionDeclaration = true;
3479+
if (!IsCtorOrDtor)
34803480
Tok->setFinalizedType(TT_FunctionDeclarationName);
3481-
}
3481+
LineIsFunctionDeclaration = true;
34823482
SeenName = true;
34833483
break;
34843484
}
34853485
}
34863486

3487-
if (IsCpp && LineIsFunctionDeclaration &&
3487+
if (IsCpp && (LineIsFunctionDeclaration || First->is(TT_CtorDtorDeclName)) &&
34883488
Line.endsWith(tok::semi, tok::r_brace)) {
34893489
auto *Tok = Line.Last->Previous;
34903490
while (Tok->isNot(tok::r_brace))
@@ -3507,7 +3507,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
35073507
if (IsCpp) {
35083508
if (!LineIsFunctionDeclaration) {
35093509
// Annotate */&/&& in `operator` function calls as binary operators.
3510-
for (const auto *Tok = Line.First; Tok; Tok = Tok->Next) {
3510+
for (const auto *Tok = First; Tok; Tok = Tok->Next) {
35113511
if (Tok->isNot(tok::kw_operator))
35123512
continue;
35133513
do {
@@ -3644,7 +3644,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
36443644

36453645
calculateUnbreakableTailLengths(Line);
36463646
unsigned IndentLevel = Line.Level;
3647-
for (Current = Line.First; Current; Current = Current->Next) {
3647+
for (Current = First; Current; Current = Current->Next) {
36483648
if (Current->Role)
36493649
Current->Role->precomputeFormattingInfos(Current);
36503650
if (Current->MatchingParen &&

clang/unittests/Format/TokenAnnotatorTest.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,20 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
25952595
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
25962596
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
25972597
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
2598+
2599+
Tokens = annotate("struct Foo {\n"
2600+
" Foo() {};\n"
2601+
" ~Foo() {};\n"
2602+
"};");
2603+
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
2604+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
2605+
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
2606+
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
2607+
EXPECT_BRACE_KIND(Tokens[7], BK_Block);
2608+
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName);
2609+
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
2610+
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
2611+
EXPECT_BRACE_KIND(Tokens[14], BK_Block);
25982612
}
25992613

26002614
TEST_F(TokenAnnotatorTest, StreamOperator) {

polly/lib/Exchange/JSONExporter.cpp

+1-1
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

+1-1
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

+1-1
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

+1-1
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)