Skip to content

Commit cac1776

Browse files
Merge pull request #2417 from adrian-prantl/main-cherry-picks
Cherry-pick commits lost in rebranch
2 parents 8443fe8 + b445cd3 commit cac1776

File tree

10 files changed

+116
-36
lines changed

10 files changed

+116
-36
lines changed

lldb/scripts/check-ast-context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ def look_for_METHOD(c):
421421
'GetFatalErrors',
422422
'PrintDiagnostics',
423423
'GetASTContext',
424-
'SetTriple'
424+
'SetTriple',
425+
'LogConfiguration'
425426
]
426427

427428
for method in methods:

lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,15 @@ std::vector<ConstString> SwiftLanguage::GetPossibleFormattersMatches(
889889

890890
const bool check_cpp = false;
891891
const bool check_objc = false;
892-
bool canBeSwiftDynamic = compiler_type.IsPossibleDynamicType(
893-
nullptr, check_cpp, check_objc);
892+
bool canBeSwiftDynamic =
893+
compiler_type.IsPossibleDynamicType(nullptr, check_cpp, check_objc) ||
894+
// Foundation (but really any library) may create new
895+
// Objective-C classes at runtime and LLDB's ObjC runtime
896+
// implementation doesn't yet get notified about this. As a
897+
// workaround we try to interpret ObjC id pointers as Swift
898+
// classes to make them available.
899+
(compiler_type.GetCanonicalType().GetTypeClass() ==
900+
eTypeClassObjCObjectPointer);
894901

895902
if (canBeSwiftDynamic) {
896903
do {

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,14 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
16841684
m_description,
16851685
target ? target->GetArchitecture().GetTriple() : triple,
16861686
target)));
1687+
bool suppress_config_log = false;
1688+
auto defer_log = llvm::make_scope_exit([swift_ast_sp, &suppress_config_log] {
1689+
// To avoid spamming the log with useless info, we don't log the
1690+
// configuration if everything went fine and the current module
1691+
// doesn't have any Swift contents (i.e., the shared cache dylibs).
1692+
if (!suppress_config_log)
1693+
swift_ast_sp->LogConfiguration();
1694+
});
16871695

16881696
// This is a module AST context, mark it as such.
16891697
swift_ast_sp->m_is_scratch_context = false;
@@ -1696,9 +1704,7 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
16961704

16971705
bool set_triple = false;
16981706
bool found_swift_modules = false;
1699-
17001707
SymbolFile *sym_file = module.GetSymbolFile();
1701-
17021708
std::string target_triple;
17031709

