[clang-tools-extra] [clang-tidy] Fix readability-trailing-comma after multi-line comments (PR #241556)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 12 00:22:55 PST 2026
https://github.com/ljkx updated https://github.com/llvm/llvm-project/pull/241556
>From ac97c5b9efbcb3c140fed23c850610260c18b754 Mon Sep 17 00:00:00 2001
From: ljkx <gh at ljkx.org>
Date: Thu, 12 Nov 2026 00:22:01 +0100
Subject: [PATCH 1/2] [clang-tidy] Fix readability-trailing-comma after
multi-line comments
The check found the last token of an enum by searching backward from the
closing brace with Lexer::findPreviousToken. That restarts lexing at the
beginning of the line, so when the closing brace follows a block comment
spanning multiple lines, lexing starts inside the comment and the `/` of
`*/` was taken as the last token. This produced wrong diagnostics and
fix-its: with the Append policy a comma was inserted after the comment
even when a trailing comma was already present, and with the Remove
policy a trailing comma was not diagnosed.
Find the last token by lexing forward to the closing brace instead,
starting at the opening brace, or at the beginning of the closing brace's
file if the braces are in different files.
Fixes #237305.
Assisted-by: Claude Code
---
.../readability/TrailingCommaCheck.cpp | 35 +++++++++++++++++--
clang-tools-extra/docs/ReleaseNotes.md | 3 ++
.../Inputs/trailing-comma/split-enum.h | 2 ++
.../readability/trailing-comma-remove.cpp | 11 ++++++
.../readability/trailing-comma-split-enum.cpp | 10 ++++++
.../checkers/readability/trailing-comma.cpp | 16 +++++++++
6 files changed, 74 insertions(+), 3 deletions(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/trailing-comma/split-enum.h
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp
diff --git a/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp b/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
index c0ed9849304de9..7599341089b74e 100644
--- a/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/TrailingCommaCheck.cpp
@@ -56,6 +56,36 @@ static bool isPreprocessorDirectiveToken(const Token &Tok,
SM.getExpansionLineNumber(Tok.getLocation());
}
+// Returns the last non-comment token before the closing brace of Enum.
+// Lexer::findPreviousToken() can't be used for this: it starts lexing at the
+// beginning of a line, which may be inside a multi-line block comment.
+static std::optional<Token> findLastEnumToken(const EnumDecl *Enum,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ const auto [LBraceFile, LBraceOffset] =
+ SM.getDecomposedExpansionLoc(Enum->getBraceRange().getBegin());
+ const auto [RBraceFile, RBraceOffset] =
+ SM.getDecomposedExpansionLoc(Enum->getBraceRange().getEnd());
+ bool Invalid = false;
+ const StringRef Buffer = SM.getBufferData(RBraceFile, &Invalid);
+ if (Invalid)
+ return std::nullopt;
+ // The braces are in different files if the enum's body is split across an
+ // #include. Then lex the closing brace's file from its beginning.
+ const bool SameFile = LBraceFile == RBraceFile;
+ Lexer RawLexer(SM.getLocForStartOfFile(RBraceFile), LangOpts, Buffer.begin(),
+ Buffer.data() + (SameFile ? LBraceOffset : 0), Buffer.end());
+ Token Tok;
+ // Skip the opening brace.
+ if (SameFile && RawLexer.LexFromRawLexer(Tok))
+ return std::nullopt;
+ std::optional<Token> LastTok;
+ while (!RawLexer.LexFromRawLexer(Tok) &&
+ SM.getFileOffset(Tok.getLocation()) < RBraceOffset)
+ LastTok = Tok;
+ return LastTok;
+}
+
namespace {
AST_POLYMORPHIC_MATCHER(isMacro,
@@ -122,9 +152,8 @@ void TrailingCommaCheck::checkEnumDecl(const EnumDecl *Enum,
if (Policy == CommaPolicyKind::Ignore)
return;
- const std::optional<Token> LastTok = Lexer::findPreviousToken(
- Enum->getBraceRange().getEnd(), *Result.SourceManager, getLangOpts(),
- /*IncludeComments=*/false);
+ const std::optional<Token> LastTok =
+ findLastEnumToken(Enum, *Result.SourceManager, getLangOpts());
if (!LastTok)
return;
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index b6774370511feb..a75607df24c2f9 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -473,6 +473,9 @@ infrastructure are described first, followed by tool-specific sections.
- Fixed a false positive on empty brace initializers of types with default
member initializers.
+ - Fixed wrong diagnostics and fix-its when an enum's last enumerator is
+ followed by a block comment that spans multiple lines.
+
- Improved {doc}`readability-use-std-min-max
<clang-tidy/checks/readability/use-std-min-max>` check by fixing spurious
trailing semicolons and lost comments when the `if` body has no braces.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/trailing-comma/split-enum.h b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/trailing-comma/split-enum.h
new file mode 100644
index 00000000000000..f7e0a32f07fa65
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/Inputs/trailing-comma/split-enum.h
@@ -0,0 +1,2 @@
+enum SplitEnum {
+ SplitA,
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-remove.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-remove.cpp
index fadce1808876f7..522f376875538c 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-remove.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-remove.cpp
@@ -33,6 +33,17 @@ enum E3 {
};
enum Empty {};
+// A multi-line block comment before '}' is skipped.
+enum MultiLineComment {
+ MLC_A, /* comment that
+ spans multiple lines */
+};
+// CHECK-MESSAGES: :[[@LINE-3]]:8: warning: enum should not have a trailing comma
+// CHECK-FIXES: enum MultiLineComment {
+// CHECK-FIXES-NEXT: MLC_A /* comment that
+// CHECK-FIXES-NEXT: spans multiple lines */
+// CHECK-FIXES-NEXT: };
+
void f() {
// Multi-line init lists with trailing commas - should warn to remove
int a[] = {
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp
new file mode 100644
index 00000000000000..02c5d6a79ea86d
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma-split-enum.cpp
@@ -0,0 +1,10 @@
+// RUN: %check_clang_tidy -std=c++98-or-later %s readability-trailing-comma %t -- \
+// RUN: -- -I %S/Inputs/trailing-comma
+
+// The enum's opening brace is in the header and its closing brace is here.
+#include "split-enum.h"
+ SplitB
+};
+// CHECK-MESSAGES: :[[@LINE-2]]:9: warning: enum should have a trailing comma
+// CHECK-FIXES: SplitB,
+// CHECK-FIXES-NEXT: };
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp
index fb15931731b076..81a556a86ec995 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp
@@ -190,6 +190,22 @@ enum NullDirectiveAfter {
#
};
+// A multi-line block comment before '}' is skipped.
+enum MultiLineCommentAfterComma {
+ MLC_A, /* comment that
+ spans multiple lines */
+};
+
+enum MultiLineCommentNoComma {
+ MLC_B /* comment that
+ spans multiple lines */
+};
+// CHECK-MESSAGES: :[[@LINE-3]]:8: warning: enum should have a trailing comma
+// CHECK-FIXES: enum MultiLineCommentNoComma {
+// CHECK-FIXES-NEXT: MLC_B, /* comment that
+// CHECK-FIXES-NEXT: spans multiple lines */
+// CHECK-FIXES-NEXT: };
+
// Macros are ignored
#define ENUM(n, a, b) enum n { a, b }
#define INIT {1, 2}
>From 0ed8801b561c552bb7e14f28cfb1964d7b741cdb Mon Sep 17 00:00:00 2001
From: ljkx <gh at ljkx.org>
Date: Thu, 12 Nov 2026 09:22:44 +0100
Subject: [PATCH 2/2] Add tests for enums that use macros
Assisted-by: Claude Code
---
.../checkers/readability/trailing-comma.cpp | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp
index 81a556a86ec995..242396f0ec3696 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/trailing-comma.cpp
@@ -206,6 +206,49 @@ enum MultiLineCommentNoComma {
// CHECK-FIXES-NEXT: spans multiple lines */
// CHECK-FIXES-NEXT: };
+// Enumerators from macros are checked like other enumerators.
+#define ENUMERATOR(n) n
+enum MacroEnumerator {
+ ENUMERATOR(ME_A),
+ ENUMERATOR(ME_B)
+};
+// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: enum should have a trailing comma
+// CHECK-FIXES: enum MacroEnumerator {
+// CHECK-FIXES-NEXT: ENUMERATOR(ME_A),
+// CHECK-FIXES-NEXT: ENUMERATOR(ME_B),
+// CHECK-FIXES-NEXT: };
+
+enum MacroEnumeratorMultiLineComment {
+ ENUMERATOR(MEC_A),
+ ENUMERATOR(MEC_B) /* comment that
+ spans multiple lines */
+};
+// CHECK-MESSAGES: :[[@LINE-3]]:20: warning: enum should have a trailing comma
+// CHECK-FIXES: enum MacroEnumeratorMultiLineComment {
+// CHECK-FIXES-NEXT: ENUMERATOR(MEC_A),
+// CHECK-FIXES-NEXT: ENUMERATOR(MEC_B), /* comment that
+// CHECK-FIXES-NEXT: spans multiple lines */
+// CHECK-FIXES-NEXT: };
+
+// The opening brace can come from a macro.
+#define LBRACE {
+enum MacroOpeningBrace LBRACE
+ MOB_A,
+ MOB_B
+};
+// CHECK-MESSAGES: :[[@LINE-2]]:8: warning: enum should have a trailing comma
+// CHECK-FIXES: enum MacroOpeningBrace LBRACE
+// CHECK-FIXES-NEXT: MOB_A,
+// CHECK-FIXES-NEXT: MOB_B,
+// CHECK-FIXES-NEXT: };
+
+// An enum whose closing brace comes from a macro is ignored.
+#define RBRACE }
+enum MacroClosingBrace {
+ MCB_A,
+ MCB_B
+RBRACE;
+
// Macros are ignored
#define ENUM(n, a, b) enum n { a, b }
#define INIT {1, 2}
More information about the cfe-commits
mailing list