Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 1 | // 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 | |
| 5 | package gc |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "internal/testenv" |
| 10 | "io/ioutil" |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 11 | "os" |
| 12 | "os/exec" |
Ian Lance Taylor | a131a66 | 2016-02-26 13:05:35 -0800 | [diff] [blame] | 13 | "path/filepath" |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 14 | "strings" |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 15 | "testing" |
| 16 | ) |
| 17 | |
| 18 | // Make sure "hello world" does not link in all the |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 19 | // fmt.scanf routines. See issue 6853. |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 20 | func TestScanfRemoval(t *testing.T) { |
| 21 | testenv.MustHaveGoBuild(t) |
Josh Bleecher Snyder | 5d98330 | 2019-05-13 12:29:58 -0700 | [diff] [blame] | 22 | t.Parallel() |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 23 | |
| 24 | // Make a directory to work in. |
| 25 | dir, err := ioutil.TempDir("", "issue6853a-") |
| 26 | if err != nil { |
Keith Randall | 01a1eaa | 2018-12-07 10:00:36 -0800 | [diff] [blame] | 27 | t.Fatalf("could not create directory: %v", err) |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 28 | } |
| 29 | defer os.RemoveAll(dir) |
| 30 | |
| 31 | // Create source. |
Ian Lance Taylor | a131a66 | 2016-02-26 13:05:35 -0800 | [diff] [blame] | 32 | src := filepath.Join(dir, "test.go") |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 33 | f, err := os.Create(src) |
| 34 | if err != nil { |
Keith Randall | 01a1eaa | 2018-12-07 10:00:36 -0800 | [diff] [blame] | 35 | t.Fatalf("could not create source file: %v", err) |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 36 | } |
| 37 | f.Write([]byte(` |
| 38 | package main |
| 39 | import "fmt" |
| 40 | func main() { |
| 41 | fmt.Println("hello world") |
| 42 | } |
| 43 | `)) |
| 44 | f.Close() |
| 45 | |
| 46 | // Name of destination. |
Ian Lance Taylor | a131a66 | 2016-02-26 13:05:35 -0800 | [diff] [blame] | 47 | dst := filepath.Join(dir, "test") |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 48 | |
| 49 | // Compile source. |
Keith Randall | 842b058 | 2016-08-30 11:08:47 -0700 | [diff] [blame] | 50 | cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dst, src) |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 51 | out, err := cmd.CombinedOutput() |
| 52 | if err != nil { |
Keith Randall | 01a1eaa | 2018-12-07 10:00:36 -0800 | [diff] [blame] | 53 | t.Fatalf("could not build target: %v", err) |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Check destination to see if scanf code was included. |
Keith Randall | 842b058 | 2016-08-30 11:08:47 -0700 | [diff] [blame] | 57 | cmd = exec.Command(testenv.GoToolPath(t), "tool", "nm", dst) |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 58 | out, err = cmd.CombinedOutput() |
| 59 | if err != nil { |
Keith Randall | 01a1eaa | 2018-12-07 10:00:36 -0800 | [diff] [blame] | 60 | t.Fatalf("could not read target: %v", err) |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 61 | } |
Dominik Honnef | 1cb3044 | 2016-04-01 03:49:43 +0200 | [diff] [blame] | 62 | if bytes.Contains(out, []byte("scanInt")) { |
Keith Randall | 01a1eaa | 2018-12-07 10:00:36 -0800 | [diff] [blame] | 63 | t.Fatalf("scanf code not removed from helloworld") |
Keith Randall | 4f97ec0 | 2015-12-03 13:20:58 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 66 | |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 67 | // Make sure -S prints assembly code. See issue 14515. |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 68 | func TestDashS(t *testing.T) { |
| 69 | testenv.MustHaveGoBuild(t) |
Josh Bleecher Snyder | 5d98330 | 2019-05-13 12:29:58 -0700 | [diff] [blame] | 70 | t.Parallel() |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 71 | |
| 72 | // Make a directory to work in. |
| 73 | dir, err := ioutil.TempDir("", "issue14515-") |
| 74 | if err != nil { |
Keith Randall | 01a1eaa | 2018-12-07 10:00:36 -0800 | [diff] [blame] | 75 | t.Fatalf("could not create directory: %v", err) |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 76 | } |
| 77 | defer os.RemoveAll(dir) |
| 78 | |
| 79 | // Create source. |
Ian Lance Taylor | a131a66 | 2016-02-26 13:05:35 -0800 | [diff] [blame] | 80 | src := filepath.Join(dir, "test.go") |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 81 | f, err := os.Create(src) |
| 82 | if err != nil { |
Keith Randall | 01a1eaa | 2018-12-07 10:00:36 -0800 | [diff] [blame] | 83 | t.Fatalf("could not create source file: %v", err) |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 84 | } |
| 85 | f.Write([]byte(` |
| 86 | package main |
| 87 | import "fmt" |
| 88 | func main() { |
| 89 | fmt.Println("hello world") |
| 90 | } |
| 91 | `)) |
| 92 | f.Close() |
| 93 | |
| 94 | // Compile source. |
Keith Randall | 842b058 | 2016-08-30 11:08:47 -0700 | [diff] [blame] | 95 | cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src) |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 96 | out, err := cmd.CombinedOutput() |
| 97 | if err != nil { |
Keith Randall | 01a1eaa | 2018-12-07 10:00:36 -0800 | [diff] [blame] | 98 | t.Fatalf("could not build target: %v", err) |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | patterns := []string{ |
| 102 | // It is hard to look for actual instructions in an |
Brad Fitzpatrick | 5fea2cc | 2016-03-01 23:21:55 +0000 | [diff] [blame] | 103 | // arch-independent way. So we'll just look for |
Keith Randall | c747fce | 2016-02-25 19:17:55 -0800 | [diff] [blame] | 104 | // 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 | } |