Skip to content

[clang-format] Fix a crash in EnumTrailingComma #135903

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 2 commits into from
Apr 19, 2025
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
17 changes: 11 additions & 6 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -2448,6 +2452,7 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
cantFail(Result.add(tooling::Replacement(
SourceMgr, BeforeRBrace->Tok.getEndLoc(), 0, ",")));
}
BeforeRBrace = nullptr;
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27981,6 +27981,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) {
Expand Down
Loading