From 678b05acf44d1c0aa15cfb6fea39675075bc34de Mon Sep 17 00:00:00 2001 From: Frederik Zipp Date: Tue, 21 Jan 2014 18:03:23 +0100 Subject: [PATCH] initial commit --- LICENSE | 27 +++++++ README.md | 30 +++++++ gocyclo.go | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 282 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 gocyclo.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..45f88d6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2013 Frederik Zipp. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of the copyright owner nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..797d7bd --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +Gocyclo calculates cyclomatic complexities of functions in Go source code. + +The cyclomatic complexity of a function is calculated according to the +following rules: + + 1 is the base complexity of a function + +1 for each 'if', 'for', 'case', '&&' or '||' + +To install, run + + $ go get github.com/fzipp/gocyclo + +and put the resulting binary in one of your PATH directories if +`$GOPATH/bin` isn't already in your PATH. + +Usage: + + $ gocyclo [ ...] ... + +Examples: + + $ gocyclo . + $ gocyclo main.go + $ gocyclo -top 10 src/ + $ gocyclo -over 25 docker + +The output fields for each line are: + + + diff --git a/gocyclo.go b/gocyclo.go new file mode 100644 index 0000000..012ec20 --- /dev/null +++ b/gocyclo.go @@ -0,0 +1,225 @@ +// Copyright 2013 Frederik Zipp. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Gocyclo calculates the cyclomatic complexities of functions and +// methods in Go source code. +// +// Usage: +// gocyclo [ ...] ... +// +// Flags +// -over N show functions with complexity > N only and +// return exit code 1 if the output is non-empty +// -top N show the top N most complex functions only +// -avg show the average complexity +// +// The output fields for each line are: +// +package main + +import ( + "flag" + "fmt" + "go/ast" + "go/parser" + "go/token" + "io" + "os" + "path/filepath" + "sort" + "strings" +) + +const usageDoc = `Calculate cyclomatic complexities of Go functions. +usage: + gocyclo [ ...] ... + +Flags + -over N show functions with complexity > N only and + return exit code 1 if the set is non-empty + -top N show the top N most complex functions only + -avg show the average complexity over all functions, + not depending on whether -over or -top are set + +The output fields for each line are: + +` + +func usage() { + fmt.Fprintf(os.Stderr, usageDoc) + os.Exit(2) +} + +var ( + over = flag.Int("over", 0, "show functions with complexity > N only") + top = flag.Int("top", -1, "show the top N most complex functions only") + avg = flag.Bool("avg", false, "show the average complexity") +) + +func main() { + flag.Usage = usage + flag.Parse() + args := flag.Args() + if len(args) == 0 { + usage() + } + + stats := analyze(args) + sort.Sort(byComplexity(stats)) + written := writeStats(os.Stdout, stats) + + if *avg { + showAverage(stats) + } + + if *over > 0 && written > 0 { + os.Exit(1) + } +} + +func analyze(paths []string) []stat { + stats := make([]stat, 0) + for _, path := range paths { + if isDir(path) { + stats = analyzeDir(path, stats) + } else { + stats = analyzeFile(path, stats) + } + } + return stats +} + +func isDir(filename string) bool { + fi, err := os.Stat(filename) + return err == nil && fi.IsDir() +} + +func analyzeFile(fname string, stats []stat) []stat { + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, fname, nil, 0) + if err != nil { + exitError(err) + } + return buildStats(f, fset, stats) +} + +func analyzeDir(dirname string, stats []stat) []stat { + filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error { + if err == nil && !info.IsDir() && strings.HasSuffix(path, ".go") { + stats = analyzeFile(path, stats) + } + return err + }) + return stats +} + +func exitError(err error) { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) +} + +func writeStats(w io.Writer, sortedStats []stat) int { + for i, stat := range sortedStats { + if i == *top { + return i + } + if stat.Complexity <= *over { + return i + } + fmt.Fprintln(w, stat) + } + return len(sortedStats) +} + +func showAverage(stats []stat) { + fmt.Printf("Average: %.3g\n", average(stats)) +} + +func average(stats []stat) float64 { + total := 0 + for _, s := range stats { + total += s.Complexity + } + return float64(total) / float64(len(stats)) +} + +type stat struct { + PkgName string + FuncName string + Complexity int + Pos token.Position +} + +func (s stat) String() string { + return fmt.Sprintf("%d %s %s %s", s.Complexity, s.PkgName, s.FuncName, s.Pos) +} + +type byComplexity []stat + +func (s byComplexity) Len() int { return len(s) } +func (s byComplexity) Swap(i, j int) { s[i], s[j] = s[j], s[i] } +func (s byComplexity) Less(i, j int) bool { + return s[i].Complexity >= s[j].Complexity +} + +func buildStats(f *ast.File, fset *token.FileSet, stats []stat) []stat { + for _, decl := range f.Decls { + if fn, ok := decl.(*ast.FuncDecl); ok { + stats = append(stats, stat{ + PkgName: f.Name.Name, + FuncName: funcName(fn), + Complexity: complexity(fn), + Pos: fset.Position(fn.Pos()), + }) + } + } + return stats +} + +// funcName returns the name representation of a function or method: +// "(Type).Name" for methods or simply "Name" for functions. +func funcName(fn *ast.FuncDecl) string { + if fn.Recv != nil { + typ := fn.Recv.List[0].Type + return fmt.Sprintf("(%s).%s", recvString(typ), fn.Name) + } + return fn.Name.Name +} + +// recvString returns a string representation of recv of the +// form "T", "*T", or "BADRECV" (if not a proper receiver type). +func recvString(recv ast.Expr) string { + switch t := recv.(type) { + case *ast.Ident: + return t.Name + case *ast.StarExpr: + return "*" + recvString(t.X) + } + return "BADRECV" +} + +// complexity calculates the cyclomatic complexity of a function. +func complexity(fn *ast.FuncDecl) int { + v := complexityVisitor{} + ast.Walk(&v, fn) + return v.Complexity +} + +type complexityVisitor struct { + // Complexity is the cyclomatic complexity + Complexity int +} + +// Visit implements the ast.Visitor interface. +func (v *complexityVisitor) Visit(n ast.Node) ast.Visitor { + switch n := n.(type) { + case *ast.FuncDecl, *ast.IfStmt, *ast.ForStmt, *ast.RangeStmt, *ast.CaseClause, *ast.CommClause: + v.Complexity++ + case *ast.BinaryExpr: + if n.Op == token.LAND || n.Op == token.LOR { + v.Complexity++ + } + } + return v +}