blob: edad6d042a36d824cea6e0da40c3ad8aa643697e [file] [log] [blame]
Keith Randall4f97ec02015-12-03 13:20:58 -08001// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package gc
6
7import (
8 "bytes"
9 "internal/testenv"
10 "io/ioutil"
Keith Randall4f97ec02015-12-03 13:20:58 -080011 "os"
12 "os/exec"
Ian Lance Taylora131a662016-02-26 13:05:35 -080013 "path/filepath"
Keith Randallc747fce2016-02-25 19:17:55 -080014 "strings"
Keith Randall4f97ec02015-12-03 13:20:58 -080015 "testing"
16)
17
18// Make sure "hello world" does not link in all the
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000019// fmt.scanf routines. See issue 6853.
Keith Randall4f97ec02015-12-03 13:20:58 -080020func TestScanfRemoval(t *testing.T) {
21 testenv.MustHaveGoBuild(t)
Josh Bleecher Snyder5d983302019-05-13 12:29:58 -070022 t.Parallel()
Keith Randall4f97ec02015-12-03 13:20:58 -080023
24 // Make a directory to work in.
25 dir, err := ioutil.TempDir("", "issue6853a-")
26 if err != nil {
Keith Randall01a1eaa2018-12-07 10:00:36 -080027 t.Fatalf("could not create directory: %v", err)
Keith Randall4f97ec02015-12-03 13:20:58 -080028 }
29 defer os.RemoveAll(dir)
30
31 // Create source.
Ian Lance Taylora131a662016-02-26 13:05:35 -080032 src := filepath.Join(dir, "test.go")
Keith Randall4f97ec02015-12-03 13:20:58 -080033 f, err := os.Create(src)
34 if err != nil {
Keith Randall01a1eaa2018-12-07 10:00:36 -080035 t.Fatalf("could not create source file: %v", err)
Keith Randall4f97ec02015-12-03 13:20:58 -080036 }
37 f.Write([]byte(`
38package main
39import "fmt"
40func main() {
41 fmt.Println("hello world")
42}
43`))
44 f.Close()
45
46 // Name of destination.
Ian Lance Taylora131a662016-02-26 13:05:35 -080047 dst := filepath.Join(dir, "test")
Keith Randall4f97ec02015-12-03 13:20:58 -080048
49 // Compile source.
Keith Randall842b0582016-08-30 11:08:47 -070050 cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dst, src)
Keith Randall4f97ec02015-12-03 13:20:58 -080051 out, err := cmd.CombinedOutput()
52 if err != nil {
Keith Randall01a1eaa2018-12-07 10:00:36 -080053 t.Fatalf("could not build target: %v", err)
Keith Randall4f97ec02015-12-03 13:20:58 -080054 }
55
56 // Check destination to see if scanf code was included.
Keith Randall842b0582016-08-30 11:08:47 -070057 cmd = exec.Command(testenv.GoToolPath(t), "tool", "nm", dst)
Keith Randall4f97ec02015-12-03 13:20:58 -080058 out, err = cmd.CombinedOutput()
59 if err != nil {
Keith Randall01a1eaa2018-12-07 10:00:36 -080060 t.Fatalf("could not read target: %v", err)
Keith Randall4f97ec02015-12-03 13:20:58 -080061 }
Dominik Honnef1cb30442016-04-01 03:49:43 +020062 if bytes.Contains(out, []byte("scanInt")) {
Keith Randall01a1eaa2018-12-07 10:00:36 -080063 t.Fatalf("scanf code not removed from helloworld")
Keith Randall4f97ec02015-12-03 13:20:58 -080064 }
65}
Keith Randallc747fce2016-02-25 19:17:55 -080066
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +000067// Make sure -S prints assembly code. See issue 14515.
Keith Randallc747fce2016-02-25 19:17:55 -080068func TestDashS(t *testing.T) {
69 testenv.MustHaveGoBuild(t)
Josh Bleecher Snyder5d983302019-05-13 12:29:58 -070070 t.Parallel()
Keith Randallc747fce2016-02-25 19:17:55 -080071
72 // Make a directory to work in.
73 dir, err := ioutil.TempDir("", "issue14515-")
74 if err != nil {
Keith Randall01a1eaa2018-12-07 10:00:36 -080075 t.Fatalf("could not create directory: %v", err)
Keith Randallc747fce2016-02-25 19:17:55 -080076 }
77 defer os.RemoveAll(dir)
78
79 // Create source.
Ian Lance Taylora131a662016-02-26 13:05:35 -080080 src := filepath.Join(dir, "test.go")
Keith Randallc747fce2016-02-25 19:17:55 -080081 f, err := os.Create(src)
82 if err != nil {
Keith Randall01a1eaa2018-12-07 10:00:36 -080083 t.Fatalf("could not create source file: %v", err)
Keith Randallc747fce2016-02-25 19:17:55 -080084 }
85 f.Write([]byte(`
86package main
87import "fmt"
88func main() {
89 fmt.Println("hello world")
90}
91`))
92 f.Close()
93
94 // Compile source.
Keith Randall842b0582016-08-30 11:08:47 -070095 cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src)
Keith Randallc747fce2016-02-25 19:17:55 -080096 out, err := cmd.CombinedOutput()
97 if err != nil {
Keith Randall01a1eaa2018-12-07 10:00:36 -080098 t.Fatalf("could not build target: %v", err)
Keith Randallc747fce2016-02-25 19:17:55 -080099 }
100
101 patterns := []string{
102 // It is hard to look for actual instructions in an
Brad Fitzpatrick5fea2cc2016-03-01 23:21:55 +0000103 // arch-independent way. So we'll just look for
Keith Randallc747fce2016-02-25 19:17:55 -0800104 // pseudo-ops that are arch-independent.
105 "\tTEXT\t",
106 "\tFUNCDATA\t",
107 "\tPCDATA\t",
108 }
109 outstr := string(out)
110 for _, p := range patterns {
111 if !strings.Contains(outstr, p) {
112 println(outstr)
113 panic("can't find pattern " + p)
114 }
115 }
116}