28
28
29
29
from __future__ import print_function
30
30
31
+ import argparse
31
32
import re
32
33
import subprocess
33
34
import sys
34
35
import tempfile
35
36
36
37
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
-
48
38
class Block (object ):
49
39
50
40
current_index = 0
@@ -84,13 +74,24 @@ class Block(object):
84
74
return self .succs
85
75
86
76
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
+
87
89
def main ():
90
+ args = parse_args ()
91
+
88
92
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
94
95
95
96
block_list = []
96
97
block_map = {}
@@ -100,8 +101,7 @@ def main():
100
101
llvm_block_pattern2 = re .compile (r'^(\d+):? *; *preds =(.*)?$' )
101
102
102
103
# Scan the input file.
103
-
104
- for line in sys .stdin :
104
+ for line in args .input_file .readlines ():
105
105
sil_block_match = sil_block_pattern .match (line )
106
106
llvm_block_match1 = llvm_block_pattern1 .match (line )
107
107
llvm_block_match2 = llvm_block_pattern2 .match (line )
@@ -149,7 +149,6 @@ def main():
149
149
pred_block .get_succs ().append (block .name )
150
150
151
151
# Write the output dot file.
152
-
153
152
file_name = tempfile .gettempdir () + "/viewcfg" + suffix + ".dot"
154
153
with open (file_name , 'w' ) as out_file :
155
154
out_file .write ('digraph "CFG" {\n ' )
@@ -172,8 +171,15 @@ def main():
172
171
173
172
out_file .write ("}\n " )
174
173
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?!' )
177
183
178
184
179
185
if __name__ == '__main__' :
0 commit comments