Skip to content

Commit da283b5

Browse files
committed
Revert "[clang-format] Fix an off-by-1 bug with -length option (#143302)"
This reverts commit 1fae591 because it may break VSCode. Closes #146036
1 parent c06d3a7 commit da283b5

File tree

3 files changed

+5
-18
lines changed

3 files changed

+5
-18
lines changed

clang/test/Format/multiple-inputs-error.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: cp %s %t-1.cpp
22
// RUN: cp %s %t-2.cpp
3-
// RUN: not clang-format 2>&1 >/dev/null -offset=1 -length=1 %t-1.cpp %t-2.cpp |FileCheck %s
3+
// RUN: not clang-format 2>&1 >/dev/null -offset=1 -length=0 %t-1.cpp %t-2.cpp |FileCheck %s
44
// RUN: not clang-format 2>&1 >/dev/null -lines=1:1 %t-1.cpp %t-2.cpp |FileCheck %s -check-prefix=CHECK-LINE
55
// CHECK: error: -offset, -length and -lines can only be used for single file.
66
// CHECK-LINE: error: -offset, -length and -lines can only be used for single file.

clang/test/Format/ranges.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: grep -Ev "// *[A-Z-]+:" %s \
2-
// RUN: | clang-format -style=LLVM -offset=2 -length=1 -offset=28 -length=1 -offset=35 -length=8 \
2+
// RUN: | clang-format -style=LLVM -offset=2 -length=0 -offset=28 -length=0 \
33
// RUN: | FileCheck -strict-whitespace %s
44
// CHECK: {{^int\ \*i;$}}
55
int*i;
@@ -9,12 +9,3 @@ int * i;
99

1010
// CHECK: {{^int\ \*i;$}}
1111
int * i;
12-
13-
// CHECK: int I;
14-
// CHECK-NEXT: int J ;
15-
int I ;
16-
int J ;
17-
18-
// RUN: not clang-format -length=0 %s 2>&1 \
19-
// RUN: | FileCheck -strict-whitespace -check-prefix=CHECK0 %s
20-
// CHECK0: error: length should be at least 1

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ static bool fillRanges(MemoryBuffer *Code,
284284
if (Offsets.size() == 1 && EmptyLengths) {
285285
Length = Sources.getFileOffset(Sources.getLocForEndOfFile(ID)) - Offsets[0];
286286
} else if (Offsets.size() != Lengths.size()) {
287-
errs() << "error: number of -offset and -length arguments must match\n";
287+
errs() << "error: number of -offset and -length arguments must match.\n";
288288
return true;
289289
}
290290
for (unsigned I = 0, E = Offsets.size(), CodeSize = Code->getBufferSize();
@@ -296,16 +296,12 @@ static bool fillRanges(MemoryBuffer *Code,
296296
}
297297
if (!EmptyLengths)
298298
Length = Lengths[I];
299-
if (Length == 0) {
300-
errs() << "error: length should be at least 1\n";
301-
return true;
302-
}
303299
if (Offset + Length > CodeSize) {
304300
errs() << "error: invalid length " << Length << ", offset + length ("
305-
<< Offset + Length << ") is outside the file\n";
301+
<< Offset + Length << ") is outside the file.\n";
306302
return true;
307303
}
308-
Ranges.push_back(tooling::Range(Offset, Length - 1));
304+
Ranges.push_back(tooling::Range(Offset, Length));
309305
}
310306
return false;
311307
}

0 commit comments

Comments
 (0)