diff options
author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2020-05-21 18:28:38 +0200 |
---|---|---|
committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2020-05-21 18:28:38 +0200 |
commit | f0fe1cd9e4fe2e937f452561b62c4ab9d0989ece (patch) | |
tree | 66ffdd8d46612d2ed98149b19f090ff53c5d4aed | |
parent | 4d96f72782e076f7c4410394364aa77336c09bd2 (diff) | |
parent | 537e3e2daebd37d69447e65535fc94e82b38fc18 (diff) | |
download | sparse-f0fe1cd9e4fe2e937f452561b62c4ab9d0989ece.tar.gz |
Merge branch 'univ'
* conditionally accept { 0 } without warnings
-rw-r--r-- | lib.c | 2 | ||||
-rw-r--r-- | lib.h | 1 | ||||
-rw-r--r-- | parse.c | 7 | ||||
-rw-r--r-- | sparse.1 | 8 | ||||
-rw-r--r-- | token.h | 7 | ||||
-rw-r--r-- | validation/Wuniv-init-ko.c | 14 | ||||
-rw-r--r-- | validation/Wuniv-init-ok.c | 11 |
7 files changed, 50 insertions, 0 deletions
@@ -295,6 +295,7 @@ int Wtransparent_union = 0; int Wtypesign = 0; int Wundef = 0; int Wuninitialized = 1; +int Wuniversal_initializer = 1; int Wunknown_attribute = 0; int Wvla = 1; @@ -782,6 +783,7 @@ static const struct flag warnings[] = { { "typesign", &Wtypesign }, { "undef", &Wundef }, { "uninitialized", &Wuninitialized }, + { "universal-initializer", &Wuniversal_initializer }, { "unknown-attribute", &Wunknown_attribute }, { "vla", &Wvla }, }; @@ -184,6 +184,7 @@ extern int Wtransparent_union; extern int Wtypesign; extern int Wundef; extern int Wuninitialized; +extern int Wuniversal_initializer; extern int Wunknown_attribute; extern int Wvla; @@ -2783,6 +2783,13 @@ 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) @@ -428,6 +428,14 @@ However, this behavior can lead to subtle errors. Sparse does not issue these warnings by default. . +.TP +.B \-Wuniversal\-initializer +Do not suppress warnings about 0 used to initialize a null-pointer +when using '{ 0 }' as initializer. + +Sparse issues these warnings by default. To turn them off, use +\fB\-Wno\-universal\-initializer\fR. +. .SH MISC OPTIONS .TP .B \-\-arch=\fIARCH\fR @@ -241,4 +241,11 @@ static inline int match_ident(struct token *token, struct ident *id) return token->pos.type == TOKEN_IDENT && token->ident == id; } +static inline int match_token_zero(struct token *token) +{ + if (token_type(token) != TOKEN_NUMBER) + return false; + return token->number[0] == '0' && !token->number[1]; +} + #endif diff --git a/validation/Wuniv-init-ko.c b/validation/Wuniv-init-ko.c new file mode 100644 index 00000000..315c211a --- /dev/null +++ b/validation/Wuniv-init-ko.c @@ -0,0 +1,14 @@ +struct s { + void *ptr; +}; + + +static struct s s = { 0 }; + +/* + * check-name: univ-init-ko + * + * check-error-start +Wuniv-init-ko.c:6:23: warning: Using plain integer as NULL pointer + * check-error-end + */ diff --git a/validation/Wuniv-init-ok.c b/validation/Wuniv-init-ok.c new file mode 100644 index 00000000..c3964751 --- /dev/null +++ b/validation/Wuniv-init-ok.c @@ -0,0 +1,11 @@ +struct s { + void *ptr; +}; + + +static struct s s = { 0 }; + +/* + * check-name: univ-init-ok + * check-command: sparse -Wno-universal-initializer $file + */ |