Skip to content

Commit

Permalink
[ELF] Delete peek2 in Lexer (#99790)
Browse files Browse the repository at this point in the history
Summary:
Thanks to Fangrui's change

28045ce
so peek2 can be removed.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251424
  • Loading branch information
yugier authored and yuxuanchen1997 committed Jul 25, 2024
1 parent a78e26c commit e75d91a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
9 changes: 0 additions & 9 deletions lld/ELF/ScriptLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,6 @@ StringRef ScriptLexer::peek() {
return tok;
}

StringRef ScriptLexer::peek2() {
skip();
StringRef tok = next();
if (errorCount())
return "";
pos = pos - 2;
return tok;
}

bool ScriptLexer::consume(StringRef tok) {
if (next() == tok)
return true;
Expand Down
1 change: 0 additions & 1 deletion lld/ELF/ScriptLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class ScriptLexer {
bool atEOF();
StringRef next();
StringRef peek();
StringRef peek2();
void skip();
bool consume(StringRef tok);
void expect(StringRef expect);
Expand Down
28 changes: 14 additions & 14 deletions lld/ELF/ScriptParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ScriptParser final : ScriptLexer {
SymbolAssignment *readSymbolAssignment(StringRef name);
ByteCommand *readByteCommand(StringRef tok);
std::array<uint8_t, 4> readFill();
bool readSectionDirective(OutputSection *cmd, StringRef tok2);
bool readSectionDirective(OutputSection *cmd, StringRef tok);
void readSectionAddressType(OutputSection *cmd);
OutputDesc *readOverlaySectionDescription();
OutputDesc *readOutputSectionDescription(StringRef outSec);
Expand Down Expand Up @@ -873,14 +873,11 @@ constexpr std::pair<const char *, unsigned> typeMap[] = {
// Tries to read the special directive for an output section definition which
// can be one of following: "(NOLOAD)", "(COPY)", "(INFO)", "(OVERLAY)", and
// "(TYPE=<value>)".
// Tok1 and Tok2 are next 2 tokens peeked. See comment for
// readSectionAddressType below.
bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok2) {
if (tok2 != "NOLOAD" && tok2 != "COPY" && tok2 != "INFO" &&
tok2 != "OVERLAY" && tok2 != "TYPE")
bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok) {
if (tok != "NOLOAD" && tok != "COPY" && tok != "INFO" && tok != "OVERLAY" &&
tok != "TYPE")
return false;

expect("(");
if (consume("NOLOAD")) {
cmd->type = SHT_NOBITS;
cmd->typeIsSet = true;
Expand Down Expand Up @@ -919,19 +916,22 @@ bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok2) {
// https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
// https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
void ScriptParser::readSectionAddressType(OutputSection *cmd) {
if (peek() == "(") {
if (consume("(")) {
// Temporarily set inExpr to support TYPE=<value> without spaces.
SaveAndRestore saved(inExpr, true);
if (readSectionDirective(cmd, peek2()))
if (readSectionDirective(cmd, peek()))
return;
cmd->addrExpr = readExpr();
expect(")");
} else {
cmd->addrExpr = readExpr();
}
cmd->addrExpr = readExpr();

if (peek() == "(") {
if (consume("(")) {
SaveAndRestore saved(inExpr, true);
StringRef tok2 = peek2();
if (!readSectionDirective(cmd, tok2))
setError("unknown section directive: " + tok2);
StringRef tok = peek();
if (!readSectionDirective(cmd, tok))
setError("unknown section directive: " + tok);
}
}

Expand Down

0 comments on commit e75d91a

Please sign in to comment.