Commit Graph

72 Commits

Author SHA1 Message Date
Erik Eckstein
1f36fa5c0b swift-demangle: don't require the $-prefix for embedded symbols
Support demangling `swift-demangle e4main8MyStructV3fooyyFAA1XV_Tg5` (note the missing $-prefix)
2025-01-28 17:35:53 +01:00
Kuba Mracek
e527e8f307 [embedded] Teach swift-demangle about prefix, add test 2024-12-02 15:01:24 -08:00
Kuba Mracek
9c77074cac [Mangling] Establish a new mangling prefix for Embedded Swift: $e 2024-12-02 15:01:24 -08:00
Erik Eckstein
b9cb5939cf swift-demangle: add an option -type to demangle runtime type strings
The motivation for this is to test the `Demangler::demangleType` API.
2024-07-05 11:37:15 +02:00
Dave Lee
c3488c60e1 Demangler: Add option to omit closure signatures (#73331)
Add a new demangler option which excludes a closure's type signature.

This will be used in lldb.

Closures are not subject to overloading, and so the signature will never be used to 
disambiguate. A demangled closure is uniquely identifiable by its index(s) and parent.

Where opaque types are involved, the concrete type signature can be quite complex. This 
demangling option allows callers to avoid printing the underlying complex nested 
concrete types.

Example:

before: `closure #1 (Swift.Int) -> () in closure #1 (Swift.Int) -> () in main`
after: `closure #1 in closure #1 in main`
2024-04-30 12:48:02 -07:00
Ben Barham
9779c18da3 Rename startswith to starts_with
LLVM is presumably moving towards `std::string_view` -
`StringRef::startswith` is deprecated on tip. `SmallString::startswith`
was just renamed there (maybe with some small deprecation inbetween, but
if so, we've missed it).

The `SmallString::startswith` references were moved to
`.str().starts_with()`, rather than adding the `starts_with` on
`stable/20230725` as we only had a few of them. Open to switching that
over if anyone feels strongly though.
2024-03-13 22:25:47 -07:00
Mike Ash
d797473ea2 [swift-demangle] Warn when passed an empty argument.
We already have a warning when a single _ is passed, which catches the case where someone passes _$someMangledName and the shell expands it as a variable. But sometimes people do this without the leading underscore. Detect and warn about that case too.
2023-07-13 10:09:46 -04:00
Alastair Houghton
5e282eef91 [Tools] Fix macro demangling.
The code in swift-demangle to cope with macro demangling was slightly
wrong.  Fix it.

rdar://109649226
2023-05-22 11:06:55 +01:00
Alex Langford
7f33efac58 [Demangling][NFC] Actually use first argument of demangle 2023-04-07 21:32:03 -07:00
Doug Gregor
f8d78e2eb9 Handle demangling prefix @__swiftmacro_ used for filenames. 2023-02-01 09:20:46 -08:00
Dave Lee
a6941723c8 [tools] Show helpful message when demangling '_' (#61369)
From most shells, the symbol(s) given to `swift demangle` should be quoted or escaped, otherwise the shell will perform variable expansion on the `$...` suffix of the symbol. For example:

```
% swift demangle _$sScMMa
_ ---> _
```

Instead the command should be edited to one of the following:
```
% swift demangle sScMMa
_$sScMMa ---> type metadata accessor for Swift.MainActor
% swift demangle '_$sScMMa'
_$sScMMa ---> type metadata accessor for Swift.MainActor
% swift demangle _\$sScMMa
_$sScMMa ---> type metadata accessor for Swift.MainActor
```

When variable expansion has happened, `swift demangle` will see the symbol only as "`_`". This change adds a warning that identifies the likely issue, and contains suggestions on how the user can fix and rerun the invocation.
2022-09-30 08:57:18 -07:00
Alastair Houghton
9013083046 [Demangler] Display error codes in various places.
Also fixed a test that broke with the previous commit.

rdar://79725187
2021-09-06 17:49:55 +01:00
Alastair Houghton
cc869b90b1 [Demangling] Remangling error handling for the OldRemangler.
First pass at adding error handling to the OldRemangler.  Still pondering
assert() calls.

rdar://79725187
2021-09-06 17:49:09 +01:00
Alastair Houghton
145f2814a2 [Demangling][Tests] Fix tests after remangler initial error handling.
Fix the tests to work after the Remangler has been fixed to do error handling.

rdar://79725187
2021-09-06 17:49:09 +01:00
Alastair Houghton
3f01f853a6 [Demangling] Add error handling to the remangler.
Mangling can fail, usually because the Node structure has been built
incorrectly or because something isn't supported with the old remangler.
We shouldn't just terminate the program when that happens, particularly
if it happens because someone has passed bad data to the demangler.

rdar://79725187
2021-09-06 17:49:09 +01:00
Alastair Houghton
60772eb988 [Demangler] Make swift-demangle faster by not using regex.
In my (admittedly silly) test case involving parsing a 48MB mangling,
swift-demangle spends 78% of its time executing the regex to find things it
might be able to unmangle, and only 21% of its time actually unmangling things.
This seemed wrong, so I removed the regex and wrote code to match mangled
strings instead.  It seems quite a bit faster overall.

rdar://80019851
2021-07-01 13:51:33 +01:00
Arnold Schwaighofer
b994bf3191 Add support for _specialize(exported: true, ...)
This attribute allows to define a pre-specialized entry point of a
generic function in a library.

The following definition provides a pre-specialized entry point for
`genericFunc(_:)` for the parameter type `Int` that clients of the
library can call.

```
@_specialize(exported: true, where T == Int)
public func genericFunc<T>(_ t: T) { ... }
```

Pre-specializations of internal `@inlinable` functions are allowed.

```
@usableFromInline
internal struct GenericThing<T> {
  @_specialize(exported: true, where T == Int)
  @inlinable
  internal func genericMethod(_ t: T) {
  }
}
```

There is syntax to pre-specialize a method from a different module.

```
import ModuleDefiningGenericFunc

@_specialize(exported: true, target: genericFunc(_:), where T == Double)
func prespecialize_genericFunc(_ t: T) { fatalError("dont call") }

```

Specially marked extensions allow for pre-specialization of internal
methods accross module boundries (respecting `@inlinable` and
`@usableFromInline`).

```
import ModuleDefiningGenericThing
public struct Something {}

@_specializeExtension
extension GenericThing {
  @_specialize(exported: true, target: genericMethod(_:), where T == Something)
  func prespecialize_genericMethod(_ t: T) { fatalError("dont call") }
}
```

rdar://64993425
2020-10-12 09:19:29 -07:00
Adrian Prantl
f4a4e4c3e0 Add a demangler option to hide local decl name contexts.
This part of a series of patches to bring ASTPrinter and Swift Demangler to
feature parity, which is needed by LLDB, which depends on using the strings
produced by either interchangibly.

rdar://problem/64222171
2020-06-11 18:01:48 -07:00
Adrian Prantl
051becaf0d Add a demangler option to hide a current module.
This is analogous to ASTPrinter's FullyQualifiedTypesIfAmbiguous option.

This part of a series of patches to bring ASTPrinter and Swift Demangler to
feature parity, which is needed by LLDB, which depends on using the strings
produced by either interchangibly.

rdar://problem/63700540
2020-05-29 17:20:42 -07:00
Adrian Prantl
fe93b19842 Add a demangler option to hide the "__C" module name.
This part of a series of patches to bring ASTPrinter and Swift Demangler to
feature parity, which is needed by LLDB, which depends on using the strings
produced by either interchangibly.

rdar://problem/63700540
2020-05-29 17:10:16 -07:00
Adrian Prantl
52e13af93d Add a demangler option to hide the "Swift" module name.
This part of a series of patches to bring ASTPrinter and Swift Demangler to
feature parity, which is needed by LLDB, which depends on using the strings
produced by either interchangibly.

<rdar://problem/63700540>
2020-05-29 16:58:34 -07:00
Fred Riss
259d78a350 Adapt to llvm.org StringRef API change 2020-03-13 19:08:22 +01:00
Erik Eckstein
a8a18c01ad swift-demangler: Add an option -strip-specialization to get the symbol name of the origin of a specialized function. 2019-03-25 14:57:03 -07:00
Erik Eckstein
8997a0e831 swift-demangle: Also accept $s symbols without the $ prefix.
This makes it easier to copy-paste $s symbols with a double-click.
We already had this for $S, now it also works for the new mangling prefix $s.
2018-10-04 12:53:58 -07:00
Rintaro Ishizaki
ad3e6b9471 [SourceKit] Make sourcekitd-test -req=demangle STDIN mode actually work
This didn't work at all because it didn't advance cursor after regex
matching.
Align regex with swift-demangle.
2018-04-11 20:08:52 +09:00
Erik Eckstein
99b925651e swift-demangle: Also accept symbol names without the $ prefix.
This makes it easier to copy-paste $S symbol names with a double-click.

rdar://problem/38546308
2018-03-24 19:28:38 -07:00
Erik Eckstein
f0ef4007b6 Demangler: support the final mangling prefix _$s
rdar://problem/37681432
2018-03-24 19:28:38 -07:00
Erik Eckstein
d21b4a35d5 swift-demangle: Add an option to test the ObjC runtime name mangling scheme.
This is basically the old mangling scheme. The option -remangle-objc-rt remangles a new mangled name to the old scheme.
This is useful to test the old remangler, which is used in the swift runtime to create the ObjC runtime names.
2018-01-29 16:25:38 -08:00
Doug Gregor
6d1e668c86 [Demangler] Eliminate more rogue ".c_str()" and ".data()". 2018-01-10 08:53:49 -08:00
Erik Eckstein
ecd177fa17 Demangler: handle suffixes in the form '.<n>'.
Demangle such suffixes as "unmangled suffix"
IRGen still uses '.<n>' to disambiguate partial apply thunks and outlined copy functions.

rdar://problem/32934962
2017-06-26 15:19:20 -07:00
Erik Eckstein
79c522a49d demangler: Support the future final mangling prefix $S
As it’s not clear if we will need underscores, the demangler accepts $S and _$S.
Note that a double underscore is handled by the demangler client.

rdar://problem/32251811
2017-06-26 11:32:26 -07:00
Erik Eckstein
a408060754 Remove the old re-mangler.
NFC except the swift-demangle does not check for correct old-style re-mangling anymore.
2017-03-17 16:10:36 -07:00
Erik Eckstein
5e80555c9b demangler: put the demangler into a separate library
Previously it was part of swiftBasic.

The demangler library does not depend on llvm (except some header-only utilities like StringRef). Putting it into its own library makes sure that no llvm stuff will be linked into clients which use the demangler library.

This change also contains other refactoring, like moving demangler code into different files. This makes it easier to remove the old demangler from the runtime library when we switch to the new symbol mangling.

Also in this commit: remove some unused API functions from the demangler Context.

fixes rdar://problem/30503344
2017-03-09 13:42:43 -08:00
Erik Eckstein
a41312288d demangler: add an API function to get the target of a thunk symbol.
rdar://problem/30820093
2017-03-02 17:21:45 -08:00
Erik Eckstein
f8f172a46b demangler: also support the future mangling prefix ‘_S’ 2017-03-01 14:16:38 -08:00
Erik Eckstein
be986d753c demangler: add an API to check if a function has the swiftcc calling convention 2017-03-01 14:16:38 -08:00
Erik Eckstein
7d7dc5aaac Demangler: Use a bump-pointer allocator for node allocation.
This makes the demangler about 10 times faster.
It also changes the lifetimes of nodes. Previously nodes were reference-counted.
Now the returned demangle  node-tree is owned by the Demangler class and it’s lifetime ends with the lifetime of the Demangler.

Therefore the old (and already deprecated) global functions demangleSymbolAsNode and demangleTypeAsNode are no longer available.

Another change is that the demangling for reflection now only supports the new mangling (which should be no problem because
we are generating only new mangled names for reflection).
2017-02-24 19:04:13 -08:00
Erik Eckstein
2d127e4192 Reinstate ”Use the new mangling for reflection."
It also uses the new mangling for type names in meta-data (except for top-level non-generic classes).
lldb has now support for new mangled metadata type names.

This reinstates commit 21ba292943.
2017-02-15 09:47:22 -08:00
Erik Eckstein
ebe19757e1 Demangler: add a new API for the demangler which enables bump-pointer allocation of demangling nodes.
Instead of a global demangleSymbolAsNode, which returns a reference-counted NodePointer, there is now a Context class which owns the nodes.
So now demangleSymbolAsNode is a member of Context and the returned NodePointer is alive as long as the Context is alive.

This is still a NFC: the new ABI still maps to the old functions.
The purpose of this change is to let lldb adapt to the new API and then we can switch to the new implementation.
2017-02-10 12:25:58 -08:00
Erik Eckstein
479237b9ac Mangling: isSwiftSymbol should get a null terminated string rather than a char pointer + length
This avoids doing a strlen in lldb when calling this function.

rdar://problem/30062026
2017-02-09 14:05:00 -08:00
Erik Eckstein
254f36aba5 Revert "Use the new mangling for reflection."
This needs some changes in lldb.
Disabled for now until lldb supports the new mangling.

This reverts commit 21ba292943.
2017-02-08 09:01:51 -08:00
Erik Eckstein
21ba292943 Use the new mangling for reflection.
For this we are linking the new re-mangler instead of the old one into the swift runtime library.
Also we are linking the new de-mangling into the swift runtime library.

It also switches to the new mangling for class names of generic swift classes in the metadata.
Note that for non-generic class we still have to use the old mangling, because the ObjC runtime in the OS depends on it (it de-mangles the class names).
But names of generic classes are not handled by the ObjC runtime anyway, so there should be no problem to change the mangling for those.
The reason for this change is that it avoids linking the old re-mangler into the runtime library.
2017-02-07 08:36:21 -08:00
Erik Eckstein
81384b6f82 demangler: Add API functions for classifying symbols.
To be used by lldb.
Also add a -classify option in swift-demangler to test those new API functions.
2017-01-31 17:27:10 -08:00
Erik Eckstein
e266466609 swift-demangle: add -remangle-new option
It can be used to convert an old mangling to the new mangling.
2017-01-20 09:50:58 -08:00
Hugh Bellamy
31af706d14 Port swift-demangle to Windows 2017-01-19 16:23:37 +00:00
Hugh Bellamy
e32985d406 Restrict <cstdlib> import to CYGWIN 2017-01-19 09:34:45 +00:00
swift-ci
294359bd69 Merge pull request #4804 from kstaring/master 2017-01-10 00:01:46 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Erik Eckstein
ea7f27e4cb swift-demangle: don’t hardcode the mangling prefix when processing the -test-remangle option 2016-12-14 16:39:00 -08:00
practicalswift
76f0fdd670 [gardening] NULL → nullptr 2016-12-09 23:17:54 +01:00