Skip to content

Commit 3011c7e

Browse files
committed
[lldb][NFC] Make LLVMUserExpression::DoExecute return early
The giant if-else isn't conforming to LLVM code style.
1 parent c09c065 commit 3011c7e

File tree

1 file changed

+142
-144
lines changed

1 file changed

+142
-144
lines changed

lldb/source/Expression/LLVMUserExpression.cpp

+142-144
Original file line numberDiff line numberDiff line change
@@ -70,174 +70,172 @@ LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
7070
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EXPRESSIONS |
7171
LIBLLDB_LOG_STEP));
7272

73-
if (m_jit_start_addr != LLDB_INVALID_ADDRESS || m_can_interpret) {
74-
lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
73+
if (m_jit_start_addr == LLDB_INVALID_ADDRESS && !m_can_interpret) {
74+
diagnostic_manager.PutString(
75+
eDiagnosticSeverityError,
76+
"Expression can't be run, because there is no JIT compiled function");
77+
return lldb::eExpressionSetupError;
78+
}
7579

76-
if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
77-
struct_address)) {
78-
diagnostic_manager.Printf(
80+
lldb::addr_t struct_address = LLDB_INVALID_ADDRESS;
81+
82+
if (!PrepareToExecuteJITExpression(diagnostic_manager, exe_ctx,
83+
struct_address)) {
84+
diagnostic_manager.Printf(
85+
eDiagnosticSeverityError,
86+
"errored out in %s, couldn't PrepareToExecuteJITExpression",
87+
__FUNCTION__);
88+
return lldb::eExpressionSetupError;
89+
}
90+
91+
lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
92+
lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
93+
94+
if (m_can_interpret) {
95+
llvm::Module *module = m_execution_unit_sp->GetModule();
96+
llvm::Function *function = m_execution_unit_sp->GetFunction();
97+
98+
if (!module || !function) {
99+
diagnostic_manager.PutString(
79100
eDiagnosticSeverityError,
80-
"errored out in %s, couldn't PrepareToExecuteJITExpression",
81-
__FUNCTION__);
101+
"supposed to interpret, but nothing is there");
82102
return lldb::eExpressionSetupError;
83103
}
84104

85-
lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
86-
lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
105+
Status interpreter_error;
87106

88-
if (m_can_interpret) {
89-
llvm::Module *module = m_execution_unit_sp->GetModule();
90-
llvm::Function *function = m_execution_unit_sp->GetFunction();
107+
std::vector<lldb::addr_t> args;
91108

92-
if (!module || !function) {
93-
diagnostic_manager.PutString(
94-
eDiagnosticSeverityError,
95-
"supposed to interpret, but nothing is there");
96-
return lldb::eExpressionSetupError;
97-
}
109+
if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
110+
diagnostic_manager.Printf(eDiagnosticSeverityError,
111+
"errored out in %s, couldn't AddArguments",
112+
__FUNCTION__);
113+
return lldb::eExpressionSetupError;
114+
}
98115

99-
Status interpreter_error;
116+
function_stack_bottom = m_stack_frame_bottom;
117+
function_stack_top = m_stack_frame_top;
100118

101-
std::vector<lldb::addr_t> args;
119+
IRInterpreter::Interpret(*module, *function, args, *m_execution_unit_sp,
120+
interpreter_error, function_stack_bottom,
121+
function_stack_top, exe_ctx);
102122

103-
if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
104-
diagnostic_manager.Printf(eDiagnosticSeverityError,
105-
"errored out in %s, couldn't AddArguments",
106-
__FUNCTION__);
107-
return lldb::eExpressionSetupError;
108-
}
123+
if (!interpreter_error.Success()) {
124+
diagnostic_manager.Printf(eDiagnosticSeverityError,
125+
"supposed to interpret, but failed: %s",
126+
interpreter_error.AsCString());
127+
return lldb::eExpressionDiscarded;
128+
}
129+
} else {
130+
if (!exe_ctx.HasThreadScope()) {
131+
diagnostic_manager.Printf(eDiagnosticSeverityError,
132+
"%s called with no thread selected",
133+
__FUNCTION__);
134+
return lldb::eExpressionSetupError;
135+
}
109136

110-
function_stack_bottom = m_stack_frame_bottom;
111-
function_stack_top = m_stack_frame_top;
137+
Address wrapper_address(m_jit_start_addr);
112138

113-
IRInterpreter::Interpret(*module, *function, args, *m_execution_unit_sp,
114-
interpreter_error, function_stack_bottom,
115-
function_stack_top, exe_ctx);
139+
std::vector<lldb::addr_t> args;
116140

117-
if (!interpreter_error.Success()) {
118-
diagnostic_manager.Printf(eDiagnosticSeverityError,
119-
"supposed to interpret, but failed: %s",
120-
interpreter_error.AsCString());
121-
return lldb::eExpressionDiscarded;
122-
}
123-
} else {
124-
if (!exe_ctx.HasThreadScope()) {
125-
diagnostic_manager.Printf(eDiagnosticSeverityError,
126-
"%s called with no thread selected",
127-
__FUNCTION__);
128-
return lldb::eExpressionSetupError;
129-
}
141+
if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
142+
diagnostic_manager.Printf(eDiagnosticSeverityError,
143+
"errored out in %s, couldn't AddArguments",
144+
__FUNCTION__);
145+
return lldb::eExpressionSetupError;
146+
}
147+
148+
lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
149+
exe_ctx.GetThreadRef(), wrapper_address, args, options,
150+
shared_ptr_to_me));
151+
152+
StreamString ss;
153+
if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
154+
diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
155+
return lldb::eExpressionSetupError;
156+
}
130157

