[PATCH] D124152: [clang-format] Fix a crash on AllowShortFunctionsOnASingleLine

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 21 00:23:28 PDT 2022


owenpan created this revision.
owenpan added reviewers: aeubanks, curdeius, MyDeveloperDay, HazardyKnusperkeks.
owenpan added a project: clang-format.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124152

Files:
  clang/lib/Format/TokenAnnotator.h
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -12773,6 +12773,14 @@
                "};",
                MergeInlineOnly);
 
+  verifyFormat("struct S {\n"
+               "// comment\n"
+               "#ifdef FOO\n"
+               "  int foo() { bar(); }\n"
+               "#endif\n"
+               "};",
+               MergeInlineOnly);
+
   // Also verify behavior when BraceWrapping.AfterFunction = true
   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
   MergeInlineOnly.BraceWrapping.AfterFunction = true;
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -306,24 +306,26 @@
 
           // TODO: Use IndentTracker to avoid loop?
           // Find the last line with lower level.
-          auto J = I - 1;
-          if (!TheLine->InPPDirective) {
-            for (; J != AnnotatedLines.begin(); --J) {
-              if (!(*J)->InPPDirective && (*J)->Level < TheLine->Level)
-                break;
+          const AnnotatedLine *Line = nullptr;
+          for (auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
+            assert(*J);
+            if (!(*J)->InPPDirective && !(*J)->isComment() &&
+                (*J)->Level < TheLine->Level) {
+              Line = *J;
+              break;
             }
           }
 
-          if ((*J)->Level >= TheLine->Level)
+          if (!Line)
             return false;
 
           // Check if the found line starts a record.
-          const FormatToken *LastNonComment = (*J)->Last;
+          const FormatToken *LastNonComment = Line->Last;
           assert(LastNonComment);
           if (LastNonComment->is(tok::comment)) {
             LastNonComment = LastNonComment->getPreviousNonComment();
             // There must be another token (usually `{`), because we chose a
-            // line that has a smaller level.
+            // non-PPDirective and non-comment line that has a smaller level.
             assert(LastNonComment);
           }
           return isRecordLBrace(*LastNonComment);
Index: clang/lib/Format/TokenAnnotator.h
===================================================================
--- clang/lib/Format/TokenAnnotator.h
+++ clang/lib/Format/TokenAnnotator.h
@@ -76,6 +76,10 @@
     }
   }
 
+  bool isComment() const {
+    return First && First->is(tok::comment) && !First->getNextNonComment();
+  }
+
   /// \c true if this line starts with the given tokens in order, ignoring
   /// comments.
   template <typename... Ts> bool startsWith(Ts... Tokens) const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124152.424112.patch
Type: text/x-patch
Size: 2804 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220421/f6a3d58e/attachment-0001.bin>


More information about the cfe-commits mailing list