Skip to content

Commit c8385b9

Browse files
committed
[viewcfg] Add an argparse based argument parser and enable the user to select to use dot instead of graphviz.
1 parent 041dfb9 commit c8385b9

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

utils/viewcfg

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,13 @@
2828

2929
from __future__ import print_function
3030

31+
import argparse
3132
import re
3233
import subprocess
3334
import sys
3435
import tempfile
3536

3637

37-
def help():
38-
print("""\
39-
Usage:
40-
41-
viewcfg [output-suffix] < file
42-
43-
By default all CFGs are opened in the same window.
44-
Use the a unique output-suffix to open a CFG in a new window.
45-
""")
46-
47-
4838
class Block(object):
4939

5040
current_index = 0
@@ -84,13 +74,24 @@ class Block(object):
8474
return self.succs
8575

8676

77+
def parse_args():
78+
parser = argparse.ArgumentParser(description="""
79+
Simple regex based transform of SIL and LLVM-IR into graphviz form.
80+
""")
81+
parser.add_argument("output_suffix", nargs='?')
82+
parser.add_argument("--input_file", type=argparse.FileType('r'),
83+
default=sys.stdin)
84+
parser.add_argument("--renderer", choices=["Graphviz", "dot"],
85+
default="Graphviz", help="Default is Graphviz")
86+
return parser.parse_args()
87+
88+
8789
def main():
90+
args = parse_args()
91+
8892
suffix = ""
89-
if len(sys.argv) >= 2:
90-
if sys.argv[1].startswith('-'):
91-
help()
92-
return
93-
suffix = sys.argv[1]
93+
if args.output_suffix:
94+
suffix = args.output_suffix
9495

9596
block_list = []
9697
block_map = {}
@@ -100,8 +101,7 @@ def main():
100101
llvm_block_pattern2 = re.compile(r'^(\d+):? *; *preds =(.*)?$')
101102

102103
# Scan the input file.
103-
104-
for line in sys.stdin:
104+
for line in args.input_file.readlines():
105105
sil_block_match = sil_block_pattern.match(line)
106106
llvm_block_match1 = llvm_block_pattern1.match(line)
107107
llvm_block_match2 = llvm_block_pattern2.match(line)
@@ -149,7 +149,6 @@ def main():
149149
pred_block.get_succs().append(block.name)
150150

151151
# Write the output dot file.
152-
153152
file_name = tempfile.gettempdir() + "/viewcfg" + suffix + ".dot"
154153
with open(file_name, 'w') as out_file:
155154
out_file.write('digraph "CFG" {\n')
@@ -172,8 +171,15 @@ def main():
172171

173172
out_file.write("}\n")
174173

175-
# Open the dot file.
176-
subprocess.call(["open", "-a", "Graphviz", file_name])
174+
# Open the dot file with the appropriate renderer.
175+
if args.renderer == 'Graphviz':
176+
subprocess.call(["open", "-a", "Graphviz", file_name])
177+
elif args.renderer == 'dot':
178+
pdf = '%s.pdf' % file_name
179+
subprocess.call(["dot", "-Tpdf", file_name, '-o%s' % pdf])
180+
subprocess.call(["open", "-a", "Preview", pdf])
181+
else:
182+
raise RuntimeError('Unknown renderer?!')
177183

178184

179185
if __name__ == '__main__':

0 commit comments

Comments
 (0)