Skip to content

Commit

Permalink
python: add bindings for children of diagnostics
Browse files Browse the repository at this point in the history
This exposes the Clang API bindings clang_getChildDiagnostics (which returns a
CXDiagnosticSet) and clang_getNumDiagnosticsInSet / clang_getDiagnosticInSet (to
traverse the CXDiagnosticSet), and adds a helper children property in the Python
Diagnostic wrapper.

Also, this adds the missing OVERLOAD_CANDIDATE (700) cursor type.

Patch by Hanson Wang!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@268167 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
compnerd committed Apr 30, 2016
1 parent c0875f9 commit 6f153a6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
32 changes: 32 additions & 0 deletions bindings/python/clang/cindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,23 @@ def __getitem__(self, key):

return FixItIterator(self)

@property
def children(self):
class ChildDiagnosticsIterator:
def __init__(self, diag):
self.diag_set = conf.lib.clang_getChildDiagnostics(diag)

def __len__(self):
return int(conf.lib.clang_getNumDiagnosticsInSet(self.diag_set))

def __getitem__(self, key):
diag = conf.lib.clang_getDiagnosticInSet(self.diag_set, key)
if not diag:
raise IndexError
return Diagnostic(diag)

return ChildDiagnosticsIterator(self)

@property
def category_number(self):
"""The category number for this diagnostic or 0 if unavailable."""
Expand Down Expand Up @@ -1120,6 +1137,9 @@ def __repr__(self):
# A type alias template declaration
CursorKind.TYPE_ALIAS_TEMPLATE_DECL = CursorKind(601)

# A code completion overload candidate.
CursorKind.OVERLOAD_CANDIDATE = CursorKind(700)

### Template Argument Kinds ###
class TemplateArgumentKind(BaseEnumeration):
"""
Expand Down Expand Up @@ -3053,6 +3073,10 @@ def cursor(self):
Type,
Type.from_result),

("clang_getChildDiagnostics",
[Diagnostic],
c_object_p),

("clang_getCompletionAvailability",
[c_void_p],
c_int),
Expand Down Expand Up @@ -3173,6 +3197,10 @@ def cursor(self):
_CXString,
_CXString.from_result),

("clang_getDiagnosticInSet",
[c_object_p, c_uint],
c_object_p),

("clang_getDiagnosticLocation",
[Diagnostic],
SourceLocation),
Expand Down Expand Up @@ -3274,6 +3302,10 @@ def cursor(self):
[c_object_p],
c_uint),

("clang_getNumDiagnosticsInSet",
[c_object_p],
c_uint),

("clang_getNumElements",
[Type],
c_longlong),
Expand Down
12 changes: 12 additions & 0 deletions bindings/python/tests/cindex/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,15 @@ def test_diagnostic_option():

assert d.option == '-Wunused-parameter'
assert d.disable_option == '-Wno-unused-parameter'

def test_diagnostic_children():
tu = get_tu('void f(int x) {} void g() { f(); }')
assert len(tu.diagnostics) == 1
d = tu.diagnostics[0]

children = d.children
assert len(children) == 1
assert children[0].severity == Diagnostic.Note
assert children[0].spelling.endswith('declared here')
assert children[0].location.line == 1
assert children[0].location.column == 1

0 comments on commit 6f153a6

Please sign in to comment.