<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hello Clang Developers,<div class=""><br class=""></div><div class="">Apologies if this is not the right place to send this, please feel free to redirect me!</div><div class=""><br class=""></div><div class="">The Bug:</div><div class=""><br class=""></div><div class="">The Python implementation of SourceRange.__contains__() gives the wrong answer for a SourceLocation on the same line that is strictly *after* the SourceRange</div><div class=""><br class=""></div><div class="">Reproducer:</div><div class=""><br class=""></div><div class="">I’ve attached the full reproducer with this mail, but consider the following snippets</div><div class=""><pre class=""><code class="">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)
)</code></pre><pre class=""><code class=""># make sourceLocation for '2' in 'int x = 2;'
src_loc = clx.SourceLocation.from_position(tu,tu.get_file(tu.spelling),4,10)<br class=""></code></pre><pre class=""><code class=""># to recap, src_loc is definitely not inside src_rng:
#
# int x = 2;
#     ~~  ~ -- src_loc
#      |
#   src_rng<br class=""></code></pre><pre class=""><code class=""># but the following assertion will fail
assert src_loc not in src_rng
</code></pre><div class="">Fix:</div></div><div class=""><br class=""></div><div class="">Problematic line here is cindex.py:356</div><div class=""><br class=""></div><div class=""><div class="">elif self.start.line == other.line:</div><div class="">  # same file first line</div><div class="">  if self.start.column <= other.column: # should check that self.end.column >= other.column</div><div class="">    return True</div></div><div class=""><br class=""></div><div class="">But IMO (unless I am missing something) this implementation seems overly complicated. Can it not just check that:</div><div class=""><br class=""></div><div class="">self.start.offset <= other.offset <= self.endoffset</div><div class=""><br class=""></div><div class="">? This would also allow SourceRange.__contains__() to handle other SourceRange (which it currently just returns a flat False for):</div><div class=""><br class=""></div><div class="">if isinstance(other,SourceRange):</div><div class="">  def contains(self, loc):</div><div class="">    return <a href="http://self.st" class="">self.st</a>art.offset <= loc.offset <= self.end.offset</div><div class=""><br class=""></div><div class="">  return contains(self, other.start) and contains(self, other.end)</div><div class=""><br class=""></div><div class=""><div class="">
<div dir="auto" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div dir="auto" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div>Best regards,<br class=""><br class="">Jacob Faibussowitsch<br class="">(Jacob Fai - booss - oh - vitch)<br class=""></div></div></div>
</div><div class=""></div></div></body></html>