[CommandInterpreter] Simplify PreprocessCommand. (NFCI)

Simplify some code in PreprocessCommand. This change improves
consistency, reduces the indentation and makes the code easier to follow
overall.

llvm-svn: 350166
This commit is contained in:
Jonas Devlieghere 2018-12-30 17:56:30 +00:00
parent 9d78c60bf4
commit 76c6feafac
1 changed files with 123 additions and 113 deletions

View File

@ -1455,130 +1455,140 @@ Status CommandInterpreter::PreprocessCommand(std::string &command) {
size_t start_backtick; size_t start_backtick;
size_t pos = 0; size_t pos = 0;
while ((start_backtick = command.find('`', pos)) != std::string::npos) { while ((start_backtick = command.find('`', pos)) != std::string::npos) {
// Stop if an error was encountered during the previous iteration.
if (error.Fail())
break;
if (start_backtick > 0 && command[start_backtick - 1] == '\\') { if (start_backtick > 0 && command[start_backtick - 1] == '\\') {
// The backtick was preceded by a '\' character, remove the slash and // The backtick was preceded by a '\' character, remove the slash and
// don't treat the backtick as the start of an expression // don't treat the backtick as the start of an expression.
command.erase(start_backtick - 1, 1); command.erase(start_backtick - 1, 1);
// No need to add one to start_backtick since we just deleted a char // No need to add one to start_backtick since we just deleted a char.
pos = start_backtick; pos = start_backtick;
} else { continue;
const size_t expr_content_start = start_backtick + 1; }
const size_t end_backtick = command.find('`', expr_content_start);
if (end_backtick == std::string::npos)
return error;
else if (end_backtick == expr_content_start) {
// Empty expression (two backticks in a row)
command.erase(start_backtick, 2);
} else {
std::string expr_str(command, expr_content_start,
end_backtick - expr_content_start);
ExecutionContext exe_ctx(GetExecutionContext()); const size_t expr_content_start = start_backtick + 1;
Target *target = exe_ctx.GetTargetPtr(); const size_t end_backtick = command.find('`', expr_content_start);
// Get a dummy target to allow for calculator mode while processing
// backticks. This also helps break the infinite loop caused when
// target is null.
if (!target)
target = m_debugger.GetDummyTarget();
if (target) {
ValueObjectSP expr_result_valobj_sp;
EvaluateExpressionOptions options; if (end_backtick == std::string::npos) {
options.SetCoerceToId(false); // Stop if there's no end backtick.
options.SetUnwindOnError(true); break;
options.SetIgnoreBreakpoints(true); }
options.SetKeepInMemory(false);
options.SetTryAllThreads(true);
options.SetTimeout(llvm::None);
ExpressionResults expr_result = target->EvaluateExpression( if (end_backtick == expr_content_start) {
expr_str.c_str(), exe_ctx.GetFramePtr(), expr_result_valobj_sp, // Skip over empty expression. (two backticks in a row)
options); command.erase(start_backtick, 2);
continue;
}
if (expr_result == eExpressionCompleted) { std::string expr_str(command, expr_content_start,
Scalar scalar; end_backtick - expr_content_start);
if (expr_result_valobj_sp)
expr_result_valobj_sp =
expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable(
expr_result_valobj_sp->GetDynamicValueType(), true);
if (expr_result_valobj_sp->ResolveValue(scalar)) {
command.erase(start_backtick, end_backtick - start_backtick + 1);
StreamString value_strm;
const bool show_type = false;
scalar.GetValue(&value_strm, show_type);
size_t value_string_size = value_strm.GetSize();
if (value_string_size) {
command.insert(start_backtick, value_strm.GetString());
pos = start_backtick + value_string_size;
continue;
} else {
error.SetErrorStringWithFormat("expression value didn't result "
"in a scalar value for the "
"expression '%s'",
expr_str.c_str());
}
} else {
error.SetErrorStringWithFormat("expression value didn't result "
"in a scalar value for the "
"expression '%s'",
expr_str.c_str());
}
} else {
if (expr_result_valobj_sp)
error = expr_result_valobj_sp->GetError();
if (error.Success()) {
switch (expr_result) { ExecutionContext exe_ctx(GetExecutionContext());
case eExpressionSetupError: Target *target = exe_ctx.GetTargetPtr();
error.SetErrorStringWithFormat(
"expression setup error for the expression '%s'", // Get a dummy target to allow for calculator mode while processing
expr_str.c_str()); // backticks. This also helps break the infinite loop caused when target is
break; // null.
case eExpressionParseError: if (!target)
error.SetErrorStringWithFormat( target = m_debugger.GetDummyTarget();
"expression parse error for the expression '%s'",
expr_str.c_str()); if (!target)
break; continue;
case eExpressionResultUnavailable:
error.SetErrorStringWithFormat( ValueObjectSP expr_result_valobj_sp;
"expression error fetching result for the expression '%s'",
expr_str.c_str()); EvaluateExpressionOptions options;
break; options.SetCoerceToId(false);
case eExpressionCompleted: options.SetUnwindOnError(true);
break; options.SetIgnoreBreakpoints(true);
case eExpressionDiscarded: options.SetKeepInMemory(false);
error.SetErrorStringWithFormat( options.SetTryAllThreads(true);
"expression discarded for the expression '%s'", options.SetTimeout(llvm::None);
expr_str.c_str());
break; ExpressionResults expr_result =
case eExpressionInterrupted: target->EvaluateExpression(expr_str.c_str(), exe_ctx.GetFramePtr(),
error.SetErrorStringWithFormat( expr_result_valobj_sp, options);
"expression interrupted for the expression '%s'",
expr_str.c_str()); if (expr_result == eExpressionCompleted) {
break; Scalar scalar;
case eExpressionHitBreakpoint: if (expr_result_valobj_sp)
error.SetErrorStringWithFormat( expr_result_valobj_sp =
"expression hit breakpoint for the expression '%s'", expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable(
expr_str.c_str()); expr_result_valobj_sp->GetDynamicValueType(), true);
break; if (expr_result_valobj_sp->ResolveValue(scalar)) {
case eExpressionTimedOut: command.erase(start_backtick, end_backtick - start_backtick + 1);
error.SetErrorStringWithFormat( StreamString value_strm;
"expression timed out for the expression '%s'", const bool show_type = false;
expr_str.c_str()); scalar.GetValue(&value_strm, show_type);
break; size_t value_string_size = value_strm.GetSize();
case eExpressionStoppedForDebug: if (value_string_size) {
error.SetErrorStringWithFormat("expression stop at entry point " command.insert(start_backtick, value_strm.GetString());
"for debugging for the " pos = start_backtick + value_string_size;
"expression '%s'", continue;
expr_str.c_str()); } else {
break; error.SetErrorStringWithFormat("expression value didn't result "
} "in a scalar value for the "
} "expression '%s'",
} expr_str.c_str());
break;
} }
} } else {
if (error.Fail()) error.SetErrorStringWithFormat("expression value didn't result "
"in a scalar value for the "
"expression '%s'",
expr_str.c_str());
break; break;
}
continue;
}
if (expr_result_valobj_sp)
error = expr_result_valobj_sp->GetError();
if (error.Success()) {
switch (expr_result) {
case eExpressionSetupError:
error.SetErrorStringWithFormat(
"expression setup error for the expression '%s'", expr_str.c_str());
break;
case eExpressionParseError:
error.SetErrorStringWithFormat(
"expression parse error for the expression '%s'", expr_str.c_str());
break;
case eExpressionResultUnavailable:
error.SetErrorStringWithFormat(
"expression error fetching result for the expression '%s'",
expr_str.c_str());
break;
case eExpressionCompleted:
break;
case eExpressionDiscarded:
error.SetErrorStringWithFormat(
"expression discarded for the expression '%s'", expr_str.c_str());
break;
case eExpressionInterrupted:
error.SetErrorStringWithFormat(
"expression interrupted for the expression '%s'", expr_str.c_str());
break;
case eExpressionHitBreakpoint:
error.SetErrorStringWithFormat(
"expression hit breakpoint for the expression '%s'",
expr_str.c_str());
break;
case eExpressionTimedOut:
error.SetErrorStringWithFormat(
"expression timed out for the expression '%s'", expr_str.c_str());
break;
case eExpressionStoppedForDebug:
error.SetErrorStringWithFormat("expression stop at entry point "
"for debugging for the "
"expression '%s'",
expr_str.c_str());
break;
}
} }
} }
return error; return error;