[lld] [lld][ELF] removed peek2 in linker script lexer (PR #99539)

Hongyu Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 18 11:14:00 PDT 2024


https://github.com/yugier created https://github.com/llvm/llvm-project/pull/99539

This removes peek2 in lexer and updates peek for a default value ll2 = false unless we need to look ahead 2 tokens in readSectionAddressType.

>From d2788c14dbb94162daceb038a95827dedd1717d7 Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyuchy at google.com>
Date: Thu, 18 Jul 2024 18:07:25 +0000
Subject: [PATCH] [lld][ELF] removed peek2 in linker script lexer

This removes peek2 in lexer and updates peek for a default value ll2 =
false unless we need to look ahead 2 tokens in readSectionAddressType.
---
 lld/ELF/ScriptLexer.cpp  | 14 ++++----------
 lld/ELF/ScriptLexer.h    |  3 +--
 lld/ELF/ScriptParser.cpp |  6 +++---
 3 files changed, 8 insertions(+), 15 deletions(-)

diff --git a/lld/ELF/ScriptLexer.cpp b/lld/ELF/ScriptLexer.cpp
index 14f39ed10e17c..d4f1b11f458fb 100644
--- a/lld/ELF/ScriptLexer.cpp
+++ b/lld/ELF/ScriptLexer.cpp
@@ -264,20 +264,14 @@ StringRef ScriptLexer::next() {
   return tokens[pos++];
 }
 
-StringRef ScriptLexer::peek() {
-  StringRef tok = next();
-  if (errorCount())
-    return "";
-  pos = pos - 1;
-  return tok;
-}
+StringRef ScriptLexer::peek(bool ll2) {
+  if (ll2)
+    skip();
 
-StringRef ScriptLexer::peek2() {
-  skip();
   StringRef tok = next();
   if (errorCount())
     return "";
-  pos = pos - 2;
+  pos = ll2 ? pos - 2 : pos - 1;
   return tok;
 }
 
diff --git a/lld/ELF/ScriptLexer.h b/lld/ELF/ScriptLexer.h
index 7919e493fa28b..7a4c1a5d14576 100644
--- a/lld/ELF/ScriptLexer.h
+++ b/lld/ELF/ScriptLexer.h
@@ -25,8 +25,7 @@ class ScriptLexer {
   StringRef skipSpace(StringRef s);
   bool atEOF();
   StringRef next();
-  StringRef peek();
-  StringRef peek2();
+  StringRef peek(bool ll2 = false);
   void skip();
   bool consume(StringRef tok);
   void expect(StringRef expect);
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index db46263115242..75a0b6b00888e 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -898,14 +898,14 @@ bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok1, Stri
 void ScriptParser::readSectionAddressType(OutputSection *cmd) {
   // Temporarily set inExpr to support TYPE=<value> without spaces.
   bool saved = std::exchange(inExpr, true);
-  bool isDirective = readSectionDirective(cmd, peek(), peek2());
+  bool isDirective = readSectionDirective(cmd, peek(), peek(/*ll2=*/true));
   inExpr = saved;
   if (isDirective)
     return;
 
   cmd->addrExpr = readExpr();
-  if (peek() == "(" && !readSectionDirective(cmd, "(", peek2()))
-    setError("unknown section directive: " + peek2());
+  if (peek() == "(" && !readSectionDirective(cmd, "(", peek(/*ll2=*/true)))
+    setError("unknown section directive: " + peek(/*ll2=*/true));
 }
 
 static Expr checkAlignment(Expr e, std::string &loc) {



More information about the llvm-commits mailing list