[clang] [libclang/python] Fix bug in `SourceRange.__contains__`, add tests (PR #101802)

Jannick Kremer via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 7 13:36:04 PDT 2024


================
@@ -386,6 +386,10 @@ def __contains__(self, other):
         # same file, in between lines
         if self.start.line < other.line < self.end.line:
             return True
+        # between columns in one-liner range
+        elif self.start.line == other.line == self.end.line:
----------------
DeinAlptraum wrote:

`SourceRange::fullyContains` implements the check via `operator<=`, which is the most straightforward way, and since the intention is not really to expand the interface but we're just adding the operator to simplify `SourceRange.__contains__` I went with this. Happy to remove this and instead implement `SourceLocation.__lt__` though if you prefer.

That said, the situation is a bit ugly though: we already have `SourceLocation.__eq__` implemented (which just passes directly to the C interface) and that one _is file-sensitive_. I.e. `loc1 == loc2` gives false if their line & column are the same but their file name is different, while `loc1 <= loc2` is `True` in that case. This is already the case in the C interface.
So we can't really use `SourceLocation.__eq__` in `SourceRange.__contains__` because the latter wants to check only if their lines/columns match, disregarding filename, so it works with half-open ranges where there is no end `SourceLocation`.

https://github.com/llvm/llvm-project/pull/101802


More information about the cfe-commits mailing list