-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
callgraphutil.WriteCosmograph
(#28)
* Add `callgraphutil.WriteCosmograph` * Update dot_test.go
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package callgraphutil | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"io" | ||
|
||
"golang.org/x/tools/go/callgraph" | ||
) | ||
|
||
// WriteComsmograph writes the given callgraph.Graph to the given io.Writer in CSV | ||
// format, which can be used to generate a visual representation of the call | ||
// graph using Comsmograph. | ||
// | ||
// https://cosmograph.app/run/ | ||
func WriteCosmograph(graph, metadata io.Writer, g *callgraph.Graph) error { | ||
graphWriter := csv.NewWriter(graph) | ||
graphWriter.Comma = ',' | ||
defer graphWriter.Flush() | ||
|
||
metadataWriter := csv.NewWriter(metadata) | ||
metadataWriter.Comma = ',' | ||
defer metadataWriter.Flush() | ||
|
||
// Write header. | ||
if err := graphWriter.Write([]string{"source", "target"}); err != nil { | ||
return fmt.Errorf("failed to write header: %w", err) | ||
} | ||
|
||
// Write metadata header. | ||
if err := metadataWriter.Write([]string{"id", "pkg", "func"}); err != nil { | ||
return fmt.Errorf("failed to write metadata header: %w", err) | ||
} | ||
|
||
// Write edges. | ||
for _, n := range g.Nodes { | ||
// TODO: fix this so there's not so many "shared" functions? | ||
// | ||
// It is a bit of a hack, but it works for now. | ||
var pkgPath string | ||
if n.Func.Pkg != nil { | ||
pkgPath = n.Func.Pkg.Pkg.Path() | ||
} else { | ||
pkgPath = "shared" | ||
} | ||
|
||
// Write metadata. | ||
if err := metadataWriter.Write([]string{ | ||
fmt.Sprintf("%d", n.ID), | ||
pkgPath, | ||
n.Func.String(), | ||
}); err != nil { | ||
return fmt.Errorf("failed to write metadata: %w", err) | ||
} | ||
|
||
for _, e := range n.Out { | ||
// Write edge. | ||
if err := graphWriter.Write([]string{ | ||
fmt.Sprintf("%d", n.ID), | ||
fmt.Sprintf("%d", e.Callee.ID), | ||
}); err != nil { | ||
return fmt.Errorf("failed to write edge: %w", err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package callgraphutil_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/picatz/taint/callgraphutil" | ||
) | ||
|
||
func TestWriteCosmograph(t *testing.T) { | ||
var ( | ||
ownerName = "picatz" | ||
repoName = "taint" | ||
) | ||
|
||
repo, _, err := cloneGitHubRepository(context.Background(), ownerName, repoName) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
pkgs, err := loadPackages(context.Background(), repo, "./...") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
mainFn, srcFns, err := loadSSA(context.Background(), pkgs) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cg, err := loadCallGraph(context.Background(), mainFn, srcFns) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
graphOutput, err := os.Create(fmt.Sprintf("%s.csv", repoName)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer graphOutput.Close() | ||
|
||
metadataOutput, err := os.Create(fmt.Sprintf("%s-metadata.csv", repoName)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer metadataOutput.Close() | ||
|
||
err = callgraphutil.WriteCosmograph(graphOutput, metadataOutput, cg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters