Skip to content

Commit

Permalink
resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
worlpaker committed Jul 9, 2024
2 parents 4fcfc23 + 71c5537 commit 049414b
Show file tree
Hide file tree
Showing 890 changed files with 52,097 additions and 31,917 deletions.
4 changes: 2 additions & 2 deletions benchmark/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func TestParseLine(t *testing.T) {
// error handling cases
{
line: "BenchPress 100 19.6 ns/op", // non-benchmark
err: true,
err: true,
},
{
line: "BenchmarkEncrypt lots 19.6 ns/op", // non-int iterations
err: true,
err: true,
},
{
line: "BenchmarkBridge 100000000 19.6 smoots", // unknown unit
Expand Down
3 changes: 1 addition & 2 deletions cmd/bisect/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"testing"

"golang.org/x/tools/internal/bisect"
"golang.org/x/tools/internal/compat"
"golang.org/x/tools/internal/diffp"
"golang.org/x/tools/txtar"
)
Expand Down Expand Up @@ -82,7 +81,7 @@ func Test(t *testing.T) {
have[color] = true
}
if m.ShouldReport(uint64(i)) {
out = compat.Appendf(out, "%s %s\n", color, bisect.Marker(uint64(i)))
out = fmt.Appendf(out, "%s %s\n", color, bisect.Marker(uint64(i)))
}
}
err = nil
Expand Down
24 changes: 20 additions & 4 deletions cmd/callgraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,20 @@ Flags:
Caller and Callee are *ssa.Function values, which print as
"(*sync/atomic.Mutex).Lock", but other attributes may be
derived from them, e.g. Caller.Pkg.Pkg.Path yields the
import path of the enclosing package. Consult the go/ssa
API documentation for details.
derived from them. For example:
- {{.Caller.Pkg.Pkg.Path}} yields the import path of the
enclosing package; and
- {{(.Caller.Prog.Fset.Position .Caller.Pos).Filename}}
yields the name of the file that declares the caller.
- The 'posn' template function returns the token.Position
of an ssa.Function, so the previous example can be
reduced to {{(posn .Caller).Filename}}.
Consult the documentation for go/token, text/template, and
golang.org/x/tools/go/ssa for more detail.
Examples:
Expand Down Expand Up @@ -238,7 +249,12 @@ func doCallgraph(dir, gopath, algo, format string, tests bool, args []string) er
format = ` {{printf "%q" .Caller}} -> {{printf "%q" .Callee}}`
}

tmpl, err := template.New("-format").Parse(format)
funcMap := template.FuncMap{
"posn": func(f *ssa.Function) token.Position {
return f.Prog.Fset.Position(f.Pos())
},
}
tmpl, err := template.New("-format").Funcs(funcMap).Parse(format)
if err != nil {
return fmt.Errorf("invalid -format template: %v", err)
}
Expand Down
78 changes: 34 additions & 44 deletions cmd/deadcode/deadcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import (
"strings"
"text/template"

"golang.org/x/telemetry/counter"
"golang.org/x/telemetry/crashmonitor"
"golang.org/x/telemetry"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/callgraph/rta"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/internal/typesinternal"
)

//go:embed doc.go
Expand Down Expand Up @@ -64,8 +64,7 @@ Flags:
}

