diff --git a/evaluate.c b/evaluate.c index 1533730..815e7e1 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2090,7 +2090,8 @@ static struct symbol *evaluate_sizeof(struct expression *expr) size = type->bit_size; if (size < 0 && is_void_type(type)) { - warning(expr->pos, "expression using sizeof(void)"); + if (Wpointer_arith) + warning(expr->pos, "expression using sizeof(void)"); size = bits_in_char; } @@ -2101,7 +2102,8 @@ static struct symbol *evaluate_sizeof(struct expression *expr) } if (is_function(type->ctype.base_type)) { - warning(expr->pos, "expression using sizeof on a function"); + if (Wpointer_arith) + warning(expr->pos, "expression using sizeof on a function"); size = bits_in_char; } diff --git a/lib.c b/lib.c index 69b5ab8..ed4a74f 100644 --- a/lib.c +++ b/lib.c @@ -234,6 +234,7 @@ int Wnon_pointer_null = 1; int Wold_initializer = 1; int Wone_bit_signed_bitfield = 1; int Wparen_string = 0; +int Wpointer_arith = 0; int Wptr_subtraction_blows = 0; int Wreturn_void = 0; int Wshadow = 0; @@ -453,6 +454,7 @@ static const struct warning { { "return-void", &Wreturn_void }, { "shadow", &Wshadow }, { "sizeof-bool", &Wsizeof_bool }, + { "pointer-arith", &Wpointer_arith }, { "transparent-union", &Wtransparent_union }, { "typesign", &Wtypesign }, { "undef", &Wundef }, diff --git a/lib.h b/lib.h index 0838342..a86615b 100644 --- a/lib.h +++ b/lib.h @@ -120,6 +120,7 @@ extern int Wnon_pointer_null; extern int Wold_initializer; extern int Wone_bit_signed_bitfield; extern int Wparen_string; +extern int Wpointer_arith; extern int Wptr_subtraction_blows; extern int Wreturn_void; extern int Wshadow; diff --git a/sparse.1 b/sparse.1 index acdce53..53eff87 100644 --- a/sparse.1 +++ b/sparse.1 @@ -265,6 +265,19 @@ initializer. GCC allows this syntax as an extension. With Sparse does not issue these warnings by default. . .TP +.B \-Wpointer\-arith +Warn about anything that depends on the \fBsizeof\fR a void or function type. + +C99 does not allow the \fBsizeof\fR operator to be applied to function types +or to incomplete types such as void. GCC allows \fBsizeof\fR to be applied to +these types as an extension and assigns these types a size of \fI1\fR. With +\fB\-pointer\-arith\fR, Sparse will warn about pointer arithmetic on void +or function pointers, as well as expressions which directly apply the +\fBsizeof\fR operator to void or function types. + +Sparse does not issue these warnings by default. +. +.TP .B \-Wptr\-subtraction\-blows Warn when subtracting two pointers to a type with a non-power-of-two size.