Skip to content

Add a test to ensure that no SwiftASTContext is initialized for setti… #3437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 11 additions & 8 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserSwift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "Plugins/TypeSystem/Swift/SwiftASTContext.h"
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
#include "lldb/Core/Module.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/Function.h"
Expand All @@ -41,7 +41,9 @@
using namespace lldb;
using namespace lldb_private;

DWARFASTParserSwift::DWARFASTParserSwift(SwiftASTContext &ast) : m_ast(ast) {}
DWARFASTParserSwift::DWARFASTParserSwift(
TypeSystemSwiftTypeRef &swift_typesystem)
: m_swift_typesystem(swift_typesystem) {}

DWARFASTParserSwift::~DWARFASTParserSwift() {}

Expand Down Expand Up @@ -151,7 +153,7 @@ lldb::TypeSP DWARFASTParserSwift::ParseTypeFromDWARF(const SymbolContext &sc,
}

if (mangled_name) {
type_sp = m_ast.GetCachedType(mangled_name);
type_sp = m_swift_typesystem.GetCachedType(mangled_name);
if (type_sp)
return type_sp;

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

// Try to import the type from one of the loaded Swift modules.
if (SwiftLanguageRuntime::IsSwiftMangledName(mangled_name.GetCString()))
compiler_type = m_ast.GetTypeFromMangledTypename(mangled_name);
compiler_type =
m_swift_typesystem.GetTypeFromMangledTypename(mangled_name);
}

if (!compiler_type && name) {
// Handle Archetypes, which are typedefs to RawPointerType.
llvm::StringRef typedef_name = GetTypedefName(die);
if (typedef_name.startswith("$sBp")) {
preferred_name = name;
compiler_type =
m_ast.GetTypeFromMangledTypename(ConstString(typedef_name));
compiler_type = m_swift_typesystem.GetTypeFromMangledTypename(
ConstString(typedef_name));
}
}

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

return type_sp;
Expand Down
6 changes: 3 additions & 3 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserSwift.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
class DWARFDebugInfoEntry;
class DWARFDIECollection;

namespace lldb_private { class SwiftASTContext; }
namespace lldb_private { class TypeSystemSwiftTypeRef; }

class DWARFASTParserSwift : public DWARFASTParser {
public:
DWARFASTParserSwift(lldb_private::SwiftASTContext &ast);
DWARFASTParserSwift(lldb_private::TypeSystemSwiftTypeRef &swift_typesystem);

virtual ~DWARFASTParserSwift();

Expand Down Expand Up @@ -56,7 +56,7 @@ class DWARFASTParserSwift : public DWARFASTParser {
lldb_private::CompilerDeclContext decl_context) override {}

protected:
lldb_private::SwiftASTContext &m_ast;
lldb_private::TypeSystemSwiftTypeRef &m_swift_typesystem;
};

#endif // SymbolFileDWARF_DWARFASTParserSwift_h_
32 changes: 1 addition & 31 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@
#include "Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h"
#include "Plugins/Platform/MacOSX/PlatformDarwin.h"
#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
#include "Plugins/SymbolFile/DWARF/DWARFASTParserSwift.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"

#define VALID_OR_RETURN(value) \
Expand Down Expand Up @@ -4514,20 +4513,6 @@ CompilerType SwiftASTContext::GetAnyObjectType() {
return ToCompilerType({ast->getAnyObjectType()});
}

CompilerType SwiftASTContext::GetVoidFunctionType() {
VALID_OR_RETURN(CompilerType());

if (!m_void_function_type) {
swift::ASTContext *ast = GetASTContext();
swift::Type empty_tuple_type(swift::TupleType::getEmpty(*ast));
// FIXME: Verify ExtInfo state is correct, not working by accident.
swift::FunctionType::ExtInfo info;
m_void_function_type =
ToCompilerType({swift::FunctionType::get({}, empty_tuple_type, info)});
}
return m_void_function_type;
}

static CompilerType ValueDeclToType(swift::ValueDecl *decl,
swift::ASTContext *ast) {
if (decl) {
Expand Down Expand Up @@ -8332,23 +8317,8 @@ void SwiftASTContext::DumpTypeDescription(opaque_compiler_type_t type,
s->Printf("<could not resolve type>");
}

TypeSP SwiftASTContext::GetCachedType(ConstString mangled) {
TypeSP type_sp;
if (m_swift_type_map.Lookup(mangled.GetCString(), type_sp))
return type_sp;
else
return TypeSP();
}

void SwiftASTContext::SetCachedType(ConstString mangled,
const TypeSP &type_sp) {
m_swift_type_map.Insert(mangled.GetCString(), type_sp);
}

DWARFASTParser *SwiftASTContext::GetDWARFParser() {
if (!m_dwarf_ast_parser_ap)
m_dwarf_ast_parser_ap.reset(new DWARFASTParserSwift(*this));
return m_dwarf_ast_parser_ap.get();
return GetTypeSystemSwiftTypeRef().GetDWARFParser();
}

std::vector<lldb::DataBufferSP> &
Expand Down
12 changes: 0 additions & 12 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h"
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
#include "lldb/Core/SwiftForward.h"
#include "lldb/Core/ThreadSafeDenseMap.h"
#include "lldb/Core/ThreadSafeDenseSet.h"
#include "lldb/Utility/Either.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -341,9 +340,6 @@ class SwiftASTContext : public TypeSystemSwift {
// Retrieve the Swift.AnyObject type.
CompilerType GetAnyObjectType();

// Get a function type that returns nothing and take no parameters
CompilerType GetVoidFunctionType();

/// Import and Swiftify a Clang type.
/// \return Returns an invalid type if unsuccessful.
CompilerType ImportClangType(CompilerType clang_type);
Expand Down Expand Up @@ -761,10 +757,6 @@ class SwiftASTContext : public TypeSystemSwift {

CompilerType GetReferentType(lldb::opaque_compiler_type_t type) override;

lldb::TypeSP GetCachedType(ConstString mangled) override;

void SetCachedType(ConstString mangled, const lldb::TypeSP &type_sp) override;

/// Retrieves the modules that need to be implicitly imported in a given
/// execution scope. This includes the modules imported by both the compile
/// unit as well as any imports from previous expression evaluations.
Expand Down Expand Up @@ -858,7 +850,6 @@ class SwiftASTContext : public TypeSystemSwift {
llvm::once_flag m_ir_gen_module_once;
std::unique_ptr<swift::DiagnosticConsumer> m_diagnostic_consumer_ap;
std::unique_ptr<swift::DependencyTracker> m_dependency_tracker;
std::unique_ptr<DWARFASTParser> m_dwarf_ast_parser_ap;
/// A collection of (not necessarily fatal) error messages that
/// should be printed by Process::PrintWarningCantLoadSwift().
std::vector<std::string> m_module_import_warnings;
Expand Down Expand Up @@ -911,9 +902,6 @@ class SwiftASTContext : public TypeSystemSwift {
typedef ThreadSafeDenseSet<const char *> SwiftMangledNameSet;
SwiftMangledNameSet m_negative_type_cache;

typedef ThreadSafeDenseMap<const char *, lldb::TypeSP> SwiftTypeMap;
SwiftTypeMap m_swift_type_map;

/// @}

/// Record the set of stored properties for each nominal type declaration
Expand Down
3 changes: 0 additions & 3 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ class TypeSystemSwift : public TypeSystem {
virtual void SetTriple(const llvm::Triple triple) = 0;
virtual void ClearModuleDependentCaches() = 0;

virtual lldb::TypeSP GetCachedType(ConstString mangled) = 0;
virtual void SetCachedType(ConstString mangled,
const lldb::TypeSP &type_sp) = 0;
virtual bool IsImportedType(lldb::opaque_compiler_type_t type,
CompilerType *original_type) = 0;
virtual CompilerType GetErrorType() = 0;
Expand Down
53 changes: 43 additions & 10 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "Plugins/ExpressionParser/Clang/ClangExternalASTSourceCallbacks.h"
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
#include "Plugins/SymbolFile/DWARF/DWARFASTParserSwift.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"

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

#ifndef NDEBUG
TypeSystemSwiftTypeRef::TypeSystemSwiftTypeRef() {}
#endif

TypeSystemSwiftTypeRef::~TypeSystemSwiftTypeRef() {}

TypeSystemSwiftTypeRef::TypeSystemSwiftTypeRef(Module &module) {
m_module = &module;
{
Expand Down Expand Up @@ -1328,13 +1335,18 @@ SwiftASTContext *TypeSystemSwiftTypeRef::GetSwiftASTContext() const {
return m_swift_ast_context;
}

SwiftASTContext *TypeSystemSwiftTypeRef::GetSwiftASTContextOrNull() const {
return m_swift_ast_context;
}

void TypeSystemSwiftTypeRef::SetTriple(const llvm::Triple triple) {
if (auto *swift_ast_context = GetSwiftASTContext())
swift_ast_context->SetTriple(triple);
}

void TypeSystemSwiftTypeRef::ClearModuleDependentCaches() {
if (auto *swift_ast_context = GetSwiftASTContext())
// There is no need to notify a not-yet created SwiftASTContext to reset.
if (auto *swift_ast_context = GetSwiftASTContextOrNull())
swift_ast_context->ClearModuleDependentCaches();
}
const char *TypeSystemSwiftTypeRef::AsMangledName(opaque_compiler_type_t type) {
Expand Down Expand Up @@ -1365,16 +1377,16 @@ CompilerType TypeSystemSwiftTypeRef::GetTypeFromMangledTypename(
return {this, (opaque_compiler_type_t)mangled_typename.AsCString()};
}

lldb::TypeSP TypeSystemSwiftTypeRef::GetCachedType(ConstString mangled) {
if (auto *swift_ast_context = GetSwiftASTContext())
swift_ast_context->GetCachedType(mangled);
TypeSP TypeSystemSwiftTypeRef::GetCachedType(ConstString mangled) {
TypeSP type_sp;
if (m_swift_type_map.Lookup(mangled.GetCString(), type_sp))
return type_sp;
return {};
}

void TypeSystemSwiftTypeRef::SetCachedType(ConstString mangled,
const lldb::TypeSP &type_sp) {
if (auto *swift_ast_context = GetSwiftASTContext())
swift_ast_context->SetCachedType(mangled, type_sp);
const TypeSP &type_sp) {
m_swift_type_map.Insert(mangled.GetCString(), type_sp);
}

ConstString TypeSystemSwiftTypeRef::GetPluginName() {
Expand All @@ -1399,9 +1411,9 @@ void TypeSystemSwiftTypeRef::DiagnoseWarnings(Process &process,
}

DWARFASTParser *TypeSystemSwiftTypeRef::GetDWARFParser() {
if (auto *swift_ast_context = GetSwiftASTContext())
return swift_ast_context->GetDWARFParser();
return {};
if (!m_dwarf_ast_parser_up)
m_dwarf_ast_parser_up.reset(new DWARFASTParserSwift(*this));
return m_dwarf_ast_parser_up.get();
}

TypeSP TypeSystemSwiftTypeRef::LookupTypeInModule(
Expand Down Expand Up @@ -2287,6 +2299,27 @@ TypeSystemSwiftTypeRef::GetPointerType(opaque_compiler_type_t type) {
(ReconstructType(type)));
}

CompilerType TypeSystemSwiftTypeRef::GetVoidFunctionType() {
using namespace swift::Demangle;
Demangler dem;
NodePointer type = dem.createNode(Node::Kind::Type);
NodePointer fnty = dem.createNode(Node::Kind::FunctionType);
type->addChild(fnty, dem);
NodePointer args = dem.createNode(Node::Kind::ArgumentTuple);
fnty->addChild(args, dem);
NodePointer args_ty = dem.createNode(Node::Kind::Type);
args->addChild(args_ty, dem);
NodePointer args_tuple = dem.createNode(Node::Kind::Tuple);
args_ty->addChild(args_tuple, dem);
NodePointer rett = dem.createNode(Node::Kind::ReturnType);
fnty->addChild(rett, dem);
NodePointer ret_ty = dem.createNode(Node::Kind::Type);
rett->addChild(ret_ty, dem);
NodePointer ret_tuple = dem.createNode(Node::Kind::Tuple);
ret_ty->addChild(ret_tuple, dem);
return RemangleAsType(dem, type);
}

// Exploring the type
llvm::Optional<uint64_t>
TypeSystemSwiftTypeRef::GetBitSize(opaque_compiler_type_t type,
Expand Down
20 changes: 16 additions & 4 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "Plugins/TypeSystem/Swift/TypeSystemSwift.h"
#include "lldb/Core/SwiftForward.h"
#include "lldb/Core/ThreadSafeDenseMap.h"

#include "swift/AST/Type.h"

Expand Down Expand Up @@ -51,11 +52,15 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {

#ifndef NDEBUG
/// Provided only for unit tests.
TypeSystemSwiftTypeRef() {}
TypeSystemSwiftTypeRef();
#endif
~TypeSystemSwiftTypeRef();
TypeSystemSwiftTypeRef(Module &module);
TypeSystemSwiftTypeRef(SwiftASTContextForExpressions &swift_ast_context);
/// Get the corresponding SwiftASTContext, and create one if necessary.
SwiftASTContext *GetSwiftASTContext() const override;
/// Return SwiftASTContext, iff one has already been created.
SwiftASTContext *GetSwiftASTContextOrNull() const;
TypeSystemSwiftTypeRef &GetTypeSystemSwiftTypeRef() override { return *this; }
void SetTriple(const llvm::Triple triple) override;
void ClearModuleDependentCaches() override;
Expand Down Expand Up @@ -147,6 +152,9 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override;
CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override;

/// Get a function type that returns nothing and take no parameters.
CompilerType GetVoidFunctionType();

// Exploring the type
llvm::Optional<uint64_t>
GetBitSize(lldb::opaque_compiler_type_t type,
Expand Down Expand Up @@ -235,8 +243,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
CompilerType *pointee_type, bool *is_rvalue) override;

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

/// The APINotesManager responsible for each Clang module.
llvm::DenseMap<clang::Module *,
std::unique_ptr<clang::api_notes::APINotesManager>>
m_apinotes_manager;
};

/// All lldb::Type pointers produced by DWARFASTParser Swift go here.
ThreadSafeDenseMap<const char *, lldb::TypeSP> m_swift_type_map;
};

} // namespace lldb_private
#endif
2 changes: 2 additions & 0 deletions lldb/test/API/lang/swift/breakpoint_perf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SWIFT_SOURCES := main.swift
include Makefile.rules
Loading