0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 //
|
147
|
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
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 // This defines a new error_category for the Object library.
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 //
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 //===----------------------------------------------------------------------===//
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 #include "llvm/Object/Error.h"
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 #include "llvm/Support/ErrorHandling.h"
|
83
|
15 #include "llvm/Support/ManagedStatic.h"
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 using namespace llvm;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 using namespace object;
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 namespace {
|
120
|
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.
|
77
|
24 class _object_error_category : public std::error_category {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 public:
|
120
|
26 const char* name() const noexcept override;
|
77
|
27 std::string message(int ev) const override;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 };
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30
|
120
|
31 const char *_object_error_category::name() const noexcept {
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 return "llvm.object";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34
|
77
|
35 std::string _object_error_category::message(int EV) const {
|
|
36 object_error E = static_cast<object_error>(EV);
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 switch (E) {
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 case object_error::arch_not_found:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 return "No object file for requested architecture";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 case object_error::invalid_file_type:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 return "The file was not recognized as a valid object file";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 case object_error::parse_failed:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 return "Invalid data was encountered while parsing the file";
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 case object_error::unexpected_eof:
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 return "The end of the file was unexpectedly encountered";
|
95
|
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";
|
83
|
50 case object_error::bitcode_section_not_found:
|
|
51 return "Bitcode section not found in object file";
|
120
|
52 case object_error::invalid_symbol_index:
|
|
53 return "Invalid symbol index";
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 llvm_unreachable("An enumerator of object_error does not have a message "
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 "defined.");
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 }
|
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58
|
147
|
59 void BinaryError::anchor() {}
|
120
|
60 char BinaryError::ID = 0;
|
|
61 char GenericBinaryError::ID = 0;
|
|
62
|
|
63 GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {}
|
|
64
|
|
65 GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride)
|
|
66 : Msg(Msg.str()) {
|
|
67 setErrorCode(make_error_code(ECOverride));
|
|
68 }
|
|
69
|
|
70 void GenericBinaryError::log(raw_ostream &OS) const {
|
|
71 OS << Msg;
|
|
72 }
|
|
73
|
83
|
74 static ManagedStatic<_object_error_category> error_category;
|
|
75
|
77
|
76 const std::error_category &object::object_category() {
|
83
|
77 return *error_category;
|
0
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 }
|
120
|
79
|
|
80 llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) {
|
147
|
81 return handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error {
|
|
82 // Try to handle 'M'. If successful, return a success value from
|
|
83 // the handler.
|
|
84 if (M->convertToErrorCode() == object_error::invalid_file_type)
|
|
85 return Error::success();
|
120
|
86
|
147
|
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));
|
|
91 });
|
120
|
92 }
|