Project

General

Profile

Bug #12994

CTASSERT should not clash on same line num

Added by Patrick Mooney 12 days ago. Updated 9 days ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Start date:
Due date:
% Done:

0%

Estimated time:
Difficulty:
Medium
Tags:
Gerrit CR:

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.

History

#1

Updated by Patrick Mooney 9 days ago

  • Description updated (diff)

Also available in: Atom PDF