Skip to content

Commit

Permalink
[lldb][NFCI] Change parameter type in UserExpression::GetObjectPointer (
Browse files Browse the repository at this point in the history
#67055)

GetObjectPointer (and other related methods) do not need `ConstString`
parameters. The string parameter in these methods boil down to getting a
StringRef and calling `StackFrame::GetValueForVariableExpressionPath`
which takes a `StringRef` parameter. All the users of `GetObjectPointer`
(and related methods) end up creating ConstString objects to pass to
these methods, but they could just as easily be StringRefs (potentially
saving us some allocations in the StringPool).
  • Loading branch information
bulbazord authored Sep 22, 2023
1 parent 9c5b38b commit 55ec9db
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
5 changes: 3 additions & 2 deletions lldb/include/lldb/Expression/UserExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ class UserExpression : public Expression {
lldb::ExpressionVariableSP &result) = 0;

static lldb::addr_t GetObjectPointer(lldb::StackFrameSP frame_sp,
ConstString &object_name, Status &err);
llvm::StringRef object_name,
Status &err);

/// Return ValueObject for a given variable name in the current stack frame
///
Expand All @@ -295,7 +296,7 @@ class UserExpression : public Expression {
/// 'nullptr' (and sets the error status parameter 'err').
static lldb::ValueObjectSP
GetObjectPointerValueObject(lldb::StackFrameSP frame,
ConstString const &object_name, Status &err);
llvm::StringRef object_name, Status &err);

/// Populate m_in_cplusplus_method and m_in_objectivec_method based on the
/// environment.
Expand Down
18 changes: 8 additions & 10 deletions lldb/source/Expression/UserExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "lldb/Target/Target.h"
#include "lldb/Target/ThreadPlan.h"
#include "lldb/Target/ThreadPlanCallUserExpression.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/State.h"
Expand Down Expand Up @@ -99,21 +98,20 @@ bool UserExpression::MatchesContext(ExecutionContext &exe_ctx) {
}

lldb::ValueObjectSP UserExpression::GetObjectPointerValueObject(
lldb::StackFrameSP frame_sp, ConstString const &object_name, Status &err) {
lldb::StackFrameSP frame_sp, llvm::StringRef object_name, Status &err) {
err.Clear();

if (!frame_sp) {
err.SetErrorStringWithFormat(
"Couldn't load '%s' because the context is incomplete",
object_name.AsCString());
err.SetErrorStringWithFormatv(
"Couldn't load '{0}' because the context is incomplete", object_name);
return {};
}

lldb::VariableSP var_sp;
lldb::ValueObjectSP valobj_sp;

return frame_sp->GetValueForVariableExpressionPath(
object_name.GetStringRef(), lldb::eNoDynamicValues,
object_name, lldb::eNoDynamicValues,
StackFrame::eExpressionPathOptionCheckPtrVsMember |
StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
StackFrame::eExpressionPathOptionsNoSyntheticChildren |
Expand All @@ -122,7 +120,7 @@ lldb::ValueObjectSP UserExpression::GetObjectPointerValueObject(
}

lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp,
ConstString &object_name,
llvm::StringRef object_name,
Status &err) {
auto valobj_sp =
GetObjectPointerValueObject(std::move(frame_sp), object_name, err);
Expand All @@ -133,9 +131,9 @@ lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp,
lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);

if (ret == LLDB_INVALID_ADDRESS) {
err.SetErrorStringWithFormat(
"Couldn't load '%s' because its value couldn't be evaluated",
object_name.AsCString());
err.SetErrorStringWithFormatv(
"Couldn't load '{0}' because its value couldn't be evaluated",
object_name);
return LLDB_INVALID_ADDRESS;
}

Expand Down
29 changes: 14 additions & 15 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ bool ClangUserExpression::Complete(ExecutionContext &exe_ctx,
}

lldb::addr_t ClangUserExpression::GetCppObjectPointer(
lldb::StackFrameSP frame_sp, ConstString &object_name, Status &err) {
lldb::StackFrameSP frame_sp, llvm::StringRef object_name, Status &err) {
auto valobj_sp =
GetObjectPointerValueObject(std::move(frame_sp), object_name, err);

Expand All @@ -889,9 +889,9 @@ lldb::addr_t ClangUserExpression::GetCppObjectPointer(
lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);

if (ret == LLDB_INVALID_ADDRESS) {
err.SetErrorStringWithFormat(
"Couldn't load '%s' because its value couldn't be evaluated",
object_name.AsCString());
err.SetErrorStringWithFormatv(
"Couldn't load '{0}' because its value couldn't be evaluated",
object_name);
return LLDB_INVALID_ADDRESS;
}

Expand All @@ -910,19 +910,18 @@ bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
if (!frame_sp)
return true;

ConstString object_name;

if (m_in_cplusplus_method) {
object_name.SetCString("this");
} else if (m_in_objectivec_method) {
object_name.SetCString("self");
} else {
if (!m_in_cplusplus_method && !m_in_objectivec_method) {
diagnostic_manager.PutString(
eDiagnosticSeverityError,
"need object pointer but don't know the language");
return false;
}

static constexpr llvm::StringLiteral g_cplusplus_object_name("this");
static constexpr llvm::StringLiteral g_objc_object_name("self");
llvm::StringRef object_name =
m_in_cplusplus_method ? g_cplusplus_object_name : g_objc_object_name;

Status object_ptr_error;

if (m_ctx_obj) {
Expand All @@ -942,14 +941,14 @@ bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
}

if (!object_ptr_error.Success()) {
exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
"warning: `%s' is not accessible (substituting 0). %s\n",
object_name.AsCString(), object_ptr_error.AsCString());
exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Format(
"warning: `{0}' is not accessible (substituting 0). {1}\n",
object_name, object_ptr_error.AsCString());
object_ptr = 0;
}

if (m_in_objectivec_method) {
ConstString cmd_name("_cmd");
static constexpr llvm::StringLiteral cmd_name("_cmd");

cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class ClangUserExpression : public LLVMUserExpression {
bool for_completion);

lldb::addr_t GetCppObjectPointer(lldb::StackFrameSP frame,
ConstString &object_name, Status &err);
llvm::StringRef object_name, Status &err);

/// Defines how the current expression should be wrapped.
ClangExpressionSourceCode::WrapKind GetWrapKind() const;
Expand Down

0 comments on commit 55ec9db

Please sign in to comment.