This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
doc.go
97 lines (66 loc) · 2.67 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Grind polishes Go programs.
Usage:
grind [-diff] [-v] packagepath...
Grind rewrites the source files in the named packages.
When grind rewrites a file, it prints a line to standard
error giving the name of the file and the rewrites applied.
As a special case, if the arguments are a list of Go source files,
they are considered to make up a single package, which
is then rewritten.
If the -diff flag is set, no files are rewritten.
Instead grind prints the differences a rewrite would introduce.
Grind does not make backup copies of the files that it edits.
Instead, use a version control system's ``diff'' functionality to inspect
the changes that grind makes before committing them.
Grind is a work in progress. More rewrites are planned.
The initial use case for grind is cleaning up Go code that looks
like C code.
Dead Code Elimination
Grind removes unreachable (dead) code. Code is considered
unreachable if it is not the target of a goto statement and is
preceded by a terminating statement
(see golang.org/ref/spec#Terminating_statements),
or a break or continue statement.
Goto Inlining
If the target of a goto is a block of code that is only reachable
by following that goto statement, grind replaces the goto with
a copy of the target code and deletes the original target code.
If the target of a goto is an explicit or implicit return statement,
replaces the goto with a copy of the return statement.
Unused Label Removal
Grind removes unused labels.
Var Declaration Insertion
Grind rewrites := declarations of complex zero types, such as:
x := (*T)(nil)
y := T{} // T a struct or array type
to use plain var declarations, as in:
var x *T
var y T
Var Declaration Placement
Grind moves var declarations as close as possible to their uses.
When possible, it combines the declaration with the initialization,
and it splits disjoint uses of a single variable into multiple variables.
For example, consider:
var i int
...
for i = 0; i < 10; i++ {
use(i)
}
...
for i = 0; i < 10; i++ {
otherUse(i)
}
Grind deletes the declaration and rewrites both loop initializers
to use a combined declaration and assignment (i := 0).
Limitation: Grind does not move variable declarations into loop bodies.
It may in the future, provided it can determine that the variable
is dead on entry to the body and that the variable does not
escape (causing heap allocation inside the loop).
Limitation: Grind considers variables that appear in closures off limits.
A more sophisticated analysis might consider them in the future.
*/
package main