1 from clang.cindex import Cursor 2 from clang.cindex import File 3 from clang.cindex import SourceLocation 4 from clang.cindex import SourceRange 5 from .util import get_cursor 6 from .util import get_tu 7 8 baseInput="int one;\nint two;\n" 9 10 def assert_location(loc, line, column, offset): 11 assert loc.line == line 12 assert loc.column == column 13 assert loc.offset == offset 14 15 def test_location(): 16 tu = get_tu(baseInput) 17 one = get_cursor(tu, 'one') 18 two = get_cursor(tu, 'two') 19 20 assert one is not None 21 assert two is not None 22 23 assert_location(one.location,line=1,column=5,offset=4) 24 assert_location(two.location,line=2,column=5,offset=13) 25 26 # adding a linebreak at top should keep columns same 27 tu = get_tu('\n' + baseInput) 28 one = get_cursor(tu, 'one') 29 two = get_cursor(tu, 'two') 30 31 assert one is not None 32 assert two is not None 33 34 assert_location(one.location,line=2,column=5,offset=5) 35 assert_location(two.location,line=3,column=5,offset=14) 36 37 # adding a space should affect column on first line only 38 tu = get_tu(' ' + baseInput) 39 one = get_cursor(tu, 'one') 40 two = get_cursor(tu, 'two') 41 42 assert_location(one.location,line=1,column=6,offset=5) 43 assert_location(two.location,line=2,column=5,offset=14) 44 45 # define the expected location ourselves and see if it matches 46 # the returned location 47 tu = get_tu(baseInput) 48 49 file = File.from_name(tu, 't.c') 50 location = SourceLocation.from_position(tu, file, 1, 5) 51 cursor = Cursor.from_location(tu, location) 52 53 one = get_cursor(tu, 'one') 54 assert one is not None 55 assert one == cursor 56 57 # Ensure locations referring to the same entity are equivalent. 58 location2 = SourceLocation.from_position(tu, file, 1, 5) 59 assert location == location2 60 location3 = SourceLocation.from_position(tu, file, 1, 4) 61 assert location2 != location3 62 63 offset_location = SourceLocation.from_offset(tu, file, 5) 64 cursor = Cursor.from_location(tu, offset_location) 65 verified = False 66 for n in [n for n in tu.cursor.get_children() if n.spelling == 'one']: 67 assert n == cursor 68 verified = True 69 70 assert verified 71 72 def test_extent(): 73 tu = get_tu(baseInput) 74 one = get_cursor(tu, 'one') 75 two = get_cursor(tu, 'two') 76 77 assert_location(one.extent.start,line=1,column=1,offset=0) 78 assert_location(one.extent.end,line=1,column=8,offset=7) 79 assert baseInput[one.extent.start.offset:one.extent.end.offset] == "int one" 80 81 assert_location(two.extent.start,line=2,column=1,offset=9) 82 assert_location(two.extent.end,line=2,column=8,offset=16) 83 assert baseInput[two.extent.start.offset:two.extent.end.offset] == "int two" 84 85 file = File.from_name(tu, 't.c') 86 location1 = SourceLocation.from_position(tu, file, 1, 1) 87 location2 = SourceLocation.from_position(tu, file, 1, 8) 88 89 range1 = SourceRange.from_locations(location1, location2) 90 range2 = SourceRange.from_locations(location1, location2) 91 assert range1 == range2 92 93 location3 = SourceLocation.from_position(tu, file, 1, 6) 94 range3 = SourceRange.from_locations(location1, location3) 95 assert range1 != range3 96