Skip to content

Commit 7a97c3e

Browse files
author
golangci
authored
Merge pull request #31 from golangci/feature/use-gocyclo-prepared-for-upstreaming
use gocyclo with changes for upstreaming, also speedup it 10x when pr…
2 parents c1f2d06 + f6b51b9 commit 7a97c3e

File tree

9 files changed

+178
-249
lines changed

9 files changed

+178
-249
lines changed

Gopkg.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@
109109
branch = "master"
110110
name = "github.com/golangci/ineffassign"
111111

112-
[[constraint]]
112+
[[override]]
113113
branch = "master"
114114
name = "github.com/golangci/lint"

pkg/astcache/astcache.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package astcache
2+
3+
import (
4+
"go/ast"
5+
"go/parser"
6+
"go/token"
7+
8+
"golang.org/x/tools/go/loader"
9+
)
10+
11+
type File struct {
12+
F *ast.File
13+
Fset *token.FileSet
14+
err error
15+
}
16+
17+
type Cache struct {
18+
m map[string]*File
19+
s []*File
20+
}
21+
22+
func (c Cache) GetAllValidFiles() []*File {
23+
return c.s
24+
}
25+
26+
func (c *Cache) prepareValidFiles() {
27+
files := make([]*File, 0, len(c.m))
28+
for _, f := range c.m {
29+
if f.err != nil || f.F == nil {
30+
continue
31+
}
32+
files = append(files, f)
33+
}
34+
c.s = files
35+
}
36+
37+
func LoadFromProgram(prog *loader.Program) *Cache {
38+
c := &Cache{
39+
m: map[string]*File{},
40+
}
41+
for _, pkg := range prog.InitialPackages() {
42+
for _, f := range pkg.Files {
43+
pos := prog.Fset.Position(0)
44+
c.m[pos.Filename] = &File{
45+
F: f,
46+
Fset: prog.Fset,
47+
}
48+
}
49+
}
50+
51+
c.prepareValidFiles()
52+
return c
53+
}
54+
55+
func LoadFromFiles(files []string) *Cache {
56+
c := &Cache{
57+
m: map[string]*File{},
58+
}
59+
fset := token.NewFileSet()
60+
for _, filePath := range files {
61+
f, err := parser.ParseFile(fset, filePath, nil, parser.ParseComments) // comments needed by e.g. golint
62+
c.m[filePath] = &File{
63+
F: f,
64+
Fset: fset,
65+
err: err,
66+
}
67+
}
68+
69+
c.prepareValidFiles()
70+
return c
71+
}

pkg/commands/run.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/golangci/go-tools/ssa"
1717
"github.com/golangci/go-tools/ssa/ssautil"
1818
"github.com/golangci/golangci-lint/pkg"
19+
"github.com/golangci/golangci-lint/pkg/astcache"
1920
"github.com/golangci/golangci-lint/pkg/config"
2021
"github.com/golangci/golangci-lint/pkg/fsutils"
2122
"github.com/golangci/golangci-lint/pkg/golinters"
@@ -195,12 +196,20 @@ func buildLintCtx(ctx context.Context, linters []pkg.Linter, cfg *config.Config)
195196
ssaProg = buildSSAProgram(ctx, prog)
196197
}
197198

199+
var astCache *astcache.Cache
200+
if prog != nil {
201+
astCache = astcache.LoadFromProgram(prog)
202+
} else {
203+
astCache = astcache.LoadFromFiles(paths.Files)
204+
}
205+
198206
return &golinters.Context{
199207
Paths: paths,
200208
Cfg: cfg,
201209
Program: prog,
202210
SSAProgram: ssaProg,
203211
LoaderConfig: loaderConfig,
212+
ASTCache: astCache,
204213
}, nil
205214
}
206215

pkg/golinters/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package golinters
22

33
import (
44
"github.com/golangci/go-tools/ssa"
5+
"github.com/golangci/golangci-lint/pkg/astcache"
56
"github.com/golangci/golangci-lint/pkg/config"
67
"github.com/golangci/golangci-lint/pkg/fsutils"
78
"golang.org/x/tools/go/loader"
@@ -13,6 +14,7 @@ type Context struct {
1314
Program *loader.Program
1415
SSAProgram *ssa.Program
1516
LoaderConfig *loader.Config
17+
ASTCache *astcache.Cache
1618
}
1719

1820
func (c *Context) Settings() *config.LintersSettings {

pkg/golinters/gocyclo.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package golinters
33
import (
44
"context"
55
"fmt"
6+
"sort"
67

7-
gocycloAPI "github.com/golangci/gocyclo"
8+
gocycloAPI "github.com/golangci/gocyclo/pkg/gocyclo"
89
"github.com/golangci/golangci-lint/pkg/result"
910
)
1011

@@ -19,7 +20,14 @@ func (Gocyclo) Desc() string {
1920
}
2021

2122
func (g Gocyclo) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
22-
stats := gocycloAPI.Run(lintCtx.Paths.MixedPaths())
23+
var stats []gocycloAPI.Stat
24+
for _, f := range lintCtx.ASTCache.GetAllValidFiles() {
25+
stats = gocycloAPI.BuildStats(f.F, f.Fset, stats)
26+
}
27+
28+
sort.Slice(stats, func(i, j int) bool {
29+
return stats[i].Complexity > stats[j].Complexity
30+
})
2331

2432
var res []result.Issue
2533
for _, s := range stats {

vendor/github.com/golangci/gocyclo/README.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)