Files
tailscale/util/codegen/codegen.go
Will Norris 3ec5be3f51 all: remove AUTHORS file and references to it
This file was never truly necessary and has never actually been used in
the history of Tailscale's open source releases.

A Brief History of AUTHORS files
---

The AUTHORS file was a pattern developed at Google, originally for
Chromium, then adopted by Go and a bunch of other projects. The problem
was that Chromium originally had a copyright line only recognizing
Google as the copyright holder. Because Google (and most open source
projects) do not require copyright assignemnt for contributions, each
contributor maintains their copyright. Some large corporate contributors
then tried to add their own name to the copyright line in the LICENSE
file or in file headers. This quickly becomes unwieldy, and puts a
tremendous burden on anyone building on top of Chromium, since the
license requires that they keep all copyright lines intact.

The compromise was to create an AUTHORS file that would list all of the
copyright holders. The LICENSE file and source file headers would then
include that list by reference, listing the copyright holder as "The
Chromium Authors".

This also become cumbersome to simply keep the file up to date with a
high rate of new contributors. Plus it's not always obvious who the
copyright holder is. Sometimes it is the individual making the
contribution, but many times it may be their employer. There is no way
for the proejct maintainer to know.

Eventually, Google changed their policy to no longer recommend trying to
keep the AUTHORS file up to date proactively, and instead to only add to
it when requested: https://opensource.google/docs/releasing/authors.
They are also clear that:

> Adding contributors to the AUTHORS file is entirely within the
> project's discretion and has no implications for copyright ownership.

It was primarily added to appease a small number of large contributors
that insisted that they be recognized as copyright holders (which was
entirely their right to do). But it's not truly necessary, and not even
the most accurate way of identifying contributors and/or copyright
holders.

In practice, we've never added anyone to our AUTHORS file. It only lists
Tailscale, so it's not really serving any purpose. It also causes
confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header
in other open source repos which don't actually have an AUTHORS file, so
it's ambiguous what that means.

Instead, we just acknowledge that the contributors to Tailscale (whoever
they are) are copyright holders for their individual contributions. We
also have the benefit of using the DCO (developercertificate.org) which
provides some additional certification of their right to make the
contribution.

The source file changes were purely mechanical with:

    git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g'

Updates #cleanup

Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d
Signed-off-by: Will Norris <will@tailscale.com>
2026-01-23 15:49:45 -08:00

