Skip to content

release/18.x: [clang-format] Correctly annotate braces of empty ctors/dtors (#82097) #87735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3450,10 +3450,11 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
for (AnnotatedLine *ChildLine : Line.Children)
calculateFormattingInformation(*ChildLine);

Line.First->TotalLength =
Line.First->IsMultiline ? Style.ColumnLimit
: Line.FirstStartColumn + Line.First->ColumnWidth;
FormatToken *Current = Line.First->Next;
auto *First = Line.First;
First->TotalLength = First->IsMultiline
? Style.ColumnLimit
: Line.FirstStartColumn + First->ColumnWidth;
FormatToken *Current = First->Next;
bool InFunctionDecl = Line.MightBeFunctionDecl;
bool AlignArrayOfStructures =
(Style.AlignArrayOfStructures != FormatStyle::AIAS_None &&
Expand All @@ -3475,16 +3476,15 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
if (const bool IsCtorOrDtor = Tok->is(TT_CtorDtorDeclName);
IsCtorOrDtor ||
isFunctionDeclarationName(Style.isCpp(), *Tok, Line, ClosingParen)) {
if (!IsCtorOrDtor) {
LineIsFunctionDeclaration = true;
if (!IsCtorOrDtor)
Tok->setFinalizedType(TT_FunctionDeclarationName);
}
LineIsFunctionDeclaration = true;
SeenName = true;
break;
}
}

if (IsCpp && LineIsFunctionDeclaration &&
if (IsCpp && (LineIsFunctionDeclaration || First->is(TT_CtorDtorDeclName)) &&
Line.endsWith(tok::semi, tok::r_brace)) {
auto *Tok = Line.Last->Previous;
while (Tok->isNot(tok::r_brace))
Expand All @@ -3507,7 +3507,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
if (IsCpp) {
if (!LineIsFunctionDeclaration) {
// Annotate */&/&& in `operator` function calls as binary operators.
for (const auto *Tok = Line.First; Tok; Tok = Tok->Next) {
for (const auto *Tok = First; Tok; Tok = Tok->Next) {
if (Tok->isNot(tok::kw_operator))
continue;
do {
Expand Down Expand Up @@ -3644,7 +3644,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {

calculateUnbreakableTailLengths(Line);
unsigned IndentLevel = Line.Level;
for (Current = Line.First; Current; Current = Current->Next) {
for (Current = First; Current; Current = Current->Next) {
if (Current->Role)
Current->Role->precomputeFormattingInfos(Current);
if (Current->MatchingParen &&
Expand Down
14 changes: 14 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2595,6 +2595,20 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[4], BK_Block);
EXPECT_BRACE_KIND(Tokens[6], BK_Block);

Tokens = annotate("struct Foo {\n"
" Foo() {};\n"
" ~Foo() {};\n"
"};");
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_CtorDtorDeclName);
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[6], BK_Block);
EXPECT_BRACE_KIND(Tokens[7], BK_Block);
EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName);
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
EXPECT_BRACE_KIND(Tokens[13], BK_Block);
EXPECT_BRACE_KIND(Tokens[14], BK_Block);
}

TEST_F(TokenAnnotatorTest, StreamOperator) {
Expand Down
2 changes: 1 addition & 1 deletion polly/lib/Exchange/JSONExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ class JSONImporterPrinterLegacyPass final : public ScopPass {
public:
static char ID;

JSONImporterPrinterLegacyPass() : JSONImporterPrinterLegacyPass(outs()){};
JSONImporterPrinterLegacyPass() : JSONImporterPrinterLegacyPass(outs()) {}
explicit JSONImporterPrinterLegacyPass(llvm::raw_ostream &OS)
: ScopPass(ID), OS(OS) {}

Expand Down
2 changes: 1 addition & 1 deletion polly/lib/Transform/DeLICM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ class DeLICMPrinterLegacyPass final : public ScopPass {
public:
static char ID;

DeLICMPrinterLegacyPass() : DeLICMPrinterLegacyPass(outs()){};
DeLICMPrinterLegacyPass() : DeLICMPrinterLegacyPass(outs()) {}
explicit DeLICMPrinterLegacyPass(llvm::raw_ostream &OS)
: ScopPass(ID), OS(OS) {}

Expand Down
2 changes: 1 addition & 1 deletion polly/lib/Transform/FlattenSchedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class FlattenSchedulePrinterLegacyPass final : public ScopPass {
static char ID;

FlattenSchedulePrinterLegacyPass()
: FlattenSchedulePrinterLegacyPass(outs()){};
: FlattenSchedulePrinterLegacyPass(outs()) {}
explicit FlattenSchedulePrinterLegacyPass(llvm::raw_ostream &OS)
: ScopPass(ID), OS(OS) {}

Expand Down
2 changes: 1 addition & 1 deletion polly/lib/Transform/ForwardOpTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ class ForwardOpTreePrinterLegacyPass final : public ScopPass {
public:
static char ID;

ForwardOpTreePrinterLegacyPass() : ForwardOpTreePrinterLegacyPass(outs()){};
ForwardOpTreePrinterLegacyPass() : ForwardOpTreePrinterLegacyPass(outs()) {}
explicit ForwardOpTreePrinterLegacyPass(llvm::raw_ostream &OS)
: ScopPass(ID), OS(OS) {}

Expand Down
Loading