Skip to content

Commit 9dddb46

Browse files
Merge pull request #3435 from adrian-prantl/dwarfastparserswift
Move the ownership of DWARFASTParserSwift from SwiftASTContext to Typ…
2 parents f2b003f + be90eef commit 9dddb46

File tree

9 files changed

+73
-75
lines changed

9 files changed

+73
-75
lines changed

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserSwift.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
2929
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
30-
#include "Plugins/TypeSystem/Swift/SwiftASTContext.h"
30+
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
3131
#include "lldb/Core/Module.h"
3232
#include "lldb/Symbol/CompileUnit.h"
3333
#include "lldb/Symbol/Function.h"
@@ -41,7 +41,9 @@
4141
using namespace lldb;
4242
using namespace lldb_private;
4343

44-
DWARFASTParserSwift::DWARFASTParserSwift(SwiftASTContext &ast) : m_ast(ast) {}
44+
DWARFASTParserSwift::DWARFASTParserSwift(
45+
TypeSystemSwiftTypeRef &swift_typesystem)
46+
: m_swift_typesystem(swift_typesystem) {}
4547

4648
DWARFASTParserSwift::~DWARFASTParserSwift() {}
4749

@@ -151,7 +153,7 @@ lldb::TypeSP DWARFASTParserSwift::ParseTypeFromDWARF(const SymbolContext &sc,
151153
}
152154

