Static assert in C

2019-01-01 03:39发布

问题:

What\'s the best way to achieve compile time static asserts in C (not C++), with particular emphasis on GCC?

回答1:

C11 standard adds the _Static_assert keyword.

This is implemented since gcc-4.6:

_Static_assert (0, \"assert1\"); /* { dg-error \"static assertion failed: \\\"assert1\\\"\" } */

The first slot needs to be an integral constant expression. The second slot is a constant string literal which can be long (_Static_assert(0, L\"assertion of doom!\")).

I should note that this is also implemented in recent versions of clang.



回答2:

This works in function and non-function scope (but not inside structs,unions).

#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1]

STATIC_ASSERT(1,this_should_be_true); 

int main()
{
 STATIC_ASSERT(1,this_should_be_true); 
}
  1. If the compile time assertion could not be matched, then an almost intelligible message is generated by GCC sas.c:4: error: size of array ‘static_assertion_this_should_be_true’ is negative

  2. The macro could or should be changed to generate a unique name for the typedef (i.e. concatenate __LINE__ at the end of the static_assert_... name)

  3. Instead of a ternary, this could be used as well #define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[2*(!!(COND))-1] which happens to work even on the rusty olde cc65 (for the 6502 cpu) compiler.

UPDATE: For completeness sake, here\'s the version with __LINE__

#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(!!(COND))*2-1]
// token pasting madness:
#define COMPILE_TIME_ASSERT3(X,L) STATIC_ASSERT(X,static_assertion_at_line_##L)
#define COMPILE_TIME_ASSERT2(X,L) COMPILE_TIME_ASSERT3(X,L)
#define COMPILE_TIME_ASSERT(X)    COMPILE_TIME_ASSERT2(X,__LINE__)

COMPILE_TIME_ASSERT(sizeof(long)==8); 
int main()
{
    COMPILE_TIME_ASSERT(sizeof(int)==4); 
}

UPDATE2: GCC specific code

GCC 4.3 (I guess) introduced the \"error\" and \"warning\" function attributes. If a call to a function with that attribute could not be eliminated through dead code elimination (or other measures) then an error or warning is generated. This can be used to make compile time asserts with user defined failure descriptions. It remains to determine how they can be used in namespace scope without resorting to a dummy function:

#define CTC(X) ({ extern int __attribute__((error(\"assertion failure: \'\" #X \"\' not true\"))) compile_time_check(); ((X)?0:compile_time_check()),0; })

// never to be called.    
static void my_constraints()
{
CTC(sizeof(long)==8); 
CTC(sizeof(int)==4); 
}

int main()
{
}

And this is how it looks like:

$ gcc-mp-4.5 -m32 sas.c 
sas.c: In function \'myc\':
sas.c:7:1: error: call to \'compile_time_check\' declared with attribute error: assertion failure: `sizeof(int)==4` not true


回答3:

cl

I know the question explicitly mentions gcc, but just for completeness here is a tweak for Microsoft compilers.

Using the negatively sized array typedef does not persuade cl to spit out a decent error. It just says error C2118: negative subscript. A zero-width bitfield fares better in this respect. Since this involves typedeffing a struct, we really need to use unique type names. __LINE__ does not cut the mustard — it is possible to have a COMPILE_TIME_ASSERT() on the same line in a header and a source file, and your compile will break. __COUNTER__ comes to the rescue (and it has been in gcc since 4.3).

#define CTASTR2(pre,post) pre ## post
#define CTASTR(pre,post) CTASTR2(pre,post)
#define STATIC_ASSERT(cond,msg) \\
    typedef struct { int CTASTR(static_assertion_failed_,msg) : !!(cond); } \\
        CTASTR(static_assertion_failed_,__COUNTER__)

Now

STATIC_ASSERT(sizeof(long)==7, use_another_compiler_luke)

under cl gives:

error C2149: \'static_assertion_failed_use_another_compiler_luke\' : named bit field cannot have zero width

Gcc also gives an intelligible message:

error: zero width for bit-field ‘static_assertion_failed_use_another_compiler_luke’



回答4:

From Wikipedia:

#define COMPILE_TIME_ASSERT(pred) switch(0){case 0:case pred:;}

COMPILE_TIME_ASSERT( BOOLEAN CONDITION );


回答5:

If using the STATIC_ASSERT() macro with __LINE__, it is possible to avoid line number clashes between an entry in a .c file and a different entry in a header file by including __INCLUDE_LEVEL__.

For example :

/* Trickery to create a unique variable name */
#define BOOST_JOIN( X, Y )      BOOST_DO_JOIN( X, Y )
#define BOOST_DO_JOIN( X, Y )   BOOST_DO_JOIN2( X, Y )
#define BOOST_DO_JOIN2( X, Y )  X##Y
#define STATIC_ASSERT(x)        typedef char \\
        BOOST_JOIN( BOOST_JOIN(level_,__INCLUDE_LEVEL__), \\
                    BOOST_JOIN(_assert_on_line_,__LINE__) ) [(x) ? 1 : -1]


回答6:

The classic way is using an array:

char int_is_4_bytes_assertion[sizeof(int) == 4 ? 1 : -1];

It works because if the assertion is true the array has size 1 and it is valid, but if it is false the size of -1 gives a compilation error.

Most compilers will show the name of the variable and point to the right part of the code where you can leave eventual comments about the assertion.



回答7:

For those of you wanting something really basic and portable but don\'t have access to C++11 features, I\'ve written just the thing.
Use STATIC_ASSERT normally (you can write it twice in the same function if you want) and use GLOBAL_STATIC_ASSERT outside of functions with a unique phrase as the first parameter.

#if defined(static_assert)
#   define STATIC_ASSERT static_assert
#   define GLOBAL_STATIC_ASSERT(a, b, c) static_assert(b, c)
#else
#   define STATIC_ASSERT(pred, explanation); {char assert[1/(pred)];(void)assert;}
#   define GLOBAL_STATIC_ASSERT(unique, pred, explanation); namespace ASSERTATION {char unique[1/(pred)];}
#endif

GLOBAL_STATIC_ASSERT(first, 1, \"Hi\");
GLOBAL_STATIC_ASSERT(second, 1, \"Hi\");

int main(int c, char** v) {
    (void)c; (void)v;
    STATIC_ASSERT(1 > 0, \"yo\");
    STATIC_ASSERT(1 > 0, \"yo\");
//    STATIC_ASSERT(1 > 2, \"yo\"); //would compile until you uncomment this one
    return 0;
}

Explanation:
First it checks if you have the real assert, which you would definitely want to be using if it\'s available.
If you don\'t it asserts by getting your predicate, and dividing it by itself. This does two things.
If it\'s zero, id est, the assertion has failed, it will cause a divide by zero error (the arithmetic is forced because it is trying to declare an array).
If it is not zero, it normalises the array size to 1. So if the assertion passed, you wouldn\'t want it to fail anyway because your predicate evaluated to -1 (invalid), or be 232442 (massive waste of space, IDK if it would be optimised out).
For STATIC_ASSERT it is wrapped in braces, this makes it a block, which scopes the variable assert, meaning you can write it many times.
It also casts it to void, which is a known way to get rid of unused variable warnings.
For GLOBAL_STATIC_ASSERT, instead of being in a code block, it generates a namespace. Namespaces are allowed outside of functions. A unique identifier is required to stop any conflicting definitions if you use this one more than once.


Worked for me on GCC and VS\'12 C++



回答8:

This works, with \"remove unused\" option set. I may use one global function to check global parameters.

//
#ifndef __sassert_h__
#define __sassert_h__

#define _cat(x, y) x##y

#define _sassert(exp, ln) \\
extern void _cat(ASSERT_WARNING_, ln)(void); \\
if(!(exp)) \\
{ \\
    _cat(ASSERT_WARNING_, ln)(); \\
}

#define sassert(exp) _sassert(exp, __LINE__)

#endif //__sassert_h__

//-----------------------------------------
static bool tab_req_set_relay(char *p_packet)
{
    sassert(TXB_TX_PKT_SIZE < 3000000);
    sassert(TXB_TX_PKT_SIZE >= 3000000);
    ...
}

//-----------------------------------------
Building target: ntank_app.elf
Invoking: Cross ARM C Linker
arm-none-eabi-gcc ...
../Sources/host_if/tab_if.c:637: undefined reference to `ASSERT_WARNING_637\'
collect2: error: ld returned 1 exit status
make: *** [ntank_app.elf] Error 1
//


回答9:

This worked for some old gcc. Sorry that I forgot what version it was:

#define _cat(x, y) x##y

#define _sassert(exp, ln)\\
extern char _cat(SASSERT_, ln)[1]; \\
extern char _cat(SASSERT_, ln)[exp ? 1 : 2]

#define sassert(exp) _sassert((exp), __LINE__)

//
sassert(1 == 2);

//
#148 declaration is incompatible with \"char SASSERT_134[1]\" (declared at line 134)  main.c  /test/source/controller line 134    C/C++ Problem


回答10:

I would NOT recommend using the solution using a typedef:

#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1]

The array declaration with typedef keyword is NOT guaranteed to be evaluated at compile time. For example, the following code in block scope will compile:

int invalid_value = 0;
STATIC_ASSERT(invalid_value, this_should_fail_at_compile_time_but_will_not);

I would recommend this instead (on C99):

#define STATIC_ASSERT(COND,MSG) static int static_assertion_##MSG[(COND)?1:-1]

Because of the static keyword, the array will be defined at compile time. Note that this assert will only work with COND which are evaluated at compile time. It will not work with (i.e. the compile will fail) with conditions that are based on values in memory, such as values assigned to variables.