Skip to content

[lldb] Fix po alias by printing fix-its to the console. #68755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lldb/source/Commands/CommandObjectDWIMPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,19 @@ bool CommandObjectDWIMPrint::DoExecute(StringRef command,
{
auto *exe_scope = m_exe_ctx.GetBestExecutionContextScope();
ValueObjectSP valobj_sp;
ExpressionResults expr_result =
target.EvaluateExpression(expr, exe_scope, valobj_sp, eval_options);
std::string fixed_expression;

ExpressionResults expr_result = target.EvaluateExpression(
expr, exe_scope, valobj_sp, eval_options, &fixed_expression);

// Only mention Fix-Its if the expression evaluator applied them.
// Compiler errors refer to the final expression after applying Fix-It(s).
if (!fixed_expression.empty() && target.GetEnableNotifyAboutFixIts()) {
Stream &error_stream = result.GetErrorStream();
error_stream << " Evaluated this expression after applying Fix-It(s):\n";
error_stream << " " << fixed_expression << "\n";
}

if (expr_result == eExpressionCompleted) {
if (verbosity != eDWIMPrintVerbosityNone) {
StringRef flags;
Expand Down
8 changes: 4 additions & 4 deletions lldb/source/Commands/CommandObjectExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
ExpressionResults success = target.EvaluateExpression(
expr, frame, result_valobj_sp, eval_options, &m_fixed_expression);

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

if (result_valobj_sp) {
Expand Down
8 changes: 6 additions & 2 deletions lldb/test/API/commands/expression/fixits/TestFixIts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def test_with_dummy_target(self):
self.assertEqual(
result, lldb.eReturnStatusSuccessFinishResult, ret_val.GetError()
)
self.assertIn("Fix-it applied", ret_val.GetError())
self.assertIn(
"Evaluated this expression after applying Fix-It(s):", ret_val.GetError()
)

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

self.assertIn("Fix-it applied, fixed expression was:", ret_val.GetError())
self.assertIn(
"Evaluated this expression after applying Fix-It(s):", ret_val.GetError()
)
self.assertIn("null_pointer->first", ret_val.GetError())

# The final function call runs into SIGILL on aarch64-linux.
Expand Down
3 changes: 3 additions & 0 deletions lldb/test/API/lang/cpp/fixits/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CXX_SOURCES := main.cpp

include Makefile.rules
44 changes: 44 additions & 0 deletions lldb/test/API/lang/cpp/fixits/TestCppFixIts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Tests a C++ fixit for the `expr` command and
`po` alias (aka DWIM aka "do what I mean") alias.
"""
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil


class TestCase(TestBase):
def test_fixit_with_dwim(self):
"""Confirms `po` shows an expression after applying Fix-It(s)."""

self.build()
lldbutil.run_to_source_breakpoint(
self, "// break here", lldb.SBFileSpec("main.cpp")
)

self.expect(
"dwim-print -O -- class C { int i; void f() { []() { ++i; }(); } }; 42",
error=True,
substrs=[
"Evaluated this expression after applying Fix-It(s)",
"class C { int i; void f() { [this]() { ++i; }(); } }",
],
)

def test_fixit_with_expression(self):
"""Confirms `expression` shows an expression after applying Fix-It(s)."""

self.build()
lldbutil.run_to_source_breakpoint(
self, "// break here", lldb.SBFileSpec("main.cpp")
)

self.expect(
"expr class C { int i; void f() { []() { ++i; }(); } }; 42",
error=True,
substrs=[
"Evaluated this expression after applying Fix-It(s)",
"class C { int i; void f() { [this]() { ++i; }(); } }",
],
)
5 changes: 5 additions & 0 deletions lldb/test/API/lang/cpp/fixits/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
int main() {
long foo = 1234;

return 0; // break here
}