diff options
author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-03-02 16:38:43 +0000 |
---|---|---|
committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-03-02 16:38:43 +0000 |
commit | 2d2d80b8bd963f59534897b3d51ef8bd546cb4bc (patch) | |
tree | efa0c55763b34cbc633bc494c2743d1b5d9aaff3 /libgo/go/go/doc/example.go | |
parent | 2ad2700dbf70b2e49575f3f2307839a45cf2f71c (diff) | |
download | gcc-2d2d80b8bd963f59534897b3d51ef8bd546cb4bc.tar.gz |
libgo: Update to weekly.2012-02-14 release.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@184798 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/go/go/doc/example.go')
-rw-r--r-- | libgo/go/go/doc/example.go | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/libgo/go/go/doc/example.go b/libgo/go/go/doc/example.go index d5b58d26643..1c23b0d95c3 100644 --- a/libgo/go/go/doc/example.go +++ b/libgo/go/go/doc/example.go @@ -9,6 +9,7 @@ package doc import ( "go/ast" "go/printer" + "go/token" "strings" "unicode" "unicode/utf8" @@ -21,28 +22,47 @@ type Example struct { } func Examples(pkg *ast.Package) []*Example { - var examples []*Example - for _, src := range pkg.Files { - for _, decl := range src.Decls { + var list []*Example + for _, file := range pkg.Files { + hasTests := false // file contains tests or benchmarks + numDecl := 0 // number of non-import declarations in the file + var flist []*Example + for _, decl := range file.Decls { + if g, ok := decl.(*ast.GenDecl); ok && g.Tok != token.IMPORT { + numDecl++ + continue + } f, ok := decl.(*ast.FuncDecl) if !ok { continue } + numDecl++ name := f.Name.Name + if isTest(name, "Test") || isTest(name, "Benchmark") { + hasTests = true + continue + } if !isTest(name, "Example") { continue } - examples = append(examples, &Example{ + flist = append(flist, &Example{ Name: name[len("Example"):], Body: &printer.CommentedNode{ Node: f.Body, - Comments: src.Comments, + Comments: file.Comments, }, Output: f.Doc.Text(), }) } + if !hasTests && numDecl > 1 && len(flist) == 1 { + // If this file only has one example function, some + // other top-level declarations, and no tests or + // benchmarks, use the whole file as the example. + flist[0].Body.Node = file + } + list = append(list, flist...) } - return examples + return list } // isTest tells whether name looks like a test, example, or benchmark. |