Skip to content

Commit

Permalink
Add an option to show LOC for each node
Browse files Browse the repository at this point in the history
Also, add another example and update the script to generate examples to
include it.
  • Loading branch information
lupino3 committed Dec 6, 2018
1 parent b7c17a5 commit 2fef3f4
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 15 deletions.
16 changes: 13 additions & 3 deletions callgraph/callgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _AnnotateNode(self, node):
line.terminating = True
node.is_exit_node = True

def PrintDot(self, out_file=sys.stdout, show_all_calls=True):
def PrintDot(self, out_file=sys.stdout, show_all_calls=True, show_node_stats=False):
kind_colors = {
"goto": "red3",
"nested": "blue3",
Expand All @@ -137,9 +137,16 @@ def PrintDot(self, out_file=sys.stdout, show_all_calls=True):
pretty_name = node.original_name

attributes = []
label_lines = ["<b>{}</b>".format(pretty_name)]

if node.line_number > 0:
attributes.append("label=<<b>{}</b><br />(line {})>".format(pretty_name, node.line_number))
label_lines.append("(line {})".format(node.line_number))

if show_node_stats:
label_lines.append("<sub>[{} LOC]</sub>".format(len(node.code)))

attributes.append("label=<{}>".format("<br/>".join(label_lines)))

if node.is_exit_node:
attributes.append("color=red")
attributes.append("penwidth=2")
Expand Down Expand Up @@ -237,7 +244,10 @@ def main():
parser.add_argument("--show-all-calls", type=bool,
help="Set to true to show all calls in the graph.", default=False,
dest="allcalls")
parser.add_argument("--show-node-stats", type=bool,
help="Set to true to show statistics about the nodes in the graph.",
default=False, dest="nodestats")
args = parser.parse_args()

call_graph = CallGraph.Build(sys.stdin)
call_graph.PrintDot(sys.stdout, args.allcalls)
call_graph.PrintDot(sys.stdout, args.allcalls, args.nodestats)
10 changes: 5 additions & 5 deletions examples/example1-noshowall.dot
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
digraph g {
"__begin__" [label=<<b>__begin__</b><br />(line 1)>]
"__begin__" [label=<<b>__begin__</b><br/>(line 1)>]
"__begin__" -> "eof" [label=goto,color=red3]
"__begin__" -> "foo" [label=call,color=green3]
"bar" [label=<<b>bar</b><br />(line 4)>]
"bar" [label=<<b>bar</b><br/>(line 4)>]
"bar" -> "baz" [label=call,color=green3]
"bar" -> "baz" [label=nested,color=blue3]
"baz" [label=<<b>baz</b><br />(line 8)>]
"baz" [label=<<b>baz</b><br/>(line 8)>]
"baz" -> "foo" [label=nested,color=blue3]
"eof" [color=red,penwidth=2]
"foo" [label=<<b>foo</b><br />(line 11)>,color=red,penwidth=2]
"eof" [label=<<b>eof</b>>,color=red,penwidth=2]
"foo" [label=<<b>foo</b><br/>(line 11)>,color=red,penwidth=2]
"foo" -> "bar" [label=goto,color=red3]
}
Binary file modified examples/example1-noshowall.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions examples/example1.dot
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
digraph g {
"__begin__" [label=<<b>__begin__</b><br />(line 1)>]
"__begin__" [label=<<b>__begin__</b><br/>(line 1)>]
"__begin__" -> "eof" [label=<<b>goto</b><br />(line 3)>,color=red3]
"__begin__" -> "foo" [label=<<b>call</b><br />(line 2)>,color=green3]
"bar" [label=<<b>bar</b><br />(line 4)>]
"bar" [label=<<b>bar</b><br/>(line 4)>]
"bar" -> "baz" [label=<<b>call</b><br />(line 6)>,color=green3]
"bar" -> "baz" [label=<<b>call</b><br />(line 7)>,color=green3]
"bar" -> "baz" [label=nested,color=blue3]
"baz" [label=<<b>baz</b><br />(line 8)>]
"baz" [label=<<b>baz</b><br/>(line 8)>]
"baz" -> "foo" [label=nested,color=blue3]
"eof" [color=red,penwidth=2]
"foo" [label=<<b>foo</b><br />(line 11)>,color=red,penwidth=2]
"eof" [label=<<b>eof</b>>,color=red,penwidth=2]
"foo" [label=<<b>foo</b><br/>(line 11)>,color=red,penwidth=2]
"foo" -> "bar" [label=<<b>goto</b><br />(line 13)>,color=red3]
}
Binary file modified examples/example1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions examples/loc.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
:one
:two
rem second line
:three
rem second line
rem third line
:four
rem second line
rem third line
rem fourth line
11 changes: 11 additions & 0 deletions examples/loc.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
digraph g {
"__begin__" [label=<<b>__begin__</b><br/>(line 1)<br/><sub>[0 LOC]</sub>>]
"eof" [label=<<b>eof</b><br/><sub>[0 LOC]</sub>>,color=red,penwidth=2]
"four" [label=<<b>four</b><br/>(line 7)<br/><sub>[4 LOC]</sub>>,color=red,penwidth=2]
"one" [label=<<b>one</b><br/>(line 1)<br/><sub>[1 LOC]</sub>>]
"one" -> "two" [label=nested,color=blue3]
"three" [label=<<b>three</b><br/>(line 4)<br/><sub>[3 LOC]</sub>>]
"three" -> "four" [label=nested,color=blue3]
"two" [label=<<b>two</b><br/>(line 2)<br/><sub>[2 LOC]</sub>>]
"two" -> "three" [label=nested,color=blue3]
}
Binary file added examples/loc.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion scripts/generate-examples.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash


python3 cmd-call-graph.py --show-all-calls=True < examples/example1.cmd > examples/example1.dot
python3 cmd-call-graph.py < examples/example1.cmd > examples/example1-noshowall.dot
python3 cmd-call-graph.py --show-node-stats=True < examples/loc.cmd > examples/loc.dot

dot -Tpng examples/example1.dot > examples/example1.png
dot -Tpng examples/example1-noshowall.dot > examples/example1-noshowall.png
dot -Tpng examples/loc.dot > examples/loc.png
10 changes: 9 additions & 1 deletion tests/test_graphbuilding.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_code_in_nodes(self):
# If allgraph is set to False, it should count as a single call,
# and no line annotations should appear in the graph,
# while if it's set to True it should count as a double call.
class AllGraphTest(CallGraphTest):
class PrintOptionsGraphTest(CallGraphTest):
def setUp(self):
CallGraphTest.setUp(self)
code ="""
Expand Down Expand Up @@ -208,6 +208,14 @@ def test_duplicate_no_allgraph(self):
self.assertEqual(0, dot.count("line 2"))
self.assertEqual(0, dot.count("line 3"))

def test_loc(self):
f = io.StringIO()
self.call_graph.PrintDot(f, show_node_stats=True)
dot = f.getvalue()

# Check the number of lines of code.
self.assertEqual(1, dot.count('3 LOC'))
self.assertEqual(1, dot.count('4 LOC'))

def test_duplicate_allgraph(self):
f = io.StringIO()
Expand Down

0 comments on commit 2fef3f4

Please sign in to comment.