[clang] 19884d6 - [clang-format] Don't skip PP lines if original line was a PP line when trying to merge lines

Arthur Eubanks via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 20 08:43:04 PDT 2022


Author: Arthur Eubanks
Date: 2022-04-20T08:42:30-07:00
New Revision: 19884d62c44cdcf236cdf4a55e8a50437070c4fc

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

LOG: [clang-format] Don't skip PP lines if original line was a PP line when trying to merge lines

Fixes a crash introduced in D123737 where LastNonComment would be null.

Reviewed By: curdeius

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 9154999e2644f..6137cdf344bc0 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -307,9 +307,13 @@ class LineJoiner {
           // TODO: Use IndentTracker to avoid loop?
           // Find the last line with lower level.
           auto J = I - 1;
-          for (; J != AnnotatedLines.begin(); --J)
-            if (!(*J)->InPPDirective && (*J)->Level < TheLine->Level)
-              break;
+          if (!TheLine->InPPDirective) {
+            for (; J != AnnotatedLines.begin(); --J) {
+              if (!(*J)->InPPDirective && (*J)->Level < TheLine->Level)
+                break;
+            }
+          }
+
           if ((*J)->Level >= TheLine->Level)
             return false;
 

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index bf4ba3d4738ee..d727ef2254fde 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -13393,6 +13393,12 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
                "    return 1;            \\\n"
                "  return 2;",
                ShortMergedIf);
+
+  verifyFormat("//\n"
+               "#define a \\\n"
+               "  if      \\\n"
+               "  0",
+               getChromiumStyle(FormatStyle::LK_Cpp));
 }
 
 TEST_F(FormatTest, FormatStarDependingOnContext) {


        


More information about the cfe-commits mailing list