Skip to content

Commit 33e7cd6

Browse files
[llvm] Prefer StringRef::substr to StringRef::slice (NFC) (#105943)
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.
1 parent 33f3ebc commit 33e7cd6

File tree

13 files changed

+29
-29
lines changed

13 files changed

+29
-29
lines changed

llvm/include/llvm/ADT/StringRef.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ namespace llvm {
713713
size_t Idx = find(Separator);
714714
if (Idx == npos)
715715
return std::make_pair(*this, StringRef());
716-
return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
716+
return std::make_pair(slice(0, Idx), substr(Idx + Separator.size()));
717717
}
718718

719719
/// Split into two substrings around the last occurrence of a separator
@@ -731,7 +731,7 @@ namespace llvm {
731731
size_t Idx = rfind(Separator);
732732
if (Idx == npos)
733733
return std::make_pair(*this, StringRef());
734-
return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
734+
return std::make_pair(slice(0, Idx), substr(Idx + Separator.size()));
735735
}
736736

737737
/// Split into substrings around the occurrences of a separator string.

llvm/lib/CodeGen/MIRParser/MIParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
580580

581581
void MIParser::lex(unsigned SkipChar) {
582582
CurrentSource = lexMIToken(
583-
CurrentSource.slice(SkipChar, StringRef::npos), Token,
583+
CurrentSource.substr(SkipChar), Token,
584584
[this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
585585
}
586586

@@ -2306,7 +2306,7 @@ bool MIParser::parseDIExpression(MDNode *&Expr) {
23062306
Expr = llvm::parseDIExpressionBodyAtBeginning(
23072307
CurrentSource, Read, Error, *PFS.MF.getFunction().getParent(),
23082308
&PFS.IRSlots);
2309-
CurrentSource = CurrentSource.slice(Read, StringRef::npos);
2309+
CurrentSource = CurrentSource.substr(Read);
23102310
lex();
23112311
if (!Expr)
23122312
return error(Error.getMessage());

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ void MCAsmStreamer::addExplicitComment(const Twine &T) {
491491
ExplicitCommentToEmit.append("\t");
492492
ExplicitCommentToEmit.append(MAI->getCommentString());
493493
// drop //
494-
ExplicitCommentToEmit.append(c.slice(2, c.size()).str());
494+
ExplicitCommentToEmit.append(c.substr(2).str());
495495
} else if (c.starts_with(StringRef("/*"))) {
496496
size_t p = 2, len = c.size() - 2;
497497
// emit each line in comment as separate newline.
@@ -512,7 +512,7 @@ void MCAsmStreamer::addExplicitComment(const Twine &T) {
512512

513513
ExplicitCommentToEmit.append("\t");
514514
ExplicitCommentToEmit.append(MAI->getCommentString());
515-
ExplicitCommentToEmit.append(c.slice(1, c.size()).str());
515+
ExplicitCommentToEmit.append(c.substr(1).str());
516516
} else
517517
assert(false && "Unexpected Assembly Comment");
518518
// full line comments immediately output

llvm/lib/Object/MachOObjectFile.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,12 +2436,12 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
24362436
a = Name.rfind('/');
24372437
if (a == Name.npos || a == 0)
24382438
goto guess_library;
2439-
Foo = Name.slice(a+1, Name.npos);
2439+
Foo = Name.substr(a + 1);
24402440

24412441
// Look for a suffix starting with a '_'
24422442
Idx = Foo.rfind('_');
24432443
if (Idx != Foo.npos && Foo.size() >= 2) {
2444-
Suffix = Foo.slice(Idx, Foo.npos);
2444+
Suffix = Foo.substr(Idx);
24452445
if (Suffix != "_debug" && Suffix != "_profile")
24462446
Suffix = StringRef();
24472447
else
@@ -2468,7 +2468,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
24682468
c = Name.rfind('/', b);
24692469
if (c == Name.npos || c == 0)
24702470
goto guess_library;
2471-
V = Name.slice(c+1, Name.npos);
2471+
V = Name.substr(c + 1);
24722472
if (!V.starts_with("Versions/"))
24732473
goto guess_library;
24742474
d = Name.rfind('/', c);
@@ -2489,7 +2489,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
24892489
a = Name.rfind('.');
24902490
if (a == Name.npos || a == 0)
24912491
return StringRef();
2492-
Dylib = Name.slice(a, Name.npos);
2492+
Dylib = Name.substr(a);
24932493
if (Dylib != ".dylib")
24942494
goto guess_qtx;
24952495

@@ -2527,7 +2527,7 @@ StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
25272527
return Lib;
25282528

25292529
guess_qtx:
2530-
Qtx = Name.slice(a, Name.npos);
2530+
Qtx = Name.substr(a);
25312531
if (Qtx != ".qtx")
25322532
return StringRef();
25332533
b = Name.rfind('/', a);

llvm/lib/Support/StringRef.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ void StringRef::split(SmallVectorImpl<StringRef> &A,
328328
A.push_back(S.slice(0, Idx));
329329

330330
// Jump forward.
331-
S = S.slice(Idx + Separator.size(), npos);
331+
S = S.substr(Idx + Separator.size());
332332
}
333333

334334
// Push the tail.
@@ -354,7 +354,7 @@ void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator,
354354
A.push_back(S.slice(0, Idx));
355355

356356
// Jump forward.
357-
S = S.slice(Idx + 1, npos);
357+
S = S.substr(Idx + 1);
358358
}
359359

360360
// Push the tail.

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,7 +2780,7 @@ bool JSONWriter::containedIn(StringRef Parent, StringRef Path) {
27802780
StringRef JSONWriter::containedPart(StringRef Parent, StringRef Path) {
27812781
assert(!Parent.empty());
27822782
assert(containedIn(Parent, Path));
2783-
return Path.slice(Parent.size() + 1, StringRef::npos);
2783+
return Path.substr(Parent.size() + 1);
27842784
}
27852785

27862786
void JSONWriter::startDirectory(StringRef Path) {
@@ -2846,7 +2846,7 @@ void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
28462846
if (UseOverlayRelative) {
28472847
assert(RPath.starts_with(OverlayDir) &&
28482848
"Overlay dir must be contained in RPath");
2849-
RPath = RPath.slice(OverlayDir.size(), RPath.size());
2849+
RPath = RPath.substr(OverlayDir.size());
28502850
}
28512851

28522852
bool IsCurrentDirEmpty = true;
@@ -2879,7 +2879,7 @@ void JSONWriter::write(ArrayRef<YAMLVFSEntry> Entries,
28792879
if (UseOverlayRelative) {
28802880
assert(RPath.starts_with(OverlayDir) &&
28812881
"Overlay dir must be contained in RPath");
2882-
RPath = RPath.slice(OverlayDir.size(), RPath.size());
2882+
RPath = RPath.substr(OverlayDir.size());
28832883
}
28842884
if (!Entry.IsDirectory) {
28852885
writeEntry(path::filename(Entry.VPath), RPath);

llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4183,7 +4183,7 @@ ParseStatus AArch64AsmParser::tryParseVectorRegister(MCRegister &Reg,
41834183

41844184
if (RegNum) {
41854185
if (Next != StringRef::npos) {
4186-
Kind = Name.slice(Next, StringRef::npos);
4186+
Kind = Name.substr(Next);
41874187
if (!isValidVectorKind(Kind, MatchKind))
41884188
return TokError("invalid vector kind qualifier");
41894189
}

llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5251,7 +5251,7 @@ ParseStatus ARMAsmParser::parseMSRMaskOperand(OperandVector &Operands) {
52515251
StringRef Flags = "";
52525252
std::string SpecReg = Mask.slice(Start, Next).lower();
52535253
if (Next != StringRef::npos)
5254-
Flags = Mask.slice(Next+1, Mask.size());
5254+
Flags = Mask.substr(Next + 1);
52555255

52565256
// FlagsVal contains the complete mask:
52575257
// 3-0: Mask
@@ -6648,15 +6648,15 @@ StringRef ARMAsmParser::splitMnemonic(StringRef Mnemonic, StringRef ExtraToken,
66486648

66496649
// The "it" instruction has the condition mask on the end of the mnemonic.
66506650
if (Mnemonic.starts_with("it")) {
6651-
ITMask = Mnemonic.slice(2, Mnemonic.size());
6651+
ITMask = Mnemonic.substr(2);
66526652
Mnemonic = Mnemonic.slice(0, 2);
66536653
}
66546654

66556655
if (Mnemonic.starts_with("vpst")) {
6656-
ITMask = Mnemonic.slice(4, Mnemonic.size());
6656+
ITMask = Mnemonic.substr(4);
66576657
Mnemonic = Mnemonic.slice(0, 4);
66586658
} else if (Mnemonic.starts_with("vpt")) {
6659-
ITMask = Mnemonic.slice(3, Mnemonic.size());
6659+
ITMask = Mnemonic.substr(3);
66606660
Mnemonic = Mnemonic.slice(0, 3);
66616661
}
66626662

llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1682,7 +1682,7 @@ bool PPCAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
16821682
Operands.push_back(PPCOperand::CreateToken(Mnemonic, NameLoc, isPPC64()));
16831683
if (Dot != StringRef::npos) {
16841684
SMLoc DotLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Dot);
1685-
StringRef DotStr = Name.slice(Dot, StringRef::npos);
1685+
StringRef DotStr = Name.substr(Dot);
16861686
if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
16871687
Operands.push_back(
16881688
PPCOperand::CreateTokenWithStringCopy(DotStr, DotLoc, isPPC64()));

llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End) {
19571957
consumeToken();
19581958
StringRef LHS = Identifier.slice(0, DotOffset);
19591959
StringRef Dot = Identifier.slice(DotOffset, DotOffset + 1);
1960-
StringRef RHS = Identifier.slice(DotOffset + 1, StringRef::npos);
1960+
StringRef RHS = Identifier.substr(DotOffset + 1);
19611961
if (!RHS.empty()) {
19621962
getLexer().UnLex(AsmToken(AsmToken::Identifier, RHS));
19631963
}

llvm/lib/TargetParser/RISCVISAInfo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
508508

509509
size_t Idx = Arch.find('_');
510510
StringRef Ext = Arch.slice(0, Idx);
511-
Arch = Arch.slice(Idx, StringRef::npos);
511+
Arch = Arch.substr(Idx);
512512

513513
StringRef Prefix, MinorVersionStr;
514514
std::tie(Prefix, MinorVersionStr) = Ext.rsplit('p');
@@ -533,7 +533,7 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
533533
return getError("missing extension name");
534534

535535
StringRef ExtName = Prefix.slice(0, VersionStart);
536-
StringRef MajorVersionStr = Prefix.slice(VersionStart, StringRef::npos);
536+
StringRef MajorVersionStr = Prefix.substr(VersionStart);
537537
if (MajorVersionStr.getAsInteger(10, MajorVersion))
538538
return getError("failed to parse major version number");
539539

@@ -662,7 +662,7 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
662662

663663
size_t Idx = Arch.find('_');
664664
StringRef Ext = Arch.slice(0, Idx);
665-
Arch = Arch.slice(Idx, StringRef::npos);
665+
Arch = Arch.substr(Idx);
666666

667667
do {
668668
StringRef Name, Vers, Desc;

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,14 +849,14 @@ parseTwoOperandConstraint(StringRef S, ArrayRef<SMLoc> Loc) {
849849
size_t start = Ops.first.find_first_of('$');
850850
if (start == std::string::npos)
851851
PrintFatalError(Loc, "expected '$' prefix on asm operand name");
852-
Ops.first = Ops.first.slice(start + 1, std::string::npos);
852+
Ops.first = Ops.first.substr(start + 1);
853853
size_t end = Ops.first.find_last_of(" \t");
854854
Ops.first = Ops.first.slice(0, end);
855855
// Now the second operand.
856856
start = Ops.second.find_first_of('$');
857857
if (start == std::string::npos)
858858
PrintFatalError(Loc, "expected '$' prefix on asm operand name");
859-
Ops.second = Ops.second.slice(start + 1, std::string::npos);
859+
Ops.second = Ops.second.substr(start + 1);
860860
end = Ops.second.find_last_of(" \t");
861861
Ops.first = Ops.first.slice(0, end);
862862
return Ops;

llvm/utils/TableGen/OptParserEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static MarshallingInfo createMarshallingInfo(const Record &R) {
178178
break;
179179
if (Idx > 0)
180180
Ret.Values.push_back(ValuesStr.slice(0, Idx));
181-
ValuesStr = ValuesStr.slice(Idx + 1, StringRef::npos);
181+
ValuesStr = ValuesStr.substr(Idx + 1);
182182
}
183183
if (!ValuesStr.empty())
184184
Ret.Values.push_back(ValuesStr);

0 commit comments

Comments
 (0)