Commit f744717d authored by Alessandro Arzilli's avatar Alessandro Arzilli Committed by Ian Lance Taylor

debug/gosym: parse remote packages correctly

Fixes #15675

Change-Id: I8bad220988e5d690f20804db970b2db037c81187
Reviewed-on: https://go-review.googlesource.com/23086Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 7b597f4d
......@@ -40,8 +40,13 @@ func (s *Sym) Static() bool { return s.Type >= 'a' }
// PackageName returns the package part of the symbol name,
// or the empty string if there is none.
func (s *Sym) PackageName() string {
if i := strings.Index(s.Name, "."); i != -1 {
return s.Name[0:i]
pathend := strings.LastIndex(s.Name, "/")
if pathend < 0 {
pathend = 0
}
if i := strings.Index(s.Name[pathend:], "."); i != -1 {
return s.Name[:pathend+i]
}
return ""
}
......@@ -49,12 +54,16 @@ func (s *Sym) PackageName() string {
// ReceiverName returns the receiver type name of this symbol,
// or the empty string if there is none.
func (s *Sym) ReceiverName() string {
l := strings.Index(s.Name, ".")
r := strings.LastIndex(s.Name, ".")
pathend := strings.LastIndex(s.Name, "/")
if pathend < 0 {
pathend = 0
}
l := strings.Index(s.Name[pathend:], ".")
r := strings.LastIndex(s.Name[pathend:], ".")
if l == -1 || r == -1 || l == r {
return ""
}
return s.Name[l+1 : r]
return s.Name[pathend+l+1 : pathend+r]
}
// BaseName returns the symbol name without the package or receiver name.
......
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gosym
import (
"fmt"
"testing"
)
func assertString(t *testing.T, dsc, out, tgt string) {
if out != tgt {
t.Fatalf("Expected: %q Actual: %q for %s", tgt, out, dsc)
}
}
func TestStandardLibPackage(t *testing.T) {
s1 := Sym{Name: "io.(*LimitedReader).Read"}
s2 := Sym{Name: "io.NewSectionReader"}
assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "io")
assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "io")
assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "(*LimitedReader)")
assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "")
}
func TestStandardLibPathPackage(t *testing.T) {
s1 := Sym{Name: "debug/gosym.(*LineTable).PCToLine"}
s2 := Sym{Name: "debug/gosym.NewTable"}
assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "debug/gosym")
assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "debug/gosym")
assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "(*LineTable)")
assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "")
}
func TestRemotePackage(t *testing.T) {
s1 := Sym{Name: "github.com/docker/doc.ker/pkg/mflag.(*FlagSet).PrintDefaults"}
s2 := Sym{Name: "github.com/docker/doc.ker/pkg/mflag.PrintDefaults"}
assertString(t, fmt.Sprintf("package of %q", s1.Name), s1.PackageName(), "github.com/docker/doc.ker/pkg/mflag")
assertString(t, fmt.Sprintf("package of %q", s2.Name), s2.PackageName(), "github.com/docker/doc.ker/pkg/mflag")
assertString(t, fmt.Sprintf("receiver of %q", s1.Name), s1.ReceiverName(), "(*FlagSet)")
assertString(t, fmt.Sprintf("receiver of %q", s2.Name), s2.ReceiverName(), "")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment