[clang] c64f670 - [clang-format] Fix a crash in EnumTrailingComma (#135903)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 18 18:11:59 PDT 2025
Author: Owen Pan
Date: 2025-04-18T18:11:56-07:00
New Revision: c64f670255d3fa279f026994da2cd80f3394e0c8
URL: https://github.com/llvm/llvm-project/commit/c64f670255d3fa279f026994da2cd80f3394e0c8
DIFF: https://github.com/llvm/llvm-project/commit/c64f670255d3fa279f026994da2cd80f3394e0c8.diff
LOG: [clang-format] Fix a crash in EnumTrailingComma (#135903)
Fix #135819
Added:
Modified:
clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index c601967a8715c..0667813110454 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2427,19 +2427,23 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
private:
void editEnumTrailingComma(SmallVectorImpl<AnnotatedLine *> &Lines,
tooling::Replacements &Result) {
+ bool InEnumBraces = false;
+ const FormatToken *BeforeRBrace = nullptr;
const auto &SourceMgr = Env.getSourceManager();
for (auto *Line : Lines) {
if (!Line->Children.empty())
editEnumTrailingComma(Line->Children, Result);
- if (!Line->Affected)
- continue;
for (const auto *Token = Line->First; Token && !Token->Finalized;
Token = Token->Next) {
- if (Token->isNot(TT_EnumRBrace))
+ if (Token->isNot(TT_EnumRBrace)) {
+ if (Token->is(TT_EnumLBrace))
+ InEnumBraces = true;
+ else if (InEnumBraces && Token->isNot(tok::comment))
+ BeforeRBrace = Line->Affected ? Token : nullptr;
continue;
- const auto *BeforeRBrace = Token->getPreviousNonComment();
- assert(BeforeRBrace);
- if (BeforeRBrace->is(TT_EnumLBrace)) // Empty braces.
+ }
+ InEnumBraces = false;
+ if (!BeforeRBrace) // Empty braces or Line not affected.
continue;
if (BeforeRBrace->is(tok::comma)) {
if (Style.EnumTrailingComma == FormatStyle::ETC_Remove)
@@ -2448,6 +2452,7 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
cantFail(Result.add(tooling::Replacement(
SourceMgr, BeforeRBrace->Tok.getEndLoc(), 0, ",")));
}
+ BeforeRBrace = nullptr;
}
}
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 49284c7f51e27..d5a6206847d68 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -27992,6 +27992,33 @@ TEST_F(FormatTest, EnumTrailingComma) {
"};\n"
"enum Color { red, green, blue /**/ };",
Code, Style);
+
+ EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
+ Style.AllowShortEnumsOnASingleLine = false;
+
+ constexpr StringRef Input("enum {\n"
+ " //\n"
+ " a,\n"
+ " /**/\n"
+ " b,\n"
+ "};");
+ verifyFormat(Input, Input, Style, {tooling::Range(12, 3)}); // line 3
+ verifyFormat("enum {\n"
+ " //\n"
+ " a,\n"
+ " /**/\n"
+ " b\n"
+ "};",
+ Input, Style, {tooling::Range(24, 3)}); // line 5
+
+ Style.EnumTrailingComma = FormatStyle::ETC_Insert;
+ verifyFormat("enum class MyEnum_E {\n"
+ " MY_ENUM = 0U,\n"
+ "};",
+ "enum class MyEnum_E {\n"
+ " MY_ENUM = 0U\n"
+ "};",
+ Style);
}
TEST_F(FormatTest, BreakAfterAttributes) {
More information about the cfe-commits
mailing list