[lld] b0d6dd3 - [ELF] Fix precedence of ? when there are 2 or more operators on the left hand side
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 25 13:48:57 PDT 2022
Author: Fangrui Song
Date: 2022-06-25T13:48:52-07:00
New Revision: b0d6dd3905db145853c7c744ac92d49b00b1fa20
URL: https://github.com/llvm/llvm-project/commit/b0d6dd3905db145853c7c744ac92d49b00b1fa20
DIFF: https://github.com/llvm/llvm-project/commit/b0d6dd3905db145853c7c744ac92d49b00b1fa20.diff
LOG: [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.
Added:
Modified:
lld/ELF/ScriptParser.cpp
lld/test/ELF/linkerscript/operators.test
Removed:
################################################################################
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 4df4462c1d42..bd9807c7f3cb 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 b096df50f825..cbccd45f0922 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
More information about the llvm-commits
mailing list