| 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 demangle |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"flag" |
| 10 |
"fmt" |
| 11 |
"os" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
var verbose = flag.Bool("verbose", false, "print each demangle-expected symbol") |
| 17 |
|
| 18 |
const filename = "testdata/demangle-expected" |
| 19 |
|
| 20 |
// exceptions is a list of exceptions from demangle-expected that we do |
| 21 |
// not handle the same as the standard demangler. We keep a list of |
| 22 |
// exceptions so that we can use an exact copy of the file. |
| 23 |
var exceptions = map[string]bool{ |
| 24 |
// These exceptions are all based on different handling of a |
| 25 |
// substitution that refers to a template parameter. |
| 26 |
// The standard demangler seems to have a bug in which |
| 27 |
// template it uses when a reference or rvalue-reference |
| 28 |
// refers to a substitution that resolves to a template parameter. |
| 29 |
"_ZN3mdr16in_cached_threadIRZNK4cudr6GPUSet17parallel_for_eachIZN5tns3d20shape_representation7compute7GPUImpl7executeERKNS_1AINS_7ptr_refIKjEELl3ELl3ENS_8c_strideILl1ELl0EEEEERKNS8_INS9_IjEELl4ELl1ESD_EEEUliRKNS1_7ContextERNS7_5StateEE_JSt6vectorISO_SaISO_EEEEEvOT_DpRT0_EUlSP_E_JSt17reference_wrapperISO_EEEENS_12ScopedFutureIDTclfp_spcl7forwardISW_Efp0_EEEEESV_DpOSW_": true, |
| 30 |
"_ZNSt9_Any_data9_M_accessIPZN3sel8Selector6SetObjI3FooJPKcMS4_FviEEEEvRT_DpT0_EUlvE_EESA_v": true, |
| 31 |
"_ZNSt9_Any_data9_M_accessIPZN13ThreadManager7newTaskIRSt5_BindIFSt7_Mem_fnIM5DiaryFivEEPS5_EEIEEESt6futureINSt9result_ofIFT_DpT0_EE4typeEEOSF_DpOSG_EUlvE_EERSF_v": true, |
| 32 |
"_ZNSt9_Any_data9_M_accessIPZN6cereal18polymorphic_detail15getInputBindingINS1_16JSONInputArchiveEEENS1_6detail15InputBindingMapIT_E11SerializersERS7_jEUlPvRSt10unique_ptrIvNS5_12EmptyDeleterIvEEEE0_EESA_v": true, |
| 33 |
"_ZNSt9_Any_data9_M_accessIPZ4postISt8functionIFvvEEEvOT_EUlvE_EERS5_v": true, |
| 34 |
"_ZNSt9_Any_data9_M_accessIPZN13ThreadManager10futureTaskISt5_BindIFSt7_Mem_fnIM6RunnerFvvEEPS5_EEEEvOT_EUlvE_EERSC_v": true, |
| 35 |
|
| 36 |
// The standard demangler appears to mishandle the argument here, |
| 37 |
// treating it as a lamdba auto when I think it ought to be a |
| 38 |
// template argument. |
| 39 |
"_Z7forwardIZ3FoovEUlPA5_T_E1_EOS0_RS0_": true, |
| 40 |
|
| 41 |
// These exceptions are because we handle recursion differently, |
| 42 |
// and permit some cases that the standard demangler blocks. |
| 43 |
"_Z1KMMMMMMMMMMMMMMMA_xooooooooooooooo": true, |
| 44 |
"_ZdvMMMMMMMMMMMMMrrrrA_DTdvfp_fp_Eededilfdfdfdfd": true, |
| 45 |
"_Z1MA_aMMMMA_MMA_MMMMMMMMSt1MS_o11T0000000000t2M0oooozoooo": true, |
| 46 |
|
| 47 |
// The standard demangler mishandles an inheriting constructor, |
| 48 |
// getting confused when it picks up the last name. |
| 49 |
"_ZN1DCI11BEi": true, |
| 50 |
|
| 51 |
// The standard demangler can handle template arguments in |
| 52 |
// a constraint expression. I'm not sure how these should be |
| 53 |
// handled. Doing the obvious things doesn't work with LLVM names, |
| 54 |
// which refer to templates that don't seem to exist. |
| 55 |
"_Z1fIiQ1CIT_EEvv": true, |
| 56 |
"_Z1fIiEvvQ1CIT_E": true, |
| 57 |
} |
| 58 |
|
| 59 |
// For simplicity, this test reads an exact copy of |
| 60 |
// libiberty/testsuite/demangle-expected from GCC. See that file for |
| 61 |
// the syntax. We ignore all tests that are not --format=gnu-v3 or |
| 62 |
// --format=auto with a string starting with _Z. |
| 63 |
func TestExpected(t *testing.T) { |
| 64 |
t.Parallel() |
| 65 |
f, err := os.Open(filename) |
| 66 |
if err != nil { |
| 67 |
t.Fatal(err) |
| 68 |
} |
| 69 |
defer f.Close() |
| 70 |
scanner := bufio.NewScanner(f) |
| 71 |
lineno := 1 |
| 72 |
for { |
| 73 |
format, got := getOptLine(t, scanner, &lineno) |
| 74 |
if !got { |
| 75 |
break |
| 76 |
} |
| 77 |
report := lineno |
| 78 |
input := getLine(t, scanner, &lineno) |
| 79 |
expect := getLine(t, scanner, &lineno) |
| 80 |
|
| 81 |
skip, testNoParams := skipExpectedTest(t, format, input, report) |
| 82 |
|
| 83 |
var expectNoParams string |
| 84 |
if testNoParams { |
| 85 |
expectNoParams = getLine(t, scanner, &lineno) |
| 86 |
} |
| 87 |
|
| 88 |
if skip { |
| 89 |
continue |
| 90 |
} |
| 91 |
|
| 92 |
oneTest(t, report, input, expect, true) |
| 93 |
if testNoParams { |
| 94 |
oneTest(t, report, input, expectNoParams, false) |
| 95 |
} |
| 96 |
} |
| 97 |
if err := scanner.Err(); err != nil { |
| 98 |
t.Error(err) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// oneTest tests one entry from demangle-expected. |
| 103 |
func oneTest(t *testing.T, report int, input, expect string, params bool) { |
| 104 |
if *verbose { |
| 105 |
fmt.Println(input) |
| 106 |
} |
| 107 |
|
| 108 |
exception := exceptions[input] |
| 109 |
|
| 110 |
var s string |
| 111 |
var err error |
| 112 |
if params { |
| 113 |
s, err = ToString(input) |
| 114 |
} else { |
| 115 |
s, err = ToString(input, NoParams) |
| 116 |
} |
| 117 |
if err != nil { |
| 118 |
if exception { |
| 119 |
t.Logf("%s:%d: ignore expected difference: got %q, want %q", filename, report, err, expect) |
| 120 |
return |
| 121 |
} |
| 122 |
|
| 123 |
if err != ErrNotMangledName { |
| 124 |
if input == expect { |
| 125 |
return |
| 126 |
} |
| 127 |
t.Errorf("%s:%d: %v", filename, report, err) |
| 128 |
return |
| 129 |
} |
| 130 |
s = input |
| 131 |
} |
| 132 |
|
| 133 |
if s != expect { |
| 134 |
if exception { |
| 135 |
t.Logf("%s:%d: ignore expected difference: got %q, want %q", filename, report, s, expect) |
| 136 |
} else { |
| 137 |
var a AST |
| 138 |
if params { |
| 139 |
a, err = ToAST(input) |
| 140 |
} else { |
| 141 |
a, err = ToAST(input, NoParams) |
| 142 |
} |
| 143 |
if err != nil { |
| 144 |
t.Logf("ToAST error: %v", err) |
| 145 |
} else { |
| 146 |
t.Logf("\n%#v", a) |
| 147 |
} |
| 148 |
t.Errorf("%s:%d: params: %t: got %q, want %q", filename, report, params, s, expect) |
| 149 |
} |
| 150 |
} else if exception && params { |
| 151 |
t.Errorf("%s:%d: unexpected success (input listed in exceptions)", filename, report) |
| 152 |
} |
| 153 |
|
| 154 |
if s == expect && s != input && params && len(expect) > 200 { |
| 155 |
ss, err := ToString(input, MaxLength(6)) |
| 156 |
if err != nil { |
| 157 |
t.Errorf("%s:%d: error with MaxLength: %v", filename, report, err) |
| 158 |
} else if ss != expect[:64] { |
| 159 |
t.Errorf("%s:%d: MaxLength mismatch: %q != %q", filename, report, ss, expect[:64]) |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// getLine reads a line from demangle-expected. |
| 165 |
func getLine(t testing.TB, scanner *bufio.Scanner, lineno *int) string { |
| 166 |
s, got := getOptLine(t, scanner, lineno) |
| 167 |
if !got { |
| 168 |
t.Fatalf("%s:%d: unexpected EOF", filename, *lineno) |
| 169 |
} |
| 170 |
return s |
| 171 |
} |
| 172 |
|
| 173 |
// getOptLine reads an optional line from demangle-expected, returning |
| 174 |
// false at EOF. It skips comment lines and updates *lineno. |
| 175 |
func getOptLine(t testing.TB, scanner *bufio.Scanner, lineno *int) (string, bool) { |
| 176 |
for { |
| 177 |
if !scanner.Scan() { |
| 178 |
return "", false |
| 179 |
} |
| 180 |
*lineno++ |
| 181 |
line := scanner.Text() |
| 182 |
if !strings.HasPrefix(line, "#") { |
| 183 |
return line, true |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
// skipExpectedTest reports whether to skip a test in demangle-expected. |
| 189 |
// It also returns whether the test has a no-parameter lines. |
| 190 |
func skipExpectedTest(t testing.TB, format, input string, report int) (skip, testNoParams bool) { |
| 191 |
if len(format) > 0 && format[0] == '-' { |
| 192 |
for _, arg := range strings.Fields(format) { |
| 193 |
switch arg { |
| 194 |
case "--format=gnu-v3": |
| 195 |
case "--format=auto": |
| 196 |
if !strings.HasPrefix(input, "_Z") { |
| 197 |
skip = true |
| 198 |
} |
| 199 |
case "--no-params": |
| 200 |
testNoParams = true |
| 201 |
case "--ret-postfix", "--ret-drop": |
| 202 |
skip = true |
| 203 |
case "--is-v3-ctor", "--is-v3-dtor": |
| 204 |
skip = true |
| 205 |
default: |
| 206 |
if !strings.HasPrefix(arg, "--format=") { |
| 207 |
t.Errorf("%s:%d: unrecognized argument %s", filename, report, arg) |
| 208 |
} |
| 209 |
skip = true |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
// The libiberty testsuite passes DMGL_TYPES to |
| 215 |
// demangle type names, but that doesn't seem useful |
| 216 |
// and we don't support it. |
| 217 |
if !strings.HasPrefix(input, "_Z") && !strings.HasPrefix(input, "_GLOBAL_") { |
| 218 |
skip = true |
| 219 |
} |
| 220 |
|
| 221 |
return skip, testNoParams |
| 222 |
} |