From 31cc38cb76317bfe50aadbc625d6ff67f727607a Mon Sep 17 00:00:00 2001 From: Gregory Szorc <gregory.szorc@gmail.com> Date: Sun, 19 Feb 2012 18:28:33 +0000 Subject: [PATCH] [clang.py] Implement Type.is_function_variadic git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150936 91177308-0d34-0410-b5e6-96231b3b80d8 --- bindings/python/clang/cindex.py | 10 ++++ bindings/python/tests/cindex/test_type.py | 63 ++++++++++++++++------- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py index d38eb1bfec7..d01d1db31a1 100644 --- a/bindings/python/clang/cindex.py +++ b/bindings/python/clang/cindex.py @@ -1205,6 +1205,12 @@ class Type(Structure): """ return Type_is_restrict_qualified(self) + def is_function_variadic(self): + """Determine whether this function Type is a variadic function type.""" + assert self.kind == TypeKind.FUNCTIONPROTO + + return Type_is_variadic(self) + def is_pod(self): """Determine whether this Type represents plain old data (POD).""" return Type_is_pod(self) @@ -1893,6 +1899,10 @@ Type_is_pod = lib.clang_isPODType Type_is_pod.argtypes = [Type] Type_is_pod.restype = bool +Type_is_variadic = lib.clang_isFunctionTypeVariadic +Type_is_variadic.argtypes = [Type] +Type_is_variadic.restype = bool + Type_get_pointee = lib.clang_getPointeeType Type_get_pointee.argtypes = [Type] Type_get_pointee.restype = Type diff --git a/bindings/python/tests/cindex/test_type.py b/bindings/python/tests/cindex/test_type.py index ba6af56e8b7..c4869fdd5a4 100644 --- a/bindings/python/tests/cindex/test_type.py +++ b/bindings/python/tests/cindex/test_type.py @@ -1,7 +1,6 @@ from clang.cindex import CursorKind from clang.cindex import Index from clang.cindex import TypeKind -from nose.tools import ok_ from nose.tools import raises kInput = """\ @@ -21,9 +20,18 @@ struct teststruct { """ -def test_a_struct(): +def get_tu(source=kInput, lang='c'): + name = 't.c' + if lang == 'cpp': + name = 't.cpp' + index = Index.create() - tu = index.parse('t.c', unsaved_files = [('t.c',kInput)]) + tu = index.parse(name, unsaved_files=[(name, source)]) + assert tu is not None + return tu + +def test_a_struct(): + tu = get_tu(kInput) for n in tu.cursor.get_children(): if n.spelling == 'teststruct': @@ -86,8 +94,7 @@ struct teststruct { }; """ def testConstantArray(): - index = Index.create() - tu = index.parse('t.c', unsaved_files = [('t.c',constarrayInput)]) + tu = get_tu(constarrayInput) for n in tu.cursor.get_children(): if n.spelling == 'teststruct': @@ -103,9 +110,7 @@ def testConstantArray(): assert False, "Didn't find teststruct??" def test_is_pod(): - index = Index.create() - tu = index.parse('t.c', unsaved_files=[('t.c', 'int i; void f();')]) - assert tu is not None + tu = get_tu('int i; void f();') i, f = None, None for cursor in tu.cursor.get_children(): @@ -120,24 +125,48 @@ def test_is_pod(): assert i.type.is_pod() assert not f.type.is_pod() -def test_element_type(): - index = Index.create() - tu = index.parse('t.c', unsaved_files=[('t.c', 'int i[5];')]) - assert tu is not None +def test_function_variadic(): + """Ensure Type.is_function_variadic works.""" + + source =""" +#include <stdarg.h> + +void foo(int a, ...); +void bar(int a, int b); +""" + + tu = get_tu(source) + foo, bar = None, None + for cursor in tu.cursor.get_children(): + if cursor.spelling == 'foo': + foo = cursor + elif cursor.spelling == 'bar': + bar = cursor + + assert foo is not None + assert bar is not None + assert isinstance(foo.type.is_function_variadic(), bool) + assert foo.type.is_function_variadic() + assert not bar.type.is_function_variadic() + +def test_element_type(): + tu = get_tu('int i[5];') + i = None for cursor in tu.cursor.get_children(): if cursor.spelling == 'i': i = cursor break + assert i is not None + assert i.type.kind == TypeKind.CONSTANTARRAY assert i.type.element_type.kind == TypeKind.INT @raises(Exception) def test_invalid_element_type(): """Ensure Type.element_type raises if type doesn't have elements.""" - index = Index.create() - tu = index.parse('t.c', unsaved_files=[('t.c', 'int i;')]) + tu = get_tu('int i;') i = None for cursor in tu.cursor.get_children(): @@ -145,13 +174,11 @@ def test_invalid_element_type(): i = cursor break - ok_(i is not None) + assert i is not None i.element_type def test_element_count(): - index = Index.create() - tu = index.parse('t.c', unsaved_files=[('t.c', 'int i[5]; int j;')]) - assert tu is not None + tu = get_tu('int i[5]; int j;') for cursor in tu.cursor.get_children(): if cursor.spelling == 'i': -- GitLab