From 7c60c990fbed62aab1369c0e4462c4081dc3cfeb Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Thu, 10 Oct 2019 14:33:47 +0100 Subject: COMPMID-2486: Remove disabled compiler warnings Removed the following flags: -Wno-format-nonliteral: This had a side effect on Error.h that resulted in rewriting most of the macros. Since I was at it I removed all the va_args in order to comply with DCL50-CPP. -Wno-deprecated-increment-bool -Wno-vla-extension -Wno-mismatched-tags -Wno-redundant-move Change-Id: I7c593854ecc3b7d595b8edcbd6a86d3c2563c6bd Signed-off-by: Michalis Spyrou Reviewed-on: https://review.mlplatform.org/c/2069 Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Reviewed-by: Georgios Pinitas --- arm_compute/core/Error.h | 253 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 191 insertions(+), 62 deletions(-) (limited to 'arm_compute/core/Error.h') diff --git a/arm_compute/core/Error.h b/arm_compute/core/Error.h index 64bfbd2787..dcbba1e990 100644 --- a/arm_compute/core/Error.h +++ b/arm_compute/core/Error.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2018 ARM Limited. + * Copyright (c) 2016-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -24,7 +24,7 @@ #ifndef __ARM_COMPUTE_ERROR_H__ #define __ARM_COMPUTE_ERROR_H__ -#include +#include #include namespace arm_compute @@ -116,39 +116,31 @@ private: std::string _error_description; }; -/** Creates an error containing the error message from variable argument list +/** Creates an error containing the error message * * @param[in] error_code Error code - * @param[in] function Function in which the error occurred. - * @param[in] file Name of the file where the error occurred. - * @param[in] line Line on which the error occurred. * @param[in] msg Message to display before aborting. - * @param[in] args Variable argument list of the message. * * @return status containing the error */ -Status create_error_va_list(ErrorCode error_code, const char *function, const char *file, const int line, const char *msg, va_list args); -/** Creates an error containing the error message +Status create_error(ErrorCode error_code, std::string msg); + +/** Creates an error and the error message * * @param[in] error_code Error code - * @param[in] function Function in which the error occurred. - * @param[in] file Name of the file where the error occurred. - * @param[in] line Line on which the error occurred. + * @param[in] func Function in which the error occurred. + * @param[in] file File in which the error occurred. + * @param[in] line Line in which the error occurred. * @param[in] msg Message to display before aborting. - * @param[in] ... Variable number of arguments of the message. * * @return status containing the error */ -Status create_error(ErrorCode error_code, const char *function, const char *file, const int line, const char *msg, ...); -/** Print an error message then throw an std::runtime_error +Status create_error_msg(ErrorCode error_code, const char *func, const char *file, int line, const char *msg); +/** Throw an std::runtime_error * - * @param[in] function Function in which the error occurred. - * @param[in] file Name of the file where the error occurred. - * @param[in] line Line on which the error occurred. - * @param[in] msg Message to display before aborting. - * @param[in] ... Variable number of arguments of the message. + * @param[in] err Error status */ -[[noreturn]] void error(const char *function, const char *file, const int line, const char *msg, ...); +[[noreturn]] void throw_error(Status err); } /** To avoid unused variables warnings * @@ -162,9 +154,9 @@ Status create_error(ErrorCode error_code, const char *function, const char *file /** Creates an error with a given message * * @param[in] error_code Error code. - * @param[in] ... Message to encapsulate. + * @param[in] msg Message to encapsulate. */ -#define ARM_COMPUTE_CREATE_ERROR(error_code, ...) ::arm_compute::create_error(error_code, __func__, __FILE__, __LINE__, __VA_ARGS__) // NOLINT +#define ARM_COMPUTE_CREATE_ERROR(error_code, msg) arm_compute::create_error_msg(error_code, __func__, __FILE__, __LINE__, msg) /** Creates an error on location with a given message * @@ -172,9 +164,28 @@ Status create_error(ErrorCode error_code, const char *function, const char *file * @param[in] func Function in which the error occurred. * @param[in] file File in which the error occurred. * @param[in] line Line in which the error occurred. - * @param[in] ... Message to display before aborting. + * @param[in] msg Message to display before aborting. + */ +#define ARM_COMPUTE_CREATE_ERROR_LOC(error_code, func, file, line, msg) arm_compute::create_error_msg(error_code, func, file, line, msg) + +/** Creates an error on location with a given message. Accepts a message format + * and a variable list of arguments matching the format description. + * + * @param[in] error_code Error code. + * @param[in] func Function in which the error occurred. + * @param[in] file File in which the error occurred. + * @param[in] line Line in which the error occurred. + * @param[in] msg Error description message format. + * @param[in] ... List of arguments matching the format description. */ -#define ARM_COMPUTE_CREATE_ERROR_LOC(error_code, func, file, line, ...) ::arm_compute::create_error(error_code, func, file, line, __VA_ARGS__) // NOLINT +#define ARM_COMPUTE_CREATE_ERROR_LOC_VAR(error_code, func, file, line, msg, ...) \ + do \ + { \ + std::array out{ 0 }; \ + int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", func, file, line); \ + snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \ + arm_compute::create_error(error_code, std::string(out.data())); \ + } while(false) /** An error is returned with the given description. * @@ -206,35 +217,76 @@ Status create_error(ErrorCode error_code, const char *function, const char *file #define ARM_COMPUTE_THROW_ON_ERROR(error) \ error.throw_if_error(); +/** If the condition is true, an error is returned. Accepts a message format + * and a variable list of arguments matching the format description. + * + * @param[in] cond Condition to evaluate. + * @param[in] msg Error description message format. + * @param[in] ... List of arguments matching the format description. + */ +#define ARM_COMPUTE_RETURN_ERROR_ON_MSG_VAR(cond, msg, ...) \ + do \ + { \ + if(cond) \ + { \ + std::array out{ 0 }; \ + int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", __func__, __FILE__, __LINE__); \ + snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \ + return arm_compute::create_error(arm_compute::ErrorCode::RUNTIME_ERROR, std::string(out.data())); \ + } \ + } while(false) + /** If the condition is true, an error is returned * * @param[in] cond Condition to evaluate. - * @param[in] ... Error description message + * @param[in] msg Error description message + */ +#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg) \ + do \ + { \ + if(cond) \ + { \ + return arm_compute::create_error_msg(arm_compute::ErrorCode::RUNTIME_ERROR, __func__, __FILE__, __LINE__, msg); \ + } \ + } while(false) + +/** If the condition is true, an error is thrown. Accepts a message format + * and a variable list of arguments matching the format description. + * + * @param[in] cond Condition to evaluate. + * @param[in] func Function in which the error occurred. + * @param[in] file File in which the error occurred. + * @param[in] line Line in which the error occurred. + * @param[in] msg Error description message format. + * @param[in] ... List of arguments matching the format description. */ -#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, ...) \ +#define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG_VAR(cond, func, file, line, msg, ...) \ do \ { \ if(cond) \ { \ - return ARM_COMPUTE_CREATE_ERROR(arm_compute::ErrorCode::RUNTIME_ERROR, __VA_ARGS__); \ + std::array out{ 0 }; \ + int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", func, file, line); \ + snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \ + return arm_compute::create_error(ErrorCode::RUNTIME_ERROR, std::string(out.data())); \ } \ } while(false) -/** If the condition is true, an error is thrown +/** If the condition is true, an error is thrown. * * @param[in] cond Condition to evaluate. * @param[in] func Function in which the error occurred. * @param[in] file File in which the error occurred. * @param[in] line Line in which the error occurred. - * @param[in] ... Error description message. + * @param[in] msg Message to display. */ -#define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, ...) \ - do \ - { \ - if(cond) \ - { \ - return ARM_COMPUTE_CREATE_ERROR_LOC(arm_compute::ErrorCode::RUNTIME_ERROR, func, file, line, __VA_ARGS__); \ - } \ +#define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, msg) \ + do \ + { \ + if(cond) \ + { \ + return arm_compute::create_error_msg(ErrorCode::RUNTIME_ERROR, func, file, line, msg); \ + } \ } while(false) /** If the condition is true, an error is returned @@ -256,31 +308,97 @@ Status create_error(ErrorCode error_code, const char *function, const char *file /** Print the given message then throw an std::runtime_error. * - * @param[in] ... Message to display before aborting. + * @param[in] func Function in which the error occurred. + * @param[in] file File in which the error occurred. + * @param[in] line Line in which the error occurred. + * @param[in] msg Message to display. + */ +#define ARM_COMPUTE_THROW_ERROR(func, file, line, msg) \ + do \ + { \ + arm_compute::throw_error(arm_compute::create_error_msg(arm_compute::ErrorCode::RUNTIME_ERROR, func, file, line, msg)); \ + } while(false) + +/** Print the given message then throw an std::runtime_error. Accepts a message format + * and a variable list of arguments matching the format description. + * + * @param[in] func Function in which the error occurred. + * @param[in] file File in which the error occurred. + * @param[in] line Line in which the error occurred. + * @param[in] msg Error description message format. + * @param[in] ... List of arguments matching the format description. + */ +#define ARM_COMPUTE_THROW_ERROR_VAR(func, file, line, msg, ...) \ + do \ + { \ + std::array out{ 0 }; \ + int offset = snprintf(out.data(), out.size(), "in %s %s:%d: ", func, file, line); \ + snprintf(out.data() + offset, out.size() - offset, msg, __VA_ARGS__); \ + arm_compute::throw_error(arm_compute::Status(arm_compute::ErrorCode::RUNTIME_ERROR, std::string(out.data()))); \ + } while(false) + +/** Print the given message then throw an std::runtime_error. Accepts a message format + * and a variable list of arguments matching the format description. + * + * @param[in] msg Error description message format. + * @param[in] ... List of arguments matching the format description. + */ +#define ARM_COMPUTE_ERROR_VAR(msg, ...) ARM_COMPUTE_THROW_ERROR_VAR(__func__, __FILE__, __LINE__, msg, __VA_ARGS__) + +/** Print the given message then throw an std::runtime_error. + * + * @param[in] msg Message to display. + */ +#define ARM_COMPUTE_ERROR(msg) ARM_COMPUTE_THROW_ERROR(__func__, __FILE__, __LINE__, msg) + +/** Print the given message then throw an std::runtime_error. Accepts a message format + * and a variable list of arguments matching the format description. + * + * @param[in] func Function in which the error occurred. + * @param[in] file File in which the error occurred. + * @param[in] line Line in which the error occurred. + * @param[in] msg Error description message format. + * @param[in] ... List of arguments matching the format description. */ -#define ARM_COMPUTE_ERROR(...) ::arm_compute::error(__func__, __FILE__, __LINE__, __VA_ARGS__) // NOLINT +#define ARM_COMPUTE_ERROR_LOC_VAR(func, file, line, msg, ...) ARM_COMPUTE_THROW_ERROR_VAR(func, file, line, msg, __VA_ARGS__) // NOLINT /** Print the given message then throw an std::runtime_error. * * @param[in] func Function in which the error occurred. * @param[in] file File in which the error occurred. * @param[in] line Line in which the error occurred. - * @param[in] ... Message to display before aborting. + * @param[in] msg Message to display. */ -#define ARM_COMPUTE_ERROR_LOC(func, file, line, ...) ::arm_compute::error(func, file, line, __VA_ARGS__) // NOLINT +#define ARM_COMPUTE_ERROR_LOC(func, file, line, msg) ARM_COMPUTE_THROW_ERROR(func, file, line, msg) // NOLINT /** If the condition is true, the given message is printed and program exits * * @param[in] cond Condition to evaluate. - * @param[in] ... Message to print if cond is false. + * @param[in] msg Message to display. */ -#define ARM_COMPUTE_EXIT_ON_MSG(cond, ...) \ - do \ - { \ - if(cond) \ - { \ - ARM_COMPUTE_ERROR(__VA_ARGS__); \ - } \ +#define ARM_COMPUTE_EXIT_ON_MSG(cond, msg) \ + do \ + { \ + if(cond) \ + { \ + ARM_COMPUTE_ERROR(msg); \ + } \ + } while(false) + +/** If the condition is true, the given message is printed and program exits. Accepts a message format + * and a variable list of arguments matching the format description. + * + * @param[in] cond Condition to evaluate. + * @param[in] msg Error description message format. + * @param[in] ... List of arguments matching the format description. + */ +#define ARM_COMPUTE_EXIT_ON_MSG_VAR(cond, msg, ...) \ + do \ + { \ + if(cond) \ + { \ + ARM_COMPUTE_ERROR_VAR(msg, __VA_ARGS__); \ + } \ } while(false) #ifdef ARM_COMPUTE_ASSERTS_ENABLED @@ -294,12 +412,22 @@ Status create_error(ErrorCode error_code, const char *function, const char *file /** If the condition is true, the given message is printed and an exception is thrown * * @param[in] cond Condition to evaluate. - * @param[in] ... Message to print if cond is false. + * @param[in] msg Message to display. */ -#define ARM_COMPUTE_ERROR_ON_MSG(cond, ...) \ - ARM_COMPUTE_EXIT_ON_MSG(cond, __VA_ARGS__) +#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg) \ + ARM_COMPUTE_EXIT_ON_MSG(cond, msg) -/** If the condition is true, the given message is printed and an exception is thrown +/** If the condition is true, the given message is printed and an exception is thrown. Accepts a message format + * and a variable list of arguments matching the format description. + * + * @param[in] cond Condition to evaluate. + * @param[in] msg Error description message format. + * @param[in] ... List of arguments matching the format description. + */ +#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg, ...) \ + ARM_COMPUTE_EXIT_ON_MSG_VAR(cond, msg, __VA_ARGS__) + +/** If the condition is true, the given message is printed and an exception is thrown. * * @param[in] cond Condition to evaluate. * @param[in] func Function in which the error occurred. @@ -307,13 +435,13 @@ Status create_error(ErrorCode error_code, const char *function, const char *file * @param[in] line Line in which the error occurred. * @param[in] ... Message to print if cond is false. */ -#define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...) \ - do \ - { \ - if(cond) \ - { \ - ARM_COMPUTE_ERROR_LOC(func, file, line, __VA_ARGS__); \ - } \ +#define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...) \ + do \ + { \ + if(cond) \ + { \ + ARM_COMPUTE_ERROR_LOC_VAR(func, file, line, __VA_ARGS__); \ + } \ } while(false) /** If the condition is true, the given message is printed and an exception is thrown, otherwise value is returned @@ -325,7 +453,8 @@ Status create_error(ErrorCode error_code, const char *function, const char *file #define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) (cond) ? throw std::logic_error(msg) : val; #else /* ARM_COMPUTE_ASSERTS_ENABLED */ #define ARM_COMPUTE_ERROR_THROW_ON(status) -#define ARM_COMPUTE_ERROR_ON_MSG(cond, ...) +#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg) +#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg, ...) #define ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, ...) #define ARM_COMPUTE_CONST_ON_ERROR(cond, val, msg) val #endif /* ARM_COMPUTE_ASSERTS_ENABLED */ @@ -345,7 +474,7 @@ Status create_error(ErrorCode error_code, const char *function, const char *file * @param[in] line Line in which the error occurred. */ #define ARM_COMPUTE_ERROR_ON_LOC(cond, func, file, line) \ - ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, #cond) + ARM_COMPUTE_ERROR_ON_LOC_MSG(cond, func, file, line, "%s", #cond) #ifndef ARM_COMPUTE_EXCEPTIONS_DISABLED #define ARM_COMPUTE_THROW(ex) throw(ex) -- cgit v1.2.1