From 28383871f586d26a78f3eb5dc9438b47905633d4 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 19 Nov 2015 22:03:01 -0700 Subject: [PATCH] document EnumeralType; add 'values' attribute --- docs/tree.rst | 11 +++++++++++ gcc-python-tree.c | 2 +- generate-tree-c.py | 5 +++++ tests/plugin/enum-type/input.c | 28 ++++++++++++++++++++++++++++ tests/plugin/enum-type/script.py | 30 ++++++++++++++++++++++++++++++ tests/plugin/enum-type/stdout.txt | 5 +++++ 6 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 tests/plugin/enum-type/input.c create mode 100644 tests/plugin/enum-type/script.py create mode 100644 tests/plugin/enum-type/stdout.txt diff --git a/docs/tree.rst b/docs/tree.rst index 0456b8a7..dd41a121 100644 --- a/docs/tree.rst +++ b/docs/tree.rst @@ -542,6 +542,17 @@ Additional attributes for various :py:class:`gcc.Type` subclasses: The :py:class:`gcc.Type` that this type points to. In the above example (`int *`), this would be the `int` type. +.. py:class:: gcc.EnumeralType + + Subclass of :py:class:`gcc.Type` representing an enumeral type. + + .. py:attribute:: values + + A list of tuple representing the constants defined in this + enumeration. Each tuple consists of two elements; the first + being the name of the constant, a :py:class:`gcc.IdentifierNode`; + and the second being the value, a :py:class:`gcc.Constant`. + .. py:class:: gcc.ArrayType Subclass of :py:class:`gcc.Type` representing an array type. For example, diff --git a/gcc-python-tree.c b/gcc-python-tree.c index 9a0b32b0..d866f759 100644 --- a/gcc-python-tree.c +++ b/gcc-python-tree.c @@ -1152,7 +1152,7 @@ PyGcc_TreeMakeListFromTreeList(tree t) Extract a list of objects for the values */ PyObject * -gcc_tree_list_of_pairs_from_tree_list_chain(tree t) +PyGcc_TreeMakeListOfPairsFromTreeListChain(tree t) { PyObject *result = NULL; diff --git a/generate-tree-c.py b/generate-tree-c.py index 53eacf28..60ce74d1 100644 --- a/generate-tree-c.py +++ b/generate-tree-c.py @@ -485,6 +485,11 @@ def add_complex_getter(name, doc): 'PyGcc_TreeListFromChain(TYPE_METHODS(self->t.inner))', "The methods of this type") + if tree_type.SYM == 'ENUMERAL_TYPE': + add_simple_getter('values', + 'PyGcc_TreeMakeListOfPairsFromTreeListChain(TYPE_VALUES(self->t.inner))', + "The values of this type") + if tree_type.SYM == 'IDENTIFIER_NODE': add_simple_getter('name', 'PyGccStringOrNone(IDENTIFIER_POINTER(self->t.inner))', diff --git a/tests/plugin/enum-type/input.c b/tests/plugin/enum-type/input.c new file mode 100644 index 00000000..67937244 --- /dev/null +++ b/tests/plugin/enum-type/input.c @@ -0,0 +1,28 @@ +/* + Copyright 2015 Tom Tromey + + 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 + . +*/ + +enum the_enum +{ + ONE = 1, + TWO = 2, + MINUS_ONE = -1 +}; + +/* We need a variable because some versions of gcc don't call + PLUGIN_FINISH_TYPE for an enum. */ +enum the_enum variable; diff --git a/tests/plugin/enum-type/script.py b/tests/plugin/enum-type/script.py new file mode 100644 index 00000000..92a3bfc9 --- /dev/null +++ b/tests/plugin/enum-type/script.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Copyright 2015 Tom Tromey +# +# 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 +# . + +import gcc + +def on_decl(v, *args, **kwargs): + if v.name != 'variable': + return + + t = v.type + print(t.name) + print('length = %d' % len(t.values)) + for (name, value) in t.values: + print('%s = %s' % (name, value)) + +gcc.register_callback(gcc.PLUGIN_FINISH_DECL, on_decl) diff --git a/tests/plugin/enum-type/stdout.txt b/tests/plugin/enum-type/stdout.txt new file mode 100644 index 00000000..3b1276d4 --- /dev/null +++ b/tests/plugin/enum-type/stdout.txt @@ -0,0 +1,5 @@ +the_enum +length = 3 +ONE = 1 +TWO = 2 +MINUS_ONE = -1