[cfe-dev] [bug][libclang][python] SourceRange.__contains__() returns incorrect value for SourceLocation on same line
Jacob Faibussowitsch via cfe-dev
cfe-dev at lists.llvm.org
Thu Jan 13 07:45:09 PST 2022
Hello Clang Developers,
Apologies if this is not the right place to send this, please feel free to redirect me!
The Bug:
The Python implementation of SourceRange.__contains__() gives the wrong answer for a SourceLocation on the same line that is strictly *after* the SourceRange
Reproducer:
I’ve attached the full reproducer with this mail, but consider the following snippets
my_file = """
int main()
{
int x = 2;
return x;
}
"""
# 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
Fix:
Problematic line here is cindex.py:356
elif self.start.line == other.line:
# same file first line
if self.start.column <= other.column: # should check that self.end.column >= other.column
return True
But IMO (unless I am missing something) this implementation seems overly complicated. Can it not just check that:
self.start.offset <= other.offset <= self.endoffset
? This would also allow SourceRange.__contains__() to handle other SourceRange (which it currently just returns a flat False for):
if isinstance(other,SourceRange):
def contains(self, loc):
return self.st <http://self.st/>art.offset <= loc.offset <= self.end.offset
return contains(self, other.start) and contains(self, other.end)
Best regards,
Jacob Faibussowitsch
(Jacob Fai - booss - oh - vitch)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20220113/8e67162f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: libclangbug.py
Type: text/x-python-script
Size: 1054 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20220113/8e67162f/attachment.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20220113/8e67162f/attachment-0001.html>
More information about the cfe-dev
mailing list