[clang] d33d563 - [clang-format] Fix a bug in SpaceInEmptyBlock option (#85508)

via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 16 14:48:56 PDT 2024


Author: Owen Pan
Date: 2024-03-16T14:48:51-07:00
New Revision: d33d5630b281debe6eabd67e323bcf767340fb6a

URL: https://github.com/llvm/llvm-project/commit/d33d5630b281debe6eabd67e323bcf767340fb6a
DIFF: https://github.com/llvm/llvm-project/commit/d33d5630b281debe6eabd67e323bcf767340fb6a.diff

LOG: [clang-format] Fix a bug in SpaceInEmptyBlock option (#85508)

Fixes #84695.

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
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 add92d3e111089..1f2e8ceb58ceff 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6959,6 +6959,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