xref: /llvm-project/lldb/test/API/lang/cpp/global_variables/TestCPPGlobalVariables.py (revision ab05d9134d18db34501985a01fbfc02609767587)
199451b44SJordan Rupprecht"""Test that C++ global variables can be inspected by name and also their mangled name."""
299451b44SJordan Rupprecht
399451b44SJordan Rupprecht
499451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
599451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
699451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
799451b44SJordan Rupprecht
899451b44SJordan Rupprecht
999451b44SJordan Rupprechtclass GlobalVariablesCppTestCase(TestBase):
1099451b44SJordan Rupprecht    def setUp(self):
1199451b44SJordan Rupprecht        TestBase.setUp(self)
122238dcc3SJonas Devlieghere        self.source = lldb.SBFileSpec("main.cpp")
1399451b44SJordan Rupprecht
1499451b44SJordan Rupprecht    def test(self):
1599451b44SJordan Rupprecht        self.build()
1699451b44SJordan Rupprecht
172238dcc3SJonas Devlieghere        (target, _, _, _) = lldbutil.run_to_source_breakpoint(
182238dcc3SJonas Devlieghere            self, "// Set break point at this line.", self.source
192238dcc3SJonas Devlieghere        )
2099451b44SJordan Rupprecht
2199451b44SJordan Rupprecht        # Check that we can access g_file_global_int by its name
222238dcc3SJonas Devlieghere        self.expect(
232238dcc3SJonas Devlieghere            "target variable g_file_global_int",
242238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
252238dcc3SJonas Devlieghere            substrs=["42"],
262238dcc3SJonas Devlieghere        )
272238dcc3SJonas Devlieghere        self.expect(
282238dcc3SJonas Devlieghere            "target variable abc::g_file_global_int",
292238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
302238dcc3SJonas Devlieghere            substrs=["42"],
312238dcc3SJonas Devlieghere        )
322238dcc3SJonas Devlieghere        self.expect(
332238dcc3SJonas Devlieghere            "target variable xyz::g_file_global_int",
342238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
352238dcc3SJonas Devlieghere            error=True,
362238dcc3SJonas Devlieghere            substrs=["can't find global variable"],
372238dcc3SJonas Devlieghere        )
3899451b44SJordan Rupprecht
39f66b69a3STonko Sabolčec        # Check that we can access g_file_global_const_int by its name
402238dcc3SJonas Devlieghere        self.expect(
412238dcc3SJonas Devlieghere            "target variable g_file_global_const_int",
422238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
432238dcc3SJonas Devlieghere            substrs=["1337"],
442238dcc3SJonas Devlieghere        )
452238dcc3SJonas Devlieghere        self.expect(
462238dcc3SJonas Devlieghere            "target variable abc::g_file_global_const_int",
472238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
482238dcc3SJonas Devlieghere            substrs=["1337"],
492238dcc3SJonas Devlieghere        )
502238dcc3SJonas Devlieghere        self.expect(
512238dcc3SJonas Devlieghere            "target variable xyz::g_file_global_const_int",
522238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
532238dcc3SJonas Devlieghere            error=True,
542238dcc3SJonas Devlieghere            substrs=["can't find global variable"],
552238dcc3SJonas Devlieghere        )
56f66b69a3STonko Sabolčec
57f66b69a3STonko Sabolčec        # Try accessing a global variable in anonymous namespace.
582238dcc3SJonas Devlieghere        self.expect(
592238dcc3SJonas Devlieghere            "target variable g_anon_namespace_const_int",
602238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
612238dcc3SJonas Devlieghere            substrs=["100"],
622238dcc3SJonas Devlieghere        )
632238dcc3SJonas Devlieghere        self.expect(
642238dcc3SJonas Devlieghere            "target variable abc::g_anon_namespace_const_int",
652238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
662238dcc3SJonas Devlieghere            error=True,
672238dcc3SJonas Devlieghere            substrs=["can't find global variable"],
682238dcc3SJonas Devlieghere        )
692238dcc3SJonas Devlieghere        var = target.FindFirstGlobalVariable(
702238dcc3SJonas Devlieghere            "abc::(anonymous namespace)::g_anon_namespace_const_int"
712238dcc3SJonas Devlieghere        )
72f66b69a3STonko Sabolčec        self.assertTrue(var.IsValid())
732238dcc3SJonas Devlieghere        self.assertEqual(
742238dcc3SJonas Devlieghere            var.GetName(), "abc::(anonymous namespace)::g_anon_namespace_const_int"
752238dcc3SJonas Devlieghere        )
76f66b69a3STonko Sabolčec        self.assertEqual(var.GetValue(), "100")
77f66b69a3STonko Sabolčec
78*ab05d913Stcwg    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
79f66b69a3STonko Sabolčec    def test_access_by_mangled_name(self):
80f66b69a3STonko Sabolčec        self.build()
81f66b69a3STonko Sabolčec
822238dcc3SJonas Devlieghere        (target, _, _, _) = lldbutil.run_to_source_breakpoint(
832238dcc3SJonas Devlieghere            self, "// Set break point at this line.", self.source
842238dcc3SJonas Devlieghere        )
85f66b69a3STonko Sabolčec
8699451b44SJordan Rupprecht        # Check that we can access g_file_global_int by its mangled name
8799451b44SJordan Rupprecht        addr = target.EvaluateExpression("&abc::g_file_global_int").GetValueAsUnsigned()
88b3a0c4d7SRaphael Isemann        self.assertNotEqual(addr, 0)
8999451b44SJordan Rupprecht        mangled = lldb.SBAddress(addr, target).GetSymbol().GetMangledName()
90b3a0c4d7SRaphael Isemann        self.assertNotEqual(mangled, None)
9199451b44SJordan Rupprecht        gv = target.FindFirstGlobalVariable(mangled)
9299451b44SJordan Rupprecht        self.assertTrue(gv.IsValid())
9399451b44SJordan Rupprecht        self.assertEqual(gv.GetName(), "abc::g_file_global_int")
9499451b44SJordan Rupprecht        self.assertEqual(gv.GetValueAsUnsigned(), 42)
95