1"""
2Test that we work properly with classes with the trivial_abi attribute
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class TestTrivialABI(TestBase):
14    NO_DEBUG_INFO_TESTCASE = True
15
16    @skipUnlessSupportedTypeAttribute("trivial_abi")
17    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr37995")
18    @expectedFailureAll(archs=["aarch64"], oslist=["freebsd", "linux"],
19                        bugnumber="llvm.org/pr44161")
20    def test_call_trivial(self):
21        """Test that we can print a variable & call a function with a trivial ABI class."""
22        self.build()
23        self.main_source_file = lldb.SBFileSpec("main.cpp")
24        self.expr_test(True)
25
26    @skipUnlessSupportedTypeAttribute("trivial_abi")
27    # fixed for SysV-x86_64 ABI, but not Windows-x86_64
28    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr36870")
29    @expectedFailureAll(archs=["arm", "aarch64"], oslist=["freebsd", "linux"],
30                        bugnumber="llvm.org/pr44161")
31    @expectedFailureAll(archs=["arm64", "arm64e"], bugnumber="<rdar://problem/57844240>")
32    def test_call_nontrivial(self):
33        """Test that we can print a variable & call a function on the same class w/o the trivial ABI marker."""
34        self.build()
35        self.main_source_file = lldb.SBFileSpec("main.cpp")
36        self.expr_test(False)
37
38    def check_value(self, test_var, ivar_value):
39        self.assertSuccess(test_var.GetError(), "Invalid valobj")
40        ivar = test_var.GetChildMemberWithName("ivar")
41        self.assertSuccess(test_var.GetError(), "Failed to fetch ivar")
42        self.assertEqual(ivar_value, ivar.GetValueAsSigned(), "Got the right value for ivar")
43
44    def check_frame(self, thread):
45        frame = thread.frames[0]
46        inVal_var = frame.FindVariable("inVal")
47        self.check_value(inVal_var, 10)
48
49        options = lldb.SBExpressionOptions()
50        inVal_expr = frame.EvaluateExpression("inVal", options)
51        self.check_value(inVal_expr, 10)
52
53        thread.StepOut()
54        outVal_ret = thread.GetStopReturnValue()
55        self.check_value(outVal_ret, 30)
56
57    def expr_test(self, trivial):
58        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
59                                   "Set a breakpoint here", self.main_source_file)
60
61        # Stop in a function that takes a trivial value, and try both frame var & expr to get its value:
62        if trivial:
63            self.check_frame(thread)
64            return
65
66        # Now continue to the same thing without the trivial_abi and see if we get that right:
67        threads = lldbutil.continue_to_breakpoint(process, bkpt)
68        self.assertEqual(len(threads), 1, "Hit my breakpoint the second time.")
69
70        self.check_frame(threads[0])
71