From 255907042245b77779e3e38c5ce66901866cabe5 Mon Sep 17 00:00:00 2001 From: Alp Toker Date: Thu, 26 Jun 2014 00:00:48 +0000 Subject: Introduce a string_ostream string builder facilty string_ostream is a safe and efficient string builder that combines opaque stack storage with a built-in ostream interface. small_string_ostream additionally permits an explicit stack storage size other than the default 128 bytes to be provided. Beyond that, storage is transferred to the heap. This convenient class can be used in most places an std::string+raw_string_ostream pair or SmallString<>+raw_svector_ostream pair would previously have been used, in order to guarantee consistent access without byte truncation. The patch also converts much of LLVM to use the new facility. These changes include several probable bug fixes for truncated output, a programming error that's no longer possible with the new interface. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211749 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 3 +-- lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp | 12 ++++-------- lib/CodeGen/MachineBlockFrequencyInfo.cpp | 7 +++---- lib/CodeGen/MachineBlockPlacement.cpp | 12 ++++-------- lib/CodeGen/MachineFunction.cpp | 5 ++--- lib/CodeGen/MachineScheduler.cpp | 3 +-- lib/CodeGen/ScheduleDAGInstrs.cpp | 3 +-- lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp | 3 +-- lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp | 3 +-- lib/CodeGen/TargetSchedule.cpp | 9 ++++----- 10 files changed, 22 insertions(+), 38 deletions(-) (limited to 'lib/CodeGen') diff --git a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 799ee92d3a..ebb49c34a7 100644 --- a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1585,8 +1585,7 @@ static const MCExpr *lowerConstant(const Constant *CV, AsmPrinter &AP) { // Otherwise report the problem to the user. { - std::string S; - raw_string_ostream OS(S); + string_ostream OS; OS << "Unsupported expression in static initializer: "; CE->printAsOperand(OS, /*PrintType=*/false, !AP.MF ? nullptr : AP.MF->getFunction()->getParent()); diff --git a/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp b/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp index 46ee0c856b..01f26d0c4c 100644 --- a/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp +++ b/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp @@ -241,8 +241,7 @@ static void EmitMSInlineAsmStr(const char *AsmStr, const MachineInstr *MI, } } if (Error) { - std::string msg; - raw_string_ostream Msg(msg); + string_ostream Msg; Msg << "invalid operand in inline asm: '" << AsmStr << "'"; MMI->getModule()->getContext().emitError(LocCookie, Msg.str()); } @@ -413,8 +412,7 @@ static void EmitGCCInlineAsmStr(const char *AsmStr, const MachineInstr *MI, } } if (Error) { - std::string msg; - raw_string_ostream Msg(msg); + string_ostream Msg; Msg << "invalid operand in inline asm: '" << AsmStr << "'"; MMI->getModule()->getContext().emitError(LocCookie, Msg.str()); } @@ -471,8 +469,7 @@ void AsmPrinter::EmitInlineAsm(const MachineInstr *MI) const { // Emit the inline asm to a temporary string so we can emit it through // EmitInlineAsm. - SmallString<256> StringData; - raw_svector_ostream OS(StringData); + small_string_ostream<256> OS; // The variant of the current asmprinter. int AsmPrinterVariant = MAI->getAssemblerDialect(); @@ -517,8 +514,7 @@ void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS, } OS << Counter; } else { - std::string msg; - raw_string_ostream Msg(msg); + string_ostream Msg; Msg << "Unknown special formatter '" << Code << "' for machine instr: " << *MI; report_fatal_error(Msg.str()); diff --git a/lib/CodeGen/MachineBlockFrequencyInfo.cpp b/lib/CodeGen/MachineBlockFrequencyInfo.cpp index 9151d99089..35e4a56cec 100644 --- a/lib/CodeGen/MachineBlockFrequencyInfo.cpp +++ b/lib/CodeGen/MachineBlockFrequencyInfo.cpp @@ -89,10 +89,9 @@ struct DOTGraphTraits : std::string getNodeLabel(const MachineBasicBlock *Node, const MachineBlockFrequencyInfo *Graph) { - std::string Result; - raw_string_ostream OS(Result); + string_ostream OS; - OS << Node->getName().str() << ":"; + OS << Node->getName() << ":"; switch (ViewMachineBlockFreqPropagationDAG) { case GVDT_Fraction: Graph->printBlockFreq(OS, Node); @@ -105,7 +104,7 @@ struct DOTGraphTraits : "never reach this point."); } - return Result; + return OS.str(); } }; diff --git a/lib/CodeGen/MachineBlockPlacement.cpp b/lib/CodeGen/MachineBlockPlacement.cpp index 74af1e2d64..891f605e2a 100644 --- a/lib/CodeGen/MachineBlockPlacement.cpp +++ b/lib/CodeGen/MachineBlockPlacement.cpp @@ -264,23 +264,19 @@ INITIALIZE_PASS_END(MachineBlockPlacement, "block-placement2", /// /// Only used by debug logging. static std::string getBlockName(MachineBasicBlock *BB) { - std::string Result; - raw_string_ostream OS(Result); + string_ostream OS; OS << "BB#" << BB->getNumber() << " (derived from LLVM BB '" << BB->getName() << "')"; - OS.flush(); - return Result; + return OS.str(); } /// \brief Helper to print the number of a MBB. /// /// Only used by debug logging. static std::string getBlockNum(MachineBasicBlock *BB) { - std::string Result; - raw_string_ostream OS(Result); + string_ostream OS; OS << "BB#" << BB->getNumber(); - OS.flush(); - return Result; + return OS.str(); } #endif diff --git a/lib/CodeGen/MachineFunction.cpp b/lib/CodeGen/MachineFunction.cpp index 6138aef4ad..20210adf30 100644 --- a/lib/CodeGen/MachineFunction.cpp +++ b/lib/CodeGen/MachineFunction.cpp @@ -465,9 +465,8 @@ MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, const char *Prefix = isLinkerPrivate ? DL->getLinkerPrivateGlobalPrefix() : DL->getPrivateGlobalPrefix(); - SmallString<60> Name; - raw_svector_ostream(Name) - << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; + small_string_ostream<60> Name; + Name << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; return Ctx.GetOrCreateSymbol(Name.str()); } diff --git a/lib/CodeGen/MachineScheduler.cpp b/lib/CodeGen/MachineScheduler.cpp index 0baf2a6c1c..f7459bbf6f 100644 --- a/lib/CodeGen/MachineScheduler.cpp +++ b/lib/CodeGen/MachineScheduler.cpp @@ -3235,8 +3235,7 @@ struct DOTGraphTraits : public DefaultDOTGraphTraits { } static std::string getNodeLabel(const SUnit *SU, const ScheduleDAG *G) { - std::string Str; - raw_string_ostream SS(Str); + string_ostream SS; const ScheduleDAGMI *DAG = static_cast(G); const SchedDFSResult *DFS = DAG->hasVRegLiveness() ? static_cast(G)->getDFSResult() : nullptr; diff --git a/lib/CodeGen/ScheduleDAGInstrs.cpp b/lib/CodeGen/ScheduleDAGInstrs.cpp index 92a9a30f24..baf4af6aba 100644 --- a/lib/CodeGen/ScheduleDAGInstrs.cpp +++ b/lib/CodeGen/ScheduleDAGInstrs.cpp @@ -1197,8 +1197,7 @@ void ScheduleDAGInstrs::dumpNode(const SUnit *SU) const { } std::string ScheduleDAGInstrs::getGraphNodeLabel(const SUnit *SU) const { - std::string s; - raw_string_ostream oss(s); + string_ostream oss; if (SU == &EntrySU) oss << ""; else if (SU == &ExitSU) diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp index 57e22e21c3..a2cc290816 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp @@ -3245,8 +3245,7 @@ SelectCodeCommon(SDNode *NodeToMatch, const unsigned char *MatcherTable, void SelectionDAGISel::CannotYetSelect(SDNode *N) { - std::string msg; - raw_string_ostream Msg(msg); + string_ostream Msg; Msg << "Cannot select: "; if (N->getOpcode() != ISD::INTRINSIC_W_CHAIN && diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp index 4df5ede388..680a7ec5dd 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGPrinter.cpp @@ -268,8 +268,7 @@ void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) { } std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const { - std::string s; - raw_string_ostream O(s); + string_ostream O; O << "SU(" << SU->NodeNum << "): "; if (SU->getNode()) { SmallVector GluedNodes; diff --git a/lib/CodeGen/TargetSchedule.cpp b/lib/CodeGen/TargetSchedule.cpp index b0f2ca6888..d18a514a85 100644 --- a/lib/CodeGen/TargetSchedule.cpp +++ b/lib/CodeGen/TargetSchedule.cpp @@ -212,11 +212,10 @@ unsigned TargetSchedModel::computeOperandLatency( if (SCDesc->isValid() && !DefMI->getOperand(DefOperIdx).isImplicit() && !DefMI->getDesc().OpInfo[DefOperIdx].isOptionalDef() && SchedModel.isComplete()) { - std::string Err; - raw_string_ostream ss(Err); - ss << "DefIdx " << DefIdx << " exceeds machine model writes for " - << *DefMI; - report_fatal_error(ss.str()); + string_ostream Err; + Err << "DefIdx " << DefIdx << " exceeds machine model writes for " + << *DefMI; + report_fatal_error(Err.str()); } #endif // FIXME: Automatically giving all implicit defs defaultDefLatency is -- cgit v1.2.3