415 lines
11 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
// Package codegen contains shared utilities for generating code.
package codegen
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/token"
"go/types"
"io"
"os"
"reflect"
"strings"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/imports"
"tailscale.com/util/mak"
)
var flagCopyright = flag.Bool("copyright", true, "add Tailscale copyright to generated file headers")
// LoadTypes returns all named types in pkgName, keyed by their type name.
func LoadTypes(buildTags string, pkgName string) (*packages.Package, map[string]types.Type, error) {
cfg := &packages.Config{
Mode: packages.NeedTypes | packages.NeedTypesInfo | packages.NeedSyntax | packages.NeedName,
Tests: buildTags == "test",
}
if buildTags != "" && !cfg.Tests {
cfg.BuildFlags = []string{"-tags=" + buildTags}
}
pkgs, err := packages.Load(cfg, pkgName)
if err != nil {
return nil, nil, err
}
if cfg.Tests {
pkgs = testPackages(pkgs)
}
if len(pkgs) != 1 {
return nil, nil, fmt.Errorf("wrong number of packages: %d", len(pkgs))
}
pkg := pkgs[0]
return pkg, namedTypes(pkg), nil
}
func testPackages(pkgs []*packages.Package) []*packages.Package {
var testPackages []*packages.Package
for _, pkg := range pkgs {
testPackageID := fmt.Sprintf("%[1]s [%[1]s.test]", pkg.PkgPath)
if pkg.ID == testPackageID {
testPackages = append(testPackages, pkg)
}
}
return testPackages
}
// HasNoClone reports whether the provided tag has `codegen:noclone`.
func HasNoClone(structTag string) bool {
val := reflect.StructTag(structTag).Get("codegen")
for _, v := range strings.Split(val, ",") {
if v == "noclone" {
return true
}
}
return false
}
const copyrightHeader = `// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
`
const genAndPackageHeader = `// Code generated by %v; DO NOT EDIT.
package %s
`
func NewImportTracker(thisPkg *types.Package) *ImportTracker {
return &ImportTracker{
thisPkg: thisPkg,
}
}
type namePkgPath struct {
name string // optional import name
pkgPath string
}
// ImportTracker provides a mechanism to track and build import paths.
type ImportTracker struct {
thisPkg *types.Package
packages map[namePkgPath]bool
}
// Import imports pkgPath under an optional import name.
func (it *ImportTracker) Import(name, pkgPath string) {
if pkgPath != "" && !it.packages[namePkgPath{name, pkgPath}] {
mak.Set(&it.packages, namePkgPath{name, pkgPath}, true)
}
}
// Has reports whether the specified package path has been imported
// under the particular import name.
func (it *ImportTracker) Has(name, pkgPath string) bool {
return it.packages[namePkgPath{name, pkgPath}]
}
func (it *ImportTracker) qualifier(pkg *types.Package) string {
if it.thisPkg == pkg {
return ""
}
it.Import("", pkg.Path())
// TODO(maisem): handle conflicts?
return pkg.Name()
}
// QualifiedName returns the string representation of t in the package.
func (it *ImportTracker) QualifiedName(t types.Type) string {
return types.TypeString(t, it.qualifier)
}
// PackagePrefix returns the prefix to be used when referencing named objects from pkg.
func (it *ImportTracker) PackagePrefix(pkg *types.Package) string {
if s := it.qualifier(pkg); s != "" {
return s + "."
}
return ""
}
// Write prints all the tracked imports in a single import block to w.
func (it *ImportTracker) Write(w io.Writer) {
fmt.Fprintf(w, "import (\n")
for s := range it.packages {
if s.name == "" {
fmt.Fprintf(w, "\t%q\n", s.pkgPath)
} else {
fmt.Fprintf(w, "\t%s %q\n", s.name, s.pkgPath)
}
}
fmt.Fprintf(w, ")\n\n")
}
func writeHeader(w io.Writer, tool, pkg string) {
if *flagCopyright {
fmt.Fprint(w, copyrightHeader)
}
fmt.Fprintf(w, genAndPackageHeader, tool, pkg)
}
// WritePackageFile adds a file with the provided imports and contents to package.
// The tool param is used to identify the tool that generated package file.
func WritePackageFile(tool string, pkg *packages.Package, path string, it *ImportTracker, contents *bytes.Buffer) error {
buf := new(bytes.Buffer)
writeHeader(buf, tool, pkg.Name)
it.Write(buf)
if _, err := buf.Write(contents.Bytes()); err != nil {
return err
}
return writeFormatted(buf.Bytes(), path)
}
// writeFormatted writes code to path.
// It runs gofmt on it before writing;
// if gofmt fails, it writes code unchanged.
// Errors can include I/O errors and gofmt errors.
//
// The advantage of always writing code to path,
// even if gofmt fails, is that it makes debugging easier.
// The code can be long, but you need it in order to debug.
// It is nicer to work with it in a file than a terminal.
// It is also easier to interpret gofmt errors
// with an editor providing file and line numbers.
func writeFormatted(code []byte, path string) error {
out, fmterr := imports.Process(path, code, &imports.Options{
Comments: true,
TabIndent: true,
TabWidth: 8,
FormatOnly: true, // fancy gofmt only
})
if fmterr != nil {
out = code
}
ioerr := os.WriteFile(path, out, 0644)
// Prefer I/O errors. They're usually easier to fix,
// and until they're fixed you can't do much else.
if ioerr != nil {
return ioerr
}
if fmterr != nil {
return fmt.Errorf("%s:%v", path, fmterr)
}
return nil
}
// namedTypes returns all named types in pkg, keyed by their type name.
func namedTypes(pkg *packages.Package) map[string]types.Type {
nt := make(map[string]types.Type)
for _, file := range pkg.Syntax {
for _, d := range file.Decls {
decl, ok := d.(*ast.GenDecl)
if !ok || decl.Tok != token.TYPE {
continue
}
for _, s := range decl.Specs {
spec, ok := s.(*ast.TypeSpec)
if !ok {
continue
}
typeNameObj, ok := pkg.TypesInfo.Defs[spec.Name]
if !ok {
continue
}
switch typ := typeNameObj.Type(); typ.(type) {
case *types.Alias, *types.Named:
nt[spec.Name.Name] = typ
}
}
}
}
return nt
}
// AssertStructUnchanged generates code that asserts at compile time that type t is unchanged.
// thisPkg is the package containing t.
// tname is the named type corresponding to t.
// ctx is a single-word context for this assertion, such as "Clone".
// If non-nil, AssertStructUnchanged will add elements to imports
// for each package path that the caller must import for the returned code to compile.
func AssertStructUnchanged(t *types.Struct, tname string, params *types.TypeParamList, ctx string, it *ImportTracker) []byte {
buf := new(bytes.Buffer)
w := func(format string, args ...any) {
fmt.Fprintf(buf, format+"\n", args...)
}
w("// A compilation failure here means this code must be regenerated, with the command at the top of this file.")
hasTypeParams := params != nil && params.Len() > 0
if hasTypeParams {
constraints, identifiers := FormatTypeParams(params, it)
w("func _%s%sNeedsRegeneration%s (%s%s) {", tname, ctx, constraints, tname, identifiers)
w("_%s%sNeedsRegeneration(struct {", tname, ctx)
} else {
w("var _%s%sNeedsRegeneration = %s(struct {", tname, ctx, tname)
}
for i := range t.NumFields() {
st := t.Field(i)
fname := st.Name()
ft := t.Field(i).Type()
if IsInvalid(ft) {
continue
}
qname := it.QualifiedName(ft)
var tag string
if hasTypeParams {
tag = t.Tag(i)
if tag != "" {
tag = "`" + tag + "`"
}
}
if st.Anonymous() {
w("\t%s %s", qname, tag)
} else {
w("\t%s %s %s", fname, qname, tag)
}
}
if hasTypeParams {
w("}{})\n}")
} else {
w("}{})")
}
return buf.Bytes()
}
// IsInvalid reports whether the provided type is invalid. It is used to allow
// codegeneration to run even when the target files have build errors or are
// missing views.
func IsInvalid(t types.Type) bool {
return t.String() == "invalid type"
}
// ContainsPointers reports whether typ contains any pointers,
// either explicitly or implicitly.
// It has special handling for some types that contain pointers
// that we know are free from memory aliasing/mutation concerns.
func ContainsPointers(typ types.Type) bool {
s := typ.String()
switch s {
case "time.Time":
// time.Time contains a pointer that does not need cloning.
return false
case "inet.af/netip.Addr":
return false
}
if strings.HasPrefix(s, "unique.Handle[") {
// unique.Handle contains a pointer that does not need cloning.
return false
}
switch ft := typ.Underlying().(type) {
case *types.Array:
return ContainsPointers(ft.Elem())
case *types.Basic:
if ft.Kind() == types.UnsafePointer {
return true
}
case *types.Chan:
return true
case *types.Interface:
if ft.Empty() || ft.IsMethodSet() {
return true
}
for i := 0; i < ft.NumEmbeddeds(); i++ {
if ContainsPointers(ft.EmbeddedType(i)) {
return true
}
}
case *types.Map:
return true
case *types.Pointer:
return true
case *types.Slice:
return true
case *types.Struct:
for i := range ft.NumFields() {
if ContainsPointers(ft.Field(i).Type()) {
return true
}
}
case *types.Union:
for i := range ft.Len() {
if ContainsPointers(ft.Term(i).Type()) {
return true
}
}
}
return false
}
// IsViewType reports whether the provided typ is a View.
func IsViewType(typ types.Type) bool {
t, ok := typ.Underlying().(*types.Struct)
if !ok {
return false
}
if t.NumFields() != 1 {
return false
}
return t.Field(0).Name() == "ж"
}
// FormatTypeParams formats the specified params and returns two strings:
// - constraints are comma-separated type parameters and their constraints in square brackets (e.g. [T any, V constraints.Integer])
// - names are comma-separated type parameter names in square brackets (e.g. [T, V])
//
// If params is nil or empty, both return values are empty strings.
func FormatTypeParams(params *types.TypeParamList, it *ImportTracker) (constraints, names string) {
if params == nil || params.Len() == 0 {
return "", ""
}
var constraintList, nameList []string
for i := range params.Len() {
param := params.At(i)
name := param.Obj().Name()
constraint := it.QualifiedName(param.Constraint())
nameList = append(nameList, name)
constraintList = append(constraintList, name+" "+constraint)
}
constraints = "[" + strings.Join(constraintList, ", ") + "]"
names = "[" + strings.Join(nameList, ", ") + "]"
return constraints, names
}
// LookupMethod returns the method with the specified name in t, or nil if the method does not exist.
func LookupMethod(t types.Type, name string) *types.Func {
switch t := t.(type) {
case *types.Alias:
return LookupMethod(t.Rhs(), name)
case *types.TypeParam:
return LookupMethod(t.Constraint(), name)
case *types.Pointer:
return LookupMethod(t.Elem(), name)
case *types.Named:
switch u := t.Underlying().(type) {
case *types.Interface:
return LookupMethod(u, name)
default:
for i := 0; i < t.NumMethods(); i++ {
if method := t.Method(i); method.Name() == name {
return method
}
}
}
case *types.Interface:
for i := 0; i < t.NumMethods(); i++ {
if method := t.Method(i); method.Name() == name {
return method
}
}
}
return nil
}
// NamedTypeOf is like t.(*types.Named), but also works with type aliases.
func NamedTypeOf(t types.Type) (named *types.Named, ok bool) {
if a, ok := t.(*types.Alias); ok {
return NamedTypeOf(types.Unalias(a))
}
named, ok = t.(*types.Named)
return
}