Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / bindings / python / tests / cindex / test_tokens.py
blob2cbf42c4c6cb9393703413fc7c524177ae1d3c74
1 import os
2 from clang.cindex import Config
4 if "CLANG_LIBRARY_PATH" in os.environ:
5 Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
7 from clang.cindex import CursorKind
8 from clang.cindex import Index
9 from clang.cindex import SourceLocation
10 from clang.cindex import SourceRange
11 from clang.cindex import TokenKind
13 from .util import get_tu
15 import unittest
18 class TestTokens(unittest.TestCase):
19 def test_token_to_cursor(self):
20 """Ensure we can obtain a Cursor from a Token instance."""
21 tu = get_tu("int i = 5;")
22 r = tu.get_extent("t.c", (0, 9))
23 tokens = list(tu.get_tokens(extent=r))
25 self.assertEqual(len(tokens), 4)
26 self.assertEqual(tokens[1].spelling, "i")
27 self.assertEqual(tokens[1].kind, TokenKind.IDENTIFIER)
29 cursor = tokens[1].cursor
30 self.assertEqual(cursor.kind, CursorKind.VAR_DECL)
31 self.assertEqual(tokens[1].cursor, tokens[2].cursor)
33 def test_token_location(self):
34 """Ensure Token.location works."""
36 tu = get_tu("int foo = 10;")
37 r = tu.get_extent("t.c", (0, 11))
39 tokens = list(tu.get_tokens(extent=r))
40 self.assertEqual(len(tokens), 4)
42 loc = tokens[1].location
43 self.assertIsInstance(loc, SourceLocation)
44 self.assertEqual(loc.line, 1)
45 self.assertEqual(loc.column, 5)
46 self.assertEqual(loc.offset, 4)
48 def test_token_extent(self):
49 """Ensure Token.extent works."""
50 tu = get_tu("int foo = 10;")
51 r = tu.get_extent("t.c", (0, 11))
53 tokens = list(tu.get_tokens(extent=r))
54 self.assertEqual(len(tokens), 4)
56 extent = tokens[1].extent
57 self.assertIsInstance(extent, SourceRange)
59 self.assertEqual(extent.start.offset, 4)
60 self.assertEqual(extent.end.offset, 7)