diff options
| author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2017-08-15 10:09:24 +0200 |
|---|---|---|
| committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2018-06-23 07:46:40 +0200 |
| commit | 756b6133e7f6605d86d0d74fd939991b11aeb1e4 (patch) | |
| tree | bfd46e192f1a6091b4aa846f96022a377811bebd | |
| parent | c8fac68af94b9d23b64e844a283f16b9bed2f9e0 (diff) | |
| download | sparse-dev-756b6133e7f6605d86d0d74fd939991b11aeb1e4.tar.gz | |
cast: add support for -Wpointer-to-int-cast
It's relatively common to cast a pointer to an unsigned long,
for example to make some bit operations.
It's much less sensical to cast a pointer to an integer smaller
(or bigger) than a pointer is.
So, emit a diagnostic for this, under the control of a new
warning flag: -Wpointer-to-int-cast.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
| -rw-r--r-- | lib.c | 2 | ||||
| -rw-r--r-- | lib.h | 1 | ||||
| -rw-r--r-- | linearize.c | 5 | ||||
| -rw-r--r-- | validation/cast-kinds-check.c | 2 | ||||
| -rw-r--r-- | validation/linear/cast-kinds.c | 2 |
5 files changed, 10 insertions, 2 deletions
@@ -270,6 +270,7 @@ int Woverride_init_all = 0; int Woverride_init_whole_range = 0; int Wparen_string = 0; int Wpointer_arith = 0; +int Wpointer_to_int_cast = 1; int Wptr_subtraction_blows = 0; int Wreturn_void = 0; int Wshadow = 0; @@ -696,6 +697,7 @@ static const struct flag warnings[] = { { "override-init", &Woverride_init }, { "override-init-all", &Woverride_init_all }, { "paren-string", &Wparen_string }, + { "pointer-to-int-cast", &Wpointer_to_int_cast }, { "ptr-subtraction-blows", &Wptr_subtraction_blows }, { "return-void", &Wreturn_void }, { "shadow", &Wshadow }, @@ -159,6 +159,7 @@ extern int Woverride_init_all; extern int Woverride_init_whole_range; extern int Wparen_string; extern int Wpointer_arith; +extern int Wpointer_to_int_cast; extern int Wptr_subtraction_blows; extern int Wreturn_void; extern int Wshadow; diff --git a/linearize.c b/linearize.c index de6e4b93..8cfcec5f 100644 --- a/linearize.c +++ b/linearize.c @@ -1294,6 +1294,11 @@ static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol * src = cast_pseudo(ep, src, from, size_t_ctype); from = size_t_ctype; break; + case OP_PTRTU: + if (from->bit_size == to->bit_size) + break; + if (Wpointer_to_int_cast) + warning(to->pos, "non size-preserving pointer to integer cast"); default: break; } diff --git a/validation/cast-kinds-check.c b/validation/cast-kinds-check.c index 736ab391..e22b6136 100644 --- a/validation/cast-kinds-check.c +++ b/validation/cast-kinds-check.c @@ -2,7 +2,7 @@ /* * check-name: cast-kinds check - * check-command: sparse -m64 -v $file + * check-command: sparse -m64 -v -Wno-pointer-to-int-cast $file * * check-error-start linear/cast-kinds.c:5:45: warning: cast drops bits diff --git a/validation/linear/cast-kinds.c b/validation/linear/cast-kinds.c index 6683ea93..fb16d335 100644 --- a/validation/linear/cast-kinds.c +++ b/validation/linear/cast-kinds.c @@ -55,7 +55,7 @@ static double double_2_double(double a) { return a; } /* * check-name: cast-kinds - * check-command: test-linearize -Wno-int-to-pointer-cast -m64 $file + * check-command: test-linearize -Wno-int-to-pointer-cast -Wno-pointer-to-int-cast -m64 $file * * check-output-start uint_2_int: |
