Skip to content

Commit

Permalink
ruleguard: use stdinfo PathByName and don't make excessive copies (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
quasilyte committed Jan 30, 2022
1 parent 8479df4 commit a4ee291
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
4 changes: 2 additions & 2 deletions ruleguard/engine.go
Expand Up @@ -64,7 +64,7 @@ func (e *engine) Load(ctx *LoadContext, buildContext *build.Context, filename st
pkg: pkg,
ctx: ctx,
importer: imp,
itab: typematch.NewImportsTab(stdinfo.PackagesList),
itab: typematch.NewImportsTab(stdinfo.PathByName),
gogrepFset: token.NewFileSet(),
}
l := newIRLoader(config)
Expand Down Expand Up @@ -97,7 +97,7 @@ func (e *engine) LoadFromIR(ctx *LoadContext, buildContext *build.Context, filen
state: e.state,
ctx: ctx,
importer: imp,
itab: typematch.NewImportsTab(stdinfo.PackagesList),
itab: typematch.NewImportsTab(stdinfo.PathByName),
gogrepFset: token.NewFileSet(),
}
l := newIRLoader(config)
Expand Down
9 changes: 2 additions & 7 deletions ruleguard/typematch/typematch.go
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/quasilyte/go-ruleguard/internal/xtypes"
"github.com/quasilyte/stdinfo"
)

//go:generate stringer -type=patternOp
Expand Down Expand Up @@ -61,12 +60,8 @@ type ImportsTab struct {
imports []map[string]string
}

func NewImportsTab(initial []stdinfo.Package) *ImportsTab {
convertMap := make(map[string]string)
for _, pack := range initial {
convertMap[pack.Name] = pack.Path
}
return &ImportsTab{imports: []map[string]string{convertMap}}
func NewImportsTab(initial map[string]string) *ImportsTab {
return &ImportsTab{imports: []map[string]string{initial}}
}

func (itab *ImportsTab) Lookup(pkgName string) (string, bool) {
Expand Down
11 changes: 4 additions & 7 deletions ruleguard/typematch/typematch_test.go
Expand Up @@ -5,8 +5,6 @@ import (
"go/types"
"path"
"testing"

"github.com/quasilyte/stdinfo"
)

var (
Expand All @@ -16,10 +14,6 @@ var (
typeUint8 = types.Typ[types.Uint8]
typeUnsafePtr = types.Typ[types.UnsafePointer]
typeEstruct = types.NewStruct(nil, nil)
packages = [2]stdinfo.Package{
{Name: "io", Path: "io"},
{Name: "syntax", Path: "regexp/syntax"},
}

stringerIface = types.NewInterfaceType([]*types.Func{
types.NewFunc(token.NoPos, nil, "String",
Expand All @@ -32,7 +26,10 @@ var (
stringVar = types.NewVar(token.NoPos, nil, "_", typeString)

testContext = &Context{
Itab: NewImportsTab(packages[:]),
Itab: NewImportsTab(map[string]string{
"io": "io",
"syntax": "regexp/syntax",
}),
}
)

Expand Down

0 comments on commit a4ee291

Please sign in to comment.