From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH] c2x: message in _Static_assert() is now optional
Date: Wed, 8 Jul 2020 01:49:22 +0200 [thread overview]
Message-ID: <20200707234922.73866-1-luc.vanoostenryck@gmail.com> (raw)
It seems that in the next version of the standard, the
second argument of _Static_assert() will be optional.
Nice. Let sparse already support this now.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
parse.c | 24 ++++++++++++++++--------
validation/static_assert.c | 5 +++++
2 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/parse.c b/parse.c
index a9222e7cbf08..bd8989ca8dd4 100644
--- a/parse.c
+++ b/parse.c
@@ -2223,17 +2223,25 @@ static struct token *parse_static_assert(struct token *token, struct symbol_list
token = constant_expression(token, &cond);
if (!cond)
sparse_error(token->pos, "Expected constant expression");
- token = expect(token, ',', "after conditional expression in _Static_assert");
- token = string_expression(token, &message, "_Static_assert()");
- if (!message)
- cond = NULL;
+ if (match_op(token, ',')) {
+ token = token->next;
+ token = string_expression(token, &message, "_Static_assert()");
+ if (!message)
+ cond = NULL;
+ }
token = expect(token, ')', "after diagnostic message in _Static_assert");
-
token = expect(token, ';', "after _Static_assert()");
- if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE)
- sparse_error(cond->pos, "static assertion failed: %s",
- show_string(message->string));
+ if (cond && !const_expression_value(cond) && cond->type == EXPR_VALUE) {
+ const char *sep = "", *msg = "";
+
+ if (message) {
+ sep = ": ";
+ msg = show_string(message->string);
+ }
+ sparse_error(cond->pos, "static assertion failed%s%s", sep, msg);
+ }
+
return token;
}
diff --git a/validation/static_assert.c b/validation/static_assert.c
index dd5e0c08c044..0ab5844edef1 100644
--- a/validation/static_assert.c
+++ b/validation/static_assert.c
@@ -53,6 +53,10 @@ _Static_assert(1, );
_Static_assert(, "");
_Static_assert(,);
+// C2x's version: without message
+_Static_assert(1);
+_Static_assert(0);
+
/*
* check-name: static assertion
*
@@ -67,5 +71,6 @@ static_assert.c:52:19: error: string literal expected for _Static_assert()
static_assert.c:53:16: error: Expected constant expression
static_assert.c:54:16: error: Expected constant expression
static_assert.c:54:17: error: string literal expected for _Static_assert()
+static_assert.c:58:16: error: static assertion failed
* check-error-end
*/
--
2.27.0
reply other threads:[~2020-07-07 23:49 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200707234922.73866-1-luc.vanoostenryck@gmail.com \
--to=luc.vanoostenryck@gmail.com \
--cc=linux-sparse@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).