| 1 |
import os |
| 2 |
from clang.cindex import Config |
| 3 |
|
| 4 |
if "CLANG_LIBRARY_PATH" in os.environ: |
| 5 |
Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"]) |
| 6 |
|
| 7 |
from clang.cindex import Cursor |
| 8 |
from clang.cindex import File |
| 9 |
from clang.cindex import SourceLocation |
| 10 |
from clang.cindex import SourceRange |
| 11 |
from clang.cindex import TranslationUnit |
| 12 |
from .util import get_cursor |
| 13 |
from .util import get_tu |
| 14 |
|
| 15 |
import unittest |
| 16 |
|
| 17 |
|
| 18 |
baseInput = "int one;\nint two;\n" |
| 19 |
|
| 20 |
|
| 21 |
class TestLocation(unittest.TestCase): |
| 22 |
def assert_location(self, loc, line, column, offset): |
| 23 |
self.assertEqual(loc.line, line) |
| 24 |
self.assertEqual(loc.column, column) |
| 25 |
self.assertEqual(loc.offset, offset) |
| 26 |
|
| 27 |
def test_location(self): |
| 28 |
tu = get_tu(baseInput) |
| 29 |
one = get_cursor(tu, "one") |
| 30 |
two = get_cursor(tu, "two") |
| 31 |
|
| 32 |
self.assertIsNotNone(one) |
| 33 |
self.assertIsNotNone(two) |
| 34 |
|
| 35 |
self.assert_location(one.location, line=1, column=5, offset=4) |
| 36 |
self.assert_location(two.location, line=2, column=5, offset=13) |
| 37 |
|
| 38 |
# adding a linebreak at top should keep columns same |
| 39 |
tu = get_tu("\n" + baseInput) |
| 40 |
one = get_cursor(tu, "one") |
| 41 |
two = get_cursor(tu, "two") |
| 42 |
|
| 43 |
self.assertIsNotNone(one) |
| 44 |
self.assertIsNotNone(two) |
| 45 |
|
| 46 |
self.assert_location(one.location, line=2, column=5, offset=5) |
| 47 |
self.assert_location(two.location, line=3, column=5, offset=14) |
| 48 |
|
| 49 |
# adding a space should affect column on first line only |
| 50 |
tu = get_tu(" " + baseInput) |
| 51 |
one = get_cursor(tu, "one") |
| 52 |
two = get_cursor(tu, "two") |
| 53 |
|
| 54 |
self.assert_location(one.location, line=1, column=6, offset=5) |
| 55 |
self.assert_location(two.location, line=2, column=5, offset=14) |
| 56 |
|
| 57 |
# define the expected location ourselves and see if it matches |
| 58 |
# the returned location |
| 59 |
tu = get_tu(baseInput) |
| 60 |
|
| 61 |
file = File.from_name(tu, "t.c") |
| 62 |
location = SourceLocation.from_position(tu, file, 1, 5) |
| 63 |
cursor = Cursor.from_location(tu, location) |
| 64 |
|
| 65 |
one = get_cursor(tu, "one") |
| 66 |
self.assertIsNotNone(one) |
| 67 |
self.assertEqual(one, cursor) |
| 68 |
|
| 69 |
# Ensure locations referring to the same entity are equivalent. |
| 70 |
location2 = SourceLocation.from_position(tu, file, 1, 5) |
| 71 |
self.assertEqual(location, location2) |
| 72 |
location3 = SourceLocation.from_position(tu, file, 1, 4) |
| 73 |
self.assertNotEqual(location2, location3) |
| 74 |
|
| 75 |
offset_location = SourceLocation.from_offset(tu, file, 5) |
| 76 |
cursor = Cursor.from_location(tu, offset_location) |
| 77 |
verified = False |
| 78 |
for n in [n for n in tu.cursor.get_children() if n.spelling == "one"]: |
| 79 |
self.assertEqual(n, cursor) |
| 80 |
verified = True |
| 81 |
|
| 82 |
self.assertTrue(verified) |
| 83 |
|
| 84 |
def test_extent(self): |
| 85 |
tu = get_tu(baseInput) |
| 86 |
one = get_cursor(tu, "one") |
| 87 |
two = get_cursor(tu, "two") |
| 88 |
|
| 89 |
self.assert_location(one.extent.start, line=1, column=1, offset=0) |
| 90 |
self.assert_location(one.extent.end, line=1, column=8, offset=7) |
| 91 |
self.assertEqual( |
| 92 |
baseInput[one.extent.start.offset : one.extent.end.offset], "int one" |
| 93 |
) |
| 94 |
|
| 95 |
self.assert_location(two.extent.start, line=2, column=1, offset=9) |
| 96 |
self.assert_location(two.extent.end, line=2, column=8, offset=16) |
| 97 |
self.assertEqual( |
| 98 |
baseInput[two.extent.start.offset : two.extent.end.offset], "int two" |
| 99 |
) |
| 100 |
|
| 101 |
file = File.from_name(tu, "t.c") |
| 102 |
location1 = SourceLocation.from_position(tu, file, 1, 1) |
| 103 |
location2 = SourceLocation.from_position(tu, file, 1, 8) |
| 104 |
|
| 105 |
range1 = SourceRange.from_locations(location1, location2) |
| 106 |
range2 = SourceRange.from_locations(location1, location2) |
| 107 |
self.assertEqual(range1, range2) |
| 108 |
|
| 109 |
location3 = SourceLocation.from_position(tu, file, 1, 6) |
| 110 |
range3 = SourceRange.from_locations(location1, location3) |
| 111 |
self.assertNotEqual(range1, range3) |
| 112 |
|
| 113 |
def test_is_system_location(self): |
| 114 |
header = os.path.normpath("./fake/fake.h") |
| 115 |
tu = TranslationUnit.from_source( |
| 116 |
"fake.c", |
| 117 |
[f"-isystem{os.path.dirname(header)}"], |
| 118 |
unsaved_files=[ |
| 119 |
( |
| 120 |
"fake.c", |
| 121 |
""" |
| 122 |
#include <fake.h> |
| 123 |
int one; |
| 124 |
""", |
| 125 |
), |
| 126 |
(header, "int two();"), |
| 127 |
], |
| 128 |
) |
| 129 |
one = get_cursor(tu, "one") |
| 130 |
two = get_cursor(tu, "two") |
| 131 |
self.assertFalse(one.location.is_in_system_header) |
| 132 |
self.assertTrue(two.location.is_in_system_header) |