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.
6 The headscan command extracts comment headings from package files;
7 it is used to detect false positives which may require an adjustment
8 to the comment formatting heuristics in comment.go.
10 Usage: headscan [-root root_directory]
12 By default, the $GOROOT/src directory is scanned.
30 root = flag.String("root", filepath.Join(runtime.GOROOT(), "src"), "root of filesystem tree to scan")
31 verbose = flag.Bool("v", false, "verbose mode")
39 func isGoFile(fi os.FileInfo) bool {
40 return strings.HasSuffix(fi.Name(), ".go") &&
41 !strings.HasSuffix(fi.Name(), "_test.go")
44 func appendHeadings(list []string, comment string) []string {
46 doc.ToHTML(&buf, comment, nil)
47 for s := buf.String(); ; {
48 i := strings.Index(s, html_h)
53 j := strings.Index(s, html_endh)
55 list = append(list, s[i:]) // incorrect HTML
58 list = append(list, s[i:j])
59 s = s[j+len(html_endh):]
66 fset := token.NewFileSet()
68 err := filepath.Walk(*root, func(path string, fi os.FileInfo, err error) error {
72 pkgs, err := parser.ParseDir(fset, path, isGoFile, parser.ParseComments)
75 fmt.Fprintln(os.Stderr, err)
79 for _, pkg := range pkgs {
80 d := doc.NewPackageDoc(pkg, path)
81 list := appendHeadings(nil, d.Doc)
82 for _, d := range d.Consts {
83 list = appendHeadings(list, d.Doc)
85 for _, d := range d.Types {
86 list = appendHeadings(list, d.Doc)
88 for _, d := range d.Vars {
89 list = appendHeadings(list, d.Doc)
91 for _, d := range d.Funcs {
92 list = appendHeadings(list, d.Doc)
95 // directories may contain multiple packages;
96 // print path and package name
97 fmt.Printf("%s (package %s)\n", path, pkg.Name)
98 for _, h := range list {
99 fmt.Printf("\t%s\n", h)
101 nheadings += len(list)
107 fmt.Fprintln(os.Stderr, err)
110 fmt.Println(nheadings, "headings found")