python bindings: fix Diagnostics.range iterator

The iterator did never throw an IndexError. It was therefore not possible
to use it in a normal foreach loop as that loop would never stop.

llvm-svn: 124953
This commit is contained in:
Tobias Grosser 2011-02-05 17:53:51 +00:00
parent 82c18a502f
commit 5153e79931
2 changed files with 25 additions and 0 deletions

View File

@ -215,6 +215,8 @@ class Diagnostic(object):
return int(_clang_getDiagnosticNumRanges(self.diag))
def __getitem__(self, key):
if (key >= len(self)):
raise IndexError
return _clang_getDiagnosticRange(self.diag, key)
return RangeIterator(self)

View File

@ -46,3 +46,26 @@ def test_diagnostic_fixit():
assert tu.diagnostics[0].fixits[0].range.end.line == 1
assert tu.diagnostics[0].fixits[0].range.end.column == 30
assert tu.diagnostics[0].fixits[0].value == '.f0 = '
def test_diagnostic_range():
index = Index.create()
tu = tu_from_source("""void f() { int i = "a" + 1; }""")
assert len(tu.diagnostics) == 1
assert tu.diagnostics[0].severity == Diagnostic.Warning
assert tu.diagnostics[0].location.line == 1
assert tu.diagnostics[0].location.column == 16
assert tu.diagnostics[0].spelling.startswith('incompatible pointer to')
assert len(tu.diagnostics[0].fixits) == 0
assert len(tu.diagnostics[0].ranges) == 1
assert tu.diagnostics[0].ranges[0].start.line == 1
assert tu.diagnostics[0].ranges[0].start.column == 20
assert tu.diagnostics[0].ranges[0].end.line == 1
assert tu.diagnostics[0].ranges[0].end.column == 27
try:
tu.diagnostics[0].ranges[1].start.line
except IndexError:
assert True
else:
assert False