Skip to content

Commit

Permalink
Merge pull request #10 from srosenberg/sr/capture_go_build_output
Browse files Browse the repository at this point in the history
Couple of small internal "debug UX" improvements owing to [1],
  • Loading branch information
yuzefovich committed May 5, 2023
2 parents 81f097a + b0f4f59 commit fed79d9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
23 changes: 19 additions & 4 deletions gcassert.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func GCAssert(w io.Writer, paths ...string) error {
// Next: invoke Go compiler with -m flags to get the compiler to print
// its optimization decisions.

args := []string{"build", "-gcflags=-m -m -d=ssa/check_bce/debug=1"}
args := []string{"build", "-gcflags=-m=2 -d=ssa/check_bce/debug=1"}
for i := range paths {
args = append(args, "./"+paths[i])
}
Expand All @@ -169,12 +169,23 @@ func GCAssert(w io.Writer, paths ...string) error {
}
cmd.Dir = cwd
pr, pw := io.Pipe()
cmd.Stdout = pw
cmd.Stderr = pw
// Create a temp file to log all diagnostic output.
f, err := os.CreateTemp("", "gcassert-*.log")
if err != nil {
return err
}
fmt.Printf("See %s for full output.\n", f.Name())
// Log full 'go build' command.
fmt.Fprintln(f, cmd)
mw := io.MultiWriter(pw, f)
cmd.Stdout = mw
cmd.Stderr = mw
cmdErr := make(chan error, 1)

go func() {
cmdErr <- cmd.Run()
pw.Close()
_ = pw.Close()
_ = f.Close()
}()

scanner := bufio.NewScanner(pr)
Expand Down Expand Up @@ -284,6 +295,10 @@ func GCAssert(w io.Writer, paths ...string) error {
}
}
}
// If 'go build' failed, return the error.
if err := <-cmdErr; err != nil {
return err
}
return nil
}

Expand Down
11 changes: 6 additions & 5 deletions gcassert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func TestParseDirectives(t *testing.T) {
"testdata/noescape.go": {
21: {directives: []assertDirective{noescape}},
28: {directives: []assertDirective{noescape}},
34: {directives: []assertDirective{noescape}},
41: {directives: []assertDirective{noescape}},
44: {directives: []assertDirective{noescape}},
35: {directives: []assertDirective{noescape}},
42: {directives: []assertDirective{noescape}},
45: {directives: []assertDirective{noescape}},
},
"testdata/issue5.go": {
4: {inlinableCallsites: []passInfo{{colNo: 14}}},
Expand All @@ -83,13 +83,14 @@ func TestGCAssert(t *testing.T) {
}

expectedOutput := `testdata/noescape.go:21: foo := foo{a: 1, b: 2}: foo escapes to heap:
testdata/noescape.go:34: // This annotation should fail, because f will escape to the heap.
testdata/noescape.go:35: // This annotation should fail, because f will escape to the heap.
//
//gcassert:noescape
func (f foo) setA(a int) *foo {
f.a = a
return &f
}: f escapes to heap:
testdata/noescape.go:44: : a escapes to heap:
testdata/noescape.go:45: : a escapes to heap:
testdata/bce.go:8: fmt.Println(ints[5]): Found IsInBounds
testdata/bce.go:17: sum += notInlinable(ints[i]): call was not inlined
testdata/bce.go:19: sum += notInlinable(ints[i]): call was not inlined
Expand Down
1 change: 1 addition & 0 deletions testdata/issue5.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func Gen() S {
// Gen().Layout() has another inlined function in it, Gen().

//gcassert:inline
//go:noinline
func (s S) Layout() {
select {}
}
Expand Down
1 change: 1 addition & 0 deletions testdata/noescape.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func returnsStackVar() foo {
}

// This annotation should fail, because f will escape to the heap.
//
//gcassert:noescape
func (f foo) setA(a int) *foo {
f.a = a
Expand Down

0 comments on commit fed79d9

Please sign in to comment.