LLVM 20.0.0git
Error.h
Go to the documentation of this file.
1//===- Error.h - 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 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
16#include "llvm/Support/Error.h"
17#include <system_error>
18
19namespace llvm {
20
21class Twine;
22
23namespace object {
24
25const std::error_category &object_category();
26
27enum class object_error {
28 // Error code 0 is absent. Use std::error_code() instead.
38};
39
40inline std::error_code make_error_code(object_error e) {
41 return std::error_code(static_cast<int>(e), object_category());
42}
43
44/// Base class for all errors indicating malformed binary files.
45///
46/// Having a subclass for all malformed binary files allows archive-walking
47/// code to skip malformed files without having to understand every possible
48/// way that a binary file might be malformed.
49///
50/// Currently inherits from ECError for easy interoperability with
51/// std::error_code, but this will be removed in the future.
52class BinaryError : public ErrorInfo<BinaryError, ECError> {
53 void anchor() override;
54public:
55 static char ID;
57 // Default to parse_failed, can be overridden with setErrorCode.
59 }
60};
61
62/// Generic binary error.
63///
64/// For errors that don't require their own specific sub-error (most errors)
65/// this class can be used to describe the error via a string message.
66class GenericBinaryError : public ErrorInfo<GenericBinaryError, BinaryError> {
67public:
68 static char ID;
69 GenericBinaryError(const Twine &Msg);
70 GenericBinaryError(const Twine &Msg, object_error ECOverride);
71 const std::string &getMessage() const { return Msg; }
72 void log(raw_ostream &OS) const override;
73private:
74 std::string Msg;
75};
76
77/// isNotObjectErrorInvalidFileType() is used when looping through the children
78/// of an archive after calling getAsBinary() on the child and it returns an
79/// llvm::Error. In the cases we want to loop through the children and ignore the
80/// non-objects in the archive this is used to test the error to see if an
81/// error() function needs to called on the llvm::Error.
83
84inline Error createError(const Twine &Err) {
85 return make_error<StringError>(Err, object_error::parse_failed);
86}
87
88} // end namespace object.
89
90} // end namespace llvm.
91
92namespace std {
93template <>
94struct is_error_code_enum<llvm::object::object_error> : std::true_type {};
95}
96
97#endif
raw_pwrite_stream & OS
void setErrorCode(std::error_code EC)
Definition: Error.h:1180
Base class for user error types.
Definition: Error.h:355
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Base class for all errors indicating malformed binary files.
Definition: Error.h:52
static char ID
Definition: Error.h:55
Generic binary error.
Definition: Error.h:66
void log(raw_ostream &OS) const override
Definition: Error.cpp:73
const std::string & getMessage() const
Definition: Error.h:71
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Error createError(const Twine &Err)
Definition: Error.h:84
std::error_code make_error_code(object_error e)
Definition: Error.h:40
object_error
Definition: Error.h:27
const std::error_category & object_category()
Definition: Error.cpp:77
Error isNotObjectErrorInvalidFileType(llvm::Error Err)
isNotObjectErrorInvalidFileType() is used when looping through the children of an archive after calli...
Definition: Error.cpp:82
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858