Skip to content
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

Stop querying for stack frames multiple times on CallerInfo() #7

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Changes from 2 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
24 changes: 16 additions & 8 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,23 @@ the problem actually occurred in calling code.*/
func CallerInfo() []string {

var pc uintptr
var ok bool
var file string
var line int
var name string

callers := []string{}
for i := 0; ; i++ {
pc, file, line, ok = runtime.Caller(i)
if !ok {
// The breaks below failed to terminate the loop, and we ran off the
// end of the call stack.
break
}
pcs := make([]uintptr, 50)
n := runtime.Callers(1, pcs)
if n == 0 {
return []string{}
}
frames := runtime.CallersFrames(pcs[:n])

for {
frame, more := frames.Next()
pc = frame.PC
file = frame.File
line = frame.Line

// This is a huge edge case, but it will panic if this is the case, see #180
if file == "<autogenerated>" {
Expand Down Expand Up @@ -263,6 +267,10 @@ func CallerInfo() []string {
isTest(name, "Example") {
break
}

if !more {
break
}
}

return callers
Expand Down