This moves the `reportUniqueWarning` method to the base class.
My motivation is the following:
I've experimented with replacing `reportWarning` calls with `reportUniqueWarning`
in ELF dumper. I've found that for example for removing them from `DynRegionInfo` helper
class, it is worth to pass a dumper instance to it (to be able to call dumper()->reportUniqueWarning()).
The problem was that `ELFDumper<ELFT>` is a template class. I had to make `DynRegionInfo` to be templated
and do lots of minor changes everywhere what did not look reasonable/nice.
At the same time I guess one day other dumpers like COFF/MachO/Wasm etc might want to
start using `reportUniqueWarning` API too. Then it looks reasonable to move the logic to the
base class.
With that the problem of passing the dumper instance will be gone.
Differential revision: https://reviews.llvm.org/D92218
public:
friend class COFFObjectDumpDelegate;
COFFDumper(const llvm::object::COFFObjectFile *Obj, ScopedPrinter &Writer)
- : ObjDumper(Writer), Obj(Obj), Writer(Writer), Types(100) {}
+ : ObjDumper(Writer, Obj->getFileName()), Obj(Obj), Writer(Writer),
+ Types(100) {}
void printFileHeaders() override;
void printSectionHeaders() override;
#include <memory>
#include <string>
#include <system_error>
-#include <unordered_set>
#include <vector>
using namespace llvm;
};
mutable SmallVector<Optional<VersionEntry>, 16> VersionMap;
- std::unordered_set<std::string> Warnings;
-
std::string describe(const Elf_Shdr &Sec) const;
public:
Expected<RelSymbol<ELFT>> getRelocationTarget(const Relocation<ELFT> &R,
const Elf_Shdr *SymTab) const;
-
- std::function<Error(const Twine &Msg)> WarningHandler;
- void reportUniqueWarning(Error Err) const;
};
template <class ELFT>
};
template <class ELFT>
-void ELFDumper<ELFT>::reportUniqueWarning(Error Err) const {
- handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
- cantFail(WarningHandler(EI.message()),
- "WarningHandler should always return ErrorSuccess");
- });
-}
-
-template <class ELFT>
void DumpStyle<ELFT>::reportUniqueWarning(Error Err) const {
this->dumper().reportUniqueWarning(std::move(Err));
}
template <typename ELFT>
ELFDumper<ELFT>::ELFDumper(const object::ELFObjectFile<ELFT> &O,
ScopedPrinter &Writer)
- : ObjDumper(Writer), ObjF(O), Obj(*O.getELFFile()), DynRelRegion(O),
- DynRelaRegion(O), DynRelrRegion(O), DynPLTRelRegion(O), DynamicTable(O) {
- // Dumper reports all non-critical errors as warnings.
- // It does not print the same warning more than once.
- WarningHandler = [this](const Twine &Msg) {
- if (Warnings.insert(Msg.str()).second)
- reportWarning(createError(Msg), ObjF.getFileName());
- return Error::success();
- };
-
+ : ObjDumper(Writer, O.getFileName()), ObjF(O), Obj(*O.getELFFile()),
+ DynRelRegion(O), DynRelaRegion(O), DynRelrRegion(O), DynPLTRelRegion(O),
+ DynamicTable(O) {
if (opts::Output == opts::GNU)
ELFDumperStyle.reset(new GNUStyle<ELFT>(Writer, *this));
else
class MachODumper : public ObjDumper {
public:
MachODumper(const MachOObjectFile *Obj, ScopedPrinter &Writer)
- : ObjDumper(Writer), Obj(Obj) {}
+ : ObjDumper(Writer, Obj->getFileName()), Obj(Obj) {}
void printFileHeaders() override;
void printSectionHeaders() override;
return createStringError(object::object_error::parse_failed, Msg);
}
-ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {}
+ObjDumper::ObjDumper(ScopedPrinter &Writer, StringRef ObjName) : W(Writer) {
+ // Dumper reports all non-critical errors as warnings.
+ // It does not print the same warning more than once.
+ WarningHandler = [=](const Twine &Msg) {
+ if (Warnings.insert(Msg.str()).second)
+ reportWarning(createError(Msg), ObjName);
+ return Error::success();
+ };
+}
+
+ObjDumper::~ObjDumper() {}
-ObjDumper::~ObjDumper() {
+void ObjDumper::reportUniqueWarning(Error Err) const {
+ handleAllErrors(std::move(Err), [&](const ErrorInfoBase &EI) {
+ cantFail(WarningHandler(EI.message()),
+ "WarningHandler should always return ErrorSuccess");
+ });
}
static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/CommandLine.h"
+#include <unordered_set>
+
namespace llvm {
namespace object {
class COFFImportFile;
class ObjDumper {
public:
- ObjDumper(ScopedPrinter &Writer);
+ ObjDumper(ScopedPrinter &Writer, StringRef ObjName);
virtual ~ObjDumper();
virtual bool canDumpContent() { return true; }
void printSectionsAsHex(const object::ObjectFile &Obj,
ArrayRef<std::string> Sections);
+ std::function<Error(const Twine &Msg)> WarningHandler;
+ void reportUniqueWarning(Error Err) const;
+
protected:
ScopedPrinter &W;
virtual void printDynamicSymbols() {}
virtual void printProgramHeaders() {}
virtual void printSectionMapping() {}
+
+ std::unordered_set<std::string> Warnings;
};
std::unique_ptr<ObjDumper> createCOFFDumper(const object::COFFObjectFile &Obj,
class WasmDumper : public ObjDumper {
public:
WasmDumper(const WasmObjectFile *Obj, ScopedPrinter &Writer)
- : ObjDumper(Writer), Obj(Obj) {}
+ : ObjDumper(Writer, Obj->getFileName()), Obj(Obj) {}
void printFileHeaders() override;
void printSectionHeaders() override;
public:
XCOFFDumper(const XCOFFObjectFile &Obj, ScopedPrinter &Writer)
- : ObjDumper(Writer), Obj(Obj) {}
+ : ObjDumper(Writer, Obj.getFileName()), Obj(Obj) {}
void printFileHeaders() override;
void printSectionHeaders() override;