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

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


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld-elf

Author: Hongyu Chen (yugier)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/99539.diff


3 Files Affected:

- (modified) lld/ELF/ScriptLexer.cpp (+4-10) 
- (modified) lld/ELF/ScriptLexer.h (+1-2) 
- (modified) lld/ELF/ScriptParser.cpp (+3-3) 


``````````diff
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) {

``````````

</details>


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


More information about the llvm-commits mailing list