[llvm] [llvm] Use StringRef::{starts_with,find} (NFC) (PR #139661)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Mon May 12 19:46:31 PDT 2025


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

Calling find/contains in the StringRef domain allows us to avoid
creating temporary instances of std::string.


>From 7772c55e228763c091c175b37bc9b4bc5c194ddf Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 12 May 2025 19:33:32 -0700
Subject: [PATCH] [llvm] Use StringRef::{starts_with,find} (NFC)

Calling find/contains in the StringRef domain allows us to avoid
creating temporary instances of std::string.
---
 llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp | 2 +-
 llvm/lib/TableGen/Record.cpp                 | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
index 22137ea172240..7a0256f10ea60 100644
--- a/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
+++ b/llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp
@@ -780,7 +780,7 @@ llvm::Error DwarfTransformer::verify(StringRef GsymPath,
           const auto &dii = DwarfInlineInfos.getFrame(Idx);
           gsymFilename = LR->getSourceFile(Idx);
           // Verify function name
-          if (dii.FunctionName.find(gii.Name.str()) != 0)
+          if (!StringRef(dii.FunctionName).starts_with(gii.Name))
             Out << "error: address " << HEX64(Addr) << " DWARF function \""
                 << dii.FunctionName.c_str()
                 << "\" doesn't match GSYM function \"" << gii.Name << "\"\n";
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index 51ed259132497..0d9fcb0e63dae 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -1796,11 +1796,11 @@ const Init *TernOpInit::Fold(const Record *CurRec) const {
     if (LHSs && MHSs && RHSs) {
       std::string Val = RHSs->getValue().str();
 
-      std::string::size_type found;
-      std::string::size_type idx = 0;
+      StringRef::size_type found;
+      StringRef::size_type idx = 0;
       while (true) {
-        found = Val.find(LHSs->getValue().str(), idx);
-        if (found == std::string::npos)
+        found = StringRef(Val).find(LHSs->getValue(), idx);
+        if (found == StringRef::npos)
           break;
         Val.replace(found, LHSs->getValue().size(), MHSs->getValue().str());
         idx = found + MHSs->getValue().size();



More information about the llvm-commits mailing list