1
0
mirror of https://github.com/cc65/cc65.git synced 2025-01-11 11:30:13 +00:00

Support C2X _Static_assert(expr) syntax

This makes the message in _Static_assert(expr, message) optional.

Fixes #1188.
This commit is contained in:
Jesse Rosenstock 2020-08-15 21:05:51 +02:00 committed by Oliver Schmidt
parent c4698dfd07
commit 1cf9404c19
2 changed files with 31 additions and 20 deletions

View File

@ -49,6 +49,7 @@ void ParseStaticAssert ()
{
/*
** static_assert-declaration ::=
** _Static_assert ( constant-expression ) ;
** _Static_assert ( constant-expression , string-literal ) ;
*/
ExprDesc Expr;
@ -67,27 +68,35 @@ void ParseStaticAssert ()
ConstAbsIntExpr (hie1, &Expr);
failed = !Expr.IVal;
/* We expect a comma */
if (!ConsumeComma ()) {
return;
}
/* String literal */
if (CurTok.Tok != TOK_SCONST) {
Error ("String literal expected for static_assert message");
return;
}
/* Issue an error including the message if the static_assert failed. */
if (failed) {
Error ("static_assert failed '%s'", GetLiteralStr (CurTok.SVal));
}
/* Consume the string constant, now that we don't need it anymore.
** This should never fail since we checked the token type above.
/* If there is a comma, we also have an error message. The message is optional because we
** support the C2X syntax with only an expression.
*/
if (!Consume (TOK_SCONST, "String literal expected")) {
return;
if (CurTok.Tok == TOK_COMMA) {
/* Skip the comma. */
NextToken ();
/* String literal */
if (CurTok.Tok != TOK_SCONST) {
Error ("String literal expected for static_assert message");
return;
}
/* Issue an error including the message if the static_assert failed. */
if (failed) {
Error ("static_assert failed '%s'", GetLiteralStr (CurTok.SVal));
}
/* Consume the string constant, now that we don't need it anymore.
** This should never fail since we checked the token type above.
*/
if (!Consume (TOK_SCONST, "String literal expected")) {
return;
}
} else {
/* No message. */
if (failed) {
Error ("static_assert failed");
}
}
/* Closing paren and semi-colon needed */

View File

@ -27,6 +27,7 @@
#include <assert.h>
_Static_assert (1, "1 should be true.");
_Static_assert (1); /* Support C2x syntax with no message. */
_Static_assert (!0, "!0 should be true.");
_Static_assert (1 == 1, "1 == 1 should be true.");
_Static_assert (1 == 1L, "1 == 1L should be true.");
@ -46,6 +47,7 @@ _Static_assert (k == 1, "k should be 1.");
/* Just test the macro version once. */
static_assert (1, "1 should be true.");
static_assert (1);
/* _Static_assert can appear anywhere a declaration can. */
void f (void)