Author: Marek Kurdej
Date: 2022-02-17T09:49:00+01:00
New Revision: 0ae2464fcd4d2c2f285b83d16ff6e2426dd722d2

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

LOG: [clang-format] Fix wrong assertion with non-negative shift when aligning 
tokens.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index 758dc5860888e..55e0b7f8e8d9e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -406,7 +406,7 @@ AlignTokenSequence(const FormatStyle &Style, unsigned 
Start, unsigned End,
       Changes[i].Spaces += Shift;
 
     // We should not remove required spaces unless we break the line before.
-    assert(Changes[i].NewlinesBefore > 0 ||
+    assert(Shift >= 0 || Changes[i].NewlinesBefore > 0 ||
            Changes[i].Spaces >=
                static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) ||
            Changes[i].Tok->is(tok::eof));

diff  --git a/clang/unittests/Format/FormatTestSelective.cpp 
b/clang/unittests/Format/FormatTestSelective.cpp
index c88d1b8bd8ba2..2725e4cf776f6 100644
--- a/clang/unittests/Format/FormatTestSelective.cpp
+++ b/clang/unittests/Format/FormatTestSelective.cpp
@@ -603,6 +603,14 @@ TEST_F(FormatTestSelective, 
KeepsIndentAfterCommentSectionImport) {
   EXPECT_EQ(Code, format(Code, 47, 1));
 }
 
+TEST_F(FormatTestSelective, DontAssert) {
+  // https://llvm.org/PR53880
+  std::string Code = "void f() {\n"
+                     "  return a == 8 ? 32 : 16;\n"
+                     "}\n";
+  EXPECT_EQ(Code, format(Code, 40, 0));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to