/src/llvm-project/llvm/lib/Object/Error.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Error.cpp - system_error extensions for Object -----------*- C++ -*-===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This defines a new error_category for the Object library. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "llvm/Object/Error.h" |
14 | | #include "llvm/ADT/Twine.h" |
15 | | #include "llvm/Support/ErrorHandling.h" |
16 | | |
17 | | using namespace llvm; |
18 | | using namespace object; |
19 | | |
20 | | namespace { |
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 { |
25 | | public: |
26 | | const char* name() const noexcept override; |
27 | | std::string message(int ev) const override; |
28 | | }; |
29 | | } |
30 | | |
31 | 0 | const char *_object_error_category::name() const noexcept { |
32 | 0 | return "llvm.object"; |
33 | 0 | } |
34 | | |
35 | 5.75k | std::string _object_error_category::message(int EV) const { |
36 | 5.75k | object_error E = static_cast<object_error>(EV); |
37 | 5.75k | switch (E) { |
38 | 0 | case object_error::arch_not_found: |
39 | 0 | return "No object file for requested architecture"; |
40 | 0 | case object_error::invalid_file_type: |
41 | 0 | return "The file was not recognized as a valid object file"; |
42 | 5.75k | case object_error::parse_failed: |
43 | 5.75k | return "Invalid data was encountered while parsing the file"; |
44 | 0 | case object_error::unexpected_eof: |
45 | 0 | return "The end of the file was unexpectedly encountered"; |
46 | 0 | case object_error::string_table_non_null_end: |
47 | 0 | return "String table must end with a null terminator"; |
48 | 0 | case object_error::invalid_section_index: |
49 | 0 | return "Invalid section index"; |
50 | 0 | case object_error::bitcode_section_not_found: |
51 | 0 | return "Bitcode section not found in object file"; |
52 | 0 | case object_error::invalid_symbol_index: |
53 | 0 | return "Invalid symbol index"; |
54 | 0 | case object_error::section_stripped: |
55 | 0 | return "Section has been stripped from the object file"; |
56 | 5.75k | } |
57 | 0 | llvm_unreachable("An enumerator of object_error does not have a message " |
58 | 0 | "defined."); |
59 | 0 | } |
60 | | |
61 | 0 | void BinaryError::anchor() {} |
62 | | char BinaryError::ID = 0; |
63 | | char GenericBinaryError::ID = 0; |
64 | | |
65 | 2 | GenericBinaryError::GenericBinaryError(const Twine &Msg) : Msg(Msg.str()) {} |
66 | | |
67 | | GenericBinaryError::GenericBinaryError(const Twine &Msg, |
68 | | object_error ECOverride) |
69 | 6 | : Msg(Msg.str()) { |
70 | 6 | setErrorCode(make_error_code(ECOverride)); |
71 | 6 | } |
72 | | |
73 | 0 | void GenericBinaryError::log(raw_ostream &OS) const { |
74 | 0 | OS << Msg; |
75 | 0 | } |
76 | | |
77 | 37.6k | const std::error_category &object::object_category() { |
78 | 37.6k | static _object_error_category error_category; |
79 | 37.6k | return error_category; |
80 | 37.6k | } |
81 | | |
82 | 0 | llvm::Error llvm::object::isNotObjectErrorInvalidFileType(llvm::Error Err) { |
83 | 0 | return handleErrors(std::move(Err), [](std::unique_ptr<ECError> M) -> Error { |
84 | | // Try to handle 'M'. If successful, return a success value from |
85 | | // the handler. |
86 | 0 | if (M->convertToErrorCode() == object_error::invalid_file_type) |
87 | 0 | return Error::success(); |
88 | | |
89 | | // We failed to handle 'M' - return it from the handler. |
90 | | // This value will be passed back from catchErrors and |
91 | | // wind up in Err2, where it will be returned from this function. |
92 | 0 | return Error(std::move(M)); |
93 | 0 | }); |
94 | 0 | } |