[clang-format] Don't skip PP lines if original line was a PP line when trying to...
authorArthur Eubanks <aeubanks@google.com>
Tue, 19 Apr 2022 20:02:54 +0000 (13:02 -0700)
committerArthur Eubanks <aeubanks@google.com>
Wed, 20 Apr 2022 15:42:30 +0000 (08:42 -0700)
Fixes a crash introduced in D123737 where LastNonComment would be null.

Reviewed By: curdeius

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

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

index 9154999..6137cdf 100644 (file)
@@ -307,9 +307,13 @@ private:
           // 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;
 
index bf4ba3d..d727ef2 100644 (file)
@@ -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) {