Skip to content

[lldb][swift] Query name of problematic variable when emitting diagnostics #10780

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

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ static bool CollectVariablesInScope(SymbolContext &sc,
}

/// Create a \c VariableInfo record for each visible variable.
static llvm::Error RegisterAllVariables(
static std::pair<llvm::Error, lldb::VariableSP> RegisterAllVariables(
SymbolContext &sc, lldb::StackFrameSP &stack_frame_sp,
SwiftASTContextForExpressions &ast_context,
llvm::SmallVectorImpl<SwiftASTManipulator::VariableInfo> &local_variables,
Expand All @@ -504,13 +504,14 @@ static llvm::Error RegisterAllVariables(
// Proceed from the innermost scope outwards, adding all variables
// not already shadowed by an inner declaration.
llvm::SmallDenseSet<const char *, 8> processed_names;
for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi)
if (auto error =
AddVariableInfo({variables.GetVariableAtIndex(vi)}, stack_frame_sp,
ast_context, language_runtime, processed_names,
local_variables, use_dynamic, bind_generic_types))
return error;
return llvm::Error::success();
for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
lldb::VariableSP var = variables.GetVariableAtIndex(vi);
if (auto error = AddVariableInfo(
{var}, stack_frame_sp, ast_context, language_runtime,
processed_names, local_variables, use_dynamic, bind_generic_types))
return {std::move(error), var};
}
return {llvm::Error::success(), nullptr};
}

/// Check if we can evaluate the expression as generic.
Expand Down Expand Up @@ -624,15 +625,17 @@ SwiftUserExpression::GetTextAndSetExpressionParser(

llvm::SmallVector<SwiftASTManipulator::VariableInfo> local_variables;

if (llvm::Error error = RegisterAllVariables(
sc, stack_frame, *m_swift_ast_ctx, local_variables,
m_options.GetUseDynamic(), m_options.GetBindGenericTypes())) {
auto [error, bad_var] = RegisterAllVariables(
sc, stack_frame, *m_swift_ast_ctx, local_variables,
m_options.GetUseDynamic(), m_options.GetBindGenericTypes());
if (error) {
diagnostic_manager.PutString(lldb::eSeverityInfo,
llvm::toString(std::move(error)));
diagnostic_manager.PutString(
lldb::eSeverityError,
"Couldn't realize Swift AST type of self. Hint: using `v` to "
"directly inspect variables and fields may still work.");
std::string diag = llvm::formatv(
"Couldn't realize Swift AST type of variable '{0}'. Hint: using `v` to "
"directly inspect variables and fields may still work.",
bad_var->GetName());
diagnostic_manager.PutString(lldb::eSeverityError, diag);
return ParseResult::retry_bind_generic_params;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def test_private_generic_type(self):
extra_images=['Public'])
# Make sure this fails without generic expression evaluation.
self.expect("expr --bind-generic-types true -- self",
substrs=["Couldn't realize Swift AST type of self."],
substrs=["note: type for self cannot be reconstructed",
"Couldn't realize Swift AST type of all variables."],
error=True)
# Test that not binding works.
self.expect("expr --bind-generic-types false -- self",
Expand All @@ -48,7 +49,8 @@ def test_private_generic_type(self):
'break here for class', lldb.SBFileSpec('Public.swift'), None)
lldbutil.continue_to_breakpoint(process, breakpoint)
self.expect("expr --bind-generic-types true -- self",
substrs=["Couldn't realize Swift AST type of self."],
substrs=["note: type for self cannot be reconstructed",
"Couldn't realize Swift AST type of all variables."],
error=True)
self.expect("expr --bind-generic-types false -- self",
substrs=["Public.ClassWrapper<Private.InvisibleStruct>",
Expand All @@ -67,7 +69,8 @@ def test_private_generic_type(self):
'break here for two generic parameters', lldb.SBFileSpec('Public.swift'), None)
lldbutil.continue_to_breakpoint(process, breakpoint)
self.expect("expr --bind-generic-types true -- self",
substrs=["Couldn't realize Swift AST type of self."],
substrs=["note: type for self cannot be reconstructed",
"Couldn't realize Swift AST type of all variables."],
error=True)
self.expect("expr --bind-generic-types false -- self",
substrs=["Public.TwoGenericParameters",
Expand Down Expand Up @@ -96,7 +99,8 @@ def test_private_generic_type(self):
'break here for three generic parameters', lldb.SBFileSpec('Public.swift'), None)
lldbutil.continue_to_breakpoint(process, breakpoint)
self.expect("expr --bind-generic-types true -- self",
substrs=["Couldn't realize Swift AST type of self."],
substrs=["note: type for self cannot be reconstructed",
"Couldn't realize Swift AST type of all variables."],
error=True)
self.expect("expr --bind-generic-types false -- self",
substrs=["Public.ThreeGenericParameters",
Expand Down Expand Up @@ -124,7 +128,8 @@ def test_private_generic_type(self):
'break here for four generic parameters', lldb.SBFileSpec('Public.swift'), None)
lldbutil.continue_to_breakpoint(process, breakpoint)
self.expect("expr --bind-generic-types true -- self",
substrs=["Couldn't realize Swift AST type of self."],
substrs=["note: type for self cannot be reconstructed",
"Couldn't realize Swift AST type of all variables."],
error=True)
self.expect("expr --bind-generic-types false -- self",
substrs=["Public.FourGenericParameters",
Expand Down Expand Up @@ -168,5 +173,6 @@ def test_private_generic_type(self):
# Check that if both binding and not binding the generic type parameters fail, we report
# the "bind generic params" error message, as that's the default case that runs first.
self.expect("expr --bind-generic-types auto -- self",
substrs=["Couldn't realize Swift AST type of self."],
substrs=["note: type for self cannot be reconstructed",
"Couldn't realize Swift AST type of all variables."],
error=True)