131-
Address wrapper_address(m_jit_start_addr);
158+
ThreadPlanCallUserExpression *user_expression_plan =
159+
static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
132160

133-
std::vector<lldb::addr_t> args;
161+
lldb::addr_t function_stack_pointer =
162+
user_expression_plan->GetFunctionStackPointer();
134163

135-
if (!AddArguments(exe_ctx, args, struct_address, diagnostic_manager)) {
136-
diagnostic_manager.Printf(eDiagnosticSeverityError,
137-
"errored out in %s, couldn't AddArguments",
138-
__FUNCTION__);
139-
return lldb::eExpressionSetupError;
140-
}
164+
function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
165+
function_stack_top = function_stack_pointer;
141166

142-
lldb::ThreadPlanSP call_plan_sp(new ThreadPlanCallUserExpression(
143-
exe_ctx.GetThreadRef(), wrapper_address, args, options,
144-
shared_ptr_to_me));
167+
LLDB_LOGF(log,
168+
"-- [UserExpression::Execute] Execution of expression begins --");
145169

146-
StreamString ss;
147-
if (!call_plan_sp || !call_plan_sp->ValidatePlan(&ss)) {
148-
diagnostic_manager.PutString(eDiagnosticSeverityError, ss.GetString());
149-
return lldb::eExpressionSetupError;
150-
}
170+
if (exe_ctx.GetProcessPtr())
171+
exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
172+
173+
lldb::ExpressionResults execution_result =
174+
exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
175+
diagnostic_manager);
176+
177+
if (exe_ctx.GetProcessPtr())
178+
exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
151179

