ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
json_utils Namespace Reference

Functions

bool ast_equal_ignoring_location (const nlohmann::json &a, const nlohmann::json &b)
bool ast_location_key (const std::string &k)
bool ast_objects_equal_ignoring_location (const nlohmann::json &a, const nlohmann::json &b)
bool ast_arrays_equal_ignoring_location (const nlohmann::json &a, const nlohmann::json &b)
bool ast_contains_call (const nlohmann::json &n)
 True for X = Y between two bare names, such as _Elem = int.
bool is_name_alias (const nlohmann::json &stmt)
std::string dotted_to_path (const std::string &module_name)
 Convert a dotted Python module name to a filesystem path segment.
template<typename JsonType>
bool search_function_in_ast (const JsonType &node, const std::string &func_name)
template<typename JsonType>
JsonType find_class (const JsonType &ast_json, const std::string &class_name)
template<class Json>
Json find_attr_annotation (const Json &class_node, const std::string &attr)
 The annotation of self.
template<class Json>
std::size_t count_attr_stores (const Json &node, const std::string &attr, bool non_self_only)
 Counts the stores and deletions of <obj>.
template<class Json>
std::string constructed_attr_class (const Json &class_node, const std::string &attr)
 C when the __init__ of class_node holds self.
template<typename JsonType>
bool is_property_def (const JsonType &function_def)
template<typename JsonType>
bool any_class_has_property (const JsonType &ast_body, const std::string &name)
template<typename JsonType>
bool any_class_defines_method (const JsonType &node, const std::string &name)
 True when a class anywhere under node defines a method named name.
template<typename JsonType>
bool is_property_method (const JsonType &ast_body, const std::string &class_name, const std::string &method_name)
template<typename JsonType>
bool class_defines_method (const JsonType &ast_body, const std::string &class_name, const std::string &method_name)
 True when class_name or one of its (transitive) base classes defines a method named method_name.
template<typename JsonType>
std::size_t count_class_defs (const JsonType &node)
 Counts every ClassDef under node, at any depth.
template<typename JsonType>
unsigned count_function_scope_classes (const JsonType &body, const std::string &class_name, bool in_function=false)
 Counts the ClassDef nodes named class_name that sit inside a function body under body.
template<typename JsonType>
void collect_class_definition_lines (const JsonType &node, const std::string &class_name, std::vector< int > &lines)
 Source lines of every ClassDef named class_name under node that the converter registers: at any scope except directly inside a class body.
template<typename JsonType>
bool is_class (const std::string &name, const JsonType &ast_json)
template<typename JsonType>
bool is_module (const std::string &module_name, const JsonType &ast)
template<typename JsonType>
JsonType try_find_function (const JsonType &json, const std::string &func_name)
 Returns an empty JsonType on a miss. Never throws.
template<typename JsonType>
JsonType & find_function_or_throw (JsonType &json, const std::string &func_name)
 Throws std::runtime_error on a miss.
template<typename JsonType>
bool is_imported_from (const JsonType &ast, const std::string &module, const std::string &entity)
 True when from <module> import <entity> appears at the AST top level.
template<typename JsonType>
const JsonType & find_imported_function (const JsonType &ast, const std::string &func_name)
std::string dotted_name (const nlohmann::json &node)
 The dotted name a Name or Attribute node spells, e.g. "collections.abc.Set".
std::string resolve_import (const nlohmann::json &ast, const std::string &name)
 What dotted name denotes through the module's imports, e.g.
std::string collections_abc_class (const nlohmann::json &ast, const std::string &name)
 The collections.abc class that dotted name denotes, or empty.
template<typename JsonType>
const std::string get_object_alias (const JsonType &ast, const std::string &obj_name)
template<typename JsonType>
const JsonType get_var_node (const std::string &var_name, const JsonType &block)
std::vector< std::string > split_function_path (const std::string &function)
template<typename JsonType>
const JsonType * class_method_scope (const JsonType &body, std::string &name)
template<typename JsonType>
JsonType find_function_by_path (const JsonType &ast, const std::vector< std::string > &path)
template<typename JsonType>
JsonType find_var_in_function (const std::string &var_name, const JsonType &func_node)
template<typename JsonType>
const JsonType find_var_decl (const std::string &var_name, const std::string &function, const JsonType &ast)
template<typename JsonType>
std::string get_annotation_type_name (const JsonType &annotation)
std::vector< std::string > split_top_level_params (const std::string &s)
 Split a rendered parameter list on commas that are not inside brackets, trimming each part.
