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    mydir = TestBase.compute_mydir(__file__)
13
14    def setUp(self):
15        TestBase.setUp(self)
16        self.source = lldb.SBFileSpec('main.cpp')
17
18    def test(self):
19        self.build()
20
21        (target, _, _, _) = lldbutil.run_to_source_breakpoint(self, "// Set break point at this line.", self.source)
22
23        # Check that we can access g_file_global_int by its name
24        self.expect("target variable g_file_global_int", VARIABLES_DISPLAYED_CORRECTLY,
25                    substrs=['42'])
26        self.expect("target variable abc::g_file_global_int", VARIABLES_DISPLAYED_CORRECTLY,
27                    substrs=['42'])
28        self.expect("target variable xyz::g_file_global_int", VARIABLES_DISPLAYED_CORRECTLY,
29                    error=True, substrs=['can\'t find global variable'])
30
31        # Check that we can access g_file_global_const_int by its name
32        self.expect("target variable g_file_global_const_int", VARIABLES_DISPLAYED_CORRECTLY,
33                    substrs=['1337'])
34        self.expect("target variable abc::g_file_global_const_int", VARIABLES_DISPLAYED_CORRECTLY,
35                    substrs=['1337'])
36        self.expect("target variable xyz::g_file_global_const_int", VARIABLES_DISPLAYED_CORRECTLY,
37                    error=True, substrs=['can\'t find global variable'])
38
39        # Try accessing a global variable in anonymous namespace.
40        self.expect("target variable g_anon_namespace_const_int", VARIABLES_DISPLAYED_CORRECTLY,
41                    substrs=['100'])
42        self.expect("target variable abc::g_anon_namespace_const_int", VARIABLES_DISPLAYED_CORRECTLY,
43                    error=True, substrs=['can\'t find global variable'])
44        var = target.FindFirstGlobalVariable("abc::(anonymous namespace)::g_anon_namespace_const_int")
45        self.assertTrue(var.IsValid())
46        self.assertEqual(var.GetName(), "abc::(anonymous namespace)::g_anon_namespace_const_int")
47        self.assertEqual(var.GetValue(), "100")
48
49    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
50    def test_access_by_mangled_name(self):
51        self.build()
52
53        (target, _, _, _) = lldbutil.run_to_source_breakpoint(self, "// Set break point at this line.", self.source)
54
55        # Check that we can access g_file_global_int by its mangled name
56        addr = target.EvaluateExpression("&abc::g_file_global_int").GetValueAsUnsigned()
57        self.assertNotEqual(addr, 0)
58        mangled = lldb.SBAddress(addr, target).GetSymbol().GetMangledName()
59        self.assertNotEqual(mangled, None)
60        gv = target.FindFirstGlobalVariable(mangled)
61        self.assertTrue(gv.IsValid())
62        self.assertEqual(gv.GetName(), "abc::g_file_global_int")
63        self.assertEqual(gv.GetValueAsUnsigned(), 42)
64