Skip to content

Commit b828c13

Browse files
authored
[ELF] Delete peek2 in Lexer (#99790)
Thanks to Fangrui's change 28045ce so peek2 can be removed.
1 parent ba9b5ff commit b828c13

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

lld/ELF/ScriptLexer.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,6 @@ StringRef ScriptLexer::peek() {
272272
return tok;
273273
}
274274

275-
StringRef ScriptLexer::peek2() {
276-
skip();
277-
StringRef tok = next();
278-
if (errorCount())
279-
return "";
280-
pos = pos - 2;
281-
return tok;
282-
}
283-
284275
bool ScriptLexer::consume(StringRef tok) {
285276
if (next() == tok)
286277
return true;

lld/ELF/ScriptLexer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class ScriptLexer {
2626
bool atEOF();
2727
StringRef next();
2828
StringRef peek();
29-
StringRef peek2();
3029
void skip();
3130
bool consume(StringRef tok);
3231
void expect(StringRef expect);

lld/ELF/ScriptParser.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class ScriptParser final : ScriptLexer {
9292
SymbolAssignment *readSymbolAssignment(StringRef name);
9393
ByteCommand *readByteCommand(StringRef tok);
9494
std::array<uint8_t, 4> readFill();
95-
bool readSectionDirective(OutputSection *cmd, StringRef tok2);
95+
bool readSectionDirective(OutputSection *cmd, StringRef tok);
9696
void readSectionAddressType(OutputSection *cmd);
9797
OutputDesc *readOverlaySectionDescription();
9898
OutputDesc *readOutputSectionDescription(StringRef outSec);
@@ -873,14 +873,11 @@ constexpr std::pair<const char *, unsigned> typeMap[] = {
873873
// Tries to read the special directive for an output section definition which
874874
// can be one of following: "(NOLOAD)", "(COPY)", "(INFO)", "(OVERLAY)", and
875875
// "(TYPE=<value>)".
876-
// Tok1 and Tok2 are next 2 tokens peeked. See comment for
877-
// readSectionAddressType below.
878-
bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok2) {
879-
if (tok2 != "NOLOAD" && tok2 != "COPY" && tok2 != "INFO" &&
880-
tok2 != "OVERLAY" && tok2 != "TYPE")
876+
bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok) {
877+
if (tok != "NOLOAD" && tok != "COPY" && tok != "INFO" && tok != "OVERLAY" &&
878+
tok != "TYPE")
881879
return false;
882880

883-
expect("(");
884881
if (consume("NOLOAD")) {
885882
cmd->type = SHT_NOBITS;
886883
cmd->typeIsSet = true;
@@ -919,19 +916,22 @@ bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok2) {
919916
// https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
920917
// https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
921918
void ScriptParser::readSectionAddressType(OutputSection *cmd) {
922-
if (peek() == "(") {
919+
if (consume("(")) {
923920
// Temporarily set inExpr to support TYPE=<value> without spaces.
924921
SaveAndRestore saved(inExpr, true);
925-
if (readSectionDirective(cmd, peek2()))
922+
if (readSectionDirective(cmd, peek()))
926923
return;
924+
cmd->addrExpr = readExpr();
925+
expect(")");
926+
} else {
927+
cmd->addrExpr = readExpr();
927928
}
928-
cmd->addrExpr = readExpr();
929929

930-
if (peek() == "(") {
930+
if (consume("(")) {
931931
SaveAndRestore saved(inExpr, true);
932-
StringRef tok2 = peek2();
933-
if (!readSectionDirective(cmd, tok2))
934-
setError("unknown section directive: " + tok2);
932+
StringRef tok = peek();
933+
if (!readSectionDirective(cmd, tok))
934+
setError("unknown section directive: " + tok);
935935
}
936936
}
937937

0 commit comments

Comments
 (0)