Skip to content

Commit de9f419

Browse files
committed
PIRINSPECTOR: allow choice of output format
1 parent befe7f6 commit de9f419

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

chb/ast/ASTViewer.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,13 @@ def visit_binary_expression(self, expr: AST.ASTBinaryOp) -> None:
382382
name,
383383
labeltxt="binop:" + expr.op + ":" + str(expr.exprid) + connections,
384384
color=nodecolors["expr"])
385-
self.add_edge(name, self.expr_name(expr.exp1), labeltxt="exp1")
386-
self.add_edge(name, self.expr_name(expr.exp2), labeltxt="exp2")
387-
expr.exp1.accept(self)
388-
expr.exp2.accept(self)
385+
if self.cutoff_at_stmt() or self.cutoff_at_instr():
386+
return
387+
else:
388+
self.add_edge(name, self.expr_name(expr.exp1), labeltxt="exp1")
389+
self.add_edge(name, self.expr_name(expr.exp2), labeltxt="exp2")
390+
expr.exp1.accept(self)
391+
expr.exp2.accept(self)
389392

390393
def visit_address_of_expression(self, expr: AST.ASTAddressOf) -> None:
391394
name = self.expr_name(expr)

chb/ast/astdotutil.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,17 @@ def __str__(self) -> str:
214214

215215
def print_dot(
216216
filename: str,
217-
g: "ASTDotGraph") -> str:
217+
g: "ASTDotGraph",
218+
fileformat: str = "pdf") -> str:
218219
dotfilename = filename + ".dot"
219-
pdffilename = filename + ".pdf"
220+
pdffilename = filename + "." + fileformat
220221

221222
# write graph to dot format
222223
with open(dotfilename, "w") as fp:
223224
fp.write(str(g))
224225

225226
# convert dot file to pdf
226-
cmd = ["dot", "-Tpdf", "-o", pdffilename, dotfilename]
227+
cmd = ["dot", "-T%s" % fileformat, "-o", pdffilename, dotfilename]
227228
try:
228229
subprocess.call(cmd, stderr=subprocess.STDOUT)
229230
except subprocess.CalledProcessError as e:

chb/ast/astutil.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,15 @@ def viewastcmd(args: argparse.Namespace) -> NoReturn:
181181
function: Optional[str] = args.function
182182
level: str = args.level
183183
outputfilename: str = args.outputfile
184+
fileformat: str = args.fileformat
184185
cutoff: Optional[str] = args.cutoff
185186

186187
with open(pirfile, "r") as fp:
187188
pirjson = json.load(fp)
188189

189190
faddr = get_function_addr(pirjson, function)
190191
g = view_ast_function(faddr, level, pirjson, cutoff)
191-
DU.print_dot(outputfilename, g)
192+
DU.print_dot(outputfilename, g, fileformat=fileformat)
192193
exit(0)
193194

194195

@@ -199,6 +200,7 @@ def viewstmtcmd(args: argparse.Namespace) -> NoReturn:
199200
function: Optional[str] = args.function
200201
stmtid: int = args.stmtid
201202
provenance: bool = args.provenance
203+
fileformat: str = args.fileformat
202204
outputfilename: str = args.output
203205

204206
with open(pirfile, "r") as fp:
@@ -218,7 +220,7 @@ def viewstmtcmd(args: argparse.Namespace) -> NoReturn:
218220
else:
219221
g = viewer.to_graph(stmt)
220222

221-
DU.print_dot(outputfilename, g)
223+
DU.print_dot(outputfilename, g, fileformat=fileformat)
222224
exit(0)
223225

224226

@@ -229,6 +231,7 @@ def viewinstrcmd(args: argparse.Namespace) -> NoReturn:
229231
function: Optional[str] = args.function
230232
instrid: int = args.instrid
231233
provenance: bool = args.provenance
234+
fileformat: str = args.fileformat
232235
outputfilename: str = args.output
233236

234237
with open(pirfile, "r") as fp:
@@ -251,7 +254,7 @@ def viewinstrcmd(args: argparse.Namespace) -> NoReturn:
251254
else:
252255
g = viewer.instr_to_graph(instr)
253256

254-
DU.print_dot(outputfilename, g)
257+
DU.print_dot(outputfilename, g, fileformat=fileformat)
255258

256259
exit(0)
257260

chb/ast/pirinspector

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ def parse() -> argparse.Namespace:
8585
help="stop expansion at stmt or instruction level",
8686
choices=["stmt", "instr"],
8787
)
88+
viewastcmd.add_argument(
89+
"--fileformat",
90+
help="desired format for output file (default is pdf)",
91+
choices=["pdf", "png"],
92+
default=["pdf"]
93+
)
8894
viewastcmd.add_argument(
8995
"-o",
9096
"--outputfile",
@@ -104,6 +110,12 @@ def parse() -> argparse.Namespace:
104110
help="show associated low-level instructions",
105111
action="store_true",
106112
)
113+
viewstmtcmd.add_argument(
114+
"--fileformat",
115+
help="desired format for output file (default is pdf)",
116+
choices=["pdf", "png"],
117+
default=["pdf"]
118+
)
107119
viewstmtcmd.add_argument(
108120
"-o", "--output",
109121
help="name of output graph file (without extension)",
@@ -123,6 +135,12 @@ def parse() -> argparse.Namespace:
123135
help="show associated low-level instruction",
124136
action="store_true",
125137
)
138+
viewinstrcmd.add_argument(
139+
"--fileformat",
140+
help="desired format for output file (default is pdf)",
141+
choices=["pdf", "png"],
142+
default=["pdf"]
143+
)
126144
viewinstrcmd.add_argument(
127145
"-o",
128146
"--output",

0 commit comments

Comments
 (0)