Skip to content

Commit 57e85ee

Browse files
committed
Visual Studio has added _Static_assert
Visual Studio now supports C11's _Static_assert (but not in compound bodies). In constrast to what is claimed at https://learn.microsoft.com/en-us/cpp/c-language/static-assert-c?view=msvc-170 the identifier static_assert is recognised as a keyword, and is not implemented as a macro.
1 parent 13a452d commit 57e85ee

File tree

11 files changed

+10
-51
lines changed

11 files changed

+10
-51
lines changed
+3-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
2-
// Not yet available in Visual Studio
3-
4-
#ifdef _MSC_VER
5-
6-
int main()
7-
{
8-
}
9-
10-
#else
11-
121
struct S
132
{
3+
// Visual Studio does not support _Static_assert in compound bodies.
4+
#ifndef _MSC_VER
145
_Static_assert(1, "in struct");
6+
#endif
157
int x;
168
} asd;
179

@@ -21,5 +13,3 @@ int main()
2113
{
2214
_Static_assert(1, "in function");
2315
}
24-
25-
#endif

regression/ansi-c/_Static_assert2/global.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE gcc-only
1+
CORE
22
global.c
33

44
static assertion failed: must fail

regression/ansi-c/_Static_assert2/local.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE gcc-only
1+
CORE
22
local.c
33

44
static assertion failed: must fail

regression/ansi-c/_Static_assert2/not_constant.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE gcc-only
1+
CORE
22
not_constant.c
33

44
expected constant expression

regression/ansi-c/anonymous_member1/main.c

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
#ifdef _MSC_VER
2-
// No _Static_assert in Visual Studio
3-
# define _Static_assert(condition, message) static_assert(condition, message)
4-
#endif
5-
61
struct S
72
{
83
struct

regression/ansi-c/simplify-overflow/main.c

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#ifdef _MSC_VER
2-
# define _Static_assert static_assert
3-
#endif
4-
51
int main()
62
{
73
_Static_assert(!__CPROVER_overflow_plus(1, 2), "");

regression/ansi-c/sizeof2/main.c

-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
#ifdef _MSC_VER
2-
3-
// No _Static_assert in Visual Studio
4-
#define _Static_assert(condition, message) \
5-
int some_array##__LINE__[(condition) ? 1 : -1];
6-
7-
#endif
8-
91
// sizeof is unsigned
102
_Static_assert( 1 - sizeof(int) >=0, "sizeof is unsigned" );
113

regression/cbmc/Bitfields5/main.c

-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ struct S0
77
int x;
88
};
99

10-
#ifdef _MSC_VER
11-
# define _Static_assert static_assert
12-
#endif
13-
1410
int main()
1511
{
1612
struct S0 g = {0};

regression/cbmc/inequality-with-constant-normalisation1/main.c

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#include <assert.h>
22

3-
#ifdef _MSC_VER
4-
# define _Static_assert(x, m) static_assert(x, m)
5-
#endif
6-
73
int main()
84
{
95
_Bool b1, b2;

regression/cbmc/member_bounds3/main.c

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ struct S
88
};
99
#pragma pack(pop)
1010

11-
#ifdef _MSC_VER
12-
# define _Static_assert(x, m) static_assert(x, m)
13-
#endif
14-
1511
int main()
1612
{
1713
int A[3];

src/ansi-c/scanner.l

+4-6
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,9 @@ or_eq { return cpp98_keyword(TOK_ORASSIGN); }
876876
private { return cpp98_keyword(TOK_PRIVATE); }
877877
protected { return cpp98_keyword(TOK_PROTECTED); }
878878
public { return cpp98_keyword(TOK_PUBLIC); }
879-
static_assert { // C++11, but Visual Studio supports it in all modes (and
880-
// doesn't support _Static_assert)
879+
static_assert { // C++11, but Visual Studio supports it in all modes
880+
// as a keyword, even though the documentation claims
881+
// it's a macro.
881882
if(PARSER.mode == configt::ansi_ct::flavourt::VISUAL_STUDIO)
882883
{
883884
loc(); return TOK_STATIC_ASSERT;
@@ -1490,10 +1491,7 @@ __decltype { if(PARSER.cpp98 &&
14901491

14911492
/* This is a C11 keyword */
14921493

1493-
"_Static_assert" { if(!PARSER.cpp98 &&
1494-
(PARSER.mode==configt::ansi_ct::flavourt::GCC ||
1495-
PARSER.mode==configt::ansi_ct::flavourt::CLANG ||
1496-
PARSER.mode==configt::ansi_ct::flavourt::ARM))
1494+
"_Static_assert" { if(!PARSER.cpp98)
14971495
{ loc(); return TOK_STATIC_ASSERT; }
14981496
else
14991497
return make_identifier();

0 commit comments

Comments
 (0)