Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:13:15

0001 // Copyright (c) ONNX Project Contributors
0002 
0003 /*
0004  * SPDX-License-Identifier: Apache-2.0
0005  */
0006 
0007 // ATTENTION: The code in this file is highly EXPERIMENTAL.
0008 // Adventurous users should note that the APIs will probably change.
0009 
0010 #pragma once
0011 
0012 #include <stdexcept>
0013 #include <string>
0014 
0015 namespace ONNX_NAMESPACE {
0016 
0017 struct assert_error : public std::runtime_error {
0018  public:
0019   explicit assert_error(const std::string& msg) : runtime_error(msg) {}
0020 };
0021 
0022 struct tensor_error : public assert_error {
0023  public:
0024   explicit tensor_error(const std::string& msg) : assert_error(msg) {}
0025 };
0026 
0027 std::string barf(const char* fmt, ...);
0028 
0029 [[noreturn]] void throw_assert_error(std::string&);
0030 
0031 [[noreturn]] void throw_tensor_error(std::string&);
0032 
0033 } // namespace ONNX_NAMESPACE
0034 
0035 #if defined(__GNUC__) || defined(__ICL) || defined(__clang__)
0036 #define _ONNX_EXPECT(x, y) (__builtin_expect((x), (y)))
0037 #else
0038 #define _ONNX_EXPECT(x, y) (x)
0039 #endif
0040 
0041 #define ONNX_ASSERT(cond)                                                                                 \
0042   if (_ONNX_EXPECT(!(cond), 0)) {                                                                         \
0043     std::string error_msg =                                                                               \
0044         ::ONNX_NAMESPACE::barf("%s:%u: %s: Assertion `%s` failed.", __FILE__, __LINE__, __func__, #cond); \
0045     throw_assert_error(error_msg);                                                                        \
0046   }
0047 
0048 // The following is used to prevent MSVC from passing the whole __VA_ARGS__ list
0049 // as the first parameter value to a macro call.
0050 #define ONNX_EXPAND(x) x
0051 
0052 // Note: msg must be a string literal
0053 #define _ONNX_ASSERTM(cond, msg, ...)                                                                \
0054   if (_ONNX_EXPECT(!(cond), 0)) {                                                                    \
0055     std::string error_msg = ::ONNX_NAMESPACE::barf(                                                  \
0056         "%s:%u: %s: Assertion `%s` failed: " msg, __FILE__, __LINE__, __func__, #cond, __VA_ARGS__); \
0057     throw_assert_error(error_msg);                                                                   \
0058   }
0059 
0060 // The trailing ' ' argument is a hack to deal with the extra comma when ... is empty.
0061 // Another way to solve this is ##__VA_ARGS__ in _ONNX_ASSERTM, but this is a non-portable
0062 // extension we shouldn't use.
0063 #define ONNX_ASSERTM(...) ONNX_EXPAND(_ONNX_ASSERTM(__VA_ARGS__, " "))
0064 
0065 #define _TENSOR_ASSERTM(cond, msg, ...)                                                              \
0066   if (_ONNX_EXPECT(!(cond), 0)) {                                                                    \
0067     std::string error_msg = ::ONNX_NAMESPACE::barf(                                                  \
0068         "%s:%u: %s: Assertion `%s` failed: " msg, __FILE__, __LINE__, __func__, #cond, __VA_ARGS__); \
0069     throw_tensor_error(error_msg);                                                                   \
0070   }
0071 
0072 #define TENSOR_ASSERTM(...) ONNX_EXPAND(_TENSOR_ASSERTM(__VA_ARGS__, " "))