Skip to content

Commit 8b8811f

Browse files
committed
Merge pull request #2387 from bitjammer/remote-mirror-demangle
SwiftRemoteMirror: Add convenience API for demangling
2 parents 7d6005b + 97fe3e7 commit 8b8811f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

include/swift/SwiftRemoteMirror/SwiftRemoteMirror.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "MemoryReaderInterface.h"
2424
#include "SwiftRemoteMirrorTypes.h"
2525

26+
#include <stdlib.h>
27+
2628
/// Major version changes when there are ABI or source incompatible changes.
2729
#define SWIFT_REFLECTION_VERSION_MAJOR 3
2830

@@ -159,6 +161,18 @@ void swift_reflection_dumpInfoForMetadata(SwiftReflectionContextRef ContextRef,
159161
void swift_reflection_dumpInfoForInstance(SwiftReflectionContextRef ContextRef,
160162
uintptr_t Object);
161163

164+
/// Demangle a type name.
165+
///
166+
/// Copies at most `MaxLength` bytes from the demangled name string into
167+
/// `OutDemangledName`.
168+
///
169+
/// Returns the length of the demangled string this function tried to copy
170+
/// into `OutDemangledName`.
171+
size_t swift_reflection_demangle(const char *MangledName,
172+
size_t Length,
173+
char *OutDemangledName,
174+
size_t MaxLength);
175+
162176
#ifdef __cplusplus
163177
} // extern "C"
164178
#endif

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,14 @@ void swift_reflection_dumpInfoForInstance(SwiftReflectionContextRef ContextRef,
287287
TI->dump();
288288
}
289289
}
290+
291+
size_t swift_reflection_demangle(const char *MangledName, size_t Length,
292+
char *OutDemangledName, size_t MaxLength) {
293+
if (MangledName == NULL || Length == 0)
294+
return 0;
295+
296+
std::string Mangled(MangledName, Length);
297+
auto Demangled = Demangle::demangleTypeAsString(Mangled);
298+
strncpy(OutDemangledName, Demangled.c_str(), MaxLength);
299+
return Demangled.size();
300+
}

0 commit comments

Comments
 (0)