Skip to content

Commit

Permalink
[llvm] Update report_fatal_error calls from raw_string_ostream to use…
Browse files Browse the repository at this point in the history
… Twine(OS.str())

As described on D111049, we're trying to remove the <string> dependency from error handling and replace uses of report_fatal_error(const std::string&) with the Twine() variant which can be forward declared.

We can use the raw_string_ostream::str() method to perform the implicit flush() and return a reference to the std::string container that we can then wrap inside Twine().
  • Loading branch information
RKSimon committed Oct 5, 2021
1 parent 5bc32ad commit 2e5daac
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 39 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Expand Up @@ -2517,7 +2517,7 @@ const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
OS << "Unsupported expression in static initializer: ";
CE->printAsOperand(OS, /*PrintType=*/false,
!MF ? nullptr : MF->getFunction().getParent());
report_fatal_error(OS.str());
report_fatal_error(Twine(OS.str()));
}
case Instruction::GetElementPtr: {
// Generate a symbolic expression for the byte address
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp
Expand Up @@ -582,7 +582,7 @@ void AsmPrinter::PrintSpecial(const MachineInstr *MI, raw_ostream &OS,
raw_string_ostream Msg(msg);
Msg << "Unknown special formatter '" << Code
<< "' for machine instr: " << *MI;
report_fatal_error(Msg.str());
report_fatal_error(Twine(Msg.str()));
}
}

Expand Down
5 changes: 1 addition & 4 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Expand Up @@ -9938,12 +9938,9 @@ SDValue SelectionDAG::getSymbolFunctionGlobalAddress(SDValue Op,

std::string ErrorStr;
raw_string_ostream ErrorFormatter(ErrorStr);

ErrorFormatter << "Undefined external symbol ";
ErrorFormatter << '"' << Symbol << '"';
ErrorFormatter.flush();

report_fatal_error(ErrorStr);
report_fatal_error(Twine(ErrorFormatter.str()));
}

