Skip to content

Commit

Permalink
feat: function level enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Apr 22, 2023
1 parent 6bc7a24 commit 1ef31ea
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 24 deletions.
22 changes: 13 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ jobs:
run: |
go test -v ./...
- name: Build
run: |
make build_linux_amd64
make build_windows_amd64
make build_macos_amd64
make build_macos_arm64
uses: crazy-max/ghaction-xgo@v2
with:
xgo_version: latest
go_version: 1.19
pkg: cmd/srctx
dest: build
prefix: srctx
targets: windows/amd64,linux/amd64,linux/arm64,darwin/amd64,darwin/arm64
v: true
x: false
ldflags: -s -w
buildmode: default
- name: Release
uses: softprops/action-gh-release@v1
with:
files: |
srctx_linux_amd64
srctx_windows_amd64.exe
srctx_macos_amd64
srctx_macos_arm64
build/srctx-*
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# syntax=docker/dockerfile:1

## Build
FROM golang:1.18-alpine AS build
FROM golang:1.19-alpine AS build

WORKDIR /app

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ default:

# linux
build_linux_amd64:
GOOS=linux GOARCH=amd64 ${GOCMD} build -o srctx_linux_amd64 ./cmd/srctx
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 ${GOCMD} build -o srctx_linux_amd64 ./cmd/srctx

# windows
build_windows_amd64:
GOOS=windows GOARCH=amd64 ${GOCMD} build -o srctx_windows_amd64.exe ./cmd/srctx
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 ${GOCMD} build -o srctx_windows_amd64.exe ./cmd/srctx

# mac
build_macos_amd64:
GOOS=darwin GOARCH=amd64 ${GOCMD} build -o srctx_macos_amd64 ./cmd/srctx
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 ${GOCMD} build -o srctx_macos_amd64 ./cmd/srctx
build_macos_arm64:
GOOS=darwin GOARCH=arm64 ${GOCMD} build -o srctx_macos_arm64 ./cmd/srctx
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 ${GOCMD} build -o srctx_macos_arm64 ./cmd/srctx

