【C/C++】函数入参检查
// 统计变参数量
#define CALC_VA_COUNT(arg...) \
({ \
int count = 0; \
int insideQuotes = 0; \
const char *str = #arg; \
printf("D> CALC_VA_COUNT: %s\n", str); \
while (*str != '\0') { \
if (*str == '"') { \
insideQuotes = !insideQuotes; \
} else if (*str == ',' && !insideQuotes) { \
count++; \
} \
str++; \
} \
++count; \
})
// 变参数量多于1个,则生效最后1个(逗号表达式)
#define CHECK(expr, ...) \
do { \
if (!(expr)) { \
printf("check fail: \"%s\" @ %s, %u\r\n", #expr, __FILE__, __LINE__); \
return __VA_ARGS__; \
} \
} while(0)
#define ASSERT(expr) (void)((!!(expr)) || (printf("assert fail: \"%s\" @ %s, %u", #expr, __FILE__, __LINE__), printf("\r\n")))
// usage:
ASSERT(arg != NULL)
#define ASSERT(expr) (void)((!!(expr)) && (printf("assert fail: \"%s\" @ %s, %u", #expr, __FILE__, __LINE__), printf("\r\n")))
// usage:
ASSERT(arg == NULL)
#define ASSERT(expr) (int)((!!(expr)) || (printf("assert fail: \"%s\" @ %s, %u", #expr, __FILE__, __LINE__), printf("\r\n"), Abort()))
// usage:
if (!ASSERT(arg != NULL)) {
return;
}
#define ASSERT(expr) (int)((!!(expr)) && (printf("assert fail: \"%s\" @ %s, %u", #expr, __FILE__, __LINE__), printf("\r\n"), Abort()))
// usage:
if (!ASSERT(arg == NULL)) {
return;
}
int Abort(void)
{
printf("reset reboot!\n");
// 处理异常代码
}
示例:
// Powered by skull
#include <stdio.h>
#define CALC_VA_COUNT(arg...) \
({ \
int count = 0; \
int insideQuotes = 0; \
const char *str = #arg; \
printf("D> CALC_VA_COUNT: %s\n", str); \
while (*str != '\0') { \
if (*str == '"') { \
insideQuotes = !insideQuotes; \
} else if (*str == ',' && !insideQuotes) { \
count++; \
} \
str++; \
} \
++count; \
})
#define CHECK(expr, ...) \
do { \
if (!(expr)) { \
printf("I> check fail: \"%s\" @ %s, %u\r\n", #expr, __FILE__, __LINE__); \
printf("D> __VA_ARGS__: %s\r\n", #__VA_ARGS__); \
if (CALC_VA_COUNT(__VA_ARGS__) > 1) printf("W> too many parameters. last effective!\r\n"); \
return __VA_ARGS__; \
} \
} while(0)
#define ASSERT1(expr) (void)((!!(expr)) || (printf("A> assert fail: \"%s\" @ %s, %u", #expr, __FILE__, __LINE__), printf("\r\n")))
#define ASSERT2(expr) (void)((!!(expr)) && (printf("A> assert fail: \"%s\" @ %s, %u", #expr, __FILE__, __LINE__), printf("\r\n")))
#define ASSERT3(expr) (int)((!!(expr)) || (printf("A> assert fail: \"%s\" @ %s, %u", #expr, __FILE__, __LINE__), printf("\r\n"), Abort1()))
#define ASSERT4(expr) (int)((!!(expr)) && (printf("A> assert fail: \"%s\" @ %s, %u", #expr, __FILE__, __LINE__), printf("\r\n"), Abort2()))
int Abort1(void)
{
printf("reset reboot!\n");
// 异常处理
}
int Abort2(void)
{
printf("reset reboot!\n");
// 异常处理
return -1;
}
void test_assert(void *pointer)
{
ASSERT1(pointer != NULL);
ASSERT2(pointer == NULL);
if (ASSERT3(pointer != NULL)) {
printf("ASSERT3\n");
}
if (!ASSERT3(pointer != NULL)) {
printf("!ASSERT3\n");
}
if (ASSERT4(pointer == NULL)) {
printf("ASSERT4\n");
}
if (!ASSERT4(pointer == NULL)) {
printf("!ASSERT4\n");
}
}
void test_check(void *pointer)
{
CHECK(pointer == NULL);
printf("check @%s\n", __func__);
}
void test_check1(void *pointer)
{
CHECK(pointer != NULL);
printf("check @%s\n", __func__);
}
char *test_check2(void *pointer)
{
CHECK(pointer != NULL, "check fail!");
printf("check @%s\n", __func__);
}
char *test_check3(void *pointer)
{
CHECK(pointer != NULL, "check fail!", __func__);
printf("check @%s\n", __func__);
}
int main(void)
{
int *arg;
printf("Hello Assert!\n");
test_assert(arg);
printf("Hello check!\n");
test_check(arg);
test_check1(arg);
printf("check result: %s\n", test_check2(arg));
printf("check result: %s\n", test_check3(arg));
return 0;
}
再牛逼的梦想也架不住傻逼似的坚持