From f498e00a30ca7fdaf4f49e778862f4cf84ffab2a Mon Sep 17 00:00:00 2001 From: Tobias Grosser Date: Sat, 5 Feb 2011 17:53:51 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124953 91177308-0d34-0410-b5e6-96231b3b80d8 --- bindings/python/clang/cindex.py | 2 ++ .../python/tests/cindex/test_diagnostics.py | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+) 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 + + -- 2.40.0