Skip to content

Commit ba443ff

Browse files
committed
rework GetIndexOfChildMemberWithName validation
1 parent 0d5d9e2 commit ba443ff

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,24 +1541,6 @@ bool Equivalent(llvm::Optional<T> l, T r) {
15411541
return Equivalent(l, llvm::Optional<T>(r));
15421542
}
15431543

1544-
template <typename T>
1545-
bool Equivalent(const std::vector<T> &l, const std::vector<T> &r) {
1546-
if (std::equal(l.begin(), l.end(), r.begin(), r.end()))
1547-
return true;
1548-
1549-
auto join = [](const std::vector<T> &v) -> std::string {
1550-
if (v.empty())
1551-
return {};
1552-
std::ostringstream buf;
1553-
buf << v[0];
1554-
for (size_t i = 1; i < v.size(); ++i)
1555-
buf << ", " << v[i];
1556-
return buf.str();
1557-
};
1558-
llvm::dbgs() << join(l) << " != " << join(r) << "\n";
1559-
return false;
1560-
}
1561-
15621544
} // namespace
15631545
#endif
15641546

@@ -2399,17 +2381,43 @@ size_t TypeSystemSwiftTypeRef::GetIndexOfChildMemberWithName(
23992381
// checking the return value, plus the by-ref `child_indexes`.
24002382
if (!m_swift_ast_context)
24012383
return *index_size;
2402-
auto v_type = ReconstructType(type);
2403-
std::vector<uint32_t> v_child_indexes;
2404-
auto v_index_size = m_swift_ast_context->GetIndexOfChildMemberWithName(
2405-
v_type, name, exe_ctx, omit_empty_base_classes, v_child_indexes);
2406-
bool equivalent =
2407-
!v_type || (Equivalent(*index_size, v_index_size) &&
2408-
Equivalent(child_indexes, v_child_indexes));
2409-
if (!equivalent)
2410-
llvm::dbgs() << "failing type was " << (const char *)type << "\n";
2411-
assert(equivalent &&
2412-
"TypeSystemSwiftTypeRef diverges from SwiftASTContext");
2384+
auto ast_type = ReconstructType(type);
2385+
if (!ast_type)
2386+
return *index_size;
2387+
std::vector<uint32_t> ast_child_indexes;
2388+
auto ast_index_size = m_swift_ast_context->GetIndexOfChildMemberWithName(
2389+
ast_type, name, exe_ctx, omit_empty_base_classes,
2390+
ast_child_indexes);
2391+
// The runtime has more info than the AST. No useful validation can be
2392+
// done.
2393+
if (*index_size > ast_index_size)
2394+
return *index_size;
2395+
2396+
auto fail = [&]() {
2397+
auto join = [](const auto &v) {
2398+
std::ostringstream buf;
2399+
buf << "{";
2400+
for (const auto &item : v)
2401+
buf << item << ",";
2402+
buf.seekp(-1, std::ios_base::end);
2403+
buf << "}";
2404+
return buf.str();
2405+
};
2406+
llvm::dbgs() << join(child_indexes)
2407+
<< " != " << join(ast_child_indexes) << "\n";
2408+
llvm::dbgs() << "failing type was " << (const char *)type
2409+
<< ", member was " << name << "\n";
2410+
assert(false &&
2411+
"TypeSystemSwiftTypeRef diverges from SwiftASTContext");
2412+
};
2413+
if (*index_size != ast_index_size)
2414+
fail();
2415+
for (unsigned i = 0; i < *index_size; ++i)
2416+
if (child_indexes[i] < ast_child_indexes[i])
2417+
// When the runtime may know know about more children. When this
2418+
// happens, indexes will be larger. But if an index is smaller, that
2419+
// means the runtime has dropped info somehow.
2420+
fail();
24132421
#endif
24142422
return *index_size;
24152423
}

0 commit comments

Comments
 (0)