| 1 | // Copyright 2020 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 json |
| 6 | |
| 7 | import ( |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | // Whether a function is inlineable is dependent on the Go compiler version |
| 15 | // and also relies on the presence of the Go toolchain itself being installed. |
| 16 | // This test is disabled by default and explicitly enabled with an |
| 17 | // environment variable that is specified in our integration tests, |
| 18 | // which have fine control over exactly which Go version is being tested. |
| 19 | var testInline = os.Getenv("TEST_INLINE") != "" |
| 20 | |
| 21 | func TestInline(t *testing.T) { |
| 22 | if !testInline { |
| 23 | t.SkipNow() |
| 24 | } |
| 25 | |
| 26 | fncs := func() map[string]bool { |
| 27 | m := make(map[string]bool) |
| 28 | for _, s := range []string{ |
| 29 | "Encoder.needFlush", |
| 30 | "Decoder.ReadValue", // thin wrapper over Decoder.readValue |
| 31 | "decodeBuffer.needMore", |
| 32 | "consumeWhitespace", |
| 33 | "consumeNull", |
| 34 | "consumeFalse", |
| 35 | "consumeTrue", |
| 36 | "consumeSimpleString", |
| 37 | "consumeString", // thin wrapper over consumeStringResumable |
| 38 | "consumeSimpleNumber", |
| 39 | "consumeNumber", // thin wrapper over consumeNumberResumable |
| 40 | "unescapeStringMayCopy", // thin wrapper over unescapeString |
| 41 | "hasSuffixByte", |
| 42 | "trimSuffixByte", |
| 43 | "trimSuffixString", |
| 44 | "trimSuffixWhitespace", |
| 45 | "stateMachine.appendLiteral", |
| 46 | "stateMachine.appendNumber", |
| 47 | "stateMachine.appendString", |
| 48 | "stateMachine.depth", |
| 49 | "stateMachine.reset", |
| 50 | "stateMachine.mayAppendDelim", |
| 51 | "stateMachine.needDelim", |
| 52 | "stateMachine.popArray", |
| 53 | "stateMachine.popObject", |
| 54 | "stateMachine.pushArray", |
| 55 | "stateMachine.pushObject", |
| 56 | "stateEntry.increment", |
| 57 | "stateEntry.decrement", |
| 58 | "stateEntry.isArray", |
| 59 | "stateEntry.isObject", |
| 60 | "stateEntry.length", |
| 61 | "stateEntry.needImplicitColon", |
| 62 | "stateEntry.needImplicitComma", |
| 63 | "stateEntry.needObjectName", |
| 64 | "stateEntry.needObjectValue", |
| 65 | "objectNameStack.reset", |
| 66 | "objectNameStack.length", |
| 67 | "objectNameStack.getUnquoted", |
| 68 | "objectNameStack.push", |
| 69 | "objectNameStack.replaceLastQuotedOffset", |
| 70 | "objectNameStack.replaceLastUnquotedName", |
| 71 | "objectNameStack.pop", |
| 72 | "objectNameStack.ensureCopiedBuffer", |
| 73 | "objectNamespace.insertQuoted", // thin wrapper over objectNamespace.insert |
| 74 | "objectNamespace.insertUnquoted", // thin wrapper over objectNamespace.insert |
| 75 | "Token.String", // thin wrapper over Token.string |
| 76 | "foldName", // thin wrapper over appendFoldedName |
| 77 | "hash64", |
| 78 | } { |
| 79 | m[s] = true |
| 80 | } |
| 81 | return m |
| 82 | }() |
| 83 | |
| 84 | cmd := exec.Command("go", "build", "-gcflags=-m") |
| 85 | b, err := cmd.CombinedOutput() |
| 86 | if err != nil { |
| 87 | t.Fatalf("exec.Command error: %v\n\n%s", err, b) |
| 88 | } |
| 89 | for _, line := range strings.Split(string(b), "\n") { |
| 90 | const phrase = ": can inline " |
| 91 | if i := strings.Index(line, phrase); i >= 0 { |
| 92 | fnc := line[i+len(phrase):] |
| 93 | fnc = strings.ReplaceAll(fnc, "(", "") |
| 94 | fnc = strings.ReplaceAll(fnc, "*", "") |
| 95 | fnc = strings.ReplaceAll(fnc, ")", "") |
| 96 | delete(fncs, fnc) |
| 97 | } |
| 98 | } |
| 99 | for fnc := range fncs { |
| 100 | t.Errorf("%v is not inlineable, expected it to be", fnc) |
| 101 | } |
| 102 | } |