r312622 - Fix __repr__ for Diagnostic in clang.cindex

Jonathan Coe via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 6 00:33:32 PDT 2017


Author: jbcoe
Date: Wed Sep  6 00:33:32 2017
New Revision: 312622

URL: http://llvm.org/viewvc/llvm-project?rev=312622&view=rev
Log:
Fix __repr__ for Diagnostic in clang.cindex

Summary: Also move misplaced tests for exception specification to fix failing Python tests.

Reviewers: hans, compnerd

Reviewed By: compnerd

Subscribers: cfe-commits

Tags: #clang-c

Differential Revision: https://reviews.llvm.org/D37490

Added:
    cfe/trunk/bindings/python/tests/cindex/test_exception_specification_kind.py
Removed:
    cfe/trunk/bindings/python/tests/test_exception_specification_kind.py
Modified:
    cfe/trunk/bindings/python/clang/cindex.py
    cfe/trunk/bindings/python/tests/cindex/test_diagnostics.py

Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=312622&r1=312621&r2=312622&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Wed Sep  6 00:33:32 2017
@@ -207,7 +207,7 @@ class _CXString(Structure):
         conf.lib.clang_disposeString(self)
 
     @staticmethod
-    def from_result(res, fn, args):
+    def from_result(res, fn=None, args=None):
         assert isinstance(res, _CXString)
         return conf.lib.clang_getCString(res)
 
@@ -459,8 +459,7 @@ class Diagnostic(object):
         """The command-line option that disables this diagnostic."""
         disable = _CXString()
         conf.lib.clang_getDiagnosticOption(self, byref(disable))
-
-        return conf.lib.clang_getCString(disable)
+        return _CXString.from_result(disable)
 
     def format(self, options=None):
         """
@@ -473,8 +472,7 @@ class Diagnostic(object):
             options = conf.lib.clang_defaultDiagnosticDisplayOptions()
         if options & ~Diagnostic._FormatOptionsMask:
             raise ValueError('Invalid format options')
-        formatted = conf.lib.clang_formatDiagnostic(self, options)
-        return conf.lib.clang_getCString(formatted)
+        return conf.lib.clang_formatDiagnostic(self, options)
 
     def __repr__(self):
         return "<Diagnostic severity %r, location %r, spelling %r>" % (

Modified: cfe/trunk/bindings/python/tests/cindex/test_diagnostics.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_diagnostics.py?rev=312622&r1=312621&r2=312622&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_diagnostics.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_diagnostics.py Wed Sep  6 00:33:32 2017
@@ -92,3 +92,11 @@ def test_diagnostic_children():
     assert children[0].spelling.endswith('declared here')
     assert children[0].location.line == 1
     assert children[0].location.column == 1
+
+def test_diagnostic_string_repr():
+    tu = get_tu('struct MissingSemicolon{}')
+    assert len(tu.diagnostics) == 1
+    d = tu.diagnostics[0]
+
+    assert repr(d) == '<Diagnostic severity 3, location <SourceLocation file \'t.c\', line 1, column 26>, spelling "expected \';\' after struct">'
+    

Added: cfe/trunk/bindings/python/tests/cindex/test_exception_specification_kind.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_exception_specification_kind.py?rev=312622&view=auto
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_exception_specification_kind.py (added)
+++ cfe/trunk/bindings/python/tests/cindex/test_exception_specification_kind.py Wed Sep  6 00:33:32 2017
@@ -0,0 +1,27 @@
+import clang.cindex
+from clang.cindex import ExceptionSpecificationKind
+from .util import get_tu
+
+
+def find_function_declarations(node, declarations=[]):
+    if node.kind == clang.cindex.CursorKind.FUNCTION_DECL:
+        declarations.append((node.spelling, node.exception_specification_kind))
+    for child in node.get_children():
+        declarations = find_function_declarations(child, declarations)
+    return declarations
+
+
+def test_exception_specification_kind():
+    source = """int square1(int x);
+                int square2(int x) noexcept;
+                int square3(int x) noexcept(noexcept(x * x));"""
+
+    tu = get_tu(source, lang='cpp', flags=['-std=c++14'])
+
+    declarations = find_function_declarations(tu.cursor)
+    expected = [
+        ('square1', ExceptionSpecificationKind.NONE),
+        ('square2', ExceptionSpecificationKind.BASIC_NOEXCEPT),
+        ('square3', ExceptionSpecificationKind.COMPUTED_NOEXCEPT)
+    ]
+    assert declarations == expected

Removed: cfe/trunk/bindings/python/tests/test_exception_specification_kind.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/test_exception_specification_kind.py?rev=312621&view=auto
==============================================================================
--- cfe/trunk/bindings/python/tests/test_exception_specification_kind.py (original)
+++ cfe/trunk/bindings/python/tests/test_exception_specification_kind.py (removed)
@@ -1,27 +0,0 @@
-import clang.cindex
-from clang.cindex import ExceptionSpecificationKind
-from .util import get_tu
-
-
-def find_function_declarations(node, declarations=[]):
-    if node.kind == clang.cindex.CursorKind.FUNCTION_DECL:
-        declarations.append((node.spelling, node.exception_specification_kind))
-    for child in node.get_children():
-        declarations = find_function_declarations(child, declarations)
-    return declarations
-
-
-def test_exception_specification_kind():
-    source = """int square1(int x);
-                int square2(int x) noexcept;
-                int square3(int x) noexcept(noexcept(x * x));"""
-
-    tu = get_tu(source, lang='cpp', flags=['-std=c++14'])
-
-    declarations = find_function_declarations(tu.cursor)
-    expected = [
-        ('square1', ExceptionSpecificationKind.NONE),
-        ('square2', ExceptionSpecificationKind.BASIC_NOEXCEPT),
-        ('square3', ExceptionSpecificationKind.COMPUTED_NOEXCEPT)
-    ]
-    assert declarations == expected




More information about the cfe-commits mailing list