forked from shoooooman/go-complexity-analysis
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomplexity.go
553 lines (516 loc) · 12.8 KB
/
complexity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package complexity
import (
"flag"
"fmt"
"math"
"go/ast"
"go/token"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const docComp = "complexity is cyclomatic complexity and maintanability index analyzer"
// Analyzer is ...
var Analyzer = &analysis.Analyzer{
Name: "complexity",
Doc: docComp,
Run: runComp,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
// FuncStatsType is statistics of a single function
type FuncStatsType struct {
Filename string
Line int
FunctionName string
LOC int
ConstantsLOC int
CyclomaticComplexity int
MaintenabilityIndex int
HalsbreadDifficulty float64
HalsbreadVolume float64
TimeToCode float64
IsTooComplex bool
IsNotMaintenable bool
}
// FuncStatsCallback is called on each processed function statictics
// Main is to define its own callback logic instead.
var FuncStatsCallback = func(s FuncStatsType) {}
var (
CycloOver int
MaintUnder int
SkipFileFnc = func(filename string) bool { return false }
)
func init() {
flag.IntVar(&CycloOver, "cycloover", 10, "print functions with the Cyclomatic complexity > N")
flag.IntVar(&MaintUnder, "maintunder", 20, "print functions with the Maintainability index < N")
}
func runComp(pass *analysis.Pass) (facts interface{}, err error) {
inspector, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, fmt.Errorf("internal error, wrong inspector.Inspector type")
}
inspector.Preorder([]ast.Node{(*ast.File)(nil)}, func(n ast.Node) {
if SkipFileFnc(pass.Fset.File(n.Pos()).Name()) {
return
}
astVisitFunctions(n, func(nn *ast.FuncDecl) {
stats := calcFuncStats(pass, nn)
reportFnc := func(msg string, args ...interface{}) {
pass.Reportf(nn.Pos(), msg, args...)
}
reportFuncStats(reportFnc, stats)
FuncStatsCallback(stats)
})
})
return
}
type branchVisitor func(n ast.Node) (w ast.Visitor)
// Visit is callback from ast to visit the node
func (v branchVisitor) Visit(n ast.Node) (w ast.Visitor) {
return v(n)
}
func calcFuncStats(pass *analysis.Pass, n *ast.FuncDecl) FuncStatsType {
nPos := n.Pos()
pos := pass.Fset.File(nPos).Position(nPos)
stats := FuncStatsType{
Filename: pos.Filename,
Line: pos.Line,
FunctionName: n.Name.Name,
LOC: countLOC(pass.Fset, n),
ConstantsLOC: countVarsLOC(pass.Fset, n),
CyclomaticComplexity: calcCycloComp(n),
}
stats.HalsbreadDifficulty, stats.HalsbreadVolume = calcHalstComp(n)
stats.MaintenabilityIndex = calcMaintIndex(stats.HalsbreadVolume, stats.CyclomaticComplexity, stats.LOC)
stats.IsTooComplex = stats.CyclomaticComplexity > CycloOver
stats.IsNotMaintenable = stats.MaintenabilityIndex < MaintUnder
stats.TimeToCode = stats.HalsbreadDifficulty * stats.HalsbreadVolume / (18 * 3600)
return stats
}
func astVisitFunctions(n ast.Node, cb func(*ast.FuncDecl)) {
var v ast.Visitor
v = branchVisitor(func(nn ast.Node) ast.Visitor {
switch nnn := nn.(type) {
case *ast.FuncDecl:
cb(nnn)
}
return v
})
ast.Walk(v, n)
}
func calcHalstComp(fd *ast.FuncDecl) (difficulty float64, volume float64) {
operators, operands := map[string]int{}, map[string]int{}
walkDecl(fd, operators, operands)
distOpt := len(operators) // distinct operators
distOpd := len(operands) // distinct operands
var sumOpt, sumOpd int
for _, val := range operators {
sumOpt += val
}
for _, val := range operands {
sumOpd += val
}
nVocab := distOpt + distOpd
length := sumOpt + sumOpd
volume = float64(length) * log2Of(float64(nVocab))
divisor := float64(2 * distOpd)
if distOpd == 0 {
divisor = 0.0000000000001
}
difficulty = float64(distOpt*sumOpd) / divisor
return
}
func walkDecl(n ast.Node, opt map[string]int, opd map[string]int) {
switch n := n.(type) {
case *ast.GenDecl:
appendValidSymb(n.Lparen.IsValid(), n.Rparen.IsValid(), opt, "()")
if n.Tok.IsOperator() {
opt[n.Tok.String()]++
} else {
opd[n.Tok.String()]++
}
for _, s := range n.Specs {
walkSpec(s, opt, opd)
}
case *ast.FuncDecl:
if n.Recv == nil {
opt["func"]++
opt[n.Name.Name]++
opt["()"]++
} else {
opt["func"]++
opt[n.Name.Name]++
opt["()"] += 2
}
walkStmt(n.Body, opt, opd)
}
}
func walkStmt(n ast.Node, opt map[string]int, opd map[string]int) {
switch n := n.(type) {
case *ast.DeclStmt:
walkDecl(n.Decl, opt, opd)
case *ast.ExprStmt:
walkExpr(n.X, opt, opd)
case *ast.SendStmt:
walkExpr(n.Chan, opt, opd)
if n.Arrow.IsValid() {
opt["<-"]++
}
walkExpr(n.Value, opt, opd)
case *ast.IncDecStmt:
walkExpr(n.X, opt, opd)
if n.Tok.IsOperator() {
opt[n.Tok.String()]++
}
case *ast.AssignStmt:
if n.Tok.IsOperator() {
opt[n.Tok.String()]++
}
for _, exp := range n.Lhs {
walkExpr(exp, opt, opd)
}
for _, exp := range n.Rhs {
walkExpr(exp, opt, opd)
}
case *ast.GoStmt:
if n.Go.IsValid() {
opt["go"]++
}
walkExpr(n.Call, opt, opd)
case *ast.DeferStmt:
if n.Defer.IsValid() {
opt["defer"]++
}
walkExpr(n.Call, opt, opd)
case *ast.ReturnStmt:
if n.Return.IsValid() {
opt["return"]++
}
for _, e := range n.Results {
walkExpr(e, opt, opd)
}
case *ast.BranchStmt:
if n.Tok.IsOperator() {
opt[n.Tok.String()]++
} else {
opd[n.Tok.String()]++
}
if n.Label != nil {
walkExpr(n.Label, opt, opd)
}
case *ast.BlockStmt:
appendValidSymb(n.Lbrace.IsValid(), n.Rbrace.IsValid(), opt, "{}")
for _, s := range n.List {
walkStmt(s, opt, opd)
}
case *ast.IfStmt:
if n.If.IsValid() {
opt["if"]++
}
if n.Init != nil {
walkStmt(n.Init, opt, opd)
}
walkExpr(n.Cond, opt, opd)
walkStmt(n.Body, opt, opd)
if n.Else != nil {
opt["else"]++
walkStmt(n.Else, opt, opd)
}
case *ast.SwitchStmt:
if n.Switch.IsValid() {
opt["switch"]++
}
if n.Init != nil {
walkStmt(n.Init, opt, opd)
}
if n.Tag != nil {
walkExpr(n.Tag, opt, opd)
}
walkStmt(n.Body, opt, opd)
case *ast.SelectStmt:
if n.Select.IsValid() {
opt["select"]++
}
walkStmt(n.Body, opt, opd)
case *ast.ForStmt:
if n.For.IsValid() {
opt["for"]++
}
if n.Init != nil {
walkStmt(n.Init, opt, opd)
}
if n.Cond != nil {
walkExpr(n.Cond, opt, opd)
}
if n.Post != nil {
walkStmt(n.Post, opt, opd)
}
walkStmt(n.Body, opt, opd)
case *ast.RangeStmt:
if n.For.IsValid() {
opt["for"]++
}
if n.Key != nil {
walkExpr(n.Key, opt, opd)
if n.Tok.IsOperator() {
opt[n.Tok.String()]++
} else {
opd[n.Tok.String()]++
}
}
if n.Value != nil {
walkExpr(n.Value, opt, opd)
}
opt["range"]++
walkExpr(n.X, opt, opd)
walkStmt(n.Body, opt, opd)
case *ast.CaseClause:
if n.List == nil {
opt["default"]++
} else {
for _, c := range n.List {
walkExpr(c, opt, opd)
}
}
if n.Colon.IsValid() {
opt[":"]++
}
if n.Body != nil {
for _, b := range n.Body {
walkStmt(b, opt, opd)
}
}
}
}
func walkSpec(spec ast.Spec, opt map[string]int, opd map[string]int) {
switch spec := spec.(type) {
case *ast.ValueSpec:
for _, n := range spec.Names {
walkExpr(n, opt, opd)
if spec.Type != nil {
walkExpr(spec.Type, opt, opd)
}
if spec.Values != nil {
for _, v := range spec.Values {
walkExpr(v, opt, opd)
}
}
}
}
}
func walkExpr(exp ast.Expr, opt map[string]int, opd map[string]int) {
switch exp := exp.(type) {
case *ast.ParenExpr:
appendValidSymb(exp.Lparen.IsValid(), exp.Rparen.IsValid(), opt, "()")
walkExpr(exp.X, opt, opd)
case *ast.SelectorExpr:
walkExpr(exp.X, opt, opd)
walkExpr(exp.Sel, opt, opd)
case *ast.IndexExpr:
walkExpr(exp.X, opt, opd)
appendValidSymb(exp.Lbrack.IsValid(), exp.Rbrack.IsValid(), opt, "{}")
walkExpr(exp.Index, opt, opd)
case *ast.SliceExpr:
walkExpr(exp.X, opt, opd)
appendValidSymb(exp.Lbrack.IsValid(), exp.Rbrack.IsValid(), opt, "[]")
if exp.Low != nil {
walkExpr(exp.Low, opt, opd)
}
if exp.High != nil {
walkExpr(exp.High, opt, opd)
}
if exp.Max != nil {
walkExpr(exp.Max, opt, opd)
}
case *ast.TypeAssertExpr:
walkExpr(exp.X, opt, opd)
appendValidSymb(exp.Lparen.IsValid(), exp.Rparen.IsValid(), opt, "()")
if exp.Type != nil {
walkExpr(exp.Type, opt, opd)
}
case *ast.CallExpr:
walkExpr(exp.Fun, opt, opd)
appendValidSymb(exp.Lparen.IsValid(), exp.Rparen.IsValid(), opt, "()")
if exp.Ellipsis != 0 {
opt["..."]++
}
for _, a := range exp.Args {
walkExpr(a, opt, opd)
}
case *ast.StarExpr:
if exp.Star.IsValid() {
opt["*"]++
}
walkExpr(exp.X, opt, opd)
case *ast.UnaryExpr:
if exp.Op.IsOperator() {
opt[exp.Op.String()]++
} else {
opd[exp.Op.String()]++
}
walkExpr(exp.X, opt, opd)
case *ast.BinaryExpr:
walkExpr(exp.X, opt, opd)
opt[exp.Op.String()]++
walkExpr(exp.Y, opt, opd)
case *ast.KeyValueExpr:
walkExpr(exp.Key, opt, opd)
if exp.Colon.IsValid() {
opt[":"]++
}
walkExpr(exp.Value, opt, opd)
case *ast.BasicLit:
if exp.Kind.IsLiteral() {
opd[exp.Value]++
} else {
opt[exp.Value]++
}
case *ast.FuncLit:
walkExpr(exp.Type, opt, opd)
walkStmt(exp.Body, opt, opd)
case *ast.CompositeLit:
appendValidSymb(exp.Lbrace.IsValid(), exp.Rbrace.IsValid(), opt, "{}")
if exp.Type != nil {
walkExpr(exp.Type, opt, opd)
}
for _, e := range exp.Elts {
walkExpr(e, opt, opd)
}
case *ast.Ident:
if exp.Obj == nil {
opt[exp.Name]++
} else {
opd[exp.Name]++
}
case *ast.Ellipsis:
if exp.Ellipsis.IsValid() {
opt["..."]++
}
if exp.Elt != nil {
walkExpr(exp.Elt, opt, opd)
}
case *ast.FuncType:
if exp.Func.IsValid() {
opt["func"]++
}
appendValidSymb(true, true, opt, "()")
if exp.Params.List != nil {
for _, f := range exp.Params.List {
walkExpr(f.Type, opt, opd)
}
}
case *ast.ChanType:
if exp.Begin.IsValid() {
opt["chan"]++
}
if exp.Arrow.IsValid() {
opt["<-"]++
}
walkExpr(exp.Value, opt, opd)
}
}
func appendValidSymb(lvalid bool, rvalid bool, opt map[string]int, symb string) {
if lvalid && rvalid {
opt[symb]++
}
}
// calcMaintComp calculates the maintainability index
// source: https://docs.microsoft.com/en-us/archive/blogs/codeanalysis/maintainability-index-range-and-meaning
func calcMaintIndex(halstComp float64, cycloComp, loc int) int {
origVal := 171.0 - 5.2*logOf(halstComp) - 0.23*float64(cycloComp) - 16.2*logOf(float64(loc))
normVal := int(math.Max(0.0, origVal*100.0/171.0))
return normVal
}
func logOf(val float64) float64 {
switch val {
case 0:
return 0
default:
return math.Log(val)
}
}
func log2Of(val float64) float64 {
switch val {
case 0:
return 0
default:
return math.Log2(val)
}
}
// calcCycloComp calculates the Cyclomatic complexity
func calcCycloComp(fd *ast.FuncDecl) int {
comp := 1
var v ast.Visitor
v = branchVisitor(func(n ast.Node) (w ast.Visitor) {
switch n := n.(type) {
case *ast.GoStmt: // subroutines are double complexity
comp += 2
case *ast.SendStmt: // writing to channels
comp++
case *ast.UnaryExpr:
if n.Op == token.ARROW { // channel reading
comp++
}
case *ast.IfStmt:
comp++
if _, ok := n.Else.(*ast.BlockStmt); ok { // include final else
comp++
}
case *ast.ForStmt, *ast.RangeStmt, *ast.SelectStmt, *ast.SwitchStmt:
comp++
case *ast.BinaryExpr:
if n.Op == token.LAND || n.Op == token.LOR {
comp++
}
}
return v
})
ast.Walk(v, fd)
return comp
}
func countVarsLOC(fs *token.FileSet, n *ast.FuncDecl) int {
loc := 0
var v ast.Visitor
v = branchVisitor(func(nn ast.Node) ast.Visitor {
switch nnn := nn.(type) {
case *ast.ValueSpec:
loc += countLOC(fs, nn)
case *ast.AssignStmt:
if nnn.Tok == token.DEFINE { // variable declaration & assignment
loc += countLOC(fs, nn)
}
}
return v
})
ast.Walk(v, n)
return loc
}
// counts lines of a function
func countLOC(fs *token.FileSet, n ast.Node) int {
f := fs.File(n.Pos())
startLine := f.Line(n.Pos())
endLine := f.Line(n.End())
return endLine - startLine + 1
}
func reportFuncStats(reportFnc func(msg string, args ...interface{}), stats FuncStatsType) {
if flag.Lookup("test.v") != nil {
// Only when `go test`
reportFnc("Cyclomatic complexity: %d, Halstead difficulty: %0.3f, volume: %0.3f", stats.CyclomaticComplexity, stats.HalsbreadDifficulty, stats.HalsbreadVolume)
return
}
msg := ToDiagnosticMsg(stats)
if msg != "" {
reportFnc("%s:%d: %s\n", stats.Filename, stats.Line, msg)
}
}
// ToDiagnosticMsg is used to form diagnostic message for not-good functions
func ToDiagnosticMsg(stats FuncStatsType) (msg string) {
if stats.IsTooComplex {
msg = fmt.Sprintf("func %s seems to be complex (cyclomatic complexity=%d)", stats.FunctionName, stats.CyclomaticComplexity)
} else if stats.IsNotMaintenable {
msg = fmt.Sprintf("func %s seems to have low maintainability (maintainability index=%d)", stats.FunctionName, stats.MaintenabilityIndex)
}
return
}