Ejemplo n.º 1
0
 def __init__(self, folder):
     self.builder = Builder()
     self.tester = Tester()
     self.folder = folder
     self.analyzer = config.get('analyzer')
     self.start()
Ejemplo n.º 2
0
class Reader:
    def __init__(self, folder):
        self.builder = Builder()
        self.tester = Tester()
        self.folder = folder
        self.analyzer = config.get('analyzer')
        self.start()
    def start(self):
        for root,_,files in os.walk(self.folder):
            for f in files:
                if f.endswith('.c') and f != 'generate.c':
                    if config.get('file_include'):
                        if f.split('/')[-1] not in file_include: continue
                    if config.get('file_exclude'):
                        if f.split('/')[-1] in file_exclude: continue
                    filename = os.path.join(root, f)
                    doc = domutil.get_doc_from_c_file(filename)
                    segments = get_segments(doc)
                    if segments:
                        if config.get('show_filename') == 'true':
                            print(filename)
                        if config.get('interact_before_segment') == 'true':
                            c = input('Enter to start the file, anything to break the file ...')
                            if c: continue
                    for segment in segments:
                        self.handle_segment(doc, segment)

    def handle_segment(self, doc, segment):
        if not segment: return
        segment_text = ''
        for node in segment:
            segment_text += domutil.get_text_content(node) + '\n'
        if config.get('max_segment_size'):
            if segment_text.count('\n') > int(config.get('max_segment_size')):
                return
        if config.get('show_segment_size') == 'true':
            print('segment size: '+str(segment_text.count('\n')))
        if config.get('show_parent_function_size') == 'true':
            function_node = domutil.get_parent_by_tagname(segment[0], 'function')
            function_size = 0
            if function_node:
                text = domutil.get_text_content(function_node)
                function_size = text.count('\n')
            print('parent function size: '+str(function_size))
        if config.get('show_segment') == 'true':
            print(segment_text)
        instrument_segment(doc, segment)
        max_linear_value = int(config.get('max_linear_value'))
        stop_criteria = StopCriteria()
        for i in range(max_linear_value+1):
            logger.info('context search: ' + str(i))
            if config.get('show_context_search_value') == 'true':
                print('context search value: '+str(i))
            context, functions = get_context(doc, segment, i)
            if not context: break
            if self.builder.build(doc, context, functions):
                if config.get('run_test') == 'true':
                    result = self.tester.test()
                    if self.analyzer == 'recursive':
                        if result == True:
                            green('found equivalent loops for recursive call')
                            print_nodes(segment)
                    elif self.analyzer == 'invariant':
                        if config.get('show_analyze_result') == 'true':
                            print(result)
                        stop_criteria.add(result)
                        stable_result = stop_criteria.get_stable()
                        if stable_result:
                            green(stable_result)
                    if config.get('interact_after_test') == 'true':
                        input('Enter to continue ...')
        remove_instrument(doc)