Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def enumerate_includes(root_dir, directories):
"""
Walk through the directory tree, starting at root_dir, and enumerate all
valid include directories.
"""
for root, dirs, _ in os.walk(root_dir, topdown=True):
# modify dirs in place
dirs[:] = [dir_label for dir_label in dirs
if dir_label in directories
or (not dir_label.startswith('TARGET_')
and not dir_label.startswith('TOOLCHAIN_'))]
yield root
class TypeDeclVisitor(c_ast.NodeVisitor):
"""
A TypeDecl visitor class that walks the ast and calls a visitor function for every node found.
"""
def __init__(self, filter_names=None):
self.names = filter_names or []
def visit(self, node):
value = None
if node.__class__.__name__ == "TypeDecl":
value = self.visit_typedecl(node)
if value is None:
for _, child_node in node.children():
value = value or self.visit(child_node)
def visit_Assignment(self, node: ca.Assignment) -> None:
if isinstance(node.lvalue, ca.ID):
add_write(node.lvalue.name, indices[node])
self.generic_visit(node)
def visit_FuncDecl(self, node):
if node.args is None:
return None # Undefined parameter list ()
params = node.args.params
if (len(params) == 1 and isinstance(params[0], c_ast.Typename) and
isinstance(params[0].type, c_ast.TypeDecl) and
params[0].type.type.names == ['void']):
return [] # Empty parameter list (void)
return ['...' if isinstance(param, c_ast.EllipsisParam) else self.visit(param.type)
for param in node.args.params]
def parseTypeDecl(typeTable,node):
if type(node) == c_ast.Decl:
return parseTypeDecl(typeTable,node.type)
elif type(node) == c_ast.Struct:
return _parseStruct(typeTable,node)
elif type(node) == c_ast.TypeDecl:
return _parseTypeDecl(typeTable,node)
elif type(node) == c_ast.PtrDecl:
return _parsePtrDecl(typeTable,node)
elif type(node) == c_ast.ArrayDecl:
return _parseArrayDecl(typeTable,node)
elif type(node) == c_ast.FuncDecl:
return _parseFuncDecl(typeTable,node)
elif type(node) == c_ast.Typename:
return parseTypeDecl(typeTable,node.type)
elif type(node) == c_ast.EllipsisParam:
return VarArgType()
else:
"""
if isinstance(n, c_ast.Typedef):
if name is None:
name = n.name
ckind = 'ctypedef '
n.storage = [ ]
n.quals = [ ]
if not generate_decl(n.type, ckind, name):
w = Writer("{}{}".format(ckind, cython_from_c(n)))
w.write()
elif isinstance(n, c_ast.Decl):
if name is None:
name = n.name
n.storage = [ ]
if not generate_decl(n.type, ckind, name):
w = Writer("{}{}".format(ckind, cython_from_c(n)))
w.write()
elif isinstance(n, c_ast.TypeDecl):
if name is None:
name = n.name
return generate_decl(n.type, ckind, name)
def _parse_decl(self, decl):
node = decl.type
if isinstance(node, pycparser.c_ast.FuncDecl):
tp, quals = self._get_type_and_quals(node, name=decl.name)
assert isinstance(tp, model.RawFunctionType)
self._declare_function(tp, quals, decl)
else:
if isinstance(node, pycparser.c_ast.Struct):
self._get_struct_union_enum_type('struct', node)
elif isinstance(node, pycparser.c_ast.Union):
self._get_struct_union_enum_type('union', node)
elif isinstance(node, pycparser.c_ast.Enum):
self._get_struct_union_enum_type('enum', node)
elif not decl.name:
raise CDefError("construct does not declare any variable",
decl)
#
if decl.name:
tp, quals = self._get_type_and_quals(node,
partial_length_ok=True)
if tp.is_raw_function:
self._declare_function(tp, quals, decl)
elif (tp.is_integer_type() and
hasattr(decl, 'init') and
hasattr(decl.init, 'value') and
_r_int_literal.match(decl.init.value)):
def brace_nested_blocks(stmt: Statement) -> None:
def brace(stmt: Statement) -> Block:
if isinstance(stmt, (ca.Compound, ca.Case, ca.Default)):
return stmt
return ca.Compound([stmt])
if isinstance(stmt, (ca.For, ca.While, ca.DoWhile)):
stmt.stmt = brace(stmt.stmt)
elif isinstance(stmt, ca.If):
stmt.iftrue = brace(stmt.iftrue)
if stmt.iffalse:
stmt.iffalse = brace(stmt.iffalse)
elif isinstance(stmt, ca.Switch):
stmt.stmt = brace(stmt.stmt)
elif isinstance(stmt, ca.Label):
brace_nested_blocks(stmt.stmt)