Skip to content
Snippets Groups Projects
Commit 56f2a6a1 authored by Dominic Kempf's avatar Dominic Kempf
Browse files

Add Try/Catch Blocks within driver

As a step towards making the driver the main function.
parent 9d05efef
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ from __future__ import absolute_import ...@@ -3,6 +3,7 @@ from __future__ import absolute_import
from cgen import * from cgen import *
from dune.perftool.cgen.clazz import Class from dune.perftool.cgen.clazz import Class
from dune.perftool.cgen.exceptions import TryCatchBlock, CatchBlock
class Namespace(PrivateNamespace): class Namespace(PrivateNamespace):
......
""" Add Try/Catch blocks to cgen """
from cgen import Block, Generable, Value
class CatchBlock(Generable):
def __init__(self, exc_decl, catch_block):
assert isinstance(exc_decl, Value)
self.exc_decl = exc_decl
assert isinstance(catch_block, Block)
self.catch_block = catch_block
def generate(self):
yield "catch ({})\n".format("".join(self.exc_decl.generate(with_semicolon=False)))
for item in self.catch_block.generate():
yield item
yield "\n"
class TryCatchBlock(Generable):
def __init__(self, try_block, catch_blocks):
# Store the try block
assert isinstance(try_block, Block)
self.try_block = try_block
assert all(isinstance(b, CatchBlock) for b in catch_blocks)
self.catch_blocks = catch_blocks
def generate(self):
# Yield the try block
yield "\n"
yield "try\n"
for item in self.try_block.generate():
yield item
yield "\n"
# and now yield all the catch blocks
for catch_block in self.catch_blocks:
for item in catch_block.generate():
yield item
...@@ -307,6 +307,21 @@ def generate_driver(): ...@@ -307,6 +307,21 @@ def generate_driver():
contents.insert(len(contents) - 1, Line(text="DUMP_TIMER({}, driver, {}, true);\n".format(get_option("instrumentation_level"), timestream))) contents.insert(len(contents) - 1, Line(text="DUMP_TIMER({}, driver, {}, true);\n".format(get_option("instrumentation_level"), timestream)))
contents.insert(0, Line(text="\n")) contents.insert(0, Line(text="\n"))
driver_body = Block(contents) driver_body = Block(contents)
# Wrap a try/catch block around the driver body
from dune.perftool.cgen import CatchBlock, TryCatchBlock, Value, Block, Line
catch_blocks = [CatchBlock(Value("Dune::Exception&", "e"),
Block([Line("std::cerr << \"Dune reported error: \" << e << std::endl;\n"),
Line("return 1;\n"),
])
),
CatchBlock(Value("std::exception&", "e"),
Block([Line("std::cerr << \"Unknown exception thrown!\" << std::endl;\n"),
Line("return 1;\n"),
])
)
]
driver_body = Block([TryCatchBlock(driver_body, catch_blocks)])
driver = FunctionBody(driver_signature, driver_body) driver = FunctionBody(driver_signature, driver_body)
filename = get_option("driver_file") filename = get_option("driver_file")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment