[lld] [lld][ELF] remove `consumeLabel` in ScriptLexer (PR #99567)

Hongyu Chen via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 23 15:50:41 PDT 2024


https://github.com/yugier updated https://github.com/llvm/llvm-project/pull/99567

>From 7b33db99581a3a9fcf60bf2b67ba8e6bd6092d7c Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyuchy at google.com>
Date: Thu, 18 Jul 2024 19:19:55 +0000
Subject: [PATCH 1/3] [lld][ELF] remove `consumeLabel` in ScriptLexer

This commit removes `consumeLabel` since we can just use consume
function to have the same functionalities.
---
 lld/ELF/ScriptLexer.cpp  | 12 ------------
 lld/ELF/ScriptLexer.h    |  1 -
 lld/ELF/ScriptParser.cpp |  4 ++--
 3 files changed, 2 insertions(+), 15 deletions(-)

diff --git a/lld/ELF/ScriptLexer.cpp b/lld/ELF/ScriptLexer.cpp
index 14f39ed10e17c..5ac1c15043ce6 100644
--- a/lld/ELF/ScriptLexer.cpp
+++ b/lld/ELF/ScriptLexer.cpp
@@ -289,18 +289,6 @@ bool ScriptLexer::consume(StringRef tok) {
   return false;
 }
 
-// Consumes Tok followed by ":". Space is allowed between Tok and ":".
-bool ScriptLexer::consumeLabel(StringRef tok) {
-  if (consume((tok + ":").str()))
-    return true;
-  if (tokens.size() >= pos + 2 && tokens[pos] == tok &&
-      tokens[pos + 1] == ":") {
-    pos += 2;
-    return true;
-  }
-  return false;
-}
-
 void ScriptLexer::skip() { (void)next(); }
 
 void ScriptLexer::expect(StringRef expect) {
diff --git a/lld/ELF/ScriptLexer.h b/lld/ELF/ScriptLexer.h
index 7919e493fa28b..b4d2ab8627a91 100644
--- a/lld/ELF/ScriptLexer.h
+++ b/lld/ELF/ScriptLexer.h
@@ -30,7 +30,6 @@ class ScriptLexer {
   void skip();
   bool consume(StringRef tok);
   void expect(StringRef expect);
-  bool consumeLabel(StringRef tok);
   std::string getCurrentLocation();
   MemoryBufferRef getCurrentMB();
 
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index db46263115242..830c4f315a5a8 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -1692,11 +1692,11 @@ ScriptParser::readSymbols() {
   while (!errorCount()) {
     if (consume("}"))
       break;
-    if (consumeLabel("local")) {
+    if (consume("local:") || (consume("local") && consume(":"))) {
       v = &locals;
       continue;
     }
-    if (consumeLabel("global")) {
+    if (consume("global:") || (consume("global") && consume(":"))) {
       v = &globals;
       continue;
     }

>From 6ec9adf7f5f06e80495b41df053e4aa401c4ce32 Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyuchy at google.com>
Date: Sun, 21 Jul 2024 05:58:45 +0000
Subject: [PATCH 2/3] [ELF] Moved consume in readSymbols function

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

diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index bb075067cc4f3..ec34238c9df7d 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -1719,19 +1719,20 @@ ScriptParser::readSymbols() {
   while (!errorCount()) {
     if (consume("}"))
       break;
-    if (consume("local:") || (consume("local") && consume(":"))) {
-      v = &locals;
-      continue;
-    }
-    if (consume("global:") || (consume("global") && consume(":"))) {
-      v = &globals;
-      continue;
-    }
 
     if (consume("extern")) {
       SmallVector<SymbolVersion, 0> ext = readVersionExtern();
       v->insert(v->end(), ext.begin(), ext.end());
     } else {
+      if (consume("local:") || (consume("local") && consume(":"))) {
+        v = &locals;
+        continue;
+      }
+      if (consume("global:") || (consume("global") && consume(":"))) {
+        v = &globals;
+        continue;
+      }
+
       StringRef tok = next();
       v->push_back({unquote(tok), false, hasWildcard(tok)});
     }

>From 31a3cd6f061d2bcc11d9799b4fbf04e31d493e66 Mon Sep 17 00:00:00 2001
From: Hongyu Chen <hongyuchy at google.com>
Date: Tue, 23 Jul 2024 22:49:25 +0000
Subject: [PATCH 3/3] [ELF] Updated check location for "local" and "global" for
 resolving potential bugs

---
 lld/ELF/ScriptParser.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index ec34238c9df7d..8637a8b0b2167 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -1724,16 +1724,15 @@ ScriptParser::readSymbols() {
       SmallVector<SymbolVersion, 0> ext = readVersionExtern();
       v->insert(v->end(), ext.begin(), ext.end());
     } else {
-      if (consume("local:") || (consume("local") && consume(":"))) {
+      StringRef tok = next();
+      if (tok == "local:" || (tok == "local" && consume(":"))) {
         v = &locals;
         continue;
       }
-      if (consume("global:") || (consume("global") && consume(":"))) {
+      if (tok == "global:" || (tok == "global" && consume(":"))) {
         v = &globals;
         continue;
       }
-
-      StringRef tok = next();
       v->push_back({unquote(tok), false, hasWildcard(tok)});
     }
     expect(";");



More information about the llvm-commits mailing list