Skip to content

Missing cherry picks 6.1 #9691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lldb/include/lldb/Expression/UserExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,9 @@ class UserExpression : public Expression {
/// definitions to be included when the expression is parsed.
///
/// \param[in,out] result_valobj_sp
/// If execution is successful, the result valobj is placed here.
///
/// \param[out] error
/// Filled in with an error in case the expression evaluation
/// fails to parse, run, or evaluated.
/// If execution is successful, the result valobj is placed
/// here. Otherwise its Error will contain an ExpressionError
/// with details about the failure mode.
///
/// \param[out] fixed_expression
/// If non-nullptr, the fixed expression is copied into the provided
Expand All @@ -265,7 +263,7 @@ class UserExpression : public Expression {
static lldb::ExpressionResults
Evaluate(ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options,
llvm::StringRef expr_cstr, llvm::StringRef expr_prefix,
lldb::ValueObjectSP &result_valobj_sp, Status &error,
lldb::ValueObjectSP &result_valobj_sp,
std::string *fixed_expression = nullptr,
ValueObject *ctx_obj = nullptr);

Expand Down
6 changes: 4 additions & 2 deletions lldb/source/API/SBValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,10 @@ const char *SBValue::GetObjectDescription() {
return nullptr;

llvm::Expected<std::string> str = value_sp->GetObjectDescription();
if (!str)
return ConstString("error: " + toString(str.takeError())).AsCString();
if (!str) {
llvm::consumeError(str.takeError());
return nullptr;
}
return ConstString(*str).AsCString();
}

Expand Down
7 changes: 2 additions & 5 deletions lldb/source/Expression/REPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,9 @@ void REPL::IOHandlerInputComplete(IOHandler &io_handler, std::string &code) {

const char *expr_prefix = nullptr;
lldb::ValueObjectSP result_valobj_sp;
lldb::ExpressionResults execution_results = UserExpression::Evaluate(
exe_ctx, expr_options, code.c_str(), expr_prefix, result_valobj_sp);
Status error;
lldb::ExpressionResults execution_results =
UserExpression::Evaluate(exe_ctx, expr_options, code.c_str(),
expr_prefix, result_valobj_sp, error,
nullptr); // fixed expression

if (llvm::Error err = OnExpressionEvaluated(exe_ctx, code, expr_options,
execution_results,
result_valobj_sp, error)) {
Expand Down
64 changes: 29 additions & 35 deletions lldb/source/Expression/UserExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ lldb::ExpressionResults
UserExpression::Evaluate(ExecutionContext &exe_ctx,
const EvaluateExpressionOptions &options,
llvm::StringRef expr, llvm::StringRef prefix,
lldb::ValueObjectSP &result_valobj_sp, Status &error,
lldb::ValueObjectSP &result_valobj_sp,
std::string *fixed_expression, ValueObject *ctx_obj) {
Log *log(GetLog(LLDBLog::Expressions | LLDBLog::Step));
auto set_error = [&](Status error) {
result_valobj_sp = ValueObjectConstResult::Create(
exe_ctx.GetBestExecutionContextScope(), std::move(error));
};

if (ctx_obj) {
static unsigned const ctx_type_mask = lldb::TypeFlags::eTypeIsClass |
Expand All @@ -155,8 +159,7 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
if (!(ctx_obj->GetTypeInfo() & ctx_type_mask)) {
LLDB_LOG(log, "== [UserExpression::Evaluate] Passed a context object of "
"an invalid type, can't run expressions.");
error =
Status::FromErrorString("a context object of an invalid type passed");
set_error(Status("a context object of an invalid type passed"));
return lldb::eExpressionSetupError;
}
}
Expand All @@ -168,8 +171,8 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
LLDB_LOG(log, "== [UserExpression::Evaluate] Passed a context object of "
"a reference type that can't be dereferenced, can't run "
"expressions.");
error = Status::FromErrorString(
"passed context object of an reference type cannot be deferenced");
set_error(Status(
"passed context object of an reference type cannot be deferenced"));
return lldb::eExpressionSetupError;
}

Expand All @@ -181,37 +184,34 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
const ResultType desired_type = options.DoesCoerceToId()
? UserExpression::eResultTypeId
: UserExpression::eResultTypeAny;
lldb::ExpressionResults execution_results = lldb::eExpressionSetupError;

Target *target = exe_ctx.GetTargetPtr();
if (!target) {
LLDB_LOG(log, "== [UserExpression::Evaluate] Passed a NULL target, can't "
"run expressions.");
error = Status::FromErrorString("expression passed a null target");
set_error(Status("expression passed a null target"));
return lldb::eExpressionSetupError;
}

Process *process = exe_ctx.GetProcessPtr();

if (process == nullptr && execution_policy == eExecutionPolicyAlways) {
if (!process && execution_policy == eExecutionPolicyAlways) {
LLDB_LOG(log, "== [UserExpression::Evaluate] No process, but the policy is "
"eExecutionPolicyAlways");

error = Status::FromErrorString(
"expression needed to run but couldn't: no process");
set_error(Status("expression needed to run but couldn't: no process"));

return execution_results;
return lldb::eExpressionSetupError;
}

// Since we might need to allocate memory, we need to be stopped to run
// an expression.
if (process != nullptr && process->GetState() != lldb::eStateStopped) {
error = Status::FromErrorStringWithFormatv(
if (process && process->GetState() != lldb::eStateStopped) {
set_error(Status::FromErrorStringWithFormatv(
"unable to evaluate expression while the process is {0}: the process "
"must be stopped because the expression might require allocating "
"memory.",
StateAsCString(process->GetState()));
return execution_results;
StateAsCString(process->GetState())));
return lldb::eExpressionSetupError;
}

// Explicitly force the IR interpreter to evaluate the expression when the
Expand Down Expand Up @@ -251,13 +251,14 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
language = frame->GetLanguage();
}

Status error;
lldb::UserExpressionSP user_expression_sp(
target->GetUserExpressionForLanguage(expr, full_prefix, language,
desired_type, options, ctx_obj,
error));
target->GetUserExpressionForLanguage(
expr, full_prefix, language, desired_type, options, ctx_obj, error));
if (error.Fail() || !user_expression_sp) {
LLDB_LOG(log, "== [UserExpression::Evaluate] Getting expression: {0} ==",
error.AsCString());
set_error(std::move(error));
return lldb::eExpressionSetupError;
}

Expand All @@ -268,10 +269,7 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
const bool generate_debug_info = options.GetGenerateDebugInfo();

if (options.InvokeCancelCallback(lldb::eExpressionEvaluationParse)) {
Status error = Status::FromErrorString(
"expression interrupted by callback before parse");
result_valobj_sp = ValueObjectConstResult::Create(
exe_ctx.GetBestExecutionContextScope(), std::move(error));
set_error(Status("expression interrupted by callback before parse"));
return lldb::eExpressionInterrupted;
}

Expand All @@ -287,6 +285,7 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
fixed_expression = &tmp_fixed_expression;

*fixed_expression = user_expression_sp->GetFixedText().str();
lldb::ExpressionResults execution_results = lldb::eExpressionSetupError;

// Swift Playgrounds disable fixits, but SwiftASTContext may get
// poisoned (see SwiftASTContextForExpressions::ModulesDidLoad())
Expand Down Expand Up @@ -365,15 +364,13 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
lldb::eExpressionSetupError,
"expression needed to run but couldn't"));
} else if (execution_policy == eExecutionPolicyTopLevel) {
error = Status(UserExpression::kNoResult, lldb::eErrorTypeGeneric);
set_error(Status(UserExpression::kNoResult, lldb::eErrorTypeGeneric));
return lldb::eExpressionCompleted;
} else {
if (options.InvokeCancelCallback(lldb::eExpressionEvaluationExecution)) {
error = Status::FromError(llvm::make_error<ExpressionError>(
set_error(Status::FromError(llvm::make_error<ExpressionError>(
lldb::eExpressionInterrupted,
"expression interrupted by callback before execution"));
result_valobj_sp = ValueObjectConstResult::Create(
exe_ctx.GetBestExecutionContextScope(), std::move(error));
"expression interrupted by callback before execution")));
return lldb::eExpressionInterrupted;
}

Expand Down Expand Up @@ -417,17 +414,14 @@ UserExpression::Evaluate(ExecutionContext &exe_ctx,
}

if (options.InvokeCancelCallback(lldb::eExpressionEvaluationComplete)) {
error = Status::FromError(llvm::make_error<ExpressionError>(
set_error(Status::FromError(llvm::make_error<ExpressionError>(
lldb::eExpressionInterrupted,
"expression interrupted by callback after complete"));
"expression interrupted by callback after complete")));
return lldb::eExpressionInterrupted;
}

if (result_valobj_sp.get() == nullptr) {
result_valobj_sp = ValueObjectConstResult::Create(
exe_ctx.GetBestExecutionContextScope(), std::move(error));
}

if (error.Fail())
set_error(std::move(error));
return execution_results;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1118,14 +1118,16 @@ AddArchetypeTypeAliases(std::unique_ptr<SwiftASTManipulator> &code_manipulator,
if (!runtime)
return llvm::createStringError("no runtime");

auto &typeref_typesystem = swift_ast_context.GetTypeSystemSwiftTypeRef();
auto typeref_typesystem = swift_ast_context.GetTypeSystemSwiftTypeRef();
if (!typeref_typesystem)
return llvm::createStringError("no typeref typesystem");

// Skip this for variadic generic functions.
ConstString func_name =
stack_frame.GetSymbolContext(lldb::eSymbolContextFunction)
.GetFunctionName(Mangled::ePreferMangled);
if (auto signature = SwiftLanguageRuntime::GetGenericSignature(
func_name.GetStringRef(), typeref_typesystem))
func_name.GetStringRef(), *typeref_typesystem))
if (signature->pack_expansions.size())
return llvm::createStringError("[AddArchetypeTypeAliases] Variadic "
"generic functions are not supported.");
Expand Down Expand Up @@ -1180,7 +1182,7 @@ AddArchetypeTypeAliases(std::unique_ptr<SwiftASTManipulator> &code_manipulator,
MetadataPointerInfo &info = pair.getSecond();

auto dependent_type =
typeref_typesystem.CreateGenericTypeParamType(info.depth, info.index);
typeref_typesystem->CreateGenericTypeParamType(info.depth, info.index);
auto bound_type =
runtime->BindGenericTypeParameters(stack_frame, dependent_type);
if (!bound_type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ static llvm::Expected<std::string> TransformPackType(
if (!tss)
return llvm::createStringError(llvm::errc::not_supported,
"unexpected typesystem");
auto &ts = tss->GetTypeSystemSwiftTypeRef();
auto ts = tss->GetTypeSystemSwiftTypeRef();
if (!ts)
return llvm::createStringError(llvm::errc::not_supported,
"no typeref typesystem");
using namespace swift::Demangle;
Demangler dem;
NodePointer node = ts.GetCanonicalDemangleTree(
NodePointer node = ts->GetCanonicalDemangleTree(
dem, type.GetMangledTypeName().GetStringRef());

node = TypeSystemSwiftTypeRef::Transform(dem, node, [](NodePointer n) {
Expand All @@ -73,7 +76,7 @@ static llvm::Expected<std::string> TransformPackType(
});

bool error = false;
ConstString type_name = ts.RemangleAsType(dem, node).GetMangledTypeName();
ConstString type_name = ts->RemangleAsType(dem, node).GetMangledTypeName();
swift::Demangle::DemangleOptions options;
options = swift::Demangle::DemangleOptions::SimplifiedUIDemangleOptions();
options.DisplayStdlibModule = false;
Expand Down
10 changes: 4 additions & 6 deletions lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ SwiftASTContextForExpressions *SwiftREPL::getSwiftASTContext() {
// our own copy of the AST and using this separate AST for completion.
//----------------------------------------------------------------------
if (m_swift_ast)
return m_swift_ast;
return m_swift_ast.get();

auto type_system_or_err =
m_target.GetScratchTypeSystemForLanguage(eLanguageTypeSwift, false);
Expand All @@ -585,11 +585,9 @@ SwiftASTContextForExpressions *SwiftREPL::getSwiftASTContext() {
if (!sc_list.GetSize())
return nullptr;

auto *target_swift_ast =
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
swift_ts->GetSwiftASTContext(sc_list[0]));
m_swift_ast = target_swift_ast;
return m_swift_ast;
m_swift_ast = std::static_pointer_cast<SwiftASTContextForExpressions>(
swift_ts->GetSwiftASTContext(sc_list[0]));
return m_swift_ast.get();
}

void SwiftREPL::CompleteCode(const std::string &current_code,
Expand Down
4 changes: 3 additions & 1 deletion lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace lldb_private {

class IRExecutionUnit;
class SwiftASTContextForExpressions;
typedef std::shared_ptr<SwiftASTContextForExpressions>
SwiftASTContextForExpressionsSP;

//----------------------------------------------------------------------
/// @class SwiftREPL SwiftREPL.h "lldb/Expression/SwiftREPL.h"
Expand Down Expand Up @@ -78,7 +80,7 @@ class SwiftREPL : public REPL {
CompletionRequest &request) override;

private:
SwiftASTContextForExpressions *m_swift_ast = nullptr;
SwiftASTContextForExpressionsSP m_swift_ast;
bool m_completion_module_initialized = false;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,13 +626,14 @@ SwiftUserExpression::GetTextAndSetExpressionParser(
return ParseResult::retry_no_bind_generic_params;
}

if (stack_frame) {
auto ts = m_swift_ast_ctx->GetTypeSystemSwiftTypeRef();
if (ts && stack_frame) {
// Extract the generic signature of the context.
ConstString func_name =
stack_frame->GetSymbolContext(lldb::eSymbolContextFunction)
.GetFunctionName(Mangled::ePreferMangled);
m_generic_signature = SwiftLanguageRuntime::GetGenericSignature(
func_name.GetStringRef(), m_swift_ast_ctx->GetTypeSystemSwiftTypeRef());
func_name.GetStringRef(), *ts);
}

if (m_options.GetBindGenericTypes() == lldb::eDontBind &&
Expand Down Expand Up @@ -742,9 +743,11 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
target->GetExecutableModule());
else
sc = frame->GetSymbolContext(lldb::eSymbolContextFunction);
auto *swift_ast_ctx = m_swift_scratch_ctx->GetSwiftASTContext(sc);
m_swift_ast_ctx =
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(swift_ast_ctx);
auto swift_ast_ctx = m_swift_scratch_ctx->GetSwiftASTContext(sc);
if (llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
swift_ast_ctx.get()))
m_swift_ast_ctx =
std::static_pointer_cast<SwiftASTContextForExpressions>(swift_ast_ctx);

if (!m_swift_ast_ctx)
return error("could not create a Swift AST context");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class SwiftUserExpression : public LLVMUserExpression {
};

TypeSystemSwiftTypeRefForExpressionsSP m_swift_scratch_ctx;
SwiftASTContextForExpressions *m_swift_ast_ctx;
SwiftASTContextForExpressionsSP m_swift_ast_ctx;
PersistentVariableDelegate m_persistent_variable_delegate;
std::unique_ptr<SwiftExpressionParser> m_parser;
std::optional<SwiftLanguageRuntime::GenericSignature> m_generic_signature;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static std::string TranslateObjCNameToSwiftName(std::string className,
sc = &swiftFrame->GetSymbolContext(eSymbolContextFunction);
if (!sc)
return "";
auto *ctx = ts->GetSwiftASTContext(*sc);
auto ctx = ts->GetSwiftASTContext(*sc);
if (!ctx)
return "";
swift::ClangImporter *imp = ctx->GetClangImporter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ StructuredData::ObjectSP InstrumentationRuntimeTSan::RetrieveReportData(

ValueObjectSP main_value;
ExecutionContext exe_ctx;
Status eval_error;
frame_sp->CalculateExecutionContext(exe_ctx);
ExpressionResults result = UserExpression::Evaluate(
exe_ctx, options, thread_sanitizer_retrieve_report_data_command, "",
main_value, eval_error);
main_value);
if (result != eExpressionCompleted) {
StreamString ss;
ss << "cannot evaluate ThreadSanitizer expression:\n";
ss << eval_error.AsCString();
if (main_value)
ss << main_value->GetError().AsCString();
Debugger::ReportWarning(ss.GetString().str(),
process_sp->GetTarget().GetDebugger().GetID());
return StructuredData::ObjectSP();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ StructuredData::ObjectSP InstrumentationRuntimeUBSan::RetrieveReportData(

ValueObjectSP main_value;
ExecutionContext exe_ctx;
Status eval_error;
frame_sp->CalculateExecutionContext(exe_ctx);
ExpressionResults result = UserExpression::Evaluate(
exe_ctx, options, ub_sanitizer_retrieve_report_data_command, "",
main_value, eval_error);
main_value);
if (result != eExpressionCompleted) {
StreamString ss;
ss << "cannot evaluate UndefinedBehaviorSanitizer expression:\n";
ss << eval_error.AsCString();
if (main_value)
ss << main_value->GetError().AsCString();
Debugger::ReportWarning(ss.GetString().str(),
process_sp->GetTarget().GetDebugger().GetID());
return StructuredData::ObjectSP();
Expand Down
Loading