- Notifications
You must be signed in to change notification settings - Fork 15.3k
[libclang/python] Fix bug in SourceRange.__contains__, add tests #101802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| @llvm/pr-subscribers-clang Author: Jannick Kremer (DeinAlptraum) ChangesResolves #22617 Full diff: https://github.com/llvm/llvm-project/pull/101802.diff 2 Files Affected:
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py index c251c46a04adf..5fd7cc6481073 100644 --- a/clang/bindings/python/clang/cindex.py +++ b/clang/bindings/python/clang/cindex.py @@ -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: + 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: diff --git a/clang/bindings/python/tests/cindex/test_source_range.py b/clang/bindings/python/tests/cindex/test_source_range.py new file mode 100644 index 0000000000000..9f76848e89020 --- /dev/null +++ b/clang/bindings/python/tests/cindex/test_source_range.py @@ -0,0 +1,56 @@ +import unittest + +from clang.cindex import SourceLocation, SourceRange + +from .util import get_tu + + +def create_location(tu, line, column): + return SourceLocation.from_position(tu, tu.get_file(tu.spelling), line, column) + + +def create_range(tu, line1, column1, line2, column2): + return SourceRange.from_locations( + create_location(tu, line1, column1), create_location(tu, line2, column2) + ) + + +class TestSourceRange(unittest.TestCase): + def test_contains(self): + tu = get_tu( + """aaaaa +aaaaa +aaaaa +aaaaa""" + ) + + l13 = create_location(tu, 1, 3) + l21 = create_location(tu, 2, 1) + l22 = create_location(tu, 2, 2) + l23 = create_location(tu, 2, 3) + l24 = create_location(tu, 2, 4) + l25 = create_location(tu, 2, 5) + l33 = create_location(tu, 3, 3) + l31 = create_location(tu, 3, 1) + r22_24 = create_range(tu, 2, 2, 2, 4) + r23_23 = create_range(tu, 2, 3, 2, 3) + r24_32 = create_range(tu, 2, 4, 3, 2) + r14_32 = create_range(tu, 1, 4, 3, 2) + + assert l13 not in r22_24 # Line before start + assert l21 not in r22_24 # Column before start + assert l22 in r22_24 # Colum on start + assert l23 in r22_24 # Column in range + assert l24 in r22_24 # Column on end + assert l25 not in r22_24 # Column after end + assert l33 not in r22_24 # Line after end + + assert l23 in r23_23 # In one-column range + + assert l23 not in r24_32 # Outside range in first line + assert l33 not in r24_32 # Outside range in last line + assert l25 in r24_32 # In range in first line + assert l31 in r24_32 # In range in last line + + assert l21 in r14_32 # In range at start of center line + assert l25 in r14_32 # In range at end of center line |
| ✅ With the latest revision this PR passed the Python code formatter. |
0ba52dc to f345048 Compare Use this to implement the operator on Python side
Endilll left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, this is moving in the right direction.
You should also add an entry to potentially breaking section of release notes, because according to Hyrum's law, someone for sure depends on the current behavior of SourceLocation.__contains__.
Also add breaking Release Note
Endilll left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add my earlier example with #include to the tests?
| Implemented all your suggestions |
Endilll left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is good to go, thank you!
@AaronBallman can you also take a look? We discussed this during office hours, so you know the context.
| ✅ With the latest revision this PR passed the C/C++ code formatter. |
AaronBallman left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM aside from a minor request for the release notes; thank you!
Resolves #22617
Resolves #52827