Skip to content

Commit 55ec9db

Browse files
authored
[lldb][NFCI] Change parameter type in UserExpression::GetObjectPointer (#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).
1 parent 9c5b38b commit 55ec9db

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

lldb/include/lldb/Expression/UserExpression.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ class UserExpression : public Expression {
278278
lldb::ExpressionVariableSP &result) = 0;
279279

280280
static lldb::addr_t GetObjectPointer(lldb::StackFrameSP frame_sp,
281-
ConstString &object_name, Status &err);
281+
llvm::StringRef object_name,
282+
Status &err);
282283

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

300301
/// Populate m_in_cplusplus_method and m_in_objectivec_method based on the
301302
/// environment.

lldb/source/Expression/UserExpression.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "lldb/Target/Target.h"
3636
#include "lldb/Target/ThreadPlan.h"
3737
#include "lldb/Target/ThreadPlanCallUserExpression.h"
38-
#include "lldb/Utility/ConstString.h"
3938
#include "lldb/Utility/LLDBLog.h"
4039
#include "lldb/Utility/Log.h"
4140
#include "lldb/Utility/State.h"
@@ -99,21 +98,20 @@ bool UserExpression::MatchesContext(ExecutionContext &exe_ctx) {
9998
}
10099

101100
lldb::ValueObjectSP UserExpression::GetObjectPointerValueObject(
102-
lldb::StackFrameSP frame_sp, ConstString const &object_name, Status &err) {
101+
lldb::StackFrameSP frame_sp, llvm::StringRef object_name, Status &err) {
103102
err.Clear();
104103

105104
if (!frame_sp) {
106-
err.SetErrorStringWithFormat(
107-
"Couldn't load '%s' because the context is incomplete",
108-
object_name.AsCString());
105+
err.SetErrorStringWithFormatv(
106+
"Couldn't load '{0}' because the context is incomplete", object_name);
109107
return {};
110108
}
111109

112110
lldb::VariableSP var_sp;
113111
lldb::ValueObjectSP valobj_sp;
114112

115113
return frame_sp->GetValueForVariableExpressionPath(
116-
object_name.GetStringRef(), lldb::eNoDynamicValues,
114+
object_name, lldb::eNoDynamicValues,
117115
StackFrame::eExpressionPathOptionCheckPtrVsMember |
118116
StackFrame::eExpressionPathOptionsNoFragileObjcIvar |
119117
StackFrame::eExpressionPathOptionsNoSyntheticChildren |
@@ -122,7 +120,7 @@ lldb::ValueObjectSP UserExpression::GetObjectPointerValueObject(
122120
}
123121

124122
lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp,
125-
ConstString &object_name,
123+
llvm::StringRef object_name,
126124
Status &err) {
127125
auto valobj_sp =
128126
GetObjectPointerValueObject(std::move(frame_sp), object_name, err);
@@ -133,9 +131,9 @@ lldb::addr_t UserExpression::GetObjectPointer(lldb::StackFrameSP frame_sp,
133131
lldb::addr_t ret = valobj_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
134132

135133
if (ret == LLDB_INVALID_ADDRESS) {
136-
err.SetErrorStringWithFormat(
137-
"Couldn't load '%s' because its value couldn't be evaluated",
138-
object_name.AsCString());
134+
err.SetErrorStringWithFormatv(
135+
"Couldn't load '{0}' because its value couldn't be evaluated",
136+
object_name);
139137
return LLDB_INVALID_ADDRESS;
140138
}
141139

lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ bool ClangUserExpression::Complete(ExecutionContext &exe_ctx,
872872
}
873873

874874
lldb::addr_t ClangUserExpression::GetCppObjectPointer(
875-
lldb::StackFrameSP frame_sp, ConstString &object_name, Status &err) {
875+
lldb::StackFrameSP frame_sp, llvm::StringRef object_name, Status &err) {
876876
auto valobj_sp =
877877
GetObjectPointerValueObject(std::move(frame_sp), object_name, err);
878878

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

891891
if (ret == LLDB_INVALID_ADDRESS) {
892-
err.SetErrorStringWithFormat(
893-
"Couldn't load '%s' because its value couldn't be evaluated",
894-
object_name.AsCString());
892+
err.SetErrorStringWithFormatv(
893+
"Couldn't load '{0}' because its value couldn't be evaluated",
894+
object_name);
895895
return LLDB_INVALID_ADDRESS;
896896
}
897897

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

913-
ConstString object_name;
914-
915-
if (m_in_cplusplus_method) {
916-
object_name.SetCString("this");
917-
} else if (m_in_objectivec_method) {
918-
object_name.SetCString("self");
919-
} else {
913+
if (!m_in_cplusplus_method && !m_in_objectivec_method) {
920914
diagnostic_manager.PutString(
921915
eDiagnosticSeverityError,
922916
"need object pointer but don't know the language");
923917
return false;
924918
}
925919

920+
static constexpr llvm::StringLiteral g_cplusplus_object_name("this");
921+
static constexpr llvm::StringLiteral g_objc_object_name("self");
922+
llvm::StringRef object_name =
923+
m_in_cplusplus_method ? g_cplusplus_object_name : g_objc_object_name;
924+
926925
Status object_ptr_error;
927926

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

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

951950
if (m_in_objectivec_method) {
952-
ConstString cmd_name("_cmd");
951+
static constexpr llvm::StringLiteral cmd_name("_cmd");
953952

954953
cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
955954

lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class ClangUserExpression : public LLVMUserExpression {
204204
bool for_completion);
205205

206206
lldb::addr_t GetCppObjectPointer(lldb::StackFrameSP frame,
207-
ConstString &object_name, Status &err);
207+
llvm::StringRef object_name, Status &err);
208208

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

0 commit comments

Comments
 (0)