Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruleguard: use stdinfo PathByName and don't make excessive copies #373

Merged
merged 1 commit into from Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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