Bug #12994
openCTASSERT should not clash on same line num
0%
Description
I was doing work on bhyve, which uses CTASSERTs to enforce sizing and alignment for certain data structures. While building to test a change, I encountered this unexpected error:
../../i86pc/io/vmm/intel/vmx.h:158: error: redefinition of typedef '__compile_time_assertion__158' ../../i86pc/sys/vmm.h:158: note: previous declaration of '__compile_time_assertion__158' was here
As it turns out, there are CTASSERTs in both of those files (checking different things, of course) on the same line. Upon examination of how CTASSERT is implemented, the reason for this conflict became clear:
/* * Compile-time assertion. The condition 'x' must be constant. */ #define CTASSERT(x) _CTASSERT(x, __LINE__) #define _CTASSERT(x, y) __CTASSERT(x, y) #define __CTASSERT(x, y) \ typedef char __compile_time_assertion__ ## y [(x) ? 1 : -1] __unused
The preprocessor tricks it is using to evaluate the conditions of the assert uses the statement line number for uniqueness. This is all fine and good until a situation like the above arises, where CTASSERTs are present on the same line number in more than one file. On C11-capable compilers, we could potentially detect and use _Static_assert
as the implementation. (It appears that _Static_assert
support was added to gcc in 4.6) Absent that detection and use of the __COUNTER__
preprocessor variable which could help differentiate CTASSERTs between different files.
Related issues
Updated by Rich Lowe 11 months ago
- Has duplicate Bug #13894: compile time assertions need more uniqueness added
Updated by Patrick Mooney 4 months ago
- Related to Feature #14454: sys/debug.h: CTASSERT should use _Static_assert added