annotate lib/Object/Error.cpp @ 147:c2174574ed3a

LLVM 10
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 16:55:33 +0900
parents 1172e4bd9c6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 // This defines a new error_category for the Object library.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "llvm/Object/Error.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "llvm/Support/ErrorHandling.h"
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
15 #include "llvm/Support/ManagedStatic.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 using namespace llvm;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 using namespace object;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 namespace {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
21 // FIXME: This class is only here to support the transition to llvm::Error. It
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
22 // will be removed once this transition is complete. Clients should prefer to
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
23 // deal with the Error value directly, rather than converting to error_code.
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
24 class _object_error_category : public std::error_category {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 public:
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
26 const char* name() const noexcept override;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
27 std::string message(int ev) const override;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 };
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
31 const char *_object_error_category::name() const noexcept {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 return "llvm.object";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
35 std::string _object_error_category::message(int EV) const {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
36 object_error E = static_cast<object_error>(EV);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 switch (E) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 case object_error::arch_not_found:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 return "No object file for requested architecture";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 case object_error::invalid_file_type:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 return "The file was not recognized as a valid object file";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 case object_error::parse_failed:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 return "Invalid data was encountered while parsing the file";
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 case object_error::unexpected_eof:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 return "The end of the file was unexpectedly encountered";
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
46 case object_error::string_table_non_null_end:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
47 return "String table must end with a null terminator";
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
48 case object_error::invalid_section_index:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
49 return "Invalid section index";
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
50 case object_error::bitcode_section_not_found:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
51 return "Bitcode section not found in object file";
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
52 case object_error::invalid_symbol_index:
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
53 return "Invalid symbol index";
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 llvm_unreachable("An enumerator of object_error does not have a message "
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 "defined.");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
59 void BinaryError::anchor() {}
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
60 char BinaryError::ID = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
61 char GenericBinaryError::ID = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
62
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
63 GenericBinaryError::GenericBinaryError(Twine Msg) : Msg(Msg.str()) {}
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
64
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
65 GenericBinaryError::GenericBinaryError(Twine Msg, object_error ECOverride)
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
66 : Msg(Msg.str()) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
67 setErrorCode(make_error_code(ECOverride));
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
68 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
69
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
70 void GenericBinaryError::log(raw_ostream &OS) const {
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
71 OS << Msg;
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
72 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
73
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
74 static ManagedStatic<_object_error_category> error_category;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
75
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
76 const std::error_category &object::object_category() {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
77 return *error_category;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
79
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
80 llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) {
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
81 return handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error {
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
82 // Try to handle 'M'. If successful, return a success value from
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
83 // the handler.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
84 if (M->convertToErrorCode() == object_error::invalid_file_type)
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
85 return Error::success();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
86
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
87 // We failed to handle 'M' - return it from the handler.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
88 // This value will be passed back from catchErrors and
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
89 // wind up in Err2, where it will be returned from this function.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
90 return Error(std::move(M));
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 120
diff changeset
91 });
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 95
diff changeset
92 }