Skip to content

[llvm] Prefer StringRef::substr(0, N) to slice(0, N) (NFC) #113793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kazutakahirata
Copy link
Contributor

I'm planning to migrate StringRef to std::string_view eventually.
Since std::string_view does not have slice, this patch migrates
slice(0, N) to substr(0, N).

I'm planning to migrate StringRef to std::string_view eventually.
Since std::string_view does not have slice, this patch migrates
slice(0, N) to substr(0, N).
@llvmbot
Copy link
Member

llvmbot commented Oct 27, 2024

@llvm/pr-subscribers-llvm-support
@llvm/pr-subscribers-backend-powerpc
@llvm/pr-subscribers-mc

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

I'm planning to migrate StringRef to std::string_view eventually.
Since std::string_view does not have slice, this patch migrates
slice(0, N) to substr(0, N).


Patch is 22.65 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/113793.diff

25 Files Affected:

  • (modified) llvm/include/llvm/ADT/StringRef.h (+2-2)
  • (modified) llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp (+2-2)
  • (modified) llvm/lib/Bitcode/Reader/MetadataLoader.cpp (+2-2)
  • (modified) llvm/lib/MC/MCParser/MasmParser.cpp (+1-1)
  • (modified) llvm/lib/ObjCopy/ELF/ELFObject.cpp (+1-1)
  • (modified) llvm/lib/Object/MachOObjectFile.cpp (+5-5)
  • (modified) llvm/lib/Option/OptTable.cpp (+1-1)
  • (modified) llvm/lib/Support/CachePruning.cpp (+1-1)
  • (modified) llvm/lib/Support/Regex.cpp (+1-1)
  • (modified) llvm/lib/Support/StringRef.cpp (+2-2)
  • (modified) llvm/lib/Support/VirtualFileSystem.cpp (+1-1)
  • (modified) llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp (+8-8)
  • (modified) llvm/lib/Target/Lanai/AsmParser/LanaiAsmParser.cpp (+1-1)
  • (modified) llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp (+1-1)
  • (modified) llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp (+2-2)
  • (modified) llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp (+1-1)
  • (modified) llvm/lib/TargetParser/RISCVISAInfo.cpp (+3-3)
  • (modified) llvm/tools/llvm-cxxdump/llvm-cxxdump.cpp (+1-1)
  • (modified) llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp (+1-1)
  • (modified) llvm/tools/llvm-objcopy/ObjcopyOptions.cpp (+2-2)
  • (modified) llvm/unittests/MC/DwarfLineTableHeaders.cpp (+1-1)
  • (modified) llvm/unittests/Support/YAMLIOTest.cpp (+1-1)
  • (modified) llvm/utils/TableGen/AsmMatcherEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/Common/CodeGenInstruction.cpp (+1-1)
  • (modified) llvm/utils/TableGen/OptionParserEmitter.cpp (+1-1)
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index f879bbf7164fd6..f056dae46d43fd 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -709,7 +709,7 @@ namespace llvm {
       size_t Idx = find(Separator);
       if (Idx == npos)
         return std::make_pair(*this, StringRef());
-      return std::make_pair(slice(0, Idx), substr(Idx + Separator.size()));
+      return std::make_pair(substr(0, Idx), substr(Idx + Separator.size()));
     }
 
     /// Split into two substrings around the last occurrence of a separator
@@ -727,7 +727,7 @@ namespace llvm {
       size_t Idx = rfind(Separator);
       if (Idx == npos)
         return std::make_pair(*this, StringRef());
-      return std::make_pair(slice(0, Idx), substr(Idx + Separator.size()));
+      return std::make_pair(substr(0, Idx), substr(Idx + Separator.size()));
     }
 
     /// Split into substrings around the occurrences of a separator string.
