1"""
2Test the diagnostics emitted by our embeded Clang instance that parses expressions.
3"""
4
5import lldb
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8from lldbsuite.test.decorators import *
9
10class ExprDiagnosticsTestCase(TestBase):
11
12    def setUp(self):
13        # Call super's setUp().
14        TestBase.setUp(self)
15
16        self.main_source = "main.cpp"
17        self.main_source_spec = lldb.SBFileSpec(self.main_source)
18
19    def test_source_and_caret_printing(self):
20        """Test that the source and caret positions LLDB prints are correct"""
21        self.build()
22
23        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
24                                          '// Break here', self.main_source_spec)
25        frame = thread.GetFrameAtIndex(0)
26
27        # Test that source/caret are at the right position.
28        value = frame.EvaluateExpression("unknown_identifier")
29        self.assertFalse(value.GetError().Success())
30        # We should get a nice diagnostic with a caret pointing at the start of
31        # the identifier.
32        self.assertIn("\nunknown_identifier\n^\n", value.GetError().GetCString())
33        self.assertIn("<user expression 0>:1:1", value.GetError().GetCString())
34
35        # Same as above but with the identifier in the middle.
36        value = frame.EvaluateExpression("1 + unknown_identifier  ")
37        self.assertFalse(value.GetError().Success())
38        self.assertIn("\n1 + unknown_identifier", value.GetError().GetCString())
39        self.assertIn("\n    ^\n", value.GetError().GetCString())
40
41        # Multiline expressions.
42        value = frame.EvaluateExpression("int a = 0;\nfoobar +=1;\na")
43        self.assertFalse(value.GetError().Success())
44        # We should still get the right line information and caret position.
45        self.assertIn("\nfoobar +=1;\n^\n", value.GetError().GetCString())
46        # It's the second line of the user expression.
47        self.assertIn("<user expression 2>:2:1", value.GetError().GetCString())
48
49        # Top-level expressions.
50        top_level_opts = lldb.SBExpressionOptions();
51        top_level_opts.SetTopLevel(True)
52
53        value = frame.EvaluateExpression("void foo(unknown_type x) {}", top_level_opts)
54        self.assertFalse(value.GetError().Success())
55        self.assertIn("\nvoid foo(unknown_type x) {}\n         ^\n", value.GetError().GetCString())
56        # Top-level expressions might use a different wrapper code, but the file name should still
57        # be the same.
58        self.assertIn("<user expression 3>:1:10", value.GetError().GetCString())
59
60        # Multiline top-level expressions.
61        value = frame.EvaluateExpression("void x() {}\nvoid foo;", top_level_opts)
62        self.assertFalse(value.GetError().Success())
63        self.assertIn("\nvoid foo;\n     ^", value.GetError().GetCString())
64        self.assertIn("<user expression 4>:2:6", value.GetError().GetCString())
65
66        # Test that we render Clang's 'notes' correctly.
67        value = frame.EvaluateExpression("struct SFoo{}; struct SFoo { int x; };", top_level_opts)
68        self.assertFalse(value.GetError().Success())
69        self.assertIn("<user expression 5>:1:8: previous definition is here\nstruct SFoo{}; struct SFoo { int x; };\n       ^\n", value.GetError().GetCString())
70
71        # Declarations from the debug information currently have no debug information. It's not clear what
72        # we should do in this case, but we should at least not print anything that's wrong.
73        # In the future our declarations should have valid source locations.
74        value = frame.EvaluateExpression("struct FooBar { double x };", top_level_opts)
75        self.assertFalse(value.GetError().Success())
76        self.assertIn("error: <user expression 6>:1:8: redefinition of 'FooBar'\nstruct FooBar { double x };\n       ^\n", value.GetError().GetCString())
77
78        value = frame.EvaluateExpression("foo(1, 2)")
79        self.assertFalse(value.GetError().Success())
80        self.assertIn("error: <user expression 7>:1:1: no matching function for call to 'foo'\nfoo(1, 2)\n^~~\nnote: candidate function not viable: requires single argument 'x', but 2 arguments were provided\n\n", value.GetError().GetCString())
81
82        # Redefine something that we defined in a user-expression. We should use the previous expression file name
83        # for the original decl.
84        value = frame.EvaluateExpression("struct Redef { double x; };", top_level_opts)
85        value = frame.EvaluateExpression("struct Redef { float y; };", top_level_opts)
86        self.assertFalse(value.GetError().Success())
87        self.assertIn("error: <user expression 9>:1:8: redefinition of 'Redef'\nstruct Redef { float y; };\n       ^\n<user expression 8>:1:8: previous definition is here\nstruct Redef { double x; };\n       ^", value.GetError().GetCString())
88
89    @add_test_categories(["objc"])
90    def test_source_locations_from_objc_modules(self):
91        self.build()
92
93        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
94                                          '// Break here', self.main_source_spec)
95        frame = thread.GetFrameAtIndex(0)
96
97        # Import foundation so that the Obj-C module is loaded (which contains source locations
98        # that can be used by LLDB).
99        self.runCmd("expr @import Foundation")
100        value = frame.EvaluateExpression("NSLog(1);")
101        self.assertFalse(value.GetError().Success())
102        # LLDB should print the source line that defines NSLog. To not rely on any
103        # header paths/line numbers or the actual formatting of the Foundation headers, only look
104        # for a few tokens in the output.
105        # File path should come from Foundation framework.
106        self.assertIn("/Foundation.framework/", value.GetError().GetCString())
107        # The NSLog definition source line should be printed. Return value and
108        # the first argument are probably stable enough that this test can check for them.
109        self.assertIn("void NSLog(NSString *format", value.GetError().GetCString())
110
111