Debian Codegraph @

src/go/doc/doc.go

raw
1 // Copyright 2009 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 // Package doc extracts source code documentation from a Go AST.
6 package doc
7
8 import (
9 "fmt"
10 "go/ast"
11 "go/doc/comment"
12 "go/token"
13 "strings"
14 )
15
16 // Package is the documentation for an entire package.
17 type Package struct {
18 Doc string
19 Name string
20 ImportPath string
21 Imports []string
22 Filenames []string
23 Notes map[string][]*Note
24
25 // Deprecated: For backward compatibility Bugs is still populated,
26 // but all new code should use Notes instead.
27 Bugs []string
28
29 // declarations
30 Consts []*Value
31 Types []*Type
32 Vars []*Value
33 Funcs []*Func
34
35 // Examples is a sorted list of examples associated with
36 // the package. Examples are extracted from _test.go files
37 // provided to NewFromFiles.
38 Examples []*Example
39
40 importByName map[string]string
41 syms map[string]bool
42 }
43
44 // Value is the documentation for a (possibly grouped) var or const declaration.
45 type Value struct {
46 Doc string
47 Names []string // var or const names in declaration order
48 Decl *ast.GenDecl
49
50 order int
51 }
52
53 // Type is the documentation for a type declaration.
54 type Type struct {
55 Doc string
56 Name string
57 Decl *ast.GenDecl
58
59 // associated declarations
60 Consts []*Value // sorted list of constants of (mostly) this type
61 Vars []*Value // sorted list of variables of (mostly) this type
62 Funcs []*Func // sorted list of functions returning this type
63 Methods []*Func // sorted list of methods (including embedded ones) of this type
64
65 // Examples is a sorted list of examples associated with
66 // this type. Examples are extracted from _test.go files
67 // provided to NewFromFiles.
68 Examples []*Example
69 }
70
71 // Func is the documentation for a func declaration.
72 type Func struct {
73 Doc string
74 Name string
75 Decl *ast.FuncDecl
76
77 // methods
78 // (for functions, these fields have the respective zero value)
79 Recv string // actual receiver "T" or "*T" possibly followed by type parameters [P1, ..., Pn]
80 Orig string // original receiver "T" or "*T"
81 Level int // embedding level; 0 means not embedded
82
83 // Examples is a sorted list of examples associated with this
84 // function or method. Examples are extracted from _test.go files
85 // provided to NewFromFiles.
86 Examples []*Example
87 }
88
89 // A Note represents a marked comment starting with "MARKER(uid): note body".
90 // Any note with a marker of 2 or more upper case [A-Z] letters and a uid of
91 // at least one character is recognized. The ":" following the uid is optional.
92 // Notes are collected in the Package.Notes map indexed by the notes marker.
93 type Note struct {
94 Pos, End token.Pos // position range of the comment containing the marker
95 UID string // uid found with the marker
96 Body string // note body text
97 }
98
99 // Mode values control the operation of [New] and [NewFromFiles].
100 type Mode int
101
102 const (
103 // AllDecls says to extract documentation for all package-level
104 // declarations, not just exported ones.
105 AllDecls Mode = 1 << iota
106
107 // AllMethods says to show all embedded methods, not just the ones of
108 // invisible (unexported) anonymous fields.
109 AllMethods
110
111 // PreserveAST says to leave the AST unmodified. Originally, pieces of
112 // the AST such as function bodies were nil-ed out to save memory in
113 // godoc, but not all programs want that behavior.
114 PreserveAST
115 )
116
117 // New computes the package documentation for the given package AST.
118 // New takes ownership of the AST pkg and may edit or overwrite it.
119 // To have the [Examples] fields populated, use [NewFromFiles] and include
120 // the package's _test.go files.
121 func New(pkg *ast.Package, importPath string, mode Mode) *Package {
122 var r reader
123 r.readPackage(pkg, mode)
124 r.computeMethodSets()
125 r.cleanupTypes()
126 p := &Package{
127 Doc: r.doc,
128 Name: pkg.Name,
129 ImportPath: importPath,
130 Imports: sortedKeys(r.imports),
131 Filenames: r.filenames,
132 Notes: r.notes,
133 Bugs: noteBodies(r.notes["BUG"]),
134 Consts: sortedValues(r.values, token.CONST),
135 Types: sortedTypes(r.types, mode&AllMethods != 0),
136 Vars: sortedValues(r.values, token.VAR),
137 Funcs: sortedFuncs(r.funcs, true),
138
139 importByName: r.importByName,
140 syms: make(map[string]bool),
141 }
142
143 p.collectValues(p.Consts)
144 p.collectValues(p.Vars)
145 p.collectTypes(p.Types)
146 p.collectFuncs(p.Funcs)
147
148 return p
149 }
150
151 func (p *Package) collectValues(values []*Value) {
152 for _, v := range values {
153 for _, name := range v.Names {
154 p.syms[name] = true
155 }
156 }
157 }
158
159 func (p *Package) collectTypes(types []*Type) {
160 for _, t := range types {
161 if p.syms[t.Name] {
162 // Shouldn't be any cycles but stop just in case.
163 continue
164 }
165 p.syms[t.Name] = true
166 p.collectValues(t.Consts)
167 p.collectValues(t.Vars)
168 p.collectFuncs(t.Funcs)
169 p.collectFuncs(t.Methods)
170 }
171 }
172
173 func (p *Package) collectFuncs(funcs []*Func) {
174 for _, f := range funcs {
175 if f.Recv != "" {
176 r := strings.TrimPrefix(f.Recv, "*")
177 if i := strings.IndexByte(r, '['); i >= 0 {
178 r = r[:i] // remove type parameters
179 }
180 p.syms[r+"."+f.Name] = true
181 } else {
182 p.syms[f.Name] = true
183 }
184 }
185 }
186
187 // NewFromFiles computes documentation for a package.
188 //
189 // The package is specified by a list of *ast.Files and corresponding
190 // file set, which must not be nil.
191 //
192 // NewFromFiles uses all provided files when computing documentation,
193 // so it is the caller's responsibility to provide only the files that
194 // match the desired build context. "go/build".Context.MatchFile can
195 // be used for determining whether a file matches a build context with
196 // the desired GOOS and GOARCH values, and other build constraints.
197 // The import path of the package is specified by importPath.
198 //
199 // Examples found in _test.go files are associated with the corresponding
200 // type, function, method, or the package, based on their name.
201 // If the example has a suffix in its name, it is set in the
202 // [Example.Suffix] field. [Examples] with malformed names are skipped.
203 //
204 // Optionally, a single extra argument of type [Mode] can be provided to
205 // control low-level aspects of the documentation extraction behavior.
206 //
207 // NewFromFiles takes ownership of the AST files and may edit them,
208 // unless the PreserveAST Mode bit is on.
209 func NewFromFiles(fset *token.FileSet, files []*ast.File, importPath string, opts ...any) (*Package, error) {
210 // Check for invalid API usage.
211 if fset == nil {
212 panic(fmt.Errorf("doc.NewFromFiles: no token.FileSet provided (fset == nil)"))
213 }
214 var mode Mode
215 switch len(opts) { // There can only be 0 or 1 options, so a simple switch works for now.
216 case 0:
217 // Nothing to do.
218 case 1:
219 m, ok := opts[0].(Mode)
220 if !ok {
221 panic(fmt.Errorf("doc.NewFromFiles: option argument type must be doc.Mode"))
222 }
223 mode = m
224 default:
225 panic(fmt.Errorf("doc.NewFromFiles: there must not be more than 1 option argument"))
226 }
227
228 // Collect .go and _test.go files.
229 var (
230 pkgName string
231 goFiles = make(map[string]*ast.File)
232 testGoFiles []*ast.File
233 )
234 for i, file := range files {
235 f := fset.File(file.Pos())
236 if f == nil {
237 return nil, fmt.Errorf("file files[%d] is not found in the provided file set", i)
238 }
239 switch filename := f.Name(); {
240 case strings.HasSuffix(filename, "_test.go"):
241 testGoFiles = append(testGoFiles, file)
242 case strings.HasSuffix(filename, ".go"):
243 pkgName = file.Name.Name
244 goFiles[filename] = file
245 default:
246 return nil, fmt.Errorf("file files[%d] filename %q does not have a .go extension", i, filename)
247 }
248 }
249
250 // Compute package documentation.
251 //
252 // Since this package doesn't need Package.{Scope,Imports}, or
253 // handle errors, and ast.File's Scope field is unset in files
254 // parsed with parser.SkipObjectResolution, we construct the
255 // Package directly instead of calling [ast.NewPackage].
256 pkg := &ast.Package{Name: pkgName, Files: goFiles}
257 p := New(pkg, importPath, mode)
258 classifyExamples(p, Examples(testGoFiles...))
259 return p, nil
260 }
261
262 // lookupSym reports whether the package has a given symbol or method.
263 //
264 // If recv == "", HasSym reports whether the package has a top-level
265 // const, func, type, or var named name.
266 //
267 // If recv != "", HasSym reports whether the package has a type
268 // named recv with a method named name.
269 func (p *Package) lookupSym(recv, name string) bool {
270 if recv != "" {
271 return p.syms[recv+"."+name]
272 }
273 return p.syms[name]
274 }
275
276 // lookupPackage returns the import path identified by name
277 // in the given package. If name uniquely identifies a single import,
278 // then lookupPackage returns that import.
279 // If multiple packages are imported as name, importPath returns "", false.
280 // Otherwise, if name is the name of p itself, importPath returns "", true,
281 // to signal a reference to p.
282 // Otherwise, importPath returns "", false.
283 func (p *Package) lookupPackage(name string) (importPath string, ok bool) {
284 if path, ok := p.importByName[name]; ok {
285 if path == "" {
286 return "", false // multiple imports used the name
287 }
288 return path, true // found import
289 }
290 if p.Name == name {
291 return "", true // allow reference to this package
292 }
293 return "", false // unknown name
294 }
295
296 // Parser returns a doc comment parser configured
297 // for parsing doc comments from package p.
298 // Each call returns a new parser, so that the caller may
299 // customize it before use.
300 func (p *Package) Parser() *comment.Parser {
301 return &comment.Parser{
302 LookupPackage: p.lookupPackage,
303 LookupSym: p.lookupSym,
304 }
305 }
306
307 // Printer returns a doc comment printer configured
308 // for printing doc comments from package p.
309 // Each call returns a new printer, so that the caller may
310 // customize it before use.
311 func (p *Package) Printer() *comment.Printer {
312 // No customization today, but having p.Printer()
313 // gives us flexibility in the future, and it is convenient for callers.
314 return &comment.Printer{}
315 }
316
317 // HTML returns formatted HTML for the doc comment text.
318 //
319 // To customize details of the HTML, use [Package.Printer]
320 // to obtain a [comment.Printer], and configure it
321 // before calling its HTML method.
322 func (p *Package) HTML(text string) []byte {
323 return p.Printer().HTML(p.Parser().Parse(text))
324 }
325
326 // Markdown returns formatted Markdown for the doc comment text.
327 //
328 // To customize details of the Markdown, use [Package.Printer]
329 // to obtain a [comment.Printer], and configure it
330 // before calling its Markdown method.
331 func (p *Package) Markdown(text string) []byte {
332 return p.Printer().Markdown(p.Parser().Parse(text))
333 }
334
335 // Text returns formatted text for the doc comment text,
336 // wrapped to 80 Unicode code points and using tabs for
337 // code block indentation.
338 //
339 // To customize details of the formatting, use [Package.Printer]
340 // to obtain a [comment.Printer], and configure it
341 // before calling its Text method.
342 func (p *Package) Text(text string) []byte {
343 return p.Printer().Text(p.Parser().Parse(text))
344 }