Skip to content

Commit 615f838

Browse files
committed
[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
1 parent ba007f2 commit 615f838

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang/test/Format/line-ranges.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ int * i;
99

1010
// CHECK: {{^int\ \*i;$}}
1111
int * i;
12+
13+
// RUN: not clang-format -lines=0:1 < %s 2>&1 \
14+
// RUN: | FileCheck -strict-whitespace -check-prefix=CHECK0 %s
15+
// CHECK0: error: start line should be at least 1
16+
17+
// RUN: not clang-format -lines=2:1 < %s 2>&1 \
18+
// RUN: | FileCheck -strict-whitespace -check-prefix=CHECK1 %s
19+
// CHECK1: error: start line should not exceed end line

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,12 @@ static bool fillRanges(MemoryBuffer *Code,
246246
errs() << "error: invalid <start line>:<end line> pair\n";
247247
return true;
248248
}
249+
if (FromLine < 1) {
250+
errs() << "error: start line should be at least 1\n";
251+
return true;
252+
}
249253
if (FromLine > ToLine) {
250-
errs() << "error: start line should be less than end line\n";
254+
errs() << "error: start line should not exceed end line\n";
251255
return true;
252256
}
253257
SourceLocation Start = Sources.translateLineCol(ID, FromLine, 1);

0 commit comments

Comments
 (0)