diff options
author | Owen Pan <owenpiano@gmail.com> | 2025-04-18 18:11:56 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-18 18:11:56 -0700 |
commit | c64f670255d3fa279f026994da2cd80f3394e0c8 (patch) | |
tree | c4ef17c7ed1c2d97661925aa1d5921a6913576e6 /clang/lib/Format/Format.cpp | |
parent | 667209e45122d0cb7a4c2ac27018d31165b1be70 (diff) | |
download | llvm-c64f670255d3fa279f026994da2cd80f3394e0c8.zip llvm-c64f670255d3fa279f026994da2cd80f3394e0c8.tar.gz llvm-c64f670255d3fa279f026994da2cd80f3394e0c8.tar.bz2 |
[clang-format] Fix a crash in EnumTrailingComma (#135903)
Fix #135819
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r-- | clang/lib/Format/Format.cpp | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index c601967..0667813 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2427,19 +2427,23 @@ public: 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 @@ private: cantFail(Result.add(tooling::Replacement( SourceMgr, BeforeRBrace->Tok.getEndLoc(), 0, ","))); } + BeforeRBrace = nullptr; } } } |