Skip to content

fix: Improve trimming of Swift function names #72335

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

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/sentry/stacktraces/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,40 @@ def process_generics(value, start):
#
# ["unsigned", "int", "whatever"] -> whatever
# ["@objc", "whatever", "->", "int"] -> whatever
try:
func_token = tokens[tokens.index("⟿") - 1]
except ValueError:
if tokens:
if function.find("initialization expression of") != -1:
# Swift initializer expression.
last_of_index = len(tokens) - 1 - tokens[::-1].index("of")
func_token = tokens[last_of_index + 1]
if func_token == "static":
# Static initializer expression, the object is the next token.
func_token = tokens[last_of_index + 2]
if func_token.startswith(":"):
# Trim the colon from the function name.
func_token = func_token[1:]
func_token = "initializer expression of " + func_token
elif tokens:
try:
last_arrow_index = len(tokens) - 1 - tokens[::-1].index("⟿")
func_token = tokens[last_arrow_index - 1]
if func_token == "throws":
# Throwing Swift function, the function name is the previous token.
func_token = tokens[last_arrow_index - 2]
except (ValueError, IndexError):
# No arrow, use the last token.
last_arrow_index = -1
func_token = tokens[-1]
else:
func_token = None
else:
func_token = None

if func_token:
if func_token.startswith("@") and platform in ("cocoa", "swift"):
# Found a Swift attribute instead of a function name, must be an
# anonymous function
func_token = ("thunk for " if is_thunk else "") + "closure"
elif "closure" in tokens and func_token != "closure":
# Found a closure.
func_token = "closure in " + func_token

function = (
func_token.replace("⟨", "<")
.replace("◯", "()")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ creator: sentry
source: tests/sentry/grouping/test_variants.py
---
app:
hash: "9b037f38798ea127aac3e8b8e1e1024a"
hash: "61c031e5e187212561f67b5567f8ae47"
component:
app*
exception*
Expand Down Expand Up @@ -122,7 +122,7 @@ app:
filename*
"mediaslideshow+extensions.swift"
function* (isolated function)
"MediaSlideshow.toSources"
"closure in MediaSlideshow.toSources"
frame*
filename*
"mediaslideshow+extensions.swift"
Expand All @@ -147,7 +147,7 @@ app:
"EXC_BREAKPOINT"
--------------------------------------------------------------------------
system:
hash: "fe8b15fe322c91ede2fc48363fe43587"
hash: "7c47e696e27052f4849cb07e99cf649e"
component:
system*
exception*
Expand Down Expand Up @@ -265,7 +265,7 @@ system:
filename*
"mediaslideshow+extensions.swift"
function* (isolated function)
"MediaSlideshow.toSources"
"closure in MediaSlideshow.toSources"
frame*
filename*
"mediaslideshow+extensions.swift"
Expand Down
31 changes: 27 additions & 4 deletions tests/sentry/stacktraces/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@
],
["pthread_cond_timedwait@@GLIBC_2.3.2", "pthread_cond_timedwait"],
["glob64@GLIBC_2.2", "glob64"],
[
"static Namespace.ThrowingFunction() throws -> Namespace.ExitValue?",
"Namespace.ThrowingFunction",
],
[
"closure #1 @Swift.MainActor () -> () in static Foo.CallFunction(args: [Swift.String]) -> ()",
"closure in Foo.CallFunction",
],
[
"closure #1 () -> () in Bar.PostTask(() -> ()) -> ()",
"closure in Bar.PostTask",
],
[
"closure #1 @Sendable () -> Swift.String in variable initialization expression of static Namespace.Class.var : Namespace.Parent",
"closure in initializer expression of Namespace.Class.var",
],
[
"variable initialization expression of static Namespace.Class.var : Namespace.Parent",
"initializer expression of Namespace.Class.var",
],
[
"closure #1 () -> () in variable initialization expression of static (extension in SpaceCreation):Namespace.Class.var : Namespace.Parent",
"closure in initializer expression of Namespace.Class.var",
],
],
)
def test_trim_native_function_name(input, output):
Expand Down Expand Up @@ -142,14 +166,13 @@ def test_trim_csharp_function_name(input, output):
"partial apply for thunk for @escaping @callee_guaranteed (@guaranteed SomeType, @guaranteed [String : SomeType2], @guaranteed SomeType3) -> ()",
"thunk for closure",
],
[ # "closure ... in ..." functions are converted to containing
# function. We might want to change this in the future
[
"closure #1 (T1) in foo(bar: T2)",
"foo",
"closure in foo",
],
[
"partial apply for closure #1 () in closure #2 (T1) in f1(_: T2, arg: T3)",
"f1",
"closure in f1",
],
],
)
Expand Down
Loading