2017-08-01 02:33:58 +02:00
|
|
|
//===- llvm-objcopy.h -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-08-01 02:33:58 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-01 22:16:06 +01:00
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_OBJCOPY_OBJCOPY_H
|
|
|
|
#define LLVM_TOOLS_OBJCOPY_OBJCOPY_H
|
2017-08-01 02:33:58 +02:00
|
|
|
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2017-08-01 02:33:58 +02:00
|
|
|
#include "llvm/Support/Error.h"
|
2017-11-01 22:16:06 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <string>
|
2017-08-01 02:33:58 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2018-07-18 02:10:51 +02:00
|
|
|
namespace objcopy {
|
2017-08-01 02:33:58 +02:00
|
|
|
|
2017-10-12 01:54:34 +02:00
|
|
|
LLVM_ATTRIBUTE_NORETURN extern void error(Twine Message);
|
[llvm-objcopy] Return Error from Buffer::allocate(), [ELF]Writer::finalize(), and [ELF]Writer::commit()
Summary:
This patch changes a few methods to return Error instead of manually calling error/reportError to abort. This will make it easier to extract into a library.
Note that error() takes just a string (this patch also adds an overload that takes an Error), while reportError() takes string + [error/code]. To help unify things, use FileError to associate a given filename with an error. Note that this takes some special care (for now), e.g. calling reportError(FileName, <something that could be FileError>) will duplicate the filename. The goal is to eventually remove reportError() and have every error associated with a file to be a FileError, and just one error handling block at the tool level.
This change was suggested in D56806. I took it a little further than suggested, but completely fixing llvm-objcopy will take a couple more patches. If this approach looks good, I'll commit this and apply similar patche(s) for the rest.
This change is NFC in terms of non-error related code, although the error message changes in one context.
Reviewers: alexshap, jhenderson, jakehehrlich, mstorsjo, espindola
Reviewed By: alexshap, jhenderson
Subscribers: llvm-commits, emaste, arichardson
Differential Revision: https://reviews.llvm.org/D56930
llvm-svn: 351896
2019-01-23 00:49:16 +01:00
|
|
|
LLVM_ATTRIBUTE_NORETURN extern void error(Error E);
|
2018-01-25 23:46:17 +01:00
|
|
|
LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File, Error E);
|
|
|
|
LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File,
|
|
|
|
std::error_code EC);
|
2017-08-01 02:33:58 +02:00
|
|
|
|
|
|
|
// This is taken from llvm-readobj.
|
|
|
|
// [see here](llvm/tools/llvm-readobj/llvm-readobj.h:38)
|
|
|
|
template <class T> T unwrapOrError(Expected<T> EO) {
|
|
|
|
if (EO)
|
|
|
|
return *EO;
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-11-11 02:46:03 +01:00
|
|
|
logAllUnhandledErrors(EO.takeError(), OS);
|
2017-08-01 02:33:58 +02:00
|
|
|
OS.flush();
|
|
|
|
error(Buf);
|
|
|
|
}
|
|
|
|
|
2018-07-18 02:10:51 +02:00
|
|
|
} // end namespace objcopy
|
2017-11-01 22:16:06 +01:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_TOOLS_OBJCOPY_OBJCOPY_H
|