[lld] [ELF] Updated some while conditions with till (PR #100893)

Hongyu Chen via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 27 12:47:14 PDT 2024


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

This change is based on [commit](https://github.com/llvm/llvm-project/commit/b32c38ab5b4cf5c66469180ba3594e98eff2c124) for a cleaner API usage. Thanks to @MaskRay !

>From c240ae2b90b96613fa6da777d72169b39a4b21db Mon Sep 17 00:00:00 2001
From: Hongyu Chen <46539970+yugier at users.noreply.github.com>
Date: Fri, 26 Jul 2024 23:31:00 -0700
Subject: [PATCH 1/3] [ELF] Updated while with till

---
 lld/ELF/ScriptLexer.cpp  |  9 +++++++++
 lld/ELF/ScriptLexer.h    |  1 +
 lld/ELF/ScriptParser.cpp | 10 ++++++----
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/lld/ELF/ScriptLexer.cpp b/lld/ELF/ScriptLexer.cpp
index 51282dba53c83..fdf5f85016d41 100644
--- a/lld/ELF/ScriptLexer.cpp
+++ b/lld/ELF/ScriptLexer.cpp
@@ -234,6 +234,15 @@ void ScriptLexer::expect(StringRef expect) {
   }
 }
 
+ScriptLexer::Token ScriptLexer::nextTok() {
+  StringRef str = next();
+  if (!atEOF())
+    return {str};
+  prevTok = {};
+  setError("Unexpected EOF");
+  return {};
+}
+
 ScriptLexer::Token ScriptLexer::till(StringRef tok) {
   StringRef str = next();
   if (str == tok)
diff --git a/lld/ELF/ScriptLexer.h b/lld/ELF/ScriptLexer.h
index bc2b5fee618f7..9f1b891ba857f 100644
--- a/lld/ELF/ScriptLexer.h
+++ b/lld/ELF/ScriptLexer.h
@@ -61,6 +61,7 @@ class ScriptLexer {
   bool consume(StringRef tok);
   void expect(StringRef expect);
   Token till(StringRef tok);
+  Token nextTok();
   std::string getCurrentLocation();
   MemoryBufferRef getCurrentMB();
 
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index a79a0b34892fc..30d5edd258a1d 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -957,13 +957,15 @@ OutputDesc *ScriptParser::readOverlaySectionDescription() {
   OutputDesc *osd = script->createOutputSection(next(), getCurrentLocation());
   osd->osec.inOverlay = true;
   expect("{");
-  while (!errorCount() && !consume("}")) {
+  while (auto tok = till("}")) {
     uint64_t withFlags = 0;
     uint64_t withoutFlags = 0;
-    if (consume("INPUT_SECTION_FLAGS"))
+    if (tok == "INPUT_SECTION_FLAGS") {
       std::tie(withFlags, withoutFlags) = readInputSectionFlags();
+      tok = nextTok();
+    }
     osd->osec.commands.push_back(
-        readInputSectionRules(next(), withFlags, withoutFlags));
+        readInputSectionRules(tok, withFlags, withoutFlags));
   }
   osd->osec.phdrs = readOutputSectionPhdrs();
   return osd;
@@ -1090,7 +1092,7 @@ SymbolAssignment *ScriptParser::readProvideHidden(bool provide, bool hidden) {
   StringRef name = next(), eq = peek();
   if (eq != "=") {
     setError("= expected, but got " + next());
-    while (!atEOF() && next() != ")")
+    while (till(")"))
       ;
     return nullptr;
   }

>From 20fa4b3f86c356697bd587bcaf0815f299f08c29 Mon Sep 17 00:00:00 2001
From: Hongyu Chen <46539970+yugier at users.noreply.github.com>
Date: Sat, 27 Jul 2024 11:17:13 -0700
Subject: [PATCH 2/3] [ELF] Remove nextTok() from last commit

---
 lld/ELF/ScriptLexer.cpp  | 9 ---------
 lld/ELF/ScriptLexer.h    | 1 -
 lld/ELF/ScriptParser.cpp | 2 +-
 3 files changed, 1 insertion(+), 11 deletions(-)

diff --git a/lld/ELF/ScriptLexer.cpp b/lld/ELF/ScriptLexer.cpp
index fdf5f85016d41..51282dba53c83 100644
--- a/lld/ELF/ScriptLexer.cpp
+++ b/lld/ELF/ScriptLexer.cpp
@@ -234,15 +234,6 @@ void ScriptLexer::expect(StringRef expect) {
   }
 }
 
-ScriptLexer::Token ScriptLexer::nextTok() {
-  StringRef str = next();
-  if (!atEOF())
-    return {str};
-  prevTok = {};
-  setError("Unexpected EOF");
-  return {};
-}
-
 ScriptLexer::Token ScriptLexer::till(StringRef tok) {
   StringRef str = next();
   if (str == tok)
diff --git a/lld/ELF/ScriptLexer.h b/lld/ELF/ScriptLexer.h
index 9f1b891ba857f..bc2b5fee618f7 100644
--- a/lld/ELF/ScriptLexer.h
+++ b/lld/ELF/ScriptLexer.h
@@ -61,7 +61,6 @@ class ScriptLexer {
   bool consume(StringRef tok);
   void expect(StringRef expect);
   Token till(StringRef tok);
-  Token nextTok();
   std::string getCurrentLocation();
   MemoryBufferRef getCurrentMB();
 
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 30d5edd258a1d..35e2be5be6d4b 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -962,7 +962,7 @@ OutputDesc *ScriptParser::readOverlaySectionDescription() {
     uint64_t withoutFlags = 0;
     if (tok == "INPUT_SECTION_FLAGS") {
       std::tie(withFlags, withoutFlags) = readInputSectionFlags();
-      tok = nextTok();
+      tok = till("");
     }
     osd->osec.commands.push_back(
         readInputSectionRules(tok, withFlags, withoutFlags));

>From d6968edc695ce6559afd2d6ff125580ac0e79884 Mon Sep 17 00:00:00 2001
From: Hongyu Chen <46539970+yugier at users.noreply.github.com>
Date: Sat, 27 Jul 2024 12:39:55 -0700
Subject: [PATCH 3/3] [ELF] Used till for while condition in readSymbols

---
 lld/ELF/ScriptParser.cpp | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 35e2be5be6d4b..b80db28764fd3 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -1733,15 +1733,11 @@ ScriptParser::readSymbols() {
   SmallVector<SymbolVersion, 0> globals;
   SmallVector<SymbolVersion, 0> *v = &globals;
 
-  while (!errorCount()) {
-    if (consume("}"))
-      break;
-
-    if (consume("extern")) {
+  while (auto tok = till("}")) {
+    if (tok == "extern") {
       SmallVector<SymbolVersion, 0> ext = readVersionExtern();
       v->insert(v->end(), ext.begin(), ext.end());
     } else {
-      StringRef tok = next();
       if (tok == "local:" || (tok == "local" && consume(":"))) {
         v = &locals;
         continue;



More information about the llvm-commits mailing list