How to use the pycparser.c_ast.Compound function in pycparser

To help you get started, we’ve selected a few pycparser examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github blexim / synth / src / frontend / frontend.py View on Github external
arr = c_ast.ArrayRef(c_ast.ID('out_vars'),
                         c_ast.Constant('int', str(id)))
    var = c_ast.ID(varname)

    copy_out.append(c_ast.Assignment("=", arr, var))


  prefix.block_items += copy_out
  prefix.block_items.append(c_ast.Return(c_ast.Constant('int', str('1'))))

  ofile.write("int prefix(word_t in_vars[%d], word_t out_vars[%d]) {\n" % (nids, nids))
  ofile.write(cgen.visit(prefix))
  ofile.write("}\n\n")

  ofile.write("int guard(word_t in_vars[%d]) {\n" % nids)
  guard_body = c_ast.Compound(copy.copy(decls))
  guard_body.block_items.append(c_ast.Return(loop.cond))
  ofile.write(cgen.visit(guard_body))
  ofile.write("}\n\n")

  ofile.write("void body(word_t in_vars[%d], word_t out_vars[%d]) {\n" % (nids, nids))
  loop_body = c_ast.Compound(copy.copy(decls))
  loop_body.block_items.append(loop.stmt)
  loop_body.block_items += copy_out
  ofile.write(cgen.visit(loop_body))
  ofile.write("}\n\n")
github ElectronVector / ravioli / ravioli / c_parser.py View on Github external
def calculate_complexity(self, node):
        complexity = 0
        if type(node) is c_ast.Compound and node.block_items is not None:
            for item in node.block_items:
                complexity += self.calculate_single_item_complexity(item)
        else:
            complexity += self.calculate_single_item_complexity(node)
        return complexity
github blexim / synth / src / frontends / splitter.py View on Github external
f.write("int inner_body(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n")
    f.write(inner_body)
    f.write("}\n\n")

    f.write("int inner_suffix(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n")
    inner_suffix.block_items = decls + inner_suffix.block_items + copy_out
    f.write(inner_suffix)
    f.write("}\n\n")

    f.write("int outer_guard(word_t in_vars[NARGS]) {\n")
    f.write(outer_guard)
    f.write("}\n\n")
  else:
    f.write("int body(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n")
    loop_body = c_ast.Compound(copy.copy(decls) + loop.stmt.block_items + copy_out)
    f.write(loop_body)
    f.write("}\n\n")

  return (rev_id_map, has_nested, nondet)
github blexim / synth / src / frontend / frontend.py View on Github external
def start_straightline(self):
    self.curr = []
    compound = c_ast.Compound(self.curr)
    self.blocks.append(compound)
github eliben / pycparser / pycparser / ast_transforms.py View on Github external
return 10
                    Case 20:
                    Case 30:
                        return 20
                    Default:
                        break

        A fixed AST node is returned. The argument may be modified.
    """
    assert isinstance(switch_node, c_ast.Switch)
    if not isinstance(switch_node.stmt, c_ast.Compound):
        return switch_node

    # The new Compound child for the Switch, which will collect children in the
    # correct order
    new_compound = c_ast.Compound([], switch_node.stmt.coord)

    # The last Case/Default node
    last_case = None

    # Goes over the children of the Compound below the Switch, adding them
    # either directly below new_compound or below the last Case as appropriate
    # (for `switch(cond) {}`, block_items would have been None)
    for child in (switch_node.stmt.block_items or []):
        if isinstance(child, (c_ast.Case, c_ast.Default)):
            # If it's a Case/Default:
            # 1. Add it to the Compound and mark as "last case"
            # 2. If its immediate child is also a Case or Default, promote it
            #    to a sibling.
            new_compound.block_items.append(child)
            _extract_nested_case(child, new_compound.block_items)
            last_case = new_compound.block_items[-1]
github eliben / pycparser / pycparser / c_parser.py View on Github external
def p_pragmacomp_or_statement(self, p):
        """ pragmacomp_or_statement     : pppragma_directive statement
                                        | statement
        """
        if isinstance(p[1], c_ast.Pragma) and len(p) == 3:
            p[0] = c_ast.Compound(
                block_items=[p[1], p[2]],
                coord=self._token_coord(p, 1))
        else:
            p[0] = p[1]
github blexim / synth / src / frontends / splitter.py View on Github external
arr = c_ast.ArrayRef(c_ast.ID('out_vars'),
                         c_ast.Constant('int', str(id)))
    var = c_ast.ID(varname)

    copy_out.append(c_ast.Assignment("=", arr, var))

  copy_out.append(c_ast.Return(c_ast.Constant('int', str('1'))))

  prefix.block_items += copy_out

  f.write("int prefix(word_t in_vars[NARGS], word_t out_vars[NARGS]) {\n")
  f.write(prefix)
  f.write("}\n\n")

  f.write("int guard(word_t in_vars[NARGS]) {\n")
  guard_body = c_ast.Compound(copy.copy(decls))
  guard_body.block_items.append(c_ast.Return(loop.cond))
  f.write(guard_body)
  ofile.write("}\n\n")

  if is_nested_loop(loop):
    has_nested = True

    nested_prog = FlatProgram()
    flatten(loop.stmt, nested_prog)
    inner_prefix = nested_prog.blocks[0]
    inner_body = nested_prog.blocks[1]
    inner_suffix = nested_prog.blocks[2]

    inner_guard = c_ast.Compound(copy.copy(decls) + [c_ast.Return(inner_body.cond)])

    if isinstance(inner_body.stmt, c_ast.Compound):
github simonlindholm / decomp-permuter / src / randomizer.py View on Github external
node.iffalse = rec(node.iffalse, True)
        elif isinstance(node, ca.Return):
            if node.expr:
                node.expr = rec(node.expr)
        elif isinstance(node, ca.Decl):
            if node.init:
                node.init = rec(node.init, isinstance(node.init, ca.InitList))
        elif isinstance(node, ca.For):
            if node.init:
                node.init = rec(node.init)
            if node.cond:
                node.cond = rec(node.cond)
            if node.next:
                node.next = rec(node.next, True)
            node.stmt = rec(node.stmt, True)
        elif isinstance(node, ca.Compound):
            for sub in node.block_items or []:
                rec(sub, True)
        elif isinstance(node, (ca.Case, ca.Default)):
            for sub in node.stmts or []:
                rec(sub, True)
        elif isinstance(node, ca.While):
            node.cond = rec(node.cond)
            node.stmt = rec(node.stmt, True)
        elif isinstance(node, ca.DoWhile):
            node.stmt = rec(node.stmt, True)
            node.cond = rec(node.cond)
        elif isinstance(node, ca.Switch):
            node.cond = rec(node.cond)
            node.stmt = rec(node.stmt, True)
        elif isinstance(node, ca.Label):
            node.stmt = rec(node.stmt, True)