diff --git a/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp b/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp
index b7ed9cdf631454..5d0e5d63257ced 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp
@@ -537,7 +537,7 @@ Error BitcodeAnalyzer::decodeMetadataStringsBlob(StringRef Indent,
   unsigned StringsOffset = Record[1];
   OS << " num-strings = " << NumStrings << " {\n";
 
-  StringRef Lengths = Blob.slice(0, StringsOffset);
+  StringRef Lengths = Blob.substr(0, StringsOffset);
   SimpleBitstreamCursor R(Lengths);
   StringRef Strings = Blob.drop_front(StringsOffset);
   do {
@@ -551,7 +551,7 @@ Error BitcodeAnalyzer::decodeMetadataStringsBlob(StringRef Indent,
       return reportError("truncated chars");
 
     OS << Indent << "    '";
-    OS.write_escaped(Strings.slice(0, Size), /*hex=*/true);
+    OS.write_escaped(Strings.substr(0, Size), /*hex=*/true);
     OS << "'\n";
     Strings = Strings.drop_front(Size);
   } while (--NumStrings);
diff --git a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
index ada1597b399206..eebffb759d3e71 100644
--- a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
+++ b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
@@ -2299,7 +2299,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
   if (StringsOffset > Blob.size())
     return error("Invalid record: metadata strings corrupt offset");
 
-  StringRef Lengths = Blob.slice(0, StringsOffset);
+  StringRef Lengths = Blob.substr(0, StringsOffset);
   SimpleBitstreamCursor R(Lengths);
 
   StringRef Strings = Blob.drop_front(StringsOffset);
@@ -2313,7 +2313,7 @@ Error MetadataLoader::MetadataLoaderImpl::parseMetadataStrings(
     if (Strings.size() < Size)
       return error("Invalid record: metadata strings truncated chars");
 
-    CallBack(Strings.slice(0, Size));
+    CallBack(Strings.substr(0, Size));
     Strings = Strings.drop_front(Size);
   } while (--NumStrings);
 
diff --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index d88fd09a1aa07c..437436f94dc0a2 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -2886,7 +2886,7 @@ bool MasmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
     }
 
     // Add the prefix.
-    OS << Body.slice(0, Pos);
+    OS << Body.substr(0, Pos);
 
     // Check if we reached the end.
     if (Pos == End)
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index fd55f974115a24..5891ebd902b2ce 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -3001,7 +3001,7 @@ SRecord SRecord::getHeader(StringRef FileName) {
   // Header is a record with Type S0, Address 0, and Data that is a
   // vendor-specific text comment. For the comment we will use the output file
   // name truncated to 40 characters to match the behavior of GNU objcopy.
-  StringRef HeaderContents = FileName.slice(0, 40);
+  StringRef HeaderContents = FileName.substr(0, 40);
   ArrayRef<uint8_t> Data(
       reinterpret_cast<const uint8_t *>(HeaderContents.data()),
       HeaderContents.size());
diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp
index 46ed3b2ff7c581..4b33ec116af1c8 100644
--- a/llvm/lib/Object/MachOObjectFile.cpp
+++ b/llvm/lib/Object/MachOObjectFile.cpp
@@ -2445,7 +2445,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
     if (Suffix != "_debug" && Suffix != "_profile")
       Suffix = StringRef();
     else
-      Foo = Foo.slice(0, Idx);
+      Foo = Foo.substr(0, Idx);
   }
 
   // First look for the form Foo.framework/Foo
@@ -2520,7 +2520,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
   if (Lib.size() >= 3) {
     Dot = Lib.substr(Lib.size() - 2, 1);
     if (Dot == ".")
-      Lib = Lib.slice(0, Lib.size()-2);
+      Lib = Lib.substr(0, Lib.size() - 2);
   }
   return Lib;
 
@@ -2530,14 +2530,14 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
     return StringRef();
   b = Name.rfind('/', a);
   if (b == Name.npos)
-    Lib = Name.slice(0, a);
+    Lib = Name.substr(0, a);
   else
     Lib = Name.slice(b+1, a);
   // There are library names of the form: QT.A.qtx so check for these.
   if (Lib.size() >= 3) {
     Dot = Lib.substr(Lib.size() - 2, 1);
     if (Dot == ".")
-      Lib = Lib.slice(0, Lib.size()-2);
+      Lib = Lib.substr(0, Lib.size() - 2);
   }
   return Lib;
 }
