[lld] [lld] Use StringRef idioms (NFC) (PR #109584)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 22 10:54:02 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/109584

None

>From edfc85618492f0654bac31435bc956e69734d66c Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 22 Sep 2024 10:33:52 -0700
Subject: [PATCH] [lld] Use StringRef idioms (NFC)

---
 lld/COFF/Driver.cpp             | 3 +--
 lld/Common/DriverDispatcher.cpp | 3 +--
 lld/ELF/Driver.cpp              | 3 +--
 lld/MachO/InputSection.cpp      | 3 +--
 4 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 1b94f10acf80e5..f66fe3cab5a2f0 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -113,9 +113,8 @@ static std::string getOutputPath(StringRef path, bool isDll, bool isDriver) {
 
 // Returns true if S matches /crtend.?\.o$/.
 static bool isCrtend(StringRef s) {
-  if (!s.ends_with(".o"))
+  if (!s.consume_back(".o"))
     return false;
-  s = s.drop_back(2);
   if (s.ends_with("crtend"))
     return true;
   return !s.empty() && s.drop_back().ends_with("crtend");
diff --git a/lld/Common/DriverDispatcher.cpp b/lld/Common/DriverDispatcher.cpp
index f5c8bcdef4e0f0..fe18c320983fa2 100644
--- a/lld/Common/DriverDispatcher.cpp
+++ b/lld/Common/DriverDispatcher.cpp
@@ -113,8 +113,7 @@ parseFlavorWithoutMinGW(llvm::SmallVectorImpl<const char *> &argsV) {
 
   // Deduct the flavor from argv[0].
   StringRef arg0 = path::filename(argsV[0]);
-  if (arg0.ends_with_insensitive(".exe"))
-    arg0 = arg0.drop_back(4);
+  arg0.consume_back_insensitive(".exe");
   Flavor f = parseProgname(arg0);
   if (f == Invalid) {
     err("lld is a generic driver.\n"
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 6c6e1535bc8626..f3acb67fa045f3 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -867,8 +867,7 @@ static StripPolicy getStrip(opt::InputArgList &args) {
 static uint64_t parseSectionAddress(StringRef s, opt::InputArgList &args,
                                     const opt::Arg &arg) {
   uint64_t va = 0;
-  if (s.starts_with("0x"))
-    s = s.drop_front(2);
+  s.consume_front("0x");
   if (!to_integer(s, va, 16))
     error("invalid argument: " + arg.getAsString(args));
   return va;
diff --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index 64c584920defba..c1b3297f321f1e 100644
--- a/lld/MachO/InputSection.cpp
+++ b/lld/MachO/InputSection.cpp
@@ -167,8 +167,7 @@ std::string InputSection::getSourceLocation(uint64_t off) const {
     // Symbols are generally prefixed with an underscore, which is not included
     // in the debug information.
     StringRef symName = sym->getName();
-    if (!symName.empty() && symName[0] == '_')
-      symName = symName.substr(1);
+    symName.consume_front("_");
 
     if (std::optional<std::pair<std::string, unsigned>> fileLine =
             dwarf->getVariableLoc(symName))



More information about the llvm-commits mailing list