mirror of
https://github.com/burrowers/garble.git
synced 2025-12-24 12:58:05 +08:00
start using go/ast.Preorder
Thanks to being able to use range-over-func, some control flow in our code gets simplified.
This commit is contained in:
@@ -175,7 +175,7 @@ func handleCompositeLiteral(obfRand *obfRand, isPointer bool, node *ast.Composit
|
|||||||
//
|
//
|
||||||
// We don't set any "end" or middle positions, because they seem irrelevant.
|
// We don't set any "end" or middle positions, because they seem irrelevant.
|
||||||
func withPos(node ast.Node, pos token.Pos) ast.Node {
|
func withPos(node ast.Node, pos token.Pos) ast.Node {
|
||||||
ast.Inspect(node, func(node ast.Node) bool {
|
for node := range ast.Preorder(node) {
|
||||||
switch node := node.(type) {
|
switch node := node.(type) {
|
||||||
case *ast.BasicLit:
|
case *ast.BasicLit:
|
||||||
node.ValuePos = pos
|
node.ValuePos = pos
|
||||||
@@ -207,8 +207,7 @@ func withPos(node ast.Node, pos token.Pos) ast.Node {
|
|||||||
case *ast.BranchStmt:
|
case *ast.BranchStmt:
|
||||||
node.TokPos = pos
|
node.TokPos = pos
|
||||||
}
|
}
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ func printFile(lpkg *listedPackage, file *ast.File) ([]byte, error) {
|
|||||||
// we use a list of offsets indexed by identifiers in source order.
|
// we use a list of offsets indexed by identifiers in source order.
|
||||||
var origCallOffsets []int
|
var origCallOffsets []int
|
||||||
nextOffset := -1
|
nextOffset := -1
|
||||||
ast.Inspect(file, func(node ast.Node) bool {
|
for node := range ast.Preorder(file) {
|
||||||
switch node := node.(type) {
|
switch node := node.(type) {
|
||||||
case *ast.CallExpr:
|
case *ast.CallExpr:
|
||||||
nextOffset = fsetFile.Position(node.Pos()).Offset
|
nextOffset = fsetFile.Position(node.Pos()).Offset
|
||||||
@@ -79,8 +79,7 @@ func printFile(lpkg *listedPackage, file *ast.File) ([]byte, error) {
|
|||||||
origCallOffsets = append(origCallOffsets, nextOffset)
|
origCallOffsets = append(origCallOffsets, nextOffset)
|
||||||
nextOffset = -1
|
nextOffset = -1
|
||||||
}
|
}
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
|
|
||||||
copied := 0
|
copied := 0
|
||||||
printBuf2.Reset()
|
printBuf2.Reset()
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ One can reverse a captured panic stack trace as follows:
|
|||||||
fieldToStruct := computeFieldToStruct(info)
|
fieldToStruct := computeFieldToStruct(info)
|
||||||
for i, file := range files {
|
for i, file := range files {
|
||||||
goFile := lpkg.CompiledGoFiles[i]
|
goFile := lpkg.CompiledGoFiles[i]
|
||||||
ast.Inspect(file, func(node ast.Node) bool {
|
for node := range ast.Preorder(file) {
|
||||||
switch node := node.(type) {
|
switch node := node.(type) {
|
||||||
|
|
||||||
// Replace names.
|
// Replace names.
|
||||||
@@ -125,9 +125,7 @@ One can reverse a captured panic stack trace as follows:
|
|||||||
fmt.Sprintf("%s/%s", lpkg.ImportPath, goFile),
|
fmt.Sprintf("%s/%s", lpkg.ImportPath, goFile),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return true
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
repl := strings.NewReplacer(replaces...)
|
repl := strings.NewReplacer(replaces...)
|
||||||
|
|||||||
@@ -201,16 +201,13 @@ func stripRuntime(basename string, file *ast.File) {
|
|||||||
}
|
}
|
||||||
case "runtime1.go":
|
case "runtime1.go":
|
||||||
usesEnv := func(node ast.Node) bool {
|
usesEnv := func(node ast.Node) bool {
|
||||||
seen := false
|
for node := range ast.Preorder(node) {
|
||||||
ast.Inspect(node, func(node ast.Node) bool {
|
|
||||||
ident, ok := node.(*ast.Ident)
|
ident, ok := node.(*ast.Ident)
|
||||||
if ok && ident.Name == "gogetenv" {
|
if ok && ident.Name == "gogetenv" {
|
||||||
seen = true
|
return true
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
return true
|
}
|
||||||
})
|
return false
|
||||||
return seen
|
|
||||||
}
|
}
|
||||||
filenames:
|
filenames:
|
||||||
switch funcDecl.Name.Name {
|
switch funcDecl.Name.Name {
|
||||||
|
|||||||
Reference in New Issue
Block a user