@@ -2169,7 +2169,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;
}
@@ -2180,7 +2181,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;
}
@@ -241,6 +241,7 @@ int Woverride_init = 1;
int Woverride_init_all = 0;
int Woverride_init_whole_range = 0;
int Wparen_string = 0;
+int Wpointer_arith = 0;
int Wptr_subtraction_blows = 0;
int Wreturn_void = 0;
int Wshadow = 0;
@@ -536,6 +537,7 @@ static const struct warning {
{ "return-void", &Wreturn_void },
{ "shadow", &Wshadow },
{ "sizeof-bool", &Wsizeof_bool },
+ { "pointer-arith", &Wpointer_arith },
{ "sparse-error", &Wsparse_error },
{ "tautological-compare", &Wtautological_compare },
{ "transparent-union", &Wtransparent_union },
@@ -134,6 +134,7 @@ extern int Woverride_init;
extern int Woverride_init_all;
extern int Woverride_init_whole_range;
extern int Wparen_string;
+extern int Wpointer_arith;
extern int Wptr_subtraction_blows;
extern int Wreturn_void;
extern int Wshadow;
@@ -282,6 +282,19 @@ Standard C syntax does not permit a parenthesized string as an array
initializer. GCC allows this syntax as an extension. With
\fB\-Wparen\-string\fR, Sparse will warn about this syntax.
+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
new file mode 100644
@@ -0,0 +1,44 @@
+#define is_constexpr(x) \
+ (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+
+static int test(void)
+{
+ unsigned int s = 0, i = 0;
+ void *ptr = &i;
+
+ // OK
+ s += sizeof i;
+ s += sizeof &i;
+ s += sizeof ptr;
+ s += sizeof &ptr;
+
+ // KO
+ s += sizeof(void);
+ s += sizeof *ptr;
+ s += is_constexpr(ptr++);
+ s += is_constexpr((i++, 1));
+ s += is_constexpr(sizeof *ptr);
+ s += is_constexpr(ptr + 1);
+ s += is_constexpr(&ptr + 1);
+ s += is_constexpr(*(((char *)&ptr) + 1));
+
+ return s;
+}
+
+/*
+ * check-name: sizeof(void) is valid
+ * check-description: sizeof(void) was rejected because void is an incomplete
+ * type
+ * check-command: sparse -Wpointer-arith $file
+ *
+ * check-error-start
+sizeof-void.c:16:14: warning: expression using sizeof(void)
+sizeof-void.c:17:14: warning: expression using sizeof(void)
+sizeof-void.c:18:14: warning: expression using sizeof(void)
+sizeof-void.c:19:14: warning: expression using sizeof(void)
+sizeof-void.c:20:14: warning: expression using sizeof(void)
+sizeof-void.c:21:14: warning: expression using sizeof(void)
+sizeof-void.c:22:14: warning: expression using sizeof(void)
+sizeof-void.c:23:14: warning: expression using sizeof(void)
+ * check-error-end
+ */