ELF: Add a skip() overload to ignore any token
authorJustin Bogner <mail@justinbogner.com>
Mon, 17 Oct 2016 06:21:13 +0000 (06:21 +0000)
committerJustin Bogner <mail@justinbogner.com>
Mon, 17 Oct 2016 06:21:13 +0000 (06:21 +0000)
Most functions that return StringRef should check their return values,
so I'm planning on marking StringRef [[nodiscard]]. This requires
splitting up functions like next() that are sometimes just used for
side effects.

llvm-svn: 284363

lld/ELF/LinkerScript.cpp
lld/ELF/ScriptParser.cpp
lld/ELF/ScriptParser.h

index 54d5a9b..061f7f8 100644 (file)
@@ -1106,14 +1106,14 @@ void ScriptParser::readOutput() {
 void ScriptParser::readOutputArch() {
   // Error checking only for now.
   expect("(");
-  next();
+  skip();
   expect(")");
 }
 
 void ScriptParser::readOutputFormat() {
   // Error checking only for now.
   expect("(");
-  next();
+  skip();
   StringRef Tok = next();
   if (Tok == ")")
     return;
@@ -1121,9 +1121,9 @@ void ScriptParser::readOutputFormat() {
     setError("unexpected token: " + Tok);
     return;
   }
-  next();
+  skip();
   expect(",");
-  next();
+  skip();
   expect(")");
 }
 
@@ -1495,7 +1495,7 @@ Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
       return readTernary(Lhs);
     if (precedence(Op1) < MinPrec)
       break;
-    next();
+    skip();
     Expr Rhs = readPrimary();
 
     // Evaluate the remaining part of the expression first if the
@@ -1623,7 +1623,7 @@ Expr ScriptParser::readPrimary() {
   }
   if (Tok == "SEGMENT_START") {
     expect("(");
-    next();
+    skip();
     expect(",");
     Expr E = readExpr();
     expect(")");
@@ -1678,7 +1678,7 @@ Expr ScriptParser::readPrimary() {
 }
 
 Expr ScriptParser::readTernary(Expr Cond) {
-  next();
+  skip();
   Expr L = readExpr();
   expect(":");
   Expr R = readExpr();
@@ -1748,7 +1748,7 @@ void ScriptParser::readVersionDeclaration(StringRef VerStr) {
   // version hierarchy is, probably against your instinct, purely for human; the
   // runtime doesn't care about them at all. In LLD, we simply skip the token.
   if (!VerStr.empty() && peek() != ";")
-    next();
+    skip();
   expect(";");
 }
 
@@ -1788,7 +1788,7 @@ void ScriptParser::readGlobal(StringRef VerStr) {
     StringRef Cur = peek();
     if (Cur == "}" || Cur == "local:" || Error)
       return;
-    next();
+    skip();
     Globals->push_back({unquote(Cur), false, hasWildcard(Cur)});
     expect(";");
   }
index 97d68d9..63c8d5a 100644 (file)
@@ -150,6 +150,16 @@ bool ScriptParserBase::skip(StringRef Tok) {
   return true;
 }
 
+void ScriptParserBase::skip() {
+  if (Error)
+    return;
+  if (atEOF()) {
+    setError("unexpected EOF");
+    return;
+  }
+  ++Pos;
+}
+
 void ScriptParserBase::expect(StringRef Expect) {
   if (Error)
     return;
index 1acd19d..64a91d4 100644 (file)
@@ -29,6 +29,7 @@ protected:
   bool atEOF();
   StringRef next();
   StringRef peek();
+  void skip();
   bool skip(StringRef Tok);
   void expect(StringRef Expect);