[PATCH] D129348: [clang-format] Fix an assertion failure on -lines=0:n

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 8 00:03:05 PDT 2022


owenpan created this revision.
owenpan added reviewers: curdeius, HazardyKnusperkeks, MyDeveloperDay.
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.

Also fixed the error message for start line > end line and added test cases.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129348

Files:
  clang/test/Format/line-ranges.cpp
  clang/tools/clang-format/ClangFormat.cpp


Index: clang/tools/clang-format/ClangFormat.cpp
===================================================================
--- clang/tools/clang-format/ClangFormat.cpp
+++ clang/tools/clang-format/ClangFormat.cpp
@@ -246,8 +246,12 @@
         errs() << "error: invalid <start line>:<end line> pair\n";
         return true;
       }
+      if (FromLine < 1) {
+        errs() << "error: start line should be at least 1\n";
+        return true;
+      }
       if (FromLine > ToLine) {
-        errs() << "error: start line should be less than end line\n";
+        errs() << "error: start line should not exceed end line\n";
         return true;
       }
       SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);
Index: clang/test/Format/line-ranges.cpp
===================================================================
--- clang/test/Format/line-ranges.cpp
+++ clang/test/Format/line-ranges.cpp
@@ -9,3 +9,11 @@
 
 // CHECK: {{^int\ \*i;$}}
 int   *   i;
+
+// RUN: not clang-format -lines=0:1 < %s 2>&1 \
+// RUN:   | FileCheck -strict-whitespace -check-prefix=CHECK0 %s
+// CHECK0: error: start line should be at least 1
+
+// RUN: not clang-format -lines=2:1 < %s 2>&1 \
+// RUN:   | FileCheck -strict-whitespace -check-prefix=CHECK1 %s
+// CHECK1: error: start line should not exceed end line


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129348.443148.patch
Type: text/x-patch
Size: 1310 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220708/45b77568/attachment.bin>


More information about the cfe-commits mailing list