199451b44SJordan Rupprechtimport lldb
299451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
399451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
499451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtclass StaticInitializers(TestBase):
799451b44SJordan Rupprecht
8*f47a84bcSMichał Górny    @expectedFailureAll(archs="aarch64", oslist=["freebsd", "linux"],
999451b44SJordan Rupprecht                        bugnumber="https://bugs.llvm.org/show_bug.cgi?id=44053")
1099451b44SJordan Rupprecht    def test(self):
1199451b44SJordan Rupprecht        """ Test a static initializer. """
1299451b44SJordan Rupprecht        self.build()
1399451b44SJordan Rupprecht
1499451b44SJordan Rupprecht        lldbutil.run_to_source_breakpoint(self, '// break here',
1599451b44SJordan Rupprecht                lldb.SBFileSpec("main.cpp", False))
1699451b44SJordan Rupprecht
1799451b44SJordan Rupprecht        # We use counter to observe if the initializer was called.
1899451b44SJordan Rupprecht        self.expect_expr("counter", result_type="int", result_value="0")
1999451b44SJordan Rupprecht        self.expect("expr -p -- struct Foo { Foo() { inc_counter(); } }; Foo f;")
2099451b44SJordan Rupprecht        self.expect_expr("counter", result_type="int", result_value="1")
2199451b44SJordan Rupprecht
2299451b44SJordan Rupprecht    def test_failing_init(self):
2399451b44SJordan Rupprecht        """ Test a static initializer that fails to execute. """
2499451b44SJordan Rupprecht        self.build()
2599451b44SJordan Rupprecht
2699451b44SJordan Rupprecht        lldbutil.run_to_source_breakpoint(self, '// break here',
2799451b44SJordan Rupprecht                lldb.SBFileSpec("main.cpp", False))
2899451b44SJordan Rupprecht
2999451b44SJordan Rupprecht        # FIXME: This error message is not even remotely helpful.
3099451b44SJordan Rupprecht        self.expect("expr -p -- struct Foo2 { Foo2() { do_abort(); } }; Foo2 f;", error=True,
3199451b44SJordan Rupprecht                    substrs=["error: couldn't run static initializer:"])
3271536fd0SRaphael Isemann
3371536fd0SRaphael Isemann    def test_without_process(self):
3471536fd0SRaphael Isemann        """ Test a static initializer without a running process. """
3571536fd0SRaphael Isemann        self.expect("expr -p -- int i = 0; struct Foo3 { Foo3() { ++i; } }; Foo3 f;", error=True,
3671536fd0SRaphael Isemann                    substrs=["Top-level code needs to be inserted into a runnable target"])
37