Exemple #1
0
def get_overrides(*sourcefiles):
    """
    Open the given source file, parse it and determine the overrides map
    """
    overrides = [
        override_scraper.get_overrides(
            ast_helpers.get_translation_unit(sourcefile))
        for sourcefile in sourcefiles
    ]

    return override_scraper.merge(overrides)
Exemple #2
0
    functions (augmented with overrides), determine the indirect types of
    all functions and return a mapping of function to its complete type
    """
    types = {}

    for function in function_types.keys():
        indirect_types = determine_indirect_type(function, call_tree,
                                                 funptr_types, function_types)
        direct_types = function_types[function]
        types[function] = indirect_types | direct_types

    return types


if __name__ == '__main__':
    from pprint import pprint
    import scrapers
    import call_tree
    from ast_helpers import get_translation_unit

    target = get_translation_unit(sys.argv[1])

    call_tree = call_tree.build_call_tree(target)
    overrides = scrapers.Overrides.scrape(target)
    func_types = scrapers.FunctionQualifiers.scrape(target)
    funcptr_types = scrapers.FunctionPointers.scrape(target)

    call_tree.augment_with_overrides(overrides)

    pprint(augment_types(call_tree, funcptr_types, func_types))
Exemple #3
0
    """
    Build a call tree by recursively visiting all the nodes in a given
    context.  This function mutates |call_tree| in place.
    """
    if (node.kind in [
            CursorKind.FUNCTION_DECL, CursorKind.CXX_METHOD,
            CursorKind.CONSTRUCTOR, CursorKind.DESTRUCTOR
    ]):
        caller = node
    elif (node.kind == CursorKind.CALL_EXPR):
        if node.referenced:
            func = node.referenced

            if type(caller) == TranslationUnit:
                caller_str = '_start'
            else:
                caller_str = caller.get_usr()

            call_tree.add(caller_str, func.get_usr())

    for c in node.get_children():
        _rec_build_call_tree(c, caller, call_tree)


if __name__ == '__main__':
    from ast_helpers import get_translation_unit
    import sys
    from pprint import pprint

    pprint(dict(build_call_tree(get_translation_unit(sys.argv[1])).tree))
Exemple #4
0
def scrape_all_files(files, ext_types, options):
    call_subtrees = []
    override_subtrees = []
    cursor_subsets = []
    func_type_subsets = []
    funcptr_type_subsets = []
    assignment_subsets = []

    tu_time = 0.0
    start_time = time.time()

    for fname in files:
        overrideScraper = scrapers.Overrides()
        cursorScraper = scrapers.FunctionCursors()
        funcTypeScraper = scrapers.FunctionQualifiers()
        funPtrTypeScraper = scrapers.FunctionPointers()
        assScraper = scrapers.FunPtrAssignments()

        pre_tu_time = time.time()
        target = ast_helpers.get_translation_unit(fname, options)

        logging.info("Translation unit: " + str(target.spelling))
        #ast_helpers.dump_ast( target.cursor, lambda x: logging.debug(x) )

        tu_time += time.time() - pre_tu_time

        scrapers.run_scrapers(target.cursor, [
            overrideScraper, cursorScraper, funcTypeScraper, funPtrTypeScraper,
            assScraper
        ])

        call_subtrees.append(build_call_tree(target))
        override_subtrees.append(overrideScraper.get())
        cursor_subsets.append(cursorScraper.get())
        func_type_subsets.append(funcTypeScraper.get())
        funcptr_type_subsets.append(funPtrTypeScraper.get())
        assignment_subsets.append(assScraper.get())

    call_tree = merge_call_trees(call_subtrees)
    overrides = scrapers.Overrides.merge(override_subtrees)
    cursors = scrapers.FunctionCursors.merge(cursor_subsets)
    func_types = scrapers.FunctionQualifiers.merge(func_type_subsets +
                                                   [ext_types])
    funcptr_types = scrapers.FunctionPointers.merge(funcptr_type_subsets)
    assignments = scrapers.FunPtrAssignments.merge(assignment_subsets)

    call_tree.augment_with_overrides(overrides)

    pre_augment_time = time.time()
    aug_func_types = augment_types(call_tree, funcptr_types, func_types)
    post_augment_time = time.time()

    standard_funcs = set([key for key in func_types.keys()])

    all_func_types = scrapers.merge_disjoint_dicts(
        [aug_func_types, funcptr_types])

    end_time = time.time()

    if options.show_time:
        print("time to parse: {:0.5f} seconds".format(tu_time))
        print("time to scrape stuff: {:0.5f} seconds".format(end_time -
                                                             start_time -
                                                             tu_time))
        print("time to infer indirect type: {:0.5f} seconds".format(
            post_augment_time - pre_augment_time))

    return (call_tree, all_func_types, cursors, assignments, standard_funcs,
            overrides)
Exemple #5
0
def main(options, args):
    tu = get_translation_unit(args[0], options)
    dump_func(tu.cursor, args[0], options)