Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
99 changes: 72 additions & 27 deletions lldb/source/ValueObject/DILEval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,40 +330,85 @@ Interpreter::Visit(const ArraySubscriptNode *node) {
return lhs_or_err;
lldb::ValueObjectSP base = *lhs_or_err;

// Check to see if 'base' has a synthetic value; if so, try using that.
StreamString var_expr_path_strm;
uint64_t child_idx = node->GetIndex();
if (lldb::ValueObjectSP synthetic = base->GetSyntheticValue()) {
llvm::Expected<uint32_t> num_children =
synthetic->GetNumChildren(child_idx + 1);
if (!num_children)
return llvm::make_error<DILDiagnosticError>(
m_expr, toString(num_children.takeError()), node->GetLocation());
if (child_idx >= *num_children) {
std::string message = llvm::formatv(
lldb::ValueObjectSP child_valobj_sp;
bool is_incomplete_array = false;
CompilerType base_type = base->GetCompilerType().GetNonReferenceType();
base->GetExpressionPath(var_expr_path_strm);
if (base_type.IsPointerType()) {
child_valobj_sp = base->GetSyntheticArrayMember(child_idx, true);
if (!child_valobj_sp) {
std::string err_msg = llvm::formatv(
"failed to use pointer as array for index {0} for "
"\"({1}) {2}\"",
child_idx, base->GetTypeName().AsCString("<invalid type>"),
var_expr_path_strm.GetData());
if (base_type.IsPointerToVoid())
err_msg = "subscript of pointer to incomplete type 'void'";
return llvm::make_error<DILDiagnosticError>(m_expr, err_msg,
node->GetLocation());
}
} else if (base_type.IsArrayType(nullptr, nullptr, &is_incomplete_array)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe too fancy, but reduces the scope of the variable.

Suggested change
} else if (base_type.IsArrayType(nullptr, nullptr, &is_incomplete_array)) {
} else if (bool is_incomplete_array; base_type.IsArrayType(nullptr, nullptr, &is_incomplete_array)) {

child_valobj_sp = base->GetChildAtIndex(child_idx);
if (!child_valobj_sp && (is_incomplete_array || m_use_synthetic))
child_valobj_sp = base->GetSyntheticArrayMember(child_idx, true);
if (!child_valobj_sp) {
std::string err_msg = llvm::formatv(
"array index {0} is not valid for \"({1}) {2}\"", child_idx,
base->GetTypeName().AsCString("<invalid type>"),
base->GetName().AsCString());
return llvm::make_error<DILDiagnosticError>(m_expr, message,
var_expr_path_strm.GetData());
return llvm::make_error<DILDiagnosticError>(m_expr, err_msg,
node->GetLocation());
}
if (lldb::ValueObjectSP child_valobj_sp =
synthetic->GetChildAtIndex(child_idx))
return child_valobj_sp;
} else if (base_type.IsScalarType()) {
child_valobj_sp =
base->GetSyntheticBitFieldChild(child_idx, child_idx, true);
if (!child_valobj_sp) {
std::string err_msg = llvm::formatv(
"bitfield range {0}-{1} is not valid for \"({2}) {3}\"", child_idx,
child_idx, base->GetTypeName().AsCString("<invalid type>"),
var_expr_path_strm.GetData());
return llvm::make_error<DILDiagnosticError>(m_expr, err_msg,
node->GetLocation(), 1);
}
} else {
lldb::ValueObjectSP synthetic = base->GetSyntheticValue();
if (!m_use_synthetic || !synthetic || synthetic == base) {
std::string err_msg =
llvm::formatv("\"{0}\" is not an array type",
base->GetTypeName().AsCString("<invalid type>"));
return llvm::make_error<DILDiagnosticError>(m_expr, err_msg,
node->GetLocation(), 1);
}
if (static_cast<uint32_t>(child_idx) >=
synthetic->GetNumChildrenIgnoringErrors(child_idx + 1)) {
std::string err_msg = llvm::formatv(
"array index {0} is not valid for \"({1}) {2}\"", child_idx,
base->GetTypeName().AsCString("<invalid type>"),
var_expr_path_strm.GetData());
return llvm::make_error<DILDiagnosticError>(m_expr, err_msg,
node->GetLocation(), 1);
}
child_valobj_sp = synthetic->GetChildAtIndex(child_idx);
if (!child_valobj_sp) {
std::string err_msg = llvm::formatv(
"array index {0} is not valid for \"({1}) {2}\"", child_idx,
base->GetTypeName().AsCString("<invalid type>"),
var_expr_path_strm.GetData());
return llvm::make_error<DILDiagnosticError>(m_expr, err_msg,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can std::move these err_msgs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

node->GetLocation(), 1);
}
}

auto base_type = base->GetCompilerType().GetNonReferenceType();
if (!base_type.IsPointerType() && !base_type.IsArrayType())
return llvm::make_error<DILDiagnosticError>(
m_expr, "subscripted value is not an array or pointer",
node->GetLocation());
if (base_type.IsPointerToVoid())
return llvm::make_error<DILDiagnosticError>(
m_expr, "subscript of pointer to incomplete type 'void'",
node->GetLocation());

if (base_type.IsArrayType()) {
if (lldb::ValueObjectSP child_valobj_sp = base->GetChildAtIndex(child_idx))
return child_valobj_sp;
if (child_valobj_sp) {
if (m_use_dynamic != lldb::eNoDynamicValues) {
lldb::ValueObjectSP dynamic_value_sp(
child_valobj_sp->GetDynamicValue(m_use_dynamic));
if (dynamic_value_sp)
child_valobj_sp = dynamic_value_sp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
lldb::ValueObjectSP dynamic_value_sp(
child_valobj_sp->GetDynamicValue(m_use_dynamic));
if (dynamic_value_sp)
child_valobj_sp = dynamic_value_sp;
if (auto dynamic_sp = child_valobj_sp->GetDynamicValue(m_use_dynamic))
child_valobj_sp = std::move(dynamic_sp);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
return child_valobj_sp;
}

int64_t signed_child_idx = node->GetIndex();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@ def test_subscript(self):
substrs=["expected 'r_square', got: <'.'"],
)

# Base should be a "pointer to T" and index should be of an integral type.
self.expect(
"frame var 'idx_1[0]'",
error=True,
substrs=["subscripted value is not an array or pointer"],
)
# Test accessing bits in scalar types.
self.expect_var_path("idx_1[0]", value="1")
self.expect_var_path("idx_1[1]", value="0")

# Bit adcess not valid for a reference.
self.expect(
"frame var 'idx_1_ref[0]'",
error=True,
substrs=["subscripted value is not an array or pointer"],
substrs=["bitfield range 0-0 is not valid"],
)

# Base should be a "pointer to T" and index should be of an integral type.
self.expect(
"frame var 'int_arr[int_ptr]'",
error=True,
Expand Down
Loading