Skip to content

Commit 461c937

Browse files
Merge pull request #3436 from adrian-prantl/dwarfastparserswift-next
Move the ownership of DWARFASTParserSwift from SwiftASTContext to Typ…
2 parents 8e67056 + f7d0d79 commit 461c937

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
@@ -128,7 +128,6 @@
128128
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
129129
#include "Plugins/Platform/MacOSX/PlatformDarwin.h"
130130
#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
131-
#include "Plugins/SymbolFile/DWARF/DWARFASTParserSwift.h"
132131
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
133132

134133
#define VALID_OR_RETURN(value) \
@@ -4491,20 +4490,6 @@ CompilerType SwiftASTContext::GetAnyObjectType() {
44914490
return ToCompilerType({ast->getAnyObjectType()});
44924491
}
44934492

4494-
CompilerType SwiftASTContext::GetVoidFunctionType() {
4495-
VALID_OR_RETURN(CompilerType());
4496-
4497-
if (!m_void_function_type) {
4498-
swift::ASTContext *ast = GetASTContext();
4499-
swift::Type empty_tuple_type(swift::TupleType::getEmpty(*ast));
4500-
// FIXME: Verify ExtInfo state is correct, not working by accident.
4501-
swift::FunctionType::ExtInfo info;
4502-
m_void_function_type =
4503-
ToCompilerType({swift::FunctionType::get({}, empty_tuple_type, info)});
4504-
}
4505-
return m_void_function_type;
4506-
}
4507-
45084493
static CompilerType ValueDeclToType(swift::ValueDecl *decl,
45094494
swift::ASTContext *ast) {
45104495
if (decl) {
@@ -8312,23 +8297,8 @@ void SwiftASTContext::DumpTypeDescription(opaque_compiler_type_t type,
83128297
s->Printf("<could not resolve type>");
83138298
}
83148299

8315-
TypeSP SwiftASTContext::GetCachedType(ConstString mangled) {
8316-
TypeSP type_sp;
8317-
if (m_swift_type_map.Lookup(mangled.GetCString(), type_sp))
8318-
return type_sp;
8319-
else
8320-
return TypeSP();
8321-
}
8322-
8323-
void SwiftASTContext::SetCachedType(ConstString mangled,
8324-
const TypeSP &type_sp) {
8325-
m_swift_type_map.Insert(mangled.GetCString(), type_sp);
8326-
}
8327-
83288300
DWARFASTParser *SwiftASTContext::GetDWARFParser() {
8329-
if (!m_dwarf_ast_parser_ap)
8330-
m_dwarf_ast_parser_ap.reset(new DWARFASTParserSwift(*this));
8331-
return m_dwarf_ast_parser_ap.get();
8301+
return GetTypeSystemSwiftTypeRef().GetDWARFParser();
83328302
}
83338303

83348304
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
@@ -119,9 +119,6 @@ class TypeSystemSwift : public TypeSystem {
119119
virtual void SetTriple(const llvm::Triple triple) = 0;
120120
virtual void ClearModuleDependentCaches() = 0;
121121

122-
virtual lldb::TypeSP GetCachedType(ConstString mangled) = 0;
123-
virtual void SetCachedType(ConstString mangled,
124-
const lldb::TypeSP &type_sp) = 0;
125122
virtual bool IsImportedType(lldb::opaque_compiler_type_t type,
126123
CompilerType *original_type) = 0;
127124
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() {
@@ -1398,9 +1405,9 @@ void TypeSystemSwiftTypeRef::DiagnoseWarnings(Process &process,
13981405
}
13991406

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

14061413
TypeSP TypeSystemSwiftTypeRef::LookupTypeInModule(
@@ -2286,6 +2293,27 @@ TypeSystemSwiftTypeRef::GetPointerType(opaque_compiler_type_t type) {
22862293
(ReconstructType(type)));
22872294
}
22882295

2296+
CompilerType TypeSystemSwiftTypeRef::GetVoidFunctionType() {
2297+
using namespace swift::Demangle;
2298+
Demangler dem;
2299+
NodePointer type = dem.createNode(Node::Kind::Type);
2300+
NodePointer fnty = dem.createNode(Node::Kind::FunctionType);
2301+
type->addChild(fnty, dem);
2302+
NodePointer args = dem.createNode(Node::Kind::ArgumentTuple);
2303+
fnty->addChild(args, dem);
2304+
NodePointer args_ty = dem.createNode(Node::Kind::Type);
2305+
args->addChild(args_ty, dem);
2306+
NodePointer args_tuple = dem.createNode(Node::Kind::Tuple);
2307+
args_ty->addChild(args_tuple, dem);
2308+
NodePointer rett = dem.createNode(Node::Kind::ReturnType);
2309+
fnty->addChild(rett, dem);
2310+
NodePointer ret_ty = dem.createNode(Node::Kind::Type);
2311+
rett->addChild(ret_ty, dem);
2312+
NodePointer ret_tuple = dem.createNode(Node::Kind::Tuple);
2313+
ret_ty->addChild(ret_tuple, dem);
2314+
return RemangleAsType(dem, type);
2315+
}
2316+
22892317
// Exploring the type
22902318
llvm::Optional<uint64_t>
22912319
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;
@@ -146,6 +148,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
146148
CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override;
147149
CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override;
148150

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

236241
// Swift-specific methods.
237-
lldb::TypeSP GetCachedType(ConstString mangled) override;
238-
void SetCachedType(ConstString mangled, const lldb::TypeSP &type_sp) override;
242+
lldb::TypeSP GetCachedType(ConstString mangled);
243+
void SetCachedType(ConstString mangled, const lldb::TypeSP &type_sp);
239244
bool IsImportedType(lldb::opaque_compiler_type_t type,
240245
CompilerType *original_type) override;
241246
/// Like \p IsImportedType(), but even returns Clang types that are also Swift
@@ -352,12 +357,16 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
352357
/// The sibling SwiftASTContext.
353358
mutable lldb::TypeSystemSP m_swift_ast_context_sp;
354359
mutable SwiftASTContext *m_swift_ast_context = nullptr;
360+
std::unique_ptr<DWARFASTParser> m_dwarf_ast_parser_up;
355361

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

362371
} // namespace lldb_private
363372
#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)