template<typename JsonType>
std::string render_annotation_type (const JsonType &annotation)
 The full generic spelling of an annotation node, e.g.
template<typename JsonType>
const JsonType get_var_value (const std::string &var_name, const std::string &function, const JsonType &ast)
std::size_t binds_by_field (const nlohmann::json &node, const std::string &kind, const std::string &name)
std::size_t count_name_bindings (const nlohmann::json &node, const std::string &name)
const nlohmann::json * top_level_value (const nlohmann::json &module, const std::string &name)
template<typename JsonType>
bool has_multiple_assignments_in_block (const std::string &var_name, const JsonType &block)
template<typename JsonType>
bool has_multiple_assignments_in_scope (const std::string &var_name, const std::string &function, const JsonType &ast)
template<typename JsonType>
bool extract_constant_integer (const JsonType &node, const std::string &function, const JsonType &ast, long long &value)
template<typename JsonType>
const JsonType get_list_element (const JsonType &list_value, int pos)
template<typename JsonType>
const JsonType find_return_node (const JsonType &block)
std::string extract_var_name_from_symbol_id (const std::string &symbol_id)
 Extract the variable name from a symbol identifier Examples: "py:test.py@l" -> "l" "py:test.py@F@foo@x" -> "x" "py:test.py@C@MyClass@F@method@var" -> "var".
template<typename JsonType>
JsonType build_char_sequence_node (const char *kind, const std::string &chars, const JsonType &loc_src)
template<typename JsonType>
bool has_overload_decorator (const JsonType &func_node)
template<typename JsonType>
std::string imported_function_return_type (const std::string &name, const JsonType &ast_json)
 The declared return-type name of a function name defined in a module this AST imports, or "" if there is none.
void collect_return_values (const nlohmann::json &node, std::vector< nlohmann::json > &values)

Function Documentation

◆ any_class_defines_method()

template<typename JsonType>
bool json_utils::any_class_defines_method ( const JsonType & node,
const std::string & name )

True when a class anywhere under node defines a method named name.

◆ any_class_has_property()

template<typename JsonType>
bool json_utils::any_class_has_property ( const JsonType & ast_body,
const std::string & name )

◆ ast_arrays_equal_ignoring_location()

bool json_utils::ast_arrays_equal_ignoring_location ( const nlohmann::json & a,
const nlohmann::json & b )
inline

◆ ast_contains_call()

bool json_utils::ast_contains_call ( const nlohmann::json & n)
inline

True for X = Y between two bare names, such as _Elem = int.

◆ ast_equal_ignoring_location()

bool json_utils::ast_equal_ignoring_location ( const nlohmann::json & a,
const nlohmann::json & b )
inline

◆ ast_location_key()

bool json_utils::ast_location_key ( const std::string & k)
inline

◆ ast_objects_equal_ignoring_location()

bool json_utils::ast_objects_equal_ignoring_location ( const nlohmann::json & a,
const nlohmann::json & b )
inline

◆ binds_by_field()

std::size_t json_utils::binds_by_field ( const nlohmann::json & node,
const std::string & kind,
const std::string & name )
inline

◆ build_char_sequence_node()

template<typename JsonType>
JsonType json_utils::build_char_sequence_node ( const char * kind,
const std::string & chars,
const JsonType & loc_src )

◆ class_defines_method()

template<typename JsonType>
bool json_utils::class_defines_method ( const JsonType & ast_body,
const std::string & class_name,
const std::string & method_name )

True when class_name or one of its (transitive) base classes defines a method named method_name.

◆ class_method_scope()

template<typename JsonType>
const JsonType * json_utils::class_method_scope ( const JsonType & body,
std::string & name )

