[cfe-commits] r94394 - in /cfe/trunk/bindings/python: clang/cindex.py tests/cindex/INPUTS/parse_arguments.c tests/cindex/test_translation_unit.py

Daniel Dunbar daniel at zuster.org
Sun Jan 24 16:43:31 PST 2010


Author: ddunbar
Date: Sun Jan 24 18:43:31 2010
New Revision: 94394

URL: http://llvm.org/viewvc/llvm-project?rev=94394&view=rev
Log:
cindex/Python: Fix/simplify Index.parse() passing command line arguments.

Added:
    cfe/trunk/bindings/python/tests/cindex/INPUTS/parse_arguments.c
Modified:
    cfe/trunk/bindings/python/clang/cindex.py
    cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py

Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=94394&r1=94393&r2=94394&view=diff

==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Sun Jan 24 18:43:31 2010
@@ -73,39 +73,6 @@
     else:
         return cdll.LoadLibrary('libCIndex.so')
 
-## Utility Types and Functions ##
-def alloc_string_vector(strs):
-    """
-    Allocate a string buffer large enough to accommodate the given list of
-    python strings.
-    """
-    n = 0
-    for i in strs: n += len(i) + 1
-    return create_string_buffer(n)
-
-def copy_string_vector(vec, strs):
-    """
-    Copy the contents of each string into the vector, preserving null
-    terminated elements.
-    """
-    n = 0
-    for i in strs:
-        # This is terribly inefficient, but I can't figure out how to copy a
-        # chunk of characters into the resultant vector. t should be: something
-        # like this: vec[n:n + len(i)] = i[:]; n += len(i) + 1
-        for j in i:
-            vec[n] = j
-            n += 1
-        n += 1
-
-def create_string_vector(strs):
-    """
-    Create a string vector (char *[]) from the given list of strings.
-    """
-    vec = alloc_string_vector(strs)
-    copy_string_vector(vec, strs)
-    return vec
-
 # ctypes doesn't implicitly convert c_void_p to the appropriate wrapper
 # object. This is a problem, because it means that from_parameter will see an
 # integer and pass the wrong value on platforms where int != void*. Work around
@@ -571,14 +538,20 @@
         return TranslationUnit(ptr) if ptr else None
 
     @staticmethod
-    def parse(ix, path, args = []):
+    def parse(ix, path, args = [], unsaved_files = []):
         """
-        Construct a translation unit from the given source file, applying
+        Construct a translation unit from the given source file, using
         the given command line argument.
         """
         # TODO: Support unsaved files.
-        argc, argv = len(args), create_string_vector(args)
-        ptr = TranslationUnit_parse(ix, path, argc, byref(argv), 0, 0)
+        arg_array = 0
+        if len(args):
+            arg_array = (c_char_p * len(args))(* args)
+        unsaved_files_array = 0
+        if len(unsaved_files):
+            raise NotImplementedError,'Unsaved files not yet implemented.'
+        ptr = TranslationUnit_parse(ix, path, len(args), arg_array,
+                                    len(unsaved_files), unsaved_files_array)
         return TranslationUnit(ptr) if ptr else None
 
 class File(ClangObject):

Added: cfe/trunk/bindings/python/tests/cindex/INPUTS/parse_arguments.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/INPUTS/parse_arguments.c?rev=94394&view=auto

==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/INPUTS/parse_arguments.c (added)
+++ cfe/trunk/bindings/python/tests/cindex/INPUTS/parse_arguments.c Sun Jan 24 18:43:31 2010
@@ -0,0 +1,2 @@
+int DECL_ONE = 1;
+int DECL_TWO = 2;

Modified: cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py?rev=94394&r1=94393&r2=94394&view=diff

==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py Sun Jan 24 18:43:31 2010
@@ -16,3 +16,11 @@
     c = tu.cursor
     assert isinstance(c, Cursor)
     assert c.kind is CursorKind.TRANSLATION_UNIT
+
+def test_parse_arguments():
+    path = os.path.join(kInputsDir, 'parse_arguments.c')
+    index = Index.create()
+    tu = index.parse(path, ['-DDECL_ONE=hello', '-DDECL_TWO=hi'])
+    spellings = [c.spelling for c in tu.cursor.get_children()]
+    assert spellings[-2] == 'hello'
+    assert spellings[-1] == 'hi'





More information about the cfe-commits mailing list