Skip to content

Commit d4c6eff

Browse files
committed
cmd/compile: add up-to-date test for generated files
This runs the ssa/_gen generator writing files into a temporary directory, and then checks that there are no differences with what is currently in the ssa directory, and also checks that any file with the "generated from _gen/..." header was actually generated, and checks that the headers on the generated file match the expected header prefix. Change-Id: Ic8eeb0b06cf6f2e576a013e865b331a12d3a77aa Reviewed-on: https://go-review.googlesource.com/c/go/+/680615 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com>
1 parent 7fa2c73 commit d4c6eff

File tree

12 files changed

+1848
-3
lines changed

12 files changed

+1848
-3
lines changed

src/cmd/compile/internal/ssa/_gen/README

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ more information.
99

1010
To regenerate everything, run "go generate" on the ssa package
1111
in the parent directory.
12+
13+
The parent directory contains a test in generate_test.go that will fail
14+
if the generated files are not up-to-date, and to allow that test to
15+
run in no-network environments, golang.org/x/tools/go/ast/astutil is
16+
vendored.

src/cmd/compile/internal/ssa/_gen/allocators.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func genAllocators() {
155155
panic(err)
156156
}
157157

158-
if err := os.WriteFile("../allocators.go", b, 0666); err != nil {
158+
if err := os.WriteFile(outFile("allocators.go"), b, 0666); err != nil {
159159
log.Fatalf("can't write output: %v\n", err)
160160
}
161161
}

src/cmd/compile/internal/ssa/_gen/main.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ var archs []arch
113113
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
114114
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
115115
var tracefile = flag.String("trace", "", "write trace to `file`")
116+
var outDir = flag.String("outdir", "..", "directory in which to write generated files")
116117

117118
func main() {
118119
flag.Parse()
@@ -144,6 +145,13 @@ func main() {
144145
defer trace.Stop()
145146
}
146147

148+
if *outDir != ".." {
149+
err := os.MkdirAll(*outDir, 0755)
150+
if err != nil {
151+
log.Fatalf("failed to create output directory: %v", err)
152+
}
153+
}
154+
147155
slices.SortFunc(archs, func(a, b arch) int {
148156
return strings.Compare(a.name, b.name)
149157
})
@@ -193,6 +201,10 @@ func main() {
193201
}
194202
}
195203

204+
func outFile(file string) string {
205+
return *outDir + "/" + file
206+
}
207+
196208
func genOp() {
197209
w := new(bytes.Buffer)
198210
fmt.Fprintf(w, "// Code generated from _gen/*Ops.go using 'go generate'; DO NOT EDIT.\n")
@@ -500,7 +512,7 @@ func genOp() {
500512
panic(err)
501513
}
502514

503-
if err := os.WriteFile("../opGen.go", b, 0666); err != nil {
515+
if err := os.WriteFile(outFile("opGen.go"), b, 0666); err != nil {
504516
log.Fatalf("can't write output: %v\n", err)
505517
}
506518

src/cmd/compile/internal/ssa/_gen/rulegen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func genRulesSuffix(arch arch, suff string) {
322322
file = astutil.Apply(file, pre, post).(*ast.File)
323323

324324
// Write the well-formatted source to file
325-
f, err := os.Create("../rewrite" + arch.name + suff + ".go")
325+
f, err := os.Create(outFile("rewrite" + arch.name + suff + ".go"))
326326
if err != nil {
327327
log.Fatalf("can't write output: %v", err)
328328
}

src/cmd/compile/internal/ssa/_gen/vendor/golang.org/x/tools/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/ssa/_gen/vendor/golang.org/x/tools/PATENTS

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)