From: Nicolai Stange <nicstange@gmail.com>
To: linux-sparse@vger.kernel.org
Subject: [PATCH RFC 04/13] expression: examine constness of preops at evaluation only
Date: Thu, 23 Jul 2015 01:15:40 +0200 [thread overview]
Message-ID: <87egjzkcmb.fsf@gmail.com> (raw)
Currently, the propagation of expressions' constness flags through
prefix expressions is done in two steps:
- Several flags are speculatively set at expression parsing time
- and possibly cleared again at evaluation time.
Set aside this unfortunate split of code, the early propagation of
constness flags is not able to recognize constant expressions such as
-__builtin_choose_expr(0, 0, 0)
~__builtin_choose_expr(0, 0, 0)
!__builtin_choose_expr(0, 0, 0)
since the final expression to be thrown into the prefix expression is
known only after evaluation.
Move the whole calculation of prefix expressions' constness flags to
the evaluation phase.
Introduce support for tracking arithmetic constness propagation through
prefix expressions.
Signed-off-by: Nicolai Stange <nicstange@gmail.com>
---
evaluate.c | 15 ++++++++++-----
expression.c | 3 ---
validation/constexpr-preop.c | 29 +++++++++++++++++++++++++++++
3 files changed, 39 insertions(+), 8 deletions(-)
create mode 100644 validation/constexpr-preop.c
diff --git a/evaluate.c b/evaluate.c
index 637824c..e1d2f3d 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -1794,8 +1794,9 @@ static struct symbol *evaluate_sign(struct expression *expr)
{
struct symbol *ctype = expr->unop->ctype;
int class = classify_type(ctype, &ctype);
- if (expr->flags && !(expr->unop->flags & EXPR_FLAG_INT_CONST_EXPR))
- expr->flags = EXPR_FLAG_NONE;
+ enum expression_flags flags;
+
+ flags = expr->flags | expr_flags_decay_consts(expr->unop->flags);
/* should be an arithmetic type */
if (!(class & TYPE_NUM))
return bad_expr_type(expr);
@@ -1812,6 +1813,7 @@ Normal:
}
if (expr->op == '+')
*expr = *expr->unop;
+ expr->flags = flags;
expr->ctype = ctype;
return ctype;
Restr:
@@ -1849,9 +1851,12 @@ static struct symbol *evaluate_preop(struct expression *expr)
return evaluate_postop(expr);
case '!':
- if (expr->flags && !(expr->unop->flags &
- EXPR_FLAG_INT_CONST_EXPR))
- expr->flags = EXPR_FLAG_NONE;
+ expr->flags |= expr_flags_decay_consts(expr->unop->flags);
+ /*
+ * A logical negation never yields an address constant
+ * [6.6(9)].
+ */
+ expr->flags &= ~expr_clear_flag_mask(EXPR_FLAG_ADDR_CONST_EXPR);
if (is_safe_type(ctype))
warning(expr->pos, "testing a 'safe expression'");
if (is_float_type(ctype)) {
diff --git a/expression.c b/expression.c
index b52ae15..7759bd0 100644
--- a/expression.c
+++ b/expression.c
@@ -451,8 +451,6 @@ struct token *primary_expression(struct token *token, struct expression **tree)
expr = alloc_expression(token->pos, EXPR_PREOP);
expr->op = '(';
token = parens_expression(token, &expr->unop, "in expression");
- if (expr->unop)
- expr->flags = expr->unop->flags;
break;
}
if (token->special == '[' && lookup_type(token->next)) {
@@ -665,7 +663,6 @@ static struct token *unary_expression(struct token *token, struct expression **t
unary = alloc_expression(token->pos, EXPR_PREOP);
unary->op = token->special;
unary->unop = unop;
- unary->flags = expr_flags_decay_consts(unop->flags);
*tree = unary;
return next;
}
diff --git a/validation/constexpr-preop.c b/validation/constexpr-preop.c
new file mode 100644
index 0000000..5d869da
--- /dev/null
+++ b/validation/constexpr-preop.c
@@ -0,0 +1,29 @@
+static int a[] = {
+ [+0] = 0, // OK
+ [+__builtin_choose_expr(0, 0, 0)] = 0, // OK
+ [+0.] = 0, // KO
+ [+__builtin_choose_expr(0, 0, 0.)] = 0, // KO
+ [-0] = 0, // OK
+ [-__builtin_choose_expr(0, 0, 0)] = 0, // OK
+ [-0.] = 0, // KO
+ [-__builtin_choose_expr(0, 0, 0.)] = 0, // KO
+ [~0] = 0, // OK
+ [~__builtin_choose_expr(0, 0, 0)] = 0, // OK
+ [!0] = 0, // OK
+ [!__builtin_choose_expr(0, 0, 0)] = 0, // OK
+ [!0.] = 0, // KO
+ [!__builtin_choose_expr(0, 0, 0.)] = 0, // KO
+};
+
+/*
+ * check-name: Expression constness propagation in preops
+ *
+ * check-error-start
+constexpr-preop.c:4:5: error: bad constant expression
+constexpr-preop.c:5:33: error: bad constant expression
+constexpr-preop.c:8:4: error: bad constant expression
+constexpr-preop.c:9:4: error: bad constant expression
+constexpr-preop.c:14:4: error: bad integer constant expression
+constexpr-preop.c:15:4: error: bad integer constant expression
+ * check-error-end
+ */
--
2.4.5
reply other threads:[~2015-07-22 23:15 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=87egjzkcmb.fsf@gmail.com \
--to=nicstange@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).