def func(directory): for root,_,files in os.walk(directory): for f in files: if f.endswith('.c'): filename = os.path.join(root, f) print(filename) with tempfile.TemporaryFile() as fp: with open(filename) as f: change_count = 0 for line in f: if line.strip().endswith(';') and ',' in line: if line.count('(') == line.count(')'): doc = domutil.get_doc_from_code(line) unit_nodes = doc.getElementsByTagName('unit') unit_node = unit_nodes.item(0) decl_stmt_node = domutil.get_first_child_by_tagname(unit_node, 'decl_stmt') if domutil.is_element(decl_stmt_node) and decl_stmt_node.tagName == 'decl_stmt': decl_nodes = domutil.get_children_by_tagname(decl_stmt_node, 'decl') if len(decl_nodes) > 1: change_count+=1 # result = syntaxutil.parse_decl_stmt_code(line) full_type = get_type(line) # FIXME job *job, *sj = deserializeJob(nextjob,remlen,&nextjob,SER_MESSAGE); if full_type and '(' not in line: variables = line.split(',')[:] var1 = variables[0] line = var1 + ';\n' + ';\n'.join([full_type + ' ' + var for var in variables[1:]]) fp.write(line.encode('utf8')) print('made '+str(change_count)+' changes to file: ' + filename) fp.seek(0) content = fp.read().decode('utf8') with open(filename, 'w') as f: f.write(content)
def get_struct_name(code): doc = domutil.get_doc_from_code(code) structs = doc.getElementsByTagName('struct') if structs: struct = structs[0] name_node = domutil.get_first_child_by_tagname(struct, 'name') name = domutil.get_text_content(name_node) return name return None
def get_struct_alias(code): doc = domutil.get_doc_from_code(code) typedefs = doc.getElementsByTagName('typedef') if typedefs: # this is typedef struct xxx {} name; typedef = typedefs[0] type_node = domutil.get_first_child_by_tagname(typedef, 'type') struct_node = domutil.get_first_child_by_tagname(type_node, 'struct') if not struct_node: logger.warning('it is not a structure') return None alias_node = domutil.get_first_child_by_tagname(typedef, 'name') alias = domutil.get_text_content(alias_node) return alias return None
def parse_cpp_define(node): """parse a #define statement. <cpp:define> :return a set of function name to resolve """ to_resolve = set() # cpp_macro = domutil.get_first_child_by_tagname(node, 'cpp:macro') cpp_value = domutil.get_first_child_by_tagname(node, 'cpp:value') # param_list_node = domutil.get_first_child_by_tagname(cpp_macro, 'parameter_list') # params = parse_parameter_list(param_list_node) value = domutil.get_text_content(cpp_value) doc = domutil.get_doc_from_code(value) for call_node in doc.getElementsByTagName('call'): name_node = domutil.get_first_child_by_tagname(call_node, 'name') name = domutil.get_text_content(name_node) to_resolve.add(name) # emitf(__LINE__, "\t" __VA_ARGS__ # this will be passed as <macro> for macro_node in doc.getElementsByTagName('macro'): name_node = domutil.get_first_child_by_tagname(macro_node, 'name') name = domutil.get_text_content(name_node) to_resolve.add(name) return to_resolve
def parse_struct_code(code): doc = domutil.get_doc_from_code(code) struct_node = doc.getElementsByTagName('struct').item(0) return parse_struct(struct_node)
def parse_typedef_code(code): doc = domutil.get_doc_from_code(code) typedef_node = doc.getElementsByTagName('typedef').item(0) return parse_typedef(typedef_node)