mirror of
https://github.com/cc65/cc65.git
synced 2025-08-08 06:25:17 +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:
committed by
Oliver Schmidt
parent
c4698dfd07
commit
1cf9404c19
@@ -49,6 +49,7 @@ void ParseStaticAssert ()
|
|||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
** static_assert-declaration ::=
|
** static_assert-declaration ::=
|
||||||
|
** _Static_assert ( constant-expression ) ;
|
||||||
** _Static_assert ( constant-expression , string-literal ) ;
|
** _Static_assert ( constant-expression , string-literal ) ;
|
||||||
*/
|
*/
|
||||||
ExprDesc Expr;
|
ExprDesc Expr;
|
||||||
@@ -67,27 +68,35 @@ void ParseStaticAssert ()
|
|||||||
ConstAbsIntExpr (hie1, &Expr);
|
ConstAbsIntExpr (hie1, &Expr);
|
||||||
failed = !Expr.IVal;
|
failed = !Expr.IVal;
|
||||||
|
|
||||||
/* We expect a comma */
|
/* If there is a comma, we also have an error message. The message is optional because we
|
||||||
if (!ConsumeComma ()) {
|
** support the C2X syntax with only an expression.
|
||||||
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 (!Consume (TOK_SCONST, "String literal expected")) {
|
if (CurTok.Tok == TOK_COMMA) {
|
||||||
return;
|
/* 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 */
|
/* Closing paren and semi-colon needed */
|
||||||
|
@@ -27,6 +27,7 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
_Static_assert (1, "1 should be true.");
|
_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 (!0, "!0 should be true.");
|
||||||
_Static_assert (1 == 1, "1 == 1 should be true.");
|
_Static_assert (1 == 1, "1 == 1 should be true.");
|
||||||
_Static_assert (1 == 1L, "1 == 1L 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. */
|
/* Just test the macro version once. */
|
||||||
static_assert (1, "1 should be true.");
|
static_assert (1, "1 should be true.");
|
||||||
|
static_assert (1);
|
||||||
|
|
||||||
/* _Static_assert can appear anywhere a declaration can. */
|
/* _Static_assert can appear anywhere a declaration can. */
|
||||||
void f (void)
|
void f (void)
|
||||||
|
Reference in New Issue
Block a user