r177408 - cindex.py: Handle NULL pointers when parsing translation units

Tobias Grosser grosser at fim.uni-passau.de
Tue Mar 19 08:30:48 PDT 2013


Author: grosser
Date: Tue Mar 19 10:30:48 2013
New Revision: 177408

URL: http://llvm.org/viewvc/llvm-project?rev=177408&view=rev
Log:
cindex.py: Handle NULL pointers when parsing translation units

The code inside cindex.py was comparing NULL pointer returned by
clang_parseTranslationUnit and clang_createTranslationUnit with None.
However, as illustrated by the two tests I've added, those conditions
were ineffective which resulted in assert triggering later on.

Instead, a pointer is now treated as a boolean value, a NULL pointer being
False.

Contributed-by: Xavier Deguillard <deguilx at gmail.com>

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=177408&r1=177407&r2=177408&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Tue Mar 19 10:30:48 2013
@@ -2015,7 +2015,7 @@ class TranslationUnit(ClangObject):
                                     len(args), unsaved_array,
                                     len(unsaved_files), options)
 
-        if ptr is None:
+        if not ptr:
             raise TranslationUnitLoadError("Error parsing translation unit.")
 
         return cls(ptr, index=index)
@@ -2037,7 +2037,7 @@ class TranslationUnit(ClangObject):
             index = Index.create()
 
         ptr = conf.lib.clang_createTranslationUnit(index, filename)
-        if ptr is None:
+        if not ptr:
             raise TranslationUnitLoadError(filename)
 
         return cls(ptr=ptr, index=index)

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=177408&r1=177407&r2=177408&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_translation_unit.py Tue Mar 19 10:30:48 2013
@@ -8,6 +8,7 @@ from clang.cindex import Index
 from clang.cindex import SourceLocation
 from clang.cindex import SourceRange
 from clang.cindex import TranslationUnitSaveError
+from clang.cindex import TranslationUnitLoadError
 from clang.cindex import TranslationUnit
 from .util import get_cursor
 from .util import get_tu
@@ -239,3 +240,19 @@ def test_get_tokens_gc():
     del tokens
     gc.collect()
     gc.collect() # Just in case.
+
+def test_fail_from_source():
+    path = os.path.join(kInputsDir, 'non-existent.cpp')
+    try:
+        tu = TranslationUnit.from_source(path)
+    except TranslationUnitLoadError:
+        tu = None
+    assert tu == None
+
+def test_fail_from_ast_file():
+    path = os.path.join(kInputsDir, 'non-existent.ast')
+    try:
+        tu = TranslationUnit.from_ast_file(path)
+    except TranslationUnitLoadError:
+        tu = None
+    assert tu == None





More information about the cfe-commits mailing list