kerncraft/pycparser_utils.py
def replace_id(ast, id_name, replacement):
    """
    Replace all matching ID nodes in ast (in-place), with replacement.

    :param id_name: name of ID node to match
    :param replacement: single or list of node to insert in replacement for ID node.
    """
    for a in ast:
        if isinstance(a, c_ast.ID) and a.name == id_name:
            # Check all attributes of ast
            for attr_name in dir(ast):
                # Exclude special and method attributes
                if attr_name.startswith('__') or callable(getattr(ast, attr_name)):
                    continue
                attr = getattr(ast, attr_name)
                # In case of direct match, just replace
                if attr is a:
                    setattr(ast, attr_name, replacement)
                # If contained in list replace occurrence with replacement
                if type(attr) is list:
                    for i, attr_element in enumerate(attr):
                        if attr_element is a:
                            if type(replacement) is list:
                                # If replacement is list, inject
                                attr[i:i+1] = replacement
                            else:
                                # otherwise replace
                                attr[i] = replacement
        else:
            replace_id(a, id_name, replacement)
def replace_id(ast, id_name, replacement):
    """
    Replace all matching ID nodes in ast (in-place), with replacement.

    :param id_name: name of ID node to match
    :param replacement: single or list of node to insert in replacement for ID node.
    """
    for a in ast:
        if isinstance(a, c_ast.ID) and a.name == id_name:
            # Check all attributes of ast
            for attr_name in dir(ast):
                # Exclude special and method attributes
                if attr_name.startswith('__') or callable(getattr(ast, attr_name)):
                    continue
                attr = getattr(ast, attr_name)
                # In case of direct match, just replace
                if attr is a:
                    setattr(ast, attr_name, replacement)
                # If contained in list replace occurrence with replacement
                if type(attr) is list:
                    for i, attr_element in enumerate(attr):
                        if attr_element is a:
                            if type(replacement) is list:
                                # If replacement is list, inject
                                attr[i:i+1] = replacement
                            else:
                                # otherwise replace
                                attr[i] = replacement
        else:
            replace_id(a, id_name, replacement)
Replace all matching ID nodes in ast (in-place), with replacement.

    :param id_name: name of ID node to match
    :param replacement: single or list of node to insert in replacement for ID node.