Skip to content

Commit

Permalink
[llvm] Avoid 'raw_string_ostream::str' (NFC)
Browse files Browse the repository at this point in the history
Since `raw_string_ostream` doesn't own the string buffer, it is
desirable (in terms of memory safety) for users to directly reference
the string buffer rather than use `raw_string_ostream::str()`.

Work towards TODO item to remove `raw_string_ostream::str()`.
  • Loading branch information
JOE1994 committed Jul 5, 2024
1 parent dc1da93 commit 3485540
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/MIRPrintingPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ struct MIRPrintingPass : public MachineFunctionPass {
std::string Str;
raw_string_ostream StrOS(Str);
printMIR(StrOS, MF);
MachineFunctions.append(StrOS.str());
MachineFunctions.append(Str);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/FileCheck/FileCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ std::string Check::FileCheckType::getModifiersDescription() const {
if (isLiteralMatch())
OS << "LITERAL";
OS << '}';
return OS.str();
return Ret;
}

std::string Check::FileCheckType::getDescription(StringRef Prefix) const {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/IR/DiagnosticInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ std::string DiagnosticInfoOptimizationBase::getMsg() const {
? Args.end()
: Args.begin() + FirstExtraArgIndex))
OS << Arg.Val;
return OS.str();
return Str;
}

DiagnosticInfoMisExpect::DiagnosticInfoMisExpect(const Instruction *Inst,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Remarks/Remark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ std::string Remark::getArgsAsMsg() const {
raw_string_ostream OS(Str);
for (const Argument &Arg : Args)
OS << Arg.Val;
return OS.str();
return Str;
}

/// Returns the value of a specified key parsed from StringRef.
Expand Down
8 changes: 4 additions & 4 deletions llvm/tools/llvm-opt-report/OptReport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,10 @@ static bool writeReport(LocationInfoTy &LocationInfo) {

if (!Succinct) {
RS << LLI.UnrollCount;
RS << std::string(UCDigits - RS.str().size(), ' ');
RS << std::string(UCDigits - R.size(), ' ');
}

return RS.str();
return R;
};

auto VStr = [VFDigits,
Expand All @@ -383,10 +383,10 @@ static bool writeReport(LocationInfoTy &LocationInfo) {

if (!Succinct) {
RS << LLI.VectorizationFactor << "," << LLI.InterleaveCount;
RS << std::string(VFDigits + ICDigits + 1 - RS.str().size(), ' ');
RS << std::string(VFDigits + ICDigits + 1 - R.size(), ' ');
}

return RS.str();
return R;
};

OS << llvm::format_decimal(L, LNDigits) << " ";
Expand Down
10 changes: 5 additions & 5 deletions llvm/utils/TableGen/CompressInstEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
}

if (CompressPatterns.empty()) {
OS << FuncH.str();
OS << FH;
OS.indent(2) << "return false;\n}\n";
if (EType == EmitterType::Compress)
OS << "\n#endif //GEN_COMPRESS_INSTR\n";
Expand Down Expand Up @@ -835,10 +835,10 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
}
if (CompressOrUncompress)
CodeStream.indent(6) << "OutInst.setLoc(MI.getLoc());\n";
mergeCondAndCode(CaseStream, CondStream.str(), CodeStream.str());
mergeCondAndCode(CaseStream, CondString, CodeString);
PrevOp = CurOp;
}
Func << CaseStream.str() << "\n";
Func << CaseString << "\n";
// Close brace for the last case.
Func.indent(4) << "} // case " << CurOp << "\n";
Func.indent(2) << "} // switch\n";
Expand Down Expand Up @@ -876,8 +876,8 @@ void CompressInstEmitter::emitCompressInstEmitter(raw_ostream &OS,
<< "}\n\n";
}

OS << FuncH.str();
OS << Func.str();
OS << FH;
OS << F;

if (EType == EmitterType::Compress)
OS << "\n#endif //GEN_COMPRESS_INSTR\n";
Expand Down
4 changes: 2 additions & 2 deletions llvm/utils/TableGen/X86DisassemblerTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,11 +1062,11 @@ void DisassemblerTables::emit(raw_ostream &o) const {
i1--;
emitContextDecisions(o1, o2, i1, i2, ModRMTableNum);

o << o1.str();
o << s1;
o << " 0x0\n";
o << "};\n";
o << "\n";
o << o2.str();
o << s2;
o << "\n";
o << "\n";
}
Expand Down

1 comment on commit 3485540

@MaskRay
Copy link
Member

@MaskRay MaskRay commented on 3485540 Jul 5, 2024

Choose a reason for hiding this comment

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

@aengelke

@JOE1994 I think str() is probably desirable. See #97704

Please sign in to comment.