[clang] [clang-format] Add BraceWrapping.AfterExportBlock option (PR #216892)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 22 06:16:10 PDT 2026
https://github.com/AhmedKamel10 updated https://github.com/llvm/llvm-project/pull/216892
>From a5f9dabdcb38468862cbf520497db5b99d3457ad Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Tue, 18 Aug 2026 03:40:15 +0300
Subject: [PATCH 1/9] [clang-format] Add BraceWrapping.AfterExportBlock option
---
clang/docs/ClangFormatStyleOptions.md | 10 ++++++++++
clang/include/clang/Format/Format.h | 9 +++++++++
clang/lib/Format/Format.cpp | 6 ++++++
clang/lib/Format/UnwrappedLineFormatter.cpp | 11 ++++++++++-
clang/lib/Format/UnwrappedLineParser.cpp | 6 ++++++
clang/unittests/Format/FormatTest.cpp | 21 +++++++++++++++++++++
6 files changed, 62 insertions(+), 1 deletion(-)
diff --git a/clang/docs/ClangFormatStyleOptions.md b/clang/docs/ClangFormatStyleOptions.md
index 9b962e6e1e083..e14fadb41d296 100644
--- a/clang/docs/ClangFormatStyleOptions.md
+++ b/clang/docs/ClangFormatStyleOptions.md
@@ -2597,6 +2597,16 @@ the configuration (without a prefix: `Auto`).
}
```
+ - `bool AfterExportBlock` Wrap export blocks.
+
+ ```c++
+ true: false:
+ export export {
+ { int foo();
+ int foo(); }
+ }
+ ```
+
- `bool BeforeCatch` Wrap before `catch`.
```c++
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 3948337d2fc3d..94333fa37cf9e 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1549,6 +1549,15 @@ struct FormatStyle {
/// }
/// \endcode
bool AfterExternBlock; // Partially superseded by IndentExternBlock
+ /// Wrap export blocks.
+ /// \code
+ /// true: false:
+ /// export export {
+ /// { int foo();
+ /// int foo(); }
+ /// }
+ /// \endcode
+ bool AfterExportBlock;
/// Wrap before `catch`.
/// \code
/// true:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 2b6e65efbf026..275d8f59d8ebc 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -220,6 +220,7 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
IO.mapOptional("AfterEnum", Wrapping.AfterEnum);
IO.mapOptional("AfterExternBlock", Wrapping.AfterExternBlock);
+ IO.mapOptional("AfterExportBlock", Wrapping.AfterExportBlock);
IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
@@ -1725,6 +1726,7 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
/*AfterStruct=*/false,
/*AfterUnion=*/false,
/*AfterExternBlock=*/false,
+ /*AfterExportBlock=*/false,
/*BeforeCatch=*/false,
/*BeforeElse=*/false,
/*BeforeLambdaBody=*/false,
@@ -1746,6 +1748,7 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
Expanded.BraceWrapping.AfterStruct = true;
Expanded.BraceWrapping.AfterUnion = true;
Expanded.BraceWrapping.AfterExternBlock = true;
+ Expanded.BraceWrapping.AfterExportBlock = true;
Expanded.BraceWrapping.SplitEmptyFunction = true;
Expanded.BraceWrapping.SplitEmptyRecord = false;
break;
@@ -1765,6 +1768,7 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
Expanded.BraceWrapping.AfterStruct = true;
Expanded.BraceWrapping.AfterUnion = true;
Expanded.BraceWrapping.AfterExternBlock = true;
+ Expanded.BraceWrapping.AfterExportBlock = true;
Expanded.BraceWrapping.BeforeCatch = true;
Expanded.BraceWrapping.BeforeElse = true;
Expanded.BraceWrapping.BeforeLambdaBody = true;
@@ -1795,6 +1799,7 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
/*AfterStruct=*/true,
/*AfterUnion=*/true,
/*AfterExternBlock=*/true,
+ /*AfterExportBlock=*/true,
/*BeforeCatch=*/true,
/*BeforeElse=*/true,
/*BeforeLambdaBody=*/true,
@@ -1897,6 +1902,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
/*AfterStruct=*/false,
/*AfterUnion=*/false,
/*AfterExternBlock=*/false,
+ /*AfterExportBlock=*/false,
/*BeforeCatch=*/false,
/*BeforeElse=*/false,
/*BeforeLambdaBody=*/false,
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 7afc7a46dd1c0..a806bc8494ea0 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -444,7 +444,8 @@ class LineJoiner {
if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&
(FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
TT_ForEachMacro) ||
- TheLine->startsWithExportBlock())) {
+ (TheLine->startsWithExportBlock() &&
+ !Style.BraceWrapping.AfterExportBlock))) {
return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never
? tryMergeSimpleBlock(I, E, Limit)
: 0;
@@ -937,6 +938,14 @@ class LineJoiner {
}
if (Line.endsWith(tok::l_brace)) {
+ // Refuse to merge export blocks if the style requires the brace to be on
+ // a new line.
+ if (Style.BraceWrapping.AfterExportBlock &&
+ Line.First->is(tok::l_brace) && I > AnnotatedLines.begin() &&
+ I[-1]->startsWith(tok::kw_export)) {
+ return 0;
+ }
+
if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never &&
Line.First->is(TT_BlockLBrace)) {
return 0;
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index da6465548bb3e..2b2e393271bdb 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3311,6 +3311,12 @@ void UnwrappedLineParser::parseNamespace() {
}
void UnwrappedLineParser::parseCppExportBlock() {
+
+ if (FormatTok->is(tok::l_brace)) {
+ if (Style.BraceWrapping.AfterExportBlock)
+ addUnwrappedLine();
+ }
+
parseNamespaceOrExportBlock(/*AddLevels=*/Style.IndentExportBlock ? 1 : 0);
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6f604167f785c..718e6530c2a90 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -4931,6 +4931,27 @@ TEST_F(FormatTest, IndentExternBlockStyle) {
Style);
}
+TEST_F(FormatTest, BraceWrappingAfterExportBlock) {
+ FormatStyle Style = getLLVMStyle();
+ Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+
+ Style.BraceWrapping.AfterExportBlock = true;
+ verifyFormat("export\n"
+ "{\n"
+ " int foo();\n"
+ "}",
+ "export {\n"
+ " int foo();\n"
+ "}",
+ Style);
+
+ Style.BraceWrapping.AfterExportBlock = false;
+ verifyFormat("export {\n"
+ " int foo();\n"
+ "}",
+ Style);
+}
+
TEST_F(FormatTest, FormatsInlineASM) {
verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
verifyFormat("asm(\"nop\" ::: \"memory\");");
>From 153e3474f7f5b165ba2aa7d131a2c45cf92bc580 Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Tue, 18 Aug 2026 23:25:49 +0300
Subject: [PATCH 2/9] [clang-format] Add TT_ExportLBrace and support
AfterExportBlock brace wrapping
---
clang/include/clang/Format/Format.h | 19 ++++++++++---------
clang/lib/Format/FormatToken.h | 1 +
clang/lib/Format/TokenAnnotator.cpp | 2 +-
clang/lib/Format/UnwrappedLineFormatter.cpp | 6 ++----
clang/lib/Format/UnwrappedLineParser.cpp | 3 +--
clang/unittests/Format/TokenAnnotatorTest.cpp | 8 ++++++++
6 files changed, 23 insertions(+), 16 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 94333fa37cf9e..82d8465bd08f6 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1535,6 +1535,15 @@ struct FormatStyle {
/// }
/// \endcode
bool AfterUnion;
+ /// Wrap export blocks.
+ /// \code
+ /// true: false:
+ /// export vs. export {
+ /// { int foo();
+ /// int foo(); }
+ /// }
+ /// \endcode
+ bool AfterExportBlock;
/// Wrap extern blocks.
/// \code
/// true:
@@ -1549,15 +1558,7 @@ struct FormatStyle {
/// }
/// \endcode
bool AfterExternBlock; // Partially superseded by IndentExternBlock
- /// Wrap export blocks.
- /// \code
- /// true: false:
- /// export export {
- /// { int foo();
- /// int foo(); }
- /// }
- /// \endcode
- bool AfterExportBlock;
+
/// Wrap before `catch`.
/// \code
/// true:
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 4a2982eaa3a17..691ecdd24f448 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -80,6 +80,7 @@ namespace format {
TYPE(EnumLBrace) \
TYPE(EnumRBrace) \
TYPE(EnumUnderlyingTypeColon) \
+ TYPE(ExportLBrace) \
TYPE(FatArrow) \
TYPE(ForEachMacro) \
TYPE(FunctionAnnotationRParen) \
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index b6c33279b0aca..8f064e38a691b 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2137,7 +2137,7 @@ class AnnotatingParser {
TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
- TT_CompoundRequirementLBrace, TT_BracedListLBrace,
+ TT_CompoundRequirementLBrace, TT_BracedListLBrace, TT_ExportLBrace,
TT_FunctionLikeMacro)) {
CurrentToken->setType(TT_Unknown);
}
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index a806bc8494ea0..50cb6b7dd11df 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -938,11 +938,9 @@ class LineJoiner {
}
if (Line.endsWith(tok::l_brace)) {
- // Refuse to merge export blocks if the style requires the brace to be on
- // a new line.
+
if (Style.BraceWrapping.AfterExportBlock &&
- Line.First->is(tok::l_brace) && I > AnnotatedLines.begin() &&
- I[-1]->startsWith(tok::kw_export)) {
+ Line.First->is(TT_ExportLBrace)) {
return 0;
}
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 2b2e393271bdb..b6a95d2aa174b 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3311,12 +3311,11 @@ void UnwrappedLineParser::parseNamespace() {
}
void UnwrappedLineParser::parseCppExportBlock() {
-
if (FormatTok->is(tok::l_brace)) {
+ FormatTok->setType(TT_ExportLBrace);
if (Style.BraceWrapping.AfterExportBlock)
addUnwrappedLine();
}
-
parseNamespaceOrExportBlock(/*AddLevels=*/Style.IndentExportBlock ? 1 : 0);
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index ae9e0b6b8e74c..f297c4a091abc 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -694,6 +694,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsEnums) {
EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_EnumRBrace);
}
+TEST_F(TokenAnnotatorTest, UnderstandsExportBlock) {
+ auto Tokens = annotate("export {\n"
+ "int foo();\n"
+ "}");
+ ASSERT_EQ(Tokens.size(), 9u);
+ EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_ExportLBrace);
+}
+
TEST_F(TokenAnnotatorTest, UnderstandsDefaultedAndDeletedFunctions) {
auto Tokens = annotate("auto operator<=>(const T &) const & = default;");
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
>From 915fa0642101092e47b8f035d62702956dc707f4 Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Wed, 19 Aug 2026 16:21:33 +0300
Subject: [PATCH 3/9] [clang-format] Use setFinalizedType for TT_ExportLBrace
---
clang/include/clang/Format/Format.h | 1 -
clang/lib/Format/TokenAnnotator.cpp | 2 +-
clang/lib/Format/UnwrappedLineParser.cpp | 2 +-
clang/unittests/Format/TokenAnnotatorTest.cpp | 2 +-
4 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 82d8465bd08f6..f4e3479392f6f 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1558,7 +1558,6 @@ struct FormatStyle {
/// }
/// \endcode
bool AfterExternBlock; // Partially superseded by IndentExternBlock
-
/// Wrap before `catch`.
/// \code
/// true:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 8f064e38a691b..b6c33279b0aca 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2137,7 +2137,7 @@ class AnnotatingParser {
TT_RecordLBrace, TT_StructLBrace, TT_UnionLBrace, TT_RequiresClause,
TT_RequiresClauseInARequiresExpression, TT_RequiresExpression,
TT_RequiresExpressionLParen, TT_RequiresExpressionLBrace,
- TT_CompoundRequirementLBrace, TT_BracedListLBrace, TT_ExportLBrace,
+ TT_CompoundRequirementLBrace, TT_BracedListLBrace,
TT_FunctionLikeMacro)) {
CurrentToken->setType(TT_Unknown);
}
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index b6a95d2aa174b..621804b060fad 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3312,7 +3312,7 @@ void UnwrappedLineParser::parseNamespace() {
void UnwrappedLineParser::parseCppExportBlock() {
if (FormatTok->is(tok::l_brace)) {
- FormatTok->setType(TT_ExportLBrace);
+ FormatTok->setFinalizedType(TT_ExportLBrace);
if (Style.BraceWrapping.AfterExportBlock)
addUnwrappedLine();
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index f297c4a091abc..b71147aaf1bc2 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -698,7 +698,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsExportBlock) {
auto Tokens = annotate("export {\n"
"int foo();\n"
"}");
- ASSERT_EQ(Tokens.size(), 9u);
+ ASSERT_EQ(Tokens.size(), 9u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::l_brace, TT_ExportLBrace);
}
>From 8b10243e5ea647d118e5c0fc58d42aeec6362df5 Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Wed, 19 Aug 2026 16:30:45 +0300
Subject: [PATCH 4/9] [clang-format] dropped blanck lines
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 50cb6b7dd11df..49e4942bd8743 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -938,7 +938,6 @@ class LineJoiner {
}
if (Line.endsWith(tok::l_brace)) {
-
if (Style.BraceWrapping.AfterExportBlock &&
Line.First->is(TT_ExportLBrace)) {
return 0;
>From 2108f739056fbc5e2abb93cf295a803a049aa6f7 Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Wed, 19 Aug 2026 23:10:31 +0300
Subject: [PATCH 5/9] Add config tests and sort AfterExportBlock alphabetically
---
clang/docs/ClangFormatStyleOptions.md | 20 ++++++++++----------
clang/lib/Format/Format.cpp | 2 +-
clang/lib/Format/TokenAnnotator.h | 4 ----
clang/lib/Format/UnwrappedLineFormatter.cpp | 4 ++--
clang/unittests/Format/ConfigParseTest.cpp | 1 +
5 files changed, 14 insertions(+), 17 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.md b/clang/docs/ClangFormatStyleOptions.md
index e14fadb41d296..d0a2805ac5eed 100644
--- a/clang/docs/ClangFormatStyleOptions.md
+++ b/clang/docs/ClangFormatStyleOptions.md
@@ -2582,6 +2582,16 @@ the configuration (without a prefix: `Auto`).
}
```
+ - `bool AfterExportBlock` Wrap export blocks.
+
+ ```c++
+ true: false:
+ export vs. export {
+ { int foo();
+ int foo(); }
+ }
+ ```
+
- `bool AfterExternBlock` Wrap extern blocks.
```c++
@@ -2597,16 +2607,6 @@ the configuration (without a prefix: `Auto`).
}
```
- - `bool AfterExportBlock` Wrap export blocks.
-
- ```c++
- true: false:
- export export {
- { int foo();
- int foo(); }
- }
- ```
-
- `bool BeforeCatch` Wrap before `catch`.
```c++
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 275d8f59d8ebc..7edd79cc828e2 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -219,8 +219,8 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
IO.mapOptional("AfterClass", Wrapping.AfterClass);
IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
IO.mapOptional("AfterEnum", Wrapping.AfterEnum);
- IO.mapOptional("AfterExternBlock", Wrapping.AfterExternBlock);
IO.mapOptional("AfterExportBlock", Wrapping.AfterExportBlock);
+ IO.mapOptional("AfterExternBlock", Wrapping.AfterExternBlock);
IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h
index 264f39b7b1d60..69eda4f993051 100644
--- a/clang/lib/Format/TokenAnnotator.h
+++ b/clang/lib/Format/TokenAnnotator.h
@@ -157,10 +157,6 @@ class AnnotatedLine {
startsWith(tok::kw_export, tok::kw_namespace);
}
- /// \c true if this line starts a C++ export block.
- bool startsWithExportBlock() const {
- return startsWith(tok::kw_export, tok::l_brace);
- }
FormatToken *getFirstNonComment() const {
assert(First);
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 49e4942bd8743..f445676692252 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -444,7 +444,7 @@ class LineJoiner {
if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&
(FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
TT_ForEachMacro) ||
- (TheLine->startsWithExportBlock() &&
+ (TheLine->Last->is(TT_ExportLBrace) &&
!Style.BraceWrapping.AfterExportBlock))) {
return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never
? tryMergeSimpleBlock(I, E, Limit)
@@ -893,7 +893,7 @@ class LineJoiner {
Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
tok::kw___finally, tok::r_brace,
Keywords.kw___except) ||
- Line.startsWithExportBlock()) {
+ Line.First->is(TT_ExportLBrace) || Line.Last->is(TT_ExportLBrace)) {
if (IsSplitBlock)
return 0;
// Don't merge when we can't except the case when
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 9350ba7eb3de4..53fbec9806ca4 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -238,6 +238,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
+ CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExportBlock);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
>From fc62eca560595cdba499a4804a21213d4d48a664 Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Wed, 19 Aug 2026 23:31:45 +0300
Subject: [PATCH 6/9] Apply clang-format fixes
---
clang/lib/Format/TokenAnnotator.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h
index 69eda4f993051..c909c39e92ea3 100644
--- a/clang/lib/Format/TokenAnnotator.h
+++ b/clang/lib/Format/TokenAnnotator.h
@@ -157,7 +157,6 @@ class AnnotatedLine {
startsWith(tok::kw_export, tok::kw_namespace);
}
-
FormatToken *getFirstNonComment() const {
assert(First);
return First->is(tok::comment) ? First->getNextNonComment() : First;
>From c225e44ca5eff1e44ba157077aebc8163799c4ec Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Thu, 20 Aug 2026 03:29:43 +0300
Subject: [PATCH 7/9] Fix clang-format: remove extra blank line in
TokenAnnotator.h
---
clang/lib/Format/TokenAnnotator.h | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h
index c909c39e92ea3..264f39b7b1d60 100644
--- a/clang/lib/Format/TokenAnnotator.h
+++ b/clang/lib/Format/TokenAnnotator.h
@@ -157,6 +157,11 @@ class AnnotatedLine {
startsWith(tok::kw_export, tok::kw_namespace);
}
+ /// \c true if this line starts a C++ export block.
+ bool startsWithExportBlock() const {
+ return startsWith(tok::kw_export, tok::l_brace);
+ }
+
FormatToken *getFirstNonComment() const {
assert(First);
return First->is(tok::comment) ? First->getNextNonComment() : First;
>From 781524c0f8da1bd771003a103e95a7b1ae2397a8 Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Fri, 21 Aug 2026 23:31:49 +0300
Subject: [PATCH 8/9] [clang-format] Removed redundant check in
tryMergeSimpleBlock
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 2 +-
clang/unittests/Format/FormatTest.cpp | 5 +----
2 files changed, 2 insertions(+), 5 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index f445676692252..e42d4a534d28b 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -893,7 +893,7 @@ class LineJoiner {
Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
tok::kw___finally, tok::r_brace,
Keywords.kw___except) ||
- Line.First->is(TT_ExportLBrace) || Line.Last->is(TT_ExportLBrace)) {
+ Line.Last->is(TT_ExportLBrace)) {
if (IsSplitBlock)
return 0;
// Don't merge when we can't except the case when
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 718e6530c2a90..7d1b3c4246a6e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -4930,11 +4930,9 @@ TEST_F(FormatTest, IndentExternBlockStyle) {
"}",
Style);
}
-
TEST_F(FormatTest, BraceWrappingAfterExportBlock) {
FormatStyle Style = getLLVMStyle();
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
-
Style.BraceWrapping.AfterExportBlock = true;
verifyFormat("export\n"
"{\n"
@@ -4944,14 +4942,13 @@ TEST_F(FormatTest, BraceWrappingAfterExportBlock) {
" int foo();\n"
"}",
Style);
-
+
Style.BraceWrapping.AfterExportBlock = false;
verifyFormat("export {\n"
" int foo();\n"
"}",
Style);
}
-
TEST_F(FormatTest, FormatsInlineASM) {
verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
verifyFormat("asm(\"nop\" ::: \"memory\");");
>From aabd46fa6304dc5cd9dea9bf4b3e73b8c344d0b5 Mon Sep 17 00:00:00 2001
From: ahmedkamel10 <amkzaher at gmail.com>
Date: Sat, 22 Aug 2026 16:11:00 +0300
Subject: [PATCH 9/9] [clang-format] Fix alphabetical order and trailing
whitespace
---
clang/lib/Format/Format.cpp | 10 +++++-----
clang/lib/Format/TokenAnnotator.h | 5 -----
clang/unittests/Format/FormatTest.cpp | 2 +-
3 files changed, 6 insertions(+), 11 deletions(-)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 7edd79cc828e2..eaf57238f1704 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1725,8 +1725,8 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
/*AfterObjCDeclaration=*/false,
/*AfterStruct=*/false,
/*AfterUnion=*/false,
- /*AfterExternBlock=*/false,
/*AfterExportBlock=*/false,
+ /*AfterExternBlock=*/false,
/*BeforeCatch=*/false,
/*BeforeElse=*/false,
/*BeforeLambdaBody=*/false,
@@ -1747,8 +1747,8 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
Expanded.BraceWrapping.AfterFunction = true;
Expanded.BraceWrapping.AfterStruct = true;
Expanded.BraceWrapping.AfterUnion = true;
- Expanded.BraceWrapping.AfterExternBlock = true;
Expanded.BraceWrapping.AfterExportBlock = true;
+ Expanded.BraceWrapping.AfterExternBlock = true;
Expanded.BraceWrapping.SplitEmptyFunction = true;
Expanded.BraceWrapping.SplitEmptyRecord = false;
break;
@@ -1767,8 +1767,8 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
Expanded.BraceWrapping.AfterObjCDeclaration = true;
Expanded.BraceWrapping.AfterStruct = true;
Expanded.BraceWrapping.AfterUnion = true;
- Expanded.BraceWrapping.AfterExternBlock = true;
Expanded.BraceWrapping.AfterExportBlock = true;
+ Expanded.BraceWrapping.AfterExternBlock = true;
Expanded.BraceWrapping.BeforeCatch = true;
Expanded.BraceWrapping.BeforeElse = true;
Expanded.BraceWrapping.BeforeLambdaBody = true;
@@ -1798,8 +1798,8 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
/*AfterObjCDeclaration=*/true,
/*AfterStruct=*/true,
/*AfterUnion=*/true,
- /*AfterExternBlock=*/true,
/*AfterExportBlock=*/true,
+ /*AfterExternBlock=*/true,
/*BeforeCatch=*/true,
/*BeforeElse=*/true,
/*BeforeLambdaBody=*/true,
@@ -1901,8 +1901,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
/*AfterObjCDeclaration=*/false,
/*AfterStruct=*/false,
/*AfterUnion=*/false,
- /*AfterExternBlock=*/false,
/*AfterExportBlock=*/false,
+ /*AfterExternBlock=*/false,
/*BeforeCatch=*/false,
/*BeforeElse=*/false,
/*BeforeLambdaBody=*/false,
diff --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h
index 264f39b7b1d60..c909c39e92ea3 100644
--- a/clang/lib/Format/TokenAnnotator.h
+++ b/clang/lib/Format/TokenAnnotator.h
@@ -157,11 +157,6 @@ class AnnotatedLine {
startsWith(tok::kw_export, tok::kw_namespace);
}
- /// \c true if this line starts a C++ export block.
- bool startsWithExportBlock() const {
- return startsWith(tok::kw_export, tok::l_brace);
- }
-
FormatToken *getFirstNonComment() const {
assert(First);
return First->is(tok::comment) ? First->getNextNonComment() : First;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 7d1b3c4246a6e..a0b9bd1398d80 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -4942,7 +4942,7 @@ TEST_F(FormatTest, BraceWrappingAfterExportBlock) {
" int foo();\n"
"}",
Style);
-
+
Style.BraceWrapping.AfterExportBlock = false;
verifyFormat("export {\n"
" int foo();\n"
More information about the cfe-commits
mailing list