Skip to content

Commit 9981afc

Browse files
authored
[NFC][TableGen] Use StringRef::str() instead of casting (#139332)
- Also eliminate unneeded std::string() around some literal strings.
1 parent 227328f commit 9981afc

33 files changed

+129
-139
lines changed

llvm/include/llvm/TableGen/Record.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,7 @@ class StringInit final : public TypedInit {
745745
return "[{" + Value.str() + "}]";
746746
}
747747

748-
std::string getAsUnquotedString() const override {
749-
return std::string(Value);
750-
}
748+
std::string getAsUnquotedString() const override { return Value.str(); }
751749

752750
const Init *getBit(unsigned Bit) const override {
753751
llvm_unreachable("Illegal bit reference off string");

llvm/lib/TableGen/Record.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,22 +1788,21 @@ const Init *TernOpInit::Fold(const Record *CurRec) const {
17881788
return Val->getDefInit();
17891789
}
17901790
if (LHSv && MHSv && RHSv) {
1791-
std::string Val = std::string(RHSv->getName());
1791+
std::string Val = RHSv->getName().str();
17921792
if (LHSv->getAsString() == RHSv->getAsString())
1793-
Val = std::string(MHSv->getName());
1793+
Val = MHSv->getName().str();
17941794
return VarInit::get(Val, getType());
17951795
}
17961796
if (LHSs && MHSs && RHSs) {
1797-
std::string Val = std::string(RHSs->getValue());
1797+
std::string Val = RHSs->getValue().str();
17981798

17991799
std::string::size_type found;
18001800
std::string::size_type idx = 0;
18011801
while (true) {
1802-
found = Val.find(std::string(LHSs->getValue()), idx);
1802+
found = Val.find(LHSs->getValue().str(), idx);
18031803
if (found == std::string::npos)
18041804
break;
1805-
Val.replace(found, LHSs->getValue().size(),
1806-
std::string(MHSs->getValue()));
1805+
Val.replace(found, LHSs->getValue().size(), MHSs->getValue().str());
18071806
idx = found + MHSs->getValue().size();
18081807
}
18091808

@@ -2418,7 +2417,7 @@ const RecTy *DefInit::getFieldType(const StringInit *FieldName) const {
24182417
return nullptr;
24192418
}
24202419

2421-
std::string DefInit::getAsString() const { return std::string(Def->getName()); }
2420+
std::string DefInit::getAsString() const { return Def->getName().str(); }
24222421

24232422
static void ProfileVarDefInit(FoldingSetNodeID &ID, const Record *Class,
24242423
ArrayRef<const ArgumentInit *> Args) {

llvm/lib/TableGen/SetTheory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ struct SequenceOp : public SetTheory::Operator {
191191

192192
std::string Format;
193193
if (const auto *SI = dyn_cast<StringInit>(Expr->arg_begin()[0]))
194-
Format = std::string(SI->getValue());
194+
Format = SI->getValue().str();
195195
else
196196
PrintFatalError(Loc, "Format must be a string: " + Expr->getAsString());
197197

llvm/lib/TableGen/TGParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4327,7 +4327,7 @@ bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
43274327
// through its template argument names. Substs contains a substitution
43284328
// value for each argument, either the value specified or the default.
43294329
// Then we can resolve the template arguments.
4330-
MultiClass *MC = MultiClasses[std::string(Ref.Rec->getName())].get();
4330+
MultiClass *MC = MultiClasses[Ref.Rec->getName().str()].get();
43314331
assert(MC && "Didn't lookup multiclass correctly?");
43324332

43334333
SubstStack Substs;

llvm/lib/TableGen/TGParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class TGVarScope {
131131
}
132132

133133
void addVar(StringRef Name, const Init *I) {
134-
bool Ins = Vars.try_emplace(std::string(Name), I).second;
134+
bool Ins = Vars.try_emplace(Name.str(), I).second;
135135
(void)Ins;
136136
assert(Ins && "Local variable already exists");
137137
}

llvm/utils/TableGen/AsmMatcherEmitter.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ bool MatchableInfo::validate(StringRef CommentDelimiter, bool IsAlias) const {
11141114
// Verify that any operand is only mentioned once.
11151115
// We reject aliases and ignore instructions for now.
11161116
if (!IsAlias && TheDef->getValueAsString("AsmMatchConverter").empty() &&
1117-
Tok[0] == '$' && !OperandNames.insert(std::string(Tok)).second) {
1117+
Tok[0] == '$' && !OperandNames.insert(Tok.str()).second) {
11181118
LLVM_DEBUG({
11191119
errs() << "warning: '" << TheDef->getName() << "': "
11201120
<< "ignoring instruction with tied operand '" << Tok << "'\n";
@@ -1170,15 +1170,15 @@ static std::string getEnumNameForToken(StringRef Str) {
11701170
}
11711171

11721172
ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
1173-
ClassInfo *&Entry = TokenClasses[std::string(Token)];
1173+
ClassInfo *&Entry = TokenClasses[Token.str()];
11741174

11751175
if (!Entry) {
11761176
Classes.emplace_front();
11771177
Entry = &Classes.front();
11781178
Entry->Kind = ClassInfo::Token;
11791179
Entry->ClassName = "Token";
11801180
Entry->Name = "MCK_" + getEnumNameForToken(Token);
1181-
Entry->ValueName = std::string(Token);
1181+
Entry->ValueName = Token.str();
11821182
Entry->PredicateMethod = "<invalid>";
11831183
Entry->RenderMethod = "<invalid>";
11841184
Entry->ParserMethod = "";
@@ -1353,11 +1353,11 @@ void AsmMatcherInfo::buildRegisterClasses(
13531353

13541354
const Init *DiagnosticType = Def->getValueInit("DiagnosticType");
13551355
if (const StringInit *SI = dyn_cast<StringInit>(DiagnosticType))
1356-
CI->DiagnosticType = std::string(SI->getValue());
1356+
CI->DiagnosticType = SI->getValue().str();
13571357

13581358
const Init *DiagnosticString = Def->getValueInit("DiagnosticString");
13591359
if (const StringInit *SI = dyn_cast<StringInit>(DiagnosticString))
1360-
CI->DiagnosticString = std::string(SI->getValue());
1360+
CI->DiagnosticString = SI->getValue().str();
13611361

13621362
// If we have a diagnostic string but the diagnostic type is not specified
13631363
// explicitly, create an anonymous diagnostic type.
@@ -1377,9 +1377,9 @@ void AsmMatcherInfo::buildRegisterClasses(
13771377
assert(CI && "Missing singleton register class info!");
13781378

13791379
if (CI->ValueName.empty()) {
1380-
CI->ClassName = std::string(Rec->getName());
1380+
CI->ClassName = Rec->getName().str();
13811381
CI->Name = "MCK_" + Rec->getName().str();
1382-
CI->ValueName = std::string(Rec->getName());
1382+
CI->ValueName = Rec->getName().str();
13831383
} else {
13841384
CI->ValueName = CI->ValueName + "," + Rec->getName().str();
13851385
}
@@ -1415,14 +1415,14 @@ void AsmMatcherInfo::buildOperandClasses() {
14151415
else
14161416
CI->SuperClasses.push_back(SC);
14171417
}
1418-
CI->ClassName = std::string(Rec->getValueAsString("Name"));
1418+
CI->ClassName = Rec->getValueAsString("Name").str();
14191419
CI->Name = "MCK_" + CI->ClassName;
1420-
CI->ValueName = std::string(Rec->getName());
1420+
CI->ValueName = Rec->getName().str();
14211421

14221422
// Get or construct the predicate method name.
14231423
const Init *PMName = Rec->getValueInit("PredicateMethod");
14241424
if (const StringInit *SI = dyn_cast<StringInit>(PMName)) {
1425-
CI->PredicateMethod = std::string(SI->getValue());
1425+
CI->PredicateMethod = SI->getValue().str();
14261426
} else {
14271427
assert(isa<UnsetInit>(PMName) && "Unexpected PredicateMethod field!");
14281428
CI->PredicateMethod = "is" + CI->ClassName;
@@ -1431,7 +1431,7 @@ void AsmMatcherInfo::buildOperandClasses() {
14311431
// Get or construct the render method name.
14321432
const Init *RMName = Rec->getValueInit("RenderMethod");
14331433
if (const StringInit *SI = dyn_cast<StringInit>(RMName)) {
1434-
CI->RenderMethod = std::string(SI->getValue());
1434+
CI->RenderMethod = SI->getValue().str();
14351435
} else {
14361436
assert(isa<UnsetInit>(RMName) && "Unexpected RenderMethod field!");
14371437
CI->RenderMethod = "add" + CI->ClassName + "Operands";
@@ -1440,15 +1440,15 @@ void AsmMatcherInfo::buildOperandClasses() {
14401440
// Get the parse method name or leave it as empty.
14411441
const Init *PRMName = Rec->getValueInit("ParserMethod");
14421442
if (const StringInit *SI = dyn_cast<StringInit>(PRMName))
1443-
CI->ParserMethod = std::string(SI->getValue());
1443+
CI->ParserMethod = SI->getValue().str();
14441444

14451445
// Get the diagnostic type and string or leave them as empty.
14461446
const Init *DiagnosticType = Rec->getValueInit("DiagnosticType");
14471447
if (const StringInit *SI = dyn_cast<StringInit>(DiagnosticType))
1448-
CI->DiagnosticType = std::string(SI->getValue());
1448+
CI->DiagnosticType = SI->getValue().str();
14491449
const Init *DiagnosticString = Rec->getValueInit("DiagnosticString");
14501450
if (const StringInit *SI = dyn_cast<StringInit>(DiagnosticString))
1451-
CI->DiagnosticString = std::string(SI->getValue());
1451+
CI->DiagnosticString = SI->getValue().str();
14521452
// If we have a DiagnosticString, we need a DiagnosticType for use within
14531453
// the matcher.
14541454
if (!CI->DiagnosticString.empty() && CI->DiagnosticType.empty())
@@ -1461,7 +1461,7 @@ void AsmMatcherInfo::buildOperandClasses() {
14611461
// Get or construct the default method name.
14621462
const Init *DMName = Rec->getValueInit("DefaultMethod");
14631463
if (const StringInit *SI = dyn_cast<StringInit>(DMName)) {
1464-
CI->DefaultMethod = std::string(SI->getValue());
1464+
CI->DefaultMethod = SI->getValue().str();
14651465
} else {
14661466
assert(isa<UnsetInit>(DMName) && "Unexpected DefaultMethod field!");
14671467
CI->DefaultMethod = "default" + CI->ClassName + "Operands";
@@ -3057,7 +3057,7 @@ static void emitAsmTiedOperandConstraints(CodeGenTarget &Target,
30573057
AsmMatcherInfo &Info, raw_ostream &OS,
30583058
bool HasOptionalOperands) {
30593059
std::string AsmParserName =
3060-
std::string(Info.AsmParser->getValueAsString("AsmParserClassName"));
3060+
Info.AsmParser->getValueAsString("AsmParserClassName").str();
30613061
OS << "static bool ";
30623062
OS << "checkAsmTiedOperandConstraints(const " << Target.getName()
30633063
<< AsmParserName << "&AsmParser,\n";

llvm/utils/TableGen/AsmWriterEmitter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void AsmWriterEmitter::FindUniqueOperandCommands(
192192
InstIdxs[idx].push_back(i);
193193
} else {
194194
UniqueOperandCommands.push_back(std::move(Command));
195-
InstrsForCase.push_back(std::string(Inst.CGI->TheDef->getName()));
195+
InstrsForCase.push_back(Inst.CGI->TheDef->getName().str());
196196
InstIdxs.emplace_back();
197197
InstIdxs.back().push_back(i);
198198

@@ -592,9 +592,9 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
592592
// "NoRegAltName" is special. We don't need to do a lookup for that,
593593
// as it's just a reference to the default register name.
594594
if (AltName == "" || AltName == "NoRegAltName") {
595-
AsmName = std::string(Reg.TheDef->getValueAsString("AsmName"));
595+
AsmName = Reg.TheDef->getValueAsString("AsmName").str();
596596
if (AsmName.empty())
597-
AsmName = std::string(Reg.getName());
597+
AsmName = Reg.getName().str();
598598
} else {
599599
// Make sure the register has an alternate name for this index.
600600
std::vector<const Record *> AltNameList =
@@ -612,7 +612,7 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
612612
PrintFatalError(Reg.TheDef->getLoc(),
613613
"Register definition missing alt name for '" +
614614
AltName + "'.");
615-
AsmName = std::string(AltNames[Idx]);
615+
AsmName = AltNames[Idx].str();
616616
}
617617
}
618618
StringTable.add(AsmName);
@@ -940,7 +940,7 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
940940
}) -
941941
PrintMethods.begin();
942942
if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
943-
PrintMethods.emplace_back(std::string(PrintMethod), IsPCRel);
943+
PrintMethods.emplace_back(PrintMethod.str(), IsPCRel);
944944
}
945945
}
946946

llvm/utils/TableGen/CodeEmitterGen.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ CodeEmitterGen::getInstructionCases(const Record *R,
309309
" case " + itostr(DefaultMode) + ": InstBitsByHw = InstBits";
310310
} else {
311311
Case += " case " + itostr(ModeId) +
312-
": InstBitsByHw = InstBits_" +
313-
std::string(HWM.getMode(ModeId).Name);
312+
": InstBitsByHw = InstBits_" + HWM.getMode(ModeId).Name.str();
314313
}
315314
Case += "; break;\n";
316315
}
@@ -362,7 +361,7 @@ void CodeEmitterGen::addInstructionCasesForEncoding(
362361
if (RV.isNonconcreteOK() || RV.getValue()->isComplete())
363362
continue;
364363

365-
Success &= addCodeToMergeInOperand(R, BI, std::string(RV.getName()), Case,
364+
Success &= addCodeToMergeInOperand(R, BI, RV.getName().str(), Case,
366365
BitOffsetCase, Target);
367366
}
368367
// Avoid empty switches.

llvm/utils/TableGen/CodeGenMapTable.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class InstrMap {
103103

104104
public:
105105
InstrMap(const Record *MapRec) {
106-
Name = std::string(MapRec->getName());
106+
Name = MapRec->getName().str();
107107

108108
// FilterClass - It's used to reduce the search space only to the
109109
// instructions that define the kind of relationship modeled by
@@ -133,8 +133,8 @@ class InstrMap {
133133

134134
// Each instruction map must specify at least one column for it to be valid.
135135
if (ColValList->empty())
136-
PrintFatalError(MapRec->getLoc(), "InstrMapping record `" +
137-
MapRec->getName() + "' has empty " +
136+
PrintFatalError(MapRec->getLoc(), "InstrMapping record `" + Name +
137+
"' has empty " +
138138
"`ValueCols' field!");
139139

140140
for (const Init *I : ColValList->getValues()) {
@@ -144,7 +144,7 @@ class InstrMap {
144144
// elements as the fields in 'ColFields'.
145145
if (ColI->size() != ColFields->size())
146146
PrintFatalError(MapRec->getLoc(),
147-
"Record `" + MapRec->getName() +
147+
"Record `" + Name +
148148
"', field `ValueCols' entries don't match with " +
149149
" the entries in 'ColFields'!");
150150
ValueCols.push_back(ColI);

llvm/utils/TableGen/Common/AsmWriterInst.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct AsmWriterOperand {
3838
unsigned MIOpNo = 0;
3939

4040
/// Str - For isLiteralTextOperand, this IS the literal text. For
41-
/// isMachineInstrOperand, this is the PrinterMethodName for the operand..
41+
/// isMachineInstrOperand, this is the PrinterMethodName for the operand.
4242
/// For isLiteralStatementOperand, this is the code to insert verbatim
4343
/// into the asm writer.
4444
std::string Str;

llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ std::string TreePredicateFn::getPredCode() const {
11491149
Code += "if (!N->hasNUsesOfValue(1, 0)) return false;\n";
11501150

11511151
std::string PredicateCode =
1152-
std::string(PatFragRec->getRecord()->getValueAsString("PredicateCode"));
1152+
PatFragRec->getRecord()->getValueAsString("PredicateCode").str();
11531153

11541154
Code += PredicateCode;
11551155

@@ -1164,8 +1164,7 @@ bool TreePredicateFn::hasImmCode() const {
11641164
}
11651165

11661166
std::string TreePredicateFn::getImmCode() const {
1167-
return std::string(
1168-
PatFragRec->getRecord()->getValueAsString("ImmediateCode"));
1167+
return PatFragRec->getRecord()->getValueAsString("ImmediateCode").str();
11691168
}
11701169

11711170
bool TreePredicateFn::immCodeUsesAPInt() const {
@@ -1286,11 +1285,13 @@ const Record *TreePredicateFn::getScalarMemoryVT() const {
12861285
return nullptr;
12871286
return R->getValueAsDef("ScalarMemoryVT");
12881287
}
1288+
12891289
bool TreePredicateFn::hasGISelPredicateCode() const {
12901290
return !PatFragRec->getRecord()
12911291
->getValueAsString("GISelPredicateCode")
12921292
.empty();
12931293
}
1294+
12941295
std::string TreePredicateFn::getGISelPredicateCode() const {
12951296
return std::string(
12961297
PatFragRec->getRecord()->getValueAsString("GISelPredicateCode"));
@@ -2916,7 +2917,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
29162917
if (R->getName() == "node" && !OpName.empty()) {
29172918
if (OpName.empty())
29182919
error("'node' argument requires a name to match with operand list");
2919-
Args.push_back(std::string(OpName));
2920+
Args.push_back(OpName.str());
29202921
}
29212922

29222923
Res->setName(OpName);
@@ -2928,7 +2929,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
29282929
if (OpName.empty())
29292930
error("'?' argument requires a name to match with operand list");
29302931
TreePatternNodePtr Res = makeIntrusiveRefCnt<TreePatternNode>(TheInit, 1);
2931-
Args.push_back(std::string(OpName));
2932+
Args.push_back(OpName.str());
29322933
Res->setName(OpName);
29332934
return Res;
29342935
}
@@ -3168,7 +3169,7 @@ bool TreePattern::InferAllTypes(
31683169
if (InNamedTypes) {
31693170
auto InIter = InNamedTypes->find(Entry.getKey());
31703171
if (InIter == InNamedTypes->end()) {
3171-
error("Node '" + std::string(Entry.getKey()) +
3172+
error("Node '" + Entry.getKey().str() +
31723173
"' in output pattern but not input pattern");
31733174
return true;
31743175
}
@@ -3300,7 +3301,7 @@ void CodeGenDAGPatterns::ParseNodeTransforms() {
33003301
reverse(Records.getAllDerivedDefinitions("SDNodeXForm"))) {
33013302
const Record *SDNode = XFormNode->getValueAsDef("Opcode");
33023303
StringRef Code = XFormNode->getValueAsString("XFormFunction");
3303-
SDNodeXForms.insert({XFormNode, NodeXForm(SDNode, std::string(Code))});
3304+
SDNodeXForms.insert({XFormNode, NodeXForm(SDNode, Code.str())});
33043305
}
33053306
}
33063307

@@ -3359,7 +3360,7 @@ void CodeGenDAGPatterns::ParsePatternFragments(bool OutFrags) {
33593360
if (!OperandsSet.erase(ArgNameStr))
33603361
P->error("'" + ArgNameStr +
33613362
"' does not occur in pattern or was multiply specified!");
3362-
Args.push_back(std::string(ArgNameStr));
3363+
Args.push_back(ArgNameStr.str());
33633364
}
33643365

33653366
if (!OperandsSet.empty())

llvm/utils/TableGen/Common/CodeGenDAGPatterns.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class ScopedName {
406406

407407
public:
408408
ScopedName(unsigned Scope, StringRef Identifier)
409-
: Scope(Scope), Identifier(std::string(Identifier)) {
409+
: Scope(Scope), Identifier(Identifier.str()) {
410410
assert(Scope != 0 &&
411411
"Scope == 0 is used to indicate predicates without arguments");
412412
}

llvm/utils/TableGen/Common/CodeGenHwModes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ StringRef CodeGenHwModes::DefaultModeName = "DefaultMode";
2020

2121
HwMode::HwMode(const Record *R) {
2222
Name = R->getName();
23-
Features = std::string(R->getValueAsString("Features"));
23+
Features = R->getValueAsString("Features").str();
2424

2525
SmallString<128> PredicateCheck;
2626
raw_svector_ostream OS(PredicateCheck);

0 commit comments

Comments
 (0)