func main() {
counter.Open() // Enable telemetry counter writing.
crashmonitor.Start() // Enable crash reporting watchdog.
telemetry.Start(telemetry.Config{ReportCrashes: true})

log.SetPrefix("deadcode: ")
log.SetFlags(0) // no time prefix
Expand Down Expand Up @@ -131,16 +130,6 @@ func main() {
log.Fatalf("packages contain errors")
}

// Gather names of generated files.
generated := make(map[string]bool)
packages.Visit(initial, nil, func(p *packages.Package) {
for _, file := range p.Syntax {
if isGenerated(file) {
generated[p.Fset.File(file.Pos()).Name()] = true
}
}
})

// If -filter is unset, use first module (if available).
if *filterFlag == "<module>" {
if mod := initial[0].Module; mod != nil && mod.Path != "" {
Expand Down Expand Up @@ -168,6 +157,32 @@ func main() {
roots = append(roots, main.Func("init"), main.Func("main"))
}

// Gather all source-level functions,
// as the user interface is expressed in terms of them.
//
// We ignore synthetic wrappers, and nested functions. Literal
// functions passed as arguments to other functions are of
// course address-taken and there exists a dynamic call of
// that signature, so when they are unreachable, it is
// invariably because the parent is unreachable.
var sourceFuncs []*ssa.Function
generated := make(map[string]bool)
packages.Visit(initial, nil, func(p *packages.Package) {
for _, file := range p.Syntax {
for _, decl := range file.Decls {
if decl, ok := decl.(*ast.FuncDecl); ok {
obj := p.TypesInfo.Defs[decl.Name].(*types.Func)
fn := prog.FuncValue(obj)
sourceFuncs = append(sourceFuncs, fn)
}
}

if isGenerated(file) {
generated[p.Fset.File(file.Pos()).Name()] = true
}
}
})

// Compute the reachabilty from main.
// (Build a call graph only for -whylive.)
res := rta.Analyze(roots, *whyLiveFlag != "")
Expand All @@ -194,10 +209,7 @@ func main() {
// is not dead, by showing a path to it from some root.
if *whyLiveFlag != "" {
targets := make(map[*ssa.Function]bool)
for fn := range ssautil.AllFunctions(prog) {
if fn.Synthetic != "" || fn.Object() == nil {
continue // not a source-level named function
}
for _, fn := range sourceFuncs {
if prettyName(fn, true) == *whyLiveFlag {
targets[fn] = true
}
Expand Down Expand Up @@ -248,7 +260,7 @@ func main() {
edges = append(edges, jsonEdge{
Initial: cond(len(edges) == 0, prettyName(edge.Caller.Func, true), ""),
Kind: cond(isStaticCall(edge), "static", "dynamic"),
Position: toJSONPosition(prog.Fset.Position(edge.Site.Pos())),
Position: toJSONPosition(prog.Fset.Position(edge.Pos())),
Callee: prettyName(edge.Callee.Func, true),
})
}
Expand All @@ -262,26 +274,7 @@ func main() {

// Group unreachable functions by package path.
byPkgPath := make(map[string]map[*ssa.Function]bool)
for fn := range ssautil.AllFunctions(prog) {
if fn.Synthetic != "" {
continue // ignore synthetic wrappers etc
}

// Use generic, as instantiations may not have a Pkg.
if orig := fn.Origin(); orig != nil {
fn = orig
}

// Ignore unreachable nested functions.
// Literal functions passed as arguments to other
// functions are of course address-taken and there
// exists a dynamic call of that signature, so when
// they are unreachable, it is invariably because the
// parent is unreachable.
if fn.Parent() != nil {
continue
}

for _, fn := range sourceFuncs {
posn := prog.Fset.Position(fn.Pos())

if !reachablePosn[posn] {
Expand Down Expand Up @@ -384,11 +377,8 @@ func prettyName(fn *ssa.Function, qualified bool) string {

// method receiver?
if recv := fn.Signature.Recv(); recv != nil {
t := recv.Type()
if ptr, ok := t.(*types.Pointer); ok {
t = ptr.Elem()
}
buf.WriteString(t.(*types.Named).Obj().Name())
_, named := typesinternal.ReceiverNamed(recv)
buf.WriteString(named.Obj().Name())
buf.WriteByte('.')
}

Expand Down
44 changes: 44 additions & 0 deletions cmd/deadcode/testdata/issue65915.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Regression test for issue 65915: the enumeration of source-level
# functions used the flawed ssautil.AllFunctions, causing it to
# miss some unexported ones.

deadcode -filter= example.com

want "unreachable func: example.UnUsed"
want "unreachable func: example.unUsed"
want "unreachable func: PublicExample.UnUsed"
want "unreachable func: PublicExample.unUsed"

-- go.mod --
module example.com
go 1.18

-- main.go --
package main

type example struct{}

func (e example) UnUsed() {}

func (e example) Used() {}

func (e example) unUsed() {}

func (e example) used() {}

type PublicExample struct{}

func (p PublicExample) UnUsed() {}

func (p PublicExample) Used() {}

func (p PublicExample) unUsed() {}

func (p PublicExample) used() {}

func main() {
example{}.Used()
example{}.used()
PublicExample{}.Used()
PublicExample{}.used()
}
37 changes: 37 additions & 0 deletions cmd/deadcode/testdata/issue67915.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Test of -whylive with reflective call
# (regression test for golang/go#67915).

# The live function is reached via reflection:

deadcode example.com
want "unreachable func: dead"
!want "unreachable func: live"

# Reflective calls have Edge.Site=nil, which formerly led to a crash
# when -whylive would compute its position. Now it has NoPos.

deadcode -whylive=example.com.live example.com
want " example.com.main"
want " static@L0006 --> reflect.Value.Call"
want "dynamic@L0000 --> example.com.live"

-- go.mod --
module example.com
go 1.18

-- main.go --
package main

import "reflect"

func main() {
reflect.ValueOf(live).Call(nil)
}

func live() {
println("hello")
}

func dead() {
println("goodbye")
}
5 changes: 0 additions & 5 deletions cmd/getgo/.dockerignore

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/getgo/.gitignore

This file was deleted.

20 changes: 0 additions & 20 deletions cmd/getgo/Dockerfile

This file was deleted.

27 changes: 0 additions & 27 deletions cmd/getgo/LICENSE

This file was deleted.

Loading

0 comments on commit 049414b

Please sign in to comment.