[PATCH] python bindings: Add support for translationUnit.reparse().
This is the first step to make the clang_complete vim plugin work with
libclang. Reparsing improves parsing time from 0.8 to 0.25 secs for
one of my LLVM .cpp files. Using libclang instead of creating a separate
file did obviously improve runtime too.
OK to commit?
Tobias
>From 4bad5d3a172578a9e9906b4fed412f6aeb8438f5 Mon Sep 17 00:00:00 2001
From: Tobias Grosser <[email protected]>
Date: Tue, 14 Dec 2010 13:15:59 -0500
Subject: [PATCH] python bindings: Add support for translationUnit.reparse().
This is the first step to make the clang_complete vim plugin work with
libclang. Reparsing improves parsing time from 0.8 to 0.25 secs for
one of my LLVM .cpp files.
---
bindings/python/clang/cindex.py | 44 +++++++++++++++++++++++++++++++++-----
1 files changed, 38 insertions(+), 6 deletions(-)
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index f0f81b5..7c0fb75 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -650,7 +650,7 @@ class Index(ClangObject):
ptr = TranslationUnit_read(self, path)
return TranslationUnit(ptr) if ptr else None
- def parse(self, path, args = [], unsaved_files = []):
+ def parse(self, path, args = [], unsaved_files = [], options = 0):
"""
Load the translation unit from the given source code file by running
clang and generating the AST before loading. Additional command line
@@ -678,8 +678,9 @@ class Index(ClangObject):
unsaved_files_array[i].name = name
unsaved_files_array[i].contents = value
unsaved_files_array[i].length = len(value)
- ptr = TranslationUnit_parse(self, path, len(args), arg_array,
- len(unsaved_files), unsaved_files_array)
+ ptr = TranslationUnit_parse(self, path, arg_array, len(args),
+ unsaved_files_array, len(unsaved_files),
+ options)
return TranslationUnit(ptr) if ptr else None
@@ -744,6 +745,33 @@ class TranslationUnit(ClangObject):
return DiagIterator(self)
+ def reparse(self, unsaved_files = [], options = 0):
+ """
+ Reparse an already parsed translation unit.
+
+ In-memory contents for files can be provided by passing a list of pairs
+ as unsaved_files, the first items should be the filenames to be mapped
+ and the second should be the contents to be substituted for the
+ file. The contents may be passed as strings or file objects.
+ """
+ unsaved_files_array = 0
+ if len(unsaved_files):
+ unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
+ for i,(name,value) in enumerate(unsaved_files):
+ if not isinstance(value, str):
+ # FIXME: It would be great to support an efficient version
+ # of this, one day.
+ value = value.read()
+ print value
+ if not isinstance(value, str):
+ raise TypeError,'Unexpected unsaved file contents.'
+ unsaved_files_array[i].name = name
+ unsaved_files_array[i].contents = value
+ unsaved_files_array[i].length = len(value)
+ ptr = TranslationUnit_reparse(self, len(unsaved_files),
+ unsaved_files_array,
+ options)
+
class File(ClangObject):
"""
The File class represents a particular source file that is part of a
@@ -893,11 +921,15 @@ TranslationUnit_read = lib.clang_createTranslationUnit
TranslationUnit_read.argtypes = [Index, c_char_p]
TranslationUnit_read.restype = c_object_p
-TranslationUnit_parse = lib.clang_createTranslationUnitFromSourceFile
-TranslationUnit_parse.argtypes = [Index, c_char_p, c_int, c_void_p,
- c_int, c_void_p]
+TranslationUnit_parse = lib.clang_parseTranslationUnit
+TranslationUnit_parse.argtypes = [Index, c_char_p, c_void_p,
+ c_int, c_void_p, c_int, c_int]
TranslationUnit_parse.restype = c_object_p
+TranslationUnit_reparse = lib.clang_reparseTranslationUnit
+TranslationUnit_reparse.argtypes = [TranslationUnit, c_int, c_void_p, c_int]
+TranslationUnit_reparse.restype = c_int
+
TranslationUnit_cursor = lib.clang_getTranslationUnitCursor
TranslationUnit_cursor.argtypes = [TranslationUnit]
TranslationUnit_cursor.restype = Cursor
--
1.7.1
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits