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

feat: add graphviz output #77

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cmd/tlin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Config struct {
AutoFix bool
DryRun bool
JsonOutput string
Output string
ConfidenceThreshold float64
}

Expand All @@ -64,7 +65,7 @@ func main() {

if config.CFGAnalysis {
runWithTimeout(ctx, func() {
runCFGAnalysis(ctx, logger, config.Paths, config.FuncName)
runCFGAnalysis(ctx, logger, config.Paths, config.FuncName, config.Output)
})
} else if config.CyclomaticComplexity {
runWithTimeout(ctx, func() {
Expand Down Expand Up @@ -92,6 +93,7 @@ func parseFlags(args []string) Config {
flagSet.BoolVar(&config.CFGAnalysis, "cfg", false, "Run control flow graph analysis")
flagSet.StringVar(&config.FuncName, "func", "", "Function name for CFG analysis")
flagSet.BoolVar(&config.AutoFix, "fix", false, "Automatically fix issues")
flagSet.StringVar(&config.Output, "o", "", "Output path of GraphViz file in a SVG format")
0xtekgrinder marked this conversation as resolved.
Show resolved Hide resolved
flagSet.BoolVar(&config.DryRun, "dry-run", false, "Run in dry-run mode (show fixes without applying them)")
flagSet.StringVar(&config.JsonOutput, "json-output", "", "Output issues in JSON format to the specified file")
flagSet.Float64Var(&config.ConfidenceThreshold, "confidence", defaultConfidenceThreshold, "Confidence threshold for auto-fixing (0.0 to 1.0)")
Expand Down Expand Up @@ -157,7 +159,7 @@ func runCyclomaticComplexityAnalysis(ctx context.Context, logger *zap.Logger, pa
}
}

func runCFGAnalysis(_ context.Context, logger *zap.Logger, paths []string, funcName string) {
func runCFGAnalysis(_ context.Context, logger *zap.Logger, paths []string, funcName string, output string) {
functionFound := false
for _, path := range paths {
fset := token.NewFileSet()
Expand All @@ -173,7 +175,14 @@ func runCFGAnalysis(_ context.Context, logger *zap.Logger, paths []string, funcN
cfgGraph := cfg.FromFunc(fn)
var buf strings.Builder
cfgGraph.PrintDot(&buf, fset, func(n ast.Stmt) string { return "" })
fmt.Printf("CFG for function %s in file %s:\n%s\n", funcName, path, buf.String())
if output != "" {
err := cfg.RenderToGraphVizFile([]byte(buf.String()), output)
if err != nil {
logger.Error("Failed to render CFG to GraphViz file", zap.Error(err))
}
} else {
fmt.Printf("CFG for function %s in file %s:\n%s\n", funcName, path, buf.String())
}
functionFound = true
return
}
Expand Down
16 changes: 12 additions & 4 deletions cmd/tlin/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"go/token"
"io"
"os"
Expand Down Expand Up @@ -88,6 +87,15 @@ func TestParseFlags(t *testing.T) {
ConfidenceThreshold: defaultConfidenceThreshold,
},
},
{
name: "Output",
args: []string{"-o", "output.svg", "file.go"},
expected: Config{
Paths: []string{"file.go"},
Output: "output.svg",
ConfidenceThreshold: defaultConfidenceThreshold,
},
},
}

for _, tt := range tests {
Expand All @@ -101,6 +109,7 @@ func TestParseFlags(t *testing.T) {
assert.Equal(t, tt.expected.ConfidenceThreshold, config.ConfidenceThreshold)
assert.Equal(t, tt.expected.Paths, config.Paths)
assert.Equal(t, tt.expected.JsonOutput, config.JsonOutput)
assert.Equal(t, tt.expected.Output, config.Output)
})
}
}
Expand Down Expand Up @@ -159,7 +168,7 @@ func ignoredFunc() { // 19
ctx := context.Background()

output := captureOutput(t, func() {
runCFGAnalysis(ctx, logger, []string{tempFile}, "targetFunc")
runCFGAnalysis(ctx, logger, []string{tempFile}, "targetFunc", "")
})

assert.Contains(t, output, "CFG for function targetFunc in file")
Expand All @@ -172,7 +181,7 @@ func ignoredFunc() { // 19
t.Logf("output: %s", output)

output = captureOutput(t, func() {
runCFGAnalysis(ctx, logger, []string{tempFile}, "nonExistentFunc")
runCFGAnalysis(ctx, logger, []string{tempFile}, "nonExistentFunc", "")
})

assert.Contains(t, output, "Function not found: nonExistentFunc")
Expand Down Expand Up @@ -286,7 +295,6 @@ func TestRunJsonOutput(t *testing.T) {

tempDir, err := os.MkdirTemp("", "json-test")
assert.NoError(t, err)
fmt.Println(tempDir)

testFile := filepath.Join(tempDir, "test.go")
err = os.WriteFile(testFile, []byte(sliceRangeIssueExample), 0644)
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ require (
)

require (
github.com/fogleman/gg v1.3.0 // indirect
github.com/goccy/go-graphviz v0.1.3 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fzipp/gocyclo v0.6.0 h1:lsblElZG7d3ALtGMx9fmxeTKZaLLpU8mET09yN4BBLo=
github.com/fzipp/gocyclo v0.6.0/go.mod h1:rXPyn8fnlpa0R2csP/31uerbiVBugk5whMdlyaLkLoA=
github.com/goccy/go-graphviz v0.1.3 h1:Pkt8y4FBnBNI9tfSobpoN5qy1qMNqRXPQYvLhaSUasY=
github.com/goccy/go-graphviz v0.1.3/go.mod h1:pMYpbAqJT10V8dzV1JN/g/wUlG/0imKPzn3ZsrchGCI=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
Expand All @@ -21,6 +29,8 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
Expand Down
16 changes: 16 additions & 0 deletions internal/analysis/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strings"

"github.com/goccy/go-graphviz"
"golang.org/x/tools/go/ast/astutil"
)

Expand Down Expand Up @@ -49,6 +50,21 @@ func FromFunc(f *ast.FuncDecl) *CFG {
return NewBuilder().BuildFromFunc(f)
}

// RenderToGraphVizFile renders the given DOT content to a GraphViz file.
func RenderToGraphVizFile(dotContent []byte, filename string) error {
graph, err := graphviz.ParseBytes(dotContent)
if err != nil {
return err
}
g := graphviz.New()
defer g.Close()
err = g.RenderFilename(graph, graphviz.SVG, filename)
if err != nil {
return err
}
return nil
}

func AnalyzeFunction(file *ast.File, fname string) *CFG {
for _, decl := range file.Decls {
if funcDecl, ok := decl.(*ast.FuncDecl); ok {
Expand Down
38 changes: 38 additions & 0 deletions internal/analysis/cfg/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
"go/parser"
"go/token"
"go/types"
"os"
"path/filepath"
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFromStmts(t *testing.T) {
Expand Down Expand Up @@ -991,6 +995,40 @@ splines="ortho";
}
}

func TestRenderToGraphVizFile(t *testing.T) {
t.Parallel()
tmpDir, err := os.MkdirTemp("", "cfg_test")
assert.NoError(t, err)
defer os.RemoveAll(tmpDir)

c := getWrapper(t, `
package main

func main() {
i := 5 //1
i++ //2
}`)

var buf bytes.Buffer
c.cfg.PrintDot(&buf, c.fset, func(s ast.Stmt) string {
if _, ok := s.(*ast.AssignStmt); ok {
return "!"
} else {
return ""
}
})
dot := buf.String()

svgFile := filepath.Join(tmpDir, "test.svg")
err = RenderToGraphVizFile([]byte(dot), svgFile)
assert.NoError(t, err)

content, err := os.ReadFile(svgFile)
assert.NoError(t, err)

assert.NotEmpty(t, content)
}

func normalizeDotOutput(dot string) string {
lines := strings.Split(dot, "\n")
var normalized []string
Expand Down
Loading