Skip to content

Commit 76aace8

Browse files
committed
internal/analysisinternal: rationalize
This CL moves declarations as described below, and updates comments. No logic was changed except in merging the DocComment functions, and in using safetoken from TypeErrorEndPos within gopls. internal/astutil (pure syntax) + func Comments + func DocComment (merging various unexported copies) + func EnclosingFile + func Format + func IsChildOf internal/typesinternal (typed syntax) + func EnclosingScope + func Imports + func IsFunctionNamed + func IsMethodNamed + func IsPointerToNamed + func IsTypeNamed - func IsZeroExpr (moved to fillreturns) internal/analysisinternal (use of analysis framework) - func AddImport - func Comments - func DeleteDecl - func DeleteSpec - func DeleteStmt - func DeleteVar - func EnclosingFile - func EnclosingScope - func Format - func FreshName - func Imports - func IsChildOf - func IsFunctionNamed - func IsMethodNamed - func IsPointerToNamed - func IsTypeNamed - func TypeErrorEndPos (moved to gopls/internal/cache) internal/refactor: (computing text edits from typed syntax) + func AddImport + func DeleteDecl + func DeleteSpec + func DeleteStmt + func DeleteVar + func FreshName Change-Id: I9fad550ee55efbeb217627570e3a2e9abfee2178 Reviewed-on: https://go-review.googlesource.com/c/tools/+/710295 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 8cf2d63 commit 76aace8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1048
-962
lines changed

go/analysis/passes/assign/assign.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"golang.org/x/tools/go/analysis/passes/inspect"
2020
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
2121
"golang.org/x/tools/go/ast/inspector"
22-
"golang.org/x/tools/internal/analysisinternal"
22+
"golang.org/x/tools/internal/astutil"
2323
)
2424

