Skip to content

Commit b0d6dd3

Browse files
committed
[ELF] Fix precedence of ? when there are 2 or more operators on the left 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.
1 parent d479b2e commit b0d6dd3

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

lld/ELF/ScriptParser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ static int precedence(StringRef op) {
645645
.Case("|", 4)
646646
.Case("&&", 3)
647647
.Case("||", 2)
648+
.Case("?", 1)
648649
.Default(-1);
649650
}
650651

@@ -1128,11 +1129,11 @@ Expr ScriptParser::combine(StringRef op, Expr l, Expr r) {
11281129
Expr ScriptParser::readExpr1(Expr lhs, int minPrec) {
11291130
while (!atEOF() && !errorCount()) {
11301131
// Read an operator and an expression.
1131-
if (consume("?"))
1132-
return readTernary(lhs);
11331132
StringRef op1 = peek();
11341133
if (precedence(op1) < minPrec)
11351134
break;
1135+
if (consume("?"))
1136+
return readTernary(lhs);
11361137
skip();
11371138
Expr rhs = readPrimary();
11381139

lld/test/ELF/linkerscript/operators.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ SECTIONS {
1313
nospace = 1+2*6/3;
1414
braces = 1 + (2 + 3) * 4;
1515
and = 0xbb & 0xee;
16-
ternary1 = 1 ? 1 : 2;
16+
ternary1 = 1 ? 2 : 3 ? 4 : 5;
1717
ternary2 = 0 ? 1 : 2;
1818
less = 1 < 0 ? 1 : 2;
1919
lesseq = 1 <= 1 ? 1 : 2;
2020
greater = 0 > 1 ? 1 : 2;
2121
greatereq = 1 >= 1 ? 1 : 2;
2222
eq = 1 == 1 ? 1 : 2;
23-
neq = (1 != 1 <= 1) ? 1 : 2;
23+
neq = 1 != 1 <= 1 ? 1 : 2;
2424
plusassign = 1;
2525
plusassign += 2;
2626
unary = -1 + 3;
@@ -64,7 +64,7 @@ SECTIONS {
6464
# CHECK-NEXT: 00000000000005 A nospace
6565
# CHECK-NEXT: 00000000000015 A braces
6666
# CHECK-NEXT: 000000000000aa A and
67-
# CHECK-NEXT: 00000000000001 A ternary1
67+
# CHECK-NEXT: 00000000000002 A ternary1
6868
# CHECK-NEXT: 00000000000002 A ternary2
6969
# CHECK-NEXT: 00000000000002 A less
7070
# CHECK-NEXT: 00000000000001 A lesseq

0 commit comments

Comments
 (0)