153155
if (mangled_name) {
154-
type_sp = m_ast.GetCachedType(mangled_name);
156+
type_sp = m_swift_typesystem.GetCachedType(mangled_name);
155157
if (type_sp)
156158
return type_sp;
157159

@@ -161,16 +163,17 @@ lldb::TypeSP DWARFASTParserSwift::ParseTypeFromDWARF(const SymbolContext &sc,
161163

162164
// Try to import the type from one of the loaded Swift modules.
163165
if (SwiftLanguageRuntime::IsSwiftMangledName(mangled_name.GetCString()))
164-
compiler_type = m_ast.GetTypeFromMangledTypename(mangled_name);
166+
compiler_type =
167+
m_swift_typesystem.GetTypeFromMangledTypename(mangled_name);
165168
}
166169

167170
if (!compiler_type && name) {
168171
// Handle Archetypes, which are typedefs to RawPointerType.
169172
llvm::StringRef typedef_name = GetTypedefName(die);
170173
if (typedef_name.startswith("$sBp")) {
171174
preferred_name = name;
172-
compiler_type =
173-
m_ast.GetTypeFromMangledTypename(ConstString(typedef_name));
175+
compiler_type = m_swift_typesystem.GetTypeFromMangledTypename(
176+
ConstString(typedef_name));
174177
}
175178
}
176179

@@ -182,7 +185,7 @@ lldb::TypeSP DWARFASTParserSwift::ParseTypeFromDWARF(const SymbolContext &sc,
182185
// Make sure we at least have some function type. The mangling for
183186
// the "top_level_code" is returning the empty tuple type "()",
184187
// which is not a function type.
185-
compiler_type = m_ast.GetVoidFunctionType();
188+
compiler_type = m_swift_typesystem.GetVoidFunctionType();
186189
}
187190
break;
188191
default:
@@ -202,7 +205,7 @@ lldb::TypeSP DWARFASTParserSwift::ParseTypeFromDWARF(const SymbolContext &sc,
202205
// Cache this type.
203206
if (type_sp && mangled_name &&
204207
SwiftLanguageRuntime::IsSwiftMangledName(mangled_name.GetStringRef()))
205-
m_ast.SetCachedType(mangled_name, type_sp);
208+
m_swift_typesystem.SetCachedType(mangled_name, type_sp);
206209
die.GetDWARF()->GetDIEToType()[die.GetDIE()] = type_sp.get();
207210

208211
return type_sp;

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserSwift.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
class DWARFDebugInfoEntry;
2020
class DWARFDIECollection;
2121

22-
namespace lldb_private { class SwiftASTContext; }
22+
namespace lldb_private { class TypeSystemSwiftTypeRef; }
2323

2424
class DWARFASTParserSwift : public DWARFASTParser {
2525
public:
26-
DWARFASTParserSwift(lldb_private::SwiftASTContext &ast);
26+
DWARFASTParserSwift(lldb_private::TypeSystemSwiftTypeRef &swift_typesystem);
2727

2828
virtual ~DWARFASTParserSwift();
2929

@@ -56,7 +56,7 @@ class DWARFASTParserSwift : public DWARFASTParser {
5656
lldb_private::CompilerDeclContext decl_context) override {}
5757

5858
protected:
59-
lldb_private::SwiftASTContext &m_ast;
59+
lldb_private::TypeSystemSwiftTypeRef &m_swift_typesystem;
6060
};
6161

6262
#endif // SymbolFileDWARF_DWARFASTParserSwift_h_

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

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@
130130
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
131131
#include "Plugins/Platform/MacOSX/PlatformDarwin.h"
132132
#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
133-
#include "Plugins/SymbolFile/DWARF/DWARFASTParserSwift.h"
134133
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
135134

136135
#define VALID_OR_RETURN(value) \
@@ -4513,20 +4512,6 @@ CompilerType SwiftASTContext::GetAnyObjectType() {
45134512
return ToCompilerType({ast->getAnyObjectType()});
45144513
}
45154514

4516-
CompilerType SwiftASTContext::GetVoidFunctionType() {
4517-
VALID_OR_RETURN(CompilerType());
4518-
4519-
if (!m_void_function_type) {
4520-
swift::ASTContext *ast = GetASTContext();
4521-
swift::Type empty_tuple_type(swift::TupleType::getEmpty(*ast));
4522-
// FIXME: Verify ExtInfo state is correct, not working by accident.
4523-
swift::FunctionType::ExtInfo info;
4524-
m_void_function_type =
4525-
ToCompilerType({swift::FunctionType::get({}, empty_tuple_type, info)});
4526-
}
4527-
return m_void_function_type;
4528-
}
4529-
45304515
static CompilerType ValueDeclToType(swift::ValueDecl *decl,
45314516
swift::ASTContext *ast) {
45324517
if (decl) {
@@ -8331,23 +8316,8 @@ void SwiftASTContext::DumpTypeDescription(opaque_compiler_type_t type,
83318316
s->Printf("<could not resolve type>");
83328317
}
83338318

8334-
TypeSP SwiftASTContext::GetCachedType(ConstString mangled) {
8335-
TypeSP type_sp;
8336-
if (m_swift_type_map.Lookup(mangled.GetCString(), type_sp))
8337-
return type_sp;
8338-
else
8339-
return TypeSP();
8340-
}
8341-
8342-
void SwiftASTContext::SetCachedType(ConstString mangled,
8343-
const TypeSP &type_sp) {
8344-
m_swift_type_map.Insert(mangled.GetCString(), type_sp);
8345-
}
8346-
83478319
DWARFASTParser *SwiftASTContext::GetDWARFParser() {
8348-
if (!m_dwarf_ast_parser_ap)
8349-
m_dwarf_ast_parser_ap.reset(new DWARFASTParserSwift(*this));
8350-
return m_dwarf_ast_parser_ap.get();
8320+
return GetTypeSystemSwiftTypeRef().GetDWARFParser();
83518321
}
83528322

83538323
std::vector<lldb::DataBufferSP> &

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
1919
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
2020
#include "lldb/Core/SwiftForward.h"
21-
#include "lldb/Core/ThreadSafeDenseMap.h"
2221
#include "lldb/Core/ThreadSafeDenseSet.h"
2322
#include "lldb/Utility/Either.h"
2423
#include "llvm/ADT/SmallVector.h"
@@ -341,9 +340,6 @@ class SwiftASTContext : public TypeSystemSwift {
341340
// Retrieve the Swift.AnyObject type.
342341
CompilerType GetAnyObjectType();
343342

344-
// Get a function type that returns nothing and take no parameters
345-
CompilerType GetVoidFunctionType();
346-
347343
/// Import and Swiftify a Clang type.
348344
/// \return Returns an invalid type if unsuccessful.
349345
CompilerType ImportClangType(CompilerType clang_type);
@@ -761,10 +757,6 @@ class SwiftASTContext : public TypeSystemSwift {
761757

762758
CompilerType GetReferentType(lldb::opaque_compiler_type_t type) override;
763759

764-
lldb::TypeSP GetCachedType(ConstString mangled) override;
765-
766-
void SetCachedType(ConstString mangled, const lldb::TypeSP &type_sp) override;
767-
768760
/// Retrieves the modules that need to be implicitly imported in a given
769761
/// execution scope. This includes the modules imported by both the compile
770762
/// unit as well as any imports from previous expression evaluations.
@@ -858,7 +850,6 @@ class SwiftASTContext : public TypeSystemSwift {
858850
llvm::once_flag m_ir_gen_module_once;
859851
std::unique_ptr<swift::DiagnosticConsumer> m_diagnostic_consumer_ap;
860852
std::unique_ptr<swift::DependencyTracker> m_dependency_tracker;
861-
std::unique_ptr<DWARFASTParser> m_dwarf_ast_parser_ap;
862853
/// A collection of (not necessarily fatal) error messages that
863854
/// should be printed by Process::PrintWarningCantLoadSwift().
864855
std::vector<std::string> m_module_import_warnings;
@@ -911,9 +902,6 @@ class SwiftASTContext : public TypeSystemSwift {
911902
typedef ThreadSafeDenseSet<const char *> SwiftMangledNameSet;
912903
SwiftMangledNameSet m_negative_type_cache;
913904

914-
typedef ThreadSafeDenseMap<const char *, lldb::TypeSP> SwiftTypeMap;
915-
SwiftTypeMap m_swift_type_map;
916-
917905
/// @}
918906

919907
/// Record the set of stored properties for each nominal type declaration

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,6 @@ class TypeSystemSwift : public TypeSystem {
120120
virtual void SetTriple(const llvm::Triple triple) = 0;
121121
virtual void ClearModuleDependentCaches() = 0;
122122

123-
virtual lldb::TypeSP GetCachedType(ConstString mangled) = 0;
124-
virtual void SetCachedType(ConstString mangled,
125-
const lldb::TypeSP &type_sp) = 0;
126123
virtual bool IsImportedType(lldb::opaque_compiler_type_t type,
127124
CompilerType *original_type) = 0;
128125
virtual CompilerType GetErrorType() = 0;

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "Plugins/ExpressionParser/Clang/ClangExternalASTSourceCallbacks.h"
2626
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
27+
#include "Plugins/SymbolFile/DWARF/DWARFASTParserSwift.h"
2728
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
2829

2930
#include "swift/AST/ClangModuleLoader.h"
@@ -1295,6 +1296,12 @@ CompilerType TypeSystemSwift::GetInstanceType(CompilerType compiler_type) {
12951296
return {};
12961297
}
12971298

1299+
#ifndef NDEBUG
1300+
TypeSystemSwiftTypeRef::TypeSystemSwiftTypeRef() {}
1301+
#endif
1302+
1303+
TypeSystemSwiftTypeRef::~TypeSystemSwiftTypeRef() {}
1304+
12981305
TypeSystemSwiftTypeRef::TypeSystemSwiftTypeRef(Module &module) {
12991306
m_module = &module;
13001307
{
@@ -1365,16 +1372,16 @@ CompilerType TypeSystemSwiftTypeRef::GetTypeFromMangledTypename(
13651372
return {this, (opaque_compiler_type_t)mangled_typename.AsCString()};
13661373
}
13671374

1368-
lldb::TypeSP TypeSystemSwiftTypeRef::GetCachedType(ConstString mangled) {
1369-
if (auto *swift_ast_context = GetSwiftASTContext())
1370-
swift_ast_context->GetCachedType(mangled);
1375+
TypeSP TypeSystemSwiftTypeRef::GetCachedType(ConstString mangled) {
1376+
TypeSP type_sp;
1377+
if (m_swift_type_map.Lookup(mangled.GetCString(), type_sp))
1378+
return type_sp;
13711379
return {};
13721380
}
13731381

13741382
void TypeSystemSwiftTypeRef::SetCachedType(ConstString mangled,
1375-
const lldb::TypeSP &type_sp) {
1376-
if (auto *swift_ast_context = GetSwiftASTContext())
1377-
swift_ast_context->SetCachedType(mangled, type_sp);
1383+
const TypeSP &type_sp) {
1384+
m_swift_type_map.Insert(mangled.GetCString(), type_sp);
13781385
}
13791386

13801387
ConstString TypeSystemSwiftTypeRef::GetPluginName() {
@@ -1399,9 +1406,9 @@ void TypeSystemSwiftTypeRef::DiagnoseWarnings(Process &process,
13991406
}
14001407

14011408
DWARFASTParser *TypeSystemSwiftTypeRef::GetDWARFParser() {
1402-
if (auto *swift_ast_context = GetSwiftASTContext())
1403-
return swift_ast_context->GetDWARFParser();
1404-
return {};
1409+
if (!m_dwarf_ast_parser_up)
1410+
m_dwarf_ast_parser_up.reset(new DWARFASTParserSwift(*this));
1411+
return m_dwarf_ast_parser_up.get();
14051412
}
14061413

14071414
TypeSP TypeSystemSwiftTypeRef::LookupTypeInModule(
@@ -2287,6 +2294,27 @@ TypeSystemSwiftTypeRef::GetPointerType(opaque_compiler_type_t type) {
22872294
(ReconstructType(type)));
22882295
}
22892296

2297+
CompilerType TypeSystemSwiftTypeRef::GetVoidFunctionType() {
2298+
using namespace swift::Demangle;
2299+
Demangler dem;
2300+
NodePointer type = dem.createNode(Node::Kind::Type);
2301+
NodePointer fnty = dem.createNode(Node::Kind::FunctionType);
2302+
type->addChild(fnty, dem);
2303+
NodePointer args = dem.createNode(Node::Kind::ArgumentTuple);
2304+
fnty->addChild(args, dem);
2305+
NodePointer args_ty = dem.createNode(Node::Kind::Type);
2306+
args->addChild(args_ty, dem);
2307+
NodePointer args_tuple = dem.createNode(Node::Kind::Tuple);
2308+
args_ty->addChild(args_tuple, dem);
2309+
NodePointer rett = dem.createNode(Node::Kind::ReturnType);
2310+
fnty->addChild(rett, dem);
2311+
NodePointer ret_ty = dem.createNode(Node::Kind::Type);
2312+
rett->addChild(ret_ty, dem);
2313+
NodePointer ret_tuple = dem.createNode(Node::Kind::Tuple);
2314+
ret_ty->addChild(ret_tuple, dem);
2315+
return RemangleAsType(dem, type);
2316+
}
2317+
22902318
// Exploring the type
22912319
llvm::Optional<uint64_t>
22922320
TypeSystemSwiftTypeRef::GetBitSize(opaque_compiler_type_t type,

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "Plugins/TypeSystem/Swift/TypeSystemSwift.h"
1717
#include "lldb/Core/SwiftForward.h"
18+
#include "lldb/Core/ThreadSafeDenseMap.h"
1819

1920
#include "swift/AST/Type.h"
2021

@@ -51,8 +52,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
5152

5253
#ifndef NDEBUG
5354
/// Provided only for unit tests.
54-
TypeSystemSwiftTypeRef() {}
55+
TypeSystemSwiftTypeRef();
5556
#endif
57+
~TypeSystemSwiftTypeRef();
5658
TypeSystemSwiftTypeRef(Module &module);
5759
TypeSystemSwiftTypeRef(SwiftASTContextForExpressions &swift_ast_context);
5860
SwiftASTContext *GetSwiftASTContext() const override;
@@ -147,6 +149,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
147149
CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override;
148150
CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override;
149151

152+
/// Get a function type that returns nothing and take no parameters.
153+
CompilerType GetVoidFunctionType();
154+
150155
// Exploring the type
151156
llvm::Optional<uint64_t>
152157
GetBitSize(lldb::opaque_compiler_type_t type,
@@ -235,8 +240,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
235240
CompilerType *pointee_type, bool *is_rvalue) override;
236241

237242
// Swift-specific methods.
238-
lldb::TypeSP GetCachedType(ConstString mangled) override;
239-
void SetCachedType(ConstString mangled, const lldb::TypeSP &type_sp) override;
243+
lldb::TypeSP GetCachedType(ConstString mangled);
244+
void SetCachedType(ConstString mangled, const lldb::TypeSP &type_sp);
240245
bool IsImportedType(lldb::opaque_compiler_type_t type,
241246
CompilerType *original_type) override;
242247
/// Like \p IsImportedType(), but even returns Clang types that are also Swift
@@ -353,12 +358,16 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
353358
/// The sibling SwiftASTContext.
354359
mutable lldb::TypeSystemSP m_swift_ast_context_sp;
355360
mutable SwiftASTContext *m_swift_ast_context = nullptr;
361+
std::unique_ptr<DWARFASTParser> m_dwarf_ast_parser_up;
356362

357363
/// The APINotesManager responsible for each Clang module.
358364
llvm::DenseMap<clang::Module *,
359365
std::unique_ptr<clang::api_notes::APINotesManager>>
360366
m_apinotes_manager;
361-
};
367+
368+
/// All lldb::Type pointers produced by DWARFASTParser Swift go here.
369+
ThreadSafeDenseMap<const char *, lldb::TypeSP> m_swift_type_map;
370+
};
362371

363372
} // namespace lldb_private
364373
#endif

lldb/test/API/lang/swift/clangimporter/Werror/TestSwiftStripWerror.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ def test(self):
3535
self.assertFalse("-Werror" in line)
3636
if "-DCONFLICT" in line:
3737
sanity += 1
38-
# We see it twice, once in the module, once in the expression context.
39-
self.assertEqual(sanity, 2)
38+
# We see it twice in the expression context and once in a Module context.
39+
# -DCONFLICT=0
40+
# -DCONFLICT=1
41+
self.assertEqual(sanity, 2+1)

lldb/test/API/lang/swift/clangimporter/config_macros/TestSwiftDedupMacros.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def testSwiftDebugMacros(self):
6464
space_with_space += 1
6565
if "-UNDEBUG" in line:
6666
ndebug += 1
67-
self.assertEqual(debug, 1)
68-
self.assertEqual(space, 1)
67+
# One extra in SwiftASTContextPerModule.
68+
self.assertEqual(debug, 1+1)
69+
self.assertEqual(space, 1+1)
6970
self.assertEqual(space_with_space, 0)
70-
self.assertEqual(ndebug, 1)
71+
self.assertEqual(ndebug, 1+1)

0 commit comments

Comments
 (0)