Skip to content

Commit d1a80b9

Browse files
owencaIanWood1
authored andcommitted
[clang-format] Fix a crash in EnumTrailingComma (llvm#135903)
Fix llvm#135819
1 parent 5db7ae9 commit d1a80b9

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

clang/lib/Format/Format.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,19 +2427,23 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
24272427
private:
24282428
void editEnumTrailingComma(SmallVectorImpl<AnnotatedLine *> &Lines,
24292429
tooling::Replacements &Result) {
2430+
bool InEnumBraces = false;
2431+
const FormatToken *BeforeRBrace = nullptr;
24302432
const auto &SourceMgr = Env.getSourceManager();
24312433
for (auto *Line : Lines) {
24322434
if (!Line->Children.empty())
24332435
editEnumTrailingComma(Line->Children, Result);
2434-
if (!Line->Affected)
2435-
continue;
24362436
for (const auto *Token = Line->First; Token && !Token->Finalized;
24372437
Token = Token->Next) {
2438-
if (Token->isNot(TT_EnumRBrace))
2438+
if (Token->isNot(TT_EnumRBrace)) {
2439+
if (Token->is(TT_EnumLBrace))
2440+
InEnumBraces = true;
2441+
else if (InEnumBraces && Token->isNot(tok::comment))
2442+
BeforeRBrace = Line->Affected ? Token : nullptr;
24392443
continue;
2440-
const auto *BeforeRBrace = Token->getPreviousNonComment();
2441-
assert(BeforeRBrace);
2442-
if (BeforeRBrace->is(TT_EnumLBrace)) // Empty braces.
2444+
}
2445+
InEnumBraces = false;
2446+
if (!BeforeRBrace) // Empty braces or Line not affected.
24432447
continue;
24442448
if (BeforeRBrace->is(tok::comma)) {
24452449
if (Style.EnumTrailingComma == FormatStyle::ETC_Remove)
@@ -2448,6 +2452,7 @@ class EnumTrailingCommaEditor : public TokenAnalyzer {
24482452
cantFail(Result.add(tooling::Replacement(
24492453
SourceMgr, BeforeRBrace->Tok.getEndLoc(), 0, ",")));
24502454
}
2455+
BeforeRBrace = nullptr;
24512456
}
24522457
}
24532458
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27992,6 +27992,33 @@ TEST_F(FormatTest, EnumTrailingComma) {
2799227992
"};\n"
2799327993
"enum Color { red, green, blue /**/ };",
2799427994
Code, Style);
27995+
27996+
EXPECT_TRUE(Style.AllowShortEnumsOnASingleLine);
27997+
Style.AllowShortEnumsOnASingleLine = false;
27998+
27999+
constexpr StringRef Input("enum {\n"
28000+
" //\n"
28001+
" a,\n"
28002+
" /**/\n"
28003+
" b,\n"
28004+
"};");
28005+
verifyFormat(Input, Input, Style, {tooling::Range(12, 3)}); // line 3
28006+
verifyFormat("enum {\n"
28007+
" //\n"
28008+
" a,\n"
28009+
" /**/\n"
28010+
" b\n"
28011+
"};",
28012+
Input, Style, {tooling::Range(24, 3)}); // line 5
28013+
28014+
Style.EnumTrailingComma = FormatStyle::ETC_Insert;
28015+
verifyFormat("enum class MyEnum_E {\n"
28016+
" MY_ENUM = 0U,\n"
28017+
"};",
28018+
"enum class MyEnum_E {\n"
28019+
" MY_ENUM = 0U\n"
28020+
"};",
28021+
Style);
2799528022
}
2799628023

2799728024
TEST_F(FormatTest, BreakAfterAttributes) {

0 commit comments

Comments
 (0)