[llvm] r313211 - Factor out duplicate code from llvm-dwarfdump (NFC)
Adrian Prantl via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 18 15:10:08 PDT 2017
> On Sep 18, 2017, at 3:07 PM, David Blaikie <dblaikie at gmail.com> wrote:
>
> Range-for is probably more legible/terse than std::for_each these days (back when we didn't have C++11 features, there was some more merit in std::for_each, especially if you had a named functor around anyway)
Good point. I think this instance might pre-date C++11 support. I'll update it to a loop.
-- adrian
> On Wed, Sep 13, 2017 at 4:08 PM Adrian Prantl via llvm-commits <llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>> wrote:
> Author: adrian
> Date: Wed Sep 13 16:07:24 2017
> New Revision: 313211
>
> URL: http://llvm.org/viewvc/llvm-project?rev=313211&view=rev <http://llvm.org/viewvc/llvm-project?rev=313211&view=rev>
> Log:
> Factor out duplicate code from llvm-dwarfdump (NFC)
>
> Modified:
> llvm/trunk/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
>
> Modified: llvm/trunk/tools/llvm-dwarfdump/llvm-dwarfdump.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dwarfdump/llvm-dwarfdump.cpp?rev=313211&r1=313210&r2=313211&view=diff <http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-dwarfdump/llvm-dwarfdump.cpp?rev=313211&r1=313210&r2=313211&view=diff>
> ==============================================================================
> --- llvm/trunk/tools/llvm-dwarfdump/llvm-dwarfdump.cpp (original)
> +++ llvm/trunk/tools/llvm-dwarfdump/llvm-dwarfdump.cpp Wed Sep 13 16:07:24 2017
> @@ -85,7 +85,7 @@ static void error(StringRef Filename, st
> exit(1);
> }
>
> -static DIDumpOptions GetDumpOpts() {
> +static DIDumpOptions getDumpOpts() {
> DIDumpOptions DumpOpts;
> DumpOpts.DumpType = DumpType;
> DumpOpts.SummarizeTypes = SummarizeTypes;
> @@ -93,7 +93,7 @@ static DIDumpOptions GetDumpOpts() {
> return DumpOpts;
> }
>
> -static void DumpObjectFile(ObjectFile &Obj, Twine Filename) {
> +static bool dumpObjectFile(ObjectFile &Obj, Twine Filename) {
> std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj);
> logAllUnhandledErrors(DICtx->loadRegisterInfo(Obj), errs(),
> Filename.str() + ": ");
> @@ -102,32 +102,11 @@ static void DumpObjectFile(ObjectFile &O
> outs() << Filename << ":\tfile format " << Obj.getFileFormatName() << '\n';
>
> // Dump the complete DWARF structure.
> - DICtx->dump(outs(), GetDumpOpts());
> + DICtx->dump(outs(), getDumpOpts());
> + return true;
> }
>
> -static void DumpInput(StringRef Filename) {
> - ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
> - MemoryBuffer::getFileOrSTDIN(Filename);
> - error(Filename, BuffOrErr.getError());
> - std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
> -
> - Expected<std::unique_ptr<Binary>> BinOrErr =
> - object::createBinary(Buff->getMemBufferRef());
> - if (!BinOrErr)
> - error(Filename, errorToErrorCode(BinOrErr.takeError()));
> -
> - if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
> - DumpObjectFile(*Obj, Filename);
> - else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
> - for (auto &ObjForArch : Fat->objects()) {
> - auto MachOOrErr = ObjForArch.getAsObjectFile();
> - error(Filename, errorToErrorCode(MachOOrErr.takeError()));
> - DumpObjectFile(**MachOOrErr,
> - Filename + " (" + ObjForArch.getArchFlagName() + ")");
> - }
> -}
> -
> -static bool VerifyObjectFile(ObjectFile &Obj, Twine Filename) {
> +static bool verifyObjectFile(ObjectFile &Obj, Twine Filename) {
> std::unique_ptr<DIContext> DICtx = DWARFContext::create(Obj);
>
> // Verify the DWARF and exit with non-zero exit status if verification
> @@ -135,7 +114,7 @@ static bool VerifyObjectFile(ObjectFile
> raw_ostream &stream = Quiet ? nulls() : outs();
> stream << "Verifying " << Filename.str() << ":\tfile format "
> << Obj.getFileFormatName() << "\n";
> - bool Result = DICtx->verify(stream, DumpType, GetDumpOpts());
> + bool Result = DICtx->verify(stream, DumpType, getDumpOpts());
> if (Result)
> stream << "No errors.\n";
> else
> @@ -143,30 +122,36 @@ static bool VerifyObjectFile(ObjectFile
> return Result;
> }
>
> -static bool VerifyInput(StringRef Filename) {
> - ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
> - MemoryBuffer::getFileOrSTDIN(Filename);
> - error(Filename, BuffOrErr.getError());
> - std::unique_ptr<MemoryBuffer> Buff = std::move(BuffOrErr.get());
> -
> +static bool handleBuffer(StringRef Filename, MemoryBuffer &Buffer,
> + std::function<bool(ObjectFile &, Twine)> HandleObj) {
> Expected<std::unique_ptr<Binary>> BinOrErr =
> - object::createBinary(Buff->getMemBufferRef());
> + object::createBinary(Buffer.getMemBufferRef());
> if (!BinOrErr)
> error(Filename, errorToErrorCode(BinOrErr.takeError()));
>
> bool Result = true;
> if (auto *Obj = dyn_cast<ObjectFile>(BinOrErr->get()))
> - Result = VerifyObjectFile(*Obj, Filename);
> + Result = HandleObj(*Obj, Filename);
> else if (auto *Fat = dyn_cast<MachOUniversalBinary>(BinOrErr->get()))
> for (auto &ObjForArch : Fat->objects()) {
> auto MachOOrErr = ObjForArch.getAsObjectFile();
> error(Filename, errorToErrorCode(MachOOrErr.takeError()));
> - if (!VerifyObjectFile(**MachOOrErr, Filename + " (" + ObjForArch.getArchFlagName() + ")"))
> + if (!HandleObj(**MachOOrErr,
> + Filename + " (" + ObjForArch.getArchFlagName() + ")"))
> Result = false;
> }
> return Result;
> }
>
> +static bool handleFile(StringRef Filename,
> + std::function<bool(ObjectFile &, Twine)> HandleObj) {
> + ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
> + MemoryBuffer::getFileOrSTDIN(Filename);
> + error(Filename, BuffOrErr.getError());
> + std::unique_ptr<MemoryBuffer> Buffer = std::move(BuffOrErr.get());
> + return handleBuffer(Filename, *Buffer, HandleObj);
> +}
> +
> /// If the input path is a .dSYM bundle (as created by the dsymutil tool),
> /// replace it with individual entries for each of the object files inside the
> /// bundle otherwise return the input path.
> @@ -251,10 +236,14 @@ int main(int argc, char **argv) {
>
> if (Verify) {
> // If we encountered errors during verify, exit with a non-zero exit status.
> - if (!std::all_of(Objects.begin(), Objects.end(), VerifyInput))
> + if (!std::all_of(Objects.begin(), Objects.end(), [](std::string Object) {
> + return handleFile(Object, verifyObjectFile);
> + }))
> exit(1);
> } else {
> - std::for_each(Objects.begin(), Objects.end(), DumpInput);
> + std::for_each(Objects.begin(), Objects.end(), [](std::string Object) {
> + handleFile(Object, dumpObjectFile);
> + });
> }
>
> return EXIT_SUCCESS;
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org <mailto:llvm-commits at lists.llvm.org>
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170918/8388a07b/attachment.html>
More information about the llvm-commits
mailing list