codegen.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright (c) 2021 Tailscale Inc & 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. // Package codegen contains shared utilities for generating code.
  5. package codegen
  6. import (
  7. "bytes"
  8. "fmt"
  9. "go/ast"
  10. "go/token"
  11. "go/types"
  12. "io"
  13. "os"
  14. "reflect"
  15. "strings"
  16. "time"
  17. "golang.org/x/tools/go/packages"
  18. "golang.org/x/tools/imports"
  19. "tailscale.com/util/mak"
  20. )
  21. // LoadTypes returns all named types in pkgName, keyed by their type name.
  22. func LoadTypes(buildTags string, pkgName string) (*packages.Package, map[string]*types.Named, error) {
  23. cfg := &packages.Config{
  24. Mode: packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax | packages.NeedName,
  25. Tests: false,
  26. }
  27. if buildTags != "" {
  28. cfg.BuildFlags = []string{"-tags=" + buildTags}
  29. }
  30. pkgs, err := packages.Load(cfg, pkgName)
  31. if err != nil {
  32. return nil, nil, err
  33. }
  34. if len(pkgs) != 1 {
  35. return nil, nil, fmt.Errorf("wrong number of packages: %d", len(pkgs))
  36. }
  37. pkg := pkgs[0]
  38. return pkg, namedTypes(pkg), nil
  39. }
  40. // HasNoClone reports whether the provided tag has `codegen:noclone`.
  41. func HasNoClone(structTag string) bool {
  42. val := reflect.StructTag(structTag).Get("codegen")
  43. for _, v := range strings.Split(val, ",") {
  44. if v == "noclone" {
  45. return true
  46. }
  47. }
  48. return false
  49. }
  50. const header = `// Copyright (c) %d Tailscale Inc & AUTHORS All rights reserved.
  51. // Use of this source code is governed by a BSD-style
  52. // license that can be found in the LICENSE file.
  53. // Code generated by %v; DO NOT EDIT.
  54. package %s
  55. `
  56. func NewImportTracker(thisPkg *types.Package) *ImportTracker {
  57. return &ImportTracker{
  58. thisPkg: thisPkg,
  59. }
  60. }
  61. // ImportTracker provides a mechanism to track and build import paths.
  62. type ImportTracker struct {
  63. thisPkg *types.Package
  64. packages map[string]bool
  65. }
  66. func (it *ImportTracker) Import(pkg string) {
  67. if pkg != "" && !it.packages[pkg] {
  68. mak.Set(&it.packages, pkg, true)
  69. }
  70. }
  71. func (it *ImportTracker) qualifier(pkg *types.Package) string {
  72. if it.thisPkg == pkg {
  73. return ""
  74. }
  75. it.Import(pkg.Path())
  76. // TODO(maisem): handle conflicts?
  77. return pkg.Name()
  78. }
  79. // QualifiedName returns the string representation of t in the package.
  80. func (it *ImportTracker) QualifiedName(t types.Type) string {
  81. return types.TypeString(t, it.qualifier)
  82. }
  83. // Write prints all the tracked imports in a single import block to w.
  84. func (it *ImportTracker) Write(w io.Writer) {
  85. fmt.Fprintf(w, "import (\n")
  86. for s := range it.packages {
  87. fmt.Fprintf(w, "\t%q\n", s)
  88. }
  89. fmt.Fprintf(w, ")\n\n")
  90. }
  91. func writeHeader(w io.Writer, tool, pkg string) {
  92. fmt.Fprintf(w, header, time.Now().Year(), tool, pkg)
  93. }
  94. // WritePackageFile adds a file with the provided imports and contents to package.
  95. // The tool param is used to identify the tool that generated package file.
  96. func WritePackageFile(tool string, pkg *packages.Package, path string, it *ImportTracker, contents *bytes.Buffer) error {
  97. buf := new(bytes.Buffer)
  98. writeHeader(buf, tool, pkg.Name)
  99. it.Write(buf)
  100. if _, err := buf.Write(contents.Bytes()); err != nil {
  101. return err
  102. }
  103. return writeFormatted(buf.Bytes(), path)
  104. }
  105. // writeFormatted writes code to path.
  106. // It runs gofmt on it before writing;
  107. // if gofmt fails, it writes code unchanged.
  108. // Errors can include I/O errors and gofmt errors.
  109. //
  110. // The advantage of always writing code to path,
  111. // even if gofmt fails, is that it makes debugging easier.
  112. // The code can be long, but you need it in order to debug.
  113. // It is nicer to work with it in a file than a terminal.
  114. // It is also easier to interpret gofmt errors
  115. // with an editor providing file and line numbers.
  116. func writeFormatted(code []byte, path string) error {
  117. out, fmterr := imports.Process(path, code, &imports.Options{
  118. Comments: true,
  119. TabIndent: true,
  120. TabWidth: 8,
  121. FormatOnly: true, // fancy gofmt only
  122. })
  123. if fmterr != nil {
  124. out = code
  125. }
  126. ioerr := os.WriteFile(path, out, 0644)
  127. // Prefer I/O errors. They're usually easier to fix,
  128. // and until they're fixed you can't do much else.
  129. if ioerr != nil {
  130. return ioerr
  131. }
  132. if fmterr != nil {
  133. return fmt.Errorf("%s:%v", path, fmterr)
  134. }
  135. return nil
  136. }
  137. // namedTypes returns all named types in pkg, keyed by their type name.
  138. func namedTypes(pkg *packages.Package) map[string]*types.Named {
  139. nt := make(map[string]*types.Named)
  140. for _, file := range pkg.Syntax {
  141. for _, d := range file.Decls {
  142. decl, ok := d.(*ast.GenDecl)
  143. if !ok || decl.Tok != token.TYPE {
  144. continue
  145. }
  146. for _, s := range decl.Specs {
  147. spec, ok := s.(*ast.TypeSpec)
  148. if !ok {
  149. continue
  150. }
  151. typeNameObj, ok := pkg.TypesInfo.Defs[spec.Name]
  152. if !ok {
  153. continue
  154. }
  155. typ, ok := typeNameObj.Type().(*types.Named)
  156. if !ok {
  157. continue
  158. }
  159. nt[spec.Name.Name] = typ
  160. }
  161. }
  162. }
  163. return nt
  164. }
  165. // AssertStructUnchanged generates code that asserts at compile time that type t is unchanged.
  166. // thisPkg is the package containing t.
  167. // tname is the named type corresponding to t.
  168. // ctx is a single-word context for this assertion, such as "Clone".
  169. // If non-nil, AssertStructUnchanged will add elements to imports
  170. // for each package path that the caller must import for the returned code to compile.
  171. func AssertStructUnchanged(t *types.Struct, tname, ctx string, it *ImportTracker) []byte {
  172. buf := new(bytes.Buffer)
  173. w := func(format string, args ...any) {
  174. fmt.Fprintf(buf, format+"\n", args...)
  175. }
  176. w("// A compilation failure here means this code must be regenerated, with the command at the top of this file.")
  177. w("var _%s%sNeedsRegeneration = %s(struct {", tname, ctx, tname)
  178. for i := 0; i < t.NumFields(); i++ {
  179. fname := t.Field(i).Name()
  180. ft := t.Field(i).Type()
  181. if IsInvalid(ft) {
  182. continue
  183. }
  184. qname := it.QualifiedName(ft)
  185. w("\t%s %s", fname, qname)
  186. }
  187. w("}{})\n")
  188. return buf.Bytes()
  189. }
  190. // IsInvalid reports whether the provided type is invalid. It is used to allow
  191. // codegeneration to run even when the target files have build errors or are
  192. // missing views.
  193. func IsInvalid(t types.Type) bool {
  194. return t.String() == "invalid type"
  195. }
  196. // ContainsPointers reports whether typ contains any pointers,
  197. // either explicitly or implicitly.
  198. // It has special handling for some types that contain pointers
  199. // that we know are free from memory aliasing/mutation concerns.
  200. func ContainsPointers(typ types.Type) bool {
  201. switch typ.String() {
  202. case "time.Time":
  203. // time.Time contains a pointer that does not need copying
  204. return false
  205. case "inet.af/netip.Addr", "net/netip.Addr", "net/netip.Prefix", "net/netip.AddrPort":
  206. return false
  207. }
  208. switch ft := typ.Underlying().(type) {
  209. case *types.Array:
  210. return ContainsPointers(ft.Elem())
  211. case *types.Chan:
  212. return true
  213. case *types.Interface:
  214. return true // a little too broad
  215. case *types.Map:
  216. return true
  217. case *types.Pointer:
  218. return true
  219. case *types.Slice:
  220. return true
  221. case *types.Struct:
  222. for i := 0; i < ft.NumFields(); i++ {
  223. if ContainsPointers(ft.Field(i).Type()) {
  224. return true
  225. }
  226. }
  227. }
  228. return false
  229. }
  230. // IsViewType reports whether the provided typ is a View.
  231. func IsViewType(typ types.Type) bool {
  232. t, ok := typ.Underlying().(*types.Struct)
  233. if !ok {
  234. return false
  235. }
  236. if t.NumFields() != 1 {
  237. return false
  238. }
  239. return t.Field(0).Name() == "ж"
  240. }