Skip to content

Commit

Permalink
cmd/cgo: use slices.ContainsFunc
Browse files Browse the repository at this point in the history
Now that Go 1.22.6 is the minimum bootstrap toolchain (cf. CL 606156),
the slices package (introduced in Go 1.21) can be used in packages built
using the bootstrap toolchain.

For #64751

Change-Id: Ib36f39016f57c5e110f78a85ca9c806d91356024
Reviewed-on: https://go-review.googlesource.com/c/go/+/612316
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
tklauser authored and gopherbot committed Sep 11, 2024
1 parent ad6ee21 commit 9deda35
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions src/cmd/cgo/gcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"math"
"os"
"os/exec"
"slices"
"strconv"
"strings"
"unicode"
Expand Down Expand Up @@ -781,16 +782,13 @@ func (p *Package) mangleName(n *Name) {
}

func (f *File) isMangledName(s string) bool {
prefix := "_C"
if strings.HasPrefix(s, prefix) {
t := s[len(prefix):]
for _, k := range nameKinds {
if strings.HasPrefix(t, k+"_") {
return true
}
}
t, ok := strings.CutPrefix(s, "_C")
if !ok {
return false
}
return false
return slices.ContainsFunc(nameKinds, func(k string) bool {
return strings.HasPrefix(t, k+"_")
})
}

// rewriteCalls rewrites all calls that pass pointers to check that
Expand Down Expand Up @@ -1050,12 +1048,9 @@ func (p *Package) hasPointer(f *File, t ast.Expr, top bool) bool {
}
return p.hasPointer(f, t.Elt, top)
case *ast.StructType:
for _, field := range t.Fields.List {
if p.hasPointer(f, field.Type, top) {
return true
}
}
return false
return slices.ContainsFunc(t.Fields.List, func(field *ast.Field) bool {
return p.hasPointer(f, field.Type, top)
})
case *ast.StarExpr: // Pointer type.
if !top {
return true
Expand Down Expand Up @@ -3202,12 +3197,9 @@ func (c *typeConv) dwarfHasPointer(dt dwarf.Type, pos token.Pos) bool {
return c.dwarfHasPointer(dt.Type, pos)

case *dwarf.StructType:
for _, f := range dt.Field {
if c.dwarfHasPointer(f.Type, pos) {
return true
}
}
return false
return slices.ContainsFunc(dt.Field, func(f *dwarf.StructField) bool {
return c.dwarfHasPointer(f.Type, pos)
})

case *dwarf.TypedefType:
if dt.Name == "_GoString_" || dt.Name == "_GoBytes_" {
Expand Down

0 comments on commit 9deda35

Please sign in to comment.