Skip to content

Commit e75f054

Browse files
authored
[lld][MachO] Silence warnings about --read-workers parsing (#156608)
The parsing of the --read-workers argument v is implemented like this: unsigned threads = 0 if (!llvm::to_integer(v, threads, 0) || threads < 0) { ... As reported by a compiler warning, the value of the "threads < 0" expession is never going to be true. It could only evaluate to true if v represents a negative number, but in this case llvm::to_integer returns false since threads is unsigned and hence the second operand of the || operator will not be evaluated. This patch removes the useless || operand to silence compiler warnings. Since I had to first find out if --read-workers=0 is valid or not (it is), I also added a test to document the valid values for the option and I adjusted the error message on invalid values to clearly state that 0 is valid.
1 parent 527c8ff commit e75f054

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lld/MachO/Driver.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,11 +1833,12 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
18331833

18341834
if (auto *arg = args.getLastArg(OPT_read_workers)) {
18351835
StringRef v(arg->getValue());
1836-
unsigned threads = 0;
1837-
if (!llvm::to_integer(v, threads, 0) || threads < 0)
1838-
error(arg->getSpelling() + ": expected a positive integer, but got '" +
1839-
arg->getValue() + "'");
1840-
config->readWorkers = threads;
1836+
unsigned workers = 0;
1837+
if (!llvm::to_integer(v, workers, 0))
1838+
error(arg->getSpelling() +
1839+
": expected a non-negative integer, but got '" + arg->getValue() +
1840+
"'");
1841+
config->readWorkers = workers;
18411842
}
18421843
if (auto *arg = args.getLastArg(OPT_threads_eq)) {
18431844
StringRef v(arg->getValue());

lld/test/MachO/read-workers.s

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.o
3+
4+
## A non-negative integer is allowed.
5+
# RUN: %lld --read-workers=0 %t.o -o /dev/null
6+
# RUN: %lld --read-workers=1 %t.o -o /dev/null
7+
# RUN: %lld --read-workers=2 %t.o -o /dev/null
8+
9+
# RUN: not %lld --read-workers=all %t.o -o /dev/null 2>&1 | FileCheck %s -DN=all
10+
# RUN: not %lld --read-workers=-1 %t.o -o /dev/null 2>&1 | FileCheck %s -DN=-1
11+
12+
# CHECK: error: --read-workers=: expected a non-negative integer, but got '[[N]]'
13+
14+
.globl _main
15+
_main:
16+
ret

0 commit comments

Comments
 (0)