Example #1
0
from utils import get_clang_args, get_clang_analyzer_diagnostics

if __name__ == '__main__':

    filenames = sys.argv[1:]

    clang_args = get_clang_args()

    clang_args = filter(lambda a: '-W' not in a, clang_args)

    # print clang_args

    for filename in filenames:
        index = cindex.Index.create()
        tu = index.parse(filename, clang_args)
        if not tu:
            print filename + " parsing failed"
            continue
        tu.reparse()

        for d in tu.diagnostics:
            if d.location.file and 'Developer/SDK' not in d.location.file.name:
                print(d.location.file.name + ':' + str(d.location.line) + ' ' +
                      d.spelling)
            else:
                # print('???:' + str(d.location.line) + ' ' + d.spelling)
                pass

        for d in get_clang_analyzer_diagnostics(filename, clang_args):
            print(d.filename + ':' + str(d.line_number) + ' ' + d.message)
Example #2
0
def lint_one_file(filename, pass_classes, clang_args):

    try:
        with open(filename) as fi:
            blob = fi.read()
    except IOError:
        print('Could not open file {0}'.format(filename))
        return []

    index = cindex.Index.create()
    parse_opts = cindex.TranslationUnit.PrecompiledPreamble

    tu = index.parse(filename, clang_args, options=parse_opts)

    if not tu:
        print('parsing {0} failed'.format(filename))
        return []

    local_cursors = [c for c in tu.cursor.get_children()\
            if c.location.file and not os.path.isabs(c.location.file.name)
               and not c.location.file.name.startswith('opt')
               and not c.location.file.name.startswith('./opt')]

    def group_cursors_by_kind(cs):
        result = {}
        all_cs = chain(*(get_children_recursively(c) for c in cs))
        for kind, sub_cs in groupby(all_cs, lambda c: c.kind):
            if kind not in result:
                result[kind] = []
            result[kind] += list(sub_cs)
        return result

    cursors_by_kind = group_cursors_by_kind(local_cursors)

    diags = []

    # get clang compiler diagnostics
    diags += [from_clang_diagnostic(d, filename) for d in tu.diagnostics]

    # get clang analyzer diagnostics
    diags += get_clang_analyzer_diagnostics(filename, clang_args)

    for pass_class in pass_classes:

        p = pass_class()

        if not p.enabled:
            continue

        if 'config' in pass_class.needs:
            p.config = config.Config()

        if 'cursors' in pass_class.needs:
            p.cursors = local_cursors

        if 'cursors_by_kind' in pass_class.needs:
            p.cursors_by_kind = cursors_by_kind

        if 'text' in pass_class.needs:
            p.text = blob

        if 'filename' in pass_class.needs:
            p.filename = filename

        if 'has_arc' in pass_class.needs:
            p.has_arc = '-fobjc-arc' in clang_args

        diags += p.get_diagnostics()

    return diags
Example #3
0
#!/usr/bin/env python
# encoding: utf-8

import sys
from clang import cindex
from utils import get_clang_args, get_clang_analyzer_diagnostics

if __name__ == '__main__':

    filename = sys.argv[1]

    clang_args = get_clang_args()

    clang_args = filter(lambda a: '-W' not in a, clang_args)

    print clang_args

    index = cindex.Index.create()
    tu = index.parse(filename, clang_args)
    tu.reparse()

    for d in tu.diagnostics:
        if d.location.file:
            print(d.location.file.name + ':' + str(d.location.line)
                    + ' ' + d.spelling)
        else:
            print('???:' + str(d.location.line) + ' ' + d.spelling)

    for d in get_clang_analyzer_diagnostics(filename, clang_args):
        print(d.filename + ':' + str(d.line_number) + ' ' + d.message)
Example #4
0
def lint_one_file(filename, pass_classes, clang_args):

    try:
        with open(filename) as fi:
            blob = fi.read()
    except IOError:
        print('Could not open file {0}'.format(filename))
        return []

    index = cindex.Index.create()
    parse_opts = cindex.TranslationUnit.PrecompiledPreamble

    tu = index.parse(filename, clang_args, options=parse_opts)

    local_cursors = [c for c in tu.cursor.get_children()\
            if c.location.file and not os.path.isabs(c.location.file.name)
               and not c.location.file.name.startswith('opt')
               and not c.location.file.name.startswith('./opt')]

    def group_cursors_by_kind(cs):
        result = {}
        all_cs = chain(*(get_children_recursively(c) for c in cs))
        for kind, sub_cs in groupby(all_cs, lambda c: c.kind):
            if kind not in result:
                result[kind] = []
            result[kind] += list(sub_cs)
        return result

    cursors_by_kind = group_cursors_by_kind(local_cursors)

    diags = []

    # get clang compiler diagnostics
    diags += [from_clang_diagnostic(d, filename) for d in tu.diagnostics]

    # get clang analyzer diagnostics
    diags += get_clang_analyzer_diagnostics(filename, clang_args)

    for pass_class in pass_classes:

        p = pass_class()

        if not p.enabled:
            continue

        if 'config' in pass_class.needs:
            p.config = config.Config()

        if 'cursors' in pass_class.needs:
            p.cursors = local_cursors

        if 'cursors_by_kind' in pass_class.needs:
            p.cursors_by_kind = cursors_by_kind

        if 'text' in pass_class.needs:
            p.text = blob

        if 'filename' in pass_class.needs:
            p.filename = filename

        if 'has_arc' in pass_class.needs:
            p.has_arc = '-fobjc-arc' in clang_args

        diags += p.get_diagnostics()

    return diags