Esempio n. 1
0
def parse_warnings_new(project_dir, files, silent=False):
    split_sources_headers(files)
    sources, headers, _ = split_sources_headers(files)
    sources_path = build_full_paths(project_dir, sources)
    p = subprocess.Popen(
        "clang-tidy %s -config='%s' --extra-arg='-fno-color-diagnostics' --" %
        (' '.join(sources_path), json.dumps(clang_tidy_checks)),
        shell=True,
        stdout=subprocess.PIPE,
        stderr=silent and subprocess.PIPE or None)

    warnings = {}
    warnings_count = 0

    if not silent:
        print('\nparsing clang-tidy results:')
        print('Ignore the following warnings, if they are suspended.')
    while p.poll() is None:
        line = p.stdout.readline().decode('utf-8').strip()
        res = re.findall(r'warning:.*?\[(.*?)\]', line)
        if res:
            if res[0] in warnings:
                warnings[res[0]] += 1
            else:
                warnings[res[0]] = 1
            warnings_count += 1

    return warnings, warnings_count
Esempio n. 2
0
def parse_functions_new(project_dir, files, silent=False, functions=None):
    if not functions:
        functions = dict()

    split_sources_headers(files)
    sources, headers, _ = split_sources_headers(files)
    files = sources + headers
    sources_path = build_full_paths(project_dir, sources)
    files_path = build_full_paths(project_dir, files)
    p = subprocess.Popen(
        "clang-check -ast-dump %s --extra-arg='-fno-color-diagnostics' --" %
        ' '.join(sources_path),
        shell=True,
        stdout=subprocess.PIPE,
        stderr=silent and subprocess.PIPE or None)
    # print(sources_path)

    current_file = None

    if not silent:
        print('parsing function declarations:')

    func_decl_lines = []

    while p.poll() is None:
        line = p.stdout.readline().decode('utf-8').strip()
        result = re.findall(r'<(?!(line|col))(.*?), (line|col):.*?>', line)
        if result:
            file_name = result[0][1]
            flag = False
            for i, file_path in enumerate(files_path):
                if file_path in file_name:
                    # print(files[i], line)
                    current_file = files[i]
                    flag = True
                    break
            if not flag:
                current_file = None

        if current_file and 'line' in line and ' default ' not in line and \
                ('FunctionDecl' in line or 'CXXConstructorDecl' in line
                 or 'CXXDestructorDecl' in line or 'CXXMethodDecl' in line):
            # print(line)
            line = line.strip()
            func_decl_lines.append((line, current_file))

    file_func_decls = {x: [] for x in files}

    for line, file in func_decl_lines:
        func_decl = FunctionDeclaration(line, file)
        if not func_decl.error:
            func_prototype = str(func_decl)
            if not silent:
                print('[%s:%d-%d] %s' % (func_decl.file, func_decl.start,
                                         func_decl.end, func_decl))
            if func_prototype not in functions:
                functions[func_prototype] = Function(func_decl)
            else:
                functions[func_prototype].add_declaration(func_decl)
            file_func_decls[file].append(func_decl)
        elif not silent:
            print('error occurred in %s' % line)

    if not silent:
        print('\nparsing cpp files:')

    for i, file in enumerate(files):
        try:
            file_contents = read_file(files_path[i], silent=silent)
            func_decls = sorted(file_func_decls[file], key=lambda x: x.start)
            for j, func_decl in enumerate(func_decls):
                if func_decl.end <= len(file_contents):
                    # print(j, func_decl)
                    if j > 0:
                        start = max(func_decls[j - 1].end,
                                    func_decl.start - 20)
                    else:
                        start = max(0, func_decl.start - 20)
                    end = min(func_decl.end, len(file_contents) - 1)
                    func_decl.calculate_length(
                        file_contents[func_decl.start:end + 1])
                    func_decl.set_body([(x, file_contents[x])
                                        for x in range(start, end + 1)])
        except Exception as e:
            if not silent:
                print(e.args)

    for function in functions.values():
        function.calculate_length()

    return functions