@@ -5319,7 +5319,7 @@ bool MachOObjectFile::isRelocatableObject() const {
 Expected<std::unique_ptr<MachOObjectFile>> ObjectFile::createMachOObjectFile(
     MemoryBufferRef Buffer, uint32_t UniversalCputype, uint32_t UniversalIndex,
     size_t MachOFilesetEntryOffset) {
-  StringRef Magic = Buffer.getBuffer().slice(0, 4);
+  StringRef Magic = Buffer.getBuffer().substr(0, 4);
   if (Magic == "\xFE\xED\xFA\xCE")
     return MachOObjectFile::create(Buffer, false, false, UniversalCputype,
                                    UniversalIndex, MachOFilesetEntryOffset);
diff --git a/llvm/lib/Option/OptTable.cpp b/llvm/lib/Option/OptTable.cpp
index 9fdafed39b8b6c..6fb916095405db 100644
--- a/llvm/lib/Option/OptTable.cpp
+++ b/llvm/lib/Option/OptTable.cpp
@@ -160,7 +160,7 @@ static unsigned matchOption(const OptTable::Info *I, StringRef Str,
 static bool optionMatches(const OptTable::Info &In, StringRef Option) {
   for (auto Prefix : In.Prefixes)
     if (Option.ends_with(In.getName()))
-      if (Option.slice(0, Option.size() - In.getName().size()) == Prefix)
+      if (Option.substr(0, Option.size() - In.getName().size()) == Prefix)
         return true;
   return false;
 }
diff --git a/llvm/lib/Support/CachePruning.cpp b/llvm/lib/Support/CachePruning.cpp
index 4eae08b18c9b53..47d294e14f0812 100644
--- a/llvm/lib/Support/CachePruning.cpp
+++ b/llvm/lib/Support/CachePruning.cpp
@@ -54,7 +54,7 @@ static Expected<std::chrono::seconds> parseDuration(StringRef Duration) {
     return make_error<StringError>("Duration must not be empty",
                                    inconvertibleErrorCode());
 
-  StringRef NumStr = Duration.slice(0, Duration.size()-1);
+  StringRef NumStr = Duration.substr(0, Duration.size() - 1);
   uint64_t Num;
   if (NumStr.getAsInteger(0, Num))
     return make_error<StringError>("'" + NumStr + "' not an integer",
diff --git a/llvm/lib/Support/Regex.cpp b/llvm/lib/Support/Regex.cpp
index 5eedf95c48e378..7505d1c99572aa 100644
--- a/llvm/lib/Support/Regex.cpp
+++ b/llvm/lib/Support/Regex.cpp
@@ -206,7 +206,7 @@ std::string Regex::sub(StringRef Repl, StringRef String,
     case '0': case '1': case '2': case '3': case '4':
     case '5': case '6': case '7': case '8': case '9': {
       // Extract the backreference number.
-      StringRef Ref = Repl.slice(0, Repl.find_first_not_of("0123456789"));
+      StringRef Ref = Repl.substr(0, Repl.find_first_not_of("0123456789"));
       Repl = Repl.substr(Ref.size());
 
       unsigned RefValue;
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index 4f5fcb4857e805..634f4911628295 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -327,7 +327,7 @@ void StringRef::split(SmallVectorImpl<StringRef> &A,
 
     // Push this split.
     if (KeepEmpty || Idx > 0)
-      A.push_back(S.slice(0, Idx));
+      A.push_back(S.substr(0, Idx));
 
     // Jump forward.
     S = S.substr(Idx + Separator.size());
@@ -353,7 +353,7 @@ void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator,
 
     // Push this split.
     if (KeepEmpty || Idx > 0)
-      A.push_back(S.slice(0, Idx));
+      A.push_back(S.substr(0, Idx));
 
     // Jump forward.
     S = S.substr(Idx + 1);
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index b3cdaa3eefc90c..382df84344c778 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -1998,7 +1998,7 @@ class llvm::vfs::RedirectingFileSystemParser {
     size_t RootPathLen = sys::path::root_path(Trimmed, path_style).size();
     while (Trimmed.size() > RootPathLen &&
            sys::path::is_separator(Trimmed.back(), path_style))
-      Trimmed = Trimmed.slice(0, Trimmed.size() - 1);
+      Trimmed = Trimmed.substr(0, Trimmed.size() - 1);
 
     // Get the last component
     StringRef LastComponent = sys::path::filename(Trimmed, path_style);
diff --git a/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp b/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
index 0df1c336a22146..2c73ce47291be4 100644
--- a/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
+++ b/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
@@ -6619,7 +6619,7 @@ StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic, StringRef ExtraToken,
          Mnemonic.starts_with("vq")))) {
     unsigned CC = ARMCondCodeFromString(Mnemonic.substr(Mnemonic.size()-2));
     if (CC != ~0U) {
-      Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
+      Mnemonic = Mnemonic.substr(0, Mnemonic.size() - 2);
       PredicationCode = static_cast<ARMCC::CondCodes>(CC);
     }
   }
@@ -6638,7 +6638,7 @@ StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic, StringRef ExtraToken,
         Mnemonic == "vfnms" || Mnemonic == "fconsts" || Mnemonic == "bxns" ||
         Mnemonic == "blxns" || Mnemonic == "vfmas" || Mnemonic == "vmlas" ||
         (Mnemonic == "movs" && isThumb()))) {
-    Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
+    Mnemonic = Mnemonic.substr(0, Mnemonic.size() - 1);
     CarrySetting = true;
   }
 
@@ -6652,7 +6652,7 @@ StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic, StringRef ExtraToken,
       .Case("id", ARM_PROC::ID)
       .Default(~0U);
     if (IMod != ~0U) {
-      Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
+      Mnemonic = Mnemonic.substr(0, Mnemonic.size() - 2);
       ProcessorIMod = IMod;
     }
   }
@@ -6667,7 +6667,7 @@ StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic, StringRef ExtraToken,
     unsigned VCC =
         ARMVectorCondCodeFromString(Mnemonic.substr(Mnemonic.size() - 1));
     if (VCC != ~0U) {
-      Mnemonic = Mnemonic.slice(0, Mnemonic.size()-1);
+      Mnemonic = Mnemonic.substr(0, Mnemonic.size() - 1);
       VPTPredicationCode = static_cast<ARMVCC::VPTCodes>(VCC);
     }
     return Mnemonic;
@@ -6676,15 +6676,15 @@ StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic, StringRef ExtraToken,
   // The "it" instruction has the condition mask on the end of the mnemonic.
   if (Mnemonic.starts_with("it")) {
     ITMask = Mnemonic.substr(2);
-    Mnemonic = Mnemonic.slice(0, 2);
+    Mnemonic = Mnemonic.substr(0, 2);
   }
 
   if (Mnemonic.starts_with("vpst")) {
     ITMask = Mnemonic.substr(4);
-    Mnemonic = Mnemonic.slice(0, 4);
+    Mnemonic = Mnemonic.substr(0, 4);
   } else if (Mnemonic.starts_with("vpt")) {
     ITMask = Mnemonic.substr(3);
-    Mnemonic = Mnemonic.slice(0, 3);
+    Mnemonic = Mnemonic.substr(0, 3);
   }
 
   return Mnemonic;
@@ -7398,7 +7398,7 @@ bool ARMAsmParser::parseInstruction(ParseInstructionInfo &Info, StringRef Name,
       // point VCMPE is actually a different instruction from VCMP, so
       // we mustn't treat them the same). In that situation, glue it
       // back on.
-      Mnemonic = Name.slice(0, Mnemonic.size() + 1);
+      Mnemonic = Name.substr(0, Mnemonic.size() + 1);
       Operands.erase(Operands.begin());
       Operands.insert(Operands.begin(),
                       ARMOperand::CreateToken(Mnemonic, NameLoc, *this));
diff --git a/llvm/lib/Target/Lanai/AsmParser/LanaiAsmParser.cpp b/llvm/lib/Target/Lanai/AsmParser/LanaiAsmParser.cpp
index 7ede5e3ed40932..a796d7b25af912 100644
--- a/llvm/lib/Target/Lanai/AsmParser/LanaiAsmParser.cpp
+++ b/llvm/lib/Target/Lanai/AsmParser/LanaiAsmParser.cpp
@@ -1058,7 +1058,7 @@ StringRef LanaiAsmParser::splitMnemonic(StringRef Name, SMLoc NameLoc,
     LPCC::CondCode CondCode =
         LPCC::suffixToLanaiCondCode(Mnemonic.substr(1, Next));
     if (CondCode != LPCC::UNKNOWN) {
-      Mnemonic = Mnemonic.slice(0, 1);
+      Mnemonic = Mnemonic.substr(0, 1);
       Operands->push_back(LanaiOperand::CreateToken(Mnemonic, NameLoc));
       Operands->push_back(LanaiOperand::createImm(
           MCConstantExpr::create(CondCode, getContext()), NameLoc, NameLoc));
diff --git a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
index bf512481cf6460..34ed3daf9535a9 100644
--- a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
+++ b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
@@ -1643,7 +1643,7 @@ bool PPCAsmParser::parseInstruction(ParseInstructionInfo &Info, StringRef Name,
   // If the instruction ends in a '.', we need to create a separate
   // token for it, to match what TableGen is doing.
   size_t Dot = Name.find('.');
-  StringRef Mnemonic = Name.slice(0, Dot);
+  StringRef Mnemonic = Name.substr(0, Dot);
   if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
     Operands.push_back(
         PPCOperand::CreateTokenWithStringCopy(Mnemonic, NameLoc, isPPC64()));
diff --git a/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp b/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
index 5073894cc7fbe1..d4c2e9b399df35 100644
--- a/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
+++ b/llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp
@@ -864,7 +864,7 @@ static StringRef parseCC(StringRef Name, unsigned Prefix, unsigned Suffix,
       (!OmitCC || (CondCode != VECC::CC_AT && CondCode != VECC::CC_AF))) {
     StringRef SuffixStr = Name.substr(Suffix);
     // Push "b".
-    Name = Name.slice(0, Prefix);
+    Name = Name.substr(0, Prefix);
     Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
     // Push $cond part.
     SMLoc CondLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Prefix);
@@ -887,7 +887,7 @@ static StringRef parseRD(StringRef Name, unsigned Prefix, SMLoc NameLoc,
   VERD::RoundingMode RoundingMode = stringToVERD(RD);
 
   if (RoundingMode != VERD::UNKNOWN) {
-    Name = Name.slice(0, Prefix);
+    Name = Name.substr(0, Prefix);
     // push 1st like `cvt.w.d.sx`
     Operands->push_back(VEOperand::CreateToken(Name, NameLoc));
     SMLoc SuffixLoc =
diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
index ae30d4dfc70f53..c7b6a038fb3efb 100644
--- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -1955,7 +1955,7 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
         size_t DotOffset = Identifier.find_first_of('.');
         if (DotOffset != StringRef::npos) {
           consumeToken();
-          StringRef LHS = Identifier.slice(0, DotOffset);
+          StringRef LHS = Identifier.substr(0, DotOffset);
           StringRef Dot = Identifier.substr(DotOffset, 1);
           StringRef RHS = Identifier.substr(DotOffset + 1);
           if (!RHS.empty()) {
diff --git a/llvm/lib/TargetParser/RISCVISAInfo.cpp b/llvm/lib/TargetParser/RISCVISAInfo.cpp
index caa5a97747ee57..cafacfc78b40ae 100644
--- a/llvm/lib/TargetParser/RISCVISAInfo.cpp
+++ b/llvm/lib/TargetParser/RISCVISAInfo.cpp
@@ -507,7 +507,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
     }
 
     size_t Idx = Arch.find('_');
-    StringRef Ext = Arch.slice(0, Idx);
+    StringRef Ext = Arch.substr(0, Idx);
     Arch = Arch.substr(Idx);
 
     StringRef Prefix, MinorVersionStr;
@@ -532,7 +532,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
     if (VersionStart == 0)
       return getError("missing extension name");
 
-    StringRef ExtName = Prefix.slice(0, VersionStart);
+    StringRef ExtName = Prefix.substr(0, VersionStart);
     StringRef MajorVersionStr = Prefix.substr(VersionStart);
     if (MajorVersionStr.getAsInteger(10, MajorVersion))
       return getError("failed to parse major version number");
@@ -661,7 +661,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
     }
 
     size_t Idx = Arch.find('_');
-    StringRef Ext = Arch.slice(0, Idx);
+    StringRef Ext = Arch.substr(0, Idx);
     Arch = Arch.substr(Idx);
 
     do {
diff --git a/llvm/tools/llvm-cxxdump/llvm-cxxdump.cpp b/llvm/tools/llvm-cxxdump/llvm-cxxdump.cpp
index c04aec19174b8d..f2b37ce3cf6294 100644
--- a/llvm/tools/llvm-cxxdump/llvm-cxxdump.cpp
+++ b/llvm/tools/llvm-cxxdump/llvm-cxxdump.cpp
@@ -316,7 +316,7 @@ static void dumpCXXData(const ObjectFile *Obj) {
     }
     // Typeinfo names in the Itanium ABI start with '_ZTS' or '__ZTS'.
     else if (SymName.starts_with("_ZTS") || SymName.starts_with("__ZTS")) {
-      TINames[SymName] = SymContents.slice(0, SymContents.find('\0'));
+      TINames[SymName] = SymContents.substr(0, SymContents.find('\0'));
     }
     // Vtables in the Itanium ABI start with '_ZTV' or '__ZTV'.
     else if (SymName.starts_with("_ZTV") || SymName.starts_with("__ZTV")) {
diff --git a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
index 41b379e8fd396b..f194e48a77e897 100644
--- a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
+++ b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp
@@ -117,7 +117,7 @@ static void SplitStringDelims(
   // Obtain any leading delimiters.
   auto Start = std::find_if(Head, Source.end(), IsLegalChar);
   if (Start != Head)
-    OutFragments.push_back({"", Source.slice(0, Start - Head)});
+    OutFragments.push_back({"", Source.substr(0, Start - Head)});
 
   // Capture each word and the delimiters following that word.
   while (Start != Source.end()) {
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index 26a888c628d9d3..51ddd7f8868292 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -579,7 +579,7 @@ static Expected<int64_t> parseChangeSectionLMA(StringRef ArgValue,
   if (!LMAValue)
     return createStringError(LMAValue.getError(),
                              "bad format for " + OptionName + ": value after " +
-                                 ArgValue.slice(0, 2) + " is " + StringValue +
+                                 ArgValue.substr(0, 2) + " is " + StringValue +
                                  " when it should be an integer");
   return *LMAValue;
 }
@@ -598,7 +598,7 @@ parseChangeSectionAddr(StringRef ArgValue, StringRef OptionName,
                                  " is invalid. See --help");
   char UpdateSymbol = ArgValue[LastSymbolIndex];
 
-  StringRef SectionPattern = ArgValue.slice(0, LastSymbolIndex);
+  StringRef SectionPattern = ArgValue.substr(0, LastSymbolIndex);
   if (SectionPattern.empty())
     return createStringError(
         errc::invalid_argument,
diff --git a/llvm/unittests/MC/DwarfLineTableHeaders.cpp b/llvm/unittests/MC/DwarfLineTableHeaders.cpp
index 1fad1ba6ce638d..837ea3ec8fb6ce 100644
--- a/llvm/unittests/MC/DwarfLineTableHeaders.cpp
+++ b/llvm/unittests/MC/DwarfLineTableHeaders.cpp
@@ -151,7 +151,7 @@ class DwarfLineTableHeaders : public ::testing::Test {
       StringRef Contents = *ContentsOrErr;
       ASSERT_TRUE(Contents.size() > ExpectedEncoding.size());
       EXPECT_EQ(
-          arrayRefFromStringRef(Contents.slice(0, ExpectedEncoding.size())),
+          arrayRefFromStringRef(Contents.substr(0, ExpectedEncoding.size())),
           ExpectedEncoding);
       return;
     }
diff --git a/llvm/unittests/Support/YA...
[truncated]

@kazutakahirata kazutakahirata requested a review from d0k October 27, 2024 06:31
@cor3ntin
Copy link
Contributor

I'm planning to migrate StringRef to std::string_view eventually.

Has there been a discussion / RFC ? The benefit seems extremely low, if not negative (given that stringref is a non template class and doesn't have the char_traits nonsense)

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaskRay
Copy link
Member

MaskRay commented Oct 27, 2024

Please wait for feedback on discourse.llvm.org/t/migrating-llvm-stringref-to-std-string-view/82785/2

While the broader discussion of StringRef migration or string_view integration is debatable, this specific member function migration seems reasonable. The substr function is well-established and widely understood, making this patch a worthwhile improvement. There is certainly some churn cost associated with such patches, but I believe it's acceptable in this case.

@dwblaikie
Copy link
Collaborator

Yeah, converging the APIs where there's not huge differences (in this case we support both, one is a standard name/behavior) seems like a win for usability even if we never coalesce StringRef with string_view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants