forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.h
218 lines (178 loc) · 6.82 KB
/
error.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_COMMON_ERROR_H_
#define CARBON_COMMON_ERROR_H_
#include <functional>
#include <string>
#include <type_traits>
#include <variant>
#include "common/check.h"
#include "common/ostream.h"
#include "common/raw_string_ostream.h"
#include "llvm/ADT/Twine.h"
namespace Carbon {
// Success values should be represented as the presence of a value in ErrorOr,
// using `ErrorOr<Success>` and `return Success();` if no value needs to be
// returned.
struct Success : public Printable<Success> {
auto Print(llvm::raw_ostream& out) const -> void { out << "Success"; }
};
// Tracks an error message.
//
// This is nodiscard to enforce error handling prior to destruction.
class [[nodiscard]] Error : public Printable<Error> {
public:
// Represents an error state.
explicit Error(llvm::Twine location, llvm::Twine message)
: location_(location.str()), message_(message.str()) {
CARBON_CHECK(!message_.empty(), "Errors must have a message.");
}
// Represents an error with no associated location.
// TODO: Consider using two different types.
explicit Error(llvm::Twine message) : Error("", message) {}
Error(Error&& other) noexcept
: location_(std::move(other.location_)),
message_(std::move(other.message_)) {}
auto operator=(Error&& other) noexcept -> Error& {
location_ = std::move(other.location_);
message_ = std::move(other.message_);
return *this;
}
// Prints the error string.
auto Print(llvm::raw_ostream& out) const -> void {
if (!location().empty()) {
out << location() << ": ";
}
out << message();
}
// Returns a string describing the location of the error, such as
// "file.cc:123".
auto location() const -> const std::string& { return location_; }
// Returns the error message.
auto message() const -> const std::string& { return message_; }
private:
// The location associated with the error.
std::string location_;
// The error message.
std::string message_;
};
// Holds a value of type `T`, or an Error explaining why the value is
// unavailable.
//
// This is nodiscard to enforce error handling prior to destruction.
template <typename T>
class [[nodiscard]] ErrorOr {
public:
using ValueT = std::remove_reference_t<T>;
// Constructs with an error; the error must not be Error::Success().
// Implicit for easy construction on returns.
// NOLINTNEXTLINE(google-explicit-constructor)
ErrorOr(Error err) : val_(std::move(err)) {}
// Constructs with a reference.
// Implicit for easy construction on returns.
// NOLINTNEXTLINE(google-explicit-constructor)
ErrorOr(T ref)
requires std::is_reference_v<T>
: val_(std::ref(ref)) {}
// Constructs with a value.
// Implicit for easy construction on returns.
// NOLINTNEXTLINE(google-explicit-constructor)
ErrorOr(T val)
requires(!std::is_reference_v<T>)
: val_(std::move(val)) {}
// Returns true for success.
auto ok() const -> bool { return std::holds_alternative<StoredT>(val_); }
// Returns the contained error.
// REQUIRES: `ok()` is false.
auto error() const& -> const Error& {
CARBON_CHECK(!ok());
return std::get<Error>(val_);
}
auto error() && -> Error {
CARBON_CHECK(!ok());
return std::get<Error>(std::move(val_));
}
// Returns the contained value.
// REQUIRES: `ok()` is true.
auto operator*() -> ValueT& {
CARBON_CHECK(ok());
return std::get<StoredT>(val_);
}
// Returns the contained value.
// REQUIRES: `ok()` is true.
auto operator*() const -> const ValueT& {
CARBON_CHECK(ok());
return std::get<StoredT>(val_);
}
// Returns the contained value.
// REQUIRES: `ok()` is true.
auto operator->() -> ValueT* { return &**this; }
// Returns the contained value.
// REQUIRES: `ok()` is true.
auto operator->() const -> const ValueT* { return &**this; }
private:
using StoredT = std::conditional_t<std::is_reference_v<T>,
std::reference_wrapper<ValueT>, T>;
// Either an error message or a value.
std::variant<Error, StoredT> val_;
};
// A helper class for accumulating error message and converting to
// `Error` and `ErrorOr<T>`.
class ErrorBuilder {
public:
explicit ErrorBuilder(std::string location = "")
: location_(std::move(location)),
out_(std::make_unique<RawStringOstream>()) {}
ErrorBuilder(ErrorBuilder&&) = default;
auto operator=(ErrorBuilder&&) -> ErrorBuilder& = default;
// Accumulates string message to a temporary `ErrorBuilder`. After streaming,
// the builder must be converted to an `Error` or `ErrorOr`.
template <typename T>
auto operator<<(T&& message) && -> ErrorBuilder&& {
*out_ << message;
return std::move(*this);
}
// Accumulates string message for an lvalue error builder.
template <typename T>
auto operator<<(T&& message) & -> ErrorBuilder& {
*out_ << message;
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
operator Error() { return Error(location_, out_->TakeStr()); }
template <typename T>
// NOLINTNEXTLINE(google-explicit-constructor): Implicit cast for returns.
operator ErrorOr<T>() {
return Error(location_, out_->TakeStr());
}
private:
std::string location_;
std::unique_ptr<RawStringOstream> out_;
};
} // namespace Carbon
// Macro hackery to get a unique variable name.
#define CARBON_MAKE_UNIQUE_NAME_IMPL(a, b, c) a##b##c
#define CARBON_MAKE_UNIQUE_NAME(a, b, c) CARBON_MAKE_UNIQUE_NAME_IMPL(a, b, c)
// Macro to prevent a top-level comma from being interpreted as a macro
// argument separator.
#define CARBON_PROTECT_COMMAS(...) __VA_ARGS__
#define CARBON_RETURN_IF_ERROR_IMPL(unique_name, expr) \
if (auto unique_name = (expr); !(unique_name).ok()) { \
return std::move(unique_name).error(); \
}
#define CARBON_RETURN_IF_ERROR(expr) \
CARBON_RETURN_IF_ERROR_IMPL( \
CARBON_MAKE_UNIQUE_NAME(_llvm_error_line, __LINE__, __COUNTER__), \
CARBON_PROTECT_COMMAS(expr))
#define CARBON_ASSIGN_OR_RETURN_IMPL(unique_name, var, expr) \
auto unique_name = (expr); \
if (!(unique_name).ok()) { \
return std::move(unique_name).error(); \
} \
var = std::move(*(unique_name));
#define CARBON_ASSIGN_OR_RETURN(var, expr) \
CARBON_ASSIGN_OR_RETURN_IMPL( \
CARBON_MAKE_UNIQUE_NAME(_llvm_expected_line, __LINE__, __COUNTER__), \
CARBON_PROTECT_COMMAS(var), CARBON_PROTECT_COMMAS(expr))
#endif // CARBON_COMMON_ERROR_H_