blob: 07744188444ac48f94a91b856d1422a3b36a5054 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- Error.h - system_error extensions for Object -------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This declares a new error_category for the Object library.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_OBJECT_ERROR_H
14#define LLVM_OBJECT_ERROR_H
15
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010016#include "llvm/Support/Error.h"
17#include <system_error>
18
19namespace llvm {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020020
21class Twine;
22
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010023namespace object {
24
25class Binary;
26
27const std::error_category &object_category();
28
29enum class object_error {
30 // Error code 0 is absent. Use std::error_code() instead.
31 arch_not_found = 1,
32 invalid_file_type,
33 parse_failed,
34 unexpected_eof,
35 string_table_non_null_end,
36 invalid_section_index,
37 bitcode_section_not_found,
38 invalid_symbol_index,
39};
40
41inline std::error_code make_error_code(object_error e) {
42 return std::error_code(static_cast<int>(e), object_category());
43}
44
45/// Base class for all errors indicating malformed binary files.
46///
47/// Having a subclass for all malformed binary files allows archive-walking
48/// code to skip malformed files without having to understand every possible
49/// way that a binary file might be malformed.
50///
51/// Currently inherits from ECError for easy interoperability with
52/// std::error_code, but this will be removed in the future.
53class BinaryError : public ErrorInfo<BinaryError, ECError> {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020054 void anchor() override;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055public:
56 static char ID;
57 BinaryError() {
58 // Default to parse_failed, can be overridden with setErrorCode.
59 setErrorCode(make_error_code(object_error::parse_failed));
60 }
61};
62
63/// Generic binary error.
64///
65/// For errors that don't require their own specific sub-error (most errors)
66/// this class can be used to describe the error via a string message.
67class GenericBinaryError : public ErrorInfo<GenericBinaryError, BinaryError> {
68public:
69 static char ID;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020070 GenericBinaryError(const Twine &Msg);
71 GenericBinaryError(const Twine &Msg, object_error ECOverride);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072 const std::string &getMessage() const { return Msg; }
73 void log(raw_ostream &OS) const override;
74private:
75 std::string Msg;
76};
77
78/// isNotObjectErrorInvalidFileType() is used when looping through the children
79/// of an archive after calling getAsBinary() on the child and it returns an
80/// llvm::Error. In the cases we want to loop through the children and ignore the
81/// non-objects in the archive this is used to test the error to see if an
82/// error() function needs to called on the llvm::Error.
83Error isNotObjectErrorInvalidFileType(llvm::Error Err);
84
85} // end namespace object.
86
87} // end namespace llvm.
88
89namespace std {
90template <>
91struct is_error_code_enum<llvm::object::object_error> : std::true_type {};
92}
93
94#endif