summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2012-01-09 20:32:33 (GMT)
committerDavid Malcolm <dmalcolm@redhat.com>2012-01-09 20:32:33 (GMT)
commitbd03e3b0845fa785330af3916f4253d505a9fa16 (patch)
treed091c5215d7ba8eacd9c926175c3caf3b088e89f
parent4099d9f8b78f5f65acf340bbda9726f90a955f41 (diff)
downloadgcc-python-plugin-bd03e3b0845fa785330af3916f4253d505a9fa16.zip
gcc-python-plugin-bd03e3b0845fa785330af3916f4253d505a9fa16.tar.gz
gcc-python-plugin-bd03e3b0845fa785330af3916f4253d505a9fa16.tar.xz
docs: document looking up a C struct by name (ticket #26)
Document how to look up the gcc.RecordType for a C struct (without needing a typedef), giving an example: tests/examples/c/struct Fixes https://fedorahosted.org/gcc-python-plugin/ticket/26
-rw-r--r--docs/tree.rst27
-rw-r--r--tests/examples/c/struct/input.c37
-rw-r--r--tests/examples/c/struct/script.py41
-rw-r--r--tests/examples/c/struct/stdout.txt16
4 files changed, 120 insertions, 1 deletions
diff --git a/docs/tree.rst b/docs/tree.rst
index bf96e82..77d20c2 100644
--- a/docs/tree.rst
+++ b/docs/tree.rst
@@ -323,7 +323,7 @@ Types
.. py:attribute:: name
- (gcc.Type or None) the name of the type
+ The :py:class:`gcc.IdentifierNode` for the name of the type, or `None`.
.. py:attribute:: pointer
@@ -505,6 +505,31 @@ Additional attributes for various :py:class:`gcc.Type` subclasses:
A tuple of :py:class:`gcc.Type` instances, representing the function's
argument types
+.. py:class:: gcc.RecordType
+
+ A compound type, such as a C `struct`
+
+ .. py:attribute:: fields
+
+ The fields of this type, as a list of :py:class:`gcc.FieldDecl` instances
+
+ You can look up C structures by looking within the top-level
+ :py:class:`gcc.Block` within the current translation unit. For example,
+ given this sample C code:
+
+ .. literalinclude:: ../tests/examples/c/struct/input.c
+ :lines: 20-30
+ :language: c
+
+ then the following Python code:
+
+ .. literalinclude:: ../tests/examples/c/struct/script.py
+ :lines: 21-40
+
+ will generate this output:
+
+ .. literalinclude:: ../tests/examples/c/struct/stdout.txt
+
Constants
---------
diff --git a/tests/examples/c/struct/input.c b/tests/examples/c/struct/input.c
new file mode 100644
index 0000000..c61d9f8
--- /dev/null
+++ b/tests/examples/c/struct/input.c
@@ -0,0 +1,37 @@
+/*
+ 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/>.
+*/
+
+/* Example of a struct: */
+struct test_struct {
+ int a;
+ char b;
+ float c;
+};
+
+void foo()
+{
+}
+
+/*
+ PEP-7
+Local variables:
+c-basic-offset: 4
+indent-tabs-mode: nil
+End:
+*/
diff --git a/tests/examples/c/struct/script.py b/tests/examples/c/struct/script.py
new file mode 100644
index 0000000..6ad3df2
--- /dev/null
+++ b/tests/examples/c/struct/script.py
@@ -0,0 +1,41 @@
+# -*- coding: utf-8 -*-
+# 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/>.
+
+# Demonstration of how to look up a struct's declaration from Python
+
+import gcc
+
+class TestPass(gcc.GimplePass):
+ def execute(self, fn):
+ print('fn: %r' % fn)
+ for u in gcc.get_translation_units():
+ for decl in u.block.vars:
+ if isinstance(decl, gcc.TypeDecl):
+ # "decl" is a gcc.TypeDecl
+ # "decl.type" is a gcc.RecordType:
+ print(' type(decl): %s' % type(decl))
+ print(' type(decl.type): %s' % type(decl.type))
+ print(' decl.type.name: %r' % decl.type.name)
+ for f in decl.type.fields:
+ print(' type(f): %s' % type(f))
+ print(' f.name: %r' % f.name)
+ print(' f.type: %s' % f.type)
+ print(' type(f.type): %s' % type(f.type))
+
+test_pass = TestPass(name='test-pass')
+test_pass.register_after('cfg')
diff --git a/tests/examples/c/struct/stdout.txt b/tests/examples/c/struct/stdout.txt
new file mode 100644
index 0000000..a6fd515
--- /dev/null
+++ b/tests/examples/c/struct/stdout.txt
@@ -0,0 +1,16 @@
+fn: gcc.Function('foo')
+ type(decl): <type 'gcc.TypeDecl'>
+ type(decl.type): <type 'gcc.RecordType'>
+ decl.type.name: gcc.IdentifierNode(name='test_struct')
+ type(f): <type 'gcc.FieldDecl'>
+ f.name: 'a'
+ f.type: int
+ type(f.type): <type 'gcc.IntegerType'>
+ type(f): <type 'gcc.FieldDecl'>
+ f.name: 'b'
+ f.type: char
+ type(f.type): <type 'gcc.IntegerType'>
+ type(f): <type 'gcc.FieldDecl'>
+ f.name: 'c'
+ f.type: float
+ type(f.type): <type 'gcc.RealType'>