commit 9ce10d2e0b368b1770df82eea0dba6185837d91b
Author: David Malcolm <dmalcolm(a)redhat.com>
Date: Mon May 7 19:41:34 2012 -0400
add macros IMPL_APPENDER, IMPL_LIST_MAKER, IMPL_GLOBAL_LIST_MAKER
gcc-python-callgraph.c | 94 +++-----------------
gcc-python-cfg.c | 223 ++++++++----------------------------------------
gcc-python-wrappers.h | 55 ++++++++++++
gcc-python.c | 34 +------
4 files changed, 111 insertions(+), 295 deletions(-)
---
diff --git a/gcc-python-callgraph.c b/gcc-python-callgraph.c
index 84bfe24..6a6749e 100644
--- a/gcc-python-callgraph.c
+++ b/gcc-python-callgraph.c
@@ -56,64 +56,24 @@ gcc_CallgraphNode_str(struct PyGccCallgraphNode * self)
Py_TYPE(self)->tp_name);
}
-static bool add_cgraph_edge_to_list(gcc_cgraph_edge edge, void *user_data)
-{
- PyObject *result = (PyObject*)user_data;
- PyObject *item;
-
- item = gcc_python_make_wrapper_cgraph_edge(edge);
- if (!item) {
- return true;
- }
-
- if (-1 == PyList_Append(result, item)) {
- Py_DECREF(item);
- return true;
- }
-
- /* Success: */
- Py_DECREF(item);
- return false;
-}
+IMPL_APPENDER(add_cgraph_edge_to_list,
+ gcc_cgraph_edge,
+ gcc_python_make_wrapper_cgraph_edge)
PyObject *
gcc_CallgraphNode_get_callees(struct PyGccCallgraphNode * self)
{
- PyObject *result;
-
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_cgraph_node_for_each_callee(self->node,
- add_cgraph_edge_to_list,
- result)) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ IMPL_LIST_MAKER(gcc_cgraph_node_for_each_callee,
+ self->node,
+ add_cgraph_edge_to_list)
}
PyObject *
gcc_CallgraphNode_get_callers(struct PyGccCallgraphNode * self)
{
- PyObject *result;
-
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_cgraph_node_for_each_caller(self->node,
- add_cgraph_edge_to_list,
- result)) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ IMPL_LIST_MAKER(gcc_cgraph_node_for_each_caller,
+ self->node,
+ add_cgraph_edge_to_list)
}
PyObject *
@@ -185,31 +145,13 @@ gcc_python_make_wrapper_cgraph_node(gcc_cgraph_node node)
real_make_cgraph_node_wrapper);
}
-static bool add_cgraph_node_to_list(gcc_cgraph_node node, void *user_data)
-{
- PyObject *result = (PyObject*)user_data;
- PyObject *item;
-
- item = gcc_python_make_wrapper_cgraph_node(node);
- if (!item) {
- return true;
- }
-
- if (-1 == PyList_Append(result, item)) {
- Py_DECREF(item);
- return true;
- }
-
- /* Success: */
- Py_DECREF(item);
- return false;
-}
+IMPL_APPENDER(add_cgraph_node_to_list,
+ gcc_cgraph_node,
+ gcc_python_make_wrapper_cgraph_node)
PyObject *
gcc_python_get_callgraph_nodes(PyObject *self, PyObject *args)
{
- PyObject *result;
-
/* For debugging, see GCC's dump of things: */
if (0) {
fprintf(stderr, "----------------BEGIN----------------\n");
@@ -217,16 +159,8 @@ gcc_python_get_callgraph_nodes(PyObject *self, PyObject *args)
fprintf(stderr, "---------------- END ----------------\n");
}
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_for_each_cgraph_node(add_cgraph_node_to_list, result)) {
- Py_DECREF(result);
- return NULL;
- }
- return result;
+ IMPL_GLOBAL_LIST_MAKER(gcc_for_each_cgraph_node,
+ add_cgraph_node_to_list)
}
/*
diff --git a/gcc-python-cfg.c b/gcc-python-cfg.c
index 98411ac..5ef2507 100644
--- a/gcc-python-cfg.c
+++ b/gcc-python-cfg.c
@@ -67,196 +67,75 @@ wrtp_mark_for_PyGccEdge(PyGccEdge *wrapper)
gcc_cfg_edge_mark_in_use(wrapper->e);
}
-static bool add_edge_to_list(gcc_cfg_edge edge, void *user_data)
-{
- PyObject *result = (PyObject*)user_data;
- PyObject *item;
-
- item = gcc_python_make_wrapper_edge(edge);
- if (!item) {
- return true;
- }
-
- if (-1 == PyList_Append(result, item)) {
- Py_DECREF(item);
- return true;
- }
-
- /* Success: */
- Py_DECREF(item);
- return false;
-}
+IMPL_APPENDER(add_edge_to_list,
+ gcc_cfg_edge,
+ gcc_python_make_wrapper_edge)
PyObject *
gcc_BasicBlock_get_preds(PyGccBasicBlock *self, void *closure)
{
- PyObject *result;
-
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_cfg_block_for_each_pred_edge(self->bb,
- add_edge_to_list,
- result)) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ IMPL_LIST_MAKER(gcc_cfg_block_for_each_pred_edge,
+ self->bb,
+ add_edge_to_list)
}
PyObject *
gcc_BasicBlock_get_succs(PyGccBasicBlock *self, void *closure)
{
- PyObject *result;
-
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_cfg_block_for_each_succ_edge(self->bb,
- add_edge_to_list,
- result)) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ IMPL_LIST_MAKER(gcc_cfg_block_for_each_succ_edge,
+ self->bb,
+ add_edge_to_list)
}
-static bool
-append_gimple_to_list(gcc_gimple stmt, void *user_data)
-{
- PyObject *result = (PyObject *)user_data;
- PyObject *obj_stmt;
-
- obj_stmt = gcc_python_make_wrapper_gimple(stmt);
- if (!obj_stmt) {
- return true;
- }
-
- if (PyList_Append(result, obj_stmt)) {
- Py_DECREF(obj_stmt);
- return true;
- }
-
- /* Success: */
- Py_DECREF(obj_stmt);
- return false;
-}
+IMPL_APPENDER(append_gimple_to_list,
+ gcc_gimple,
+ gcc_python_make_wrapper_gimple)
PyObject *
gcc_BasicBlock_get_gimple(PyGccBasicBlock *self, void *closure)
{
- PyObject *result = NULL;
-
assert(self);
assert(self->bb.inner);
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_cfg_block_for_each_gimple(self->bb,
- append_gimple_to_list,
- result)) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ IMPL_LIST_MAKER(gcc_cfg_block_for_each_gimple,
+ self->bb,
+ append_gimple_to_list)
}
-static bool
-append_gimple_phi_to_list(gcc_gimple_phi stmt, void *user_data)
+static PyObject*
+gcc_python_make_wrapper_gimple_phi(gcc_gimple_phi phi)
{
- PyObject *result = (PyObject *)user_data;
- PyObject *obj_stmt;
-
- obj_stmt = gcc_python_make_wrapper_gimple(gcc_gimple_phi_upcast(stmt));
- if (!obj_stmt) {
- return true;
- }
-
- if (PyList_Append(result, obj_stmt)) {
- Py_DECREF(obj_stmt);
- return true;
- }
-
- /* Success: */
- Py_DECREF(obj_stmt);
- return false;
+ return gcc_python_make_wrapper_gimple(gcc_gimple_phi_upcast(phi));
}
+IMPL_APPENDER(append_gimple_phi_to_list,
+ gcc_gimple_phi,
+ gcc_python_make_wrapper_gimple_phi)
+
PyObject *
gcc_BasicBlock_get_phi_nodes(PyGccBasicBlock *self, void *closure)
{
- PyObject *result = NULL;
-
assert(self);
assert(self->bb.inner);
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_cfg_block_for_each_gimple_phi(self->bb,
- append_gimple_phi_to_list,
- result)) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ IMPL_LIST_MAKER(gcc_cfg_block_for_each_gimple_phi,
+ self->bb,
+ append_gimple_phi_to_list)
}
-static bool
-append_rtl_to_list(gcc_rtl_insn insn, void *user_data)
-{
- PyObject *result = (PyObject *)user_data;
- PyObject *obj;
-
- obj = gcc_python_make_wrapper_rtl(insn);
- if (!obj) {
- return true;
- }
-
- if (PyList_Append(result, obj)) {
- Py_DECREF(obj);
- return true;
- }
-
- /* Success: */
- Py_DECREF(obj);
- return false;
-}
+IMPL_APPENDER(append_rtl_to_list,
+ gcc_rtl_insn,
+ gcc_python_make_wrapper_rtl)
PyObject *
gcc_BasicBlock_get_rtl(PyGccBasicBlock *self, void *closure)
{
- PyObject *result = NULL;
-
assert(self);
assert(self->bb.inner);
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_cfg_block_for_each_rtl_insn(self->bb,
- append_rtl_to_list,
- result)) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ IMPL_LIST_MAKER(gcc_cfg_block_for_each_rtl_insn,
+ self->bb,
+ append_rtl_to_list)
}
@@ -461,44 +340,16 @@ gcc_python_make_wrapper_basic_block(gcc_cfg_block bb)
real_make_basic_block_wrapper);
}
-static bool add_block_to_list(gcc_cfg_block block, void *user_data)
-{
- PyObject *result = (PyObject*)user_data;
- PyObject *item;
-
- item = gcc_python_make_wrapper_basic_block(block);
- if (!item) {
- return true;
- }
-
- if (-1 == PyList_Append(result, item)) {
- Py_DECREF(item);
- return true;
- }
-
- /* Success: */
- Py_DECREF(item);
- return false;
-}
+IMPL_APPENDER(add_block_to_list,
+ gcc_cfg_block,
+ gcc_python_make_wrapper_basic_block)
PyObject *
gcc_Cfg_get_basic_blocks(PyGccCfg *self, void *closure)
{
- PyObject *result;
-
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_cfg_for_each_block(self->cfg,
- add_block_to_list,
- result)) {
- Py_DECREF(result);
- return NULL;
- }
-
- return result;
+ IMPL_LIST_MAKER(gcc_cfg_for_each_block,
+ self->cfg,
+ add_block_to_list)
}
extern PyTypeObject gcc_LabelDeclType;
diff --git a/gcc-python-wrappers.h b/gcc-python-wrappers.h
index fb70fb4..7657948 100644
--- a/gcc-python-wrappers.h
+++ b/gcc-python-wrappers.h
@@ -25,6 +25,61 @@
#include "opts.h"
#include "cgraph.h"
+/*
+ Create a callback for use in a gcc for_each iterator to make wrapper
+ objects for the underlying gcc objects being iterated, and append the
+ wrapper objects to a Python list
+*/
+#define IMPL_APPENDER(FNNAME, KIND, MAKE_WRAPPER) \
+ static bool FNNAME(KIND var, void *user_data) \
+ { \
+ PyObject *result = (PyObject*)user_data; \
+ PyObject *obj_var; \
+ obj_var = MAKE_WRAPPER(var); \
+ if (!obj_var) { \
+ return true; \
+ } \
+ if (-1 == PyList_Append(result, obj_var)) { \
+ Py_DECREF(obj_var); \
+ return true; \
+ } \
+ /* Success: */ \
+ Py_DECREF(obj_var); \
+ return false; \
+ }
+
+/*
+ Create the body of a function that builds a list by calling a for_each
+ ITERATOR, passing in the APPENDER callback to convert the iterated items
+ into Python wrapper objects
+ */
+#define IMPL_LIST_MAKER(ITERATOR, ARG, APPENDER) \
+ PyObject *result; \
+ result = PyList_New(0); \
+ if (!result) { \
+ return NULL; \
+ } \
+ if (ITERATOR((ARG), APPENDER, result)) { \
+ Py_DECREF(result); \
+ return NULL; \
+ } \
+ return result;
+
+/*
+As per IMPL_LIST_MAKER, but for a global iterator that takes no ARG
+ */
+#define IMPL_GLOBAL_LIST_MAKER(ITERATOR, APPENDER) \
+ PyObject *result; \
+ result = PyList_New(0); \
+ if (!result) { \
+ return NULL; \
+ } \
+ if (ITERATOR(APPENDER, result)) { \
+ Py_DECREF(result); \
+ return NULL; \
+ } \
+ return result;
+
PyMODINIT_FUNC initoptpass(void);
/* gcc-python-attribute.c: */
diff --git a/gcc-python.c b/gcc-python.c
index 462bf4d..4627770 100644
--- a/gcc-python.c
+++ b/gcc-python.c
@@ -226,39 +226,15 @@ gcc_python_get_parameters(PyObject *self, PyObject *args)
return NULL;
}
-static bool add_var_to_list(gcc_variable var, void *user_data)
-{
- PyObject *result = (PyObject*)user_data;
- PyObject *obj_var;
-
- obj_var = gcc_python_make_wrapper_variable(var);
- if (!obj_var) {
- return true;
- }
- if (-1 == PyList_Append(result, obj_var)) {
- Py_DECREF(obj_var);
- return true;
- }
- /* Success: */
- Py_DECREF(obj_var);
- return false;
-}
+IMPL_APPENDER(add_var_to_list,
+ gcc_variable,
+ gcc_python_make_wrapper_variable)
static PyObject *
gcc_python_get_variables(PyObject *self, PyObject *args)
{
- PyObject *result;
-
- result = PyList_New(0);
- if (!result) {
- return NULL;
- }
-
- if (gcc_for_each_variable(add_var_to_list, result)) {
- Py_DECREF(result);
- return NULL;
- }
- return result;
+ IMPL_GLOBAL_LIST_MAKER(gcc_for_each_variable,
+ add_var_to_list)
}
static PyObject *