I needed to be able to access the range of an array's type for a project
I'm working on.
This patch adds ArrayType.range, plus documentation and a test case.
Tom
From 9c6e8dc36597f830331e073b86186ddda2fb9432 Mon Sep 17 00:00:00 2001
From: Tom Tromey <tromey(a)redhat.com>
Date: Mon, 23 Jan 2012 07:49:59 -0700
Subject: [PATCH] Add ArrayType.range attribute
---
docs/tree.rst | 18 +++++++++++++++++
generate-tree-c.py | 5 ++++
tests/plugin/array-type/input.c | 37 ++++++++++++++++++++++++++++++++++++
tests/plugin/array-type/script.py | 36 +++++++++++++++++++++++++++++++++++
tests/plugin/array-type/stdout.txt | 3 ++
5 files changed, 99 insertions(+), 0 deletions(-)
create mode 100644 tests/plugin/array-type/input.c
create mode 100644 tests/plugin/array-type/script.py
create mode 100644 tests/plugin/array-type/stdout.txt
diff --git a/docs/tree.rst b/docs/tree.rst
index 316d8c4..cd3090f 100644
--- a/docs/tree.rst
+++ b/docs/tree.rst
@@ -459,6 +459,24 @@ Types
The :py:class:`gcc.Type` that this type points to. In the above
example, this would be the `char` type.
+ .. py:attribute:: range
+
+ The :py:class:`gcc.Type` that represents the range of the
+ array's indices. If the array has a known range, then this will
+ ordinarily be an :py:class:`gcc.IntegerType` whose `min_value`
+ and `max_value` are the (inclusive) bounds of the array. If the
+ array does not have a known range, then this attribute will be
+ `None`.
+
+ That is, in the example above, `range.min_val` is `0`, and
+ `range.max_val` is `15`.
+
+ But, for a C declaration like::
+
+ extern char array[];
+
+ the type's `range` would be `None`.
+
.. py:class:: gcc.VectorType
.. py:attribute:: dereference
diff --git a/generate-tree-c.py b/generate-tree-c.py
index 9e32924..9cf8f82 100644
--- a/generate-tree-c.py
+++ b/generate-tree-c.py
@@ -399,6 +399,11 @@ def generate_tree_code_classes():
'gcc_python_make_wrapper_tree(TREE_TYPE(self->t))',
"The gcc.Type that this type points to'")
+ if tree_type.SYM == 'ARRAY_TYPE':
+ add_simple_getter('range',
+
'gcc_python_make_wrapper_tree(TYPE_DOMAIN(self->t))',
+ "The gcc.Type that is the range of this array
type")
+
if tree_type.SYM == 'ARRAY_REF':
add_simple_getter('array',
'gcc_python_make_wrapper_tree(TREE_OPERAND(self->t,
0))',
diff --git a/tests/plugin/array-type/input.c b/tests/plugin/array-type/input.c
new file mode 100644
index 0000000..992d06d
--- /dev/null
+++ b/tests/plugin/array-type/input.c
@@ -0,0 +1,37 @@
+/*
+ Copyright 2012 Tom Tromey <tromey(a)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/>.
+*/
+
+/* Test some array attributes. */
+
+int arr[37];
+
+extern int arr2[];
+
+int function(void)
+{
+ return 23;
+}
+
+/*
+ PEP-7
+Local variables:
+c-basic-offset: 4
+indent-tabs-mode: nil
+End:
+*/
diff --git a/tests/plugin/array-type/script.py b/tests/plugin/array-type/script.py
new file mode 100644
index 0000000..5736323
--- /dev/null
+++ b/tests/plugin/array-type/script.py
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+# Copyright 2012 Tom Tromey <tromey(a)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/>.
+
+import gcc
+from gccutils import pprint
+
+def on_pass_execution(p, fn):
+ if p.name == '*warn_function_return':
+ assert isinstance(fn, gcc.Function)
+
+ for u in gcc.get_translation_units():
+ for v in u.block.vars:
+ if v.name == 'arr':
+ print('arr-range-min:%s' % v.type.range.min_value)
+ print('arr-range-max:%s' % v.type.range.max_value)
+ if v.name == 'arr2':
+ print('arr2-range:%s' % v.type.range)
+
+gcc.register_callback(gcc.PLUGIN_PASS_EXECUTION,
+ on_pass_execution)
+
diff --git a/tests/plugin/array-type/stdout.txt b/tests/plugin/array-type/stdout.txt
new file mode 100644
index 0000000..9dc5628
--- /dev/null
+++ b/tests/plugin/array-type/stdout.txt
@@ -0,0 +1,3 @@
+arr-range-min:0
+arr-range-max:36
+arr2-range:None
--
1.7.6.5