commit 653332428968519fa29d0e42bfa4360c6db7cd20 Author: David Malcolm dmalcolm@redhat.com Date: Thu Mar 29 23:02:07 2012 -0400
introduce GccLocationI
Makefile | 1 + gcc-python-callbacks.c | 8 ++- gcc-python-diagnostics.c | 8 +- gcc-python-location.c | 17 +++--- gcc-python-pass.c | 27 +++++---- gcc-python-rtl.c | 2 +- gcc-python.c | 6 +- gcc-python.h | 2 +- generate-function-c.py | 4 +- generate-gimple-c.py | 2 +- generate-location-c.py | 10 +-- generate-tree-c.py | 4 +- proposed-plugin-api/gcc-location.c | 82 +++++++++++++++++++++++++++ proposed-plugin-api/gcc-location.h | 52 +++++++++++++++++ proposed-plugin-api/gcc-public-types.h | 3 + proposed-plugin-api/gcc-semiprivate-types.h | 11 ++++ 16 files changed, 196 insertions(+), 43 deletions(-) --- diff --git a/Makefile b/Makefile index 86d24d5..d0d6e8b 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ PLUGIN_SOURCE_FILES= \ proposed-plugin-api/gcc-cfg.c \ proposed-plugin-api/gcc-gimple.c \ + proposed-plugin-api/gcc-location.c \ proposed-plugin-api/gcc-rtl.c \ gcc-python.c \ gcc-python-attribute.c \ diff --git a/gcc-python-callbacks.c b/gcc-python-callbacks.c index 5e23f58..ab17ce0 100644 --- a/gcc-python-callbacks.c +++ b/gcc-python-callbacks.c @@ -23,6 +23,8 @@ #include "gcc-python-closure.h" #include "gcc-python-wrappers.h"
+#include "proposed-plugin-api/gcc-location.h" + /* Notes on the passes
@@ -128,7 +130,7 @@ gcc_python_finish_invoking_callback(PyGILState_STATE gstate, struct callback_closure *closure = (struct callback_closure *)user_data; PyObject *args = NULL; PyObject *result = NULL; - location_t saved_loc = input_location; + GccLocationI saved_loc = Gcc_GetInputLocation(); enum plugin_event saved_event;
assert(closure); @@ -141,7 +143,7 @@ gcc_python_finish_invoking_callback(PyGILState_STATE gstate,
if (cfun) { /* Temporarily override input_location to the top of the function: */ - input_location = cfun->function_start_locus; + Gcc_SetInputLocation(GccPrivate_make_LocationI(cfun->function_start_locus)); }
args = gcc_python_closure_make_args(closure, 1, wrapped_gcc_data); @@ -169,7 +171,7 @@ cleanup: Py_XDECREF(result);
PyGILState_Release(gstate); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); }
/* diff --git a/gcc-python-diagnostics.c b/gcc-python-diagnostics.c index 7b6f9db..2bdeef2 100644 --- a/gcc-python-diagnostics.c +++ b/gcc-python-diagnostics.c @@ -57,7 +57,7 @@ gcc_python_permerror(PyObject *self, PyObject *args) }
/* Invoke the GCC function: */ - result_b = permerror(loc_obj->loc, "%s", msgid); + result_b = permerror(loc_obj->loc.inner, "%s", msgid);
result_obj = PyBool_FromLong(result_b);
@@ -80,7 +80,7 @@ gcc_python_error(PyObject *self, PyObject *args, PyObject *kwargs) return NULL; }
- error_at(loc_obj->loc, "%s", msg); + error_at(loc_obj->loc.inner, "%s", msg);
Py_RETURN_NONE; } @@ -135,7 +135,7 @@ gcc_python_warning(PyObject *self, PyObject *args, PyObject *kwargs) } }
- was_reported = warning_at(loc_obj->loc, opt_code, "%s", msg); + was_reported = warning_at(loc_obj->loc.inner, opt_code, "%s", msg);
return PyBool_FromLong(was_reported); } @@ -156,7 +156,7 @@ gcc_python_inform(PyObject *self, PyObject *args, PyObject *kwargs) return NULL; }
- inform(loc_obj->loc, "%s", msg); + inform(loc_obj->loc.inner, "%s", msg);
Py_RETURN_NONE; } diff --git a/gcc-python-location.c b/gcc-python-location.c index 964c921..546455f 100644 --- a/gcc-python-location.c +++ b/gcc-python-location.c @@ -20,6 +20,7 @@ #include <Python.h> #include "gcc-python.h" #include "gcc-python-wrappers.h" +#include "proposed-plugin-api/gcc-location.h"
/* Wrapper for GCC's "location_t" @@ -36,16 +37,16 @@ PyObject * gcc_Location_repr(struct PyGccLocation * self) { return gcc_python_string_from_format("gcc.Location(file='%s', line=%i)", - LOCATION_FILE(self->loc), - LOCATION_LINE(self->loc)); + GccLocationI_GetFilename(self->loc), + GccLocationI_GetLine(self->loc)); }
PyObject * gcc_Location_str(struct PyGccLocation * self) { return gcc_python_string_from_format("%s:%i", - LOCATION_FILE(self->loc), - LOCATION_LINE(self->loc)); + GccLocationI_GetFilename(self->loc), + GccLocationI_GetLine(self->loc)); }
PyObject * @@ -68,11 +69,11 @@ gcc_Location_richcompare(PyObject *o1, PyObject *o2, int op)
switch (op) { case Py_EQ: - cond = (locobj1->loc == locobj2->loc); + cond = (locobj1->loc.inner == locobj2->loc.inner); break;
case Py_NE: - cond = (locobj1->loc != locobj2->loc); + cond = (locobj1->loc.inner != locobj2->loc.inner); break;
default: @@ -87,11 +88,11 @@ gcc_Location_richcompare(PyObject *o1, PyObject *o2, int op) }
PyObject * -gcc_python_make_wrapper_location(location_t loc) +gcc_python_make_wrapper_location(GccLocationI loc) { struct PyGccLocation *location_obj = NULL;
- if (UNKNOWN_LOCATION == loc) { + if (GccLocationI_IsUnknown(loc)) { Py_RETURN_NONE; }
diff --git a/gcc-python-pass.c b/gcc-python-pass.c index 5315df1..8471222 100644 --- a/gcc-python-pass.c +++ b/gcc-python-pass.c @@ -21,6 +21,7 @@ #include "gcc-python.h" #include "gcc-python-wrappers.h" #include "diagnostic.h" +#include "proposed-plugin-api/gcc-location.h"
/* Wrapper for GCC's (opt_pass *) @@ -42,7 +43,7 @@ static bool impl_gate(void) PyObject *cfun_obj = NULL; PyObject* result_obj; int result; - location_t saved_loc = input_location; + GccLocationI saved_loc = Gcc_GetInputLocation();
assert(current_pass); pass_obj = gcc_python_make_wrapper_pass(current_pass); @@ -57,12 +58,12 @@ static bool impl_gate(void) /* Supply the current function, if any */ if (cfun) { /* Temporarily override input_location to the top of the function: */ - input_location = cfun->function_start_locus; + Gcc_SetInputLocation(GccPrivate_make_LocationI(cfun->function_start_locus)); cfun_obj = gcc_python_make_wrapper_function(cfun); if (!cfun_obj) { gcc_python_print_exception("Unhandled Python exception raised calling 'gate' method"); Py_DECREF(pass_obj); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); return false; } result_obj = PyObject_CallMethod(pass_obj, "gate", "O", cfun_obj, NULL); @@ -75,13 +76,13 @@ static bool impl_gate(void)
if (!result_obj) { gcc_python_print_exception("Unhandled Python exception raised calling 'gate' method"); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); return false; }
result = PyObject_IsTrue(result_obj); Py_DECREF(result_obj); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); return result; }
@@ -90,7 +91,7 @@ static unsigned int impl_execute(void) PyObject *pass_obj; PyObject *cfun_obj = NULL; PyObject* result_obj; - location_t saved_loc = input_location; + GccLocationI saved_loc = Gcc_GetInputLocation();
assert(current_pass); pass_obj = gcc_python_make_wrapper_pass(current_pass); @@ -99,12 +100,12 @@ static unsigned int impl_execute(void) /* Supply the current function, if any */ if (cfun) { /* Temporarily override input_location to the top of the function: */ - input_location = cfun->function_start_locus; + Gcc_SetInputLocation(GccPrivate_make_LocationI(cfun->function_start_locus)); cfun_obj = gcc_python_make_wrapper_function(cfun); if (!cfun_obj) { gcc_python_print_exception("Unhandled Python exception raised calling 'execute' method"); Py_DECREF(pass_obj); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); return false; } result_obj = PyObject_CallMethod(pass_obj, "execute", "O", cfun_obj, NULL); @@ -117,13 +118,13 @@ static unsigned int impl_execute(void)
if (!result_obj) { gcc_python_print_exception("Unhandled Python exception raised calling 'execute' method"); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); return 0; }
if (result_obj == Py_None) { Py_DECREF(result_obj); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); return 0; }
@@ -131,7 +132,7 @@ static unsigned int impl_execute(void) if (PyInt_Check(result_obj)) { long result = PyInt_AS_LONG(result_obj); Py_DECREF(result_obj); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); return result; } #endif @@ -139,7 +140,7 @@ static unsigned int impl_execute(void) if (PyLong_Check(result_obj)) { long result = PyLong_AsLong(result_obj); Py_DECREF(result_obj); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); return result; }
@@ -149,7 +150,7 @@ static unsigned int impl_execute(void) Py_TYPE(result_obj)->tp_name); Py_DECREF(result_obj); gcc_python_print_exception("Unhandled Python exception raised calling 'execute' method"); - input_location = saved_loc; + Gcc_SetInputLocation(saved_loc); return 0; }
diff --git a/gcc-python-rtl.c b/gcc-python-rtl.c index e499cdf..76114fd 100644 --- a/gcc-python-rtl.c +++ b/gcc-python-rtl.c @@ -31,7 +31,7 @@ gcc_Rtl_get_location(struct PyGccRtl *self, void *closure) { int locator = INSN_LOCATOR (self->insn.inner); if (locator && insn_file (self->insn.inner)) { - return gcc_python_make_wrapper_location(locator_location(locator)); + return gcc_python_make_wrapper_location(GccPrivate_make_LocationI(locator_location(locator))); }
Py_RETURN_NONE; diff --git a/gcc-python.c b/gcc-python.c index b837c1c..659e836 100644 --- a/gcc-python.c +++ b/gcc-python.c @@ -23,6 +23,8 @@ #include "gcc-python-closure.h" #include "gcc-python-wrappers.h"
+#include "proposed-plugin-api/gcc-location.h" + int plugin_is_GPL_compatible;
#include "plugin-version.h" @@ -124,7 +126,7 @@ gcc_python_set_location(PyObject *self, PyObject *args) return NULL; }
- input_location = loc_obj->loc; + Gcc_SetInputLocation(loc_obj->loc);
Py_RETURN_NONE; } @@ -912,7 +914,7 @@ void gcc_python_print_exception(const char *msg) within passes is initialized to the top of the function; it can be temporarily overridden using gcc.set_location() */ - error_at(input_location, "%s", msg); + error_at(Gcc_GetInputLocation().inner, "%s", msg);
/* Print the traceback: */ PyErr_PrintEx(1); diff --git a/gcc-python.h b/gcc-python.h index ff3ad4b..a4f004e 100644 --- a/gcc-python.h +++ b/gcc-python.h @@ -172,7 +172,7 @@ DECLARE_SIMPLE_WRAPPER(PyGccPass, DECLARE_SIMPLE_WRAPPER(PyGccLocation, gcc_LocationType, location, - location_t, loc) + GccLocationI, loc)
DECLARE_SIMPLE_WRAPPER(PyGccGimple, gcc_GimpleType, diff --git a/generate-function-c.py b/generate-function-c.py index 9e03fb0..67b007f 100644 --- a/generate-function-c.py +++ b/generate-function-c.py @@ -60,11 +60,11 @@ def generate_function(): 'Function sequence number for profiling, debugging, etc.') getsettable.add_simple_getter(cu, 'start', - 'gcc_python_make_wrapper_location(self->fun->function_start_locus)', + 'gcc_python_make_wrapper_location(GccPrivate_make_LocationI(self->fun->function_start_locus))', 'Location of the start of the function') getsettable.add_simple_getter(cu, 'end', - 'gcc_python_make_wrapper_location(self->fun->function_end_locus)', + 'gcc_python_make_wrapper_location(GccPrivate_make_LocationI(self->fun->function_end_locus))', 'Location of the end of the function') cu.add_defn(getsettable.c_defn())
diff --git a/generate-gimple-c.py b/generate-gimple-c.py index 4f859e1..4893b9e 100644 --- a/generate-gimple-c.py +++ b/generate-gimple-c.py @@ -192,7 +192,7 @@ def generate_gimple(): static PyObject * gcc_Gimple_get_location(struct PyGccGimple *self, void *closure) { - return gcc_python_make_wrapper_location(gimple_location(self->stmt.inner)); + return gcc_python_make_wrapper_location(GccPrivate_make_LocationI(gimple_location(self->stmt.inner))); }
static PyObject * diff --git a/generate-location-c.py b/generate-location-c.py index 2fdf308..6312409 100644 --- a/generate-location-c.py +++ b/generate-location-c.py @@ -22,7 +22,7 @@ cu = CompilationUnit() cu.add_include('gcc-python.h') cu.add_include('gcc-python-wrappers.h') cu.add_include('gcc-plugin.h') -cu.add_include("tree.h") +cu.add_include("proposed-plugin-api/gcc-location.h")
modinit_preinit = '' modinit_postinit = '' @@ -38,7 +38,7 @@ def generate_location(): static PyObject * gcc_Location_get_file(struct PyGccLocation *self, void *closure) { - return gcc_python_string_from_string(LOCATION_FILE(self->loc)); + return gcc_python_string_from_string(GccLocationI_GetFilename(self->loc)); } """)
@@ -46,7 +46,7 @@ gcc_Location_get_file(struct PyGccLocation *self, void *closure) static PyObject * gcc_Location_get_line(struct PyGccLocation *self, void *closure) { - return gcc_python_int_from_long(LOCATION_LINE(self->loc)); + return gcc_python_int_from_long(GccLocationI_GetLine(self->loc)); } """)
@@ -54,9 +54,7 @@ gcc_Location_get_line(struct PyGccLocation *self, void *closure) static PyObject * gcc_Location_get_column(struct PyGccLocation *self, void *closure) { - expanded_location exploc = expand_location(self->loc); - - return gcc_python_int_from_long(exploc.column); + return gcc_python_int_from_long(GccLocationI_GetColumn(self->loc)); } """)
diff --git a/generate-tree-c.py b/generate-tree-c.py index f6fc0d3..ef63e2d 100644 --- a/generate-tree-c.py +++ b/generate-tree-c.py @@ -163,7 +163,7 @@ gcc_Declaration_get_name(struct PyGccTree *self, void *closure) static PyObject * gcc_Declaration_get_location(struct PyGccTree *self, void *closure) { - return gcc_python_make_wrapper_location(DECL_SOURCE_LOCATION(self->t)); + return gcc_python_make_wrapper_location(GccPrivate_make_LocationI(DECL_SOURCE_LOCATION(self->t))); } """)
@@ -277,7 +277,7 @@ PyObject* if localname in ('Reference', 'Comparison', 'Unary', 'Binary', 'Statement' 'VlExp', 'Expression'): add_simple_getter('location', - 'gcc_python_make_wrapper_location(EXPR_LOCATION(self->t))', + 'gcc_python_make_wrapper_location(GccPrivate_make_LocationI(EXPR_LOCATION(self->t)))', "The source location of this expression")
methods.add_method('get_symbol', diff --git a/proposed-plugin-api/gcc-location.c b/proposed-plugin-api/gcc-location.c new file mode 100644 index 0000000..7ca8303 --- /dev/null +++ b/proposed-plugin-api/gcc-location.c @@ -0,0 +1,82 @@ +/* + Copyright 2012 David Malcolm dmalcolm@redhat.com + Copyright 2012 Red Hat, Inc. + + This is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see + http://www.gnu.org/licenses/. +*/ + +#include "proposed-plugin-api/gcc-cfg.h" + +/*********************************************************** + GccLocationI +************************************************************/ +GCC_IMPLEMENT_PRIVATE_API(struct GccLocationI) +GccPrivate_make_LocationI(location_t inner) +{ + struct GccLocationI result; + result.inner = inner; + return result; +} + +GCC_IMPLEMENT_PUBLIC_API(void) +GccLocationI_MarkInUse(GccLocationI loc) +{ + /* empty */ +} + +GCC_IMPLEMENT_PUBLIC_API(void) +Gcc_SetInputLocation(GccLocationI loc) +{ + input_location = loc.inner; +} + +GCC_IMPLEMENT_PUBLIC_API(GccLocationI) +Gcc_GetInputLocation(void) +{ + return GccPrivate_make_LocationI(input_location); +} + +GCC_IMPLEMENT_PUBLIC_API(const char *) +GccLocationI_GetFilename(GccLocationI loc) +{ + return LOCATION_FILE(loc.inner); +} + +GCC_IMPLEMENT_PUBLIC_API(int) +GccLocationI_GetLine(GccLocationI loc) +{ + return LOCATION_LINE(loc.inner); +} + +GCC_IMPLEMENT_PUBLIC_API(int) +GccLocationI_GetColumn(GccLocationI loc) +{ + expanded_location exploc = expand_location(loc.inner); + return exploc.column; +} + +GCC_PUBLIC_API(bool) +GccLocationI_IsUnknown(GccLocationI loc) +{ + return UNKNOWN_LOCATION == loc.inner; +} + +/* + PEP-7 +Local variables: +c-basic-offset: 4 +indent-tabs-mode: nil +End: +*/ diff --git a/proposed-plugin-api/gcc-location.h b/proposed-plugin-api/gcc-location.h new file mode 100644 index 0000000..c1a8e1c --- /dev/null +++ b/proposed-plugin-api/gcc-location.h @@ -0,0 +1,52 @@ +/* + Copyright 2012 David Malcolm dmalcolm@redhat.com + Copyright 2012 Red Hat, Inc. + + This is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see + http://www.gnu.org/licenses/. +*/ + +#include "proposed-plugin-api/gcc-common.h" + +/* Declarations: locations */ + +/* GccLocationI */ +GCC_PUBLIC_API(void) +GccLocationI_MarkInUse(GccLocationI loc); + +GCC_PUBLIC_API(void) +Gcc_SetInputLocation(GccLocationI loc); + +GCC_PUBLIC_API(GccLocationI) +Gcc_GetInputLocation(void); + +GCC_PUBLIC_API(const char *) +GccLocationI_GetFilename(GccLocationI loc); + +GCC_PUBLIC_API(int) +GccLocationI_GetLine(GccLocationI loc); + +GCC_PUBLIC_API(int) +GccLocationI_GetColumn(GccLocationI loc); + +GCC_PUBLIC_API(bool) +GccLocationI_IsUnknown(GccLocationI loc); + +/* + PEP-7 +Local variables: +c-basic-offset: 4 +indent-tabs-mode: nil +End: +*/ diff --git a/proposed-plugin-api/gcc-public-types.h b/proposed-plugin-api/gcc-public-types.h index 795463d..007c123 100644 --- a/proposed-plugin-api/gcc-public-types.h +++ b/proposed-plugin-api/gcc-public-types.h @@ -37,4 +37,7 @@ typedef struct GccRtlInsnI GccRtlInsnI; /* Opaque types: pretty-printing */ typedef struct GccPrinterI GccPrinterI;
+/* Opaque types: locations */ +typedef struct GccLocationI GccLocationI; + #endif /* INCLUDED__GCC_PUBLIC_TYPES_H */ diff --git a/proposed-plugin-api/gcc-semiprivate-types.h b/proposed-plugin-api/gcc-semiprivate-types.h index f0b7474..c94e86c 100644 --- a/proposed-plugin-api/gcc-semiprivate-types.h +++ b/proposed-plugin-api/gcc-semiprivate-types.h @@ -20,6 +20,8 @@ #ifndef INCLUDED__GCC_SEMIPRIVATE_TYPES_H #define INCLUDED__GCC_SEMIPRIVATE_TYPES_H
+#include "input.h" /* for location_t */ + /* These "interface types" should be treated like pointers, only that users are required to collaborate with the garbage-collector. @@ -79,4 +81,13 @@ struct GccRtlInsnI { GCC_PRIVATE_API(struct GccRtlInsnI) GccPrivate_make_RtlInsnI(struct rtx_def *inner);
+/* Semiprivate types: locations */ +struct GccLocationI { + location_t inner; +}; + +GCC_PRIVATE_API(struct GccLocationI) +GccPrivate_make_LocationI(location_t inner); + + #endif /* INCLUDED__GCC_SEMIPRIVATE_TYPES_H */
gcc-python-plugin-commits@lists.stg.fedorahosted.org