@@ -1190,16 +1190,63 @@ static Mirror ObjC_getMirrorForSuperclass(Class sup,
1190
1190
}
1191
1191
1192
1192
// NB: This function is not used directly in the Swift codebase, but is
1193
- // exported for Xcode support. Please coordinate before changing.
1193
+ // exported for Xcode support and is used by the sanitizers. Please coordinate
1194
+ // before changing.
1195
+ //
1196
+ // / Demangles a Swift symbol name.
1197
+ // /
1198
+ // / \param mangledName is the symbol name that needs to be demangled.
1199
+ // / \param mangledNameLength is the length of the string that should be
1200
+ // / demangled.
1201
+ // / \param outputBuffer is the user provided buffer where the demangled name
1202
+ // / will be placed. If nullptr, a new buffer will be malloced. In that case,
1203
+ // / the user of this API is responsible for freeing the returned buffer.
1204
+ // / \param outputBufferSize is the size of the output buffer. If the demangled
1205
+ // / name does not fit into the outputBuffer, the output will be truncated and
1206
+ // / the size will be updated, indicating how large the buffer should be.
1207
+ // / \param flags can be used to select the demangling style. TODO: We should
1208
+ // // define what these will be.
1209
+ // / \returns the demangled name. Returns nullptr if the input String is not a
1210
+ // / Swift mangled name.
1194
1211
SWIFT_RUNTIME_EXPORT
1195
- extern " C" void swift_stdlib_demangleName (const char *mangledName,
1196
- size_t mangledNameLength,
1197
- String *demangledName) {
1212
+ extern " C" char *swift_demangle (const char *mangledName,
1213
+ size_t mangledNameLength,
1214
+ char *outputBuffer,
1215
+ size_t *outputBufferSize,
1216
+ uint32_t flags) {
1217
+ if (flags != 0 ) {
1218
+ swift::fatalError (0 , " Only 'flags' value of '0' is currently supported." );
1219
+ }
1220
+ if (outputBuffer != nullptr && outputBufferSize == nullptr ) {
1221
+ swift::fatalError (0 , " 'outputBuffer' is passed but the size is 'nullptr'." );
1222
+ }
1223
+
1224
+ // Check if we are dealing with Swift mangled name, otherwise, don't try
1225
+ // to demangle and send indication to the user.
1226
+ if (mangledName[0 ] != ' _' || mangledName[1 ] != ' T' ) {
1227
+ return nullptr ;
1228
+ }
1229
+
1230
+ // Demangle the name.
1198
1231
auto options = Demangle::DemangleOptions ();
1199
1232
options.DisplayDebuggerGeneratedModule = false ;
1200
1233
auto result =
1201
1234
Demangle::demangleSymbolAsString (mangledName,
1202
1235
mangledNameLength,
1203
1236
options);
1204
- new (demangledName) String (result.data (), result.size ());
1237
+
1238
+ // If the output buffer is not provided, malloc memory ourselves.
1239
+ if (outputBuffer == nullptr || *outputBufferSize == 0 ) {
1240
+ return strdup (result.c_str ());
1241
+ }
1242
+
1243
+ // Indicate a failure if the result does not fit and will be truncated
1244
+ // and set the required outputBufferSize.
1245
+ if (*outputBufferSize < result.length () + 1 ) {
1246
+ *outputBufferSize = result.length () + 1 ;
1247
+ }
1248
+
1249
+ // Copy into the provided buffer.
1250
+ strlcpy (outputBuffer, result.c_str (), *outputBufferSize);
1251
+ return outputBuffer;
1205
1252
}
0 commit comments