-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[stdlib] Introduce demangle function #25314
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2019 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftShims | ||
|
||
/// Represents the potential return types from a call to demangle. | ||
public enum DemangleResult: Equatable { | ||
/// The demangle completed successfully. | ||
case success | ||
|
||
/// The demangle resulted in truncating the result. The payload value is the | ||
/// number of bytes necessary for a full demangle. | ||
case truncated(Int) | ||
|
||
/// The passed Swift mangled symbol was invalid. | ||
case invalidSymbol | ||
} | ||
|
||
/// Given a mangled Swift symbol, demangle it into a human readable format. | ||
/// | ||
/// Valid Swift symbols begin with the following prefixes: | ||
/// ┌─────────────────────╥────────┐ | ||
/// │ Swift Version ║ │ | ||
/// ╞═════════════════════╬════════╡ | ||
/// │ Swift 3 and below ║ _T │ | ||
/// ├─────────────────────╫────────┤ | ||
/// │ Swift 4 ║ _T0 │ | ||
/// ├─────────────────────╫────────┤ | ||
/// │ Swift 4.x ║ $S │ | ||
/// ├─────────────────────╫────────┤ | ||
/// │ Swift 5+ ║ $s │ | ||
/// └─────────────────────╨────────┘ | ||
/// | ||
/// - Parameters: | ||
/// - mangledName: A mangled Swift symbol. | ||
/// - Returns: A human readable demangled Swift symbol. | ||
public func demangle(_ mangledName: String) -> String? { | ||
return mangledName.utf8CString.withUnsafeBufferPointer { | ||
let demangledPtr = _swift_stdlib_demangle( | ||
/* mangledName */ $0.baseAddress, | ||
/* mangledNameLength */ $0.count - 1, | ||
/* outputBuffer */ nil, | ||
/* outputBufferSize */ nil, | ||
/* flags */ 0 | ||
) | ||
|
||
guard demangledPtr != nil else { | ||
return nil | ||
} | ||
|
||
let demangledName = String(cString: demangledPtr!) | ||
_swift_stdlib_free(demangledPtr!) | ||
return demangledName | ||
} | ||
} | ||
|
||
/// Given a mangled Swift symbol, demangle it into a human readable format. | ||
/// | ||
/// Valid Swift symbols begin with the following prefixes: | ||
/// ┌─────────────────────╥────────┐ | ||
/// │ Swift Version ║ │ | ||
/// ╞═════════════════════╬════════╡ | ||
/// │ Swift 3 and below ║ _T │ | ||
/// ├─────────────────────╫────────┤ | ||
/// │ Swift 4 ║ _T0 │ | ||
/// ├─────────────────────╫────────┤ | ||
/// │ Swift 4.x ║ $S │ | ||
/// ├─────────────────────╫────────┤ | ||
/// │ Swift 5+ ║ $s │ | ||
/// └─────────────────────╨────────┘ | ||
/// | ||
/// - Parameters: | ||
/// - mangledNameBuffer: A buffer pointer pointing to a null-terminated C | ||
/// string that contains the mangled Swift symbol. | ||
/// - buffer: A pre-allocated buffer to demangle the Swift symbol into. | ||
/// - Returns: An enum, `DemangleResult`, indicating the various result states | ||
/// of demangling. | ||
public func demangle( | ||
_ mangledNameBuffer: UnsafeBufferPointer<Int8>, | ||
into buffer: UnsafeMutableBufferPointer<Int8> | ||
) -> DemangleResult { | ||
var bufferSize = buffer.count | ||
|
||
let demangledPtr = _swift_stdlib_demangle( | ||
/* mangledName */ mangledNameBuffer.baseAddress, | ||
/* mangledNameLength */ mangledNameBuffer.count - 1, | ||
/* outputBuffer */ buffer.baseAddress, | ||
/* outputBufferSize */ &bufferSize, | ||
/* flags */ 0 | ||
) | ||
|
||
guard demangledPtr != nil else { | ||
return .invalidSymbol | ||
} | ||
|
||
// If the buffer size is still equal to the buffer count, the demangle was | ||
// successful. | ||
if bufferSize == buffer.count { | ||
return .success | ||
} | ||
|
||
// However if it's not equal, the result was truncated. Return the amount | ||
// needed to get a full demangle. | ||
return .truncated(bufferSize) | ||
} | ||
|
||
/// Given a mangled Swift symbol, demangle it into a human readable format. | ||
/// | ||
/// Valid Swift symbols begin with the following prefixes: | ||
/// ┌─────────────────────╥────────┐ | ||
/// │ Swift Version ║ │ | ||
/// ╞═════════════════════╬════════╡ | ||
/// │ Swift 3 and below ║ _T │ | ||
/// ├─────────────────────╫────────┤ | ||
/// │ Swift 4 ║ _T0 │ | ||
/// ├─────────────────────╫────────┤ | ||
/// │ Swift 4.x ║ $S │ | ||
/// ├─────────────────────╫────────┤ | ||
/// │ Swift 5+ ║ $s │ | ||
/// └─────────────────────╨────────┘ | ||
/// | ||
/// - Parameters: | ||
/// - mangledName: A mangled Swift symbol. | ||
/// - buffer: A pre-allocated buffer to demangle the Swift symbol into. | ||
/// - Returns: An enum, `DemangleResult`, indicating the various result states | ||
/// of demangling. | ||
public func demangle( | ||
_ mangledName: String, | ||
into buffer: UnsafeMutableBufferPointer<Int8> | ||
) -> DemangleResult { | ||
mangledName.utf8CString.withUnsafeBufferPointer { | ||
demangle($0, into: buffer) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// RUN: %target-run-simple-swift | ||
// REQUIRES: executable_test | ||
|
||
// We don't really want to excerise actual demangling here, but rather just that | ||
// the stdlib demangle function actually works as intended. | ||
|
||
import Swift | ||
import StdlibUnittest | ||
|
||
var DemangleTests = TestSuite("Demangle") | ||
|
||
DemangleTests.test("basic string return API") { | ||
// First, test that we get back the mangled name with invalid input. | ||
expectEqual(demangle("abc123"), nil) | ||
expectEqual(demangle("Si"), nil) | ||
expectEqual(demangle("Swift is super cool!"), nil) | ||
|
||
// Test that correct symbols are demangled. (Test all documented prefixes) | ||
expectEqual(demangle("_TSb"), "Swift.Bool") | ||
expectEqual(demangle("_T0Si"), "Swift.Int") | ||
expectEqual(demangle("$SSdXSaXSq"), "[Swift.Double]?") | ||
expectEqual(demangle("_$S8Demangle4main4argc4argvs5Int32VAF_SpySpys4Int8VGSgGtF"), "Demangle.main(argc: Swift.Int32, argv: Swift.UnsafeMutablePointer<Swift.Optional<Swift.UnsafeMutablePointer<Swift.Int8>>>) -> Swift.Int32") | ||
expectEqual(demangle("$sSG"), "Swift.RandomNumberGenerator") | ||
expectEqual(demangle("_$sSS7cStringSSSPys4Int8VG_tcfC"), "Swift.String.init(cString: Swift.UnsafePointer<Swift.Int8>) -> Swift.String") | ||
} | ||
|
||
DemangleTests.test("buffer API") { | ||
let buffer = UnsafeMutableBufferPointer<Int8>.allocate(capacity: 140) | ||
|
||
defer { buffer.deallocate() } | ||
|
||
buffer[0] = 0 // Ensure that when we do String(cString: ptr) it halts at first byte. | ||
let ptr = buffer.baseAddress! | ||
|
||
// First, test that the buffer is still empty after failed demanglings. | ||
expectEqual(demangle("abc123", into: buffer), .invalidSymbol) | ||
expectEqual(String(cString: ptr), "") | ||
|
||
expectEqual(demangle("Si", into: buffer), .invalidSymbol) | ||
expectEqual(String(cString: ptr), "") | ||
|
||
expectEqual(demangle("Swift is super cool!", into: buffer), .invalidSymbol) | ||
expectEqual(String(cString: ptr), "") | ||
|
||
// Test that correct symbols are demangled. (Test all documented prefixes) | ||
expectEqual(demangle("_TSb", into: buffer), .success) | ||
expectEqual(String(cString: ptr), "Swift.Bool") | ||
|
||
expectEqual(demangle("_T0Si", into: buffer), .success) | ||
expectEqual(String(cString: ptr), "Swift.Int") | ||
|
||
expectEqual(demangle("$SSdXSaXSq", into: buffer), .success) | ||
expectEqual(String(cString: ptr), "[Swift.Double]?") | ||
|
||
expectEqual(demangle("_$S8Demangle4main4argc4argvs5Int32VAF_SpySpys4Int8VGSgGtF", into: buffer), .success) | ||
expectEqual(String(cString: ptr), "Demangle.main(argc: Swift.Int32, argv: Swift.UnsafeMutablePointer<Swift.Optional<Swift.UnsafeMutablePointer<Swift.Int8>>>) -> Swift.Int32") | ||
|
||
expectEqual(demangle("$sSG", into: buffer), .success) | ||
expectEqual(String(cString: ptr), "Swift.RandomNumberGenerator") | ||
|
||
expectEqual(demangle("_$sSS7cStringSSSPys4Int8VG_tcfC", into: buffer), .success) | ||
expectEqual(String(cString: ptr), "Swift.String.init(cString: Swift.UnsafePointer<Swift.Int8>) -> Swift.String") | ||
|
||
// Test the return of demangle into with a smaller buffer. | ||
// Swift.Int requires 10 bytes, give this 9 | ||
let smolBuffer = UnsafeMutableBufferPointer<Int8>.allocate(capacity: 9) | ||
|
||
defer { smolBuffer.deallocate() } | ||
|
||
let smolPtr = smolBuffer.baseAddress! | ||
|
||
let fail = demangle("$sSi", into: smolBuffer) | ||
expectEqual(fail, .truncated(10)) | ||
expectEqual(String(cString: smolPtr), "Swift.In") | ||
|
||
// Test nil return on successful demangle. | ||
let success = demangle("$s4Smol3IntV", into: smolBuffer) | ||
expectEqual(success, .success) | ||
expectEqual(String(cString: smolPtr), "Smol.Int") | ||
} | ||
|
||
runAllTests() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
mangledNameBuffer
is always null-terminated then you could change it tomangledName: UnsafePointer<CChar>
, so thatString
arguments can be implicitly converted.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have the String argument version, so I'm unsure why supporting a pointer version for implicit conversion is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm suggesting that:
either
mangledNameBuffer: UnsafeBufferPointer<Int8>
doesn't need to be null-terminated;or
mangledName: UnsafePointer<Int8>
would make the thirddemangle
function unnecessary.On the other hand, John McCall wants to deprecate the implicit conversions.
By the way, are
Int8
orUInt8
elements more suitable for UTF-8 string parameters?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we use your suggestion about using
StringRef(mangledName, mangledNameLength))
mangledNameBuffer doesn't need to be null terminated. However, we must commit to either the name buffer being null terminated (because we calculate length with nameBuffer.count - 1), or must not be null terminated (nameBuffer.count).As for
Int8
orUInt8
,CChar
maps toInt8
and the string functionswithCString
andutf8CString.withUnsafeBufferPointer
, etc all haveInt8
element types.withUTF8
on the other hand usesUInt8
, so I'm unsure. I think going withInt8
is the better call because of theCChar
typealias.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CChar
is either signed or unsigned, depending on the platform; it reflects the signedness of the Cchar
type.