Skip to content

Conversation

@DeinAlptraum
Copy link
Contributor

@DeinAlptraum DeinAlptraum commented Aug 3, 2024

Resolves #22617
Resolves #52827

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Aug 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 3, 2024

@llvm/pr-subscribers-clang

Author: Jannick Kremer (DeinAlptraum)

Changes

Resolves #22617
Resolves #52827


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

2 Files Affected:

  • (modified) clang/bindings/python/clang/cindex.py (+4)
  • (added) clang/bindings/python/tests/cindex/test_source_range.py (+56)
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 
@DeinAlptraum DeinAlptraum added the clang:as-a-library libclang and C++ API label Aug 3, 2024
@DeinAlptraum DeinAlptraum requested a review from Endilll August 3, 2024 09:38
@github-actions
Copy link

github-actions bot commented Aug 7, 2024

✅ With the latest revision this PR passed the Python code formatter.

@DeinAlptraum DeinAlptraum force-pushed the bugfix branch 3 times, most recently from 0ba52dc to f345048 Compare August 8, 2024 18:42
Use this to implement the operator on Python side
Copy link
Contributor

@Endilll Endilll left a 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__.

Copy link
Contributor

@Endilll Endilll left a 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?

@llvmbot llvmbot added the clang:frontend Language frontend issues, e.g. anything involving "Sema" label Aug 15, 2024
@DeinAlptraum
Copy link
Contributor Author

Implemented all your suggestions

Copy link
Contributor

@Endilll Endilll left a 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.

@github-actions
Copy link

github-actions bot commented Aug 15, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@AaronBallman AaronBallman left a 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!

@DeinAlptraum DeinAlptraum merged commit c5b611a into llvm:main Aug 15, 2024
@DeinAlptraum DeinAlptraum deleted the bugfix branch August 15, 2024 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:as-a-library libclang and C++ API clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

4 participants