Skip to content

Commit

Permalink
[clang-format] Fix an assertion failure on -lines=0:n
Browse files Browse the repository at this point in the history
Also fixed the error message for start line > end line and added test cases.

Fixes #56438.

Differential Revision: https://reviews.llvm.org/D129348
  • Loading branch information
owenca committed Jul 9, 2022
1 parent ba007f2 commit 615f838
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions clang/test/Format/line-ranges.cpp
Expand Up @@ -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
6 changes: 5 additions & 1 deletion clang/tools/clang-format/ClangFormat.cpp
Expand Up @@ -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);
Expand Down

0 comments on commit 615f838

Please sign in to comment.