LCOV - code coverage report
Current view: top level - legacy/ascend950/common/utils - exception_util.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 12 12
Test Date: 2026-08-18 17:47:01 Functions: 66.9 % 145 97

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #ifndef HCCLV2_EXCEPTION_UTIL_H
      12              : #define HCCLV2_EXCEPTION_UTIL_H
      13              : 
      14              : #include "../exception/null_ptr_exception.h"
      15              : #include "log.h"
      16              : #include "string_util.h"
      17              : #include <string>
      18              : #include <exception>
      19              : 
      20              : #define MACRO_THROW(EXCEPTION, MSG)      \
      21              :     do {                                 \
      22              :         HCCL_ERROR("%s", (MSG).c_str()); \
      23              :         throw EXCEPTION(MSG);            \
      24              :     } while (0)
      25              : 
      26              : #define DECTOR_TRY_CATCH(classname, expr)                                                     \
      27              :     try {                                                                                     \
      28              :         expr;                                                                                 \
      29              :     } catch (HcclException & e) {                                                             \
      30              :         HCCL_ERROR("Exception occurred: %s", e.what());                                       \
      31              :     } catch (std::exception & e) {                                                            \
      32              :         HCCL_ERROR("Exception occurred: %s", e.what());                                       \
      33              :     } catch (...) {                                                                           \
      34              :         HCCL_ERROR("Unknown error occurs when destructor object of class \"%s\"", classname); \
      35              :     }
      36              : 
      37              : #define TRY_CATCH_THROW(EXCEPTION, MSG, EXPR)                                        \
      38              :     do {                                                                             \
      39              :         try {                                                                        \
      40              :             EXPR;                                                                    \
      41              :         } catch (HcclException & e) {                                                \
      42              :             THROW<EXCEPTION>(StringFormat("%s, %s", e.what(), MSG.c_str()));         \
      43              :         } catch (std::exception & e) {                                               \
      44              :             THROW<EXCEPTION>(StringFormat("%s, %s", e.what(), MSG.c_str()));         \
      45              :         } catch (...) {                                                              \
      46              :             THROW<EXCEPTION>(StringFormat("Unknown error occurs, %s", MSG.c_str())); \
      47              :         }                                                                            \
      48              :     } while (0)
      49              : 
      50              : #define TRY_CATCH_RETURN(expr)                                                    \
      51              :     do {                                                                          \
      52              :         try {                                                                     \
      53              :             expr;                                                                 \
      54              :         } catch (HcclException & e) {                                             \
      55              :             HCCL_ERROR("Exception occurred: %s", e.what());                       \
      56              :             auto backTraces = e.GetBackTraceStrings();                            \
      57              :             std::for_each(backTraces.begin(), backTraces.end(), [](string item) { \
      58              :                 HCCL_INFO("backTraces item: %s", item.c_str());                   \
      59              :             });                                                                   \
      60              :             return e.GetErrorCode();                                              \
      61              :         } catch (exception & e) {                                                 \
      62              :             HCCL_ERROR("Exception occurred: %s", e.what());                       \
      63              :             return HcclResult::HCCL_E_INTERNAL;                                   \
      64              :         } catch (...) {                                                           \
      65              :             HCCL_ERROR("Unkown error occurs!");                                   \
      66              :             return HcclResult::HCCL_E_INTERNAL;                                   \
      67              :         }                                                                         \
      68              :     } while (0)
      69              : 
      70              : #define TRY_CATCH_PROCESS_THROW(EXCEPTION, EXPR, MSG, PROCESS)                             \
      71              :     do {                                                                                   \
      72              :         try {                                                                              \
      73              :             EXPR;                                                                          \
      74              :         } catch (HcclException & e) {                                                      \
      75              :             PROCESS;                                                                       \
      76              :             HCCL_ERROR("%s due to %s", MSG, e.what());                                     \
      77              :             throw e;                                                                       \
      78              :         } catch (std::exception & e) {                                                     \
      79              :             PROCESS;                                                                       \
      80              :             MACRO_THROW(EXCEPTION, StringFormat("%s due to %s", MSG, e.what()));           \
      81              :         } catch (...) {                                                                    \
      82              :             PROCESS;                                                                       \
      83              :             MACRO_THROW(EXCEPTION, StringFormat("%s due to: Unknown error occurs!", MSG)); \
      84              :         }                                                                                  \
      85              :     } while (0)
      86              : 
      87              : #define TRY_CATCH_PRINT_ERROR(expr)                                               \
      88              :     do {                                                                          \
      89              :         try {                                                                     \
      90              :             expr;                                                                 \
      91              :         } catch (HcclException & e) {                                             \
      92              :             HCCL_ERROR("Exception occurred: %s", e.what());                       \
      93              :             auto backTraces = e.GetBackTraceStrings();                            \
      94              :             std::for_each(backTraces.begin(), backTraces.end(), [](string item) { \
      95              :                 HCCL_INFO("backTraces item: %s", item.c_str());                   \
      96              :             });                                                                   \
      97              :         } catch (exception & e) {                                                 \
      98              :             HCCL_ERROR("Exception occurred: %s", e.what());                       \
      99              :         } catch (...) {                                                           \
     100              :             HCCL_ERROR("Unkown error occurs!");                                   \
     101              :         }                                                                         \
     102              :     } while (0)
     103              : 
     104              : #define CHK_RET_THROW(EXCEPTION, MSG, expr)              \
     105              :     do {                                                 \
     106              :         auto ret = (expr);                               \
     107              :         if (UNLIKELY(ret != HcclResult::HCCL_SUCCESS)) { \
     108              :             THROW<EXCEPTION>(MSG);                       \
     109              :         }                                                \
     110              :     } while (0)
     111              : 
     112              : #define CHK_PRT_THROW(expr, exeLog, EXCEPTION, MSG) \
     113              :     do {                                            \
     114              :         if (UNLIKELY(expr)) {                       \
     115              :             exeLog;                                 \
     116              :             throw EXCEPTION(MSG);                   \
     117              :         }                                           \
     118              :     } while (0)
     119              : 
     120              : namespace Hccl {
     121              : 
     122              : template <typename EXCEPTION>
     123          416 : [[noreturn]] inline void THROW(const std::string& msg)
     124              : {
     125         1040 :     HCCL_ERROR("%s", msg.c_str());
     126          416 :     throw EXCEPTION(msg);
     127              : }
     128              : 
     129              : template <typename EXCEPTION, typename... Args>
     130          132 : [[noreturn]] inline void THROW(const char* format, Args... args)
     131              : {
     132          132 :     auto msg = StringFormat(format, args...);
     133          310 :     HCCL_ERROR("%s", msg.c_str());
     134          132 :     throw EXCEPTION(msg);
     135          132 : }
     136              : 
     137              : template <typename POINTER>
     138        17763 : inline void CHECK_NULLPTR(const POINTER& p, const std::string& msg)
     139              : {
     140        17763 :     if (UNLIKELY(p == nullptr)) {
     141            7 :         THROW<NullPtrException>(msg);
     142              :     }
     143        17756 : }
     144              : 
     145              : } // namespace Hccl
     146              : 
     147              : #endif // HCCLV2_EXCEPTION_UTIL_H
        

Generated by: LCOV version 2.0-1