199451b44SJordan Rupprecht"""Test that C++ global variables can be inspected by name and also their mangled name."""
299451b44SJordan Rupprecht
399451b44SJordan Rupprecht
499451b44SJordan Rupprecht
599451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
699451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
799451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
899451b44SJordan Rupprecht
999451b44SJordan Rupprecht
1099451b44SJordan Rupprechtclass GlobalVariablesCppTestCase(TestBase):
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprecht    def setUp(self):
1399451b44SJordan Rupprecht        TestBase.setUp(self)
1499451b44SJordan Rupprecht        self.source = lldb.SBFileSpec('main.cpp')
1599451b44SJordan Rupprecht
1699451b44SJordan Rupprecht    def test(self):
1799451b44SJordan Rupprecht        self.build()
1899451b44SJordan Rupprecht
1999451b44SJordan Rupprecht        (target, _, _, _) = lldbutil.run_to_source_breakpoint(self, "// Set break point at this line.", self.source)
2099451b44SJordan Rupprecht
2199451b44SJordan Rupprecht        # Check that we can access g_file_global_int by its name
2299451b44SJordan Rupprecht        self.expect("target variable g_file_global_int", VARIABLES_DISPLAYED_CORRECTLY,
2399451b44SJordan Rupprecht                    substrs=['42'])
2499451b44SJordan Rupprecht        self.expect("target variable abc::g_file_global_int", VARIABLES_DISPLAYED_CORRECTLY,
2599451b44SJordan Rupprecht                    substrs=['42'])
2699451b44SJordan Rupprecht        self.expect("target variable xyz::g_file_global_int", VARIABLES_DISPLAYED_CORRECTLY,
2799451b44SJordan Rupprecht                    error=True, substrs=['can\'t find global variable'])
2899451b44SJordan Rupprecht
29*f66b69a3STonko Sabolčec        # Check that we can access g_file_global_const_int by its name
30*f66b69a3STonko Sabolčec        self.expect("target variable g_file_global_const_int", VARIABLES_DISPLAYED_CORRECTLY,
31*f66b69a3STonko Sabolčec                    substrs=['1337'])
32*f66b69a3STonko Sabolčec        self.expect("target variable abc::g_file_global_const_int", VARIABLES_DISPLAYED_CORRECTLY,
33*f66b69a3STonko Sabolčec                    substrs=['1337'])
34*f66b69a3STonko Sabolčec        self.expect("target variable xyz::g_file_global_const_int", VARIABLES_DISPLAYED_CORRECTLY,
35*f66b69a3STonko Sabolčec                    error=True, substrs=['can\'t find global variable'])
36*f66b69a3STonko Sabolčec
37*f66b69a3STonko Sabolčec        # Try accessing a global variable in anonymous namespace.
38*f66b69a3STonko Sabolčec        self.expect("target variable g_anon_namespace_const_int", VARIABLES_DISPLAYED_CORRECTLY,
39*f66b69a3STonko Sabolčec                    substrs=['100'])
40*f66b69a3STonko Sabolčec        self.expect("target variable abc::g_anon_namespace_const_int", VARIABLES_DISPLAYED_CORRECTLY,
41*f66b69a3STonko Sabolčec                    error=True, substrs=['can\'t find global variable'])
42*f66b69a3STonko Sabolčec        var = target.FindFirstGlobalVariable("abc::(anonymous namespace)::g_anon_namespace_const_int")
43*f66b69a3STonko Sabolčec        self.assertTrue(var.IsValid())
44*f66b69a3STonko Sabolčec        self.assertEqual(var.GetName(), "abc::(anonymous namespace)::g_anon_namespace_const_int")
45*f66b69a3STonko Sabolčec        self.assertEqual(var.GetValue(), "100")
46*f66b69a3STonko Sabolčec
47*f66b69a3STonko Sabolčec    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
48*f66b69a3STonko Sabolčec    def test_access_by_mangled_name(self):
49*f66b69a3STonko Sabolčec        self.build()
50*f66b69a3STonko Sabolčec
51*f66b69a3STonko Sabolčec        (target, _, _, _) = lldbutil.run_to_source_breakpoint(self, "// Set break point at this line.", self.source)
52*f66b69a3STonko Sabolčec
5399451b44SJordan Rupprecht        # Check that we can access g_file_global_int by its mangled name
5499451b44SJordan Rupprecht        addr = target.EvaluateExpression("&abc::g_file_global_int").GetValueAsUnsigned()
55b3a0c4d7SRaphael Isemann        self.assertNotEqual(addr, 0)
5699451b44SJordan Rupprecht        mangled = lldb.SBAddress(addr, target).GetSymbol().GetMangledName()
57b3a0c4d7SRaphael Isemann        self.assertNotEqual(mangled, None)
5899451b44SJordan Rupprecht        gv = target.FindFirstGlobalVariable(mangled)
5999451b44SJordan Rupprecht        self.assertTrue(gv.IsValid())
6099451b44SJordan Rupprecht        self.assertEqual(gv.GetName(), "abc::g_file_global_int")
6199451b44SJordan Rupprecht        self.assertEqual(gv.GetValueAsUnsigned(), 42)
62