diff options
-rw-r--r-- | bindings/python/clang/cindex.py | 2 | ||||
-rw-r--r-- | bindings/python/tests/cindex/test_diagnostics.py | 23 |
2 files changed, 25 insertions, 0 deletions
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py index c398656a0a..ee0abd6315 100644 --- a/bindings/python/clang/cindex.py +++ b/bindings/python/clang/cindex.py @@ -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) diff --git a/bindings/python/tests/cindex/test_diagnostics.py b/bindings/python/tests/cindex/test_diagnostics.py index 8518765291..33876cfd20 100644 --- a/bindings/python/tests/cindex/test_diagnostics.py +++ b/bindings/python/tests/cindex/test_diagnostics.py @@ -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 + + |