Debian Codegraph @

cases_test.go

raw
1 // Copyright 2021 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 "io"
10 "os"
11 "strings"
12 "testing"
13 )
14
15 // casesExpectedFailures is a list of exceptions from cases that we
16 // don't expect to pass. This is to let us incrementally fix them
17 // over time.
18 var casesExpectedFailures = map[string]bool{
19 "_ZSteqIcEN9__gnu_cxx11__enable_ifIXsr9__is_charIT_EE7__valueEbE6__typeERKSbIS2_St11char_traitsIS2_ESaIS2_EESA_": true,
20 "_Z1fPU11objcproto1A11objc_object": true,
21 "_Z1fPKU11objcproto1A7NSArray": true,
22 "_ZNK1AIJ1Z1Y1XEEcv1BIJDpPT_EEIJS2_S1_S0_EEEv": true,
23 "_ZNK3Ncr6Silver7Utility6detail12CallOnThreadIZ53-[DeploymentSetupController handleManualServerEntry:]E3$_5EclIJEEEDTclclL_ZNS2_4getTIS4_EERT_vEEspclsr3stdE7forwardIT_Efp_EEEDpOSA_": true,
24 "_Z1fIJicEEvDp7MuncherIAstT__S1_E": true,
25 "_ZN5test31aINS_1XEMS1_PiEEvT_T0_DTdsfL0p_fL0p0_E": true,
26 "_Z1fPU3AS1KiS0_": true,
27 "_Z1pILb1EEiM1SKDOT_EFivRE": true,
28 "_Z1pIJicfEEiM1SVKDwDpT_EFivOE": true,
29 "_ZZ18test_assign_throwsI20small_throws_on_copyLb0EEvvENKUlRNSt3__13anyEOT_E_clIRS0_EEDaS3_S5_": true,
30 "_ZN1Scv7MuncherIJDpPT_EEIJFivEA_iEEEv": true,
31 "_Z2f8IiJ8identityIiES0_IfEEEvRAsPiDpT0_T_DpNS3_4typeEE_i": true,
32 "_ZZ11inline_funcvENKUlTyTyT_T0_E_clIiiEEDaS_S0_": true,
33 "_ZZ11inline_funcvENKUlTyTyT_T1_T0_E_clIiiiEEDaS_S0_S1_": true,
34
35 "_Z1fIXfLpm1x1yEEvv": true,
36 "_Z1fIXfLds1x1yEEvv": true,
37 }
38
39 // caseExceptions is a list of exceptions from the LLVM list that we
40 // do not handle the same as the LLVM demangler. We keep a list of
41 // exceptions so that we can use an exact copy of the test cases. We
42 // map to an empty string if we expect a demangling failure; this
43 // differs from caseExpectedFailures in that we've decided that we
44 // intentionally should not demangle this case. Otherwise this maps
45 // to the expected demangling.
46 var casesExceptions = map[string]string{
47 "_ZN1XIZ1fIiEvOT_EUlOT_DpT0_E_EclIJEEEvDpT_": "void X<void f<int>(int&&)::'lambda'(auto&&, auto)>::operator()<>()",
48 "_ZN1XIZ1fIiEvOT_EUlS2_DpT0_E_EclIJEEEvDpT_": "void X<void f<int>(int&&)::'lambda'(auto&&, auto)>::operator()<>()",
49 "_Z1h1XIJZ1fIiEDaOT_E1AZ1gIdEDaS2_E1BEE": "h(X<auto f<int>(int&&)::A, auto g<double>(double&&)::B>)",
50 "_Zcv1BIRT_EIS1_E": "",
51
52 // For the next four test cases it seems to me that
53 // <int, int>(int) is more correct than <int, int>(auto).
54 // It depends on how we handle a template parameter in
55 // and out of a lambda expression.
56 "_ZZN5test71fIiEEvvENKUlTyQaa1CIT_E1CITL0__ET0_E_clIiiEEDaS3_Q1CIDtfp_EE": "auto void test7::f<int>()::'lambda'<typename $T> requires C<T> && C<TL0_> (auto)::operator()<int, int>(int) const requires C<decltype(fp)>",
57 "_ZZN5test71fIiEEvvENKUlTyQaa1CIT_E1CITL0__ET0_E0_clIiiEEDaS3_Qaa1CIDtfp_EELb1E": "auto void test7::f<int>()::'lambda0'<typename $T> requires C<T> && C<TL0_> (auto)::operator()<int, int>(int) const requires C<decltype(fp)> && true",
58 "_ZZN5test71fIiEEvvENKUlTyQaa1CIT_E1CITL0__ET0_E1_clIiiEEDaS3_Q1CIDtfp_EE": "auto void test7::f<int>()::'lambda1'<typename $T> requires C<T> && C<TL0_> (auto)::operator()<int, int>(int) const requires C<decltype(fp)>",
59 "_ZZN5test71fIiEEvvENKUlTyT0_E_clIiiEEDaS1_": "auto void test7::f<int>()::'lambda'<typename $T>(auto)::operator()<int, int>(int) const",
60 }
61
62 func TestCases(t *testing.T) {
63 t.Parallel()
64
65 cases := readCases(t)
66
67 expectedErrors := 0
68 expectedDifferent := 0
69 found := make(map[string]bool)
70 for _, test := range cases {
71 expectedFail := casesExpectedFailures[test[0]]
72 exception, haveException := casesExceptions[test[0]]
73 if expectedFail && haveException {
74 t.Errorf("test case error: %s in both expectedFailures and exceptions", test[0])
75 }
76 want := test[1]
77 if haveException && exception != "" {
78 if want == exception {
79 t.Errorf("test case error: %s exception is expected result", test[0])
80 }
81 want = exception
82 }
83
84 // We don't strip an extra underscore.
85 testStr := test[0]
86 if strings.HasPrefix(testStr, "__Z") || strings.HasPrefix(testStr, "____Z") {
87 testStr = testStr[1:]
88 }
89
90 // We don't demangle plain types, so just skip them.
91 if !strings.HasPrefix(testStr, "_") {
92 continue
93 }
94
95 if got, err := ToString(testStr, LLVMStyle); err != nil {
96 if expectedFail || (haveException && exception == "") {
97 t.Logf("demangling %s: expected failure: error %v", test[0], err)
98 if expectedFail {
99 expectedErrors++
100 found[test[0]] = true
101 }
102 } else {
103 t.Errorf("demangling %s: unexpected error %v", test[0], err)
104 }
105 } else if got != want {
106 if expectedFail {
107 t.Logf("demangling %s: expected failure: got %s, want %s", test[0], got, want)
108 expectedDifferent++
109 found[test[0]] = true
110 } else if haveException && exception == "" {
111 t.Errorf("demangling %s: expected to fail, but succeeded with %s", test[0], got)
112 } else {
113 t.Errorf("demangling %s: got %s, want %s", test[0], got, want)
114 }
115 } else if expectedFail || (haveException && exception == "") {
116 t.Errorf("demangling %s: expected to fail, but succeeded with %s", test[0], got)
117 if expectedFail {
118 found[test[0]] = true
119 }
120 }
121 }
122 if len(found) != len(casesExpectedFailures) {
123 for expected := range casesExpectedFailures {
124 if !found[expected] {
125 t.Errorf("expected %s to fail but did not see it", expected)
126 }
127 }
128 for f := range found {
129 if !casesExpectedFailures[f] {
130 t.Errorf("internal error: found failing but unexpected %s", f)
131 }
132 }
133 }
134 if expectedDifferent > 0 {
135 t.Logf("%d different demanglings out of %d cases", expectedDifferent, len(cases))
136 }
137 if expectedErrors > 0 {
138 t.Logf("%d expected failures out of %d cases", expectedErrors, len(cases))
139 }
140 }
141
142 // readCases reads the LLVM test cases from DemangleTestCases.inc.
143 // That file is copied from
144 //
145 // llvm-project/libcxxabi/test/DemangleTestCases.inc
146 //
147 // That file has no license, but LLVM in general has the license text:
148 //
149 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
150 // See https://llvm.org/LICENSE.txt for license information.
151 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
152 //
153 // This file is only used for testing and does not form part of the
154 // demangle package when it is used by other code.
155 //
156 // The file does not contain any code, only test cases and comments.
157 func readCases(t testing.TB) [][2]string {
158 const fn = "testdata/DemangleTestCases.inc"
159 f, err := os.Open(fn)
160 if err != nil {
161 t.Fatal(err)
162 }
163 defer f.Close()
164
165 var cases [][2]string
166 r := bufio.NewReader(f)
167 lineno := 1
168 for {
169 b, atEOF := readCasesUnquotedByte(t, r, fn, &lineno)
170 if atEOF {
171 break
172 }
173 if b != '{' {
174 t.Fatalf("%s:%d: got %c, want {", fn, lineno, b)
175 }
176
177 s1 := readCasesString(t, r, fn, &lineno)
178
179 for {
180 b = readCasesUnquotedByteNoEOF(t, r, fn, &lineno)
181 if b == ',' {
182 break
183 }
184 if b != '"' {
185 t.Fatalf("%s:%d: got %c, want ,", fn, lineno, b)
186 }
187
188 r.UnreadByte()
189 s1 += readCasesString(t, r, fn, &lineno)
190 }
191
192 s2 := readCasesString(t, r, fn, &lineno)
193
194 for {
195 b = readCasesUnquotedByteNoEOF(t, r, fn, &lineno)
196 if b == '}' {
197 break
198 }
199 if b != '"' {
200 t.Fatalf("%s:%d: got %c, want }", fn, lineno, b)
201 }
202
203 r.UnreadByte()
204 s2 += readCasesString(t, r, fn, &lineno)
205 }
206
207 b = readCasesUnquotedByteNoEOF(t, r, fn, &lineno)
208 if b != ',' {
209 t.Fatalf("%s:%d: got %c, want ,", fn, lineno, b)
210 }
211
212 cases = append(cases, [2]string{s1, s2})
213 }
214
215 return cases
216 }
217
218 // readCasesString reads a quoted string from the cases file.
219 func readCasesString(t testing.TB, r *bufio.Reader, fn string, lineno *int) string {
220 b, atEOF := readCasesUnquotedByte(t, r, fn, lineno)
221 if atEOF {
222 t.Fatalf(`%s:%d: got EOF, want "`, fn, *lineno)
223 }
224 if b != '"' {
225 t.Fatalf(`%s:%d: got %c, want "`, fn, *lineno, b)
226 }
227
228 var sb strings.Builder
229 for {
230 b = readCasesByteNoEOF(t, r, fn, *lineno)
231 if b == '"' {
232 break
233 }
234 if b != '\\' {
235 sb.WriteByte(b)
236 continue
237 }
238
239 b = readCasesByteNoEOF(t, r, fn, *lineno)
240 switch b {
241 case '"', '\'', '?', '\\':
242 sb.WriteByte(b)
243 continue
244 case 't':
245 sb.WriteByte('\t')
246 continue
247
248 case '0', '1', '2', '3', '4', '5', '6', '7':
249 val := b - '0'
250 octalLoop:
251 for {
252 b = readCasesByteNoEOF(t, r, fn, *lineno)
253 switch b {
254 case '0', '1', '2', '3', '4', '5', '6', '7':
255 val <<= 3
256 val += b - '0'
257 default:
258 r.UnreadByte()
259 break octalLoop
260 }
261 }
262 sb.WriteByte(val)
263
264 case 'x':
265 val := byte(0)
266 seen := false
267 hexLoop:
268 for {
269 b = readCasesByteNoEOF(t, r, fn, *lineno)
270 bval := byte(0)
271 switch b {
272 case '0', '1', '2', '3', '4', '5', '6', '7':
273 bval = b - '0'
274 case 'A', 'B', 'C', 'D', 'E', 'F':
275 bval = b - 'A'
276 case 'a', 'b', 'c', 'd', 'e', 'f':
277 bval = b - 'a'
278 default:
279 r.UnreadByte()
280 break hexLoop
281 }
282 val <<= 4
283 val += bval
284 seen = true
285 }
286 if !seen {
287 t.Fatalf(`%s:%d: no hex digits after \x`, fn, *lineno)
288 }
289 sb.WriteByte(val)
290
291 default:
292 t.Fatalf(`%s:%d: unexpected escape sequence \%c in string`, fn, *lineno, b)
293 }
294 }
295
296 return sb.String()
297 }
298
299 // readCasesUnquotedByte reads a byte from the cases file,
300 // where the byte is not in a quoted string.
301 // This skips comments and whitespace.
302 // The bool result reports whether we are at EOF.
303 func readCasesUnquotedByte(t testing.TB, r *bufio.Reader, fn string, lineno *int) (byte, bool) {
304 for {
305 b, atEOF := readCasesByte(t, r, fn, *lineno)
306 if atEOF {
307 return 0, true
308 }
309 if b == ' ' || b == '\t' {
310 continue
311 }
312 if b == '\n' {
313 *lineno++
314 continue
315 }
316 if b == '/' {
317 b, atEOF = readCasesByte(t, r, fn, *lineno)
318 if atEOF || b != '/' {
319 t.Fatalf("%s:%d: unexpected single /", fn, *lineno)
320 }
321 for {
322 _, err := r.ReadSlice('\n')
323 if err == nil {
324 *lineno++
325 break
326 }
327 switch err {
328 case bufio.ErrBufferFull:
329 case io.EOF:
330 return 0, true
331 default:
332 t.Fatalf("%s:%d: %v", fn, *lineno, err)
333 }
334 }
335 continue
336 }
337
338 return b, false
339 }
340 }
341
342 // readCasesUnquotedByteNoEOF is like readCasesUnquotedByte,
343 // but fails on EOF.
344 func readCasesUnquotedByteNoEOF(t testing.TB, r *bufio.Reader, fn string, lineno *int) byte {
345 b, atEOF := readCasesUnquotedByte(t, r, fn, lineno)
346 if atEOF {
347 t.Helper()
348 t.Fatalf("%s:%d: unexpected EOF", fn, *lineno)
349 }
350 return b
351 }
352
353 // readCasesByte reads a byte from the cases file.
354 // The bool result reports whether we are at EOF.
355 func readCasesByte(t testing.TB, r *bufio.Reader, fn string, lineno int) (byte, bool) {
356 b, err := r.ReadByte()
357 if err != nil {
358 if err == io.EOF {
359 return 0, true
360 }
361 t.Helper()
362 t.Fatalf("%s:%d: %v", fn, lineno, err)
363 }
364 return b, false
365 }
366
367 // readCasesByteNoEOF is like readCasesByte, but fails on EOF.
368 func readCasesByteNoEOF(t testing.TB, r *bufio.Reader, fn string, lineno int) byte {
369 b, atEOF := readCasesByte(t, r, fn, lineno)
370 if atEOF {
371 t.Helper()
372 t.Fatalf("%s:%d: unexpected EOF", fn, lineno)
373 }
374 return b
375 }