[clang] 0ac35ec - [clang-format] Fix breaking enum braces when combined with export (#189128)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 28 08:55:57 PDT 2026
Author: Björn Schäpers
Date: 2026-03-28T15:55:52Z
New Revision: 0ac35ecc0bdd8c3f87f0ecdfe862e3eaf8922444
URL: https://github.com/llvm/llvm-project/commit/0ac35ecc0bdd8c3f87f0ecdfe862e3eaf8922444
DIFF: https://github.com/llvm/llvm-project/commit/0ac35ecc0bdd8c3f87f0ecdfe862e3eaf8922444.diff
LOG: [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.
Added:
Modified:
clang/lib/Format/Format.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 2b0aa1735c895..93859d7ea9c4a 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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};
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d2cdc28a7da7b..646182a673129 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -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 {`.
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index a3e8a3e270e73..ff01d8442f0d1 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 2701a7fca7346..0f7a31af8a5e6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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"
More information about the cfe-commits
mailing list