Skip to content

Commit b818f97

Browse files
committed
feat: update gofmt code from go1.22
1 parent 399ed8a commit b818f97

19 files changed

+905
-203
lines changed

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.PHONY: build build_gofmt build_goimports
2+
3+
default: build
4+
5+
build: build_gofmt build_goimports
6+
7+
build_gofmt:
8+
go build ./gofmt
9+
10+
build_goimports:
11+
go build ./goimports

go.mod

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/golangci/gofmt
22

3-
go 1.20
3+
go 1.22
44

55
require (
6-
golang.org/x/sync v0.7.0
7-
golang.org/x/tools v0.18.0
6+
golang.org/x/sync v0.8.0
7+
golang.org/x/tools v0.24.0
88
)
99

10-
require golang.org/x/mod v0.15.0 // indirect
10+
require golang.org/x/mod v0.20.0 // indirect

go.sum

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
golang.org/x/mod v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
2-
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
3-
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
4-
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
5-
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
6-
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
1+
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
2+
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
3+
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
4+
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
5+
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
6+
golang.org/x/tools v0.24.0/go.mod h1:YhNqVBIfWHdzvTLs0d8LCuMhkKUgSUKldakyV7W/WDQ=

gofmt/internal/cfg/cfg.go

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const KnownEnv = `
3838
GOARM
3939
GOBIN
4040
GOCACHE
41+
GOCACHEPROG
4142
GOENV
4243
GOEXE
4344
GOEXPERIMENT
@@ -59,6 +60,7 @@ const KnownEnv = `
5960
GOROOT
6061
GOSUMDB
6162
GOTMPDIR
63+
GOTOOLCHAIN
6264
GOTOOLDIR
6365
GOVCS
6466
GOWASM

gofmt/internal/diff/diff.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func Diff(oldName string, old []byte, newName string, new []byte) []byte {
7474
continue
7575
}
7676

