Skip to content

Commit 8e2bd05

Browse files
authored
[lldb] Fix po alias by printing fix-its to the console. (llvm#68755)
The `po` alias now matches the behavior of the `expression` command when the it can apply a Fix-It to an expression. Modifications - Add has `m_fixed_expression` to the `CommandObjectDWIMPrint` class a `protected` member that stores the post Fix-It expression, just like the `CommandObjectExpression` class. - Converted messages to present tense. - Add test cases that confirms a Fix-It for a C++ expression for both `po` and `expressions` rdar://115317419
1 parent bbecd42 commit 8e2bd05

File tree

6 files changed

+75
-8
lines changed

6 files changed

+75
-8
lines changed

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,19 @@ bool CommandObjectDWIMPrint::DoExecute(StringRef command,
172172
{
173173
auto *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
174174
ValueObjectSP valobj_sp;
175-
ExpressionResults expr_result =
176-
target.EvaluateExpression(expr, exe_scope, valobj_sp, eval_options);
175+
std::string fixed_expression;
176+
177+
ExpressionResults expr_result = target.EvaluateExpression(
178+
expr, exe_scope, valobj_sp, eval_options, &fixed_expression);
179+
180+
// Only mention Fix-Its if the expression evaluator applied them.
181+
// Compiler errors refer to the final expression after applying Fix-It(s).
182+
if (!fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) {
183+
Stream &error_stream = result.GetErrorStream();
184+
error_stream << " Evaluated this expression after applying Fix-It(s):\n";
185+
error_stream << " " << fixed_expression << "\n";
186+
}
187+
177188
if (expr_result == eExpressionCompleted) {
178189
if (verbosity != eDWIMPrintVerbosityNone) {
179190
StringRef flags;

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,11 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
439439
ExpressionResults success = target.EvaluateExpression(
440440
expr, frame, result_valobj_sp, eval_options, &m_fixed_expression);
441441

442-
// We only tell you about the FixIt if we applied it. The compiler errors
443-
// will suggest the FixIt if it parsed.
442+
// Only mention Fix-Its if the expression evaluator applied them.
443+
// Compiler errors refer to the final expression after applying Fix-It(s).
444444
if (!m_fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) {
445-
error_stream.Printf(" Fix-it applied, fixed expression was: \n %s\n",
446-
m_fixed_expression.c_str());
445+
error_stream << " Evaluated this expression after applying Fix-It(s):\n";
446+
error_stream << " " << m_fixed_expression << "\n";
447447
}
448448

449449
if (result_valobj_sp) {

lldb/test/API/commands/expression/fixits/TestFixIts.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def test_with_dummy_target(self):
2222
self.assertEqual(
2323
result, lldb.eReturnStatusSuccessFinishResult, ret_val.GetError()
2424
)
25-
self.assertIn("Fix-it applied", ret_val.GetError())
25+
self.assertIn(
26+
"Evaluated this expression after applying Fix-It(s):", ret_val.GetError()
27+
)
2628

2729
def test_with_target(self):
2830
"""Test calling expressions with errors that can be fixed by the FixIts."""
@@ -99,7 +101,9 @@ def test_with_target_error_applies_fixit(self):
99101
)
100102
self.assertEqual(result, lldb.eReturnStatusFailed, ret_val.GetError())
101103

102-
self.assertIn("Fix-it applied, fixed expression was:", ret_val.GetError())
104+
self.assertIn(
105+
"Evaluated this expression after applying Fix-It(s):", ret_val.GetError()
106+
)
103107
self.assertIn("null_pointer->first", ret_val.GetError())
104108

105109
# The final function call runs into SIGILL on aarch64-linux.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Tests a C++ fixit for the `expr` command and
3+
`po` alias (aka DWIM aka "do what I mean") alias.
4+
"""
5+
import lldb
6+
from lldbsuite.test.decorators import *
7+
from lldbsuite.test.lldbtest import *
8+
from lldbsuite.test import lldbutil
9+
10+
11+
class TestCase(TestBase):
12+
def test_fixit_with_dwim(self):
13+
"""Confirms `po` shows an expression after applying Fix-It(s)."""
14+
15+
self.build()
16+
lldbutil.run_to_source_breakpoint(
17+
self, "// break here", lldb.SBFileSpec("main.cpp")
18+
)
19+
20+
self.expect(
21+
"dwim-print -O -- class C { int i; void f() { []() { ++i; }(); } }; 42",
22+
error=True,
23+
substrs=[
24+
"Evaluated this expression after applying Fix-It(s)",
25+
"class C { int i; void f() { [this]() { ++i; }(); } }",
26+
],
27+
)
28+
29+
def test_fixit_with_expression(self):
30+
"""Confirms `expression` shows an expression after applying Fix-It(s)."""
31+
32+
self.build()
33+
lldbutil.run_to_source_breakpoint(
34+
self, "// break here", lldb.SBFileSpec("main.cpp")
35+
)
36+
37+
self.expect(
38+
"expr class C { int i; void f() { []() { ++i; }(); } }; 42",
39+
error=True,
40+
substrs=[
41+
"Evaluated this expression after applying Fix-It(s)",
42+
"class C { int i; void f() { [this]() { ++i; }(); } }",
43+
],
44+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
int main() {
2+
long foo = 1234;
3+
4+
return 0; // break here
5+
}

0 commit comments

Comments
 (0)