Internal change.

PiperOrigin-RevId: 333287864
This commit is contained in:
gVisor bot
2020-09-23 07:29:31 -07:00
parent 99decaadd6
commit d00207ff48
3 changed files with 16 additions and 3 deletions
+4 -2
View File
@@ -77,6 +77,7 @@ func main() {
// Create a new declaration slice with all imports at the top, merging any
// redundant imports.
imports := make(map[string]*ast.ImportSpec)
var importNames []string // Keep imports in the original order to get deterministic output.
var anonImports []*ast.ImportSpec
for _, d := range f.Decls {
if g, ok := d.(*ast.GenDecl); ok && g.Tok == token.IMPORT {
@@ -98,6 +99,7 @@ func main() {
}
} else {
imports[n] = i
importNames = append(importNames, n)
}
}
}
@@ -112,8 +114,8 @@ func main() {
Lparen: token.NoPos + 1,
Specs: make([]ast.Spec, 0, l),
}
for _, i := range imports {
d.Specs = append(d.Specs, i)
for _, i := range importNames {
d.Specs = append(d.Specs, imports[i])
}
for _, i := range anonImports {
d.Specs = append(d.Specs, i)
+9 -1
View File
@@ -21,6 +21,7 @@ import (
"go/format"
"go/parser"
"go/token"
"sort"
"strconv"
"gvisor.dev/gvisor/tools/go_generics/globals"
@@ -132,10 +133,17 @@ func updateImports(maps []mapValue, imports mapValue) (ast.Decl, error) {
if len(importsUsed) == 0 {
return nil, nil
}
var names []string
for n := range importsUsed {
names = append(names, n)
}
// Sort the new imports for deterministic build outputs.
sort.Strings(names)
// Create spec array for each new import.
specs := make([]ast.Spec, 0, len(importsUsed))
for _, i := range importsUsed {
for _, n := range names {
i := importsUsed[n]
specs = append(specs, &ast.ImportSpec{
Name: &ast.Ident{Name: i.newName},
Path: &ast.BasicLit{Value: i.path},
@@ -20,6 +20,7 @@ package gomarshal
import (
"fmt"
"go/ast"
"sort"
"strings"
)
@@ -40,6 +41,8 @@ func (g *interfaceGenerator) areFieldsPackedExpression() (string, bool) {
for accessor, _ := range g.as {
cs = append(cs, fmt.Sprintf("%s.Packed()", accessor))
}
// Sort expressions for determinstic build outputs.
sort.Strings(cs)
return strings.Join(cs, " && "), true
}