[PATCH] SourceRange.__contains__ may return wrong result in python bindings

Tao Bao tbao2011 at gmail.com
Thu Feb 12 18:08:19 PST 2015


Adding tests for the change.


http://reviews.llvm.org/D7277

Files:
  bindings/python/clang/cindex.py
  bindings/python/tests/cindex/test_location.py

Index: bindings/python/clang/cindex.py
===================================================================
--- bindings/python/clang/cindex.py
+++ bindings/python/clang/cindex.py
@@ -279,6 +279,10 @@
         # same file, in between lines
         if self.start.line < other.line < self.end.line:
             return True
+        # same file, same line
+        elif self.start.line == other.line == self.end.line:
+            if self.start.column <= other.column <= self.end.column:
+                return True
         elif self.start.line == other.line:
             # same file first line
             if self.start.column <= other.column:
Index: bindings/python/tests/cindex/test_location.py
===================================================================
--- bindings/python/tests/cindex/test_location.py
+++ bindings/python/tests/cindex/test_location.py
@@ -93,3 +93,34 @@
     location3 = SourceLocation.from_position(tu, file, 1, 6)
     range3 = SourceRange.from_locations(location1, location3)
     assert range1 != range3
+
+def test_contains():
+    tu = get_tu(baseInput)
+    one = get_cursor(tu, 'one')
+    two = get_cursor(tu, 'two')
+
+    assert one.location in one.extent
+    assert two.location in two.extent
+    assert one.location not in two.extent
+    assert two.location not in one.extent
+
+    # adding a new line that has two statements
+    tu = get_tu(baseInput + "int three; int four;\n")
+    three = get_cursor(tu, 'three');
+    four = get_cursor(tu, 'four');
+
+    assert three.location in three.extent
+    assert four.location in four.extent
+    assert three.location not in four.extent
+    assert four.location not in three.extent
+
+    file = File.from_name(tu, 't.c')
+    # last character ';' of 'int three;'
+    location1 = SourceLocation.from_position(tu, file, 3, 10)
+    # 'f' in "int four;"
+    location2 = SourceLocation.from_position(tu, file, 3, 16)
+
+    assert location1 in three.extent
+    assert location1 not in four.extent
+    assert location2 in four.extent
+    assert location2 not in three.extent

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7277.19865.patch
Type: text/x-patch
Size: 2077 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150213/cdf6d597/attachment.bin>


More information about the cfe-commits mailing list