[cfe-commits] r94396 - in /cfe/trunk/bindings/python: clang/cindex.py tests/cindex/test_translation_unit.py

Daniel Dunbar daniel at zuster.org
Sun Jan 24 16:44:11 PST 2010


Author: ddunbar
Date: Sun Jan 24 18:44:11 2010
New Revision: 94396

URL: http://llvm.org/viewvc/llvm-project?rev=94396&view=rev
Log:
cindex/Python: Implement support for unsaved/remapped files.

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=94396&r1=94395&r2=94396&view=diff

==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Sun Jan 24 18:44:11 2010
@@ -479,6 +479,11 @@
     def from_param(self):
         return self._as_parameter_
 
+
+class _CXUnsavedFile(Structure):
+    """Helper for passing unsaved file arguments."""
+    _fields_ = [("name", c_char_p), ("contents", c_char_p), ('length', c_ulong)]
+
 class Index(ClangObject):
     """
     The Index type provides the primary interface to the Clang CIndex library,
@@ -503,13 +508,18 @@
         """Load the translation unit from the given AST file."""
         return TranslationUnit.read(self, path)
 
-    def parse(self, path, args = []):
+    def parse(self, path, args = [], unsaved_files = []):
         """
         Load the translation unit from the given source code file by running
         clang and generating the AST before loading. Additional command line
         parameters can be passed to clang via the args parameter.
+
+        In-memory contents for files can be provided by passing a list of pairs
+        to as unsaved_files, the first item 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.
         """
-        return TranslationUnit.parse(self, path, args)
+        return TranslationUnit.parse(self, path, args, unsaved_files)
 
 
 class TranslationUnit(ClangObject):
@@ -549,7 +559,12 @@
             arg_array = (c_char_p * len(args))(* args)
         unsaved_files_array = 0
         if len(unsaved_files):
-            raise NotImplementedError,'Unsaved files not yet implemented.'
+            unsaved_files_array = (_CXUnsavedFile * len(unsaved_files))()
+            for i,(name,value) in enumerate(unsaved_files):
+                # FIXME: Support file objects.
+                unsaved_files_array[i].name = name
+                unsaved_files_array[i].contents = value
+                unsaved_files_array[i].length = len(value)
         ptr = TranslationUnit_parse(ix, path, len(args), arg_array,
                                     len(unsaved_files), unsaved_files_array)
         return TranslationUnit(ptr) if ptr else None

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=94396&r1=94395&r2=94396&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:44:11 2010
@@ -24,3 +24,20 @@
     spellings = [c.spelling for c in tu.cursor.get_children()]
     assert spellings[-2] == 'hello'
     assert spellings[-1] == 'hi'
+
+def test_unsaved_files():
+    index = Index.create()
+    # FIXME: Why can't we just use "fake.h" here (instead of /tmp/fake.h)?
+    tu = index.parse('fake.c', unsaved_files = [
+            ('fake.c', """
+#include "/tmp/fake.h"
+int x;
+int SOME_DEFINE;
+"""),
+            ('/tmp/fake.h', """
+#define SOME_DEFINE y
+""")
+            ])
+    spellings = [c.spelling for c in tu.cursor.get_children()]
+    assert spellings[-2] == 'x'
+    assert spellings[-1] == 'y'





More information about the cfe-commits mailing list