Skip to content

Commit 0b8fbbe

Browse files
committed
Further work towards GCC 5 support
1 parent 4376e71 commit 0b8fbbe

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

gcc-python-tree.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,9 @@ PyGccType_get_sizeof(struct PyGccTree *self, void *closure)
491491
0);
492492
PyObject *str;
493493

494-
/* Did TREE_INT_CST go away in 5.0? (wide-int?) */
495-
496494
/* This gives us either an INTEGER_CST or the dummy error type: */
497495
if (INTEGER_CST == TREE_CODE(t_sizeof)) {
498-
return PyGcc_int_from_double_int(TREE_INT_CST(t_sizeof),
499-
1);
496+
return PyGcc_int_from_int_cst (t_sizeof);
500497
}
501498

502499
/* Error handling: */
@@ -668,23 +665,39 @@ PyGccConstructor_get_elements(PyObject *self, void *closure)
668665
return NULL;
669666
}
670667

668+
PyObject *
669+
PyGcc_int_from_int_cst(tree int_cst)
670+
{
671+
tree type = TREE_TYPE(int_cst);
672+
#if (GCC_VERSION >= 5000)
673+
char buf[WIDE_INT_PRINT_BUFFER_SIZE];
674+
print_dec(int_cst, buf, TYPE_SIGN (type));
675+
return PyGcc_int_from_decimal_string_buffer(buf);
676+
#else
677+
return PyGcc_int_from_double_int(TREE_INT_CST(int_cst),
678+
TYPE_UNSIGNED(type));
679+
#endif
680+
}
681+
671682
PyObject *
672683
PyGccIntegerConstant_get_constant(struct PyGccTree * self, void *closure)
673684
{
674-
tree type = TREE_TYPE(self->t.inner);
675-
return PyGcc_int_from_double_int(TREE_INT_CST(self->t.inner),
676-
TYPE_UNSIGNED(type));
685+
return PyGcc_int_from_int_cst(self->t.inner);
677686
}
678687

679688
PyObject *
680689
PyGccIntegerConstant_repr(struct PyGccTree * self)
681690
{
682691
tree type = TREE_TYPE(self->t.inner);
692+
#if (GCC_VERSION >= 5000)
693+
char buf[WIDE_INT_PRINT_BUFFER_SIZE];
694+
print_dec(self->t.inner, buf, TYPE_SIGN (type));
695+
#else
683696
char buf[512];
684-
685697
PyGcc_DoubleIntAsText(TREE_INT_CST(self->t.inner),
686698
TYPE_UNSIGNED(type),
687699
buf, sizeof(buf));
700+
#endif
688701
return PyGccString_FromFormat("%s(%s)",
689702
Py_TYPE(self)->tp_name,
690703
buf);

gcc-python.c

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
Copyright 2011, 2012, 2013 David Malcolm <dmalcolm@redhat.com>
3-
Copyright 2011, 2012, 2013 Red Hat, Inc.
2+
Copyright 2011-2013, 2015 David Malcolm <dmalcolm@redhat.com>
3+
Copyright 2011-2013, 2015 Red Hat, Inc.
44
55
This is free software: you can redistribute it and/or modify it
66
under the terms of the GNU General Public License as published by
@@ -854,6 +854,38 @@ PyGccStringOrNone(const char *str_or_null)
854854
}
855855
}
856856

857+
PyObject *
858+
PyGcc_int_from_decimal_string_buffer(const char *buf)
859+
{
860+
PyObject *long_obj;
861+
#if PY_MAJOR_VERSION < 3
862+
long long_val;
863+
int overflow;
864+
#endif
865+
long_obj = PyLong_FromString((char *)buf, NULL, 10);
866+
if (!long_obj) {
867+
return NULL;
868+
}
869+
#if PY_MAJOR_VERSION >= 3
870+
return long_obj;
871+
#else
872+
long_val = PyLong_AsLongAndOverflow(long_obj, &overflow);
873+
if (overflow) {
874+
/* Doesn't fit in a PyIntObject; use the PyLongObject: */
875+
return long_obj;
876+
} else {
877+
/* Fits in a PyIntObject: use that */
878+
PyObject *int_obj = PyInt_FromLong(long_val);
879+
if (!int_obj) {
880+
return long_obj;
881+
}
882+
Py_DECREF(long_obj);
883+
return int_obj;
884+
}
885+
#endif
886+
}
887+
888+
#if (GCC_VERSION < 5000)
857889
/*
858890
"double_int" is declared in gcc/double-int.h as a pair of HOST_WIDE_INT.
859891
These in turn are defined in gcc/hwint.h as a #define to one of "long",
@@ -882,37 +914,13 @@ PyGcc_DoubleIntAsText(double_int di, bool is_unsigned,
882914
PyObject *
883915
PyGcc_int_from_double_int(double_int di, bool is_unsigned)
884916
{
885-
PyObject *long_obj;
886-
#if PY_MAJOR_VERSION < 3
887-
long long_val;
888-
int overflow;
889-
#endif
890917
char buf[512]; /* FIXME */
891918
PyGcc_DoubleIntAsText(di, is_unsigned, buf, sizeof(buf));
892-
893-
long_obj = PyLong_FromString(buf, NULL, 10);
894-
if (!long_obj) {
895-
return NULL;
896-
}
897-
#if PY_MAJOR_VERSION >= 3
898-
return long_obj;
899-
#else
900-
long_val = PyLong_AsLongAndOverflow(long_obj, &overflow);
901-
if (overflow) {
902-
/* Doesn't fit in a PyIntObject; use the PyLongObject: */
903-
return long_obj;
904-
} else {
905-
/* Fits in a PyIntObject: use that */
906-
PyObject *int_obj = PyInt_FromLong(long_val);
907-
if (!int_obj) {
908-
return long_obj;
909-
}
910-
Py_DECREF(long_obj);
911-
return int_obj;
912-
}
913-
#endif
919+
return PyGcc_int_from_decimal_string_buffer(buf);
914920
}
915921

922+
#endif /* #if (GCC_VERSION < 5000) */
923+
916924
/*
917925
GCC's headers "poison" strdup to make it unavailable, so we provide our own.
918926

gcc-python.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ PyObject *
317317
VEC_tree_as_PyList(VEC(tree,gc) *vec_nodes);
318318
#endif
319319

320+
PyObject *
321+
PyGcc_int_from_int_cst(tree int_cst);
322+
320323
PyObject *
321324
PyGcc_int_from_decimal_string_buffer(const char *buf);
322325

0 commit comments

Comments
 (0)