[cfe-commits] r149824 - in /cfe/trunk/bindings/python: clang/cindex.py tests/cindex/test_location.py
Tobias Grosser
grosser at fim.uni-passau.de
Sun Feb 5 03:40:59 PST 2012
Author: grosser
Date: Sun Feb 5 05:40:59 2012
New Revision: 149824
URL: http://llvm.org/viewvc/llvm-project?rev=149824&view=rev
Log:
[clang.py] Implement __eq__ and __ne__ on SourceLocation and SourceRange
There is no type checking in __eq__, so ctypes will throw if the wrong
Python type is passed in to the C function. Personally, I feel garbage
in means garbage out and it isn't worth testing for this explicitly.
Contributed by: Gregory Szorc <gregory.szorc at gmail.com>
Modified:
cfe/trunk/bindings/python/clang/cindex.py
cfe/trunk/bindings/python/tests/cindex/test_location.py
Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=149824&r1=149823&r2=149824&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Sun Feb 5 05:40:59 2012
@@ -146,6 +146,12 @@
"""Get the file offset represented by this source location."""
return self._get_instantiation()[3]
+ def __eq__(self, other):
+ return SourceLocation_equalLocations(self, other)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
def __repr__(self):
if self.file:
filename = self.file.name
@@ -186,6 +192,12 @@
"""
return SourceRange_end(self)
+ def __eq__(self, other):
+ return SourceRange_equalRanges(self, other)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
def __repr__(self):
return "<SourceRange start %r, end %r>" % (self.start, self.end)
@@ -1613,6 +1625,10 @@
SourceLocation_getLocation.argtypes = [TranslationUnit, File, c_uint, c_uint]
SourceLocation_getLocation.restype = SourceLocation
+SourceLocation_equalLocations = lib.clang_equalLocations
+SourceLocation_equalLocations.argtypes = [SourceLocation, SourceLocation]
+SourceLocation_equalLocations.restype = bool
+
# Source Range Functions
SourceRange_getRange = lib.clang_getRange
SourceRange_getRange.argtypes = [SourceLocation, SourceLocation]
@@ -1626,6 +1642,10 @@
SourceRange_end.argtypes = [SourceRange]
SourceRange_end.restype = SourceLocation
+SourceRange_equalRanges = lib.clang_equalRanges
+SourceRange_equalRanges.argtypes = [SourceRange, SourceRange]
+SourceRange_equalRanges.restype = bool
+
# CursorKind Functions
CursorKind_is_decl = lib.clang_isDeclaration
CursorKind_is_decl.argtypes = [CursorKind]
Modified: cfe/trunk/bindings/python/tests/cindex/test_location.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_location.py?rev=149824&r1=149823&r2=149824&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_location.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_location.py Sun Feb 5 05:40:59 2012
@@ -1,4 +1,4 @@
-from clang.cindex import Index, File, SourceLocation, Cursor
+from clang.cindex import Index, File, SourceLocation, SourceRange, Cursor
baseInput="int one;\nint two;\n"
@@ -47,6 +47,12 @@
if n.spelling == 'one':
assert n == cursor
+ # Ensure locations referring to the same entity are equivalent.
+ location2 = SourceLocation.from_position(tu, file, 1, 5)
+ assert location == location2
+ location3 = SourceLocation.from_position(tu, file, 1, 4)
+ assert location2 != location3
+
def test_extent():
index = Index.create()
tu = index.parse('t.c', unsaved_files = [('t.c',baseInput)])
@@ -60,3 +66,15 @@
assert_location(n.extent.start,line=2,column=1,offset=9)
assert_location(n.extent.end,line=2,column=8,offset=16)
assert baseInput[n.extent.start.offset:n.extent.end.offset] == "int two"
+
+ file = File.from_name(tu, 't.c')
+ location1 = SourceLocation.from_position(tu, file, 1, 1)
+ location2 = SourceLocation.from_position(tu, file, 1, 8)
+
+ range1 = SourceRange.from_locations(location1, location2)
+ range2 = SourceRange.from_locations(location1, location2)
+ assert range1 == range2
+
+ location3 = SourceLocation.from_position(tu, file, 1, 6)
+ range3 = SourceRange.from_locations(location1, location3)
+ assert range1 != range3
More information about the cfe-commits
mailing list