[Bug 25945] C_ASSERT doesn't fail if given non-constant expression

wine-bugs at winehq.org wine-bugs at winehq.org
Mon Jan 31 19:07:51 CST 2011


http://bugs.winehq.org/show_bug.cgi?id=25945

Alexander Scott-Johns <alexander.scott.johns+winebug at googlemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |download, source
                 CC|                            |julliard at codeweavers.com

--- Comment #1 from Alexander Scott-Johns <alexander.scott.johns+winebug at googlemail.com> 2011-01-31 19:07:50 CST ---
We could try something like (based on linux/kernel.h's BUILD_BUG_*):

  #define C_ASSERT(e)  (sizeof(struct { signed __static_assert : (e)?1:-1; }))

This will evaluate to sizeof(int), and can be used in constant expressions.
I'm not sure if this version of C_ASSERT can be made into a declaration, as
using it inside a function prototype makes GCC complain: "anonymous struct
declared inside parameter list".

If C99 features are available, this should work (it uses array literals and
array designators):

  #define C_ASSERT(e)  \
     extern void __C_ASSERT__(int [sizeof((char[]){ [-!(e)] = 0 })])

The _Static_assert(e,m) thing from C1x is implemented in GCC 4.6. If it's
available, we should use it, instead of using a homebrew solution (which
can then be a hacky, not-accepted-by-modern-GCC thing).

  #ifdef _Static_assert  /* There is a way to check for this, right? */
  # define C_ASSERT(e)  _Static_assert((e), #e " was false")
  #elsif defined(_MSC_VER)
  # define C_ASSERT(e)  typedef char __C_ASSERT__[(e)?1:-1]
  #elsif __STDC_VERSION__ >= 199901L
  # define C_ASSERT(e)  \
              extern void __C_ASSERT__(int [sizeof((char[]){ [-!(e)] = 0 })])
  #else  /* C99 not supported: no VLAs */
  # define C_ASSERT(e)  extern void __C_ASSERT__(int [(e)?1:-1])
  #endif

-- 
Configure bugmail: http://bugs.winehq.org/userprefs.cgi?tab=email
Do not reply to this email, post in Bugzilla using the
above URL to reply.
------- You are receiving this mail because: -------
You are watching all bug changes.



More information about the wine-bugs mailing list