Skip to content

Commit e6d04c1

Browse files
committed
Use std::function and remove the boilerplate in the SB API
1 parent 3586cf4 commit e6d04c1

File tree

3 files changed

+17
-51
lines changed

3 files changed

+17
-51
lines changed

lldb/include/lldb/Interpreter/CommandInterpreter.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,9 @@ class CommandInterpreter : public Broadcaster,
254254
eCommandTypesAllThem = 0xFFFF //< all commands
255255
};
256256

257-
typedef lldb::CommandReturnObjectCallbackResult (
258-
*CommandReturnObjectCallback)(CommandReturnObject &, void *);
257+
using CommandReturnObjectCallback =
258+
std::function<lldb::CommandReturnObjectCallbackResult(
259+
CommandReturnObject &)>;
259260

260261
// The CommandAlias and CommandInterpreter both have a hand in
261262
// substituting for alias commands. They work by writing special tokens
@@ -668,8 +669,7 @@ class CommandInterpreter : public Broadcaster,
668669
++m_command_usages[cmd_obj.GetCommandName()];
669670
}
670671

671-
void SetPrintCallback(CommandReturnObjectCallback callback,
672-
lldb::BatonSP baton_sp);
672+
void SetPrintCallback(CommandReturnObjectCallback callback);
673673

674674
llvm::json::Value GetStatistics();
675675
const StructuredData::Array &GetTranscript() const;
@@ -782,10 +782,7 @@ class CommandInterpreter : public Broadcaster,
782782
CommandInterpreterRunResult m_result;
783783

784784
/// An optional callback to handle printing the CommandReturnObject.
785-
/// @{
786-
CommandReturnObjectCallback m_print_callback = nullptr;
787-
lldb::BatonSP m_print_callback_baton_sp;
788-
/// @}
785+
CommandReturnObjectCallback m_print_callback;
789786

790787
// The exit code the user has requested when calling the 'quit' command.
791788
// No value means the user hasn't set a custom exit code so far.

lldb/source/API/SBCommandInterpreter.cpp

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs)
9898

9999
SBCommandInterpreter::~SBCommandInterpreter() = default;
100100

101-
const SBCommandInterpreter &SBCommandInterpreter::
102-
operator=(const SBCommandInterpreter &rhs) {
101+
const SBCommandInterpreter &
102+
SBCommandInterpreter::operator=(const SBCommandInterpreter &rhs) {
103103
LLDB_INSTRUMENT_VA(this, rhs);
104104

105105
m_opaque_ptr = rhs.m_opaque_ptr;
@@ -222,8 +222,7 @@ void SBCommandInterpreter::HandleCommandsFromFile(
222222
if (override_context.get())
223223
m_opaque_ptr->HandleCommandsFromFile(tmp_spec,
224224
override_context.get()->Lock(true),
225-
options.ref(),
226-
result.ref());
225+
options.ref(), result.ref());
227226

228227
else
229228
m_opaque_ptr->HandleCommandsFromFile(tmp_spec, options.ref(), result.ref());
@@ -649,7 +648,8 @@ SBCommand::operator bool() const {
649648
const char *SBCommand::GetName() {
650649
LLDB_INSTRUMENT_VA(this);
651650

652-
return (IsValid() ? ConstString(m_opaque_sp->GetCommandName()).AsCString() : nullptr);
651+
return (IsValid() ? ConstString(m_opaque_sp->GetCommandName()).AsCString()
652+
: nullptr);
653653
}
654654

655655
const char *SBCommand::GetHelp() {
@@ -744,40 +744,14 @@ void SBCommand::SetFlags(uint32_t flags) {
744744
m_opaque_sp->GetFlags().Set(flags);
745745
}
746746

747-
namespace lldb_private {
748-
struct CommandCallbackData {
749-
SBCommandPrintCallback callback;
750-
void *callback_baton;
751-
};
752-
753-
class CommandPrintCallbackBaton
754-
: public lldb_private::TypedBaton<CommandCallbackData> {
755-
public:
756-
CommandPrintCallbackBaton(SBCommandPrintCallback callback, void *baton)
757-
: TypedBaton(std::make_unique<CommandCallbackData>()) {
758-
getItem()->callback = callback;
759-
getItem()->callback_baton = baton;
760-
}
761-
762-
static lldb::CommandReturnObjectCallbackResult
763-
PrivateCallback(lldb_private::CommandReturnObject &result, void *baton) {
764-
if (baton) {
765-
CommandCallbackData *data = (CommandCallbackData *)baton;
766-
SBCommandReturnObject sb_result(result);
767-
return data->callback(sb_result, data->callback_baton);
768-
}
769-
return eCommandReturnObjectPrintCallbackSkipped;
770-
}
771-
};
772-
} // namespace lldb_private
773-
774747
void SBCommandInterpreter::SetPrintCallback(
775748
lldb::SBCommandPrintCallback callback, void *baton) {
776749
LLDB_INSTRUMENT_VA(this, callback, baton);
777750

778-
BatonSP baton_sp =
779-
std::make_shared<CommandPrintCallbackBaton>(callback, baton);
780751
if (m_opaque_ptr)
781752
return m_opaque_ptr->SetPrintCallback(
782-
&CommandPrintCallbackBaton::PrivateCallback, baton_sp);
753+
[callback, baton](lldb_private::CommandReturnObject &result) {
754+
SBCommandReturnObject sb_result(result);
755+
return callback(sb_result, baton);
756+
});
783757
}

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,11 +3215,7 @@ void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler,
32153215
};
32163216

32173217
if (m_print_callback) {
3218-
void *baton = m_print_callback_baton_sp
3219-
? m_print_callback_baton_sp->data()
3220-
: nullptr;
3221-
lldb::CommandReturnObjectCallbackResult callback_result =
3222-
m_print_callback(result, baton);
3218+
const auto callback_result = m_print_callback(result);
32233219
if (callback_result == eCommandReturnObjectPrintCallbackSkipped)
32243220
DefaultPrintCallback(result);
32253221
} else {
@@ -3675,8 +3671,7 @@ const StructuredData::Array &CommandInterpreter::GetTranscript() const {
36753671
return m_transcript;
36763672
}
36773673

3678-
void CommandInterpreter::SetPrintCallback(CommandReturnObjectCallback callback,
3679-
lldb::BatonSP baton_sp) {
3674+
void CommandInterpreter::SetPrintCallback(
3675+
CommandReturnObjectCallback callback) {
36803676
m_print_callback = callback;
3681-
m_print_callback_baton_sp = baton_sp;
36823677
}

0 commit comments

Comments
 (0)