[clang] 27818f0 - [clang-format] Fix tabs when using BreakBeforeTernaryOperators=false.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 16 00:28:04 PST 2021


Author: Marek Kurdej
Date: 2021-12-16T09:28:00+01:00
New Revision: 27818f01fec2dd8f18325281f97646aed5ca9c89

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

LOG: [clang-format] Fix tabs when using BreakBeforeTernaryOperators=false.

Fixes https://github.com/llvm/llvm-project/issues/52724.

This is rather a workaround than a correct fix. To properly fix it, we'd need to find a better way to tell when not to decrease the StartOfTokenColumn.

Reviewed By: MyDeveloperDay, owenpan

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index fae8a1c3fdc66..96a66da0f82ba 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1282,9 +1282,12 @@ void WhitespaceManager::generateChanges() {
                                  C.EscapedNewlineColumn);
       else
         appendNewlineText(ReplacementText, C.NewlinesBefore);
+      // FIXME: This assert should hold if we computed the column correctly.
+      // assert((int)C.StartOfTokenColumn >= C.Spaces);
       appendIndentText(
           ReplacementText, C.Tok->IndentLevel, std::max(0, C.Spaces),
-          C.StartOfTokenColumn - std::max(0, C.Spaces), C.IsAligned);
+          std::max((int)C.StartOfTokenColumn, C.Spaces) - std::max(0, C.Spaces),
+          C.IsAligned);
       ReplacementText.append(C.CurrentLinePrefix);
       storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
     }

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f54139227e871..37d08d2d91292 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13452,6 +13452,25 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
                    "}",
                    Tab));
 
+  verifyFormat("void f() {\n"
+               "\treturn true ? aaaaaaaaaaaaaaaaaa\n"
+               "\t            : bbbbbbbbbbbbbbbbbb\n"
+               "}",
+               Tab);
+  FormatStyle TabNoBreak = Tab;
+  TabNoBreak.BreakBeforeTernaryOperators = false;
+  verifyFormat("void f() {\n"
+               "\treturn true ? aaaaaaaaaaaaaaaaaa :\n"
+               "\t              bbbbbbbbbbbbbbbbbb\n"
+               "}",
+               TabNoBreak);
+  verifyFormat("void f() {\n"
+               "\treturn true ?\n"
+               "\t           aaaaaaaaaaaaaaaaaaaa :\n"
+               "\t           bbbbbbbbbbbbbbbbbbbb\n"
+               "}",
+               TabNoBreak);
+
   Tab.UseTab = FormatStyle::UT_Never;
   EXPECT_EQ("/*\n"
             "              a\t\tcomment\n"


        


More information about the cfe-commits mailing list