1"""
2Test expression command options.
3
4Test cases:
5
6o test_expr_options:
7  Test expression command options.
8"""
9
10
11
12import lldb
13import lldbsuite.test.lldbutil as lldbutil
14from lldbsuite.test.decorators import *
15from lldbsuite.test.lldbtest import *
16
17
18class ExprOptionsTestCase(TestBase):
19
20    mydir = TestBase.compute_mydir(__file__)
21
22    def setUp(self):
23        # Call super's setUp().
24        TestBase.setUp(self)
25
26        self.main_source = "main.cpp"
27        self.main_source_spec = lldb.SBFileSpec(self.main_source)
28        self.line = line_number('main.cpp', '// breakpoint_in_main')
29        self.exe = self.getBuildArtifact("a.out")
30
31    def test_expr_options(self):
32        """These expression command options should work as expected."""
33        self.build()
34
35        # Set debugger into synchronous mode
36        self.dbg.SetAsync(False)
37
38        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
39            self, '// breakpoint_in_main', self.main_source_spec)
40
41        frame = thread.GetFrameAtIndex(0)
42        options = lldb.SBExpressionOptions()
43
44        # test --language on C++ expression using the SB API's
45
46        # Make sure we can evaluate a C++11 expression.
47        val = frame.EvaluateExpression('foo != nullptr')
48        self.assertTrue(val.IsValid())
49        self.assertSuccess(val.GetError())
50        self.DebugSBValue(val)
51
52        # Make sure it still works if language is set to C++11:
53        options.SetLanguage(lldb.eLanguageTypeC_plus_plus_11)
54        val = frame.EvaluateExpression('foo != nullptr', options)
55        self.assertTrue(val.IsValid())
56        self.assertSuccess(val.GetError())
57        self.DebugSBValue(val)
58
59        # Make sure it fails if language is set to C:
60        options.SetLanguage(lldb.eLanguageTypeC)
61        val = frame.EvaluateExpression('foo != nullptr', options)
62        self.assertTrue(val.IsValid())
63        self.assertFalse(val.GetError().Success())
64
65    @skipIfDarwin
66    def test_expr_options_lang(self):
67        """These expression language options should work as expected."""
68        self.build()
69
70        # Set debugger into synchronous mode
71        self.dbg.SetAsync(False)
72
73        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
74            self, '// breakpoint_in_main', self.main_source_spec)
75
76        frame = thread.GetFrameAtIndex(0)
77        options = lldb.SBExpressionOptions()
78
79        # Make sure we can retrieve `id` variable if language is set to C++11:
80        options.SetLanguage(lldb.eLanguageTypeC_plus_plus_11)
81        val = frame.EvaluateExpression('id == 0', options)
82        self.assertTrue(val.IsValid())
83        self.assertSuccess(val.GetError())
84        self.DebugSBValue(val)
85
86        # Make sure we can't retrieve `id` variable if language is set to ObjC:
87        options.SetLanguage(lldb.eLanguageTypeObjC)
88        val = frame.EvaluateExpression('id == 0', options)
89        self.assertTrue(val.IsValid())
90        self.assertFalse(val.GetError().Success())
91