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