17041710
if (sym_file) {
@@ -1821,15 +1827,17 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
18211827

18221828
std::vector<std::string> module_names;
18231829
swift_ast_sp->RegisterSectionModules(module, module_names);
1824-
if (module_names.size()) {
1830+
if (!module_names.size()) {
1831+
// This dylib has no Swift contents; logging the configuration is pointless.
1832+
suppress_config_log = true;
1833+
} else {
18251834
swift_ast_sp->ValidateSectionModules(module, module_names);
18261835
if (lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES)) {
18271836
std::lock_guard<std::recursive_mutex> locker(g_log_mutex);
18281837
LOG_PRINTF(LIBLLDB_LOG_TYPES, "((Module*)%p, \"%s\") = %p",
18291838
static_cast<void *>(&module),
18301839
module.GetFileSpec().GetFilename().AsCString("<anonymous>"),
18311840
static_cast<void *>(swift_ast_sp.get()));
1832-
swift_ast_sp->LogConfiguration();
18331841
}
18341842
}
18351843

@@ -1916,6 +1924,8 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
19161924
// detect if we have a iOS simulator.
19171925
std::shared_ptr<SwiftASTContextForExpressions> swift_ast_sp(
19181926
new SwiftASTContextForExpressions(m_description, target));
1927+
auto defer_log = llvm::make_scope_exit(
1928+
[swift_ast_sp] { swift_ast_sp->LogConfiguration(); });
19191929

19201930
LOG_PRINTF(LIBLLDB_LOG_TYPES, "(Target)");
19211931

@@ -2270,7 +2280,6 @@ lldb::TypeSystemSP SwiftASTContext::CreateInstance(lldb::LanguageType language,
22702280
LOG_PRINTF(LIBLLDB_LOG_TYPES, "((Target*)%p) = %p",
22712281
static_cast<void *>(&target),
22722282
static_cast<void *>(swift_ast_sp.get()));
2273-
swift_ast_sp->LogConfiguration();
22742283

22752284
if (swift_ast_sp->HasFatalErrors()) {
22762285
logError(swift_ast_sp->GetFatalErrors().AsCString());
@@ -4795,6 +4804,12 @@ CompilerType SwiftASTContext::GetErrorType() {
47954804
return {};
47964805
}
47974806

4807+
CompilerType SwiftASTContext::GetObjCObjectType() {
4808+
// FIXME: ClangImporter::Implementation stores this type, but it's not
4809+
// exposed.
4810+
return GetCompilerType(ConstString("$sSo8NSObjectCD"));
4811+
}
4812+
47984813
SwiftASTContext *SwiftASTContext::GetSwiftASTContext(swift::ASTContext *ast) {
47994814
SwiftASTContext *swift_ast = GetASTMap().Lookup(ast);
48004815
return swift_ast;
@@ -4913,8 +4928,8 @@ void SwiftASTContext::ClearModuleDependentCaches() {
49134928
}
49144929

49154930
void SwiftASTContext::LogConfiguration() {
4916-
VALID_OR_RETURN_VOID();
4917-
4931+
// It makes no sense to call VALID_OR_RETURN here. We specifically
4932+
// want the logs in the error case!
49184933
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
49194934
if (!log)
49204935
return;

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ class SwiftASTContext : public TypeSystemSwift {
356356
CreateTupleType(const std::vector<TupleElement> &elements) override;
357357

358358
CompilerType GetErrorType() override;
359+
CompilerType GetObjCObjectType() override;
359360

360361
bool HasErrors();
361362

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class TypeSystemSwift : public TypeSystem {
116116
virtual bool IsImportedType(lldb::opaque_compiler_type_t type,
117117
CompilerType *original_type) = 0;
118118
virtual CompilerType GetErrorType() = 0;
119+
virtual CompilerType GetObjCObjectType() = 0;
119120
virtual CompilerType GetReferentType(lldb::opaque_compiler_type_t type) = 0;
120121
static CompilerType GetInstanceType(CompilerType ct);
121122
virtual CompilerType GetInstanceType(lldb::opaque_compiler_type_t type) = 0;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2752,6 +2752,23 @@ CompilerType TypeSystemSwiftTypeRef::GetErrorType() {
27522752
VALIDATE_AND_RETURN_STATIC(impl, GetErrorType);
27532753
}
27542754

2755+
CompilerType TypeSystemSwiftTypeRef::GetObjCObjectType() {
2756+
auto impl = [&]() {
2757+
using namespace swift::Demangle;
2758+
Demangler dem;
2759+
auto *obj_type = dem.createNode(Node::Kind::Type);
2760+
NodePointer s = dem.createNode(Node::Kind::Structure);
2761+
NodePointer m =
2762+
dem.createNode(Node::Kind::Module, swift::MANGLING_MODULE_OBJC);
2763+
NodePointer ident = dem.createNode(Node::Kind::Identifier, "NSObject");
2764+
s->addChild(m, dem);
2765+
s->addChild(ident, dem);
2766+
obj_type->addChild(s, dem);
2767+
return RemangleAsType(dem, obj_type);
2768+
};
2769+
VALIDATE_AND_RETURN_STATIC(impl, GetObjCObjectType);
2770+
}
2771+
27552772
CompilerType
27562773
TypeSystemSwiftTypeRef::GetReferentType(opaque_compiler_type_t type) {
27572774
auto impl = [&]() -> CompilerType {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
233233
/// builtins (int <-> Swift.Int) as Clang types.
234234
CompilerType GetAsClangTypeOrNull(lldb::opaque_compiler_type_t type);
235235
CompilerType GetErrorType() override;
236+
CompilerType GetObjCObjectType() override;
236237
CompilerType GetReferentType(lldb::opaque_compiler_type_t type) override;
237238
CompilerType GetInstanceType(lldb::opaque_compiler_type_t type) override;
238239
TypeAllocationStrategy

lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ llvm::Optional<uint64_t> SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemote
886886
auto frame = instance ? instance->GetExecutionContextRef().GetFrameSP().get()
887887
: nullptr;
888888
if (auto *ti = llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(
889-
GetTypeInfo(instance_type, frame))) {
889+
GetSwiftRuntimeTypeInfo(instance_type, frame))) {
890890
auto fields = ti->getFields();
891891
LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
892892
"using record type info");
@@ -1040,10 +1040,11 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
10401040
auto frame =
10411041
valobj ? valobj->GetExecutionContextRef().GetFrameSP().get() : nullptr;
10421042
const swift::reflection::TypeRef *tr = nullptr;
1043-
auto *ti = GetTypeInfo(type, frame, &tr);
1043+
auto *ti = GetSwiftRuntimeTypeInfo(type, frame, &tr);
1044+
if (!ti)
1045+
return {};
10441046
// Structs and Tuples.
1045-
if (auto *rti =
1046-
llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(ti)) {
1047+
if (auto *rti = llvm::dyn_cast<swift::reflection::RecordTypeInfo>(ti)) {
10471048
switch (rti->getRecordKind()) {
10481049
case swift::reflection::RecordKind::ExistentialMetatype:
10491050
case swift::reflection::RecordKind::ThickFunction:
@@ -1060,13 +1061,11 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
10601061
return rti->getNumFields();
10611062
}
10621063
}
1063-
if (auto *eti = llvm::dyn_cast_or_null<swift::reflection::EnumTypeInfo>(ti)) {
1064+
if (auto *eti = llvm::dyn_cast<swift::reflection::EnumTypeInfo>(ti)) {
10641065
return eti->getNumPayloadCases();
10651066
}
10661067
// Objects.
1067-
if (auto *rti =
1068-
llvm::dyn_cast_or_null<swift::reflection::ReferenceTypeInfo>(ti)) {
1069-
1068+
if (auto *rti = llvm::dyn_cast<swift::reflection::ReferenceTypeInfo>(ti)) {
10701069
switch (rti->getReferenceKind()) {
10711070
case swift::reflection::ReferenceKind::Weak:
10721071
case swift::reflection::ReferenceKind::Unowned:
@@ -1113,7 +1112,9 @@ SwiftLanguageRuntimeImpl::GetNumFields(CompilerType type,
11131112
using namespace swift::reflection;
11141113
// Try the static type metadata.
11151114
const TypeRef *tr = nullptr;
1116-
auto *ti = GetTypeInfo(type, exe_ctx->GetFramePtr(), &tr);
1115+
auto *ti = GetSwiftRuntimeTypeInfo(type, exe_ctx->GetFramePtr(), &tr);
1116+
if (!ti)
1117+
return {};
11171118
// Structs and Tuples.
11181119
switch (ti->getKind()) {
11191120
case TypeInfoKind::Record: {
@@ -1246,7 +1247,9 @@ llvm::Optional<std::string> SwiftLanguageRuntimeImpl::GetEnumCaseName(
12461247
CompilerType type, const DataExtractor &data, ExecutionContext *exe_ctx) {
12471248
using namespace swift::reflection;
12481249
using namespace swift::remote;
1249-
auto *ti = GetTypeInfo(type, exe_ctx->GetFramePtr());
1250+
auto *ti = GetSwiftRuntimeTypeInfo(type, exe_ctx->GetFramePtr());
1251+
if (!ti)
1252+
return {};
12501253
if (ti->getKind() != TypeInfoKind::Enum)
12511254
return {};
12521255

@@ -1278,7 +1281,9 @@ llvm::Optional<size_t> SwiftLanguageRuntimeImpl::GetIndexOfChildMemberWithName(
12781281
using namespace swift::reflection;
12791282
// Try the static type metadata.
12801283
const TypeRef *tr = nullptr;
1281-
auto *ti = GetTypeInfo(type, exe_ctx->GetFramePtr(), &tr);
1284+
auto *ti = GetSwiftRuntimeTypeInfo(type, exe_ctx->GetFramePtr(), &tr);
1285+
if (!ti)
1286+
return {};
12821287
switch (ti->getKind()) {
12831288
case TypeInfoKind::Record: {
12841289
// Structs and Tuples.
@@ -1391,7 +1396,9 @@ CompilerType SwiftLanguageRuntimeImpl::GetChildCompilerTypeAtIndex(
13911396
// Try the static type metadata.
13921397
auto frame =
13931398
valobj ? valobj->GetExecutionContextRef().GetFrameSP().get() : nullptr;
1394-
auto *ti = GetTypeInfo(type, frame);
1399+
auto *ti = GetSwiftRuntimeTypeInfo(type, frame);
1400+
if (!ti)
1401+
return {};
13951402
// Structs and Tuples.
13961403
if (auto *rti =
13971404
llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(ti)) {
@@ -2486,7 +2493,8 @@ static bool CouldHaveDynamicValue(ValueObject &in_value) {
24862493
// disable it.
24872494
return !in_value.IsBaseClass();
24882495
}
2489-
return var_type.IsPossibleDynamicType(nullptr, false, false);
2496+
bool check_objc = true;
2497+
return var_type.IsPossibleDynamicType(nullptr, false, check_objc);
24902498
}
24912499

24922500
bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
@@ -2497,10 +2505,6 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
24972505
if (use_dynamic == lldb::eNoDynamicValues)
24982506
return false;
24992507

2500-
// Try to import a Clang type into Swift.
2501-
if (in_value.GetObjectRuntimeLanguage() == eLanguageTypeObjC)
2502-
return GetDynamicTypeAndAddress_ClangType(
2503-
in_value, use_dynamic, class_type_or_name, address, value_type);
25042508

25052509
if (!CouldHaveDynamicValue(in_value))
25062510
return false;
@@ -2509,11 +2513,33 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
25092513
// use the scratch context where such operations are legal and safe.
25102514
assert(IsScratchContextLocked(in_value.GetTargetSP()) &&
25112515
"Swift scratch context not locked ahead of dynamic type resolution");
2516+
CompilerType val_type(in_value.GetCompilerType());
2517+
25122518
llvm::Optional<SwiftASTContextReader> maybe_scratch_ctx =
25132519
in_value.GetScratchSwiftASTContext();
2520+
2521+
// Try to import a Clang type into Swift.
2522+
if (in_value.GetObjectRuntimeLanguage() == eLanguageTypeObjC) {
2523+
if (GetDynamicTypeAndAddress_ClangType(
2524+
in_value, use_dynamic, class_type_or_name, address, value_type))
2525+
return true;
2526+
// If the type couldn't be resolved by the Clang runtime:
2527+
// Foundation, for example, generates new Objective-C classes on
2528+
// the fly (such as instances of _DictionaryStorage<T1, T2>) and
2529+
// LLDB's ObjC runtime implementation isn't set up to recognize
2530+
// these. As a workaround, try to resolve them as Swift types.
2531+
if (val_type.GetCanonicalType().GetTypeClass() ==
2532+
eTypeClassObjCObjectPointer)
2533+
if (maybe_scratch_ctx)
2534+
if (auto *scratch_ctx = maybe_scratch_ctx->get())
2535+
val_type = scratch_ctx->GetObjCObjectType();
2536+
}
2537+
25142538
if (!maybe_scratch_ctx)
25152539
return false;
25162540
SwiftASTContextForExpressions *scratch_ctx = maybe_scratch_ctx->get();
2541+
if (!scratch_ctx)
2542+
return false;
25172543

25182544
auto retry_once = [&]() {
25192545
// Retry exactly once using the per-module fallback scratch context.
@@ -2536,7 +2562,6 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress(
25362562

25372563
// Import the type into the scratch context. Any form of dynamic
25382564
// type resolution may trigger a cross-module import.
2539-
CompilerType val_type(in_value.GetCompilerType());
25402565
Flags type_info(val_type.GetTypeInfo());
25412566
if (!type_info.AnySet(eTypeIsSwift))
25422567
return false;
@@ -2754,7 +2779,8 @@ SwiftLanguageRuntimeImpl::GetTypeRef(CompilerType type,
27542779
return type_ref;
27552780
}
27562781

2757-
const swift::reflection::TypeInfo *SwiftLanguageRuntimeImpl::GetTypeInfo(
2782+
const swift::reflection::TypeInfo *
2783+
SwiftLanguageRuntimeImpl::GetSwiftRuntimeTypeInfo(
27582784
CompilerType type, ExecutionContextScope *exe_scope,
27592785
swift::reflection::TypeRef const **out_tr) {
27602786
auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(type.GetTypeSystem());
@@ -2796,30 +2822,30 @@ const swift::reflection::TypeInfo *SwiftLanguageRuntimeImpl::GetTypeInfo(
27962822
}
27972823

27982824
bool SwiftLanguageRuntimeImpl::IsStoredInlineInBuffer(CompilerType type) {
2799-
if (auto *type_info = GetTypeInfo(type, nullptr))
2825+
if (auto *type_info = GetSwiftRuntimeTypeInfo(type, nullptr))
28002826
return type_info->isBitwiseTakable() && type_info->getSize() <= 24;
28012827
return true;
28022828
}
28032829

28042830
llvm::Optional<uint64_t>
28052831
SwiftLanguageRuntimeImpl::GetBitSize(CompilerType type,
28062832
ExecutionContextScope *exe_scope) {
2807-
if (auto *type_info = GetTypeInfo(type, exe_scope))
2833+
if (auto *type_info = GetSwiftRuntimeTypeInfo(type, exe_scope))
28082834
return type_info->getSize() * 8;
28092835
return {};
28102836
}
28112837

28122838
llvm::Optional<uint64_t>
28132839
SwiftLanguageRuntimeImpl::GetByteStride(CompilerType type) {
2814-
if (auto *type_info = GetTypeInfo(type, nullptr))
2840+
if (auto *type_info = GetSwiftRuntimeTypeInfo(type, nullptr))
28152841
return type_info->getStride();
28162842
return {};
28172843
}
28182844

28192845
llvm::Optional<size_t>
28202846
SwiftLanguageRuntimeImpl::GetBitAlignment(CompilerType type,
28212847
ExecutionContextScope *exe_scope) {
2822-
if (auto *type_info = GetTypeInfo(type, exe_scope))
2848+
if (auto *type_info = GetSwiftRuntimeTypeInfo(type, exe_scope))
28232849
return type_info->getAlignment() * 8;
28242850
return {};
28252851
}

lldb/source/Target/SwiftLanguageRuntimeImpl.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ class SwiftLanguageRuntimeImpl {
7777
Status &error);
7878

7979
/// Ask Remote Mirrors for the type info about a Swift type.
80+
/// This will return a nullptr if the lookup fails.
8081
const swift::reflection::TypeInfo *
81-
GetTypeInfo(CompilerType type, ExecutionContextScope *exe_scope,
82-
swift::reflection::TypeRef const **out_tr = nullptr);
82+
GetSwiftRuntimeTypeInfo(CompilerType type, ExecutionContextScope *exe_scope,
83+
swift::reflection::TypeRef const **out_tr = nullptr);
8384

8485
llvm::Optional<const swift::reflection::TypeInfo *>
8586
lookupClangTypeInfo(CompilerType clang_type);

lldb/test/Shell/SwiftREPL/DictBridging.test

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,17 @@ let d_objc2 = NSArray(object: [1: 2] as [NSNumber: NSNumber] as NSDictionary)
6868
let d_objc3 = NSArray(object: [1: 2] as [Int: Int] as NSDictionary)
6969
// DICT-LABEL: d_objc3: NSArray = 1 element {
7070
// DICT-NEXT: [0] = 1 key/value pair {
71-
// DICT-NEXT: [0] = (key = 1, value = 2)
71+
//
72+
// Allowing for both multi-line and single-line
73+
// formatting, depending on CoreFoundation
74+
// implementation:
75+
//
76+
// DICT-NOT: }
77+
// DICT: [0] =
78+
// DICT-NOT: }
79+
// DICT: key = 1
80+
// DICT-NOT: }
81+
// DICT: value = 2
7282
// DICT-NEXT: }
7383
// DICT-NEXT: }
7484

0 commit comments

Comments
 (0)