diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp index 4df4462c1d423..bd9807c7f3cb5 100644 --- a/lld/ELF/ScriptParser.cpp +++ b/lld/ELF/ScriptParser.cpp @@ -645,6 +645,7 @@ static int precedence(StringRef op) { .Case("|", 4) .Case("&&", 3) .Case("||", 2) + .Case("?", 1) .Default(-1); } @@ -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(); diff --git a/lld/test/ELF/linkerscript/operators.test b/lld/test/ELF/linkerscript/operators.test index b096df50f8250..cbccd45f09224 100644 --- a/lld/test/ELF/linkerscript/operators.test +++ b/lld/test/ELF/linkerscript/operators.test @@ -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; @@ -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