Skip to content

Commit

Permalink
[ELF] Fix precedence of ? when there are 2 or more operators on the l…
Browse files Browse the repository at this point in the history
…eft hand side

For 1 != 1 <= 1 ? 1 : 2, the current code incorrectly considers that ?
has a higher precedence than != (minPrec).

Also, add a test for right associativity.
  • Loading branch information
MaskRay committed Jun 25, 2022
1 parent d479b2e commit b0d6dd3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lld/ELF/ScriptParser.cpp
Expand Up @@ -645,6 +645,7 @@ static int precedence(StringRef op) {
.Case("|", 4)
.Case("&&", 3)
.Case("||", 2)
.Case("?", 1)
.Default(-1);
}

Expand Down Expand Up @@ -1128,11 +1129,11 @@ Expr ScriptParser::combine(StringRef op, Expr l, Expr r) {
Expr ScriptParser::readExpr1(Expr lhs, int minPrec) {
while (!atEOF() && !errorCount()) {
// Read an operator and an expression.
if (consume("?"))
return readTernary(lhs);
StringRef op1 = peek();
if (precedence(op1) < minPrec)
break;
if (consume("?"))
return readTernary(lhs);
skip();
Expr rhs = readPrimary();

Expand Down
6 changes: 3 additions & 3 deletions lld/test/ELF/linkerscript/operators.test
Expand Up @@ -13,14 +13,14 @@ SECTIONS {
nospace = 1+2*6/3;
braces = 1 + (2 + 3) * 4;
and = 0xbb & 0xee;
ternary1 = 1 ? 1 : 2;
ternary1 = 1 ? 2 : 3 ? 4 : 5;
ternary2 = 0 ? 1 : 2;
less = 1 < 0 ? 1 : 2;
lesseq = 1 <= 1 ? 1 : 2;
greater = 0 > 1 ? 1 : 2;
greatereq = 1 >= 1 ? 1 : 2;
eq = 1 == 1 ? 1 : 2;
neq = (1 != 1 <= 1) ? 1 : 2;
neq = 1 != 1 <= 1 ? 1 : 2;
plusassign = 1;
plusassign += 2;
unary = -1 + 3;
Expand Down Expand Up @@ -64,7 +64,7 @@ SECTIONS {
# CHECK-NEXT: 00000000000005 A nospace
# CHECK-NEXT: 00000000000015 A braces
# CHECK-NEXT: 000000000000aa A and
# CHECK-NEXT: 00000000000001 A ternary1
# CHECK-NEXT: 00000000000002 A ternary1
# CHECK-NEXT: 00000000000002 A ternary2
# CHECK-NEXT: 00000000000002 A less
# CHECK-NEXT: 00000000000001 A lesseq
Expand Down

0 comments on commit b0d6dd3

Please sign in to comment.