[llvm] [llvm] Prefer StringRef::substr to StringRef::slice (NFC) (PR #105943)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 24 08:48:10 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Kazu Hirata (kazutakahirata)
<details>
<summary>Changes</summary>
S.substr(N) is simpler than S.slice(N, StringRef::npos) and
S.slice(N, S.size()). Also, substr is probably better recognizable
than slice thanks to std::string_view::substr.
---
Full diff: https://github.com/llvm/llvm-project/pull/105943.diff
13 Files Affected:
- (modified) llvm/include/llvm/ADT/StringRef.h (+2-2)
- (modified) llvm/lib/CodeGen/MIRParser/MIParser.cpp (+2-2)
- (modified) llvm/lib/MC/MCAsmStreamer.cpp (+2-2)
- (modified) llvm/lib/Object/MachOObjectFile.cpp (+5-5)
- (modified) llvm/lib/Support/StringRef.cpp (+2-2)
- (modified) llvm/lib/Support/VirtualFileSystem.cpp (+3-3)
- (modified) llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp (+1-1)
- (modified) llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp (+4-4)
- (modified) llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp (+1-1)
- (modified) llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp (+1-1)
- (modified) llvm/lib/TargetParser/RISCVISAInfo.cpp (+3-3)
- (modified) llvm/utils/TableGen/AsmMatcherEmitter.cpp (+2-2)
- (modified) llvm/utils/TableGen/OptParserEmitter.cpp (+1-1)
``````````diff
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 952d6485dafc1a..17ab10b9181f1a 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -713,7 +713,7 @@ namespace llvm {
size_t Idx = find(Separator);
if (Idx == npos)
return std::make_pair(*this, StringRef());
- return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
+ return std::make_pair(slice(0, Idx), substr(Idx + Separator.size()));
}
/// Split into two substrings around the last occurrence of a separator
@@ -731,7 +731,7 @@ namespace llvm {
size_t Idx = rfind(Separator);
if (Idx == npos)
return std::make_pair(*this, StringRef());
- return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
+ return std::make_pair(slice(0, Idx), substr(Idx + Separator.size()));
}
/// Split into substrings around the occurrences of a separator string.
diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.cpp b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
index 0e2b71729fbf51..47b220172602d4 100644
--- a/llvm/lib/CodeGen/MIRParser/MIParser.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
@@ -580,7 +580,7 @@ MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
void MIParser::lex(unsigned SkipChar) {
CurrentSource = lexMIToken(
- CurrentSource.slice(SkipChar, StringRef::npos), Token,
+ CurrentSource.substr(SkipChar), Token,
[this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
}
@@ -2306,7 +2306,7 @@ bool MIParser::parseDIExpression(MDNode *&Expr) {
Expr = llvm::parseDIExpressionBodyAtBeginning(
CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
&PFS.IRSlots);
- CurrentSource = CurrentSource.slice(Read, StringRef::npos);
+ CurrentSource = CurrentSource.substr(Read);
lex();
if (!Expr)
return error(Error.getMessage());
diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp
index 9309d5987dc949..bd6e8f03edf454 100644
--- a/llvm/lib/MC/MCAsmStreamer.cpp
+++ b/llvm/lib/MC/MCAsmStreamer.cpp
@@ -491,7 +491,7 @@ void MCAsmStreamer::addExplicitComment(const Twine &T) {
ExplicitCommentToEmit.append("\t");
ExplicitCommentToEmit.append(MAI->getCommentString());
// drop //
- ExplicitCommentToEmit.append(c.slice(2, c.size()).str());
+ ExplicitCommentToEmit.append(c.substr(2).str());
} else if (c.starts_with(StringRef("/*"))) {
size_t p = 2, len = c.size() - 2;
// emit each line in comment as separate newline.
@@ -512,7 +512,7 @@ void MCAsmStreamer::addExplicitComment(const Twine &T) {
ExplicitCommentToEmit.append("\t");
ExplicitCommentToEmit.append(MAI->getCommentString());
- ExplicitCommentToEmit.append(c.slice(1, c.size()).str());
+ ExplicitCommentToEmit.append(c.substr(1).str());
} else
assert(false && "Unexpected Assembly Comment");
// full line comments immediately output
diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp
index bd9b5dd13cdfe8..6f3dd4d8b51801 100644
--- a/llvm/lib/Object/MachOObjectFile.cpp
+++ b/llvm/lib/Object/MachOObjectFile.cpp
@@ -2436,12 +2436,12 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
a = Name.rfind('/');
if (a == Name.npos || a == 0)
goto guess_library;
- Foo = Name.slice(a+1, Name.npos);
+ Foo = Name.substr(a + 1);
// Look for a suffix starting with a '_'
Idx = Foo.rfind('_');
if (Idx != Foo.npos && Foo.size() >= 2) {
- Suffix = Foo.slice(Idx, Foo.npos);
+ Suffix = Foo.substr(Idx);
if (Suffix != "_debug" && Suffix != "_profile")
Suffix = StringRef();
else
@@ -2468,7 +2468,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
c = Name.rfind('/', b);
if (c == Name.npos || c == 0)
goto guess_library;
- V = Name.slice(c+1, Name.npos);
+ V = Name.substr(c + 1);
if (!V.starts_with("Versions/"))
goto guess_library;
d = Name.rfind('/', c);
@@ -2489,7 +2489,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
a = Name.rfind('.');
if (a == Name.npos || a == 0)
return StringRef();
- Dylib = Name.slice(a, Name.npos);
+ Dylib = Name.substr(a);
if (Dylib != ".dylib")
goto guess_qtx;
@@ -2527,7 +2527,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
return Lib;
guess_qtx:
- Qtx = Name.slice(a, Name.npos);
+ Qtx = Name.substr(a);
if (Qtx != ".qtx")
return StringRef();
b = Name.rfind('/', a);
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index f1042131a89cb7..4bbe4168820962 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -328,7 +328,7 @@ void StringRef::split(SmallVectorImpl<StringRef> &A,
A.push_back(S.slice(0, Idx));
// Jump forward.
- S = S.slice(Idx + Separator.size(), npos);
+ S = S.substr(Idx + Separator.size());
}
// Push the tail.
@@ -354,7 +354,7 @@ void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator,
A.push_back(S.slice(0, Idx));
// Jump forward.
- S = S.slice(Idx + 1, npos);
+ S = S.substr(Idx + 1);
}
// Push the tail.
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index 9d21eba9df635f..6d756f425fb888 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -2780,7 +2780,7 @@ bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
assert(!Parent.empty());
assert(containedIn(Parent, Path));
- return Path.slice(Parent.size() + 1, StringRef::npos);
+ return Path.substr(Parent.size() + 1);
}
void JSONWriter::startDirectory(StringRef Path) {
@@ -2846,7 +2846,7 @@ void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
if (UseOverlayRelative) {
assert(RPath.starts_with(OverlayDir) &&
"Overlay dir must be contained in RPath");
- RPath = RPath.slice(OverlayDir.size(), RPath.size());
+ RPath = RPath.substr(OverlayDir.size());
}
bool IsCurrentDirEmpty = true;
@@ -2879,7 +2879,7 @@ void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
if (UseOverlayRelative) {
assert(RPath.starts_with(OverlayDir) &&
"Overlay dir must be contained in RPath");
- RPath = RPath.slice(OverlayDir.size(), RPath.size());
+ RPath = RPath.substr(OverlayDir.size());
}
if (!Entry.IsDirectory) {
writeEntry(path::filename(Entry.VPath), RPath);
diff --git a/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp b/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
index 1c5909c64bccd3..37add682b150e7 100644
--- a/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
+++ b/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
@@ -4183,7 +4183,7 @@ ParseStatus AArch64AsmParser::tryParseVectorRegister(MCRegister &Reg,
if (RegNum) {
if (Next != StringRef::npos) {
- Kind = Name.slice(Next, StringRef::npos);
+ Kind = Name.substr(Next);
if (!isValidVectorKind(Kind, MatchKind))
return TokError("invalid vector kind qualifier");
}
diff --git a/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp b/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
index c2951bf6dbf78f..1b6405c93820fc 100644
--- a/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
+++ b/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
@@ -5251,7 +5251,7 @@ ParseStatus ARMAsmParser::parseMSRMaskOperand(OperandVector &Operands) {
StringRef Flags = "";
std::string SpecReg = Mask.slice(Start, Next).lower();
if (Next != StringRef::npos)
- Flags = Mask.slice(Next+1, Mask.size());
+ Flags = Mask.substr(Next + 1);
// FlagsVal contains the complete mask:
// 3-0: Mask
@@ -6648,15 +6648,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.slice(2, Mnemonic.size());
+ ITMask = Mnemonic.substr(2);
Mnemonic = Mnemonic.slice(0, 2);
}
if (Mnemonic.starts_with("vpst")) {
- ITMask = Mnemonic.slice(4, Mnemonic.size());
+ ITMask = Mnemonic.substr(4);
Mnemonic = Mnemonic.slice(0, 4);
} else if (Mnemonic.starts_with("vpt")) {
- ITMask = Mnemonic.slice(3, Mnemonic.size());
+ ITMask = Mnemonic.substr(3);
Mnemonic = Mnemonic.slice(0, 3);
}
diff --git a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
index 55978af38000de..59ad995b44b04a 100644
--- a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
+++ b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
@@ -1682,7 +1682,7 @@ bool PPCAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
Operands.push_back(PPCOperand::CreateToken(Mnemonic, NameLoc, isPPC64()));
if (Dot != StringRef::npos) {
SMLoc DotLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Dot);
- StringRef DotStr = Name.slice(Dot, StringRef::npos);
+ StringRef DotStr = Name.substr(Dot);
if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
Operands.push_back(
PPCOperand::CreateTokenWithStringCopy(DotStr, DotLoc, isPPC64()));
diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
index 6e17150edf2785..eda3c9fd50bf56 100644
--- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -1957,7 +1957,7 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
consumeToken();
StringRef LHS = Identifier.slice(0, DotOffset);
StringRef Dot = Identifier.slice(DotOffset, DotOffset + 1);
- StringRef RHS = Identifier.slice(DotOffset + 1, StringRef::npos);
+ StringRef RHS = Identifier.substr(DotOffset + 1);
if (!RHS.empty()) {
getLexer().UnLex(AsmToken(AsmToken::Identifier, RHS));
}
diff --git a/llvm/lib/TargetParser/RISCVISAInfo.cpp b/llvm/lib/TargetParser/RISCVISAInfo.cpp
index d82501ebf8d2fb..7fa3d8edec84d5 100644
--- a/llvm/lib/TargetParser/RISCVISAInfo.cpp
+++ b/llvm/lib/TargetParser/RISCVISAInfo.cpp
@@ -508,7 +508,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
size_t Idx = Arch.find('_');
StringRef Ext = Arch.slice(0, Idx);
- Arch = Arch.slice(Idx, StringRef::npos);
+ Arch = Arch.substr(Idx);
StringRef Prefix, MinorVersionStr;
std::tie(Prefix, MinorVersionStr) = Ext.rsplit('p');
@@ -533,7 +533,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
return getError("missing extension name");
StringRef ExtName = Prefix.slice(0, VersionStart);
- StringRef MajorVersionStr = Prefix.slice(VersionStart, StringRef::npos);
+ StringRef MajorVersionStr = Prefix.substr(VersionStart);
if (MajorVersionStr.getAsInteger(10, MajorVersion))
return getError("failed to parse major version number");
@@ -662,7 +662,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
size_t Idx = Arch.find('_');
StringRef Ext = Arch.slice(0, Idx);
- Arch = Arch.slice(Idx, StringRef::npos);
+ Arch = Arch.substr(Idx);
do {
StringRef Name, Vers, Desc;
diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
index 5035ef52707f4e..5738d167c71493 100644
--- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
@@ -849,14 +849,14 @@ parseTwoOperandConstraint(StringRef S, ArrayRef<SMLoc> Loc) {
size_t start = Ops.first.find_first_of('$');
if (start == std::string::npos)
PrintFatalError(Loc, "expected '$' prefix on asm operand name");
- Ops.first = Ops.first.slice(start + 1, std::string::npos);
+ Ops.first = Ops.first.substr(start + 1);
size_t end = Ops.first.find_last_of(" \t");
Ops.first = Ops.first.slice(0, end);
// Now the second operand.
start = Ops.second.find_first_of('$');
if (start == std::string::npos)
PrintFatalError(Loc, "expected '$' prefix on asm operand name");
- Ops.second = Ops.second.slice(start + 1, std::string::npos);
+ Ops.second = Ops.second.substr(start + 1);
end = Ops.second.find_last_of(" \t");
Ops.first = Ops.first.slice(0, end);
return Ops;
diff --git a/llvm/utils/TableGen/OptParserEmitter.cpp b/llvm/utils/TableGen/OptParserEmitter.cpp
index 4fb1ca18ac11db..81195c8c106c23 100644
--- a/llvm/utils/TableGen/OptParserEmitter.cpp
+++ b/llvm/utils/TableGen/OptParserEmitter.cpp
@@ -178,7 +178,7 @@ static MarshallingInfo createMarshallingInfo(const Record &R) {
break;
if (Idx > 0)
Ret.Values.push_back(ValuesStr.slice(0, Idx));
- ValuesStr = ValuesStr.slice(Idx + 1, StringRef::npos);
+ ValuesStr = ValuesStr.substr(Idx + 1);
}
if (!ValuesStr.empty())
Ret.Values.push_back(ValuesStr);
``````````
</details>
https://github.com/llvm/llvm-project/pull/105943
More information about the llvm-commits
mailing list