1import lldb
2from lldbsuite.test.decorators import *
3from lldbsuite.test.lldbtest import *
4from lldbsuite.test import lldbutil
5
6class StaticInitializers(TestBase):
7
8    @expectedFailureAll(archs="aarch64", oslist=["freebsd", "linux"],
9                        bugnumber="https://bugs.llvm.org/show_bug.cgi?id=44053")
10    def test(self):
11        """ Test a static initializer. """
12        self.build()
13
14        lldbutil.run_to_source_breakpoint(self, '// break here',
15                lldb.SBFileSpec("main.cpp", False))
16
17        # We use counter to observe if the initializer was called.
18        self.expect_expr("counter", result_type="int", result_value="0")
19        self.expect("expr -p -- struct Foo { Foo() { inc_counter(); } }; Foo f;")
20        self.expect_expr("counter", result_type="int", result_value="1")
21
22    def test_failing_init(self):
23        """ Test a static initializer that fails to execute. """
24        self.build()
25
26        lldbutil.run_to_source_breakpoint(self, '// break here',
27                lldb.SBFileSpec("main.cpp", False))
28
29        # FIXME: This error message is not even remotely helpful.
30        self.expect("expr -p -- struct Foo2 { Foo2() { do_abort(); } }; Foo2 f;", error=True,
31                    substrs=["error: couldn't run static initializer:"])
32
33    def test_without_process(self):
34        """ Test a static initializer without a running process. """
35        self.expect("expr -p -- int i = 0; struct Foo3 { Foo3() { ++i; } }; Foo3 f;", error=True,
36                    substrs=["Top-level code needs to be inserted into a runnable target"])
37