2525
//go:embed doc.go
@@ -66,8 +66,8 @@ func run(pass *analysis.Pass) (any, error) {
6666
!isMapIndex(pass.TypesInfo, lhs) &&
6767
reflect.TypeOf(lhs) == reflect.TypeOf(rhs) { // short-circuit the heavy-weight gofmt check
6868

69-
le = analysisinternal.Format(pass.Fset, lhs)
70-
re := analysisinternal.Format(pass.Fset, rhs)
69+
le = astutil.Format(pass.Fset, lhs)
70+
re := astutil.Format(pass.Fset, rhs)
7171
if le == re {
7272
isSelfAssign = true
7373
}

go/analysis/passes/atomic/atomic.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
1515
"golang.org/x/tools/go/ast/inspector"
1616
"golang.org/x/tools/go/types/typeutil"
17-
"golang.org/x/tools/internal/analysisinternal"
17+
"golang.org/x/tools/internal/astutil"
18+
"golang.org/x/tools/internal/typesinternal"
1819
)
1920

2021
//go:embed doc.go
@@ -30,7 +31,7 @@ var Analyzer = &analysis.Analyzer{
3031
}
3132

3233
func run(pass *analysis.Pass) (any, error) {
33-
if !analysisinternal.Imports(pass.Pkg, "sync/atomic") {
34+
if !typesinternal.Imports(pass.Pkg, "sync/atomic") {
3435
return nil, nil // doesn't directly import sync/atomic
3536
}
3637

@@ -54,7 +55,7 @@ func run(pass *analysis.Pass) (any, error) {
5455
continue
5556
}
5657
obj := typeutil.Callee(pass.TypesInfo, call)
57-
if analysisinternal.IsFunctionNamed(obj, "sync/atomic", "AddInt32", "AddInt64", "AddUint32", "AddUint64", "AddUintptr") {
58+
if typesinternal.IsFunctionNamed(obj, "sync/atomic", "AddInt32", "AddInt64", "AddUint32", "AddUint64", "AddUintptr") {
5859
checkAtomicAddAssignment(pass, n.Lhs[i], call)
5960
}
6061
}
@@ -72,7 +73,7 @@ func checkAtomicAddAssignment(pass *analysis.Pass, left ast.Expr, call *ast.Call
7273
arg := call.Args[0]
7374
broken := false
7475

75-
gofmt := func(e ast.Expr) string { return analysisinternal.Format(pass.Fset, e) }
76+
gofmt := func(e ast.Expr) string { return astutil.Format(pass.Fset, e) }
7677

7778
if uarg, ok := arg.(*ast.UnaryExpr); ok && uarg.Op == token.AND {
7879
broken = gofmt(left) == gofmt(uarg.X)

go/analysis/passes/atomicalign/atomicalign.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"golang.org/x/tools/go/analysis/passes/inspect"
1919
"golang.org/x/tools/go/ast/inspector"
2020
"golang.org/x/tools/go/types/typeutil"
21-
"golang.org/x/tools/internal/analysisinternal"
21+
"golang.org/x/tools/internal/typesinternal"
2222
)
2323

2424
const Doc = "check for non-64-bits-aligned arguments to sync/atomic functions"
@@ -35,7 +35,7 @@ func run(pass *analysis.Pass) (any, error) {
3535
if 8*pass.TypesSizes.Sizeof(types.Typ[types.Uintptr]) == 64 {
3636
return nil, nil // 64-bit platform
3737
}
38-
if !analysisinternal.Imports(pass.Pkg, "sync/atomic") {
38+
if !typesinternal.Imports(pass.Pkg, "sync/atomic") {
3939
return nil, nil // doesn't directly import sync/atomic
4040
}
4141

@@ -54,7 +54,7 @@ func run(pass *analysis.Pass) (any, error) {
5454
inspect.Preorder(nodeFilter, func(node ast.Node) {
5555
call := node.(*ast.CallExpr)
5656
obj := typeutil.Callee(pass.TypesInfo, call)
57-
if analysisinternal.IsFunctionNamed(obj, "sync/atomic", funcNames...) {
57+
if typesinternal.IsFunctionNamed(obj, "sync/atomic", funcNames...) {
5858
// For all the listed functions, the expression to check is always the first function argument.
5959
check64BitAlignment(pass, obj.Name(), call.Args[0])
6060
}

go/analysis/passes/bools/bools.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"golang.org/x/tools/go/analysis/passes/inspect"
1616
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
1717
"golang.org/x/tools/go/ast/inspector"
18-
"golang.org/x/tools/internal/analysisinternal"
18+
"golang.org/x/tools/internal/astutil"
1919
)
2020

2121
const Doc = "check for common mistakes involving boolean operators"
@@ -104,7 +104,7 @@ func (op boolOp) commutativeSets(info *types.Info, e *ast.BinaryExpr, seen map[*
104104
func (op boolOp) checkRedundant(pass *analysis.Pass, exprs []ast.Expr) {
105105
seen := make(map[string]bool)
106106
for _, e := range exprs {
107-
efmt := analysisinternal.Format(pass.Fset, e)
107+
efmt := astutil.Format(pass.Fset, e)
108108
if seen[efmt] {
109109
pass.ReportRangef(e, "redundant %s: %s %s %s", op.name, efmt, op.tok, efmt)
110110
} else {
@@ -150,8 +150,8 @@ func (op boolOp) checkSuspect(pass *analysis.Pass, exprs []ast.Expr) {
150150
}
151151

152152
// e is of the form 'x != c' or 'x == c'.
153-
xfmt := analysisinternal.Format(pass.Fset, x)
154-
efmt := analysisinternal.Format(pass.Fset, e)
153+
xfmt := astutil.Format(pass.Fset, x)
154+
efmt := astutil.Format(pass.Fset, e)
155155
if prev, found := seen[xfmt]; found {
156156
// checkRedundant handles the case in which efmt == prev.
157157
if efmt != prev {

go/analysis/passes/cgocall/cgocall.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"strconv"
1919

2020
"golang.org/x/tools/go/analysis"
21-
"golang.org/x/tools/internal/analysisinternal"
21+
"golang.org/x/tools/internal/typesinternal"
2222
)
2323

2424
const debug = false
@@ -41,7 +41,7 @@ var Analyzer = &analysis.Analyzer{
4141
}
4242

4343
func run(pass *analysis.Pass) (any, error) {
44-
if !analysisinternal.Imports(pass.Pkg, "runtime/cgo") {
44+
if !typesinternal.Imports(pass.Pkg, "runtime/cgo") {
4545
return nil, nil // doesn't use cgo
4646
}
4747

go/analysis/passes/copylock/copylock.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import (
1616
"golang.org/x/tools/go/analysis"
1717
"golang.org/x/tools/go/analysis/passes/inspect"
1818
"golang.org/x/tools/go/ast/inspector"
19-
"golang.org/x/tools/internal/analysisinternal"
19+
"golang.org/x/tools/internal/astutil"
2020
"golang.org/x/tools/internal/typeparams"
21+
"golang.org/x/tools/internal/typesinternal"
2122
"golang.org/x/tools/internal/versions"
2223
)
2324

@@ -86,7 +87,7 @@ func checkCopyLocksAssign(pass *analysis.Pass, assign *ast.AssignStmt, goversion
8687
lhs := assign.Lhs
8788
for i, x := range assign.Rhs {
8889
if path := lockPathRhs(pass, x); path != nil {
89-
pass.ReportRangef(x, "assignment copies lock value to %v: %v", analysisinternal.Format(pass.Fset, assign.Lhs[i]), path)
90+
pass.ReportRangef(x, "assignment copies lock value to %v: %v", astutil.Format(pass.Fset, assign.Lhs[i]), path)
9091
lhs = nil // An lhs has been reported. We prefer the assignment warning and do not report twice.
9192
}
9293
}
@@ -100,7 +101,7 @@ func checkCopyLocksAssign(pass *analysis.Pass, assign *ast.AssignStmt, goversion
100101
if id, ok := l.(*ast.Ident); ok && id.Name != "_" {
101102
if obj := pass.TypesInfo.Defs[id]; obj != nil && obj.Type() != nil {
102103
if path := lockPath(pass.Pkg, obj.Type(), nil); path != nil {
103-
pass.ReportRangef(l, "for loop iteration copies lock value to %v: %v", analysisinternal.Format(pass.Fset, l), path)
104+
pass.ReportRangef(l, "for loop iteration copies lock value to %v: %v", astutil.Format(pass.Fset, l), path)
104105
}
105106
}
106107
}
@@ -132,7 +133,7 @@ func checkCopyLocksCompositeLit(pass *analysis.Pass, cl *ast.CompositeLit) {
132133
x = node.Value
133134
}
134135
if path := lockPathRhs(pass, x); path != nil {
135-
pass.ReportRangef(x, "literal copies lock value from %v: %v", analysisinternal.Format(pass.Fset, x), path)
136+
pass.ReportRangef(x, "literal copies lock value from %v: %v", astutil.Format(pass.Fset, x), path)
136137
}
137138
}
138139
}
@@ -166,7 +167,7 @@ func checkCopyLocksCallExpr(pass *analysis.Pass, ce *ast.CallExpr) {
166167
}
167168
for _, x := range ce.Args {
168169
if path := lockPathRhs(pass, x); path != nil {
169-
pass.ReportRangef(x, "call of %s copies lock value: %v", analysisinternal.Format(pass.Fset, ce.Fun), path)
170+
pass.ReportRangef(x, "call of %s copies lock value: %v", astutil.Format(pass.Fset, ce.Fun), path)
170171
}
171172
}
172173
}
@@ -233,7 +234,7 @@ func checkCopyLocksRangeVar(pass *analysis.Pass, rtok token.Token, e ast.Expr) {
233234
return
234235
}
235236
if path := lockPath(pass.Pkg, typ, nil); path != nil {
236-
pass.Reportf(e.Pos(), "range var %s copies lock: %v", analysisinternal.Format(pass.Fset, e), path)
237+
pass.Reportf(e.Pos(), "range var %s copies lock: %v", astutil.Format(pass.Fset, e), path)
237238
}
238239
}
239240

@@ -353,7 +354,7 @@ func lockPath(tpkg *types.Package, typ types.Type, seen map[types.Type]bool) typ
353354
// In go1.10, sync.noCopy did not implement Locker.
354355
// (The Unlock method was added only in CL 121876.)
355356
// TODO(adonovan): remove workaround when we drop go1.10.
356-
if analysisinternal.IsTypeNamed(typ, "sync", "noCopy") {
357+
if typesinternal.IsTypeNamed(typ, "sync", "noCopy") {
357358
return []string{typ.String()}
358359
}
359360

go/analysis/passes/deepequalerrors/deepequalerrors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"golang.org/x/tools/go/analysis/passes/inspect"
1515
"golang.org/x/tools/go/ast/inspector"
1616
"golang.org/x/tools/go/types/typeutil"
17-
"golang.org/x/tools/internal/analysisinternal"
17+
"golang.org/x/tools/internal/typesinternal"
1818
)
1919

2020
const Doc = `check for calls of reflect.DeepEqual on error values
@@ -35,7 +35,7 @@ var Analyzer = &analysis.Analyzer{
3535
}
3636

3737
func run(pass *analysis.Pass) (any, error) {
38-
if !analysisinternal.Imports(pass.Pkg, "reflect") {
38+
if !typesinternal.Imports(pass.Pkg, "reflect") {
3939
return nil, nil // doesn't directly import reflect
4040
}
4141

@@ -47,7 +47,7 @@ func run(pass *analysis.Pass) (any, error) {
4747
inspect.Preorder(nodeFilter, func(n ast.Node) {
4848
call := n.(*ast.CallExpr)
4949
obj := typeutil.Callee(pass.TypesInfo, call)
50-
if analysisinternal.IsFunctionNamed(obj, "reflect", "DeepEqual") && hasError(pass, call.Args[0]) && hasError(pass, call.Args[1]) {
50+
if typesinternal.IsFunctionNamed(obj, "reflect", "DeepEqual") && hasError(pass, call.Args[0]) && hasError(pass, call.Args[1]) {
5151
pass.ReportRangef(call, "avoid using reflect.DeepEqual with errors")
5252
}
5353
})

go/analysis/passes/defers/defers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
1414
"golang.org/x/tools/go/ast/inspector"
1515
"golang.org/x/tools/go/types/typeutil"
16-
"golang.org/x/tools/internal/analysisinternal"
16+
"golang.org/x/tools/internal/typesinternal"
1717
)
1818

1919
//go:embed doc.go
@@ -29,14 +29,14 @@ var Analyzer = &analysis.Analyzer{
2929
}
3030

3131
func run(pass *analysis.Pass) (any, error) {
32-
if !analysisinternal.Imports(pass.Pkg, "time") {
32+
if !typesinternal.Imports(pass.Pkg, "time") {
3333
return nil, nil
3434
}
3535

3636
checkDeferCall := func(node ast.Node) bool {
3737
switch v := node.(type) {
3838
case *ast.CallExpr:
39-
if analysisinternal.IsFunctionNamed(typeutil.Callee(pass.TypesInfo, v), "time", "Since") {
39+
if typesinternal.IsFunctionNamed(typeutil.Callee(pass.TypesInfo, v), "time", "Since") {
4040
pass.Reportf(v.Pos(), "call to time.Since is not deferred")
4141
}
4242
case *ast.FuncLit:

go/analysis/passes/httpmux/httpmux.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"golang.org/x/tools/go/analysis/passes/inspect"
1818
"golang.org/x/tools/go/ast/inspector"
1919
"golang.org/x/tools/go/types/typeutil"
20-
"golang.org/x/tools/internal/analysisinternal"
2120
"golang.org/x/tools/internal/typesinternal"
2221
)
2322

@@ -46,7 +45,7 @@ func run(pass *analysis.Pass) (any, error) {
4645
return nil, nil
4746
}
4847
}
49-
if !analysisinternal.Imports(pass.Pkg, "net/http") {
48+
if !typesinternal.Imports(pass.Pkg, "net/http") {
5049
return nil, nil
5150
}
5251
// Look for calls to ServeMux.Handle or ServeMux.HandleFunc.
@@ -79,15 +78,15 @@ func isServeMuxRegisterCall(pass *analysis.Pass, call *ast.CallExpr) bool {
7978
if fn == nil {
8079
return false
8180
}
82-
if analysisinternal.IsFunctionNamed(fn, "net/http", "Handle", "HandleFunc") {
81+
if typesinternal.IsFunctionNamed(fn, "net/http", "Handle", "HandleFunc") {
8382
return true
8483
}
8584
if !isMethodNamed(fn, "net/http", "Handle", "HandleFunc") {
8685
return false
8786
}
8887
recv := fn.Type().(*types.Signature).Recv() // isMethodNamed() -> non-nil
8988
isPtr, named := typesinternal.ReceiverNamed(recv)
90-
return isPtr && analysisinternal.IsTypeNamed(named, "net/http", "ServeMux")
89+
return isPtr && typesinternal.IsTypeNamed(named, "net/http", "ServeMux")
9190
}
9291

9392
// isMethodNamed reports when a function f is a method,

go/analysis/passes/httpresponse/httpresponse.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"golang.org/x/tools/go/analysis"
1414
"golang.org/x/tools/go/analysis/passes/inspect"
1515
"golang.org/x/tools/go/ast/inspector"
16-
"golang.org/x/tools/internal/analysisinternal"
1716
"golang.org/x/tools/internal/typesinternal"
1817
)
1918

@@ -46,7 +45,7 @@ func run(pass *analysis.Pass) (any, error) {
4645

4746
// Fast path: if the package doesn't import net/http,
4847
// skip the traversal.
49-
if !analysisinternal.Imports(pass.Pkg, "net/http") {
48+
if !typesinternal.Imports(pass.Pkg, "net/http") {
5049
return nil, nil
5150
}
5251

@@ -118,7 +117,7 @@ func isHTTPFuncOrMethodOnClient(info *types.Info, expr *ast.CallExpr) bool {
118117
return false // the function called does not return two values.
119118
}
120119
isPtr, named := typesinternal.ReceiverNamed(res.At(0))
121-
if !isPtr || named == nil || !analysisinternal.IsTypeNamed(named, "net/http", "Response") {
120+
if !isPtr || named == nil || !typesinternal.IsTypeNamed(named, "net/http", "Response") {
122121
return false // the first return type is not *http.Response.
123122
}
124123

@@ -133,11 +132,11 @@ func isHTTPFuncOrMethodOnClient(info *types.Info, expr *ast.CallExpr) bool {
133132
return ok && id.Name == "http" // function in net/http package.
134133
}
135134

136-
if analysisinternal.IsTypeNamed(typ, "net/http", "Client") {
135+
if typesinternal.IsTypeNamed(typ, "net/http", "Client") {
137136
return true // method on http.Client.
138137
}
139138
ptr, ok := types.Unalias(typ).(*types.Pointer)
140-
return ok && analysisinternal.IsTypeNamed(ptr.Elem(), "net/http", "Client") // method on *http.Client.
139+
return ok && typesinternal.IsTypeNamed(ptr.Elem(), "net/http", "Client") // method on *http.Client.
141140
}
142141

143142
// restOfBlock, given a traversal stack, finds the innermost containing

0 commit comments

Comments
 (0)