[PR rtl-optimization/115877][2/n] Improve liveness computation for constant initializ...
[official-gcc.git] / libgo / go / go / printer / printer.go
blobe4679b0021d91e8edce40b0731c05257b43a8425
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.
5 // Package printer implements printing of AST nodes.
6 package printer
8 import (
9 "fmt"
10 "go/ast"
11 "go/build/constraint"
12 "go/token"
13 "io"
14 "os"
15 "strings"
16 "text/tabwriter"
17 "unicode"
20 const (
21 maxNewlines = 2 // max. number of newlines between source text
22 debug = false // enable for debugging
23 infinity = 1 << 30
26 type whiteSpace byte
28 const (
29 ignore = whiteSpace(0)
30 blank = whiteSpace(' ')
31 vtab = whiteSpace('\v')
32 newline = whiteSpace('\n')
33 formfeed = whiteSpace('\f')
34 indent = whiteSpace('>')
35 unindent = whiteSpace('<')
38 // A pmode value represents the current printer mode.
39 type pmode int
41 const (
42 noExtraBlank pmode = 1 << iota // disables extra blank after /*-style comment
43 noExtraLinebreak // disables extra line break after /*-style comment
46 type commentInfo struct {
47 cindex int // current comment index
48 comment *ast.CommentGroup // = printer.comments[cindex]; or nil
49 commentOffset int // = printer.posFor(printer.comments[cindex].List[0].Pos()).Offset; or infinity
50 commentNewline bool // true if the comment group contains newlines
53 type printer struct {
54 // Configuration (does not change after initialization)
55 Config
56 fset *token.FileSet
58 // Current state
59 output []byte // raw printer result
60 indent int // current indentation
61 level int // level == 0: outside composite literal; level > 0: inside composite literal
62 mode pmode // current printer mode
63 endAlignment bool // if set, terminate alignment immediately
64 impliedSemi bool // if set, a linebreak implies a semicolon
65 lastTok token.Token // last token printed (token.ILLEGAL if it's whitespace)
66 prevOpen token.Token // previous non-brace "open" token (, [, or token.ILLEGAL
67 wsbuf []whiteSpace // delayed white space
68 goBuild []int // start index of all //go:build comments in output
69 plusBuild []int // start index of all // +build comments in output
71 // Positions
72 // The out position differs from the pos position when the result
73 // formatting differs from the source formatting (in the amount of
74 // white space). If there's a difference and SourcePos is set in
75 // ConfigMode, //line directives are used in the output to restore
76 // original source positions for a reader.
77 pos token.Position // current position in AST (source) space
78 out token.Position // current position in output space
79 last token.Position // value of pos after calling writeString
80 linePtr *int // if set, record out.Line for the next token in *linePtr
82 // The list of all source comments, in order of appearance.
83 comments []*ast.CommentGroup // may be nil
84 useNodeComments bool // if not set, ignore lead and line comments of nodes
86 // Information about p.comments[p.cindex]; set up by nextComment.
87 commentInfo
89 // Cache of already computed node sizes.
90 nodeSizes map[ast.Node]int
92 // Cache of most recently computed line position.
93 cachedPos token.Pos
94 cachedLine int // line corresponding to cachedPos
97 func (p *printer) init(cfg *Config, fset *token.FileSet, nodeSizes map[ast.Node]int) {
98 p.Config = *cfg
99 p.fset = fset
100 p.pos = token.Position{Line: 1, Column: 1}
101 p.out = token.Position{Line: 1, Column: 1}
102 p.wsbuf = make([]whiteSpace, 0, 16) // whitespace sequences are short
103 p.nodeSizes = nodeSizes
104 p.cachedPos = -1
107 func (p *printer) internalError(msg ...any) {
108 if debug {
109 fmt.Print(p.pos.String() + ": ")
110 fmt.Println(msg...)
111 panic("go/printer")
115 // commentsHaveNewline reports whether a list of comments belonging to
116 // an *ast.CommentGroup contains newlines. Because the position information
117 // may only be partially correct, we also have to read the comment text.
118 func (p *printer) commentsHaveNewline(list []*ast.Comment) bool {
119 // len(list) > 0
120 line := p.lineFor(list[0].Pos())
121 for i, c := range list {
122 if i > 0 && p.lineFor(list[i].Pos()) != line {
123 // not all comments on the same line
124 return true
126 if t := c.Text; len(t) >= 2 && (t[1] == '/' || strings.Contains(t, "\n")) {
127 return true
130 _ = line
131 return false
134 func (p *printer) nextComment() {
135 for p.cindex < len(p.comments) {
136 c := p.comments[p.cindex]
137 p.cindex++
138 if list := c.List; len(list) > 0 {
139 p.comment = c
140 p.commentOffset = p.posFor(list[0].Pos()).Offset
141 p.commentNewline = p.commentsHaveNewline(list)
142 return
144 // we should not reach here (correct ASTs don't have empty
145 // ast.CommentGroup nodes), but be conservative and try again
147 // no more comments
148 p.commentOffset = infinity
151 // commentBefore reports whether the current comment group occurs
152 // before the next position in the source code and printing it does
153 // not introduce implicit semicolons.
155 func (p *printer) commentBefore(next token.Position) bool {
156 return p.commentOffset < next.Offset && (!p.impliedSemi || !p.commentNewline)
159 // commentSizeBefore returns the estimated size of the
160 // comments on the same line before the next position.
162 func (p *printer) commentSizeBefore(next token.Position) int {
163 // save/restore current p.commentInfo (p.nextComment() modifies it)
164 defer func(info commentInfo) {
165 p.commentInfo = info
166 }(p.commentInfo)
168 size := 0
169 for p.commentBefore(next) {
170 for _, c := range p.comment.List {
171 size += len(c.Text)
173 p.nextComment()
175 return size
178 // recordLine records the output line number for the next non-whitespace
179 // token in *linePtr. It is used to compute an accurate line number for a
180 // formatted construct, independent of pending (not yet emitted) whitespace
181 // or comments.
183 func (p *printer) recordLine(linePtr *int) {
184 p.linePtr = linePtr
187 // linesFrom returns the number of output lines between the current
188 // output line and the line argument, ignoring any pending (not yet
189 // emitted) whitespace or comments. It is used to compute an accurate
190 // size (in number of lines) for a formatted construct.
192 func (p *printer) linesFrom(line int) int {
193 return p.out.Line - line
196 func (p *printer) posFor(pos token.Pos) token.Position {
197 // not used frequently enough to cache entire token.Position
198 return p.fset.PositionFor(pos, false /* absolute position */)
201 func (p *printer) lineFor(pos token.Pos) int {
202 if pos != p.cachedPos {
203 p.cachedPos = pos
204 p.cachedLine = p.fset.PositionFor(pos, false /* absolute position */).Line
206 return p.cachedLine
209 // writeLineDirective writes a //line directive if necessary.
210 func (p *printer) writeLineDirective(pos token.Position) {
211 if pos.IsValid() && (p.out.Line != pos.Line || p.out.Filename != pos.Filename) {
212 p.output = append(p.output, tabwriter.Escape) // protect '\n' in //line from tabwriter interpretation
213 p.output = append(p.output, fmt.Sprintf("//line %s:%d\n", pos.Filename, pos.Line)...)
214 p.output = append(p.output, tabwriter.Escape)
215 // p.out must match the //line directive
216 p.out.Filename = pos.Filename
217 p.out.Line = pos.Line
221 // writeIndent writes indentation.
222 func (p *printer) writeIndent() {
223 // use "hard" htabs - indentation columns
224 // must not be discarded by the tabwriter
225 n := p.Config.Indent + p.indent // include base indentation
226 for i := 0; i < n; i++ {
227 p.output = append(p.output, '\t')
230 // update positions
231 p.pos.Offset += n
232 p.pos.Column += n
233 p.out.Column += n
236 // writeByte writes ch n times to p.output and updates p.pos.
237 // Only used to write formatting (white space) characters.
238 func (p *printer) writeByte(ch byte, n int) {
239 if p.endAlignment {
240 // Ignore any alignment control character;
241 // and at the end of the line, break with
242 // a formfeed to indicate termination of
243 // existing columns.
244 switch ch {
245 case '\t', '\v':
246 ch = ' '
247 case '\n', '\f':
248 ch = '\f'
249 p.endAlignment = false
253 if p.out.Column == 1 {
254 // no need to write line directives before white space
255 p.writeIndent()
258 for i := 0; i < n; i++ {
259 p.output = append(p.output, ch)
262 // update positions
263 p.pos.Offset += n
264 if ch == '\n' || ch == '\f' {
265 p.pos.Line += n
266 p.out.Line += n
267 p.pos.Column = 1
268 p.out.Column = 1
269 return
271 p.pos.Column += n
272 p.out.Column += n
275 // writeString writes the string s to p.output and updates p.pos, p.out,
276 // and p.last. If isLit is set, s is escaped w/ tabwriter.Escape characters
277 // to protect s from being interpreted by the tabwriter.
279 // Note: writeString is only used to write Go tokens, literals, and
280 // comments, all of which must be written literally. Thus, it is correct
281 // to always set isLit = true. However, setting it explicitly only when
282 // needed (i.e., when we don't know that s contains no tabs or line breaks)
283 // avoids processing extra escape characters and reduces run time of the
284 // printer benchmark by up to 10%.
286 func (p *printer) writeString(pos token.Position, s string, isLit bool) {
287 if p.out.Column == 1 {
288 if p.Config.Mode&SourcePos != 0 {
289 p.writeLineDirective(pos)
291 p.writeIndent()
294 if pos.IsValid() {
295 // update p.pos (if pos is invalid, continue with existing p.pos)
296 // Note: Must do this after handling line beginnings because
297 // writeIndent updates p.pos if there's indentation, but p.pos
298 // is the position of s.
299 p.pos = pos
302 if isLit {
303 // Protect s such that is passes through the tabwriter
304 // unchanged. Note that valid Go programs cannot contain
305 // tabwriter.Escape bytes since they do not appear in legal
306 // UTF-8 sequences.
307 p.output = append(p.output, tabwriter.Escape)
310 if debug {
311 p.output = append(p.output, fmt.Sprintf("/*%s*/", pos)...) // do not update p.pos!
313 p.output = append(p.output, s...)
315 // update positions
316 nlines := 0
317 var li int // index of last newline; valid if nlines > 0
318 for i := 0; i < len(s); i++ {
319 // Raw string literals may contain any character except back quote (`).
320 if ch := s[i]; ch == '\n' || ch == '\f' {
321 // account for line break
322 nlines++
323 li = i
324 // A line break inside a literal will break whatever column
325 // formatting is in place; ignore any further alignment through
326 // the end of the line.
327 p.endAlignment = true
330 p.pos.Offset += len(s)
331 if nlines > 0 {
332 p.pos.Line += nlines
333 p.out.Line += nlines
334 c := len(s) - li
335 p.pos.Column = c
336 p.out.Column = c
337 } else {
338 p.pos.Column += len(s)
339 p.out.Column += len(s)
342 if isLit {
343 p.output = append(p.output, tabwriter.Escape)
346 p.last = p.pos
349 // writeCommentPrefix writes the whitespace before a comment.
350 // If there is any pending whitespace, it consumes as much of
351 // it as is likely to help position the comment nicely.
352 // pos is the comment position, next the position of the item
353 // after all pending comments, prev is the previous comment in
354 // a group of comments (or nil), and tok is the next token.
356 func (p *printer) writeCommentPrefix(pos, next token.Position, prev *ast.Comment, tok token.Token) {
357 if len(p.output) == 0 {
358 // the comment is the first item to be printed - don't write any whitespace
359 return
362 if pos.IsValid() && pos.Filename != p.last.Filename {
363 // comment in a different file - separate with newlines
364 p.writeByte('\f', maxNewlines)
365 return
368 if pos.Line == p.last.Line && (prev == nil || prev.Text[1] != '/') {
369 // comment on the same line as last item:
370 // separate with at least one separator
371 hasSep := false
372 if prev == nil {
373 // first comment of a comment group
374 j := 0
375 for i, ch := range p.wsbuf {
376 switch ch {
377 case blank:
378 // ignore any blanks before a comment
379 p.wsbuf[i] = ignore
380 continue
381 case vtab:
382 // respect existing tabs - important
383 // for proper formatting of commented structs
384 hasSep = true
385 continue
386 case indent:
387 // apply pending indentation
388 continue
390 j = i
391 break
393 p.writeWhitespace(j)
395 // make sure there is at least one separator
396 if !hasSep {
397 sep := byte('\t')
398 if pos.Line == next.Line {
399 // next item is on the same line as the comment
400 // (which must be a /*-style comment): separate
401 // with a blank instead of a tab
402 sep = ' '
404 p.writeByte(sep, 1)
407 } else {
408 // comment on a different line:
409 // separate with at least one line break
410 droppedLinebreak := false
411 j := 0
412 for i, ch := range p.wsbuf {
413 switch ch {
414 case blank, vtab:
415 // ignore any horizontal whitespace before line breaks
416 p.wsbuf[i] = ignore
417 continue
418 case indent:
419 // apply pending indentation
420 continue
421 case unindent:
422 // if this is not the last unindent, apply it
423 // as it is (likely) belonging to the last
424 // construct (e.g., a multi-line expression list)
425 // and is not part of closing a block
426 if i+1 < len(p.wsbuf) && p.wsbuf[i+1] == unindent {
427 continue
429 // if the next token is not a closing }, apply the unindent
430 // if it appears that the comment is aligned with the
431 // token; otherwise assume the unindent is part of a
432 // closing block and stop (this scenario appears with
433 // comments before a case label where the comments
434 // apply to the next case instead of the current one)
435 if tok != token.RBRACE && pos.Column == next.Column {
436 continue
438 case newline, formfeed:
439 p.wsbuf[i] = ignore
440 droppedLinebreak = prev == nil // record only if first comment of a group
442 j = i
443 break
445 p.writeWhitespace(j)
447 // determine number of linebreaks before the comment
448 n := 0
449 if pos.IsValid() && p.last.IsValid() {
450 n = pos.Line - p.last.Line
451 if n < 0 { // should never happen
452 n = 0
456 // at the package scope level only (p.indent == 0),
457 // add an extra newline if we dropped one before:
458 // this preserves a blank line before documentation
459 // comments at the package scope level (issue 2570)
460 if p.indent == 0 && droppedLinebreak {
464 // make sure there is at least one line break
465 // if the previous comment was a line comment
466 if n == 0 && prev != nil && prev.Text[1] == '/' {
467 n = 1
470 if n > 0 {
471 // use formfeeds to break columns before a comment;
472 // this is analogous to using formfeeds to separate
473 // individual lines of /*-style comments
474 p.writeByte('\f', nlimit(n))
479 // Returns true if s contains only white space
480 // (only tabs and blanks can appear in the printer's context).
482 func isBlank(s string) bool {
483 for i := 0; i < len(s); i++ {
484 if s[i] > ' ' {
485 return false
488 return true
491 // commonPrefix returns the common prefix of a and b.
492 func commonPrefix(a, b string) string {
493 i := 0
494 for i < len(a) && i < len(b) && a[i] == b[i] && (a[i] <= ' ' || a[i] == '*') {
497 return a[0:i]
500 // trimRight returns s with trailing whitespace removed.
501 func trimRight(s string) string {
502 return strings.TrimRightFunc(s, unicode.IsSpace)
505 // stripCommonPrefix removes a common prefix from /*-style comment lines (unless no
506 // comment line is indented, all but the first line have some form of space prefix).
507 // The prefix is computed using heuristics such that is likely that the comment
508 // contents are nicely laid out after re-printing each line using the printer's
509 // current indentation.
511 func stripCommonPrefix(lines []string) {
512 if len(lines) <= 1 {
513 return // at most one line - nothing to do
515 // len(lines) > 1
517 // The heuristic in this function tries to handle a few
518 // common patterns of /*-style comments: Comments where
519 // the opening /* and closing */ are aligned and the
520 // rest of the comment text is aligned and indented with
521 // blanks or tabs, cases with a vertical "line of stars"
522 // on the left, and cases where the closing */ is on the
523 // same line as the last comment text.
525 // Compute maximum common white prefix of all but the first,
526 // last, and blank lines, and replace blank lines with empty
527 // lines (the first line starts with /* and has no prefix).
528 // In cases where only the first and last lines are not blank,
529 // such as two-line comments, or comments where all inner lines
530 // are blank, consider the last line for the prefix computation
531 // since otherwise the prefix would be empty.
533 // Note that the first and last line are never empty (they
534 // contain the opening /* and closing */ respectively) and
535 // thus they can be ignored by the blank line check.
536 prefix := ""
537 prefixSet := false
538 if len(lines) > 2 {
539 for i, line := range lines[1 : len(lines)-1] {
540 if isBlank(line) {
541 lines[1+i] = "" // range starts with lines[1]
542 } else {
543 if !prefixSet {
544 prefix = line
545 prefixSet = true
547 prefix = commonPrefix(prefix, line)
552 // If we don't have a prefix yet, consider the last line.
553 if !prefixSet {
554 line := lines[len(lines)-1]
555 prefix = commonPrefix(line, line)
559 * Check for vertical "line of stars" and correct prefix accordingly.
561 lineOfStars := false
562 if p, _, ok := strings.Cut(prefix, "*"); ok {
563 // remove trailing blank from prefix so stars remain aligned
564 prefix = strings.TrimSuffix(p, " ")
565 lineOfStars = true
566 } else {
567 // No line of stars present.
568 // Determine the white space on the first line after the /*
569 // and before the beginning of the comment text, assume two
570 // blanks instead of the /* unless the first character after
571 // the /* is a tab. If the first comment line is empty but
572 // for the opening /*, assume up to 3 blanks or a tab. This
573 // whitespace may be found as suffix in the common prefix.
574 first := lines[0]
575 if isBlank(first[2:]) {
576 // no comment text on the first line:
577 // reduce prefix by up to 3 blanks or a tab
578 // if present - this keeps comment text indented
579 // relative to the /* and */'s if it was indented
580 // in the first place
581 i := len(prefix)
582 for n := 0; n < 3 && i > 0 && prefix[i-1] == ' '; n++ {
585 if i == len(prefix) && i > 0 && prefix[i-1] == '\t' {
588 prefix = prefix[0:i]
589 } else {
590 // comment text on the first line
591 suffix := make([]byte, len(first))
592 n := 2 // start after opening /*
593 for n < len(first) && first[n] <= ' ' {
594 suffix[n] = first[n]
597 if n > 2 && suffix[2] == '\t' {
598 // assume the '\t' compensates for the /*
599 suffix = suffix[2:n]
600 } else {
601 // otherwise assume two blanks
602 suffix[0], suffix[1] = ' ', ' '
603 suffix = suffix[0:n]
605 // Shorten the computed common prefix by the length of
606 // suffix, if it is found as suffix of the prefix.
607 prefix = strings.TrimSuffix(prefix, string(suffix))
611 // Handle last line: If it only contains a closing */, align it
612 // with the opening /*, otherwise align the text with the other
613 // lines.
614 last := lines[len(lines)-1]
615 closing := "*/"
616 before, _, _ := strings.Cut(last, closing) // closing always present
617 if isBlank(before) {
618 // last line only contains closing */
619 if lineOfStars {
620 closing = " */" // add blank to align final star
622 lines[len(lines)-1] = prefix + closing
623 } else {
624 // last line contains more comment text - assume
625 // it is aligned like the other lines and include
626 // in prefix computation
627 prefix = commonPrefix(prefix, last)
630 // Remove the common prefix from all but the first and empty lines.
631 for i, line := range lines {
632 if i > 0 && line != "" {
633 lines[i] = line[len(prefix):]
638 func (p *printer) writeComment(comment *ast.Comment) {
639 text := comment.Text
640 pos := p.posFor(comment.Pos())
642 const linePrefix = "//line "
643 if strings.HasPrefix(text, linePrefix) && (!pos.IsValid() || pos.Column == 1) {
644 // Possibly a //-style line directive.
645 // Suspend indentation temporarily to keep line directive valid.
646 defer func(indent int) { p.indent = indent }(p.indent)
647 p.indent = 0
650 // shortcut common case of //-style comments
651 if text[1] == '/' {
652 if constraint.IsGoBuild(text) {
653 p.goBuild = append(p.goBuild, len(p.output))
654 } else if constraint.IsPlusBuild(text) {
655 p.plusBuild = append(p.plusBuild, len(p.output))
657 p.writeString(pos, trimRight(text), true)
658 return
661 // for /*-style comments, print line by line and let the
662 // write function take care of the proper indentation
663 lines := strings.Split(text, "\n")
665 // The comment started in the first column but is going
666 // to be indented. For an idempotent result, add indentation
667 // to all lines such that they look like they were indented
668 // before - this will make sure the common prefix computation
669 // is the same independent of how many times formatting is
670 // applied (was issue 1835).
671 if pos.IsValid() && pos.Column == 1 && p.indent > 0 {
672 for i, line := range lines[1:] {
673 lines[1+i] = " " + line
677 stripCommonPrefix(lines)
679 // write comment lines, separated by formfeed,
680 // without a line break after the last line
681 for i, line := range lines {
682 if i > 0 {
683 p.writeByte('\f', 1)
684 pos = p.pos
686 if len(line) > 0 {
687 p.writeString(pos, trimRight(line), true)
692 // writeCommentSuffix writes a line break after a comment if indicated
693 // and processes any leftover indentation information. If a line break
694 // is needed, the kind of break (newline vs formfeed) depends on the
695 // pending whitespace. The writeCommentSuffix result indicates if a
696 // newline was written or if a formfeed was dropped from the whitespace
697 // buffer.
699 func (p *printer) writeCommentSuffix(needsLinebreak bool) (wroteNewline, droppedFF bool) {
700 for i, ch := range p.wsbuf {
701 switch ch {
702 case blank, vtab:
703 // ignore trailing whitespace
704 p.wsbuf[i] = ignore
705 case indent, unindent:
706 // don't lose indentation information
707 case newline, formfeed:
708 // if we need a line break, keep exactly one
709 // but remember if we dropped any formfeeds
710 if needsLinebreak {
711 needsLinebreak = false
712 wroteNewline = true
713 } else {
714 if ch == formfeed {
715 droppedFF = true
717 p.wsbuf[i] = ignore
721 p.writeWhitespace(len(p.wsbuf))
723 // make sure we have a line break
724 if needsLinebreak {
725 p.writeByte('\n', 1)
726 wroteNewline = true
729 return
732 // containsLinebreak reports whether the whitespace buffer contains any line breaks.
733 func (p *printer) containsLinebreak() bool {
734 for _, ch := range p.wsbuf {
735 if ch == newline || ch == formfeed {
736 return true
739 return false
742 // intersperseComments consumes all comments that appear before the next token
743 // tok and prints it together with the buffered whitespace (i.e., the whitespace
744 // that needs to be written before the next token). A heuristic is used to mix
745 // the comments and whitespace. The intersperseComments result indicates if a
746 // newline was written or if a formfeed was dropped from the whitespace buffer.
748 func (p *printer) intersperseComments(next token.Position, tok token.Token) (wroteNewline, droppedFF bool) {
749 var last *ast.Comment
750 for p.commentBefore(next) {
751 for _, c := range p.comment.List {
752 p.writeCommentPrefix(p.posFor(c.Pos()), next, last, tok)
753 p.writeComment(c)
754 last = c
756 p.nextComment()
759 if last != nil {
760 // If the last comment is a /*-style comment and the next item
761 // follows on the same line but is not a comma, and not a "closing"
762 // token immediately following its corresponding "opening" token,
763 // add an extra separator unless explicitly disabled. Use a blank
764 // as separator unless we have pending linebreaks, they are not
765 // disabled, and we are outside a composite literal, in which case
766 // we want a linebreak (issue 15137).
767 // TODO(gri) This has become overly complicated. We should be able
768 // to track whether we're inside an expression or statement and
769 // use that information to decide more directly.
770 needsLinebreak := false
771 if p.mode&noExtraBlank == 0 &&
772 last.Text[1] == '*' && p.lineFor(last.Pos()) == next.Line &&
773 tok != token.COMMA &&
774 (tok != token.RPAREN || p.prevOpen == token.LPAREN) &&
775 (tok != token.RBRACK || p.prevOpen == token.LBRACK) {
776 if p.containsLinebreak() && p.mode&noExtraLinebreak == 0 && p.level == 0 {
777 needsLinebreak = true
778 } else {
779 p.writeByte(' ', 1)
782 // Ensure that there is a line break after a //-style comment,
783 // before EOF, and before a closing '}' unless explicitly disabled.
784 if last.Text[1] == '/' ||
785 tok == token.EOF ||
786 tok == token.RBRACE && p.mode&noExtraLinebreak == 0 {
787 needsLinebreak = true
789 return p.writeCommentSuffix(needsLinebreak)
792 // no comment was written - we should never reach here since
793 // intersperseComments should not be called in that case
794 p.internalError("intersperseComments called without pending comments")
795 return
798 // whiteWhitespace writes the first n whitespace entries.
799 func (p *printer) writeWhitespace(n int) {
800 // write entries
801 for i := 0; i < n; i++ {
802 switch ch := p.wsbuf[i]; ch {
803 case ignore:
804 // ignore!
805 case indent:
806 p.indent++
807 case unindent:
808 p.indent--
809 if p.indent < 0 {
810 p.internalError("negative indentation:", p.indent)
811 p.indent = 0
813 case newline, formfeed:
814 // A line break immediately followed by a "correcting"
815 // unindent is swapped with the unindent - this permits
816 // proper label positioning. If a comment is between
817 // the line break and the label, the unindent is not
818 // part of the comment whitespace prefix and the comment
819 // will be positioned correctly indented.
820 if i+1 < n && p.wsbuf[i+1] == unindent {
821 // Use a formfeed to terminate the current section.
822 // Otherwise, a long label name on the next line leading
823 // to a wide column may increase the indentation column
824 // of lines before the label; effectively leading to wrong
825 // indentation.
826 p.wsbuf[i], p.wsbuf[i+1] = unindent, formfeed
827 i-- // do it again
828 continue
830 fallthrough
831 default:
832 p.writeByte(byte(ch), 1)
836 // shift remaining entries down
837 l := copy(p.wsbuf, p.wsbuf[n:])
838 p.wsbuf = p.wsbuf[:l]
841 // ----------------------------------------------------------------------------
842 // Printing interface
844 // nlimit limits n to maxNewlines.
845 func nlimit(n int) int {
846 if n > maxNewlines {
847 n = maxNewlines
849 return n
852 func mayCombine(prev token.Token, next byte) (b bool) {
853 switch prev {
854 case token.INT:
855 b = next == '.' // 1.
856 case token.ADD:
857 b = next == '+' // ++
858 case token.SUB:
859 b = next == '-' // --
860 case token.QUO:
861 b = next == '*' // /*
862 case token.LSS:
863 b = next == '-' || next == '<' // <- or <<
864 case token.AND:
865 b = next == '&' || next == '^' // && or &^
867 return
870 // print prints a list of "items" (roughly corresponding to syntactic
871 // tokens, but also including whitespace and formatting information).
872 // It is the only print function that should be called directly from
873 // any of the AST printing functions in nodes.go.
875 // Whitespace is accumulated until a non-whitespace token appears. Any
876 // comments that need to appear before that token are printed first,
877 // taking into account the amount and structure of any pending white-
878 // space for best comment placement. Then, any leftover whitespace is
879 // printed, followed by the actual token.
881 func (p *printer) print(args ...any) {
882 for _, arg := range args {
883 // information about the current arg
884 var data string
885 var isLit bool
886 var impliedSemi bool // value for p.impliedSemi after this arg
888 // record previous opening token, if any
889 switch p.lastTok {
890 case token.ILLEGAL:
891 // ignore (white space)
892 case token.LPAREN, token.LBRACK:
893 p.prevOpen = p.lastTok
894 default:
895 // other tokens followed any opening token
896 p.prevOpen = token.ILLEGAL
899 switch x := arg.(type) {
900 case pmode:
901 // toggle printer mode
902 p.mode ^= x
903 continue
905 case whiteSpace:
906 if x == ignore {
907 // don't add ignore's to the buffer; they
908 // may screw up "correcting" unindents (see
909 // LabeledStmt)
910 continue
912 i := len(p.wsbuf)
913 if i == cap(p.wsbuf) {
914 // Whitespace sequences are very short so this should
915 // never happen. Handle gracefully (but possibly with
916 // bad comment placement) if it does happen.
917 p.writeWhitespace(i)
918 i = 0
920 p.wsbuf = p.wsbuf[0 : i+1]
921 p.wsbuf[i] = x
922 if x == newline || x == formfeed {
923 // newlines affect the current state (p.impliedSemi)
924 // and not the state after printing arg (impliedSemi)
925 // because comments can be interspersed before the arg
926 // in this case
927 p.impliedSemi = false
929 p.lastTok = token.ILLEGAL
930 continue
932 case *ast.Ident:
933 data = x.Name
934 impliedSemi = true
935 p.lastTok = token.IDENT
937 case *ast.BasicLit:
938 data = x.Value
939 isLit = true
940 impliedSemi = true
941 p.lastTok = x.Kind
943 case token.Token:
944 s := x.String()
945 if mayCombine(p.lastTok, s[0]) {
946 // the previous and the current token must be
947 // separated by a blank otherwise they combine
948 // into a different incorrect token sequence
949 // (except for token.INT followed by a '.' this
950 // should never happen because it is taken care
951 // of via binary expression formatting)
952 if len(p.wsbuf) != 0 {
953 p.internalError("whitespace buffer not empty")
955 p.wsbuf = p.wsbuf[0:1]
956 p.wsbuf[0] = ' '
958 data = s
959 // some keywords followed by a newline imply a semicolon
960 switch x {
961 case token.BREAK, token.CONTINUE, token.FALLTHROUGH, token.RETURN,
962 token.INC, token.DEC, token.RPAREN, token.RBRACK, token.RBRACE:
963 impliedSemi = true
965 p.lastTok = x
967 case token.Pos:
968 if x.IsValid() {
969 p.pos = p.posFor(x) // accurate position of next item
971 continue
973 case string:
974 // incorrect AST - print error message
975 data = x
976 isLit = true
977 impliedSemi = true
978 p.lastTok = token.STRING
980 default:
981 fmt.Fprintf(os.Stderr, "print: unsupported argument %v (%T)\n", arg, arg)
982 panic("go/printer type")
984 // data != ""
986 next := p.pos // estimated/accurate position of next item
987 wroteNewline, droppedFF := p.flush(next, p.lastTok)
989 // intersperse extra newlines if present in the source and
990 // if they don't cause extra semicolons (don't do this in
991 // flush as it will cause extra newlines at the end of a file)
992 if !p.impliedSemi {
993 n := nlimit(next.Line - p.pos.Line)
994 // don't exceed maxNewlines if we already wrote one
995 if wroteNewline && n == maxNewlines {
996 n = maxNewlines - 1
998 if n > 0 {
999 ch := byte('\n')
1000 if droppedFF {
1001 ch = '\f' // use formfeed since we dropped one before
1003 p.writeByte(ch, n)
1004 impliedSemi = false
1008 // the next token starts now - record its line number if requested
1009 if p.linePtr != nil {
1010 *p.linePtr = p.out.Line
1011 p.linePtr = nil
1014 p.writeString(next, data, isLit)
1015 p.impliedSemi = impliedSemi
1019 // flush prints any pending comments and whitespace occurring textually
1020 // before the position of the next token tok. The flush result indicates
1021 // if a newline was written or if a formfeed was dropped from the whitespace
1022 // buffer.
1024 func (p *printer) flush(next token.Position, tok token.Token) (wroteNewline, droppedFF bool) {
1025 if p.commentBefore(next) {
1026 // if there are comments before the next item, intersperse them
1027 wroteNewline, droppedFF = p.intersperseComments(next, tok)
1028 } else {
1029 // otherwise, write any leftover whitespace
1030 p.writeWhitespace(len(p.wsbuf))
1032 return
1035 // getNode returns the ast.CommentGroup associated with n, if any.
1036 func getDoc(n ast.Node) *ast.CommentGroup {
1037 switch n := n.(type) {
1038 case *ast.Field:
1039 return n.Doc
1040 case *ast.ImportSpec:
1041 return n.Doc
1042 case *ast.ValueSpec:
1043 return n.Doc
1044 case *ast.TypeSpec:
1045 return n.Doc
1046 case *ast.GenDecl:
1047 return n.Doc
1048 case *ast.FuncDecl:
1049 return n.Doc
1050 case *ast.File:
1051 return n.Doc
1053 return nil
1056 func getLastComment(n ast.Node) *ast.CommentGroup {
1057 switch n := n.(type) {
1058 case *ast.Field:
1059 return n.Comment
1060 case *ast.ImportSpec:
1061 return n.Comment
1062 case *ast.ValueSpec:
1063 return n.Comment
1064 case *ast.TypeSpec:
1065 return n.Comment
1066 case *ast.GenDecl:
1067 if len(n.Specs) > 0 {
1068 return getLastComment(n.Specs[len(n.Specs)-1])
1070 case *ast.File:
1071 if len(n.Comments) > 0 {
1072 return n.Comments[len(n.Comments)-1]
1075 return nil
1078 func (p *printer) printNode(node any) error {
1079 // unpack *CommentedNode, if any
1080 var comments []*ast.CommentGroup
1081 if cnode, ok := node.(*CommentedNode); ok {
1082 node = cnode.Node
1083 comments = cnode.Comments
1086 if comments != nil {
1087 // commented node - restrict comment list to relevant range
1088 n, ok := node.(ast.Node)
1089 if !ok {
1090 goto unsupported
1092 beg := n.Pos()
1093 end := n.End()
1094 // if the node has associated documentation,
1095 // include that commentgroup in the range
1096 // (the comment list is sorted in the order
1097 // of the comment appearance in the source code)
1098 if doc := getDoc(n); doc != nil {
1099 beg = doc.Pos()
1101 if com := getLastComment(n); com != nil {
1102 if e := com.End(); e > end {
1103 end = e
1106 // token.Pos values are global offsets, we can
1107 // compare them directly
1108 i := 0
1109 for i < len(comments) && comments[i].End() < beg {
1112 j := i
1113 for j < len(comments) && comments[j].Pos() < end {
1116 if i < j {
1117 p.comments = comments[i:j]
1119 } else if n, ok := node.(*ast.File); ok {
1120 // use ast.File comments, if any
1121 p.comments = n.Comments
1124 // if there are no comments, use node comments
1125 p.useNodeComments = p.comments == nil
1127 // get comments ready for use
1128 p.nextComment()
1130 p.print(pmode(0))
1132 // format node
1133 switch n := node.(type) {
1134 case ast.Expr:
1135 p.expr(n)
1136 case ast.Stmt:
1137 // A labeled statement will un-indent to position the label.
1138 // Set p.indent to 1 so we don't get indent "underflow".
1139 if _, ok := n.(*ast.LabeledStmt); ok {
1140 p.indent = 1
1142 p.stmt(n, false)
1143 case ast.Decl:
1144 p.decl(n)
1145 case ast.Spec:
1146 p.spec(n, 1, false)
1147 case []ast.Stmt:
1148 // A labeled statement will un-indent to position the label.
1149 // Set p.indent to 1 so we don't get indent "underflow".
1150 for _, s := range n {
1151 if _, ok := s.(*ast.LabeledStmt); ok {
1152 p.indent = 1
1155 p.stmtList(n, 0, false)
1156 case []ast.Decl:
1157 p.declList(n)
1158 case *ast.File:
1159 p.file(n)
1160 default:
1161 goto unsupported
1164 return nil
1166 unsupported:
1167 return fmt.Errorf("go/printer: unsupported node type %T", node)
1170 // ----------------------------------------------------------------------------
1171 // Trimmer
1173 // A trimmer is an io.Writer filter for stripping tabwriter.Escape
1174 // characters, trailing blanks and tabs, and for converting formfeed
1175 // and vtab characters into newlines and htabs (in case no tabwriter
1176 // is used). Text bracketed by tabwriter.Escape characters is passed
1177 // through unchanged.
1179 type trimmer struct {
1180 output io.Writer
1181 state int
1182 space []byte
1185 // trimmer is implemented as a state machine.
1186 // It can be in one of the following states:
1187 const (
1188 inSpace = iota // inside space
1189 inEscape // inside text bracketed by tabwriter.Escapes
1190 inText // inside text
1193 func (p *trimmer) resetSpace() {
1194 p.state = inSpace
1195 p.space = p.space[0:0]
1198 // Design note: It is tempting to eliminate extra blanks occurring in
1199 // whitespace in this function as it could simplify some
1200 // of the blanks logic in the node printing functions.
1201 // However, this would mess up any formatting done by
1202 // the tabwriter.
1204 var aNewline = []byte("\n")
1206 func (p *trimmer) Write(data []byte) (n int, err error) {
1207 // invariants:
1208 // p.state == inSpace:
1209 // p.space is unwritten
1210 // p.state == inEscape, inText:
1211 // data[m:n] is unwritten
1212 m := 0
1213 var b byte
1214 for n, b = range data {
1215 if b == '\v' {
1216 b = '\t' // convert to htab
1218 switch p.state {
1219 case inSpace:
1220 switch b {
1221 case '\t', ' ':
1222 p.space = append(p.space, b)
1223 case '\n', '\f':
1224 p.resetSpace() // discard trailing space
1225 _, err = p.output.Write(aNewline)
1226 case tabwriter.Escape:
1227 _, err = p.output.Write(p.space)
1228 p.state = inEscape
1229 m = n + 1 // +1: skip tabwriter.Escape
1230 default:
1231 _, err = p.output.Write(p.space)
1232 p.state = inText
1233 m = n
1235 case inEscape:
1236 if b == tabwriter.Escape {
1237 _, err = p.output.Write(data[m:n])
1238 p.resetSpace()
1240 case inText:
1241 switch b {
1242 case '\t', ' ':
1243 _, err = p.output.Write(data[m:n])
1244 p.resetSpace()
1245 p.space = append(p.space, b)
1246 case '\n', '\f':
1247 _, err = p.output.Write(data[m:n])
1248 p.resetSpace()
1249 if err == nil {
1250 _, err = p.output.Write(aNewline)
1252 case tabwriter.Escape:
1253 _, err = p.output.Write(data[m:n])
1254 p.state = inEscape
1255 m = n + 1 // +1: skip tabwriter.Escape
1257 default:
1258 panic("unreachable")
1260 if err != nil {
1261 return
1264 n = len(data)
1266 switch p.state {
1267 case inEscape, inText:
1268 _, err = p.output.Write(data[m:n])
1269 p.resetSpace()
1272 return
1275 // ----------------------------------------------------------------------------
1276 // Public interface
1278 // A Mode value is a set of flags (or 0). They control printing.
1279 type Mode uint
1281 const (
1282 RawFormat Mode = 1 << iota // do not use a tabwriter; if set, UseSpaces is ignored
1283 TabIndent // use tabs for indentation independent of UseSpaces
1284 UseSpaces // use spaces instead of tabs for alignment
1285 SourcePos // emit //line directives to preserve original source positions
1288 // The mode below is not included in printer's public API because
1289 // editing code text is deemed out of scope. Because this mode is
1290 // unexported, it's also possible to modify or remove it based on
1291 // the evolving needs of go/format and cmd/gofmt without breaking
1292 // users. See discussion in CL 240683.
1293 const (
1294 // normalizeNumbers means to canonicalize number
1295 // literal prefixes and exponents while printing.
1297 // This value is known in and used by go/format and cmd/gofmt.
1298 // It is currently more convenient and performant for those
1299 // packages to apply number normalization during printing,
1300 // rather than by modifying the AST in advance.
1301 normalizeNumbers Mode = 1 << 30
1304 // A Config node controls the output of Fprint.
1305 type Config struct {
1306 Mode Mode // default: 0
1307 Tabwidth int // default: 8
1308 Indent int // default: 0 (all code is indented at least by this much)
1311 // fprint implements Fprint and takes a nodesSizes map for setting up the printer state.
1312 func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node any, nodeSizes map[ast.Node]int) (err error) {
1313 // print node
1314 var p printer
1315 p.init(cfg, fset, nodeSizes)
1316 if err = p.printNode(node); err != nil {
1317 return
1319 // print outstanding comments
1320 p.impliedSemi = false // EOF acts like a newline
1321 p.flush(token.Position{Offset: infinity, Line: infinity}, token.EOF)
1323 // output is buffered in p.output now.
1324 // fix //go:build and // +build comments if needed.
1325 p.fixGoBuildLines()
1327 // redirect output through a trimmer to eliminate trailing whitespace
1328 // (Input to a tabwriter must be untrimmed since trailing tabs provide
1329 // formatting information. The tabwriter could provide trimming
1330 // functionality but no tabwriter is used when RawFormat is set.)
1331 output = &trimmer{output: output}
1333 // redirect output through a tabwriter if necessary
1334 if cfg.Mode&RawFormat == 0 {
1335 minwidth := cfg.Tabwidth
1337 padchar := byte('\t')
1338 if cfg.Mode&UseSpaces != 0 {
1339 padchar = ' '
1342 twmode := tabwriter.DiscardEmptyColumns
1343 if cfg.Mode&TabIndent != 0 {
1344 minwidth = 0
1345 twmode |= tabwriter.TabIndent
1348 output = tabwriter.NewWriter(output, minwidth, cfg.Tabwidth, 1, padchar, twmode)
1351 // write printer result via tabwriter/trimmer to output
1352 if _, err = output.Write(p.output); err != nil {
1353 return
1356 // flush tabwriter, if any
1357 if tw, _ := output.(*tabwriter.Writer); tw != nil {
1358 err = tw.Flush()
1361 return
1364 // A CommentedNode bundles an AST node and corresponding comments.
1365 // It may be provided as argument to any of the Fprint functions.
1367 type CommentedNode struct {
1368 Node any // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt
1369 Comments []*ast.CommentGroup
1372 // Fprint "pretty-prints" an AST node to output for a given configuration cfg.
1373 // Position information is interpreted relative to the file set fset.
1374 // The node type must be *ast.File, *CommentedNode, []ast.Decl, []ast.Stmt,
1375 // or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt.
1377 func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node any) error {
1378 return cfg.fprint(output, fset, node, make(map[ast.Node]int))
1381 // Fprint "pretty-prints" an AST node to output.
1382 // It calls Config.Fprint with default settings.
1383 // Note that gofmt uses tabs for indentation but spaces for alignment;
1384 // use format.Node (package go/format) for output that matches gofmt.
1386 func Fprint(output io.Writer, fset *token.FileSet, node any) error {
1387 return (&Config{Tabwidth: 8}).Fprint(output, fset, node)