[lld] r299519 - Simplify. NFC.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 4 22:40:22 PDT 2017


Author: ruiu
Date: Wed Apr  5 00:40:21 2017
New Revision: 299519

URL: http://llvm.org/viewvc/llvm-project?rev=299519&view=rev
Log:
Simplify. NFC.

Looks like we can use consume() in many more places.

Modified:
    lld/trunk/ELF/ScriptParser.cpp
    lld/trunk/test/ELF/invalid-linkerscript.test

Modified: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=299519&r1=299518&r2=299519&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp Wed Apr  5 00:40:21 2017
@@ -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::readOutputSectionDescripti
   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::readOutputSectionDescripti
     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, u
 }
 
 BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
-  int Size = StringSwitch<unsigned>(Tok)
+  int Size = StringSwitch<int>(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<SymbolVersion> ScriptParser:
 
 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;
   }

Modified: lld/trunk/test/ELF/invalid-linkerscript.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/invalid-linkerscript.test?rev=299519&r1=299518&r2=299519&view=diff
==============================================================================
--- lld/trunk/test/ELF/invalid-linkerscript.test (original)
+++ lld/trunk/test/ELF/invalid-linkerscript.test Wed Apr  5 00:40:21 2017
@@ -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:




More information about the llvm-commits mailing list