File: static_assert.c

package info (click to toggle)
frama-c 20220511-manganese-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 66,472 kB
  • sloc: ml: 278,832; ansic: 47,093; sh: 4,823; makefile: 3,618; javascript: 2,436; python: 1,919; perl: 897; lisp: 293; xml: 62
file content (39 lines) | stat: -rw-r--r-- 867 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* run.config
   STDOPT: #"-c11"
   EXIT: 1
   STDOPT: #"-c11 -cpp-extra-args=-DFAIL"
*/

_Static_assert(1, "string");

#include <assert.h>

#ifdef FAIL
static_assert(0, "fail");
#endif

_Static_assert(2); // without message string; this is not C11-compliant, but
                   // a C++17 extension. GCC/Clang allow it.

// _Static_assert can also appear inside struct declarations
struct st { _Static_assert (sizeof(char) == 1, "inside struct"); int a; };

int main() {
  static_assert(sizeof(int) > sizeof(char), "int must be greater than char");

#ifdef FAIL
  int a = 42;
  static_assert(a == 42, "non-static condition");
#endif

  int ret = 42;

  static_assert(3); // between statements

  return ret;
}

#ifdef FAIL
static_assert(0); // fail without message string
struct st2 { _Static_assert (sizeof(char) > 1, "failure inside struct"); int a; };
#endif