//===----------------------------------------------------------------------===//
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/ExecutionEngine/MCJIT/MCJIT.cpp
Expand Up @@ -218,8 +218,7 @@ void MCJIT::generateCodeForModule(Module *M) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(LoadedObject.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> L =
Dyld.loadObject(*LoadedObject.get());
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp
Expand Up @@ -1236,8 +1236,7 @@ RuntimeDyldELF::processRelocationRef(
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
SymType = *SymTypeOrErr;
}
Expand All @@ -1257,8 +1256,7 @@ RuntimeDyldELF::processRelocationRef(
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(SectionOrErr.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
section_iterator si = *SectionOrErr;
if (si == Obj.section_end())
Expand Down
Expand Up @@ -29,8 +29,7 @@ static bool isThumbFunc(object::symbol_iterator Symbol,
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}

if (*SymTypeOrErr != object::SymbolRef::ST_Function)
Expand Down
9 changes: 3 additions & 6 deletions llvm/lib/Object/Object.cpp
Expand Up @@ -222,8 +222,7 @@ void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(SecOrErr.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
*unwrap(Sect) = *SecOrErr;
}
Expand Down Expand Up @@ -304,8 +303,7 @@ const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(Ret.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
return Ret->data();
}
Expand All @@ -316,8 +314,7 @@ uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(Ret.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
return *Ret;
}
Expand Down
Expand Up @@ -453,7 +453,7 @@ void HexagonMCCodeEmitter::EncodeSingleInstruction(const MCInst &MI,
raw_string_ostream Stream(Text);
Stream << "Unrecognized relocation combination: width=" << Width
<< " kind=" << Kind;
report_fatal_error(Stream.str());
report_fatal_error(Twine(Stream.str()));
}

/// Some insns are not extended and thus have no bits. These cases require
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
Expand Up @@ -1923,7 +1923,7 @@ NVPTXAsmPrinter::lowerConstantForGV(const Constant *CV, bool ProcessingGeneric)
OS << "Unsupported expression in static initializer: ";
CE->printAsOperand(OS, /*PrintType=*/false,
!MF ? nullptr : MF->getFunction().getParent());
report_fatal_error(OS.str());
report_fatal_error(Twine(OS.str()));
}

case Instruction::AddrSpaceCast: {
Expand All @@ -1937,7 +1937,7 @@ NVPTXAsmPrinter::lowerConstantForGV(const Constant *CV, bool ProcessingGeneric)
OS << "Unsupported expression in static initializer: ";
CE->printAsOperand(OS, /*PrintType=*/ false,
!MF ? nullptr : MF->getFunction().getParent());
report_fatal_error(OS.str());
report_fatal_error(Twine(OS.str()));
}

case Instruction::GetElementPtr: {
Expand Down
18 changes: 6 additions & 12 deletions llvm/tools/llvm-readobj/ARMWinEHPrinter.cpp
Expand Up @@ -282,8 +282,7 @@ ErrorOr<SymbolRef> Decoder::getSymbolForLocation(
std::string Buf;
llvm::raw_string_ostream OS(Buf);
logAllUnhandledErrors(AddressOrErr.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
// We apply SymbolOffset here directly. We return it separately to allow
// the caller to print it as an offset on the symbol name.
Expand Down Expand Up @@ -1006,8 +1005,7 @@ bool Decoder::dumpXDataRecord(const COFFObjectFile &COFF,
std::string Buf;
llvm::raw_string_ostream OS(Buf);
logAllUnhandledErrors(Name.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}

ListScope EHS(SW, "ExceptionHandler");
Expand Down Expand Up @@ -1046,8 +1044,7 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
std::string Buf;
llvm::raw_string_ostream OS(Buf);
logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
FunctionName = *FunctionNameOrErr;
}
Expand All @@ -1061,8 +1058,7 @@ bool Decoder::dumpUnpackedEntry(const COFFObjectFile &COFF,
std::string Buf;
llvm::raw_string_ostream OS(Buf);
logAllUnhandledErrors(Name.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}

SW.printString("ExceptionRecord",
Expand Down Expand Up @@ -1107,8 +1103,7 @@ bool Decoder::dumpPackedEntry(const object::COFFObjectFile &COFF,
std::string Buf;
llvm::raw_string_ostream OS(Buf);
logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
FunctionName = *FunctionNameOrErr;
}
Expand Down Expand Up @@ -1149,8 +1144,7 @@ bool Decoder::dumpPackedARM64Entry(const object::COFFObjectFile &COFF,
std::string Buf;
llvm::raw_string_ostream OS(Buf);
logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
FunctionName = *FunctionNameOrErr;
}
Expand Down
3 changes: 1 addition & 2 deletions llvm/tools/obj2yaml/coff2yaml.cpp
Expand Up @@ -204,8 +204,7 @@ void COFFDumper::dumpSections(unsigned NumSections) {
std::string Buf;
raw_string_ostream OS(Buf);
logAllUnhandledErrors(SymbolNameOrErr.takeError(), OS);
OS.flush();
report_fatal_error(Buf);
report_fatal_error(Twine(OS.str()));
}
if (SymbolUnique.lookup(*SymbolNameOrErr))
Rel.SymbolName = *SymbolNameOrErr;
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/Analysis/TargetLibraryInfoTest.cpp
Expand Up @@ -37,7 +37,7 @@ class TargetLibraryInfoTest : public testing::Test {
Error.print("", os);

if (!M)
report_fatal_error(os.str());
report_fatal_error(Twine(os.str()));
}

::testing::AssertionResult isLibFunc(const Function *FDecl,
Expand Down
2 changes: 1 addition & 1 deletion llvm/unittests/Analysis/VectorUtilsTest.cpp
Expand Up @@ -36,7 +36,7 @@ class VectorUtilsTest : public testing::Test {

// A failure here means that the test itself is buggy.
if (!M)
report_fatal_error(os.str());
report_fatal_error(Twine(os.str()));

Function *F = M->getFunction("test");
if (F == nullptr)
Expand Down

0 comments on commit 2e5daac

Please sign in to comment.