Source file src/go/doc/example.go

Documentation: go/doc

     1  // Copyright 2011 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  // Extract example functions from file ASTs.
     6  
     7  package doc
     8  
     9  import (
    10  	"go/ast"
    11  	"go/token"
    12  	"internal/lazyregexp"
    13  	"path"
    14  	"sort"
    15  	"strconv"
    16  	"strings"
    17  	"unicode"
    18  	"unicode/utf8"
    19  )
    20  
    21  // An Example represents an example function found in a test source file.
    22  type Example struct {
    23  	Name        string // name of the item being exemplified (including optional suffix)
    24  	Suffix      string // example suffix, without leading '_' (only populated by NewFromFiles)
    25  	Doc         string // example function doc string
    26  	Code        ast.Node
    27  	Play        *ast.File // a whole program version of the example
    28  	Comments    []*ast.CommentGroup
    29  	Output      string // expected output
    30  	Unordered   bool
    31  	EmptyOutput bool // expect empty output
    32  	Order       int  // original source code order
    33  }
    34  
    35  // Examples returns the examples found in testFiles, sorted by Name field.
    36  // The Order fields record the order in which the examples were encountered.
    37  // The Suffix field is not populated when Examples is called directly, it is
    38  // only populated by NewFromFiles for examples it finds in _test.go files.
    39  //
    40  // Playable Examples must be in a package whose name ends in "_test".
    41  // An Example is "playable" (the Play field is non-nil) in either of these
    42  // circumstances:
    43  //   - The example function is self-contained: the function references only
    44  //     identifiers from other packages (or predeclared identifiers, such as
    45  //     "int") and the test file does not include a dot import.
    46  //   - The entire test file is the example: the file contains exactly one
    47  //     example function, zero test or benchmark functions, and at least one
    48  //     top-level function, type, variable, or constant declaration other
    49  //     than the example function.
    50  func Examples(testFiles ...*ast.File) []*Example {
    51  	var list []*Example
    52  	for _, file := range testFiles {
    53  		hasTests := false // file contains tests or benchmarks
    54  		numDecl := 0      // number of non-import declarations in the file
    55  		var flist []*Example
    56  		for _, decl := range file.Decls {
    57  			if g, ok := decl.(*ast.GenDecl); ok && g.Tok != token.IMPORT {
    58  				numDecl++
    59  				continue
    60  			}
    61  			f, ok := decl.(*ast.FuncDecl)
    62  			if !ok || f.Recv != nil {
    63  				continue
    64  			}
    65  			numDecl++
    66  			name := f.Name.Name
    67  			if isTest(name, "Test") || isTest(name, "Benchmark") {
    68  				hasTests = true
    69  				continue
    70  			}
    71  			if !isTest(name, "Example") {
    72  				continue
    73  			}
    74  			if params := f.Type.Params; len(params.List) != 0 {
    75  				continue // function has params; not a valid example
    76  			}
    77  			if f.Body == nil { // ast.File.Body nil dereference (see issue 28044)
    78  				continue
    79  			}
    80  			var doc string
    81  			if f.Doc != nil {
    82  				doc = f.Doc.Text()
    83  			}
    84  			output, unordered, hasOutput := exampleOutput(f.Body, file.Comments)
    85  			flist = append(flist, &Example{
    86  				Name:        name[len("Example"):],
    87  				Doc:         doc,
    88  				Code:        f.Body,
    89  				Play:        playExample(file, f),
    90  				Comments:    file.Comments,
    91  				Output:      output,
    92  				Unordered:   unordered,
    93  				EmptyOutput: output == "" && hasOutput,
    94  				Order:       len(flist),
    95  			})
    96  		}
    97  		if !hasTests && numDecl > 1 && len(flist) == 1 {
    98  			// If this file only has one example function, some
    99  			// other top-level declarations, and no tests or
   100  			// benchmarks, use the whole file as the example.
   101  			flist[0].Code = file
   102  			flist[0].Play = playExampleFile(file)
   103  		}
   104  		list = append(list, flist...)
   105  	}
   106  	// sort by name
   107  	sort.Slice(list, func(i, j int) bool {
   108  		return list[i].Name < list[j].Name
   109  	})
   110  	return list
   111  }
   112  
   113  var outputPrefix = lazyregexp.New(`(?i)^[[:space:]]*(unordered )?output:`)
   114  
   115  // Extracts the expected output and whether there was a valid output comment
   116  func exampleOutput(b *ast.BlockStmt, comments []*ast.CommentGroup) (output string, unordered, ok bool) {
   117  	if _, last := lastComment(b, comments); last != nil {
   118  		// test that it begins with the correct prefix
   119  		text := last.Text()
   120  		if loc := outputPrefix.FindStringSubmatchIndex(text); loc != nil {
   121  			if loc[2] != -1 {
   122  				unordered = true
   123  			}
   124  			text = text[loc[1]:]
   125  			// Strip zero or more spaces followed by \n or a single space.
   126  			text = strings.TrimLeft(text, " ")
   127  			if len(text) > 0 && text[0] == '\n' {
   128  				text = text[1:]
   129  			}
   130  			return text, unordered, true
   131  		}
   132  	}
   133  	return "", false, false // no suitable comment found
   134  }
   135  
   136  // isTest tells whether name looks like a test, example, or benchmark.
   137  // It is a Test (say) if there is a character after Test that is not a
   138  // lower-case letter. (We don't want Testiness.)
   139  func isTest(name, prefix string) bool {
   140  	if !strings.HasPrefix(name, prefix) {
   141  		return false
   142  	}
   143  	if len(name) == len(prefix) { // "Test" is ok
   144  		return true
   145  	}
   146  	rune, _ := utf8.DecodeRuneInString(name[len(prefix):])
   147  	return !unicode.IsLower(rune)
   148  }
   149  
   150  // playExample synthesizes a new *ast.File based on the provided
   151  // file with the provided function body as the body of main.
   152  func playExample(file *ast.File, f *ast.FuncDecl) *ast.File {
   153  	body := f.Body
   154  
   155  	if !strings.HasSuffix(file.Name.Name, "_test") {
   156  		// We don't support examples that are part of the
   157  		// greater package (yet).
   158  		return nil
   159  	}
   160  
   161  	// Collect top-level declarations in the file.
   162  	topDecls := make(map[*ast.Object]ast.Decl)
   163  	typMethods := make(map[string][]ast.Decl)
   164  
   165  	for _, decl := range file.Decls {
   166  		switch d := decl.(type) {
   167  		case *ast.FuncDecl:
   168  			if d.Recv == nil {
   169  				topDecls[d.Name.Obj] = d
   170  			} else {
   171  				if len(d.Recv.List) == 1 {
   172  					t := d.Recv.List[0].Type
   173  					tname, _ := baseTypeName(t)
   174  					typMethods[tname] = append(typMethods[tname], d)
   175  				}
   176  			}
   177  		case *ast.GenDecl:
   178  			for _, spec := range d.Specs {
   179  				switch s := spec.(type) {
   180  				case *ast.TypeSpec:
   181  					topDecls[s.Name.Obj] = d
   182  				case *ast.ValueSpec:
   183  					for _, name := range s.Names {
   184  						topDecls[name.Obj] = d
   185  					}
   186  				}
   187  			}
   188  		}
   189  	}
   190  
   191  	// Find unresolved identifiers and uses of top-level declarations.
   192  	unresolved := make(map[string]bool)
   193  	var depDecls []ast.Decl
   194  	hasDepDecls := make(map[ast.Decl]bool)
   195  
   196  	var inspectFunc func(ast.Node) bool
   197  	inspectFunc = func(n ast.Node) bool {
   198  		switch e := n.(type) {
   199  		case *ast.Ident:
   200  			if e.Obj == nil && e.Name != "_" {
   201  				unresolved[e.Name] = true
   202  			} else if d := topDecls[e.Obj]; d != nil {
   203  				if !hasDepDecls[d] {
   204  					hasDepDecls[d] = true
   205  					depDecls = append(depDecls, d)
   206  				}
   207  			}
   208  			return true
   209  		case *ast.SelectorExpr:
   210  			// For selector expressions, only inspect the left hand side.
   211  			// (For an expression like fmt.Println, only add "fmt" to the
   212  			// set of unresolved names, not "Println".)
   213  			ast.Inspect(e.X, inspectFunc)
   214  			return false
   215  		case *ast.KeyValueExpr:
   216  			// For key value expressions, only inspect the value
   217  			// as the key should be resolved by the type of the
   218  			// composite literal.
   219  			ast.Inspect(e.Value, inspectFunc)
   220  			return false
   221  		}
   222  		return true
   223  	}
   224  	ast.Inspect(body, inspectFunc)
   225  	for i := 0; i < len(depDecls); i++ {
   226  		switch d := depDecls[i].(type) {
   227  		case *ast.FuncDecl:
   228  			// Inspect types of parameters and results. See #28492.
   229  			if d.Type.Params != nil {
   230  				for _, p := range d.Type.Params.List {
   231  					ast.Inspect(p.Type, inspectFunc)
   232  				}
   233  			}
   234  			if d.Type.Results != nil {
   235  				for _, r := range d.Type.Results.List {
   236  					ast.Inspect(r.Type, inspectFunc)
   237  				}
   238  			}
   239  
   240  			ast.Inspect(d.Body, inspectFunc)
   241  		case *ast.GenDecl:
   242  			for _, spec := range d.Specs {
   243  				switch s := spec.(type) {
   244  				case *ast.TypeSpec:
   245  					ast.Inspect(s.Type, inspectFunc)
   246  
   247  					depDecls = append(depDecls, typMethods[s.Name.Name]...)
   248  				case *ast.ValueSpec:
   249  					if s.Type != nil {
   250  						ast.Inspect(s.Type, inspectFunc)
   251  					}
   252  					for _, val := range s.Values {
   253  						ast.Inspect(val, inspectFunc)
   254  					}
   255  				}
   256  			}
   257  		}
   258  	}
   259  
   260  	// Remove predeclared identifiers from unresolved list.
   261  	for n := range unresolved {
   262  		if predeclaredTypes[n] || predeclaredConstants[n] || predeclaredFuncs[n] {
   263  			delete(unresolved, n)
   264  		}
   265  	}
   266  
   267  	// Use unresolved identifiers to determine the imports used by this
   268  	// example. The heuristic assumes package names match base import
   269  	// paths for imports w/o renames (should be good enough most of the time).
   270  	namedImports := make(map[string]string) // [name]path
   271  	var blankImports []ast.Spec             // _ imports
   272  	for _, s := range file.Imports {
   273  		p, err := strconv.Unquote(s.Path.Value)
   274  		if err != nil {
   275  			continue
   276  		}
   277  		if p == "syscall/js" {
   278  			// We don't support examples that import syscall/js,
   279  			// because the package syscall/js is not available in the playground.
   280  			return nil
   281  		}
   282  		n := path.Base(p)
   283  		if s.Name != nil {
   284  			n = s.Name.Name
   285  			switch n {
   286  			case "_":
   287  				blankImports = append(blankImports, s)
   288  				continue
   289  			case ".":
   290  				// We can't resolve dot imports (yet).
   291  				return nil
   292  			}
   293  		}
   294  		if unresolved[n] {
   295  			namedImports[n] = p
   296  			delete(unresolved, n)
   297  		}
   298  	}
   299  
   300  	// If there are other unresolved identifiers, give up because this
   301  	// synthesized file is not going to build.
   302  	if len(unresolved) > 0 {
   303  		return nil
   304  	}
   305  
   306  	// Include documentation belonging to blank imports.
   307  	var comments []*ast.CommentGroup
   308  	for _, s := range blankImports {
   309  		if c := s.(*ast.ImportSpec).Doc; c != nil {
   310  			comments = append(comments, c)
   311  		}
   312  	}
   313  
   314  	// Include comments that are inside the function body.
   315  	for _, c := range file.Comments {
   316  		if body.Pos() <= c.Pos() && c.End() <= body.End() {
   317  			comments = append(comments, c)
   318  		}
   319  	}
   320  
   321  	// Strip the "Output:" or "Unordered output:" comment and adjust body
   322  	// end position.
   323  	body, comments = stripOutputComment(body, comments)
   324  
   325  	// Include documentation belonging to dependent declarations.
   326  	for _, d := range depDecls {
   327  		switch d := d.(type) {
   328  		case *ast.GenDecl:
   329  			if d.Doc != nil {
   330  				comments = append(comments, d.Doc)
   331  			}
   332  		case *ast.FuncDecl:
   333  			if d.Doc != nil {
   334  				comments = append(comments, d.Doc)
   335  			}
   336  		}
   337  	}
   338  
   339  	// Synthesize import declaration.
   340  	importDecl := &ast.GenDecl{
   341  		Tok:    token.IMPORT,
   342  		Lparen: 1, // Need non-zero Lparen and Rparen so that printer
   343  		Rparen: 1, // treats this as a factored import.
   344  	}
   345  	for n, p := range namedImports {
   346  		s := &ast.ImportSpec{Path: &ast.BasicLit{Value: strconv.Quote(p)}}
   347  		if path.Base(p) != n {
   348  			s.Name = ast.NewIdent(n)
   349  		}
   350  		importDecl.Specs = append(importDecl.Specs, s)
   351  	}
   352  	importDecl.Specs = append(importDecl.Specs, blankImports...)
   353  
   354  	// Synthesize main function.
   355  	funcDecl := &ast.FuncDecl{
   356  		Name: ast.NewIdent("main"),
   357  		Type: f.Type,
   358  		Body: body,
   359  	}
   360  
   361  	decls := make([]ast.Decl, 0, 2+len(depDecls))
   362  	decls = append(decls, importDecl)
   363  	decls = append(decls, depDecls...)
   364  	decls = append(decls, funcDecl)
   365  
   366  	sort.Slice(decls, func(i, j int) bool {
   367  		return decls[i].Pos() < decls[j].Pos()
   368  	})
   369  
   370  	sort.Slice(comments, func(i, j int) bool {
   371  		return comments[i].Pos() < comments[j].Pos()
   372  	})
   373  
   374  	// Synthesize file.
   375  	return &ast.File{
   376  		Name:     ast.NewIdent("main"),
   377  		Decls:    decls,
   378  		Comments: comments,
   379  	}
   380  }
   381  
   382  // playExampleFile takes a whole file example and synthesizes a new *ast.File
   383  // such that the example is function main in package main.
   384  func playExampleFile(file *ast.File) *ast.File {
   385  	// Strip copyright comment if present.
   386  	comments := file.Comments
   387  	if len(comments) > 0 && strings.HasPrefix(comments[0].Text(), "Copyright") {
   388  		comments = comments[1:]
   389  	}
   390  
   391  	// Copy declaration slice, rewriting the ExampleX function to main.
   392  	var decls []ast.Decl
   393  	for _, d := range file.Decls {
   394  		if f, ok := d.(*ast.FuncDecl); ok && isTest(f.Name.Name, "Example") {
   395  			// Copy the FuncDecl, as it may be used elsewhere.
   396  			newF := *f
   397  			newF.Name = ast.NewIdent("main")
   398  			newF.Body, comments = stripOutputComment(f.Body, comments)
   399  			d = &newF
   400  		}
   401  		decls = append(decls, d)
   402  	}
   403  
   404  	// Copy the File, as it may be used elsewhere.
   405  	f := *file
   406  	f.Name = ast.NewIdent("main")
   407  	f.Decls = decls
   408  	f.Comments = comments
   409  	return &f
   410  }
   411  
   412  // stripOutputComment finds and removes the "Output:" or "Unordered output:"
   413  // comment from body and comments, and adjusts the body block's end position.
   414  func stripOutputComment(body *ast.BlockStmt, comments []*ast.CommentGroup) (*ast.BlockStmt, []*ast.CommentGroup) {
   415  	// Do nothing if there is no "Output:" or "Unordered output:" comment.
   416  	i, last := lastComment(body, comments)
   417  	if last == nil || !outputPrefix.MatchString(last.Text()) {
   418  		return body, comments
   419  	}
   420  
   421  	// Copy body and comments, as the originals may be used elsewhere.
   422  	newBody := &ast.BlockStmt{
   423  		Lbrace: body.Lbrace,
   424  		List:   body.List,
   425  		Rbrace: last.Pos(),
   426  	}
   427  	newComments := make([]*ast.CommentGroup, len(comments)-1)
   428  	copy(newComments, comments[:i])
   429  	copy(newComments[i:], comments[i+1:])
   430  	return newBody, newComments
   431  }
   432  
   433  // lastComment returns the last comment inside the provided block.
   434  func lastComment(b *ast.BlockStmt, c []*ast.CommentGroup) (i int, last *ast.CommentGroup) {
   435  	if b == nil {
   436  		return
   437  	}
   438  	pos, end := b.Pos(), b.End()
   439  	for j, cg := range c {
   440  		if cg.Pos() < pos {
   441  			continue
   442  		}
   443  		if cg.End() > end {
   444  			break
   445  		}
   446  		i, last = j, cg
   447  	}
   448  	return
   449  }
   450  
   451  // classifyExamples classifies examples and assigns them to the Examples field
   452  // of the relevant Func, Type, or Package that the example is associated with.
   453  //
   454  // The classification process is ambiguous in some cases:
   455  //
   456  // 	- ExampleFoo_Bar matches a type named Foo_Bar
   457  // 	  or a method named Foo.Bar.
   458  // 	- ExampleFoo_bar matches a type named Foo_bar
   459  // 	  or Foo (with a "bar" suffix).
   460  //
   461  // Examples with malformed names are not associated with anything.
   462  //
   463  func classifyExamples(p *Package, examples []*Example) {
   464  	if len(examples) == 0 {
   465  		return
   466  	}
   467  
   468  	// Mapping of names for funcs, types, and methods to the example listing.
   469  	ids := make(map[string]*[]*Example)
   470  	ids[""] = &p.Examples // package-level examples have an empty name
   471  	for _, f := range p.Funcs {
   472  		if !token.IsExported(f.Name) {
   473  			continue
   474  		}
   475  		ids[f.Name] = &f.Examples
   476  	}
   477  	for _, t := range p.Types {
   478  		if !token.IsExported(t.Name) {
   479  			continue
   480  		}
   481  		ids[t.Name] = &t.Examples
   482  		for _, f := range t.Funcs {
   483  			if !token.IsExported(f.Name) {
   484  				continue
   485  			}
   486  			ids[f.Name] = &f.Examples
   487  		}
   488  		for _, m := range t.Methods {
   489  			if !token.IsExported(m.Name) || m.Level != 0 { // avoid forwarded methods from embedding
   490  				continue
   491  			}
   492  			ids[strings.TrimPrefix(m.Recv, "*")+"_"+m.Name] = &m.Examples
   493  		}
   494  	}
   495  
   496  	// Group each example with the associated func, type, or method.
   497  	for _, ex := range examples {
   498  		// Consider all possible split points for the suffix
   499  		// by starting at the end of string (no suffix case),
   500  		// then trying all positions that contain a '_' character.
   501  		//
   502  		// An association is made on the first successful match.
   503  		// Examples with malformed names that match nothing are skipped.
   504  		for i := len(ex.Name); i >= 0; i = strings.LastIndexByte(ex.Name[:i], '_') {
   505  			prefix, suffix, ok := splitExampleName(ex.Name, i)
   506  			if !ok {
   507  				continue
   508  			}
   509  			exs, ok := ids[prefix]
   510  			if !ok {
   511  				continue
   512  			}
   513  			ex.Suffix = suffix
   514  			*exs = append(*exs, ex)
   515  			break
   516  		}
   517  	}
   518  
   519  	// Sort list of example according to the user-specified suffix name.
   520  	for _, exs := range ids {
   521  		sort.Slice((*exs), func(i, j int) bool {
   522  			return (*exs)[i].Suffix < (*exs)[j].Suffix
   523  		})
   524  	}
   525  }
   526  
   527  // splitExampleName attempts to split example name s at index i,
   528  // and reports if that produces a valid split. The suffix may be
   529  // absent. Otherwise, it must start with a lower-case letter and
   530  // be preceded by '_'.
   531  //
   532  // One of i == len(s) or s[i] == '_' must be true.
   533  func splitExampleName(s string, i int) (prefix, suffix string, ok bool) {
   534  	if i == len(s) {
   535  		return s, "", true
   536  	}
   537  	if i == len(s)-1 {
   538  		return "", "", false
   539  	}
   540  	prefix, suffix = s[:i], s[i+1:]
   541  	return prefix, suffix, isExampleSuffix(suffix)
   542  }
   543  
   544  func isExampleSuffix(s string) bool {
   545  	r, size := utf8.DecodeRuneInString(s)
   546  	return size > 0 && unicode.IsLower(r)
   547  }
   548  

View as plain text