]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/cgo/internal/testerrors/argposition_test.go
runtime: remove crash_cgo_test CgoRaceSignal timeout
[gostls13.git] / src / cmd / cgo / internal / testerrors / argposition_test.go
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 // Issue 42580: cmd/cgo: shifting identifier position in ast
6
7 package errorstest
8
9 import (
10         "bytes"
11         "fmt"
12         "go/ast"
13         "go/parser"
14         "go/token"
15         "os"
16         "os/exec"
17         "path/filepath"
18         "strings"
19         "testing"
20 )
21
22 type ShortPosition struct {
23         Line    int
24         Column  int
25         Visited bool
26 }
27
28 type IdentPositionInfo map[string][]ShortPosition
29
30 type Visitor struct {
31         identPosInfo IdentPositionInfo
32         fset         *token.FileSet
33         t            *testing.T
34 }
35
36 func (v *Visitor) Visit(node ast.Node) ast.Visitor {
37         if ident, ok := node.(*ast.Ident); ok {
38                 if expectedPositions, ok := v.identPosInfo[ident.Name]; ok {
39                         gotMatch := false
40                         var errorMessage strings.Builder
41                         for caseIndex, expectedPos := range expectedPositions {
42                                 actualPosition := v.fset.PositionFor(ident.Pos(), true)
43                                 errorOccured := false
44                                 if expectedPos.Line != actualPosition.Line {
45                                         fmt.Fprintf(&errorMessage, "wrong line number for ident %s: expected: %d got: %d\n", ident.Name, expectedPos.Line, actualPosition.Line)
46                                         errorOccured = true
47                                 }
48                                 if expectedPos.Column != actualPosition.Column {
49                                         fmt.Fprintf(&errorMessage, "wrong column number for ident %s: expected: %d got: %d\n", ident.Name, expectedPos.Column, actualPosition.Column)
50                                         errorOccured = true
51                                 }
52                                 if errorOccured {
53                                         continue
54                                 }
55                                 gotMatch = true
56                                 expectedPositions[caseIndex].Visited = true
57                         }
58
59                         if !gotMatch {
60                                 v.t.Errorf(errorMessage.String())
61                         }
62                 }
63         }
64         return v
65 }
66
67 func TestArgumentsPositions(t *testing.T) {
68         testdata, err := filepath.Abs("testdata")
69         if err != nil {
70                 t.Fatal(err)
71         }
72
73         tmpPath := t.TempDir()
74
75         dir := filepath.Join(tmpPath, "src", "testpositions")
76         if err := os.MkdirAll(dir, 0755); err != nil {
77                 t.Fatal(err)
78         }
79
80         cmd := exec.Command("go", "tool", "cgo",
81                 "-srcdir", testdata,
82                 "-objdir", dir,
83                 "issue42580.go")
84         cmd.Stderr = new(bytes.Buffer)
85
86         err = cmd.Run()
87         if err != nil {
88                 t.Fatalf("%s: %v\n%s", cmd, err, cmd.Stderr)
89         }
90         mainProcessed, err := os.ReadFile(filepath.Join(dir, "issue42580.cgo1.go"))
91         if err != nil {
92                 t.Fatal(err)
93         }
94         fset := token.NewFileSet()
95         f, err := parser.ParseFile(fset, "", mainProcessed, parser.AllErrors)
96         if err != nil {
97                 fmt.Println(err)
98                 return
99         }
100
101         expectation := IdentPositionInfo{
102                 "checkedPointer": []ShortPosition{
103                         ShortPosition{
104                                 Line:   32,
105                                 Column: 56,
106                         },
107                 },
108                 "singleInnerPointerChecked": []ShortPosition{
109                         ShortPosition{
110                                 Line:   37,
111                                 Column: 91,
112                         },
113                 },
114                 "doublePointerChecked": []ShortPosition{
115                         ShortPosition{
116                                 Line:   42,
117                                 Column: 91,
118                         },
119                 },
120         }
121         for _, decl := range f.Decls {
122                 if fdecl, ok := decl.(*ast.FuncDecl); ok {
123                         ast.Walk(&Visitor{expectation, fset, t}, fdecl.Body)
124                 }
125         }
126         for ident, positions := range expectation {
127                 for _, position := range positions {
128                         if !position.Visited {
129                                 t.Errorf("Position %d:%d missed for %s ident", position.Line, position.Column, ident)
130                         }
131                 }
132         }
133 }