1 //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This defines a new error_category for the Object library.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Object/Error.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "llvm/Support/ManagedStatic.h"
18 using namespace object
;
21 // FIXME: This class is only here to support the transition to llvm::Error. It
22 // will be removed once this transition is complete. Clients should prefer to
23 // deal with the Error value directly, rather than converting to error_code.
24 class _object_error_category
: public std::error_category
{
26 const char* name() const noexcept override
;
27 std::string
message(int ev
) const override
;
31 const char *_object_error_category::name() const noexcept
{
35 std::string
_object_error_category::message(int EV
) const {
36 object_error E
= static_cast<object_error
>(EV
);
38 case object_error::arch_not_found
:
39 return "No object file for requested architecture";
40 case object_error::invalid_file_type
:
41 return "The file was not recognized as a valid object file";
42 case object_error::parse_failed
:
43 return "Invalid data was encountered while parsing the file";
44 case object_error::unexpected_eof
:
45 return "The end of the file was unexpectedly encountered";
46 case object_error::string_table_non_null_end
:
47 return "String table must end with a null terminator";
48 case object_error::invalid_section_index
:
49 return "Invalid section index";
50 case object_error::bitcode_section_not_found
:
51 return "Bitcode section not found in object file";
52 case object_error::invalid_symbol_index
:
53 return "Invalid symbol index";
55 llvm_unreachable("An enumerator of object_error does not have a message "
59 void BinaryError::anchor() {}
60 char BinaryError::ID
= 0;
61 char GenericBinaryError::ID
= 0;
63 GenericBinaryError::GenericBinaryError(Twine Msg
) : Msg(Msg
.str()) {}
65 GenericBinaryError::GenericBinaryError(Twine Msg
, object_error ECOverride
)
67 setErrorCode(make_error_code(ECOverride
));
70 void GenericBinaryError::log(raw_ostream
&OS
) const {
74 static ManagedStatic
<_object_error_category
> error_category
;
76 const std::error_category
&object::object_category() {
77 return *error_category
;
80 llvm::Error
llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err
) {
81 return handleErrors(std::move(Err
), [](std::unique_ptr
<ECError
> M
) -> Error
{
82 // Try to handle 'M'. If successful, return a success value from
84 if (M
->convertToErrorCode() == object_error::invalid_file_type
)
85 return Error::success();
87 // We failed to handle 'M' - return it from the handler.
88 // This value will be passed back from catchErrors and
89 // wind up in Err2, where it will be returned from this function.
90 return Error(std::move(M
));