Skip to content

Commit

Permalink
Avoid warnings about casting pointers to int
Browse files Browse the repository at this point in the history
intptr_t is defined to be an integer type large enough to hold any
pointer value, so that's what we should cast to if it is available.
These specific values are just alignments, though, so it's fine for
those intptr_t values to subsequently be coerced to int.  That also
simplifies printing, since it means we don't need wacky macros like
PRIdPTR to build printf format strings.
  • Loading branch information
liblit committed Sep 20, 2015
1 parent d9d6a08 commit 7e1795b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
7 changes: 7 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* config.h.in. Generated from configure.ac by autoheader. */

/* Define to 1 if the system has the type `intptr_t'. */
#undef HAVE_INTPTR_T

/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H

Expand Down Expand Up @@ -86,3 +89,7 @@
#ifndef __cplusplus
#undef inline
#endif

/* Define to the type of a signed integer type wide enough to hold a pointer,
if such a type exists, and if the system does not define it. */
#undef intptr_t
36 changes: 36 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -5557,6 +5557,42 @@ $as_echo "#define TIME_WITH_SYS_TIME 1" >>confdefs.h
fi
ac_fn_c_check_type "$LINENO" "intptr_t" "ac_cv_type_intptr_t" "$ac_includes_default"
if test "x$ac_cv_type_intptr_t" = xyes; then :
$as_echo "#define HAVE_INTPTR_T 1" >>confdefs.h
else
for ac_type in 'int' 'long int' 'long long int'; do
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
$ac_includes_default
int
main ()
{
static int test_array [1 - 2 * !(sizeof (void *) <= sizeof ($ac_type))];
test_array [0] = 0;
return test_array [0];
;
return 0;
}
_ACEOF
if ac_fn_c_try_compile "$LINENO"; then :
cat >>confdefs.h <<_ACEOF
#define intptr_t $ac_type
_ACEOF
ac_type=
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
test -z "$ac_type" && break
done
fi
# checks for library functions; more autoscan stuff
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for working memcmp" >&5
$as_echo_n "checking for working memcmp... " >&6; }
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ AC_CHECK_HEADERS(stdlib.h strings.h sys/time.h unistd.h wchar.h stdbool.h)
AC_C_CONST
AC_C_INLINE
AC_HEADER_TIME
AC_TYPE_INTPTR_T

# checks for library functions; more autoscan stuff
AC_FUNC_MEMCMP
Expand Down
28 changes: 18 additions & 10 deletions src/machdep-ml.c.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
typedef int bool;
#endif

#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#define THREAD_IS_KEYWORD "@THREAD_IS_KEYWORD@"
#define HAVE_BUILTIN_VA_LIST "@HAVE_BUILTIN_VA_LIST@"

Expand Down Expand Up @@ -103,7 +111,7 @@ int main(int argc, char **argv)
char c;
short s;
};
alignof_short = (int)(&((struct shortstruct*)0)->s);
alignof_short = (intptr_t)(&((struct shortstruct*)0)->s);
}

// The alignment of an int
Expand All @@ -112,7 +120,7 @@ int main(int argc, char **argv)
char c;
int i;
};
alignof_int = (int)(&((struct intstruct*)0)->i);
alignof_int = (intptr_t)(&((struct intstruct*)0)->i);
}

// The alignment of a bool
Expand All @@ -121,7 +129,7 @@ int main(int argc, char **argv)
char c;
bool b;
};
alignof_bool = (int)(&((struct boolstruct*)0)->b);
alignof_bool = (intptr_t)(&((struct boolstruct*)0)->b);
}

// The alignment of a long
Expand All @@ -130,7 +138,7 @@ int main(int argc, char **argv)
char c;
long l;
};
alignof_long = (int)(&((struct longstruct*)0)->l);
alignof_long = (intptr_t)(&((struct longstruct*)0)->l);
}

// The alignment of long long
Expand All @@ -139,7 +147,7 @@ int main(int argc, char **argv)
char c;
LONGLONG ll;
};
alignof_longlong = (int)(&((struct longlong*)0)->ll);
alignof_longlong = (intptr_t)(&((struct longlong*)0)->ll);
}

// The alignment of a ptr
Expand All @@ -148,7 +156,7 @@ int main(int argc, char **argv)
char c;
int * p;
};
alignof_ptr = (int)(&((struct ptrstruct*)0)->p);
alignof_ptr = (intptr_t)(&((struct ptrstruct*)0)->p);
}

// The alignment of an enum
Expand All @@ -157,7 +165,7 @@ int main(int argc, char **argv)
char c;
enum e2 { THREE, FOUR, FIVE } e;
};
alignof_enum = (int)(&((struct enumstruct*)0)->e);
alignof_enum = (intptr_t)(&((struct enumstruct*)0)->e);
}

// The alignment of a float
Expand All @@ -166,7 +174,7 @@ int main(int argc, char **argv)
char c;
float f;
};
alignof_float = (int)(&((struct floatstruct*)0)->f);
alignof_float = (intptr_t)(&((struct floatstruct*)0)->f);
}

// The alignment of double
Expand All @@ -175,7 +183,7 @@ int main(int argc, char **argv)
char c;
double d;
};
alignof_double = (int)(&((struct s1*)0)->d);
alignof_double = (intptr_t)(&((struct s1*)0)->d);
}

// The alignment of long double
Expand All @@ -184,7 +192,7 @@ int main(int argc, char **argv)
char c;
long double ld;
};
alignof_longdouble = (int)(&((struct s1*)0)->ld);
alignof_longdouble = (intptr_t)(&((struct s1*)0)->ld);
}

alignof_str = __alignof("a string");
Expand Down

0 comments on commit 7e1795b

Please sign in to comment.