5 patches attached, 4 first is mostly trivial stuff.

* [python] Add testcase for annotation cursor kind

 Old testcase I had laying around.

* [python] Fix warning in testcase

 Trivial fix for a warning in the c-code in testcase

* [python] Run tests for c++ with std=c++11

 Makes c++ python testcases compile with -std=c++11,
 which is needed by...

* [python] Add testcase for enum with specified underlaying type

 Adds a more interesting enum_type testcase which
 has a different type.

* [python] Add Cursor.enum_value wrapping clang_getEnumConstantDeclValue

 This is the main patch. It adds a new "enum_value" property to the
 Cursor type, allowing to get the value of enum constants
>From c7760bf0b5c588c99403ddee363699b94f116888 Mon Sep 17 00:00:00 2001
From: Anders Waldenborg <[email protected]>
Date: Mon, 19 Dec 2011 13:53:53 +0100
Subject: [PATCH 1/5] [python] Add testcase for annotation cursor kind

---
 bindings/python/tests/cindex/test_cursor_kind.py |   21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/bindings/python/tests/cindex/test_cursor_kind.py b/bindings/python/tests/cindex/test_cursor_kind.py
index f8466e5..9ff6bf2 100644
--- a/bindings/python/tests/cindex/test_cursor_kind.py
+++ b/bindings/python/tests/cindex/test_cursor_kind.py
@@ -1,4 +1,4 @@
-from clang.cindex import CursorKind
+from clang.cindex import Index,CursorKind
 
 def test_name():
     assert CursorKind.UNEXPOSED_DECL.name is 'UNEXPOSED_DECL'
@@ -38,3 +38,22 @@ def test_kind_groups():
             assert len(group) == 0
         else:
             assert len(group) == 1
+
+annotationInput="""
+int foo (void) __attribute__ ((annotate("here be annotation attribute")));
+"""
+def test_annotation_attribute():
+    index = Index.create()
+    tu = index.parse('t.c', unsaved_files = [('t.c',annotationInput)])
+
+    for n in tu.cursor.get_children():
+        if n.spelling == 'foo':
+            for c in n.get_children():
+                if c.kind == CursorKind.ANNOTATE_ATTR:
+                    assert c.displayname == "here be annotation attribute"
+                    break
+            else:
+                assert False, "Couldn't find annotation"
+            break
+    else:
+        assert False, "Didn't find foo??"
-- 
1.7.9.5

>From ec388f809b6f55982aea1d7392ba58eeef910061 Mon Sep 17 00:00:00 2001
From: Anders Waldenborg <[email protected]>
Date: Thu, 26 Apr 2012 14:13:13 +0200
Subject: [PATCH 2/5] [python] Fix warning in testcase

---
 bindings/python/tests/cindex/test_type.py |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bindings/python/tests/cindex/test_type.py b/bindings/python/tests/cindex/test_type.py
index 03621f3..9b5a16e 100644
--- a/bindings/python/tests/cindex/test_type.py
+++ b/bindings/python/tests/cindex/test_type.py
@@ -263,7 +263,7 @@ def test_is_volatile_qualified():
 def test_is_restrict_qualified():
     """Ensure Type.is_restrict_qualified works."""
 
-    tu = get_tu('struct s { void * restrict i; void * j };')
+    tu = get_tu('struct s { void * restrict i; void * j; };')
 
     i = get_cursor(tu, 'i')
     j = get_cursor(tu, 'j')
-- 
1.7.9.5

>From 9f3c02b5278ea1a1feb12bcf416eef7e1d8f2cab Mon Sep 17 00:00:00 2001
From: Anders Waldenborg <[email protected]>
Date: Thu, 26 Apr 2012 14:14:19 +0200
Subject: [PATCH 3/5] [python] Run tests for c++ with std=c++11

---
 bindings/python/tests/cindex/util.py |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/bindings/python/tests/cindex/util.py b/bindings/python/tests/cindex/util.py
