def temp_hack(dir_name, main_file, main_file_bkp): main_file_name = main_file[:-2] if os.path.exists(dir_name + '%s_units/%s_main.c.callee'%(main_file_name, main_file_name)): return os.system('mv %s%s %s%s.new'%(dir_name, main_file, dir_name, main_file)) os.system('mv %s%s %s%s'%(dir_name, main_file_bkp, dir_name, main_file)) index = Index.create() tu = index.parse(dir_name + main_file) callee_list = get_callee_list(tu.cursor, 'main') callee_file = open(dir_name + '%s_units/%s_%sMain.c.callee'%(main_file_name, main_file_name, main_file_name), 'w+') for c in callee_list: callee_file.write(c + '\n') callee_file.close() os.system('mv %s%s %s%s'%(dir_name, main_file, dir_name, main_file_bkp)) os.system('mv %s%s.new %s%s'%(dir_name, main_file, dir_name, main_file))
def main(): global opts global g_sym_var_count global func_nodes global decl_vars decl_vars = [] func_nodes = [] inj_code = [] func_names = [] prob_branch = [] prob_exprs = [] parser = OptionParser("usage: %prog -f {source filename} [-n {func_to_test}] [-a {test all functions}]") parser.add_option('-f', '--file', action='store', type='string', dest='filename', default='None', help='Source file name') parser.add_option('-a', '--all', action='store_true', dest='all_funcs', default='False', help='Analyse all functions in the source file') parser.add_option('-n', '--name', action='store', type='string', dest='func_name', default='None', help='Name of the function to be analysed') (opts, args) = parser.parse_args() # pprint(('diags', map(get_diag_info, tu.diagnostics))) # Transform using a command line flag all_funcs = opts.all_funcs src_fl_name = opts.filename func_name = opts.func_name if not src_fl_name.endswith('.c'): print 'The input file does not seem to be a C source.\n"--help" for usage\nExiting.' sys.exit() index = Index.create() tu = index.parse(src_fl_name) if not tu: parser.error("unable to load input") # Very important: Which function to unit test? if all_funcs==True: print 'analysing all functions in source file: ' + src_fl_name get_func_nodes(tu.cursor) for f in func_nodes: func_names.append(f.spelling) else: if func_name==None: print 'Supply function name using flag "-n"\nOr use "-a" to analyse all functions' sys.exit(-1) print 'analysing ' + func_name + ' in source file: ' + src_fl_name func_names = [func_name] orig_fl = open(src_fl_name, 'r') for f in func_names: func_node = find_func_node(tu.cursor, f) if not func_node: print 'No function found with the name: ' + f+ '\nSomething went wrong' continue #sys.exit(-1) decl_code = generate_decl_code(func_node, orig_fl) instr_code = generate_sym_params(func_node, orig_fl) inj_code.append((func_node, decl_code, instr_code)) prob_branch_res, prob_expr = analyze(src_fl_name, f) prob_branch.extend(prob_branch_res) prob_exprs.extend(prob_expr) modified_prob_exprs = [] for pb in prob_branch: if ":" not in pb: print "Found some problem in branch. Trying to move on\n" continue line, symb, st = pb.split(':')[:3] line = int(line.strip()) symb = symb.strip() # Find datatype of symb datatype = '' for ex in prob_exprs: if ex.iden==symb: datatype = ex.datatype if datatype=='': print 'Something went wrong.\nExiting.' sys.exit(-1) modified_prob_exprs.append((line, symb, datatype)) main_funcs = generate_main_code(inj_code, orig_fl) orig_fl.close() orig_fl = open(src_fl_name, 'r') all_minus_main = copy_all_minus_main(orig_fl, tu.cursor, modified_prob_exprs) ''' if 'main' in func_names: # Analyzing main function main_aux = generate_main_aux(orig_fl, tu.cursor, modified_prob_exprs) ''' # Finally write all unit tests inside a folder if not os.path.isdir(src_fl_name[:-2]+'_units'): os.system('mkdir '+src_fl_name[:-2]+'_units') for unit_i, func_name in enumerate(main_funcs.keys()): if func_name=='main': os.system('cp ' + src_fl_name + ' ' + src_fl_name[:-2]+ '_units/' + os.path.basename(src_fl_name)[:-2]+'_'+func_name+'.c.units') else: instr_fl = open(src_fl_name[:-2]+'_units/' + os.path.basename(src_fl_name)[:-2]+'_'+func_name+'.c.units', 'w') instr_fl.write(all_minus_main) #TODO: send prob_branch and prob_exprs to instrument code in the functions instr_fl.write(main_funcs[func_name]) instr_fl.close() callee_fl = open(src_fl_name[:-2]+'_units/' + os.path.basename(src_fl_name)[:-2]+'_'+func_name+'.c.callee', 'w') callee_list = get_callee_list(tu.cursor, func_name) for c in callee_list: if not c=='': callee_fl.write(c+'\n') callee_fl.close() hints_fl = open(src_fl_name+'.hints', 'w') if len(prob_branch)>0: print 'Some assignments may benefit from symbolic return values -' print 'Recorded these line numbers in %s.hints'%(src_fl_name) hints_fl.write('line# : var : statement\n') for b in prob_branch: hints_fl.write(b+'\n')
def main(): global opts global g_sym_var_count global func_nodes global decl_vars decl_vars = [] func_nodes = [] inj_code = [] func_names = [] prob_branch = [] prob_exprs = [] parser = OptionParser( "usage: %prog -f {source filename} [-n {func_to_test}] [-a {test all functions}]" ) parser.add_option('-f', '--file', action='store', type='string', dest='filename', default='None', help='Source file name') parser.add_option('-a', '--all', action='store_true', dest='all_funcs', default='False', help='Analyse all functions in the source file') parser.add_option('-n', '--name', action='store', type='string', dest='func_name', default='None', help='Name of the function to be analysed') (opts, args) = parser.parse_args() # pprint(('diags', map(get_diag_info, tu.diagnostics))) # Transform using a command line flag all_funcs = opts.all_funcs src_fl_name = opts.filename func_name = opts.func_name if not src_fl_name.endswith('.c'): print 'The input file does not seem to be a C source.\n"--help" for usage\nExiting.' sys.exit() index = Index.create() tu = index.parse(src_fl_name) if not tu: parser.error("unable to load input") # Very important: Which function to unit test? if all_funcs == True: print 'analysing all functions in source file: ' + src_fl_name get_func_nodes(tu.cursor) for f in func_nodes: func_names.append(f.spelling) else: if func_name == None: print 'Supply function name using flag "-n"\nOr use "-a" to analyse all functions' sys.exit(-1) print 'analysing ' + func_name + ' in source file: ' + src_fl_name func_names = [func_name] orig_fl = open(src_fl_name, 'r') for f in func_names: func_node = find_func_node(tu.cursor, f) if not func_node: print 'No function found with the name: ' + f + '\nSomething went wrong' continue #sys.exit(-1) decl_code = generate_decl_code(func_node, orig_fl) instr_code = generate_sym_params(func_node, orig_fl) inj_code.append((func_node, decl_code, instr_code)) prob_branch_res, prob_expr = analyze(src_fl_name, f) prob_branch.extend(prob_branch_res) prob_exprs.extend(prob_expr) modified_prob_exprs = [] for pb in prob_branch: if ":" not in pb: print "Found some problem in branch. Trying to move on\n" continue line, symb, st = pb.split(':')[:3] line = int(line.strip()) symb = symb.strip() # Find datatype of symb datatype = '' for ex in prob_exprs: if ex.iden == symb: datatype = ex.datatype if datatype == '': print 'Something went wrong.\nExiting.' sys.exit(-1) modified_prob_exprs.append((line, symb, datatype)) main_funcs = generate_main_code(inj_code, orig_fl) orig_fl.close() orig_fl = open(src_fl_name, 'r') all_minus_main = copy_all_minus_main(orig_fl, tu.cursor, modified_prob_exprs) ''' if 'main' in func_names: # Analyzing main function main_aux = generate_main_aux(orig_fl, tu.cursor, modified_prob_exprs) ''' # Finally write all unit tests inside a folder if not os.path.isdir(src_fl_name[:-2] + '_units'): os.system('mkdir ' + src_fl_name[:-2] + '_units') for unit_i, func_name in enumerate(main_funcs.keys()): if func_name == 'main': os.system('cp ' + src_fl_name + ' ' + src_fl_name[:-2] + '_units/' + os.path.basename(src_fl_name)[:-2] + '_' + func_name + '.c.units') else: instr_fl = open( src_fl_name[:-2] + '_units/' + os.path.basename(src_fl_name)[:-2] + '_' + func_name + '.c.units', 'w') instr_fl.write( all_minus_main ) #TODO: send prob_branch and prob_exprs to instrument code in the functions instr_fl.write(main_funcs[func_name]) instr_fl.close() callee_fl = open( src_fl_name[:-2] + '_units/' + os.path.basename(src_fl_name)[:-2] + '_' + func_name + '.c.callee', 'w') callee_list = get_callee_list(tu.cursor, func_name) for c in callee_list: if not c == '': callee_fl.write(c + '\n') callee_fl.close() hints_fl = open(src_fl_name + '.hints', 'w') if len(prob_branch) > 0: print 'Some assignments may benefit from symbolic return values -' print 'Recorded these line numbers in %s.hints' % (src_fl_name) hints_fl.write('line# : var : statement\n') for b in prob_branch: hints_fl.write(b + '\n')