◆ collect_class_definition_lines()

template<typename JsonType>
void json_utils::collect_class_definition_lines ( const JsonType & node,
const std::string & class_name,
std::vector< int > & lines )

Source lines of every ClassDef named class_name under node that the converter registers: at any scope except directly inside a class body.

◆ collect_return_values()

void json_utils::collect_return_values ( const nlohmann::json & node,
std::vector< nlohmann::json > & values )
inline

◆ collections_abc_class()

std::string json_utils::collections_abc_class ( const nlohmann::json & ast,
const std::string & name )
inline

The collections.abc class that dotted name denotes, or empty.

◆ constructed_attr_class()

template<class Json>
std::string json_utils::constructed_attr_class ( const Json & class_node,
const std::string & attr )

C when the __init__ of class_node holds self.

<attr> = C(...) as a top-level statement, or empty.

◆ count_attr_stores()

template<class Json>
std::size_t json_utils::count_attr_stores ( const Json & node,
const std::string & attr,
bool non_self_only )

Counts the stores and deletions of <obj>.

<attr> under node; with non_self_only, only those whose receiver is not self.

◆ count_class_defs()

template<typename JsonType>
std::size_t json_utils::count_class_defs ( const JsonType & node)

Counts every ClassDef under node, at any depth.

◆ count_function_scope_classes()

template<typename JsonType>
unsigned json_utils::count_function_scope_classes ( const JsonType & body,
const std::string & class_name,
bool in_function = false )

Counts the ClassDef nodes named class_name that sit inside a function body under body.

Module-scope definitions are not counted: find_class already reports those.

◆ count_name_bindings()

std::size_t json_utils::count_name_bindings ( const nlohmann::json & node,
const std::string & name )
inline

◆ dotted_name()

std::string json_utils::dotted_name ( const nlohmann::json & node)
inline

The dotted name a Name or Attribute node spells, e.g. "collections.abc.Set".

◆ dotted_to_path()

std::string json_utils::dotted_to_path ( const std::string & module_name)
inline

Convert a dotted Python module name to a filesystem path segment.

Example: "pkg.mod4" -> "pkg/mod4", "l.ks.foo" -> "l/ks/foo"

◆ extract_constant_integer()

template<typename JsonType>
bool json_utils::extract_constant_integer ( const JsonType & node,
const std::string & function,
const JsonType & ast,
long long & value )

◆ extract_var_name_from_symbol_id()

std::string json_utils::extract_var_name_from_symbol_id ( const std::string & symbol_id)
inline

Extract the variable name from a symbol identifier Examples: "py:test.py@l" -> "l" "py:test.py@F@foo@x" -> "x" "py:test.py@C@MyClass@F@method@var" -> "var".

◆ find_attr_annotation()

template<class Json>
Json json_utils::find_attr_annotation ( const Json & class_node,
const std::string & attr )

The annotation of self.

<attr> in the __init__ of class_node, or of <attr> in its body, or an empty Json.

◆ find_class()

template<typename JsonType>
JsonType json_utils::find_class ( const JsonType & ast_json,
const std::string & class_name )

◆ find_function_by_path()

template<typename JsonType>
JsonType json_utils::find_function_by_path ( const JsonType & ast,
const std::vector< std::string > & path )

◆ find_function_or_throw()

template<typename JsonType>
JsonType & json_utils::find_function_or_throw ( JsonType & json,
const std::string & func_name )

Throws std::runtime_error on a miss.

◆ find_imported_function()

template<typename JsonType>
const JsonType & json_utils::find_imported_function ( const JsonType & ast,
const std::string & func_name )

◆ find_return_node()

template<typename JsonType>
const JsonType json_utils::find_return_node ( const JsonType & block)

◆ find_var_decl()

template<typename JsonType>
const JsonType json_utils::find_var_decl ( const std::string & var_name,
const std::string & function,
const JsonType & ast )

◆ find_var_in_function()

template<typename JsonType>
JsonType json_utils::find_var_in_function ( const std::string & var_name,
const JsonType & func_node )

◆ get_annotation_type_name()

