Issue 52827
Summary [LibClang][Python Bindings] SourceRange.__contains__() returns incorrect value for SourceLocation on same line
Labels new issue
Assignees
Reporter Jacobfaib
    To reproduce
-------------
```
my_file = """
int main()
{
  int x = 2;
  return x;
}
"""
file_name = 'test.c'
index     = clx.Index.create()
tu        = index.parse(file_name,unsaved_files=[(file_name,my_file)])
# make sourceRange for 'x' in 'int x = 2;'
src_rng   = clx.SourceRange.from_locations(
  clx.SourceLocation.from_position(tu,tu.get_file(tu.spelling),4,7),
  clx.SourceLocation.from_position(tu,tu.get_file(tu.spelling),4,8)
)
# make sourceLocation for '2' in 'int x = 2;'
src_loc   = clx.SourceLocation.from_position(tu,tu.get_file(tu.spelling),4,10)
# to recap, src_loc is definitely not inside src_rng:
#
# int x = 2;
#     ~~  ~ -- src_loc
#      |
#   src_rng
#
# but the following assertion will fail
assert src_loc not in src_rng
```

The fix
---------
```
--- cindex.py	2021-12-21 18:15:45.000000000 -0500
+++ cindex.py 2021-12-21 18:16:16.000000000 -0500
@@ -353,7 +353,7 @@
             return True
         elif self.start.line == other.line:
             # same file first line
-            if self.start.column <= other.column:
+            if self.start.column <= other.column <= self.end.column:
                 return True
         elif other.line == self.end.line:
             # same file last line
```

Version info
------------
```
$ pip show clang
Name: clang
Version: 11.0
Summary: libclang python bindings
Home-page: http://clang.llvm.org/
Author: LLVM team - pypi upload by Loic Jaquemet
Author-email: UNKNOWN
License: License :: OSI Approved :: University of Illinois/NCSA Open Source License
Location: /usr/local/lib/python3.9/site-packages
Requires:
Required-by:
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to