test:
$(GOTEST) ./...
55 changes: 53 additions & 2 deletions cmd/srctx/cmd_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"github.com/dominikbraun/graph"
"github.com/dominikbraun/graph/draw"
"github.com/gocarina/gocsv"
"github.com/opensibyl/sibyl2/pkg/extractor/object"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/williamfzc/srctx/collector"
"github.com/williamfzc/srctx/diff"
"github.com/williamfzc/srctx/parser"
)
Expand All @@ -22,6 +24,7 @@ func AddDiffCmd(app *cli.App) {
var outputJson string
var outputCsv string
var outputDot string
var funcEnhance bool

diffCmd := &cli.Command{
Name: "diff",
Expand Down Expand Up @@ -69,6 +72,13 @@ func AddDiffCmd(app *cli.App) {
Usage: "reference dot file output",
Destination: &outputDot,
},
// experimental
&cli.BoolFlag{
Name: "funcEnhance",
Value: true,
Usage: "function level diff json",
Destination: &funcEnhance,
},
},
Action: func(cCtx *cli.Context) error {
// prepare
Expand All @@ -92,6 +102,7 @@ func AddDiffCmd(app *cli.App) {
}

for path, lines := range lineMap {
curFileLines := make([]*LineStat, 0, len(lines))
for _, eachLine := range lines {
lineStat := NewLineStat(path, eachLine)
vertices, _ := sourceContext.RefsByLine(path, eachLine)
Expand Down Expand Up @@ -119,8 +130,48 @@ func AddDiffCmd(app *cli.App) {
lineStat.RefScope.CrossDirRefCount++
}
}
lineStats = append(lineStats, lineStat)
curFileLines = append(curFileLines, lineStat)
}

if funcEnhance {
functionFile, err := collector.GetFunctionMetadataFromFile(filepath.Join(src, path))
if _, ok := err.(*collector.NotSupportLangError); ok {
log.Warnf("file %v not supported", err)
goto eachFileEnd
}

if err != nil {
return err
}
// what happened in these lines
influences := make([]*object.Function, 0)
for _, eachUnit := range functionFile.Units {
if eachUnit.GetSpan().ContainAnyLine(lines...) {
influences = append(influences, eachUnit)
}
}
for _, eachFunc := range influences {
// def line (not precise
funcDefLine := eachFunc.GetSpan().Start.Row + 1
vertices, _ := sourceContext.RefsByLine(path, int(funcDefLine))
log.Debugf("[func scope] path %s line %d affected %d vertexes", path, funcDefLine, len(vertices))
// update status
for _, eachLine := range curFileLines {
eachLine.FuncRefScope.TotalFuncRefCount += len(vertices)
for _, eachVertex := range vertices {
fileName := sourceContext.FileName(eachVertex.FileId)
if fileName != path {
eachLine.FuncRefScope.CrossFuncFileRefCount++
}
if filepath.Dir(fileName) != filepath.Dir(path) {
eachLine.FuncRefScope.CrossFuncDirRefCount++
}
}
}
}
}
eachFileEnd:
lineStats = append(lineStats, curFileLines...)
}
log.Infof("diff finished.")

Expand All @@ -139,7 +190,7 @@ func AddDiffCmd(app *cli.App) {

unsafeLines := make([]*LineStat, 0)
for _, each := range lineStats {
if !each.RefScope.IsSafe() {
if !each.IsSafe() {
unsafeLines = append(unsafeLines, each)
}
}
Expand Down
22 changes: 21 additions & 1 deletion cmd/srctx/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ type ReferenceScope struct {
CrossDirRefCount int `json:"crossDirRefCount"`
}

type FuncReferenceScope struct {
TotalFuncRefCount int `json:"totalFuncRefCount"`
CrossFuncFileRefCount int `json:"crossFuncFileRefCount"`
CrossFuncDirRefCount int `json:"crossFuncDirRefCount"`
}

func (r *ReferenceScope) IsSafe() bool {
return r.TotalRefCount == 0 && r.CrossFileRefCount == 0 && r.CrossDirRefCount == 0
}

func (fr *FuncReferenceScope) IsSafe() bool {
return fr.TotalFuncRefCount == 0 && fr.CrossFuncFileRefCount == 0 && fr.CrossFuncDirRefCount == 0
}

type LineStat struct {
*FileScope
RefScope *ReferenceScope `json:"ref"`
RefScope *ReferenceScope `json:"ref"`
FuncRefScope *FuncReferenceScope `json:"funcRef"`
}

func (ls *LineStat) IsSafe() bool {
return ls.RefScope.IsSafe() && ls.FuncRefScope.IsSafe()
}

func NewLineStat(fileName string, lineNumber int) *LineStat {
Expand All @@ -30,5 +45,10 @@ func NewLineStat(fileName string, lineNumber int) *LineStat {
CrossFileRefCount: 0,
CrossDirRefCount: 0,
},
FuncRefScope: &FuncReferenceScope{
TotalFuncRefCount: 0,
CrossFuncFileRefCount: 0,
CrossFuncDirRefCount: 0,
},
}
}
35 changes: 28 additions & 7 deletions collector/sibyl.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
package collector

import (
"fmt"
"strings"

"github.com/opensibyl/sibyl2"
"github.com/opensibyl/sibyl2/pkg/extractor"
)

type FunctionMetaMap = map[string]*extractor.FunctionFileResult

func GetFunctionsMetadata(rootDir string) (FunctionMetaMap, error) {
functionFileResults, err := sibyl2.ExtractFunction(rootDir, sibyl2.DefaultConfig())
func IsSupported(path string) bool {
if strings.HasSuffix(path, ".java") {
return true
}
if strings.HasSuffix(path, ".go") {
return true
}
return false
}

type NotSupportLangError struct {
msg string
}

func (e *NotSupportLangError) Error() string {
return fmt.Sprintf("not supported lang: %s", e.msg)
}

func GetFunctionMetadataFromFile(targetFile string) (*extractor.FunctionFileResult, error) {
if !IsSupported(targetFile) {
return nil, &NotSupportLangError{targetFile}
}
res, err := sibyl2.ExtractFunction(targetFile, sibyl2.DefaultConfig())
if err != nil {
return nil, err
}
fmm := make(FunctionMetaMap, len(functionFileResults))
for _, each := range functionFileResults {
fmm[each.Path] = each
}
return fmm, nil

return res[0], nil
}

0 comments on commit 1ef31ea

Please sign in to comment.