Skip to content

[viewcfg] Add an argparse based argument parser and enable the user to select to use dot instead of graphviz. #31357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions utils/viewcfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,13 @@

from __future__ import print_function

import argparse
import re
import subprocess
import sys
import tempfile


def help():
print("""\
Usage:

viewcfg [output-suffix] < file

By default all CFGs are opened in the same window.
Use the a unique output-suffix to open a CFG in a new window.
""")


class Block(object):

current_index = 0
Expand Down Expand Up @@ -84,13 +74,24 @@ class Block(object):
return self.succs


def parse_args():
parser = argparse.ArgumentParser(description="""
Simple regex based transform of SIL and LLVM-IR into graphviz form.
""")
parser.add_argument("output_suffix", nargs='?')
parser.add_argument("--input_file", type=argparse.FileType('r'),
default=sys.stdin)
parser.add_argument("--renderer", choices=["Graphviz", "dot"],
default="Graphviz", help="Default is Graphviz")
return parser.parse_args()


def main():
args = parse_args()

suffix = ""
if len(sys.argv) >= 2:
if sys.argv[1].startswith('-'):
help()
return
suffix = sys.argv[1]
if args.output_suffix:
suffix = args.output_suffix

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

# Scan the input file.

for line in sys.stdin:
for line in args.input_file.readlines():
sil_block_match = sil_block_pattern.match(line)
llvm_block_match1 = llvm_block_pattern1.match(line)
llvm_block_match2 = llvm_block_pattern2.match(line)
Expand Down Expand Up @@ -149,7 +149,6 @@ def main():
pred_block.get_succs().append(block.name)

# Write the output dot file.

file_name = tempfile.gettempdir() + "/viewcfg" + suffix + ".dot"
with open(file_name, 'w') as out_file:
out_file.write('digraph "CFG" {\n')
Expand All @@ -172,8 +171,15 @@ def main():

out_file.write("}\n")

# Open the dot file.
subprocess.call(["open", "-a", "Graphviz", file_name])
# Open the dot file with the appropriate renderer.
if args.renderer == 'Graphviz':
subprocess.call(["open", "-a", "Graphviz", file_name])
elif args.renderer == 'dot':
pdf = '%s.pdf' % file_name
subprocess.call(["dot", "-Tpdf", file_name, '-o%s' % pdf])
subprocess.call(["open", "-a", "Preview", pdf])
else:
raise RuntimeError('Unknown renderer?!')


if __name__ == '__main__':
Expand Down