From ec67b5e56f47faec996ccd479bd517f63cc08ec2 Mon Sep 17 00:00:00 2001
From: Gregory Szorc <gregory.szorc@gmail.com>
Date: Sat, 18 Feb 2012 19:53:43 -0800
Subject: [PATCH 1/4] [clang.py] Implement Type.is_function_variadic

---
 bindings/python/clang/cindex.py           |   10 +++++
 bindings/python/tests/cindex/test_type.py |   56 +++++++++++++++++++++--------
 2 files changed, 51 insertions(+), 15 deletions(-)

diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index d38eb1b..d01d1db 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -1200,16 +1200,22 @@ class Type(Structure):
     def is_restrict_qualified(self):
         """
         Determine whether a Type has the "restrict" qualifier set,
         without looking through typedefs that may have added
         "restrict" at a different level.
         """
         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)
 
     def get_pointee(self):
         """
         For pointer types, returns the type of the pointee.
         """
@@ -1888,16 +1894,20 @@ Type_is_volatile_qualified.restype = bool
 Type_is_restrict_qualified = lib.clang_isRestrictQualifiedType
 Type_is_restrict_qualified.argtypes = [Type]
 Type_is_restrict_qualified.restype = bool
 
 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
 Type_get_pointee.errcheck = Type.from_result
 
 Type_get_declaration = lib.clang_getTypeDeclaration
 Type_get_declaration.argtypes = [Type]
 Type_get_declaration.restype = Cursor
diff --git a/bindings/python/tests/cindex/test_type.py b/bindings/python/tests/cindex/test_type.py
index ba6af56..81fbc7b 100644
--- a/bindings/python/tests/cindex/test_type.py
+++ b/bindings/python/tests/cindex/test_type.py
@@ -16,19 +16,28 @@ struct teststruct {
   signed long e;
   const int f;
   int *g;
   int ***h;
 };
 
 """
 
-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)])
+    ok_(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':
             fields = list(n.get_children())
 
             assert all(x.kind == CursorKind.FIELD_DECL for x in fields)
 
             assert fields[0].spelling == 'a'
@@ -81,82 +90,99 @@ def test_a_struct():
 
 
 constarrayInput="""
 struct teststruct {
   void *A[2];
 };
 """
 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':
             fields = list(n.get_children())
             assert fields[0].spelling == 'A'
             assert fields[0].type.kind == TypeKind.CONSTANTARRAY
             assert fields[0].type.get_array_element_type() is not None
             assert fields[0].type.get_array_element_type().kind == TypeKind.POINTER
             assert fields[0].type.get_array_size() == 2
 
             break
     else:
         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():
         if cursor.spelling == 'i':
             i = cursor
         elif cursor.spelling == 'f':
             f = cursor
 
     assert i is not None
     assert f is not None
 
     assert i.type.is_pod()
     assert not f.type.is_pod()
 
+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
+
+    ok_(foo is not None)
+    ok_(bar is not None)
+
+    ok_(isinstance(foo.type.is_function_variadic(), bool))
+    ok_(foo.type.is_function_variadic())
+    ok_(not bar.type.is_function_variadic())
+
 def test_element_type():
-    index = Index.create()
-    tu = index.parse('t.c', unsaved_files=[('t.c', 'int i[5];')])
-    assert tu is not None
+    tu = get_tu('int i[5];')
 
     for cursor in tu.cursor.get_children():
         if cursor.spelling == 'i':
             i = cursor
             break
 
     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():
         if cursor.spelling == 'i':
             i = cursor
             break
 
     ok_(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':
             i = cursor
         elif cursor.spelling == 'j':
             j = cursor
 
     assert i is not None
-- 
1.7.8.2

