[clang] 6257832 - [clang-format] Wrap inserted braces only if preceded by comments

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 20 12:09:52 PDT 2022


Author: owenca
Date: 2022-09-20T12:09:39-07:00
New Revision: 6257832bf94fbd83836ee3616480d815a1fdfbf3

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

LOG: [clang-format] Wrap inserted braces only if preceded by comments

Fixes #57805.

Differential Revision: https://reviews.llvm.org/D134233

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 025e1aa977247..67f6818bb7e02 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1871,7 +1871,7 @@ class BracesInserter : public TokenAnalyzer {
         std::string Brace;
         if (Token->BraceCount < 0) {
           assert(Token->BraceCount == -1);
-          Brace = "\n{";
+          Brace = Token->is(tok::comment) ? "\n{" : "{";
         } else {
           Brace = '\n' + std::string(Token->BraceCount, '}');
         }

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 8999e590612e3..f27fff1e9ebd0 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25388,6 +25388,12 @@ TEST_F(FormatTest, InsertBraces) {
                "} while (0);",
                Style);
 
+  Style.RemoveBracesLLVM = true;
+  verifyFormat("if (a) //\n"
+               "  return b;",
+               Style);
+  Style.RemoveBracesLLVM = false;
+
   Style.ColumnLimit = 15;
 
   verifyFormat("#define A     \\\n"


        


More information about the cfe-commits mailing list