aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
-rw-r--r--evaluate.c3
-rw-r--r--expression.h1
-rw-r--r--parse.c17
-rw-r--r--validation/Wuniv-init-ko.c17
-rw-r--r--validation/Wuniv-init-ok.c18
5 files changed, 49 insertions, 7 deletions
diff --git a/evaluate.c b/evaluate.c
index 8d2e6869..bc69a1fa 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -2608,6 +2608,9 @@ static void handle_list_initializer(struct expression *expr,
struct expression *e, *last = NULL, *top = NULL, *next;
int jumped = 0;
+ if (expr->zero_init)
+ free_ptr_list(&expr->expr_list);
+
FOR_EACH_PTR(expr->expr_list, e) {
struct expression **v;
struct symbol *type;
diff --git a/expression.h b/expression.h
index 64aa1fc2..07fe8502 100644
--- a/expression.h
+++ b/expression.h
@@ -159,6 +159,7 @@ DECLARE_ALLOCATOR(type_expression);
struct expression {
enum expression_type type:8;
unsigned flags:8;
+ unsigned zero_init:1;
int op;
struct position pos;
struct symbol *ctype;
diff --git a/parse.c b/parse.c
index 687c8c0c..570b2516 100644
--- a/parse.c
+++ b/parse.c
@@ -2783,13 +2783,6 @@ static struct token *initializer_list(struct expression_list **list, struct toke
{
struct expression *expr;
- // '{ 0 }' is equivalent to '{ }' unless wanting all possible
- // warnings about using '0' to initialize a null-pointer.
- if (!Wuniversal_initializer) {
- if (match_token_zero(token) && match_op(token->next, '}'))
- token = token->next;
- }
-
for (;;) {
token = single_initializer(&expr, token);
if (!expr)
@@ -2807,6 +2800,16 @@ struct token *initializer(struct expression **tree, struct token *token)
if (match_op(token, '{')) {
struct expression *expr = alloc_expression(token->pos, EXPR_INITIALIZER);
*tree = expr;
+ if (!Wuniversal_initializer) {
+ struct token *next = token->next;
+ // '{ 0 }' is equivalent to '{ }' except for some
+ // warnings, like using 0 to initialize a null-pointer.
+ if (match_token_zero(next)) {
+ if (match_op(next->next, '}'))
+ expr->zero_init = 1;
+ }
+ }
+
token = initializer_list(&expr->expr_list, token->next);
return expect(token, '}', "at end of initializer");
}
diff --git a/validation/Wuniv-init-ko.c b/validation/Wuniv-init-ko.c
index 315c211a..1fb5669f 100644
--- a/validation/Wuniv-init-ko.c
+++ b/validation/Wuniv-init-ko.c
@@ -4,11 +4,28 @@ struct s {
static struct s s = { 0 };
+static int a = { 0 };
+static int b = { };
+static int c = { 1, 2 };
+static struct s *ptr = { 0 };
+
+struct o {
+ struct i {
+ int a;
+ };
+};
+
+static struct o o = { 0 };
/*
* check-name: univ-init-ko
+ * check-command: sparse -Wuniversal-initializer $file
*
* check-error-start
Wuniv-init-ko.c:6:23: warning: Using plain integer as NULL pointer
+Wuniv-init-ko.c:8:16: error: invalid initializer
+Wuniv-init-ko.c:9:16: error: invalid initializer
+Wuniv-init-ko.c:10:26: warning: Using plain integer as NULL pointer
+Wuniv-init-ko.c:18:23: warning: missing braces around initializer
* check-error-end
*/
diff --git a/validation/Wuniv-init-ok.c b/validation/Wuniv-init-ok.c
index c3964751..1f0c3dcb 100644
--- a/validation/Wuniv-init-ok.c
+++ b/validation/Wuniv-init-ok.c
@@ -4,8 +4,26 @@ struct s {
static struct s s = { 0 };
+static int a = { 0 };
+static int b = { };
+static int c = { 1, 2 };
+static struct s *ptr = { 0 };
+
+struct o {
+ struct i {
+ int a;
+ };
+};
+
+static struct o o = { 0 };
/*
* check-name: univ-init-ok
* check-command: sparse -Wno-universal-initializer $file
+ *
+ * check-error-start
+Wuniv-init-ok.c:8:16: error: invalid initializer
+Wuniv-init-ok.c:9:16: error: invalid initializer
+Wuniv-init-ok.c:10:26: warning: Using plain integer as NULL pointer
+ * check-error-end
*/