Skip to content

Commit

Permalink
Add parsing of relative paths for go callgraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Jul 30, 2024
1 parent fbabdc3 commit 81f9640
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
13 changes: 10 additions & 3 deletions internal/callgraph/language/golang/callgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ func (cg *CallgraphBuilder) createPackageConfig() (*packages.Config, []string) {
Tests: cg.includeTests,
Dir: cg.workingDirectory,
}

args := []string{cg.mainFile}

return cfg, args
Expand Down Expand Up @@ -171,6 +170,14 @@ func IsApplicationNode(filename string, pwd string) bool {
return false
}

func RelativePath(filename string, pwd string) string {
if len(filename) > len(pwd) && filename[:len(pwd)] == pwd {
return filename[len(pwd)+1:] // +1 for "/"
}

return filename
}

func (cg *CallgraphBuilder) outputCallGraph(icg *callgraph.Graph, prog *ssa.Program) error {
data := Edge{fset: prog.Fset}
pwd, err := os.Getwd()
Expand All @@ -190,14 +197,14 @@ func (cg *CallgraphBuilder) outputCallGraph(icg *callgraph.Graph, prog *ssa.Prog
var ok bool
if callerNode, ok = cg.cgModel.Nodes[callerSymbol]; !ok {
callerFilename := data.CallerFilename()
callerNode = cg.cgModel.AddNode(callerFilename, data.Caller.Name(), callerSymbol, IsApplicationNode(callerFilename, pwd), false, -1, -1)
callerNode = cg.cgModel.AddNode(RelativePath(callerFilename, pwd), data.Caller.Name(), callerSymbol, IsApplicationNode(callerFilename, pwd), false, -1, -1)
}

var calleeNode *model.Node
calleeSymbol := cleanSymbol(edge.Callee.Func.String())
if calleeNode, ok = cg.cgModel.Nodes[calleeSymbol]; !ok {
calleeFilename := data.CalleeFilename()
calleeNode = cg.cgModel.AddNode(calleeFilename, data.Callee.Name(), calleeSymbol, IsApplicationNode(calleeFilename, pwd), false, -1, -1)
calleeNode = cg.cgModel.AddNode(RelativePath(calleeFilename, pwd), data.Callee.Name(), calleeSymbol, IsApplicationNode(calleeFilename, pwd), false, -1, -1)
}

cg.cgModel.AddEdge(callerNode, calleeNode, data.CallLine())
Expand Down
29 changes: 29 additions & 0 deletions internal/callgraph/language/golang/callgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,32 @@ func TestIsApplicationNode(t *testing.T) {
}

}

func TestRelativeFilename(t *testing.T) {
tests := []struct {
name string
pwd string
in string
want string
}{
{
name: "Test with standard library",
pwd: "testdata/fixture",
in: "testdata/fixture/main.go.Println",
want: "main.go.Println",
},
{
name: "Test with non-standard library",
pwd: "testdata/fixture",
in: "github.com/spf13/afero/mem.File.Open",
want: "github.com/spf13/afero/mem.File.Open",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, RelativePath(tt.in, tt.pwd))
})
}

}
3 changes: 0 additions & 3 deletions internal/callgraph/language/golang/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (s Strategy) Invoke() ([]job.IJob, error) {
}

for _, path := range s.paths {

files, err := s.finder.FindFiles([]string{path}, s.exclusions, s.inclusions)
if err != nil {
strategyWarning("Error while finding files: " + err.Error())
Expand All @@ -52,10 +51,8 @@ func (s Strategy) Invoke() ([]job.IJob, error) {
}

for _, rootFilePath := range roots {

rootFileDir := filepath.Dir(rootFilePath)
rootFile := filepath.Base(rootFilePath)

jobs = append(jobs, NewJob(
rootFileDir,
rootFile,
Expand Down

0 comments on commit 81f9640

Please sign in to comment.