From 5424e7c7dc504a5cb12cb71a8b6330471f4e0da6 Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Mon, 17 Oct 2016 06:21:13 +0000 Subject: [PATCH] ELF: Add a skip() overload to ignore any token 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 | 18 +++++++++--------- lld/ELF/ScriptParser.cpp | 10 ++++++++++ lld/ELF/ScriptParser.h | 1 + 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 54d5a9b..061f7f8 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -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(";"); } diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp index 97d68d9..63c8d5a 100644 --- a/lld/ELF/ScriptParser.cpp +++ b/lld/ELF/ScriptParser.cpp @@ -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; diff --git a/lld/ELF/ScriptParser.h b/lld/ELF/ScriptParser.h index 1acd19d..64a91d4 100644 --- a/lld/ELF/ScriptParser.h +++ b/lld/ELF/ScriptParser.h @@ -29,6 +29,7 @@ protected: bool atEOF(); StringRef next(); StringRef peek(); + void skip(); bool skip(StringRef Tok); void expect(StringRef Expect); -- 2.7.4