我的影片定義爲斷言宏:多個錯誤斷言宏
#define likely(cond) (__builtin_expect((cond), 1))
#define unlikely(cond) (__builtin_expect(!!(cond), 0))
static void assert_fail(const char *__assertion, const char *__file,
unsigned int __line, const char *__function) {
fprintf(stderr, "\nASSERT failed %s:%d %s()\n%s\n", __file, __line, __function, __assertion);
void *array[50];
size_t size = backtrace(array, 50); // Fill out array of pointers to stack entries
backtrace_symbols_fd(&array[1], size, STDERR_FILENO);
exit(1);
}
#define assert(expr) (likely(expr) ? (void) (0) : \
assert_fail(#expr, __FILE__, __LINE__, __func__))
當你做出一個斷言狀態裏一個簡單的錯誤,如錯誤的變量名,除了工作正常,:
assert(size > 0);
它打印出一個非常明智的錯誤,然後是5個註釋(包括重複),這使得它更難以閱讀。有沒有解決這個問題的方法可以讓你更容易閱讀?核心問題似乎是使用宏,但我看不到如何避免在這裏,給定使用__FILE__
,__LINE__
等
禁用「注:每個未申報的標識符只報告一次每個功能」將縮短一半,如果這是可能的(雖然我無法找到任何方式做到這一點)
abc.c: In function 'init':
abc.c:53:12: error: 'size' undeclared (first use in this function)
assert(size > 0);
^
include/assert.h:21:41: note: in definition of macro 'likely'
#define likely(cond) (__builtin_expect((cond), 1))
^
abc.c:53:5: note: in expansion of macro 'assert'
assert(size > 0);
^
abc.c:53:12: note: each undeclared identifier is reported only once for each function it appears in
assert(size > 0);
^
include/assert.h:21:41: note: in definition of macro 'likely'
#define likely(cond) (__builtin_expect((cond), 1))
^
abc.c:53:5: note: in expansion of macro 'assert'
assert(size > 0);
^
第一個錯誤信息就足以對此進行診斷。就像往常一樣修復它 –
我從gcc切換到鏗鏘聲,大多修復它。在這種情況下,錯誤消息看起來更好,並且據稱還有很多其他消息。 – Martin