Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gccutils: get pretty-printing working again #176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/versions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ On my machine, running this currently gives::
4.8 4008
4.9 4009
=========== ========================

.. py:function:: gcc.is_cplusplus()
:rtype: bool

Determine whether or not we are compiling C++
8 changes: 8 additions & 0 deletions gcc-python-pretty-printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ PyGccPrettyPrinter_as_string(PyObject *obj)
ppobj = (struct PyGccPrettyPrinter *)obj;

/* Flush the pp first. This forcibly adds a trailing newline: */
#if (GCC_VERSION < 5003)
pp_flush(&ppobj->pp);
#else
/*
* pp_newline_and_flush provides the same functionality on GCC 5.3
* and later
*/
pp_newline_and_flush(&ppobj->pp);
#endif

/* Convert to a python string, leaving off the trailing newline: */
len = strlen(ppobj->buf);
Expand Down
13 changes: 0 additions & 13 deletions gcc-python-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,6 @@ extern PyGccWrapperTypeObject PyGccIntegerCst_TypeObj;

__typeof__ (lang_check_failed) lang_check_failed __attribute__ ((weak));


/*
Unfortunately, decl_as_string() is only available from the C++
frontend: cc1plus (it's defined in gcc/cp/error.c).

See http://gcc.gnu.org/ml/gcc/2011-11/msg00504.html

Hence we redeclare the symbol as weak, and then check its definition
against 0 before using it.
*/

__typeof__ (decl_as_string) decl_as_string __attribute__ ((weak));

/* Similar for namespace_binding, though gcc 8's r247654 (aka
f906dcc33dd818b71e16c88cef38f33c161070db) replaced it with
get_namespace_value and reversed the order of the params
Expand Down
9 changes: 9 additions & 0 deletions gcc-python.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ PyGcc_get_is_lto(PyObject *self, PyObject *noargs)
return PyBool_FromLong(in_lto_p);
}

static PyObject *
PyGcc_get_is_cplusplus(PyObject *self, PyObject *noargs)
{
return PyBool_FromLong(decl_as_string != NULL);
}

static PyMethodDef GccMethods[] = {
{"register_attribute",
(PyCFunction)PyGcc_RegisterAttribute,
Expand Down Expand Up @@ -491,6 +497,9 @@ static PyMethodDef GccMethods[] = {
{"is_lto", PyGcc_get_is_lto, METH_NOARGS,
"Determine whether or not we're being invoked during link-time optimization"},

{"is_cplusplus", PyGcc_get_is_cplusplus, METH_NOARGS,
"Determine whether or not we're compiling C++"},

/* Garbage collection */
{"_force_garbage_collection", PyGcc__force_garbage_collection, METH_NOARGS,
"Forcibly trigger a single run of GCC's garbage collector"},
Expand Down
13 changes: 13 additions & 0 deletions gcc-python.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "params.h"
#endif
#include "gcc-c-api/gcc-cfg.h"
#include "cp/cp-tree.h" /* for decl_as_string */

/* GCC doesn't seem to give us an ID for "invalid event", so invent one: */
#define GCC_PYTHON_PLUGIN_BAD_EVENT (0xffff)
Expand Down Expand Up @@ -69,6 +70,18 @@
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(typename)
#endif

/*
Unfortunately, decl_as_string() is only available from the C++
frontend: cc1plus (it's defined in gcc/cp/error.c).

See http://gcc.gnu.org/ml/gcc/2011-11/msg00504.html

Hence we redeclare the symbol as weak, and then check its definition
against 0 before using it.
*/

__typeof__ (decl_as_string) decl_as_string __attribute__ ((weak));

/*
PyObject shared header for wrapping GCC objects, for integration
with GCC's garbage collector (so that things we wrap don't get collected
Expand Down
13 changes: 12 additions & 1 deletion gccutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,18 @@ def iter_tree_attrs(self, obj):
# Ignore private and "magic" attributes:
if name.startswith('_'):
continue
value = getattr(obj, name)
if (isinstance(obj, gcc.FunctionDecl)
and name == 'fullname'
and not gcc.is_cplusplus()):
continue
try:
value = getattr(obj, name)
except TypeError as e:
# for gcc.Type, sizeof is not always defined, and
# TypeError is raise under this condition
if not isinstance(obj, gcc.Type) or name != 'sizeof':
raise e
continue
# Ignore methods:
if hasattr(value, '__call__'):
continue
Expand Down