You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

82 lines
2.5 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package doc
  7. import (
  8. "fmt"
  9. "go/ast"
  10. "go/token"
  11. "strconv"
  12. "strings"
  13. "github.com/golang/gddo/gosrc"
  14. )
  15. // This list of deprecated exports is used to find code that has not been
  16. // updated for Go 1.
  17. var deprecatedExports = map[string][]string{
  18. `"bytes"`: {"Add"},
  19. `"crypto/aes"`: {"Cipher"},
  20. `"crypto/hmac"`: {"NewSHA1", "NewSHA256"},
  21. `"crypto/rand"`: {"Seed"},
  22. `"encoding/json"`: {"MarshalForHTML"},
  23. `"encoding/xml"`: {"Marshaler", "NewParser", "Parser"},
  24. `"html"`: {"NewTokenizer", "Parse"},
  25. `"image"`: {"Color", "NRGBAColor", "RGBAColor"},
  26. `"io"`: {"Copyn"},
  27. `"log"`: {"Exitf"},
  28. `"math"`: {"Fabs", "Fmax", "Fmod"},
  29. `"os"`: {"Envs", "Error", "Getenverror", "NewError", "Time", "UnixSignal", "Wait"},
  30. `"reflect"`: {"MapValue", "Typeof"},
  31. `"runtime"`: {"UpdateMemStats"},
  32. `"strconv"`: {"Atob", "Atof32", "Atof64", "AtofN", "Atoi64", "Atoui", "Atoui64", "Btoui64", "Ftoa64", "Itoa64", "Uitoa", "Uitoa64"},
  33. `"time"`: {"LocalTime", "Nanoseconds", "NanosecondsToLocalTime", "Seconds", "SecondsToLocalTime", "SecondsToUTC"},
  34. `"unicode/utf8"`: {"NewString"},
  35. }
  36. type vetVisitor struct {
  37. errors map[string]token.Pos
  38. }
  39. func (v *vetVisitor) Visit(n ast.Node) ast.Visitor {
  40. if sel, ok := n.(*ast.SelectorExpr); ok {
  41. if x, _ := sel.X.(*ast.Ident); x != nil {
  42. if obj := x.Obj; obj != nil && obj.Kind == ast.Pkg {
  43. if spec, _ := obj.Decl.(*ast.ImportSpec); spec != nil {
  44. for _, name := range deprecatedExports[spec.Path.Value] {
  45. if name == sel.Sel.Name {
  46. v.errors[fmt.Sprintf("%s.%s not found", spec.Path.Value, sel.Sel.Name)] = n.Pos()
  47. return nil
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. return v
  55. }
  56. func (b *builder) vetPackage(pkg *Package, apkg *ast.Package) {
  57. errors := make(map[string]token.Pos)
  58. for _, file := range apkg.Files {
  59. for _, is := range file.Imports {
  60. importPath, _ := strconv.Unquote(is.Path.Value)
  61. if !gosrc.IsValidPath(importPath) &&
  62. !strings.HasPrefix(importPath, "exp/") &&
  63. !strings.HasPrefix(importPath, "appengine") {
  64. errors[fmt.Sprintf("Unrecognized import path %q", importPath)] = is.Pos()
  65. }
  66. }
  67. v := vetVisitor{errors: errors}
  68. ast.Walk(&v, file)
  69. }
  70. for message, pos := range errors {
  71. pkg.Errors = append(pkg.Errors,
  72. fmt.Sprintf("%s (%s)", message, b.fset.Position(pos)))
  73. }
  74. }