Skip to content

Commit

Permalink
Fix pedantic warning on assert macro (#2436)
Browse files Browse the repository at this point in the history
* Fix macro pedantic warning

* Removed unnecessary declaration

* Added documentation
  • Loading branch information
JohanBertrand authored Dec 20, 2023
1 parent 221149a commit 1e96e67
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 156 deletions.
190 changes: 51 additions & 139 deletions Fw/Types/Assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,188 +165,100 @@ namespace Fw {
s_assertHook = this->previousHook;
}

NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo) {
// Default handler of SwAssert functions
NATIVE_INT_TYPE defaultSwAssert(
FILE_NAME_ARG file,
NATIVE_UINT_TYPE lineNo,
NATIVE_UINT_TYPE numArgs,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3,
FwAssertArgType arg4,
FwAssertArgType arg5,
FwAssertArgType arg6) {
if (nullptr == s_assertHook) {
CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
defaultReportAssert(
file,
lineNo,
0,
0,0,0,0,0,0,
numArgs,
arg1,arg2,arg3,arg4,arg5,arg6,
assertMsg,sizeof(assertMsg));
// print message
defaultPrintAssert(assertMsg);
assert(0);
}
else {
s_assertHook->reportAssert(
file,
lineNo,
0,
0,0,0,0,0,0);
numArgs,
arg1,arg2,arg3,arg4,arg5,arg6);
s_assertHook->doAssert();
}
return 0;
}

NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
FwAssertArgType arg1) {
if (nullptr == s_assertHook) {
CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
defaultReportAssert(
file,
lineNo,
1,
arg1,0,0,0,0,0,
assertMsg,sizeof(assertMsg));
// print message
defaultPrintAssert(assertMsg);
assert(0);
}
else {
s_assertHook->reportAssert(
file,
lineNo,
1,
arg1,0,0,0,0,0);
s_assertHook->doAssert();
}
return 0;
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
NATIVE_UINT_TYPE lineNo) {
return defaultSwAssert(file, lineNo, 0, 0, 0, 0, 0, 0, 0);
}

NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2) {
if (nullptr == s_assertHook) {
CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
defaultReportAssert(
file,
lineNo,
2,
arg1,arg2,0,0,0,0,
assertMsg,sizeof(assertMsg));
defaultPrintAssert(assertMsg);
assert(0);
}
else {
s_assertHook->reportAssert(
file,
lineNo,
2,
arg1,arg2,0,0,0,0);
s_assertHook->doAssert();
}
return 0;
NATIVE_UINT_TYPE lineNo) {
return defaultSwAssert(file, lineNo, 1, arg1, 0, 0, 0, 0, 0);
}

NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3) {
if (nullptr == s_assertHook) {
CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
defaultReportAssert(
file,
lineNo,
3,
arg1,arg2,arg3,0,0,0,
assertMsg,sizeof(assertMsg));
defaultPrintAssert(assertMsg);
assert(0);
}
else {
s_assertHook->reportAssert(
file,
lineNo,
3,
arg1,arg2,arg3,0,0,0);
s_assertHook->doAssert();
}
return 0;
NATIVE_UINT_TYPE lineNo) {
return defaultSwAssert(file, lineNo, 2, arg1, arg2, 0, 0, 0, 0);
}

NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3,
FwAssertArgType arg4) {
if (nullptr == s_assertHook) {
CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
defaultReportAssert(
file,
lineNo,
4,
arg1,arg2,arg3,arg4,0,0,
assertMsg,sizeof(assertMsg));
defaultPrintAssert(assertMsg);
assert(0);
}
else {
s_assertHook->reportAssert(
file,
lineNo,
4,
arg1,arg2,arg3,arg4,0,0);
s_assertHook->doAssert();
}
return 0;
NATIVE_UINT_TYPE lineNo) {
return defaultSwAssert(file, lineNo, 3, arg1, arg2, arg3, 0, 0, 0);
}

NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3,
FwAssertArgType arg4,
FwAssertArgType arg5) {
if (nullptr == s_assertHook) {
CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
defaultReportAssert(
file,
lineNo,
5,
arg1,arg2,arg3,arg4,arg5,0,
assertMsg,sizeof(assertMsg));
defaultPrintAssert(assertMsg);
assert(0);
}
else {
s_assertHook->reportAssert(
file,
lineNo,
5,
arg1,arg2,arg3,arg4,arg5,0);
s_assertHook->doAssert();
}
return 0;
NATIVE_UINT_TYPE lineNo) {
return defaultSwAssert(file, lineNo, 4, arg1, arg2, arg3, arg4, 0, 0);
}

NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo,
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3,
FwAssertArgType arg4,
FwAssertArgType arg5,
FwAssertArgType arg6) {
if (nullptr == s_assertHook) {
CHAR assertMsg[FW_ASSERT_DFL_MSG_LEN];
defaultReportAssert(
file,
lineNo,
6,
arg1,arg2,arg3,arg4,arg5,arg6,
assertMsg,sizeof(assertMsg));
defaultPrintAssert(assertMsg);
assert(0);
}
else {
s_assertHook->reportAssert(
file,
lineNo,
6,
arg1,arg2,arg3,arg4,arg5,arg6);
s_assertHook->doAssert();
}
return 0;
NATIVE_UINT_TYPE lineNo) {
return defaultSwAssert(file, lineNo, 5, arg1, arg2, arg3, arg4, arg5, 0);
}

NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3,
FwAssertArgType arg4,
FwAssertArgType arg5,
FwAssertArgType arg6,
NATIVE_UINT_TYPE lineNo) {
return defaultSwAssert(file, lineNo, 6, arg1, arg2, arg3, arg4, arg5, arg6);
}
}

Expand Down
88 changes: 71 additions & 17 deletions Fw/Types/Assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@
#define FW_ASSERT(...)
#else // ASSERT is defined

// Return only the first argument passed to the macro.
#define FW_ASSERT_FIRST_ARG(ARG_0, ...) ARG_0
// Return all the arguments of the macro, but the first one
#define FW_ASSERT_NO_FIRST_ARG(ARG_0, ...) __VA_ARGS__

// Passing the __LINE__ argument at the end of the function ensures that
// the FW_ASSERT_NO_FIRST_ARG macro will never have an empty variadic variable
#if FW_ASSERT_LEVEL == FW_FILEID_ASSERT
#define FILE_NAME_ARG U32
#define FW_ASSERT(cond, ...) \
((void) ((cond) ? (0) : \
(Fw::SwAssert(ASSERT_FILE_ID, __LINE__, ##__VA_ARGS__))))
#define FW_ASSERT(...) \
((void) ((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) ? (0) : \
(Fw::SwAssert(ASSERT_FILE_ID, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
#elif FW_ASSERT_LEVEL == FW_RELATIVE_PATH_ASSERT
#define FILE_NAME_ARG const CHAR*
#define FW_ASSERT(cond, ...) \
((void) ((cond) ? (0) : \
(Fw::SwAssert(ASSERT_RELATIVE_PATH, __LINE__, ##__VA_ARGS__))))
#define FW_ASSERT(...) \
((void) ((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) ? (0) : \
(Fw::SwAssert(ASSERT_RELATIVE_PATH, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
#else
#define FILE_NAME_ARG const CHAR*
#define FW_ASSERT(cond, ...) \
((void) ((cond) ? (0) : \
(Fw::SwAssert(__FILE__, __LINE__, ##__VA_ARGS__))))
#define FW_ASSERT(...) \
((void) ((FW_ASSERT_FIRST_ARG(__VA_ARGS__, 0)) ? (0) : \
(Fw::SwAssert(__FILE__, FW_ASSERT_NO_FIRST_ARG(__VA_ARGS__, __LINE__)))))
#endif

// F' Assertion functions can technically return even though the intention is for the assertion to terminate the program.
Expand All @@ -40,13 +46,61 @@
#endif

namespace Fw {
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo) CLANG_ANALYZER_NORETURN; //!< Assert with no arguments
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, FwAssertArgType arg1) CLANG_ANALYZER_NORETURN; //!< Assert with one argument
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, FwAssertArgType arg1, FwAssertArgType arg2) CLANG_ANALYZER_NORETURN; //!< Assert with two arguments
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3) CLANG_ANALYZER_NORETURN; //!< Assert with three arguments
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4) CLANG_ANALYZER_NORETURN; //!< Assert with four arguments
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5) CLANG_ANALYZER_NORETURN; //!< Assert with five arguments
NATIVE_INT_TYPE SwAssert(FILE_NAME_ARG file, NATIVE_UINT_TYPE lineNo, FwAssertArgType arg1, FwAssertArgType arg2, FwAssertArgType arg3, FwAssertArgType arg4, FwAssertArgType arg5, FwAssertArgType arg6) CLANG_ANALYZER_NORETURN; //!< Assert with six arguments
//! Assert with no arguments
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
NATIVE_UINT_TYPE lineNo) CLANG_ANALYZER_NORETURN;

//! Assert with one argument
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
NATIVE_UINT_TYPE lineNo) CLANG_ANALYZER_NORETURN;

//! Assert with two arguments
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
NATIVE_UINT_TYPE lineNo) CLANG_ANALYZER_NORETURN;

//! Assert with three arguments
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3,
NATIVE_UINT_TYPE lineNo) CLANG_ANALYZER_NORETURN;

//! Assert with four arguments
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3,
FwAssertArgType arg4,
NATIVE_UINT_TYPE lineNo) CLANG_ANALYZER_NORETURN;

//! Assert with five arguments
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3,
FwAssertArgType arg4,
FwAssertArgType arg5,
NATIVE_UINT_TYPE lineNo) CLANG_ANALYZER_NORETURN;

//! Assert with six arguments
NATIVE_INT_TYPE SwAssert(
FILE_NAME_ARG file,
FwAssertArgType arg1,
FwAssertArgType arg2,
FwAssertArgType arg3,
FwAssertArgType arg4,
FwAssertArgType arg5,
FwAssertArgType arg6,
NATIVE_UINT_TYPE lineNo) CLANG_ANALYZER_NORETURN;
}

// Base class for declaring an assert hook
Expand Down Expand Up @@ -91,4 +145,4 @@ namespace Fw {
#endif // if ASSERT is defined


#endif
#endif // FW_ASSERT_HPP

0 comments on commit 1e96e67

Please sign in to comment.