File | File | |
| | @@ -1,65 +1,119 @@
|
1 | | // Package generated provides a function that parses a Go file and reports
|
| 1 | // Package generated provides a function that parses a source file and reports
|
2 | 2 | // whether it contains a "// Code generated … DO NOT EDIT." line comment.
|
3 | 3 | //
|
4 | | // It implements the specification at https://golang.org/s/generatedcode.
|
| 4 | // It implements the specification at https://go.dev/s/generatedcode.
|
5 | 5 | //
|
6 | 6 | // The first priority is correctness (no false negatives, no false positives).
|
7 | | // It must return accurate results even if the input Go source code is not gofmted.
|
| 7 | // It must return accurate results even if the input source code is formatted
|
| 8 | // unconventionally.
|
8 | 9 | //
|
9 | 10 | // The second priority is performance. The current version uses bufio.Reader and
|
10 | 11 | // ReadBytes. Performance can be optimized further by using lower level I/O
|
11 | | // primitives and allocating less. That can be explored later. A lot of the time
|
12 | | // is spent on reading the entire file without being able to stop early,
|
13 | | // since the specification allows the comment to appear anywhere in the file.
|
| 12 | // primitives and allocating less. That can be explored later.
|
14 | 13 | package generated
|
15 | 14 |
|
16 | 15 | import (
|
17 | 16 | "bufio"
|
18 | 17 | "bytes"
|
19 | 18 | "io"
|
20 | 19 | "os"
|
21 | 20 | )
|
22 | 21 |
|
23 | | // Parse parses the source code of a single Go source file
|
24 | | // provided via src, and reports whether the file contains
|
25 | | // a "// Code generated ... DO NOT EDIT." line comment
|
26 | | // matching the specification at https://golang.org/s/generatedcode:
|
| 22 | // Parse parses a source file provided via src, and reports whether
|
| 23 | // the file contains a "// Code generated ... DO NOT EDIT." line comment
|
| 24 | // matching the specification at https://go.dev/s/generatedcode:
|
27 | 25 | //
|
28 | | // Generated files are marked by a line of text that matches
|
29 | | // the regular expression, in Go syntax:
|
| 26 | // To convey to humans and machine tools that code is generated,
|
| 27 | // generated source should have a line that matches the following
|
| 28 | // regular expression (in Go syntax):
|
30 | 29 | //
|
31 | 30 | // ^// Code generated .* DO NOT EDIT\.$
|
32 | 31 | //
|
33 | | // The .* means the tool can put whatever folderol it wants in there,
|
34 | | // but the comment must be a single line and must start with Code generated
|
35 | | // and end with DO NOT EDIT., with a period.
|
36 | | //
|
37 | | // The text may appear anywhere in the file.
|
| 32 | // This line must appear before the first non-comment, non-blank
|
| 33 | // text in the file.
|
38 | 34 | func Parse(src io.Reader) (hasGeneratedComment bool, err error) {
|
39 | 35 | br := bufio.NewReader(src)
|
| 36 | // Use inBlock to track whether we're inside a multi-line
|
| 37 | // /* */ comment block across calls to containsNonComment.
|
| 38 | var inBlock bool
|
40 | 39 | for {
|
41 | 40 | s, err := br.ReadBytes('\n')
|
42 | 41 | if err == io.EOF {
|
43 | | return containsComment(s), nil
|
| 42 | return containsGenComment(s), nil
|
44 | 43 | } else if err != nil {
|
45 | 44 | return false, err
|
46 | 45 | }
|
47 | 46 | if len(s) >= 2 && s[len(s)-2] == '\r' {
|
48 | 47 | s = s[:len(s)-2] // Trim "\r\n".
|
49 | 48 | } else {
|
50 | 49 | s = s[:len(s)-1] // Trim "\n".
|
51 | 50 | }
|
52 | | if containsComment(s) {
|
| 51 | if containsGenComment(s) {
|
53 | 52 | return true, nil
|
| 53 | } else if containsNonComment(s, &inBlock) {
|
| 54 | return false, nil
|
54 | 55 | }
|
55 | 56 | }
|
56 | 57 | }
|
57 | 58 |
|
58 | | // containsComment reports whether a line of Go source code s (without newline character)
|
| 59 | // containsNonComment reports whether a line of source code s (without newline)
|
| 60 | // contains something other than a line comment, block comment, or white space.
|
| 61 | func containsNonComment(s []byte, inBlock *bool) bool {
|
| 62 | type state int
|
| 63 | const (
|
| 64 | normal state = iota
|
| 65 | normalSlash
|
| 66 | block
|
| 67 | blockStar
|
| 68 | )
|
| 69 | var p state // Parser state.
|
| 70 | if *inBlock {
|
| 71 | p = block
|
| 72 | }
|
| 73 | for _, c := range s {
|
| 74 | switch p {
|
| 75 | case normal:
|
| 76 | switch c {
|
| 77 | case ' ', '\t': // White space, ignore.
|
| 78 | case '/':
|
| 79 | p = normalSlash
|
| 80 | default: // Non-comment found.
|
| 81 | return true // Return early and don't bother updating *inBlock since it won't matter.
|
| 82 | }
|
| 83 | case normalSlash:
|
| 84 | switch c {
|
| 85 | case '/': // Start of inline comment, "//". Ignore the rest of the line.
|
| 86 | *inBlock = false
|
| 87 | return false
|
| 88 | case '*': // Start of comment block, "/*".
|
| 89 | p = block
|
| 90 | default: // Non-comment found.
|
| 91 | return true // Return early and don't bother updating *inBlock since it won't matter.
|
| 92 | }
|
| 93 | case block:
|
| 94 | switch c {
|
| 95 | case '*':
|
| 96 | p = blockStar
|
| 97 | }
|
| 98 | case blockStar:
|
| 99 | switch c {
|
| 100 | case '/': // End of comment block, "*/".
|
| 101 | p = normal
|
| 102 | case '*': // Another '*', stay in blockStar.
|
| 103 | default:
|
| 104 | p = block
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 | *inBlock = p >= block
|
| 109 | return p == normalSlash
|
| 110 | }
|
| 111 |
|
| 112 | // containsGenComment reports whether a line of source code s (without newline)
|
59 | 113 | // contains the generated comment.
|
60 | | func containsComment(s []byte) bool {
|
| 114 | func containsGenComment(s []byte) bool {
|
61 | 115 | return len(s) >= len(prefix)+len(suffix) &&
|
62 | 116 | bytes.HasPrefix(s, prefix) &&
|
63 | 117 | bytes.HasSuffix(s, suffix)
|
64 | 118 | }
|
65 | 119 |
|