[clang] 615f838 - [clang-format] Fix an assertion failure on -lines=0:n
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 8 23:41:22 PDT 2022
Author: owenca
Date: 2022-07-08T23:41:16-07:00
New Revision: 615f838f7380e6a4f3df8e16f04dea3c8eed656c
URL: https://github.com/llvm/llvm-project/commit/615f838f7380e6a4f3df8e16f04dea3c8eed656c
DIFF: https://github.com/llvm/llvm-project/commit/615f838f7380e6a4f3df8e16f04dea3c8eed656c.diff
LOG: [clang-format] Fix an assertion failure on -lines=0:n
Also fixed the error message for start line > end line and added test cases.
Fixes #56438.
Differential Revision: https://reviews.llvm.org/D129348
Added:
Modified:
clang/test/Format/line-ranges.cpp
clang/tools/clang-format/ClangFormat.cpp
Removed:
################################################################################
diff --git a/clang/test/Format/line-ranges.cpp b/clang/test/Format/line-ranges.cpp
index e81e9624344d0..91b7d2cffc8f0 100644
--- a/clang/test/Format/line-ranges.cpp
+++ b/clang/test/Format/line-ranges.cpp
@@ -9,3 +9,11 @@ int * i;
// 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
diff --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 946ba131f6dc9..269bc59506b2d 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -246,8 +246,12 @@ static bool fillRanges(MemoryBuffer *Code,
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);
More information about the cfe-commits
mailing list