@@ -121,13 +121,57 @@ using ::executorch::runtime::error_code_t;
121121 * @param[in] message__ Format string for the log error message.
122122 * @param[in] ... Optional additional arguments for the format string.
123123 */
124+ // MSVC doesn't support ##__VA_ARGS__, so we need a workaround
125+ #if defined(_MSC_VER)
126+ #define ET_CHECK_OR_RETURN_ERROR_IMPL (cond__, error__, message__, ...) \
127+ { \
128+ if (!(cond__)) { \
129+ ET_LOG (Error, message__, __VA_ARGS__); \
130+ return ::executorch::runtime::Error::error__; \
131+ } \
132+ }
133+ #define ET_CHECK_OR_RETURN_ERROR_NOARGS (cond__, error__, message__ ) \
134+ { \
135+ if (!(cond__)) { \
136+ ET_LOG (Error, message__); \
137+ return ::executorch::runtime::Error::error__; \
138+ } \
139+ }
140+ // This macro expansion trick detects if there are additional arguments after message__
141+ #define ET_CHECK_OR_RETURN_ERROR_SELECT (cond__, error__, message__, arg1, ...) \
142+ ET_CHECK_OR_RETURN_ERROR_SELECT_I ( \
143+ cond__, error__, message__, arg1, ##__VA_ARGS__)
144+ #define ET_CHECK_OR_RETURN_ERROR_SELECT_I (cond__, error__, message__, arg1, ...) \
145+ ET_CHECK_OR_RETURN_ERROR_SELECT_II ( \
146+ cond__, error__, message__, arg1, ##__VA_ARGS__)
147+ #define ET_CHECK_OR_RETURN_ERROR_SELECT_II (cond__, error__, message__, arg1, ...) \
148+ ET_CHECK_OR_RETURN_ERROR_SELECT_IMPL ( \
149+ cond__, error__, message__, arg1, ##__VA_ARGS__)
150+ // Final selection based on presence of arguments
151+ #define ET_CHECK_OR_RETURN_ERROR_SELECT_IMPL (cond__, error__, message__, arg1, ...) \
152+ ET_CHECK_OR_RETURN_ERROR_SELECT_IMPL_ ( \
153+ cond__, error__, message__, arg1, ##__VA_ARGS__)
154+ #define ET_CHECK_OR_RETURN_ERROR_SELECT_IMPL_ (cond__, error__, message__, arg1, ...) \
155+ ET_CHECK_OR_RETURN_ERROR_##arg1(cond__, error__, message__, ##__VA_ARGS__)
156+ // Define the VOID and non-VOID versions
157+ #define ET_CHECK_OR_RETURN_ERROR_VOID (cond__, error__, message__ ) \
158+ ET_CHECK_OR_RETURN_ERROR_NOARGS (cond__, error__, message__)
159+ #define ET_CHECK_OR_RETURN_ERROR_NONVOID (cond__, error__, message__, ...) \
160+ ET_CHECK_OR_RETURN_ERROR_IMPL (cond__, error__, message__, __VA_ARGS__)
161+ // The main macro that selects between VOID and non-VOID
162+ #define ET_CHECK_OR_RETURN_ERROR (cond__, error__, message__, ...) \
163+ ET_CHECK_OR_RETURN_ERROR_SELECT ( \
164+ cond__, error__, message__, NONVOID, ##__VA_ARGS__, VOID)
165+ #else
166+ // For non-MSVC compilers, use the original macro
124167#define ET_CHECK_OR_RETURN_ERROR (cond__, error__, message__, ...) \
125168 { \
126169 if (!(cond__)) { \
127170 ET_LOG (Error, message__, ##__VA_ARGS__); \
128171 return ::executorch::runtime::Error::error__; \
129172 } \
130173 }
174+ #endif
131175
132176/* *
133177 * A convenience macro to be used in utility functions that check whether input
@@ -137,13 +181,57 @@ using ::executorch::runtime::error_code_t;
137181 * @param[in] cond the condition to check
138182 * @param[in] message an additional message to log with `cond`
139183 */
184+ // MSVC doesn't support ##__VA_ARGS__, so we need a workaround
185+ #if defined(_MSC_VER)
186+ #define ET_CHECK_OR_RETURN_FALSE_IMPL (cond__, message__, ...) \
187+ { \
188+ if (!(cond__)) { \
189+ ET_LOG (Error, " Check failed (%s): " message__, #cond__, __VA_ARGS__); \
190+ return false ; \
191+ } \
192+ }
193+ #define ET_CHECK_OR_RETURN_FALSE_NOARGS (cond__, message__ ) \
194+ { \
195+ if (!(cond__)) { \
196+ ET_LOG (Error, " Check failed (%s): " message__, #cond__); \
197+ return false ; \
198+ } \
199+ }
200+ // This macro expansion trick detects if there are additional arguments after message__
201+ #define ET_CHECK_OR_RETURN_FALSE_SELECT (cond__, message__, arg1, ...) \
202+ ET_CHECK_OR_RETURN_FALSE_SELECT_I ( \
203+ cond__, message__, arg1, ##__VA_ARGS__)
204+ #define ET_CHECK_OR_RETURN_FALSE_SELECT_I (cond__, message__, arg1, ...) \
205+ ET_CHECK_OR_RETURN_FALSE_SELECT_II ( \
206+ cond__, message__, arg1, ##__VA_ARGS__)
207+ #define ET_CHECK_OR_RETURN_FALSE_SELECT_II (cond__, message__, arg1, ...) \
208+ ET_CHECK_OR_RETURN_FALSE_SELECT_IMPL ( \
209+ cond__, message__, arg1, ##__VA_ARGS__)
210+ // Final selection based on presence of arguments
211+ #define ET_CHECK_OR_RETURN_FALSE_SELECT_IMPL (cond__, message__, arg1, ...) \
212+ ET_CHECK_OR_RETURN_FALSE_SELECT_IMPL_ ( \
213+ cond__, message__, arg1, ##__VA_ARGS__)
214+ #define ET_CHECK_OR_RETURN_FALSE_SELECT_IMPL_ (cond__, message__, arg1, ...) \
215+ ET_CHECK_OR_RETURN_FALSE_##arg1(cond__, message__, ##__VA_ARGS__)
216+ // Define the VOID and non-VOID versions
217+ #define ET_CHECK_OR_RETURN_FALSE_VOID (cond__, message__ ) \
218+ ET_CHECK_OR_RETURN_FALSE_NOARGS (cond__, message__)
219+ #define ET_CHECK_OR_RETURN_FALSE_NONVOID (cond__, message__, ...) \
220+ ET_CHECK_OR_RETURN_FALSE_IMPL (cond__, message__, __VA_ARGS__)
221+ // The main macro that selects between VOID and non-VOID
222+ #define ET_CHECK_OR_RETURN_FALSE (cond__, message__, ...) \
223+ ET_CHECK_OR_RETURN_FALSE_SELECT ( \
224+ cond__, message__, NONVOID, ##__VA_ARGS__, VOID)
225+ #else
226+ // For non-MSVC compilers, use the original macro
140227#define ET_CHECK_OR_RETURN_FALSE (cond__, message__, ...) \
141228 { \
142229 if (!(cond__)) { \
143230 ET_LOG (Error, " Check failed (%s): " message__, #cond__, ##__VA_ARGS__); \
144231 return false ; \
145232 } \
146233 }
234+ #endif
147235
148236/* *
149237 * If error__ is not Error::Ok, optionally log a message and return the error
@@ -201,6 +289,50 @@ using ::executorch::runtime::error_code_t;
201289 } while (0 )
202290
203291// Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead.
292+ #if defined(_MSC_VER)
293+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_IMPL (error__, message__, ...) \
294+ do { \
295+ const auto et_error__ = (error__); \
296+ if (et_error__ != ::executorch::runtime::Error::Ok) { \
297+ ET_LOG (Error, message__, __VA_ARGS__); \
298+ return et_error__; \
299+ } \
300+ } while (0 )
301+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_NOARGS (error__, message__ ) \
302+ do { \
303+ const auto et_error__ = (error__); \
304+ if (et_error__ != ::executorch::runtime::Error::Ok) { \
305+ ET_LOG (Error, message__); \
306+ return et_error__; \
307+ } \
308+ } while (0 )
309+ // This macro expansion trick detects if there are additional arguments after message__
310+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT (error__, message__, arg1, ...) \
311+ ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT_I ( \
312+ error__, message__, arg1, ##__VA_ARGS__)
313+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT_I (error__, message__, arg1, ...) \
314+ ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT_II ( \
315+ error__, message__, arg1, ##__VA_ARGS__)
316+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT_II (error__, message__, arg1, ...) \
317+ ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT_IMPL ( \
318+ error__, message__, arg1, ##__VA_ARGS__)
319+ // Final selection based on presence of arguments
320+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT_IMPL (error__, message__, arg1, ...) \
321+ ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT_IMPL_ ( \
322+ error__, message__, arg1, ##__VA_ARGS__)
323+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT_IMPL_ (error__, message__, arg1, ...) \
324+ ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_##arg1(error__, message__, ##__VA_ARGS__)
325+ // Define the VOID and non-VOID versions
326+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_VOID (error__, message__ ) \
327+ ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_NOARGS (error__, message__)
328+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_NONVOID (error__, message__, ...) \
329+ ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_IMPL (error__, message__, __VA_ARGS__)
330+ // The main macro that selects between VOID and non-VOID
331+ #define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 (error__, message__, ...) \
332+ ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2_SELECT ( \
333+ error__, message__, NONVOID, ##__VA_ARGS__, VOID)
334+ #else
335+ // For non-MSVC compilers, use the original macro
204336#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 (error__, message__, ...) \
205337 do { \
206338 const auto et_error__ = (error__); \
@@ -209,6 +341,7 @@ using ::executorch::runtime::error_code_t;
209341 return et_error__; \
210342 } \
211343 } while (0 )
344+ #endif
212345
213346// Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead.
214347#define ET_INTERNAL_CHECK_OK_OR_RETURN_ERROR_3 \
0 commit comments