diff options
| author | colincross <github@colincross.com> | 2019-02-28 19:52:33 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-02-28 19:52:33 -0800 |
| commit | bf8c1d31d9cb9ade9efa50ccbe6ae5bb7c1eb343 (patch) | |
| tree | e7daffaa826c2ee1bb39ca7d01f6da83765115e5 | |
| parent | 284c742a3322e1f0a02df7aefb7a3be071bbcff2 (diff) | |
| parent | 16b01609cee56ee32bca9935665a77f56aae4bf5 (diff) | |
| download | android_build_blueprint-bf8c1d31d9cb9ade9efa50ccbe6ae5bb7c1eb343.tar.gz android_build_blueprint-bf8c1d31d9cb9ade9efa50ccbe6ae5bb7c1eb343.tar.bz2 android_build_blueprint-bf8c1d31d9cb9ade9efa50ccbe6ae5bb7c1eb343.zip | |
Merge pull request #239 from colincross/go1.12
Support go 1.12
| -rw-r--r-- | .travis.yml | 2 | ||||
| -rw-r--r-- | package_ctx.go | 30 |
2 files changed, 14 insertions, 18 deletions
diff --git a/.travis.yml b/.travis.yml index 3397bfd..6abd686 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ language: go go: - 1.9 - "1.10" + - "1.11" + - "1.12" cache: directories: diff --git a/package_ctx.go b/package_ctx.go index 0c64e45..e0a03c8 100644 --- a/package_ctx.go +++ b/package_ctx.go @@ -18,6 +18,7 @@ import ( "errors" "fmt" "reflect" + "regexp" "runtime" "strings" "sync" @@ -137,12 +138,17 @@ func checkCalledFromInit() { panic("not called from an init func") } - if funcName == "init" || strings.HasPrefix(funcName, "init·") { + if funcName == "init" || strings.HasPrefix(funcName, "init·") || + funcName == "init.ializers" || strings.HasPrefix(funcName, "init.") { return } } } +// A regex to find a package path within a function name. It finds the shortest string that is +// followed by '.' and doesn't have any '/'s left. +var pkgPathRe = regexp.MustCompile(`^(.*?)\.([^/]+)$`) + // callerName returns the package path and function name of the calling // function. The skip argument has the same meaning as the skip argument of // runtime.Callers. @@ -153,25 +159,13 @@ func callerName(skip int) (pkgPath, funcName string, ok bool) { return "", "", false } - f := runtime.FuncForPC(pc[0]) - fullName := f.Name() - - lastDotIndex := strings.LastIndex(fullName, ".") - if lastDotIndex == -1 { - panic("unable to distinguish function name from package") - } - - if fullName[lastDotIndex-1] == ')' { - // The caller is a method on some type, so it's name looks like - // "pkg/path.(type).method". We need to go back one dot farther to get - // to the package name. - lastDotIndex = strings.LastIndex(fullName[:lastDotIndex], ".") + f := runtime.FuncForPC(pc[0]).Name() + s := pkgPathRe.FindStringSubmatch(f) + if len(s) < 3 { + panic(fmt.Errorf("failed to extract package path and function name from %q", f)) } - pkgPath = fullName[:lastDotIndex] - funcName = fullName[lastDotIndex+1:] - ok = true - return + return s[1], s[2], true } // pkgPathToName makes a Ninja-friendly name out of a Go package name by |
