[PATCH] D94287: [clang-format] Fix include sorting bug

Kent Sommer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 7 22:41:51 PST 2021


kentsommer created this revision.
kentsommer added a project: clang-format.
kentsommer requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

- Fixes a bug that caused `// clang-format off` in a includes section to disabled include sorting even after a `// clang-format on` was found.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94287

Files:
  clang/lib/Format/Format.cpp
  clang/test/Format/line-disable-include-sorting.cpp


Index: clang/test/Format/line-disable-include-sorting.cpp
===================================================================
--- /dev/null
+++ clang/test/Format/line-disable-include-sorting.cpp
@@ -0,0 +1,13 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s \
+// RUN:   | clang-format -style="{BasedOnStyle: LLVM, SortIncludes: true}" \
+// RUN:   | FileCheck -strict-whitespace %s
+// CHECK: {{^//\ clang-format\ off$}}
+// CHECK-NEXT: {{^#include\ <c>$}}
+// CHECK-NEXT: {{^//\ clang-format\ on$}}
+// CHECK-NEXT: {{^#include\ <a>$}}
+// CHECK-NEXT: {{^#include\ <b>$}}
+// clang-format off
+#include <c>
+// clang-format on
+#include <b>
+#include <a>
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -2307,8 +2307,8 @@
         IncludesInBlock.clear();
         FirstIncludeBlock = false;
       }
-      Prev = Pos + 1;
     }
+    Prev = Pos + 1;
     if (Pos == StringRef::npos || Pos + 1 == Code.size())
       break;
     SearchFrom = Pos + 1;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94287.315300.patch
Type: text/x-patch
Size: 1065 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210108/12a7dd33/attachment.bin>


More information about the cfe-commits mailing list