Skip to content

Commit 606f89a

Browse files
authored
[lldb] Fix po alias by printing fix-its to the console. (#68452)
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 Co-authored-by: Pete Lawrence <plawrence@apple.com>
1 parent bfa7d10 commit 606f89a

File tree

8 files changed

+81
-6
lines changed

8 files changed

+81
-6
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) {
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Tests whether the do-what-I-mean (DWIM) print `po` alias applies FixIts like `expr` does
3+
"""
4+
import lldb
5+
from lldbsuite.test.decorators import *
6+
from lldbsuite.test.lldbtest import *
7+
from lldbsuite.test import lldbutil
8+
9+
10+
class TestCase(TestBase):
11+
def test_with_run_command(self):
12+
"""Confirms `po` shows an expression after applying Fix-It(s)."""
13+
14+
self.build()
15+
lldbutil.run_to_source_breakpoint(
16+
self, "// break here", lldb.SBFileSpec("main.cpp")
17+
)
18+
19+
self.expect(
20+
"dwim-print -O -- class C { int i; void f() { []() { ++i; }(); } }; 42",
21+
error = True,
22+
substrs=["Evaluated this expression after applying Fix-It(s)",
23+
"class C { int i; void f() { [this]() { ++i; }(); } }"],
24+
)
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+
}
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Tests whether the expression command applies FixIts
3+
"""
4+
import lldb
5+
from lldbsuite.test.decorators import *
6+
from lldbsuite.test.lldbtest import *
7+
from lldbsuite.test import lldbutil
8+
9+
10+
class TestCase(TestBase):
11+
def test_with_run_command(self):
12+
"""Confirms `expression` shows an expression after applying Fix-It(s)."""
13+
14+
self.build()
15+
lldbutil.run_to_source_breakpoint(
16+
self, "// break here", lldb.SBFileSpec("main.cpp")
17+
)
18+
19+
self.expect(
20+
"expr class C { int i; void f() { []() { ++i; }(); } }; 42",
21+
error = True,
22+
substrs=["Evaluated this expression after applying Fix-It(s)",
23+
"class C { int i; void f() { [this]() { ++i; }(); } }"],
24+
)
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)