ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
python_exception_handler.h
Go to the documentation of this file.
1#pragma once
2
4#include <util/irep/expr.h>
6
7#include <nlohmann/json.hpp>
8#include <functional>
9#include <string>
10
12class type_handler;
13
24{
25public:
27 python_converter &converter,
29
30 // -----------------------------------------------------------------------
31 // Statement converters (called from python_converter::get_block)
32 // -----------------------------------------------------------------------
33
45 void get_try_statement(const nlohmann::json &element, codet &block);
46
51 static bool
52 body_has_escaping_control_flow(const nlohmann::json &node, bool in_loop);
53
57 static bool
58 escape_in_nested_cleanup_scope(const nlohmann::json &node, bool in_loop);
59
61 static bool
62 escape_in_nested_cleanup_children(const nlohmann::json &node, bool in_loop);
63
73 void get_raise_statement(const nlohmann::json &element, codet &block);
74
84 void
85 get_except_handler_statement(const nlohmann::json &element, codet &block);
86
87 // Emit one catch block for a single exception-type node (or null for a bare
88 // `except:`); used per type when `except (A, B):` lists a tuple of types.
90 const nlohmann::json &element,
91 const nlohmann::json &type_node,
92 codet &block);
93
94 // -----------------------------------------------------------------------
95 // Assertion helpers (previously python_converter private methods)
96 // -----------------------------------------------------------------------
97
111 const nlohmann::json &element,
112 const exprt &test,
113 code_blockt &block,
114 const std::function<void(code_assertt &)> &attach_assert_message);
115
131 const nlohmann::json &element,
132 const exprt &func_call_expr,
133 bool is_negated,
134 code_blockt &block,
135 const std::function<void(code_assertt &)> &attach_assert_message);
136
137 // -----------------------------------------------------------------------
138 // Expression helpers (previously function_call_expr / exception_utils)
139 // -----------------------------------------------------------------------
140
148 exprt
149 gen_exception_raise(const std::string &exc, const std::string &message) const;
150
151private:
152 python_converter &converter_;
153 type_handler &type_handler_;
154
155 // ------------------------------------------------------------------
156 // Internal helpers
157 // ------------------------------------------------------------------
158
161 exprt
162 build_builtin_exception(const nlohmann::json &exc, const typet &type) const;
163
166 bool raises_instance_variable(
167 const nlohmann::json &exc,
168 const std::string &exc_name) const;
169
174 nlohmann::json inject_finally_before_escapes(
175 const nlohmann::json &body,
176 const nlohmann::json &finalbody,
177 bool in_loop);
178
181 void emit_finally_then_escape(
182 const nlohmann::json &stmt,
183 const nlohmann::json &finalbody,
184 nlohmann::json &out);
185
187 unsigned finally_spill_count_ = 0;
188
189 exprt make_finally_rethrow_handler(const nlohmann::json &element);
190
191 void emit_else_clause(
192 const nlohmann::json &element,
193 exprt else_block,
194 const exprt &no_raise,
195 codet &block);
196
198 symbolt create_assert_temp_variable(const locationt &location) const;
199
204 static code_function_callt create_function_call_statement(
205 const exprt &func_call_expr,
206 const exprt &lhs_var,
207 const locationt &location);
208};
Definition std_code.h:243
Definition std_code.h:49
A `switch' instruction */ class code_switcht : public codet { public: inline code_switcht() : codet("...
Definition std_code.h:574
Definition std_code.h:8
Definition expr.h:34
Definition location.h:7
Main converter for transforming Python AST into ESBMC's intermediate representation.
Definition python_converter.h:76
python_exception_handler(python_converter &converter, type_handler &type_handler)
Definition python_exception_handler.cpp:25
exprt gen_exception_raise(const std::string &exc, const std::string &message) const
Build a cpp-throw side-effect expression for a named Python exception.
Definition python_exception_handler.cpp:882
void handle_list_assertion(const nlohmann::json &element, const exprt &test, code_blockt &block, const std::function< void(code_assertt &)> &attach_assert_message)
Emit a truthiness assertion on a list value.
Definition python_exception_handler.cpp:758
static bool escape_in_nested_cleanup_children(const nlohmann::json &node, bool in_loop)
escape_in_nested_cleanup_scope's walk over a statement's child bodies.
Definition python_exception_handler.cpp:337
static bool escape_in_nested_cleanup_scope(const nlohmann::json &node, bool in_loop)
True if an escaping return/break/continue sits inside a nested try or with, whose own cleanup must ru...
Definition python_exception_handler.cpp:356
void emit_catch_block(const nlohmann::json &element, const nlohmann::json &type_node, codet &block)
Definition python_exception_handler.cpp:615
void handle_function_call_assertion(const nlohmann::json &element, const exprt &func_call_expr, bool is_negated, code_blockt &block, const std::function< void(code_assertt &)> &attach_assert_message)
Emit an assertion whose condition depends on a function-call result.
Definition python_exception_handler.cpp:818
void get_except_handler_statement(const nlohmann::json &element, codet &block)
Convert a Python except-handler clause.
Definition python_exception_handler.cpp:733
void get_try_statement(const nlohmann::json &element, codet &block)
Convert a Python try/except block.
Definition python_exception_handler.cpp:36
void get_raise_statement(const nlohmann::json &element, codet &block)
Convert a Python raise statement.
Definition python_exception_handler.cpp:477
static bool body_has_escaping_control_flow(const nlohmann::json &node, bool in_loop)
True if node contains a return/break/continue that would transfer control out of an enclosing try (us...
Definition python_exception_handler.cpp:388
Definition symbol.h:12
Definition type_handler.h:113
Definition type.h:9