152-
ThreadPlanCallUserExpression *user_expression_plan =
153-
static_cast<ThreadPlanCallUserExpression *>(call_plan_sp.get());
154-
155-
lldb::addr_t function_stack_pointer =
156-
user_expression_plan->GetFunctionStackPointer();
157-
158-
function_stack_bottom = function_stack_pointer - HostInfo::GetPageSize();
159-
function_stack_top = function_stack_pointer;
160-
161-
LLDB_LOGF(
162-
log,
163-
"-- [UserExpression::Execute] Execution of expression begins --");
164-
165-
if (exe_ctx.GetProcessPtr())
166-
exe_ctx.GetProcessPtr()->SetRunningUserExpression(true);
167-
168-
lldb::ExpressionResults execution_result =
169-
exe_ctx.GetProcessRef().RunThreadPlan(exe_ctx, call_plan_sp, options,
170-
diagnostic_manager);
171-
172-
if (exe_ctx.GetProcessPtr())
173-
exe_ctx.GetProcessPtr()->SetRunningUserExpression(false);
174-
175-
LLDB_LOGF(log, "-- [UserExpression::Execute] Execution of expression "
176-
"completed --");
177-
178-
if (execution_result == lldb::eExpressionInterrupted ||
179-
execution_result == lldb::eExpressionHitBreakpoint) {
180-
const char *error_desc = nullptr;
181-
182-
if (call_plan_sp) {
183-
lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
184-
if (real_stop_info_sp)
185-
error_desc = real_stop_info_sp->GetDescription();
186-
}
187-
if (error_desc)
188-
diagnostic_manager.Printf(eDiagnosticSeverityError,
189-
"Execution was interrupted, reason: %s.",
190-
error_desc);
191-
else
192-
diagnostic_manager.PutString(eDiagnosticSeverityError,
193-
"Execution was interrupted.");
194-
195-
if ((execution_result == lldb::eExpressionInterrupted &&
196-
options.DoesUnwindOnError()) ||
197-
(execution_result == lldb::eExpressionHitBreakpoint &&
198-
options.DoesIgnoreBreakpoints()))
199-
diagnostic_manager.AppendMessageToDiagnostic(
200-
"The process has been returned to the state before expression "
201-
"evaluation.");
202-
else {
203-
if (execution_result == lldb::eExpressionHitBreakpoint)
204-
user_expression_plan->TransferExpressionOwnership();
205-
diagnostic_manager.AppendMessageToDiagnostic(
206-
"The process has been left at the point where it was "
207-
"interrupted, "
208-
"use \"thread return -x\" to return to the state before "
209-
"expression evaluation.");
210-
}
211-
212-
return execution_result;
213-
} else if (execution_result == lldb::eExpressionStoppedForDebug) {
214-
diagnostic_manager.PutString(
215-
eDiagnosticSeverityRemark,
216-
"Execution was halted at the first instruction of the expression "
217-
"function because \"debug\" was requested.\n"
218-
"Use \"thread return -x\" to return to the state before expression "
180+
LLDB_LOGF(log, "-- [UserExpression::Execute] Execution of expression "
181+
"completed --");
182+
183+
if (execution_result == lldb::eExpressionInterrupted ||
184+
execution_result == lldb::eExpressionHitBreakpoint) {
185+
const char *error_desc = nullptr;
186+
187+
if (call_plan_sp) {
188+
lldb::StopInfoSP real_stop_info_sp = call_plan_sp->GetRealStopInfo();
189+
if (real_stop_info_sp)
190+
error_desc = real_stop_info_sp->GetDescription();
191+
}
192+
if (error_desc)
193+
diagnostic_manager.Printf(eDiagnosticSeverityError,
194+
"Execution was interrupted, reason: %s.",
195+
error_desc);
196+
else
197+
diagnostic_manager.PutString(eDiagnosticSeverityError,
198+
"Execution was interrupted.");
199+
200+
if ((execution_result == lldb::eExpressionInterrupted &&
201+
options.DoesUnwindOnError()) ||
202+
(execution_result == lldb::eExpressionHitBreakpoint &&
203+
options.DoesIgnoreBreakpoints()))
204+
diagnostic_manager.AppendMessageToDiagnostic(
205+
"The process has been returned to the state before expression "
219206
"evaluation.");
220-
return execution_result;
221-
} else if (execution_result != lldb::eExpressionCompleted) {
222-
diagnostic_manager.Printf(
223-
eDiagnosticSeverityError,
224-
"Couldn't execute function; result was %s",
225-
Process::ExecutionResultAsCString(execution_result));
226-
return execution_result;
207+
else {
208+
if (execution_result == lldb::eExpressionHitBreakpoint)
209+
user_expression_plan->TransferExpressionOwnership();
210+
diagnostic_manager.AppendMessageToDiagnostic(
211+
"The process has been left at the point where it was "
212+
"interrupted, "
213+
"use \"thread return -x\" to return to the state before "
214+
"expression evaluation.");
227215
}
228-
}
229216

230-
if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
231-
function_stack_bottom, function_stack_top)) {
232-
return lldb::eExpressionCompleted;
233-
} else {
234-
return lldb::eExpressionResultUnavailable;
217+
return execution_result;
218+
} else if (execution_result == lldb::eExpressionStoppedForDebug) {
219+
diagnostic_manager.PutString(
220+
eDiagnosticSeverityRemark,
221+
"Execution was halted at the first instruction of the expression "
222+
"function because \"debug\" was requested.\n"
223+
"Use \"thread return -x\" to return to the state before expression "
224+
"evaluation.");
225+
return execution_result;
226+
} else if (execution_result != lldb::eExpressionCompleted) {
227+
diagnostic_manager.Printf(
228+
eDiagnosticSeverityError, "Couldn't execute function; result was %s",
229+
Process::ExecutionResultAsCString(execution_result));
230+
return execution_result;
235231
}
232+
}
233+
234+
if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result,
235+
function_stack_bottom, function_stack_top)) {
236+
return lldb::eExpressionCompleted;
236237
} else {
237-
diagnostic_manager.PutString(
238-
eDiagnosticSeverityError,
239-
"Expression can't be run, because there is no JIT compiled function");
240-
return lldb::eExpressionSetupError;
238+
return lldb::eExpressionResultUnavailable;
241239
}
242240
}
243241

0 commit comments

Comments
 (0)