"""
Test the diagnostics emitted by our embeded Clang instance that parses expressions.
"""
import lldb
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil
from lldbsuite.test.decorators import *
class ExprDiagnosticsTestCase(TestBase):
def setUp(self):
TestBase.setUp(self)
self.main_source = "main.cpp"
self.main_source_spec = lldb.SBFileSpec(self.main_source)
def test_source_and_caret_printing(self):
"""Test that the source and caret positions LLDB prints are correct"""
self.build()
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
'// Break here', self.main_source_spec)
frame = thread.GetFrameAtIndex(0)
value = frame.EvaluateExpression("unknown_identifier")
self.assertFalse(value.GetError().Success())
self.assertIn("\nunknown_identifier\n^\n", value.GetError().GetCString())
self.assertIn("<user expression 0>:1:1", value.GetError().GetCString())
value = frame.EvaluateExpression("1 + unknown_identifier ")
self.assertFalse(value.GetError().Success())
self.assertIn("\n1 + unknown_identifier", value.GetError().GetCString())
self.assertIn("\n ^\n", value.GetError().GetCString())
value = frame.EvaluateExpression("int a = 0;\nfoobar +=1;\na")
self.assertFalse(value.GetError().Success())
self.assertIn("\nfoobar +=1;\n^\n", value.GetError().GetCString())
self.assertIn("<user expression 2>:2:1", value.GetError().GetCString())
top_level_opts = lldb.SBExpressionOptions();
top_level_opts.SetTopLevel(True)
value = frame.EvaluateExpression("void foo(unknown_type x) {}", top_level_opts)
self.assertFalse(value.GetError().Success())
self.assertIn("\nvoid foo(unknown_type x) {}\n ^\n", value.GetError().GetCString())
self.assertIn("<user expression 3>:1:10", value.GetError().GetCString())
value = frame.EvaluateExpression("void x() {}\nvoid foo;", top_level_opts)
self.assertFalse(value.GetError().Success())
self.assertIn("\nvoid foo;\n ^", value.GetError().GetCString())
self.assertIn("<user expression 4>:2:6", value.GetError().GetCString())
value = frame.EvaluateExpression("struct SFoo{}; struct SFoo { int x; };", top_level_opts)
self.assertFalse(value.GetError().Success())
self.assertIn("<user expression 5>:1:8: previous definition is here\nstruct SFoo{}; struct SFoo { int x; };\n ^\n", value.GetError().GetCString())
value = frame.EvaluateExpression("struct FooBar { double x };", top_level_opts)
self.assertFalse(value.GetError().Success())
self.assertIn("error: <user expression 6>:1:8: redefinition of 'FooBar'\nstruct FooBar { double x };\n ^\n", value.GetError().GetCString())
value = frame.EvaluateExpression("foo(1, 2)")
self.assertFalse(value.GetError().Success())
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())
value = frame.EvaluateExpression("struct Redef { double x; };", top_level_opts)
value = frame.EvaluateExpression("struct Redef { float y; };", top_level_opts)
self.assertFalse(value.GetError().Success())
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())
@add_test_categories(["objc"])
def test_source_locations_from_objc_modules(self):
self.build()
(target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
'// Break here', self.main_source_spec)
frame = thread.GetFrameAtIndex(0)
self.runCmd("expr @import Foundation")
value = frame.EvaluateExpression("NSLog(1);")
self.assertFalse(value.GetError().Success())
self.assertIn("/Foundation.framework/", value.GetError().GetCString())
self.assertIn("void NSLog(NSString *format", value.GetError().GetCString())