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, "The expression was successful.") 24 self.assertTrue("Fix-it applied" in ret_val.GetError(), "Found the applied FixIt.") 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.assertTrue(value.GetError().Success()) 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.assertTrue(value.GetError().Success()) 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.assertTrue(value.GetError().Success(), 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 86 # Test repeatedly applying Fix-Its to expressions and reparsing them. 87 multiple_runs_options = lldb.SBExpressionOptions() 88 multiple_runs_options.SetAutoApplyFixIts(True) 89 multiple_runs_options.SetTopLevel(True) 90 91 # An expression that needs two parse attempts with one Fix-It each 92 # to be successfully parsed. 93 two_runs_expr = """ 94 struct Data { int m; }; 95 96 template<typename T> 97 struct S1 : public T { 98 using T::TypeDef; 99 int f() { 100 Data d; 101 d.m = 123; 102 // The first error as the using above requires a 'typename '. 103 // Will trigger a Fix-It that puts 'typename' in the right place. 104 typename S1<T>::TypeDef i = &d; 105 // i has the type "Data *", so this should be i.m. 106 // The second run will change the . to -> via the Fix-It. 107 return i.m; 108 } 109 }; 110 111 struct ClassWithTypeDef { 112 typedef Data *TypeDef; 113 }; 114 115 int test_X(int i) { 116 S1<ClassWithTypeDef> s1; 117 return s1.f(); 118 } 119 """ 120 121 # Disable retries which will fail. 122 multiple_runs_options.SetRetriesWithFixIts(0) 123 value = frame.EvaluateExpression(two_runs_expr, multiple_runs_options) 124 self.assertIn("expression failed to parse, fixed expression suggested:", 125 value.GetError().GetCString()) 126 self.assertIn("using typename T::TypeDef", 127 value.GetError().GetCString()) 128 # The second Fix-It shouldn't be suggested here as Clang should have 129 # aborted the parsing process. 130 self.assertNotIn("i->m", 131 value.GetError().GetCString()) 132 133 # Retry once, but the expression needs two retries. 134 multiple_runs_options.SetRetriesWithFixIts(1) 135 value = frame.EvaluateExpression(two_runs_expr, multiple_runs_options) 136 self.assertIn("expression failed to parse, fixed expression suggested:", 137 value.GetError().GetCString()) 138 # Both our fixed expressions should be in the suggested expression. 139 self.assertIn("using typename T::TypeDef", 140 value.GetError().GetCString()) 141 self.assertIn("i->m", 142 value.GetError().GetCString()) 143 144 # Retry twice, which will get the expression working. 145 multiple_runs_options.SetRetriesWithFixIts(2) 146 value = frame.EvaluateExpression(two_runs_expr, multiple_runs_options) 147 # This error signals success for top level expressions. 148 self.assertEquals(value.GetError().GetCString(), "error: No value") 149 150 # Test that the code above compiles to the right thing. 151 self.expect_expr("test_X(1)", result_type="int", result_value="123") 152