index 388b269..f924094 100644
--- a/bindings/python/tests/cindex/util.py
+++ b/bindings/python/tests/cindex/util.py
@@ -15,16 +15,17 @@ def get_tu(source, lang='c', all_warnings=False):
     all_warnings is a convenience argument to enable all compiler warnings.
     """
     name = 't.c'
+    args = []
     if lang == 'cpp':
         name = 't.cpp'
+        args.append('-std=c++11')
     elif lang == 'objc':
         name = 't.m'
     elif lang != 'c':
         raise Exception('Unknown language: %s' % lang)
 
-    args = []
     if all_warnings:
-        args = ['-Wall', '-Wextra']
+        args += ['-Wall', '-Wextra']
 
     index = Index.create()
     tu = index.parse(name, args=args, unsaved_files=[(name, source)])
-- 
1.7.9.5

>From 776ba962efbc3488d92695c8adf15a154e145f1c Mon Sep 17 00:00:00 2001
From: Anders Waldenborg <[email protected]>
Date: Thu, 26 Apr 2012 14:15:13 +0200
Subject: [PATCH 4/5] [python] Add testcase for enum with specified
 underlaying type

---
 bindings/python/tests/cindex/test_cursor.py |    8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/bindings/python/tests/cindex/test_cursor.py b/bindings/python/tests/cindex/test_cursor.py
index 9f02bb2..5e8d1dc 100644
--- a/bindings/python/tests/cindex/test_cursor.py
+++ b/bindings/python/tests/cindex/test_cursor.py
@@ -84,6 +84,14 @@ def test_enum_type():
     enum_type = enum.enum_type
     assert enum_type.kind == TypeKind.UINT
 
+def test_enum_type_cpp():
+    tu = get_tu('enum TEST : long long { FOO=1, BAR=2 };', lang="cpp")
+    enum = get_cursor(tu, 'TEST')
+    assert enum is not None
+
+    assert enum.kind == CursorKind.ENUM_DECL
+    assert enum.enum_type.kind == TypeKind.LONGLONG
+
 def test_objc_type_encoding():
     tu = get_tu('int i;', lang='objc')
     i = get_cursor(tu, 'i')
-- 
1.7.9.5

>From 475db845e8afdc1777260b4e35c06e7250745404 Mon Sep 17 00:00:00 2001
From: Anders Waldenborg <[email protected]>
Date: Thu, 26 Apr 2012 17:01:17 +0200
Subject: [PATCH 5/5] [python] Add Cursor.enum_value wrapping
 clang_getEnumConstantDeclValue

---
 bindings/python/clang/cindex.py             |   30 ++++++++++++++++++++++
 bindings/python/tests/cindex/test_cursor.py |   37 +++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index 6f0d25f..35188a7 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -1008,6 +1008,28 @@ class Cursor(Structure):
         return self._enum_type
 
     @property
+    def enum_value(self):
+        """Return the value of an enum constant."""
+        if not hasattr(self, '_enum_value'):
+            assert self.kind == CursorKind.ENUM_CONSTANT_DECL
+            typ = self.type
+            if typ.kind == TypeKind.ENUM:
+                typ = typ.get_declaration().enum_type
+            if typ.kind in (TypeKind.CHAR_U,
+                            TypeKind.UCHAR,
+                            TypeKind.CHAR16,
+                            TypeKind.CHAR32,
+                            TypeKind.USHORT,
+                            TypeKind.UINT,
+                            TypeKind.ULONG,
+                            TypeKind.ULONGLONG,
+                            TypeKind.UINT128):
+                self._enum_value = Cursor_enum_const_decl_unsigned(self)
+            else:
+                self._enum_value = Cursor_enum_const_decl(self)
+        return self._enum_value
+
+    @property
     def objc_type_encoding(self):
         """Return the Objective-C type encoding as a str."""
         if not hasattr(self, '_objc_type_encoding'):
@@ -1937,6 +1959,14 @@ Cursor_enum_type.argtypes = [Cursor]
 Cursor_enum_type.restype = Type
 Cursor_enum_type.errcheck = Type.from_result
 
+Cursor_enum_const_decl = lib.clang_getEnumConstantDeclValue
+Cursor_enum_const_decl.argtypes = [Cursor]
+Cursor_enum_const_decl.restype = c_longlong
+
+Cursor_enum_const_decl_unsigned = lib.clang_getEnumConstantDeclUnsignedValue
+Cursor_enum_const_decl_unsigned.argtypes = [Cursor]
+Cursor_enum_const_decl_unsigned.restype = c_ulonglong
+
 Cursor_objc_type_encoding = lib.clang_getDeclObjCTypeEncoding
 Cursor_objc_type_encoding.argtypes = [Cursor]
 Cursor_objc_type_encoding.restype = _CXString
diff --git a/bindings/python/tests/cindex/test_cursor.py b/bindings/python/tests/cindex/test_cursor.py
index 5e8d1dc..6aa9594 100644
--- a/bindings/python/tests/cindex/test_cursor.py
+++ b/bindings/python/tests/cindex/test_cursor.py
@@ -98,3 +98,40 @@ def test_objc_type_encoding():
 
     assert i is not None
     assert i.objc_type_encoding == 'i'
+
+def test_enum_values():
+    tu = get_tu('enum TEST { SPAM=1, EGG, HAM = EGG * 20};')
+    enum = get_cursor(tu, 'TEST')
+    assert enum is not None
+
+    assert enum.kind == CursorKind.ENUM_DECL
+
+    chld = list(enum.get_children())
+    assert len(chld) == 3
+
+    spam, egg, ham = chld
+
+    assert spam.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert spam.enum_value == 1
+    assert egg.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert egg.enum_value == 2
+    assert ham.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert ham.enum_value == 40
+
+def test_enum_values_cpp():
+    tu = get_tu('enum TEST : long long { SPAM = -1, HAM = 0x10000000000};', lang="cpp")
+    enum = get_cursor(tu, 'TEST')
+    assert enum is not None
+
+    assert enum.kind == CursorKind.ENUM_DECL
+
+    chld = list(enum.get_children())
+    assert len(chld) == 2
+
+    spam, ham = chld
+
+    assert spam.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert spam.enum_value == -1
+    assert ham.kind == CursorKind.ENUM_CONSTANT_DECL
+    assert ham.enum_value == 0x10000000000
+
-- 
1.7.9.5

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to