Clang Project

clang_source_code/bindings/python/tests/cindex/test_tls_kind.py
1import os
2from clang.cindex import Config
3if 'CLANG_LIBRARY_PATH' in os.environ:
4    Config.set_library_path(os.environ['CLANG_LIBRARY_PATH'])
5
6from clang.cindex import TLSKind
7from clang.cindex import Cursor
8from clang.cindex import TranslationUnit
9
10from .util import get_cursor
11from .util import get_tu
12
13import unittest
14
15
16class TestTLSKind(unittest.TestCase):
17    def test_tls_kind(self):
18        """Ensure that thread-local storage kinds are available on cursors."""
19
20        tu = get_tu("""
21int tls_none;
22thread_local int tls_dynamic;
23_Thread_local int tls_static;
24""", lang = 'cpp')
25
26        tls_none = get_cursor(tu.cursor, 'tls_none')
27        self.assertEqual(tls_none.tls_kind, TLSKind.NONE)
28
29        tls_dynamic = get_cursor(tu.cursor, 'tls_dynamic')
30        self.assertEqual(tls_dynamic.tls_kind, TLSKind.DYNAMIC)
31
32        tls_static = get_cursor(tu.cursor, 'tls_static')
33        self.assertEqual(tls_static.tls_kind, TLSKind.STATIC)
34
35        # The following case tests '__declspec(thread)'.  Since it is a Microsoft
36        # specific extension, specific flags are required for the parser to pick
37        # these up.
38        flags = ['-fms-extensions', '-target', 'x86_64-unknown-windows-win32',
39                 '-fms-compatibility-version=18']
40        tu = get_tu("""
41__declspec(thread) int tls_declspec_msvc18;
42""", lang = 'cpp', flags=flags)
43
44        tls_declspec_msvc18 = get_cursor(tu.cursor, 'tls_declspec_msvc18')
45        self.assertEqual(tls_declspec_msvc18.tls_kind, TLSKind.STATIC)
46
47        flags = ['-fms-extensions', '-target', 'x86_64-unknown-windows-win32',
48                 '-fms-compatibility-version=19']
49        tu = get_tu("""
50__declspec(thread) int tls_declspec_msvc19;
51""", lang = 'cpp', flags=flags)
52
53        tls_declspec_msvc19 = get_cursor(tu.cursor, 'tls_declspec_msvc19')
54        self.assertEqual(tls_declspec_msvc19.tls_kind, TLSKind.DYNAMIC)
55