[clang] [clang-format] Don't alter SpacesRequiredBefore of finalized tokens (PR #69897)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 22 23:23:39 PDT 2023


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/69897

Fixes #69237.

>From 69e39f7e19e7f2538df0b8c6ba89d5efa493dd7b Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 22 Oct 2023 23:15:08 -0700
Subject: [PATCH] [clang-format] Don't alter SpacesRequiredBefore of finalized
 tokens

Fixes #69237.
---
 clang/lib/Format/TokenAnnotator.cpp   |  2 +-
 clang/unittests/Format/FormatTest.cpp | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 7f85f48de2ed2ed..8d3d622bca035a3 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3544,7 +3544,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const {
           }
         }
       }
-    } else if (Current->SpacesRequiredBefore == 0 &&
+    } else if (!Current->Finalized && Current->SpacesRequiredBefore == 0 &&
                spaceRequiredBefore(Line, *Current)) {
       Current->SpacesRequiredBefore = 1;
     }
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 0a87cfc4f1d6af9..d5711782a23ef5b 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -26459,6 +26459,25 @@ TEST_F(FormatTest, AllowBreakBeforeNoexceptSpecifier) {
                Style);
 }
 
+TEST_F(FormatTest, PPBranchesInBracedInit) {
+  verifyFormat("A a_{kFlag1,\n"
+               "#if BUILD_FLAG\n"
+               "     kFlag2,\n"
+               "#else\n"
+               "     kFlag3,\n"
+               "#endif\n"
+               "     kFlag4};",
+               "A a_{\n"
+               "  kFlag1,\n"
+               "#if BUILD_FLAG\n"
+               "      kFlag2,\n"
+               "#else\n"
+               "      kFlag3,\n"
+               "#endif\n"
+               "      kFlag4\n"
+               "};");
+}
+
 } // namespace
 } // namespace test
 } // namespace format



More information about the cfe-commits mailing list