C: Drop qualifiers during lvalue conversion [PR97702]

2020-11-20  Martin Uecker  <muecker@gwdg.de>

gcc/
	* gimplify.c (gimplify_modify_expr_rhs): Optimizie
	NOP_EXPRs that contain compound literals.

gcc/c/
	* c-typeck.c (convert_lvalue_to_rvalue): Drop qualifiers.

gcc/testsuite/
	* gcc.dg/cond-constqual-1.c: Adapt test.
	* gcc.dg/lvalue-11.c: New test.
	* gcc.dg/pr60195.c: Add warning.
This commit is contained in:
Martin Uecker 2020-11-20 07:21:40 +01:00
parent d62586ee56
commit 32934a4f45
5 changed files with 58 additions and 2 deletions

View file

@ -2080,6 +2080,9 @@ convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
exp = default_function_array_conversion (loc, exp);
if (!VOID_TYPE_P (TREE_TYPE (exp.value)))
exp.value = require_complete_type (loc, exp.value);
if (convert_p && !error_operand_p (exp.value)
&& (TREE_CODE (TREE_TYPE (exp.value)) != ARRAY_TYPE))
exp.value = convert (build_qualified_type (TREE_TYPE (exp.value), TYPE_UNQUALIFIED), exp.value);
if (really_atomic_lvalue (exp.value))
{
vec<tree, va_gc> *params;

View file

@ -5518,6 +5518,19 @@ gimplify_modify_expr_rhs (tree *expr_p, tree *from_p, tree *to_p,
return GS_OK;
}
case NOP_EXPR:
/* Pull out compound literal expressions from a NOP_EXPR.
Those are created in the C FE to drop qualifiers during
lvalue conversion. */
if ((TREE_CODE (TREE_OPERAND (*from_p, 0)) == COMPOUND_LITERAL_EXPR)
&& tree_ssa_useless_type_conversion (*from_p))
{
*from_p = TREE_OPERAND (*from_p, 0);
ret = GS_OK;
changed = true;
}
break;
case COMPOUND_LITERAL_EXPR:
{
tree complit = TREE_OPERAND (*expr_p, 1);

View file

@ -11,5 +11,5 @@ test (void)
__typeof__ (1 ? foo (0) : 0) texpr;
__typeof__ (1 ? i : 0) texpr2;
texpr = 0; /* { dg-bogus "read-only variable" "conditional expression with call to const function" } */
texpr2 = 0; /* { dg-error "read-only variable" "conditional expression with const variable" } */
texpr2 = 0; /* { dg-bogus "read-only variable" "conditional expression with const variable" } */
}

View file

@ -0,0 +1,40 @@
/* test that lvalue conversions drops qualifiers, Bug 97702 */
/* { dg-do compile } */
/* { dg-options "" } */
const int jc;
extern int j;
extern typeof(0,jc) j;
extern typeof(+jc) j;
extern typeof(-jc) j;
extern typeof(1?jc:0) j;
extern typeof((int)jc) j;
extern typeof((const int)jc) j;
volatile int kv;
extern int k;
extern typeof(0,kv) k;
extern typeof(+kv) k;
extern typeof(-kv) k;
extern typeof(1?kv:0) k;
extern typeof((int)kv) k;
extern typeof((volatile int)kv) k;
_Atomic int la;
extern int l;
extern typeof(0,la) l;
extern typeof(+la) l;
extern typeof(-la) l;
extern typeof(1?la:0) l;
extern typeof((int)la) l;
extern typeof((_Atomic int)la) l;
int * restrict mr;
extern int *m;
extern typeof(0,mr) m;
extern typeof(1?mr:0) m;
extern typeof((int *)mr) m;
extern typeof((int * restrict)mr) m;

View file

@ -15,7 +15,7 @@ atomic_int
fn2 (void)
{
atomic_int y = 0;
y;
y; /* { dg-warning "statement with no effect" } */
return y;
}