[clang-format] Fix breaking enum braces when combined with export (#189128)

This fixes #186684.

Also fix (not) breaking variables declared on the same line as the
closing brace.

And adapt whitesmith to that changes.
This commit is contained in:
Björn Schäpers 2026-03-28 16:55:52 +01:00 committed by GitHub
parent f0ce26d06d
commit 0ac35ecc0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 36 additions and 5 deletions

View File

@ -4115,10 +4115,14 @@ reformat(const FormatStyle &Style, StringRef Code,
expandPresetsBraceWrapping(Expanded);
expandPresetsSpaceBeforeParens(Expanded);
expandPresetsSpacesInParens(Expanded);
// These are handled by separate passes.
Expanded.InsertBraces = false;
Expanded.RemoveBracesLLVM = false;
Expanded.RemoveParentheses = FormatStyle::RPS_Leave;
Expanded.RemoveSemicolon = false;
// Make some sanity adjustments.
switch (Expanded.RequiresClausePosition) {
case FormatStyle::RCPS_SingleLine:
case FormatStyle::RCPS_WithPreceding:
@ -4127,6 +4131,8 @@ reformat(const FormatStyle &Style, StringRef Code,
default:
break;
}
if (Expanded.BraceWrapping.AfterEnum)
Expanded.AllowShortEnumsOnASingleLine = false;
if (Expanded.DisableFormat)
return {tooling::Replacements(), 0};

View File

@ -6045,7 +6045,8 @@ bool TokenAnnotator::mustBreakBefore(AnnotatedLine &Line,
if (Style.BraceWrapping.AfterEnum) {
if (Line.startsWith(tok::kw_enum) ||
Line.startsWith(tok::kw_typedef, tok::kw_enum)) {
Line.startsWith(tok::kw_typedef, tok::kw_enum) ||
Line.startsWith(tok::kw_export, tok::kw_enum)) {
return true;
}
// Ensure BraceWrapping for `public enum A {`.

View File

@ -3875,26 +3875,40 @@ bool UnwrappedLineParser::parseEnum() {
return true;
}
const bool ManageWhitesmithsBraces =
Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
if (!Style.AllowShortEnumsOnASingleLine &&
ShouldBreakBeforeBrace(Style, InitialToken,
Tokens->peekNextToken()->is(tok::r_brace))) {
addUnwrappedLine();
// If we're in Whitesmiths mode, indent the brace if we're not indenting
// the whole block.
if (ManageWhitesmithsBraces)
++Line->Level;
}
// Parse enum body.
nextToken();
if (!Style.AllowShortEnumsOnASingleLine) {
addUnwrappedLine();
Line->Level += 1;
if (!ManageWhitesmithsBraces)
++Line->Level;
}
const auto OpeningLineIndex = CurrentLines->empty()
? UnwrappedLine::kInvalidIndex
: CurrentLines->size() - 1;
bool HasError = !parseBracedList(/*IsAngleBracket=*/false, /*IsEnum=*/true);
if (!Style.AllowShortEnumsOnASingleLine)
Line->Level -= 1;
if (!Style.AllowShortEnumsOnASingleLine && !ManageWhitesmithsBraces)
--Line->Level;
if (HasError) {
if (FormatTok->is(tok::semi))
nextToken();
addUnwrappedLine();
}
setPreviousRBraceType(TT_EnumRBrace);
if (ManageWhitesmithsBraces)
Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
return true;
// There is no addUnwrappedLine() here so that we fall through to parsing a

View File

@ -2885,6 +2885,15 @@ TEST_F(FormatTest, ShortEnums) {
" C\n"
"} ShortEnum1, ShortEnum2;",
Style);
Style.AllowShortEnumsOnASingleLine = true;
verifyFormat("export enum\n"
"{\n"
" A,\n"
" B,\n"
" C\n"
"} ShortEnum1, ShortEnum2;",
Style);
}
TEST_F(FormatTest, ShortCompoundRequirement) {
@ -22146,7 +22155,8 @@ TEST_F(FormatTest, WhitesmithsBraceBreaking) {
" {\n"
" Y = 0,\n"
" Z = 1\n"
" };",
" };\n"
"int i;",
WhitesmithsBraceStyle);
verifyFormat("@interface BSApplicationController ()\n"