[clang] 00ca105 - [clang-format] Fixing erroneous trailing comma (#205631)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 26 11:56:13 PDT 2026
Author: Barry Revzin
Date: 2026-06-26T20:56:08+02:00
New Revision: 00ca10569f366f59a6a6f52fdd2f0ce8b9128a01
URL: https://github.com/llvm/llvm-project/commit/00ca10569f366f59a6a6f52fdd2f0ce8b9128a01
DIFF: https://github.com/llvm/llvm-project/commit/00ca10569f366f59a6a6f52fdd2f0ce8b9128a01.diff
LOG: [clang-format] Fixing erroneous trailing comma (#205631)
Fixes #205571
---------
Co-authored-by: Barry Revzin <brevzin at jumptrading.com>
Co-authored-by: Björn Schäpers <bjoern at hazardy.de>
Added:
Modified:
clang/lib/Format/Format.cpp
clang/lib/Format/FormatToken.h
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 51a976783b589..ecfe5d2ce60d0 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2824,12 +2824,12 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
void editEnumTrailingComma(SmallVectorImpl<AnnotatedLine *> &Lines,
tooling::Replacements &Result) {
bool InEnumBraces = false;
- const FormatToken *BeforeRBrace = nullptr;
+ FormatToken *BeforeRBrace = nullptr;
const auto &SourceMgr = Env.getSourceManager();
for (auto *Line : Lines) {
if (!Line->Children.empty())
editEnumTrailingComma(Line->Children, Result);
- for (const auto *Token = Line->First; Token && !Token->Finalized;
+ for (auto *Token = Line->First; Token && !Token->Finalized;
Token = Token->Next) {
if (Token->isNot(TT_EnumRBrace)) {
if (Token->is(TT_EnumLBrace))
@@ -2839,8 +2839,10 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
continue;
}
InEnumBraces = false;
- if (!BeforeRBrace) // Empty braces or Line not affected.
+ if (!BeforeRBrace || BeforeRBrace->HasEnumTrailingCommaHandled) {
+ // Empty braces, or Line not affected, or already handled.
continue;
+ }
if (BeforeRBrace->is(tok::comma)) {
if (Style.EnumTrailingComma == FormatStyle::ETC_Remove)
replaceToken(*BeforeRBrace, BeforeRBrace->Next, SourceMgr, Result);
@@ -2848,6 +2850,7 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
cantFail(Result.add(tooling::Replacement(
SourceMgr, BeforeRBrace->Tok.getEndLoc(), 0, ",")));
}
+ BeforeRBrace->HasEnumTrailingCommaHandled = true;
BeforeRBrace = nullptr;
}
}
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index ed9ce435ea765..0eeb95950ea2b 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -331,9 +331,9 @@ struct FormatToken {
EndsBinaryExpression(false), PartOfMultiVariableDeclStmt(false),
ContinuesLineCommentSection(false), Finalized(false),
ClosesRequiresClause(false), EndsCppAttributeGroup(false),
- BlockKind(BK_Unknown), Decision(FD_Unformatted),
- PackingKind(PPK_Inconclusive), TypeIsFinalized(false),
- Type(TT_Unknown) {}
+ HasEnumTrailingCommaHandled(false), BlockKind(BK_Unknown),
+ Decision(FD_Unformatted), PackingKind(PPK_Inconclusive),
+ TypeIsFinalized(false), Type(TT_Unknown) {}
/// The \c Token.
Token Tok;
@@ -409,6 +409,9 @@ struct FormatToken {
/// \c true if this token ends a group of C++ attributes.
unsigned EndsCppAttributeGroup : 1;
+ /// \c true if a comma has been inserted or removed after the token.
+ unsigned HasEnumTrailingCommaHandled : 1;
+
private:
/// Contains the kind of block if this token is a brace.
unsigned BlockKind : 2;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index c42cc147cf21e..78ddb27e61e95 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25210,6 +25210,19 @@ TEST_F(FormatTest, EnumTrailingComma) {
" MY_ENUM = 0U\n"
"};",
Style);
+
+ // Issue https://github.com/llvm/llvm-project/issues/205571
+ verifyFormat("#ifdef FOO\n"
+ "#else\n"
+ "#endif\n"
+ "enum {\n"
+ " E = 1,\n"
+ "};",
+ "#ifdef FOO\n"
+ "#else\n"
+ "#endif\n"
+ "enum { E = 1 };",
+ Style);
}
TEST_F(FormatTest, BreakAfterAttributes) {
More information about the cfe-commits
mailing list