Skip to content

Commit b46cc57

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Macros to check and return if error. (#1621)
Summary: Pull Request resolved: #1621 . Reviewed By: dbort Differential Revision: D52846710 fbshipit-source-id: a8c170553e837e42e35aa68ae1c14c5f56243cb3
1 parent 52e8ba9 commit b46cc57

File tree

2 files changed

+72
-28
lines changed

2 files changed

+72
-28
lines changed

runtime/core/error.h

+64-20
Original file line numberDiff line numberDiff line change
@@ -96,34 +96,78 @@ enum class Error : error_code_t {
9696
} // namespace torch
9797

9898
/**
99-
* If cond__ is false, log message__ and return the Error
100-
* from the current function, which must be declared to return
101-
* torch::executor::Error
99+
* If cond__ is false, log the specified message and return the specified Error
100+
* from the current function, which must be of return type
101+
* torch::executor::Error.
102102
*
103-
* @param[in] cond__ Condition asserted as true
104-
* @param[in] error__ Error enum value like `InvalidArgument`.
105-
* @param[in] message__ Log error message format string.
103+
* @param[in] cond__ The condition to be checked, asserted as true.
104+
* @param[in] error__ Error enum value to return without the `Error::` prefix,
105+
* like `InvalidArgument`.
106+
* @param[in] message__ Format string for the log error message.
107+
* @param[in] ... Optional additional arguments for the format string.
106108
*/
107109
#define ET_CHECK_OR_RETURN_ERROR(cond__, error__, message__, ...) \
108-
({ \
110+
{ \
109111
if (!(cond__)) { \
110112
ET_LOG(Error, message__, ##__VA_ARGS__); \
111-
return torch::executor::Error::error__; \
113+
return ::torch::executor::Error::error__; \
112114
} \
113-
})
115+
}
114116

115117
/**
116-
* If error__ is not Error::Ok, log message__ and return the Error
117-
* from the current function, which must be declared to return
118-
* torch::executor::Error
118+
* If error__ is not Error::Ok, optionally log a message and return the error
119+
* from the current function, which must be of return type
120+
* torch::executor::Error.
119121
*
120122
* @param[in] error__ Error enum value asserted to be Error::Ok.
121-
* @param[in] message__ Log error message format string.
123+
* @param[in] ... Optional format string for the log error message and its
124+
* arguments.
122125
*/
123-
#define ET_CHECK_OK_OR_RETURN_ERROR(error__, message__, ...) \
124-
({ \
125-
if ((error__) != Error::Ok) { \
126-
ET_LOG(Error, message__, ##__VA_ARGS__); \
127-
return error__; \
128-
} \
129-
})
126+
#define ET_CHECK_OK_OR_RETURN_ERROR(...) \
127+
ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_SELECT(__VA_ARGS__, 2, 1)(__VA_ARGS__)
128+
129+
/**
130+
* Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead.
131+
* This macro selects the correct version of
132+
* ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR based on the number of arguments passed.
133+
* It uses a trick with the preprocessor to count the number of arguments and
134+
* then selects the appropriate macro.
135+
*
136+
* The macro expansion uses __VA_ARGS__ to accept any number of arguments and
137+
* then appends them to ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_, followed by the
138+
* count of arguments. The count is determined by the macro
139+
* ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_SELECT which takes the arguments and
140+
* passes them along with a sequence of numbers (2, 1). The preprocessor then
141+
* matches this sequence to the correct number of arguments provided.
142+
*
143+
* If two arguments are passed, ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 is
144+
* selected, suitable for cases where an error code and a custom message are
145+
* provided. If only one argument is passed,
146+
* ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_1 is selected, which is used for cases
147+
* with just an error code.
148+
*
149+
* Usage:
150+
* ET_CHECK_OK_OR_RETURN_ERROR(error_code); // Calls v1
151+
* ET_CHECK_OK_OR_RETURN_ERROR(error_code, "Error message", ...); // Calls v2
152+
*/
153+
#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_SELECT(_1, _2, N, ...) \
154+
ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_##N
155+
156+
// Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead.
157+
#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_1(error__) \
158+
do { \
159+
const auto et_error__ = (error__); \
160+
if (et_error__ != ::torch::executor::Error::Ok) { \
161+
return et_error__; \
162+
} \
163+
} while (0)
164+
165+
// Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead.
166+
#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2(error__, message__, ...) \
167+
do { \
168+
const auto et_error__ = (error__); \
169+
if (et_error__ != ::torch::executor::Error::Ok) { \
170+
ET_LOG(Error, message__, ##__VA_ARGS__); \
171+
return et_error__; \
172+
} \
173+
} while (0)

runtime/core/result.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ T* Result<T>::operator->() {
207207
*
208208
* Note: A function using ET_UNWRAP should itself return a Result or Error.
209209
*
210-
* @param[in] _result Expression yielding the result to unwrap.
210+
* @param[in] result__ Expression yielding the result to unwrap.
211211
*/
212-
#define ET_UNWRAP(_result) \
213-
({ \
214-
auto result_recv = (_result); \
215-
if (!result_recv.ok()) { \
216-
return result_recv.error(); \
217-
} \
218-
*result_recv; \
212+
#define ET_UNWRAP(result__) \
213+
({ \
214+
auto et_result__ = (result__); \
215+
if (!et_result__.ok()) { \
216+
return et_result__.error(); \
217+
} \
218+
std::move(*et_result__); \
219219
})

0 commit comments

Comments
 (0)