internal/stack: helper package for querying stack frames

Intended for #5. Also replaces a helper function in the `printer`
package.
This commit is contained in:
Michael McLoughlin
2019-01-04 00:45:01 -08:00
parent 52b6e2c03b
commit 301d0c137a
3 changed files with 109 additions and 16 deletions

View File

@@ -4,10 +4,10 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/internal/stack"
)
type Printer interface {
@@ -67,21 +67,8 @@ func (c Config) GeneratedWarning() string {
// mainfile attempts to determine the file path of the main function by
// inspecting the stack. Returns empty string on failure.
func mainfile() string {
pc := make([]uintptr, 10)
n := runtime.Callers(0, pc)
if n == 0 {
return ""
}
pc = pc[:n]
frames := runtime.CallersFrames(pc)
for {
frame, more := frames.Next()
if frame.Function == "main.main" {
return frame.File
}
if !more {
break
}
if m := stack.Main(); m != nil {
return m.File
}
return ""
}