1""" 2Test calling an expression with errors that a FixIt can fix. 3""" 4 5import lldb 6from lldbsuite.test.decorators import * 7from lldbsuite.test.lldbtest import * 8from lldbsuite.test import lldbutil 9 10 11class ExprCommandWithFixits(TestBase): 12 13 mydir = TestBase.compute_mydir(__file__) 14 15 def test_with_dummy_target(self): 16 """Test calling expressions in the dummy target with errors that can be fixed by the FixIts.""" 17 18 # Enable fix-its as they were intentionally disabled by TestBase.setUp. 19 self.runCmd("settings set target.auto-apply-fixits true") 20 21 ret_val = lldb.SBCommandReturnObject() 22 result = self.dbg.GetCommandInterpreter().HandleCommand("expression ((1 << 16) - 1))", ret_val) 23 self.assertEqual(result, lldb.eReturnStatusSuccessFinishResult, ret_val.GetError()) 24 self.assertIn("Fix-it applied", ret_val.GetError()) 25 26 def test_with_target(self): 27 """Test calling expressions with errors that can be fixed by the FixIts.""" 28 self.build() 29 (target, process, self.thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 30 'Stop here to evaluate expressions', 31 lldb.SBFileSpec("main.cpp")) 32 33 options = lldb.SBExpressionOptions() 34 options.SetAutoApplyFixIts(True) 35 36 top_level_options = lldb.SBExpressionOptions() 37 top_level_options.SetAutoApplyFixIts(True) 38 top_level_options.SetTopLevel(True) 39 40 frame = self.thread.GetFrameAtIndex(0) 41 42 # Try with one error: 43 value = frame.EvaluateExpression("my_pointer.first", options) 44 self.assertTrue(value.IsValid()) 45 self.assertSuccess(value.GetError()) 46 self.assertEquals(value.GetValueAsUnsigned(), 10) 47 48 # Try with one error in a top-level expression. 49 # The Fix-It changes "ptr.m" to "ptr->m". 50 expr = "struct X { int m; }; X x; X *ptr = &x; int m = ptr.m;" 51 value = frame.EvaluateExpression(expr, top_level_options) 52 # A successfully parsed top-level expression will yield an error 53 # that there is 'no value'. If a parsing error would have happened we 54 # would get a different error kind, so let's check the error kind here. 55 self.assertEquals(value.GetError().GetCString(), "error: No value") 56 57 # Try with two errors: 58 two_error_expression = "my_pointer.second->a" 59 value = frame.EvaluateExpression(two_error_expression, options) 60 self.assertTrue(value.IsValid()) 61 self.assertSuccess(value.GetError()) 62 self.assertEquals(value.GetValueAsUnsigned(), 20) 63 64 # Try a Fix-It that is stored in the 'note:' diagnostic of an error. 65 # The Fix-It here is adding parantheses around the ToStr parameters. 66 fixit_in_note_expr ="#define ToStr(x) #x\nToStr(0 {, })" 67 value = frame.EvaluateExpression(fixit_in_note_expr, options) 68 self.assertTrue(value.IsValid()) 69 self.assertSuccess(value.GetError()) 70 self.assertEquals(value.GetSummary(), '"(0 {, })"') 71 72 # Now turn off the fixits, and the expression should fail: 73 options.SetAutoApplyFixIts(False) 74 value = frame.EvaluateExpression(two_error_expression, options) 75 self.assertTrue(value.IsValid()) 76 self.assertTrue(value.GetError().Fail()) 77 error_string = value.GetError().GetCString() 78 self.assertTrue( 79 error_string.find("fixed expression suggested:") != -1, 80 "Fix was suggested") 81 self.assertTrue( 82 error_string.find("my_pointer->second.a") != -1, 83 "Fix was right") 84 85 # The final function call runs into SIGILL on aarch64-linux. 86 @expectedFailureAll(archs=["aarch64"], oslist=["linux"]) 87 def test_with_multiple_retries(self): 88 """Test calling expressions with errors that can be fixed by the FixIts.""" 89 self.build() 90 (target, process, self.thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 91 'Stop here to evaluate expressions', 92 lldb.SBFileSpec("main.cpp")) 93 94 # Test repeatedly applying Fix-Its to expressions and reparsing them. 95 multiple_runs_options = lldb.SBExpressionOptions() 96 multiple_runs_options.SetAutoApplyFixIts(True) 97 multiple_runs_options.SetTopLevel(True) 98 99 frame = self.thread.GetFrameAtIndex(0) 100 101 # An expression that needs two parse attempts with one Fix-It each 102 # to be successfully parsed. 103 two_runs_expr = """ 104 struct Data { int m; }; 105 106 template<typename T> 107 struct S1 : public T { 108 using T::TypeDef; 109 int f() { 110 Data data; 111 data.m = 123; 112 // The first error as the using above requires a 'typename '. 113 // Will trigger a Fix-It that puts 'typename' in the right place. 114 typename S1<T>::TypeDef i = &data; 115 // i has the type "Data *", so this should be i.m. 116 // The second run will change the . to -> via the Fix-It. 117 return i.m; 118 } 119 }; 120 121 struct ClassWithTypeDef { 122 typedef Data *TypeDef; 123 }; 124 125 int test_X(int i) { 126 S1<ClassWithTypeDef> s1; 127 return s1.f(); 128 } 129 """ 130 131 # Disable retries which will fail. 132 multiple_runs_options.SetRetriesWithFixIts(0) 133 value = frame.EvaluateExpression(two_runs_expr, multiple_runs_options) 134 self.assertIn("expression failed to parse, fixed expression suggested:", 135 value.GetError().GetCString()) 136 self.assertIn("using typename T::TypeDef", 137 value.GetError().GetCString()) 138 # The second Fix-It shouldn't be suggested here as Clang should have 139 # aborted the parsing process. 140 self.assertNotIn("i->m", 141 value.GetError().GetCString()) 142 143 # Retry once, but the expression needs two retries. 144 multiple_runs_options.SetRetriesWithFixIts(1) 145 value = frame.EvaluateExpression(two_runs_expr, multiple_runs_options) 146 self.assertIn("expression failed to parse, fixed expression suggested:", 147 value.GetError().GetCString()) 148 # Both our fixed expressions should be in the suggested expression. 149 self.assertIn("using typename T::TypeDef", 150 value.GetError().GetCString()) 151 self.assertIn("i->m", 152 value.GetError().GetCString()) 153 154 # Retry twice, which will get the expression working. 155 multiple_runs_options.SetRetriesWithFixIts(2) 156 value = frame.EvaluateExpression(two_runs_expr, multiple_runs_options) 157 # This error signals success for top level expressions. 158 self.assertEquals(value.GetError().GetCString(), "error: No value") 159 160 # Test that the code above compiles to the right thing. 161 self.expect_expr("test_X(1)", result_type="int", result_value="123") 162