template<typename JsonType>
std::string json_utils::get_annotation_type_name ( const JsonType & annotation)

◆ get_list_element()

template<typename JsonType>
const JsonType json_utils::get_list_element ( const JsonType & list_value,
int pos )

◆ get_object_alias()

template<typename JsonType>
const std::string json_utils::get_object_alias ( const JsonType & ast,
const std::string & obj_name )

◆ get_var_node()

template<typename JsonType>
const JsonType json_utils::get_var_node ( const std::string & var_name,
const JsonType & block )

◆ get_var_value()

template<typename JsonType>
const JsonType json_utils::get_var_value ( const std::string & var_name,
const std::string & function,
const JsonType & ast )

◆ has_multiple_assignments_in_block()

template<typename JsonType>
bool json_utils::has_multiple_assignments_in_block ( const std::string & var_name,
const JsonType & block )

◆ has_multiple_assignments_in_scope()

template<typename JsonType>
bool json_utils::has_multiple_assignments_in_scope ( const std::string & var_name,
const std::string & function,
const JsonType & ast )

◆ has_overload_decorator()

template<typename JsonType>
bool json_utils::has_overload_decorator ( const JsonType & func_node)

◆ imported_function_return_type()

template<typename JsonType>
std::string json_utils::imported_function_return_type ( const std::string & name,
const JsonType & ast_json )

The declared return-type name of a function name defined in a module this AST imports, or "" if there is none.

Some operational models implement what Python calls a class as a function (collections.deque -> list[int]), and such a name is still legal in an annotation; resolving it to the return type is what makes d: collections.deque usable (#6639). Only imported modules are scanned, so a user function never shadows a type name this way.

◆ is_class()

template<typename JsonType>
bool json_utils::is_class ( const std::string & name,
const JsonType & ast_json )

◆ is_imported_from()

template<typename JsonType>
bool json_utils::is_imported_from ( const JsonType & ast,
const std::string & module,
const std::string & entity )

True when from <module> import <entity> appears at the AST top level.

◆ is_module()

template<typename JsonType>
bool json_utils::is_module ( const std::string & module_name,
const JsonType & ast )

◆ is_name_alias()

bool json_utils::is_name_alias ( const nlohmann::json & stmt)
inline

◆ is_property_def()

template<typename JsonType>
bool json_utils::is_property_def ( const JsonType & function_def)

◆ is_property_method()

template<typename JsonType>
bool json_utils::is_property_method ( const JsonType & ast_body,
const std::string & class_name,
const std::string & method_name )

◆ render_annotation_type()

template<typename JsonType>
std::string json_utils::render_annotation_type ( const JsonType & annotation)

The full generic spelling of an annotation node, e.g.

"int", "list[tuple[int, int]]". Unlike get_annotation_type_name, which flattens a parameterized slice to its base, this recurses, so an element type that is itself a generic survives round-tripping through python_annotation::create_annotation_from_type. Empty when the node is not an annotation this renderer understands.

◆ resolve_import()

std::string json_utils::resolve_import ( const nlohmann::json & ast,
const std::string & name )
inline

What dotted name denotes through the module's imports, e.g.

"m.Set" after import collections.abc as m is "collections.abc.Set"; name itself when no import binds its head.

◆ search_function_in_ast()

template<typename JsonType>
bool json_utils::search_function_in_ast ( const JsonType & node,
const std::string & func_name )

◆ split_function_path()

std::vector< std::string > json_utils::split_function_path ( const std::string & function)
inline

◆ split_top_level_params()

std::vector< std::string > json_utils::split_top_level_params ( const std::string & s)
inline

Split a rendered parameter list on commas that are not inside brackets, trimming each part.

"str, list[int, int]" -> {"str", "list[int, int]"}.

◆ top_level_value()

const nlohmann::json * json_utils::top_level_value ( const nlohmann::json & module,
const std::string & name )
inline

◆ try_find_function()

template<typename JsonType>
JsonType json_utils::try_find_function ( const JsonType & json,
const std::string & func_name )

Returns an empty JsonType on a miss. Never throws.