Skip to content

Commit 5b12a15

Browse files
committed
Print the Swift mangled name as a field in C++ interop generated classes
Print the Swift mangled name as a constexpr static char as a field in compiler generated types so LLDB can display them nicely to users.
1 parent 08bc6ec commit 5b12a15

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,14 @@ void ClangSyntaxPrinter::printKnownCType(
435435
if (info->canBeNullable)
436436
os << " _Null_unspecified";
437437
}
438+
439+
void ClangSyntaxPrinter::printSwiftMangledNameForDebugger(
440+
const NominalTypeDecl *typeDecl) {
441+
auto mangled_name =
442+
mangler.mangleTypeForDebugger(typeDecl->getDeclaredInterfaceType(), nullptr);
443+
if (!mangled_name.empty()) {
444+
os << " typedef char " << mangled_name << ";\n";
445+
os << " static inline constexpr " << mangled_name
446+
<< " __swift_mangled_name = 0;\n";
447+
}
448+
}

lib/PrintAsClang/ClangSyntaxPrinter.h

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_PRINTASCLANG_CLANGSYNTAXPRINTER_H
1414
#define SWIFT_PRINTASCLANG_CLANGSYNTAXPRINTER_H
1515

16+
#include "swift/AST/ASTMangler.h"
1617
#include "swift/AST/Type.h"
1718
#include "swift/Basic/LLVM.h"
1819
#include "swift/ClangImporter/ClangImporter.h"
@@ -230,8 +231,14 @@ class ClangSyntaxPrinter {
230231
/// Print the given **known** type as a C type.
231232
void printKnownCType(Type t, PrimitiveTypeMapping &typeMapping) const;
232233

234+
/// Print the nominal type's Swift mangled name as a typedef from a char to
235+
/// the mangled name, and a static constexpr variable declaration, whose type
236+
/// is the aforementioned typedef, and whose name is known to the debugger.
237+
void printSwiftMangledNameForDebugger(const NominalTypeDecl *typeDecl);
238+
233239
protected:
234240
raw_ostream &os;
241+
swift::Mangle::ASTMangler mangler;
235242
};
236243

237244
} // end namespace swift

lib/PrintAsClang/PrintClangClassType.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ void ClangClassTypePrinter::printClassTypeDecl(
7777
os << " friend class " << cxx_synthesis::getCxxImplNamespaceName() << "::";
7878
printCxxImplClassName(os, typeDecl);
7979
os << ";\n";
80+
81+
printer.printSwiftMangledNameForDebugger(typeDecl);
82+
8083
os << "};\n\n";
8184

8285
// Print out the "hidden" _impl class.

lib/PrintAsClang/PrintClangValueType.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ void ClangValueTypePrinter::printValueTypeDecl(
383383
printCxxImplClassName(os, typeDecl);
384384
printGenericParamRefs(os);
385385
os << ";\n";
386+
387+
printer.printSwiftMangledNameForDebugger(typeDecl);
388+
386389
os << "};\n";
387390
os << '\n';
388391

0 commit comments

Comments
 (0)