77-
// Expand matching lines as far possible,
77+
// Expand matching lines as far as possible,
7878
// establishing that x[start.x:end.x] == y[start.y:end.y].
79-
// Note that on the first (or last) iteration we may (or definitey do)
79+
// Note that on the first (or last) iteration we may (or definitely do)
8080
// have an empty match: start.x==end.x and start.y==end.y.
8181
start := m
8282
for start.x > done.x && start.y > done.y && x[start.x-1] == y[start.y-1] {

gofmt/internal/goroot/importcfg.go

-66
This file was deleted.

gofmt/internal/platform/supported.go

+109-28
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:generate go test . -run=^TestGenerated$ -fix
6+
57
package platform
68

9+
// An OSArch is a pair of GOOS and GOARCH values indicating a platform.
10+
type OSArch struct {
11+
GOOS, GOARCH string
12+
}
13+
14+
func (p OSArch) String() string {
15+
return p.GOOS + "/" + p.GOARCH
16+
}
17+
718
// RaceDetectorSupported reports whether goos/goarch supports the race
819
// detector. There is a copy of this function in cmd/dist/test.go.
920
// Race detector only supports 48-bit VMA on arm64. But it will always
@@ -24,11 +35,10 @@ func RaceDetectorSupported(goos, goarch string) bool {
2435

2536
// MSanSupported reports whether goos/goarch supports the memory
2637
// sanitizer option.
27-
// There is a copy of this function in misc/cgo/testsanitizers/cc_test.go.
2838
func MSanSupported(goos, goarch string) bool {
2939
switch goos {
3040
case "linux":
31-
return goarch == "amd64" || goarch == "arm64"
41+
return goarch == "amd64" || goarch == "arm64" || goarch == "loong64"
3242
case "freebsd":
3343
return goarch == "amd64"
3444
default:
@@ -38,11 +48,10 @@ func MSanSupported(goos, goarch string) bool {
3848

3949
// ASanSupported reports whether goos/goarch supports the address
4050
// sanitizer option.
41-
// There is a copy of this function in misc/cgo/testsanitizers/cc_test.go.
4251
func ASanSupported(goos, goarch string) bool {
4352
switch goos {
4453
case "linux":
45-
return goarch == "arm64" || goarch == "amd64" || goarch == "riscv64" || goarch == "ppc64le"
54+
return goarch == "arm64" || goarch == "amd64" || goarch == "loong64" || goarch == "riscv64" || goarch == "ppc64le"
4655
default:
4756
return false
4857
}
@@ -71,20 +80,12 @@ func FuzzInstrumented(goos, goarch string) bool {
7180
}
7281
}
7382

74-
// MustLinkExternal reports whether goos/goarch requires external linking.
75-
func MustLinkExternal(goos, goarch string) bool {
76-
return MustLinkExternalGo121(goos, goarch, false)
77-
}
78-
79-
// MustLinkExternalGo121 reports whether goos/goarch requires external linking,
80-
// with or without cgo dependencies. [This version back-ported from
81-
// Go 1.21 as part of a test].
82-
func MustLinkExternalGo121(goos, goarch string, withCgo bool) bool {
83+
// MustLinkExternal reports whether goos/goarch requires external linking
84+
// with or without cgo dependencies.
85+
func MustLinkExternal(goos, goarch string, withCgo bool) bool {
8386
if withCgo {
8487
switch goarch {
85-
case "loong64",
86-
"mips", "mipsle", "mips64", "mips64le",
87-
"riscv64":
88+
case "loong64", "mips", "mipsle", "mips64", "mips64le":
8889
// Internally linking cgo is incomplete on some architectures.
8990
// https://go.dev/issue/14449
9091
return true
@@ -96,7 +97,9 @@ func MustLinkExternalGo121(goos, goarch string, withCgo bool) bool {
9697
case "ppc64":
9798
// Big Endian PPC64 cgo internal linking is not implemented for aix or linux.
9899
// https://go.dev/issue/8912
99-
return true
100+
if goos == "aix" || goos == "linux" {
101+
return true
102+
}
100103
}
101104

102105
switch goos {
@@ -125,25 +128,48 @@ func MustLinkExternalGo121(goos, goarch string, withCgo bool) bool {
125128

126129
// BuildModeSupported reports whether goos/goarch supports the given build mode
127130
// using the given compiler.
131+
// There is a copy of this function in cmd/dist/test.go.
128132
func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
129133
if compiler == "gccgo" {
130134
return true
131135
}
132136

133-
platform := goos + "/" + goarch
137+
if _, ok := distInfo[OSArch{goos, goarch}]; !ok {
138+
return false // platform unrecognized
139+
}
134140

141+
platform := goos + "/" + goarch
135142
switch buildmode {
136143
case "archive":
137144
return true
138145

139146
case "c-archive":
140-
// TODO(bcmills): This seems dubious.
141-
// Do we really support c-archive mode on js/wasm‽
142-
return platform != "linux/ppc64"
147+
switch goos {
148+
case "aix", "darwin", "ios", "windows":
149+
return true
150+
case "linux":
151+
switch goarch {
152+
case "386", "amd64", "arm", "armbe", "arm64", "arm64be", "loong64", "ppc64le", "riscv64", "s390x":
153+
// linux/ppc64 not supported because it does
154+
// not support external linking mode yet.
155+
return true
156+
default:
157+
// Other targets do not support -shared,
158+
// per ParseFlags in
159+
// cmd/compile/internal/base/flag.go.
160+
// For c-archive the Go tool passes -shared,
161+
// so that the result is suitable for inclusion
162+
// in a PIE or shared library.
163+
return false
164+
}
165+
case "freebsd":
166+
return goarch == "amd64"
167+
}
168+
return false
143169

144170
case "c-shared":
145171
switch platform {
146-
case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le", "linux/riscv64", "linux/s390x",
172+
case "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/386", "linux/ppc64le", "linux/riscv64", "linux/s390x",
147173
"android/amd64", "android/arm", "android/arm64", "android/386",
148174
"freebsd/amd64",
149175
"darwin/amd64", "darwin/arm64",
@@ -160,7 +186,7 @@ func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
160186

161187
case "pie":
162188
switch platform {
163-
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/riscv64", "linux/s390x",
189+
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/loong64", "linux/ppc64le", "linux/riscv64", "linux/s390x",
164190
"android/amd64", "android/arm", "android/arm64", "android/386",
165191
"freebsd/amd64",
166192
"darwin/amd64", "darwin/arm64",
@@ -180,8 +206,8 @@ func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
180206

181207
case "plugin":
182208
switch platform {
183-
case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
184-
"android/amd64", "android/arm", "android/arm64", "android/386",
209+
case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/loong64", "linux/s390x", "linux/ppc64le",
210+
"android/amd64", "android/386",
185211
"darwin/amd64", "darwin/arm64",
186212
"freebsd/amd64":
187213
return true
@@ -195,11 +221,66 @@ func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
195221

196222
func InternalLinkPIESupported(goos, goarch string) bool {
197223
switch goos + "/" + goarch {
198-
case "darwin/amd64", "darwin/arm64",
224+
case "android/arm64",
225+
"darwin/amd64", "darwin/arm64",
199226
"linux/amd64", "linux/arm64", "linux/ppc64le",
200-
"android/arm64",
201-
"windows-amd64", "windows-386", "windows-arm":
227+
"windows/386", "windows/amd64", "windows/arm", "windows/arm64":
228+
return true
229+
}
230+
return false
231+
}
232+
233+
// DefaultPIE reports whether goos/goarch produces a PIE binary when using the
234+
// "default" buildmode. On Windows this is affected by -race,
235+
// so force the caller to pass that in to centralize that choice.
236+
func DefaultPIE(goos, goarch string, isRace bool) bool {
237+
switch goos {
238+
case "android", "ios":
239+
return true
240+
case "windows":
241+
if isRace {
242+
// PIE is not supported with -race on windows;
243+
// see https://go.dev/cl/416174.
244+
return false
245+
}
246+
return true
247+
case "darwin":
202248
return true
203249
}
204250
return false
205251
}
252+
253+
// ExecutableHasDWARF reports whether the linked executable includes DWARF
254+
// symbols on goos/goarch.
255+
func ExecutableHasDWARF(goos, goarch string) bool {
256+
switch goos {
257+
case "plan9", "ios":
258+
return false
259+
}
260+
return true
261+
}
262+
263+
// osArchInfo describes information about an OSArch extracted from cmd/dist and
264+
// stored in the generated distInfo map.
265+
type osArchInfo struct {
266+
CgoSupported bool
267+
FirstClass bool
268+
Broken bool
269+
}
270+
271+
// CgoSupported reports whether goos/goarch supports cgo.
272+
func CgoSupported(goos, goarch string) bool {
273+
return distInfo[OSArch{goos, goarch}].CgoSupported
274+
}
275+
276+
// FirstClass reports whether goos/goarch is considered a “first class” port.
277+
// (See https://go.dev/wiki/PortingPolicy#first-class-ports.)
278+
func FirstClass(goos, goarch string) bool {
279+
return distInfo[OSArch{goos, goarch}].FirstClass
280+
}
281+
282+
// Broken reportsr whether goos/goarch is considered a broken port.
283+
// (See https://go.dev/wiki/PortingPolicy#broken-ports.)
284+
func Broken(goos, goarch string) bool {
285+
return distInfo[OSArch{goos, goarch}].Broken
286+
}

0 commit comments

Comments
 (0)