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.
8 The headscan command extracts comment headings from package files;
9 it is used to detect false positives which may require an adjustment
10 to the comment formatting heuristics in comment.go.
12 Usage: headscan [-root root_directory]
14 By default, the $GOROOT/src directory is scanned.
32 root
= flag
.String("root", filepath
.Join(runtime
.GOROOT(), "src"), "root of filesystem tree to scan")
33 verbose
= flag
.Bool("v", false, "verbose mode")
41 func isGoFile(fi os
.FileInfo
) bool {
42 return strings
.HasSuffix(fi
.Name(), ".go") &&
43 !strings
.HasSuffix(fi
.Name(), "_test.go")
46 func appendHeadings(list
[]string, comment
string) []string {
48 doc
.ToHTML(&buf
, comment
, nil)
49 for s
:= buf
.String(); ; {
50 i
:= strings
.Index(s
, html_h
)
55 j
:= strings
.Index(s
, html_endh
)
57 list
= append(list
, s
[i
:]) // incorrect HTML
60 list
= append(list
, s
[i
:j
])
61 s
= s
[j
+len(html_endh
):]
68 fset
:= token
.NewFileSet()
70 err
:= filepath
.Walk(*root
, func(path
string, fi os
.FileInfo
, err error
) error
{
74 pkgs
, err
:= parser
.ParseDir(fset
, path
, isGoFile
, parser
.ParseComments
)
77 fmt
.Fprintln(os
.Stderr
, err
)
81 for _
, pkg
:= range pkgs
{
82 d
:= doc
.New(pkg
, path
, doc
.Mode(0))
83 list
:= appendHeadings(nil, d
.Doc
)
84 for _
, d
:= range d
.Consts
{
85 list
= appendHeadings(list
, d
.Doc
)
87 for _
, d
:= range d
.Types
{
88 list
= appendHeadings(list
, d
.Doc
)
90 for _
, d
:= range d
.Vars
{
91 list
= appendHeadings(list
, d
.Doc
)
93 for _
, d
:= range d
.Funcs
{
94 list
= appendHeadings(list
, d
.Doc
)
97 // directories may contain multiple packages;
98 // print path and package name
99 fmt
.Printf("%s (package %s)\n", path
, pkg
.Name
)
100 for _
, h
:= range list
{
101 fmt
.Printf("\t%s\n", h
)
103 nheadings
+= len(list
)
109 fmt
.Fprintln(os
.Stderr
, err
)
112 fmt
.Println(nheadings
, "headings found")