@@ -2841,6 +2841,8 @@ static struct symbol *evaluate_cast(struct expression *expr)
addr->ctype = &lazy_ptr_ctype; /* Lazy eval */
addr->symbol = sym;
+ if (sym->ctype.modifiers & MOD_TOPLEVEL)
+ addr->flags |= CEF_ADDR;
expr->type = EXPR_PREOP;
expr->op = '*';
@@ -711,6 +711,8 @@ static struct token *cast_expression(struct token *token, struct expression **tr
cast->cast_type = sym;
token = expect(token, ')', "at end of cast operator");
if (match_op(token, '{')) {
+ if (toplevel(block_scope))
+ sym->ctype.modifiers |= MOD_TOPLEVEL;
if (is_force)
warning(sym->pos,
"[force] in compound literal");
new file mode 100644
@@ -0,0 +1,19 @@
+static int *a = &(int){ 1 }; // OK
+static int *b = &(int){ *a }; // KO
+
+static void foo(void)
+{
+ int *b = &(int){ 1 }; // OK
+ int *c = &(int){ *a }; // OK
+ static int *d = &(int){ 1 }; // KO
+}
+
+/*
+ * check-name: compound literal address constness verification
+ * check-command: sparse -Wconstexpr-not-const $file
+ *
+ * check-error-start
+constexpr-compound-literal.c:2:25: warning: non-constant initializer for static object
+constexpr-compound-literal.c:8:27: warning: non-constant initializer for static object
+ * check-error-end
+ */