Skip to content

[NFC][TableGen] Use StringRef::str() instead of casting #139332

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

Merged
merged 1 commit into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions llvm/include/llvm/TableGen/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,7 @@ class StringInit final : public TypedInit {
return "[{" + Value.str() + "}]";
}

std::string getAsUnquotedString() const override {
return std::string(Value);
}
std::string getAsUnquotedString() const override { return Value.str(); }

const Init *getBit(unsigned Bit) const override {
llvm_unreachable("Illegal bit reference off string");
Expand Down
13 changes: 6 additions & 7 deletions llvm/lib/TableGen/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1788,22 +1788,21 @@ const Init *TernOpInit::Fold(const Record *CurRec) const {
return Val->getDefInit();
}
if (LHSv && MHSv && RHSv) {
std::string Val = std::string(RHSv->getName());
std::string Val = RHSv->getName().str();
if (LHSv->getAsString() == RHSv->getAsString())
Val = std::string(MHSv->getName());
Val = MHSv->getName().str();
return VarInit::get(Val, getType());
}
if (LHSs && MHSs && RHSs) {
std::string Val = std::string(RHSs->getValue());
std::string Val = RHSs->getValue().str();

std::string::size_type found;
std::string::size_type idx = 0;
while (true) {
found = Val.find(std::string(LHSs->getValue()), idx);
found = Val.find(LHSs->getValue().str(), idx);
if (found == std::string::npos)
break;
Val.replace(found, LHSs->getValue().size(),
std::string(MHSs->getValue()));
Val.replace(found, LHSs->getValue().size(), MHSs->getValue().str());
idx = found + MHSs->getValue().size();
}

Expand Down Expand Up @@ -2418,7 +2417,7 @@ const RecTy *DefInit::getFieldType(const StringInit *FieldName) const {
return nullptr;
}

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

static void ProfileVarDefInit(FoldingSetNodeID &ID, const Record *Class,
ArrayRef<const ArgumentInit *> Args) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/TableGen/SetTheory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ struct SequenceOp : public SetTheory::Operator {

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

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/TableGen/TGParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4327,7 +4327,7 @@ bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
// through its template argument names. Substs contains a substitution
// value for each argument, either the value specified or the default.
// Then we can resolve the template arguments.
MultiClass *MC = MultiClasses[std::string(Ref.Rec->getName())].get();
MultiClass *MC = MultiClasses[Ref.Rec->getName().str()].get();
assert(MC && "Didn't lookup multiclass correctly?");

SubstStack Substs;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/TableGen/TGParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class TGVarScope {
}

void addVar(StringRef Name, const Init *I) {
bool Ins = Vars.try_emplace(std::string(Name), I).second;
bool Ins = Vars.try_emplace(Name.str(), I).second;
(void)Ins;
assert(Ins && "Local variable already exists");
}
Expand Down
32 changes: 16 additions & 16 deletions llvm/utils/TableGen/AsmMatcherEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ bool MatchableInfo::validate(StringRef CommentDelimiter, bool IsAlias) const {
// Verify that any operand is only mentioned once.
// We reject aliases and ignore instructions for now.
if (!IsAlias && TheDef->getValueAsString("AsmMatchConverter").empty() &&
Tok[0] == '$' && !OperandNames.insert(std::string(Tok)).second) {
Tok[0] == '$' && !OperandNames.insert(Tok.str()).second) {
LLVM_DEBUG({
errs() << "warning: '" << TheDef->getName() << "': "
<< "ignoring instruction with tied operand '" << Tok << "'\n";
Expand Down Expand Up @@ -1170,15 +1170,15 @@ static std::string getEnumNameForToken(StringRef Str) {
}

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

if (!Entry) {
Classes.emplace_front();
Entry = &Classes.front();
Entry->Kind = ClassInfo::Token;
Entry->ClassName = "Token";
Entry->Name = "MCK_" + getEnumNameForToken(Token);
Entry->ValueName = std::string(Token);
Entry->ValueName = Token.str();
Entry->PredicateMethod = "<invalid>";
Entry->RenderMethod = "<invalid>";
Entry->ParserMethod = "";
Expand Down Expand Up @@ -1353,11 +1353,11 @@ void AsmMatcherInfo::buildRegisterClasses(

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

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

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

if (CI->ValueName.empty()) {
CI->ClassName = std::string(Rec->getName());
CI->ClassName = Rec->getName().str();
CI->Name = "MCK_" + Rec->getName().str();
CI->ValueName = std::string(Rec->getName());
CI->ValueName = Rec->getName().str();
} else {
CI->ValueName = CI->ValueName + "," + Rec->getName().str();
}
Expand Down Expand Up @@ -1415,14 +1415,14 @@ void AsmMatcherInfo::buildOperandClasses() {
else
CI->SuperClasses.push_back(SC);
}
CI->ClassName = std::string(Rec->getValueAsString("Name"));
CI->ClassName = Rec->getValueAsString("Name").str();
CI->Name = "MCK_" + CI->ClassName;
CI->ValueName = std::string(Rec->getName());
CI->ValueName = Rec->getName().str();

// Get or construct the predicate method name.
const Init *PMName = Rec->getValueInit("PredicateMethod");
if (const StringInit *SI = dyn_cast<StringInit>(PMName)) {
CI->PredicateMethod = std::string(SI->getValue());
CI->PredicateMethod = SI->getValue().str();
} else {
assert(isa<UnsetInit>(PMName) && "Unexpected PredicateMethod field!");
CI->PredicateMethod = "is" + CI->ClassName;
Expand All @@ -1431,7 +1431,7 @@ void AsmMatcherInfo::buildOperandClasses() {
// Get or construct the render method name.
const Init *RMName = Rec->getValueInit("RenderMethod");
if (const StringInit *SI = dyn_cast<StringInit>(RMName)) {
CI->RenderMethod = std::string(SI->getValue());
CI->RenderMethod = SI->getValue().str();
} else {
assert(isa<UnsetInit>(RMName) && "Unexpected RenderMethod field!");
CI->RenderMethod = "add" + CI->ClassName + "Operands";
Expand All @@ -1440,15 +1440,15 @@ void AsmMatcherInfo::buildOperandClasses() {
// Get the parse method name or leave it as empty.
const Init *PRMName = Rec->getValueInit("ParserMethod");
if (const StringInit *SI = dyn_cast<StringInit>(PRMName))
CI->ParserMethod = std::string(SI->getValue());
CI->ParserMethod = SI->getValue().str();

// Get the diagnostic type and string or leave them as empty.
const Init *DiagnosticType = Rec->getValueInit("DiagnosticType");
if (const StringInit *SI = dyn_cast<StringInit>(DiagnosticType))
CI->DiagnosticType = std::string(SI->getValue());
CI->DiagnosticType = SI->getValue().str();
const Init *DiagnosticString = Rec->getValueInit("DiagnosticString");
if (const StringInit *SI = dyn_cast<StringInit>(DiagnosticString))
CI->DiagnosticString = std::string(SI->getValue());
CI->DiagnosticString = SI->getValue().str();
// If we have a DiagnosticString, we need a DiagnosticType for use within
// the matcher.
if (!CI->DiagnosticString.empty() && CI->DiagnosticType.empty())
Expand All @@ -1461,7 +1461,7 @@ void AsmMatcherInfo::buildOperandClasses() {
// Get or construct the default method name.
const Init *DMName = Rec->getValueInit("DefaultMethod");
if (const StringInit *SI = dyn_cast<StringInit>(DMName)) {
CI->DefaultMethod = std::string(SI->getValue());
CI->DefaultMethod = SI->getValue().str();
} else {
assert(isa<UnsetInit>(DMName) && "Unexpected DefaultMethod field!");
CI->DefaultMethod = "default" + CI->ClassName + "Operands";
Expand Down Expand Up @@ -3057,7 +3057,7 @@ static void emitAsmTiedOperandConstraints(CodeGenTarget &Target,
AsmMatcherInfo &Info, raw_ostream &OS,
bool HasOptionalOperands) {
std::string AsmParserName =
std::string(Info.AsmParser->getValueAsString("AsmParserClassName"));
Info.AsmParser->getValueAsString("AsmParserClassName").str();
OS << "static bool ";
OS << "checkAsmTiedOperandConstraints(const " << Target.getName()
<< AsmParserName << "&AsmParser,\n";
Expand Down
10 changes: 5 additions & 5 deletions llvm/utils/TableGen/AsmWriterEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void AsmWriterEmitter::FindUniqueOperandCommands(
InstIdxs[idx].push_back(i);
} else {
UniqueOperandCommands.push_back(std::move(Command));
InstrsForCase.push_back(std::string(Inst.CGI->TheDef->getName()));
InstrsForCase.push_back(Inst.CGI->TheDef->getName().str());
InstIdxs.emplace_back();
InstIdxs.back().push_back(i);

Expand Down Expand Up @@ -592,9 +592,9 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
// "NoRegAltName" is special. We don't need to do a lookup for that,
// as it's just a reference to the default register name.
if (AltName == "" || AltName == "NoRegAltName") {
AsmName = std::string(Reg.TheDef->getValueAsString("AsmName"));
AsmName = Reg.TheDef->getValueAsString("AsmName").str();
if (AsmName.empty())
AsmName = std::string(Reg.getName());
AsmName = Reg.getName().str();
} else {
// Make sure the register has an alternate name for this index.
std::vector<const Record *> AltNameList =
Expand All @@ -612,7 +612,7 @@ emitRegisterNameString(raw_ostream &O, StringRef AltName,
PrintFatalError(Reg.TheDef->getLoc(),
"Register definition missing alt name for '" +
AltName + "'.");
AsmName = std::string(AltNames[Idx]);
AsmName = AltNames[Idx].str();
}
}
StringTable.add(AsmName);
Expand Down Expand Up @@ -940,7 +940,7 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
}) -
PrintMethods.begin();
if (static_cast<unsigned>(PrintMethodIdx) == PrintMethods.size())
PrintMethods.emplace_back(std::string(PrintMethod), IsPCRel);
PrintMethods.emplace_back(PrintMethod.str(), IsPCRel);
}
}

Expand Down
5 changes: 2 additions & 3 deletions llvm/utils/TableGen/CodeEmitterGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ CodeEmitterGen::getInstructionCases(const Record *R,
" case " + itostr(DefaultMode) + ": InstBitsByHw = InstBits";
} else {
Case += " case " + itostr(ModeId) +
": InstBitsByHw = InstBits_" +
std::string(HWM.getMode(ModeId).Name);
": InstBitsByHw = InstBits_" + HWM.getMode(ModeId).Name.str();
}
Case += "; break;\n";
}
Expand Down Expand Up @@ -362,7 +361,7 @@ void CodeEmitterGen::addInstructionCasesForEncoding(
if (RV.isNonconcreteOK() || RV.getValue()->isComplete())
continue;

Success &= addCodeToMergeInOperand(R, BI, std::string(RV.getName()), Case,
Success &= addCodeToMergeInOperand(R, BI, RV.getName().str(), Case,
BitOffsetCase, Target);
}
// Avoid empty switches.
Expand Down
8 changes: 4 additions & 4 deletions llvm/utils/TableGen/CodeGenMapTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class InstrMap {

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

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

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

for (const Init *I : ColValList->getValues()) {
Expand All @@ -144,7 +144,7 @@ class InstrMap {
// elements as the fields in 'ColFields'.
if (ColI->size() != ColFields->size())
PrintFatalError(MapRec->getLoc(),
"Record `" + MapRec->getName() +
"Record `" + Name +
"', field `ValueCols' entries don't match with " +
" the entries in 'ColFields'!");
ValueCols.push_back(ColI);
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/TableGen/Common/AsmWriterInst.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct AsmWriterOperand {
unsigned MIOpNo = 0;

/// Str - For isLiteralTextOperand, this IS the literal text. For
/// isMachineInstrOperand, this is the PrinterMethodName for the operand..
/// isMachineInstrOperand, this is the PrinterMethodName for the operand.
/// For isLiteralStatementOperand, this is the code to insert verbatim
/// into the asm writer.
std::string Str;
Expand Down
17 changes: 9 additions & 8 deletions llvm/utils/TableGen/Common/CodeGenDAGPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ std::string TreePredicateFn::getPredCode() const {
Code += "if (!N->hasNUsesOfValue(1, 0)) return false;\n";

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

Code += PredicateCode;

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

std::string TreePredicateFn::getImmCode() const {
return std::string(
PatFragRec->getRecord()->getValueAsString("ImmediateCode"));
return PatFragRec->getRecord()->getValueAsString("ImmediateCode").str();
}

bool TreePredicateFn::immCodeUsesAPInt() const {
Expand Down Expand Up @@ -1286,11 +1285,13 @@ const Record *TreePredicateFn::getScalarMemoryVT() const {
return nullptr;
return R->getValueAsDef("ScalarMemoryVT");
}

bool TreePredicateFn::hasGISelPredicateCode() const {
return !PatFragRec->getRecord()
->getValueAsString("GISelPredicateCode")
.empty();
}

std::string TreePredicateFn::getGISelPredicateCode() const {
return std::string(
PatFragRec->getRecord()->getValueAsString("GISelPredicateCode"));
Expand Down Expand Up @@ -2916,7 +2917,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
if (R->getName() == "node" && !OpName.empty()) {
if (OpName.empty())
error("'node' argument requires a name to match with operand list");
Args.push_back(std::string(OpName));
Args.push_back(OpName.str());
}

Res->setName(OpName);
Expand All @@ -2928,7 +2929,7 @@ TreePatternNodePtr TreePattern::ParseTreePattern(const Init *TheInit,
if (OpName.empty())
error("'?' argument requires a name to match with operand list");
TreePatternNodePtr Res = makeIntrusiveRefCnt<TreePatternNode>(TheInit, 1);
Args.push_back(std::string(OpName));
Args.push_back(OpName.str());
Res->setName(OpName);
return Res;
}
Expand Down Expand Up @@ -3168,7 +3169,7 @@ bool TreePattern::InferAllTypes(
if (InNamedTypes) {
auto InIter = InNamedTypes->find(Entry.getKey());
if (InIter == InNamedTypes->end()) {
error("Node '" + std::string(Entry.getKey()) +
error("Node '" + Entry.getKey().str() +
"' in output pattern but not input pattern");
return true;
}
Expand Down Expand Up @@ -3300,7 +3301,7 @@ void CodeGenDAGPatterns::ParseNodeTransforms() {
reverse(Records.getAllDerivedDefinitions("SDNodeXForm"))) {
const Record *SDNode = XFormNode->getValueAsDef("Opcode");
StringRef Code = XFormNode->getValueAsString("XFormFunction");
SDNodeXForms.insert({XFormNode, NodeXForm(SDNode, std::string(Code))});
SDNodeXForms.insert({XFormNode, NodeXForm(SDNode, Code.str())});
}
}

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

if (!OperandsSet.empty())
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/TableGen/Common/CodeGenDAGPatterns.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ class ScopedName {

public:
ScopedName(unsigned Scope, StringRef Identifier)
: Scope(Scope), Identifier(std::string(Identifier)) {
: Scope(Scope), Identifier(Identifier.str()) {
assert(Scope != 0 &&
"Scope == 0 is used to indicate predicates without arguments");
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/TableGen/Common/CodeGenHwModes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ StringRef CodeGenHwModes::DefaultModeName = "DefaultMode";

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

SmallString<128> PredicateCheck;
raw_svector_ostream OS(PredicateCheck);
Expand Down
Loading
Loading