1"""Test that C++ global variables can be inspected by name and also their mangled name."""
2
3
4
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class GlobalVariablesCppTestCase(TestBase):
11
12    def setUp(self):
13        TestBase.setUp(self)
14        self.source = lldb.SBFileSpec('main.cpp')
15
16    def test(self):
17        self.build()
18
19        (target, _, _, _) = lldbutil.run_to_source_breakpoint(self, "// Set break point at this line.", self.source)
20
21        # Check that we can access g_file_global_int by its name
22        self.expect("target variable g_file_global_int", VARIABLES_DISPLAYED_CORRECTLY,
23                    substrs=['42'])
24        self.expect("target variable abc::g_file_global_int", VARIABLES_DISPLAYED_CORRECTLY,
25                    substrs=['42'])
26        self.expect("target variable xyz::g_file_global_int", VARIABLES_DISPLAYED_CORRECTLY,
27                    error=True, substrs=['can\'t find global variable'])
28
29        # Check that we can access g_file_global_const_int by its name
30        self.expect("target variable g_file_global_const_int", VARIABLES_DISPLAYED_CORRECTLY,
31                    substrs=['1337'])
32        self.expect("target variable abc::g_file_global_const_int", VARIABLES_DISPLAYED_CORRECTLY,
33                    substrs=['1337'])
34        self.expect("target variable xyz::g_file_global_const_int", VARIABLES_DISPLAYED_CORRECTLY,
35                    error=True, substrs=['can\'t find global variable'])
36
37        # Try accessing a global variable in anonymous namespace.
38        self.expect("target variable g_anon_namespace_const_int", VARIABLES_DISPLAYED_CORRECTLY,
39                    substrs=['100'])
40        self.expect("target variable abc::g_anon_namespace_const_int", VARIABLES_DISPLAYED_CORRECTLY,
41                    error=True, substrs=['can\'t find global variable'])
42        var = target.FindFirstGlobalVariable("abc::(anonymous namespace)::g_anon_namespace_const_int")
43        self.assertTrue(var.IsValid())
44        self.assertEqual(var.GetName(), "abc::(anonymous namespace)::g_anon_namespace_const_int")
45        self.assertEqual(var.GetValue(), "100")
46
47    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
48    def test_access_by_mangled_name(self):
49        self.build()
50
51        (target, _, _, _) = lldbutil.run_to_source_breakpoint(self, "// Set break point at this line.", self.source)
52
53        # Check that we can access g_file_global_int by its mangled name
54        addr = target.EvaluateExpression("&abc::g_file_global_int").GetValueAsUnsigned()
55        self.assertNotEqual(addr, 0)
56        mangled = lldb.SBAddress(addr, target).GetSymbol().GetMangledName()
57        self.assertNotEqual(mangled, None)
58        gv = target.FindFirstGlobalVariable(mangled)
59        self.assertTrue(gv.IsValid())
60        self.assertEqual(gv.GetName(), "abc::g_file_global_int")
61        self.assertEqual(gv.GetValueAsUnsigned(), 42)
62