argposition_test.go 3.0 KB

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