From b579c439c137db8ee25eeff18e9d20f6b910d319 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 5 Apr 2017 05:40:21 +0000 Subject: [PATCH] Simplify. NFC. Looks like we can use consume() in many more places. llvm-svn: 299519 --- lld/ELF/ScriptParser.cpp | 76 ++++++++++++++-------------------- lld/test/ELF/invalid-linkerscript.test | 2 +- 2 files changed, 33 insertions(+), 45 deletions(-) diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp index 405de28..4dc03fc 100644 --- a/lld/ELF/ScriptParser.cpp +++ b/lld/ELF/ScriptParser.cpp @@ -290,11 +290,10 @@ void ScriptParser::readExtern() { void ScriptParser::readGroup() { expect("("); while (!Error && !consume(")")) { - StringRef Tok = next(); - if (Tok == "AS_NEEDED") + if (consume("AS_NEEDED")) readAsNeeded(); else - addFile(unquote(Tok)); + addFile(unquote(next())); } } @@ -337,13 +336,9 @@ void ScriptParser::readOutputFormat() { // Error checking only for now. expect("("); skip(); - StringRef Tok = next(); - if (Tok == ")") + if (consume(")")) return; - if (Tok != ",") { - setError("unexpected token: " + Tok); - return; - } + expect(","); skip(); expect(","); skip(); @@ -353,31 +348,24 @@ void ScriptParser::readOutputFormat() { void ScriptParser::readPhdrs() { expect("{"); while (!Error && !consume("}")) { - StringRef Tok = next(); Script->Opt.PhdrsCommands.push_back( - {Tok, PT_NULL, false, false, UINT_MAX, nullptr}); - PhdrsCommand &PhdrCmd = Script->Opt.PhdrsCommands.back(); + {next(), PT_NULL, false, false, UINT_MAX, nullptr}); + PhdrsCommand &PhdrCmd = Script->Opt.PhdrsCommands.back(); PhdrCmd.Type = readPhdrType(); - do { - Tok = next(); - if (Tok == ";") - break; - if (Tok == "FILEHDR") + + while (!Error && !consume(";")) { + if (consume("FILEHDR")) PhdrCmd.HasFilehdr = true; - else if (Tok == "PHDRS") + else if (consume("PHDRS")) PhdrCmd.HasPhdrs = true; - else if (Tok == "AT") + else if (consume("AT")) PhdrCmd.LMAExpr = readParenExpr(); - else if (Tok == "FLAGS") { - expect("("); - // Passing 0 for the value of dot is a bit of a hack. It means that - // we accept expressions like ".|1". - PhdrCmd.Flags = readExpr()().getValue(); - expect(")"); - } else - setError("unexpected header attribute: " + Tok); - } while (!Error); + else if (consume("FLAGS")) + PhdrCmd.Flags = readParenExpr()().getValue(); + else + setError("unexpected header attribute: " + next()); + } } } @@ -391,6 +379,7 @@ void ScriptParser::readSearchDir() { void ScriptParser::readSections() { Script->Opt.HasSections = true; + // -no-rosegment is used to avoid placing read only non-executable sections in // their own segment. We do the same if SECTIONS command is present in linker // script. See comment for computeFlags(). @@ -539,6 +528,7 @@ Expr ScriptParser::readAssert() { expect(","); StringRef Msg = unquote(next()); expect(")"); + return [=] { if (!E().getValue()) error(Msg); @@ -564,7 +554,7 @@ ScriptParser::readOutputSectionDescription(StringRef OutSec) { Cmd->Location = getCurrentLocation(); // Read an address expression. - // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address + // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html if (peek() != ":") Cmd->AddrExpr = readExpr(); @@ -588,8 +578,8 @@ ScriptParser::readOutputSectionDescription(StringRef OutSec) { StringRef Tok = next(); if (Tok == ";") { // Empty commands are allowed. Do nothing here. - } else if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok)) { - Cmd->Commands.push_back(Assignment); + } else if (SymbolAssignment *Assign = readProvideOrAssignment(Tok)) { + Cmd->Commands.push_back(Assign); } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) { Cmd->Commands.push_back(Data); } else if (Tok == "ASSERT") { @@ -690,15 +680,14 @@ Expr ScriptParser::readExpr() { } static Expr combine(StringRef Op, Expr L, Expr R) { - if (Op == "*") - return [=] { return mul(L(), R()); }; - if (Op == "/") { - return [=] { return div(L(), R()); }; - } if (Op == "+") return [=] { return add(L(), R()); }; if (Op == "-") return [=] { return sub(L(), R()); }; + if (Op == "*") + return [=] { return mul(L(), R()); }; + if (Op == "/") + return [=] { return div(L(), R()); }; if (Op == "<<") return [=] { return L().getValue() << R().getValue(); }; if (Op == ">>") @@ -795,7 +784,7 @@ static bool readInteger(StringRef Tok, uint64_t &Result) { } BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) { - int Size = StringSwitch(Tok) + int Size = StringSwitch(Tok) .Case("BYTE", 1) .Case("SHORT", 2) .Case("LONG", 4) @@ -849,13 +838,12 @@ Expr ScriptParser::readPrimary() { if (Tok == "ALIGN") { expect("("); Expr E = readExpr(); - if (consume(",")) { - Expr E2 = readExpr(); - expect(")"); - return [=] { return alignTo(E().getValue(), E2().getValue()); }; - } + if (consume(")")) + return [=] { return alignTo(Script->getDot(), E().getValue()); }; + expect(","); + Expr E2 = readExpr(); expect(")"); - return [=] { return alignTo(Script->getDot(), E().getValue()); }; + return [=] { return alignTo(E().getValue(), E2().getValue()); }; } if (Tok == "ALIGNOF") { StringRef Name = readParenLiteral(); @@ -1092,7 +1080,7 @@ std::vector ScriptParser::readVersionExtern() { uint64_t ScriptParser::readMemoryAssignment(StringRef S1, StringRef S2, StringRef S3) { - if (!(consume(S1) || consume(S2) || consume(S3))) { + if (!consume(S1) && !consume(S2) && !consume(S3)) { setError("expected one of: " + S1 + ", " + S2 + ", or " + S3); return 0; } diff --git a/lld/test/ELF/invalid-linkerscript.test b/lld/test/ELF/invalid-linkerscript.test index 3316bec..280686e 100644 --- a/lld/test/ELF/invalid-linkerscript.test +++ b/lld/test/ELF/invalid-linkerscript.test @@ -50,5 +50,5 @@ # RUN: echo "OUTPUT_FORMAT(x y z)" > %t8 # RUN: not ld.lld %t8 no-such-file 2>&1 | FileCheck -check-prefix=ERR8 %s -# ERR8: unexpected token: y +# ERR8: , expected, but got y # ERR8: cannot open no-such-file: -- 2.7.4