@@ -96,34 +96,78 @@ enum class Error : error_code_t {
96
96
} // namespace torch
97
97
98
98
/* *
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.
102
102
*
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.
106
108
*/
107
109
#define ET_CHECK_OR_RETURN_ERROR (cond__, error__, message__, ...) \
108
- ({ \
110
+ { \
109
111
if (!(cond__)) { \
110
112
ET_LOG (Error, message__, ##__VA_ARGS__); \
111
- return torch::executor::Error::error__; \
113
+ return :: torch::executor::Error::error__; \
112
114
} \
113
- })
115
+ }
114
116
115
117
/* *
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.
119
121
*
120
122
* @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.
122
125
*/
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 )
0 commit comments