[clang] [clang-format] Fix a bug in SpaceInEmptyBlock option (PR #85508)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sat Mar 16 02:20:46 PDT 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/85508
Fixes #84695.
>From d15f3ddd2f9aef02f1706e7af64e763eeee59a89 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 16 Mar 2024 02:18:33 -0700
Subject: [PATCH] [clang-format] Fix a bug in SpaceInEmptyBlock option
Fixes #84695.
---
clang/lib/Format/TokenAnnotator.cpp | 4 ++++
clang/unittests/Format/FormatTest.cpp | 4 ++++
2 files changed, 8 insertions(+)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 1342d37a147915..5a715ac8a5f23f 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4271,6 +4271,10 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
return Left.is(tok::hash);
if (Left.isOneOf(tok::hashhash, tok::hash))
return Right.is(tok::hash);
+ if (Left.is(BK_Block) && Right.is(tok::r_brace) &&
+ Right.MatchingParen == &Left && Line.Children.empty()) {
+ return Style.SpaceInEmptyBlock;
+ }
if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
(Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index fc367a7a5a8981..4a72c043862b90 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6938,6 +6938,10 @@ TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
"else if (b) { }\n"
"else { }",
Style);
+
+ Style = getLLVMStyle(FormatStyle::LK_CSharp);
+ Style.SpaceInEmptyBlock = true;
+ verifyFormat("Event += () => { };", Style);
}
TEST_F(FormatTest, FormatBeginBlockEndMacros) {
More information about the cfe-commits
mailing list