Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(237)

Delta Between Two Patch Sets: src/cmd/godoc/godoc.go

Issue 6970051: code review 6970051: cmd/godoc: ignore misnamed examples and print a warning
Left Patch Set: diff -r 5acb449b2a67 https://code.google.com/p/go Created 11 years, 3 months ago
Right Patch Set: diff -r 5acb449b2a67 https://code.google.com/p/go Created 11 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/json" 9 "encoding/json"
10 "flag" 10 "flag"
(...skipping 872 matching lines...) Expand 10 before | Expand all | Expand 10 after
883 // implementation). 883 // implementation).
884 // 884 //
885 func packageExports(fset *token.FileSet, pkg *ast.Package) { 885 func packageExports(fset *token.FileSet, pkg *ast.Package) {
886 for _, src := range pkg.Files { 886 for _, src := range pkg.Files {
887 cmap := ast.NewCommentMap(fset, src, src.Comments) 887 cmap := ast.NewCommentMap(fset, src, src.Comments)
888 ast.FileExports(src) 888 ast.FileExports(src)
889 src.Comments = cmap.Filter(src).Comments() 889 src.Comments = cmap.Filter(src).Comments()
890 } 890 }
891 } 891 }
892 892
893 // hasGlobalName returns true if any of the packages in p have a declaration 893 // declNames returns the names declared by decl.
894 // with a matching name. 894 // Method names are returned in the form Receiver_Method.
895 func hasGlobalName(p map[string]*ast.Package, name string) bool { 895 func declNames(decl ast.Decl) (names []string) {
896 » for _, pkg := range p {
897 » » for _, file := range pkg.Files {
898 » » » for _, decl := range file.Decls {
899 » » » » if hasName(decl, name) {
900 » » » » » return true
901 » » » » }
902 » » » }
903 » » }
904 » }
905 » return false
906 }
907
908 // hasName returns true if decl has a matching name.
909 func hasName(decl ast.Decl, name string) bool {
910 switch d := decl.(type) { 896 switch d := decl.(type) {
911 case *ast.FuncDecl: 897 case *ast.FuncDecl:
912 » » if d.Name.Name == name { 898 » » name := d.Name.Name
913 » » » return true 899 » » if d.Recv != nil {
914 » » } 900 » » » var typeName string
901 » » » switch r := d.Recv.List[0].Type.(type) {
902 » » » case *ast.StarExpr:
903 » » » » typeName = r.X.(*ast.Ident).Name
904 » » » case *ast.Ident:
905 » » » » typeName = r.Name
906 » » » }
907 » » » name = typeName + "_" + name
908 » » }
909 » » names = []string{name}
915 case *ast.GenDecl: 910 case *ast.GenDecl:
916 for _, spec := range d.Specs { 911 for _, spec := range d.Specs {
917 switch s := spec.(type) { 912 switch s := spec.(type) {
918 case *ast.TypeSpec: 913 case *ast.TypeSpec:
919 » » » » if s.Name.Name == name { 914 » » » » names = append(names, s.Name.Name)
920 » » » » » return true
921 » » » » }
922 case *ast.ValueSpec: 915 case *ast.ValueSpec:
923 for _, id := range s.Names { 916 for _, id := range s.Names {
924 » » » » » if id.Name == name { 917 » » » » » names = append(names, id.Name)
925 » » » » » » return true
926 » » » » » }
927 } 918 }
928 } 919 }
929 } 920 }
930 } 921 }
931 » return false 922 » return
923 }
924
925 // globalNames finds all top-level declarations in pkgs and returns a map
926 // with the identifier names as keys.
927 func globalNames(pkgs map[string]*ast.Package) map[string]bool {
928 » names := make(map[string]bool)
929 » for _, pkg := range pkgs {
930 » » for _, file := range pkg.Files {
931 » » » for _, decl := range file.Decls {
932 » » » » for _, name := range declNames(decl) {
933 » » » » » names[name] = true
934 » » » » }
935 » » » }
936 » » }
937 » }
938 » return names
932 } 939 }
933 940
934 // parseExamples gets examples for packages in pkgs from *_test.go files in dir. 941 // parseExamples gets examples for packages in pkgs from *_test.go files in dir.
935 func parseExamples(fset *token.FileSet, pkgs map[string]*ast.Package, dir string ) ([]*doc.Example, error) { 942 func parseExamples(fset *token.FileSet, pkgs map[string]*ast.Package, dir string ) ([]*doc.Example, error) {
936 var examples []*doc.Example 943 var examples []*doc.Example
937 filter := func(d os.FileInfo) bool { 944 filter := func(d os.FileInfo) bool {
938 return isGoFile(d) && strings.HasSuffix(d.Name(), "_test.go") 945 return isGoFile(d) && strings.HasSuffix(d.Name(), "_test.go")
939 } 946 }
940 testpkgs, err := parseDir(fset, dir, filter) 947 testpkgs, err := parseDir(fset, dir, filter)
941 if err != nil { 948 if err != nil {
942 return nil, err 949 return nil, err
943 } 950 }
951 globals := globalNames(pkgs)
944 for _, testpkg := range testpkgs { 952 for _, testpkg := range testpkgs {
945 var files []*ast.File 953 var files []*ast.File
946 for _, f := range testpkg.Files { 954 for _, f := range testpkg.Files {
947 files = append(files, f) 955 files = append(files, f)
948 } 956 }
949 for _, e := range doc.Examples(files...) { 957 for _, e := range doc.Examples(files...) {
950 name := stripExampleSuffix(e.Name) 958 name := stripExampleSuffix(e.Name)
951 » » » if name == "" || hasGlobalName(pkgs, name) { 959 » » » if name == "" || globals[name] {
adg 2012/12/20 06:14:03 I just realized: this hasGlobalName function is pr
kisielk 2012/12/20 07:38:20 Right on both counts. I was just testing with the
952 examples = append(examples, e) 960 examples = append(examples, e)
953 } else { 961 } else {
954 log.Printf("skipping example Example%s: refers t o unknown function or type", e.Name) 962 log.Printf("skipping example Example%s: refers t o unknown function or type", e.Name)
955 } 963 }
956 } 964 }
957 } 965 }
958 return examples, nil 966 return examples, nil
959 } 967 }
960 968
961 // getPageInfo returns the PageInfo for a package directory abspath. If the 969 // getPageInfo returns the PageInfo for a package directory abspath. If the
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after
1480 updateIndex() 1488 updateIndex()
1481 } 1489 }
1482 delay := 60 * time.Second // by default, try every 60s 1490 delay := 60 * time.Second // by default, try every 60s
1483 if *testDir != "" { 1491 if *testDir != "" {
1484 // in test mode, try once a second for fast startup 1492 // in test mode, try once a second for fast startup
1485 delay = 1 * time.Second 1493 delay = 1 * time.Second
1486 } 1494 }
1487 time.Sleep(delay) 1495 time.Sleep(delay)
1488 } 1496 }
1489 } 1497 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b