Beispiel #1
0
def test_create_fail():
    """Check we fail loading a database with an assertion"""
    path = os.path.dirname(__file__)
    try:
        CompilationDatabase.fromDirectory(path)
    except CompilationDatabaseError as e:
        assert e.cdb_error == CompilationDatabaseError.ERROR_CANNOTLOADDATABASE
    else:
        assert False
Beispiel #2
0
 def test_compilationDB_references(self):
     """Ensure CompilationsCommands are independent of the database"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
     del cdb
     gc.collect()
     workingdir = cmds[0].directory
Beispiel #3
0
def test_2_compilecommand():
    """Check file with 2 compile commands"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project2.cpp")
    assert len(cmds) == 2
    expected = [
        {
            "wd": "/home/john.doe/MyProjectA",
            "line": ["clang++", "-o", "project2.o", "-c", "/home/john.doe/MyProject/project2.cpp"],
        },
        {
            "wd": "/home/john.doe/MyProjectB",
            "line": [
                "clang++",
                "-DFEATURE=1",
                "-o",
                "project2-feature.o",
                "-c",
                "/home/john.doe/MyProject/project2.cpp",
            ],
        },
    ]
    for i in range(len(cmds)):
        assert cmds[i].directory == expected[i]["wd"]
        for arg, exp in zip(cmds[i].arguments, expected[i]["line"]):
            assert arg == exp
Beispiel #4
0
    def test_all_compilecommand(self):
        """Check we get all results from the db"""
        cdb = CompilationDatabase.fromDirectory(kInputsDir)
        cmds = cdb.getAllCompileCommands()
        self.assertEqual(len(cmds), 3)
        expected = [
            { 'wd': '/home/john.doe/MyProject',
              'file': '/home/john.doe/MyProject/project.cpp',
              'line': ['clang++', '-o', 'project.o', '-c',
                       '/home/john.doe/MyProject/project.cpp']},
            { 'wd': '/home/john.doe/MyProjectA',
              'file': '/home/john.doe/MyProject/project2.cpp',
              'line': ['clang++', '-o', 'project2.o', '-c',
                       '/home/john.doe/MyProject/project2.cpp']},
            { 'wd': '/home/john.doe/MyProjectB',
              'file': '/home/john.doe/MyProject/project2.cpp',
              'line': ['clang++', '-DFEATURE=1', '-o', 'project2-feature.o', '-c',
                       '/home/john.doe/MyProject/project2.cpp']},

            ]
        for i in range(len(cmds)):
            self.assertEqual(cmds[i].directory, expected[i]['wd'])
            self.assertEqual(cmds[i].filename, expected[i]['file'])
            for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
                self.assertEqual(arg, exp)
Beispiel #5
0
 def test_2_compilecommand(self):
     """Check file with 2 compile commands"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp')
     self.assertEqual(len(cmds), 2)
     expected = [{
         'wd':
         '/home/john.doe/MyProjectA',
         'line': [
             'clang++', '--driver-mode=g++', '-o', 'project2.o', '-c',
             '/home/john.doe/MyProject/project2.cpp'
         ]
     }, {
         'wd':
         '/home/john.doe/MyProjectB',
         'line': [
             'clang++', '--driver-mode=g++', '-DFEATURE=1', '-o',
             'project2-feature.o', '-c',
             '/home/john.doe/MyProject/project2.cpp'
         ]
     }]
     for i in range(len(cmds)):
         self.assertEqual(cmds[i].directory, expected[i]['wd'])
         for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
             self.assertEqual(arg, exp)
Beispiel #6
0
 def test_compilationDB_references(self):
     """Ensure CompilationsCommands are independent of the database"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
     del cdb
     gc.collect()
     workingdir = cmds[0].directory
Beispiel #7
0
 def test_compilecommand_iterator_stops(self):
     """Check that iterator stops after the correct number of elements"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     count = 0
     for cmd in cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp'):
         count += 1
         self.assertLessEqual(count, 2)
Beispiel #8
0
    def test_all_compilecommand(self):
        """Check we get all results from the db"""
        cdb = CompilationDatabase.fromDirectory(kInputsDir)
        cmds = cdb.getAllCompileCommands()
        self.assertEqual(len(cmds), 3)
        expected = [
            { 'wd': '/home/john.doe/MyProject',
              'file': '/home/john.doe/MyProject/project.cpp',
              'line': ['clang++', '--driver-mode=g++', '-o', 'project.o', '-c',
                       '/home/john.doe/MyProject/project.cpp']},
            { 'wd': '/home/john.doe/MyProjectA',
              'file': '/home/john.doe/MyProject/project2.cpp',
              'line': ['clang++', '--driver-mode=g++', '-o', 'project2.o', '-c',
                       '/home/john.doe/MyProject/project2.cpp']},
            { 'wd': '/home/john.doe/MyProjectB',
              'file': '/home/john.doe/MyProject/project2.cpp',
              'line': ['clang++', '--driver-mode=g++', '-DFEATURE=1', '-o',
                       'project2-feature.o', '-c',
                       '/home/john.doe/MyProject/project2.cpp']},

            ]
        for i in range(len(cmds)):
            self.assertEqual(cmds[i].directory, expected[i]['wd'])
            self.assertEqual(cmds[i].filename, expected[i]['file'])
            for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
                self.assertEqual(arg, exp)
Beispiel #9
0
def test_2_compilecommand():
    """Check file with 2 compile commands"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp')
    assert len(cmds) == 2
    expected = [
        {
            'wd': b'/home/john.doe/MyProjectA',
            'line': [
                b'clang++',
                b'-o', b'project2.o',
                b'-c', b'/home/john.doe/MyProject/project2.cpp'
            ]
        },
        {
            'wd': b'/home/john.doe/MyProjectB',
            'line': [
                b'clang++',
                b'-DFEATURE=1',
                b'-o', b'project2-feature.o',
                b'-c', b'/home/john.doe/MyProject/project2.cpp'
            ]
        }
    ]
    for i in range(len(cmds)):
        assert cmds[i].directory == expected[i]['wd']
        for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
            assert arg == exp
Beispiel #10
0
def test_compilecommand_iterator_stops():
    """Check that iterator stops after the correct number of elements"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    count = 0
    for cmd in cdb.getCompileCommands("/home/john.doe/MyProject/project2.cpp"):
        count += 1
        assert count <= 2
Beispiel #11
0
def test_all_compilecommand():
    """Check we get all results from the db"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    cmds = cdb.getAllCompileCommands()
    assert len(cmds) == 3
    expected = [{
        'wd':
        '/home/john.doe/MyProjectA',
        'line': [
            'clang++', '-o', 'project2.o', '-c',
            '/home/john.doe/MyProject/project2.cpp'
        ]
    }, {
        'wd':
        '/home/john.doe/MyProjectB',
        'line': [
            'clang++', '-DFEATURE=1', '-o', 'project2-feature.o', '-c',
            '/home/john.doe/MyProject/project2.cpp'
        ]
    }, {
        'wd':
        '/home/john.doe/MyProject',
        'line': [
            'clang++', '-o', 'project.o', '-c',
            '/home/john.doe/MyProject/project.cpp'
        ]
    }]
    for i in range(len(cmds)):
        assert cmds[i].directory == expected[i]['wd']
        for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
            assert arg == exp
Beispiel #12
0
 def test_compilecommand_iterator_stops(self):
     """Check that iterator stops after the correct number of elements"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     count = 0
     for cmd in cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp'):
         count += 1
         self.assertLessEqual(count, 2)
Beispiel #13
0
 def test_create_fail(self):
     """Check we fail loading a database with an assertion"""
     path = os.path.dirname(__file__)
     with self.assertRaises(CompilationDatabaseError) as cm:
         cdb = CompilationDatabase.fromDirectory(path)
     e = cm.exception
     self.assertEqual(e.cdb_error,
         CompilationDatabaseError.ERROR_CANNOTLOADDATABASE)
Beispiel #14
0
 def test_create_fail(self):
     """Check we fail loading a database with an assertion"""
     path = os.path.dirname(__file__)
     with self.assertRaises(CompilationDatabaseError) as cm:
         cdb = CompilationDatabase.fromDirectory(path)
     e = cm.exception
     self.assertEqual(e.cdb_error,
                      CompilationDatabaseError.ERROR_CANNOTLOADDATABASE)
Beispiel #15
0
def initClangComplete(clang_complete_flags, clang_compilation_database,
                      library_path):
    global index

    debug = int(vim.eval("g:clang_debug")) == 1
    quiet = int(vim.eval("g:clang_quiet")) == 1

    if library_path:
        if os.path.isdir(library_path):
            Config.set_library_path(library_path)
        else:
            Config.set_library_file(library_path)

    Config.set_compatibility_check(False)

    try:
        index = Index.create()
    except Exception as e:
        if quiet:
            return 0
        if library_path:
            suggestion = "Are you sure '%s' contains libclang?" % library_path
        else:
            suggestion = "Consider setting g:clang_library_path."

        if debug:
            exception_msg = str(e)
        else:
            exception_msg = ''

        print('''Loading libclang failed, completion won't be available. %s
    %s
    ''' % (suggestion, exception_msg))
        return 0

    global builtinHeaderPath
    builtinHeaderPath = None
    if not canFindBuiltinHeaders(index):
        builtinHeaderPath = getBuiltinHeaderPath(library_path)

        if not builtinHeaderPath:
            print("WARNING: libclang can not find the builtin includes.")
            print("         This will cause slow code completion.")
            print("         Please report the problem.")

    global translationUnits
    translationUnits = dict()
    global complete_flags
    complete_flags = int(clang_complete_flags)
    global compilation_database
    if clang_compilation_database != '':
        compilation_database = CompilationDatabase.fromDirectory(
            clang_compilation_database)
    else:
        compilation_database = None
    global libclangLock
    libclangLock = threading.Lock()
    return 1
Beispiel #16
0
def test_1_compilecommand():
    """Check file with single compile command"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
    assert len(cmds) == 1
    assert cmds[0].directory == "/home/john.doe/MyProject"
    expected = ["clang++", "-o", "project.o", "-c", "/home/john.doe/MyProject/project.cpp"]
    for arg, exp in zip(cmds[0].arguments, expected):
        assert arg == exp
Beispiel #17
0
 def test_compilationCommands_references(self):
     """Ensure CompilationsCommand keeps a reference to CompilationCommands"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
     del cdb
     cmd0 = cmds[0]
     del cmds
     gc.collect()
     workingdir = cmd0.directory
Beispiel #18
0
def initClangComplete(clang_complete_flags, clang_compilation_database,
                      library_path):
  global index

  debug = int(vim.eval("g:clang_debug")) == 1
  quiet = int(vim.eval("g:clang_quiet")) == 1

  if library_path:
    if os.path.isdir(library_path):
      Config.set_library_path(library_path)
    else:
      Config.set_library_file(library_path)

  Config.set_compatibility_check(False)

  try:
    index = Index.create()
  except Exception as e:
    if quiet:
      return 0
    if library_path:
      suggestion = "Are you sure '%s' contains libclang?" % library_path
    else:
      suggestion = "Consider setting g:clang_library_path."

    if debug:
      exception_msg = str(e)
    else:
      exception_msg = ''

    print('''Loading libclang failed, completion won't be available. %s
    %s
    ''' % (suggestion, exception_msg))
    return 0

  global builtinHeaderPath
  builtinHeaderPath = None
  if not canFindBuiltinHeaders(index):
    builtinHeaderPath = getBuiltinHeaderPath(library_path)

    if not builtinHeaderPath:
      print("WARNING: libclang can not find the builtin includes.")
      print("         This will cause slow code completion.")
      print("         Please report the problem.")

  global translationUnits
  translationUnits = dict()
  global complete_flags
  complete_flags = int(clang_complete_flags)
  global compilation_database
  if clang_compilation_database != '':
    compilation_database = CompilationDatabase.fromDirectory(clang_compilation_database)
  else:
    compilation_database = None
  global libclangLock
  libclangLock = threading.Lock()
  return 1
Beispiel #19
0
 def test_compilationCommands_references(self):
     """Ensure CompilationsCommand keeps a reference to CompilationCommands"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
     del cdb
     cmd0 = cmds[0]
     del cmds
     gc.collect()
     workingdir = cmd0.directory
Beispiel #20
0
def test_1_compilecommand():
    """Check file with single compile command"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
    assert len(cmds) == 1
    assert cmds[0].directory == '/home/john.doe/MyProject'
    expected = [ 'clang++', '-o', 'project.o', '-c',
                 '/home/john.doe/MyProject/project.cpp']
    for arg, exp in zip(cmds[0].arguments, expected):
        assert arg == exp
Beispiel #21
0
def test_1_compilecommand():
    """Check file with single compile command"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
    assert len(cmds) == 1
    assert cmds[0].directory == '/home/john.doe/MyProject'
    expected = [ 'clang++', '-o', 'project.o', '-c',
                 '/home/john.doe/MyProject/project.cpp']
    for arg, exp in zip(cmds[0].arguments, expected):
        assert arg.spelling == exp
 def __init__(self, task_queue, max_contexts_num, max_path_len,
              max_subtokens_num, max_ast_depth, input_path, output_path):
     multiprocessing.Process.__init__(self)
     self.task_queue = task_queue
     self.parser = AstParser(max_contexts_num, max_path_len,
                             max_subtokens_num, max_ast_depth, output_path)
     try:
         self.compdb = CompilationDatabase.fromDirectory(input_path)
     except CompilationDatabaseError:
         self.compdb = None
Beispiel #23
0
 def test_1_compilecommand(self):
     """Check file with single compile command"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     file = '/home/john.doe/MyProject/project.cpp'
     cmds = cdb.getCompileCommands(file)
     self.assertEqual(len(cmds), 1)
     self.assertEqual(cmds[0].directory, os.path.dirname(file))
     self.assertEqual(cmds[0].filename, file)
     expected = [ 'clang++', '-o', 'project.o', '-c',
                  '/home/john.doe/MyProject/project.cpp']
     for arg, exp in zip(cmds[0].arguments, expected):
         self.assertEqual(arg, exp)
Beispiel #24
0
 def test_1_compilecommand(self):
     """Check file with single compile command"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     file = '/home/john.doe/MyProject/project.cpp'
     cmds = cdb.getCompileCommands(file)
     self.assertEqual(len(cmds), 1)
     self.assertEqual(cmds[0].directory, os.path.dirname(file))
     self.assertEqual(cmds[0].filename, file)
     expected = [ 'clang++', '--driver-mode=g++', '-o', 'project.o', '-c',
                  '/home/john.doe/MyProject/project.cpp']
     for arg, exp in zip(cmds[0].arguments, expected):
         self.assertEqual(arg, exp)
Beispiel #25
0
def get_comp_args(target_file: str,
                  compdb: cindex.CompilationDatabase) -> Iterator[str]:
    result = list()

    for db_cmd in compdb.getCompileCommands(target_file):
        result.extend(db_cmd.arguments)

    # Remove compiler.
    result = result[1:]

    # Remove filename itself.
    result = result[:len(result) - 1]

    # Parse all comments. Clang, by default, parses only doxygen.
    result.append("-fparse-all-comments")
    return result
Beispiel #26
0
    def test_create_fail(self):
        """Check we fail loading a database with an assertion"""
        path = os.path.dirname(__file__)

        # clang_CompilationDatabase_fromDirectory calls fprintf(stderr, ...)
        # Suppress its output.
        stderr = os.dup(2)
        with open(os.devnull, 'wb') as null:
            os.dup2(null.fileno(), 2)
        with self.assertRaises(CompilationDatabaseError) as cm:
            cdb = CompilationDatabase.fromDirectory(path)
        os.dup2(stderr, 2)
        os.close(stderr)

        e = cm.exception
        self.assertEqual(e.cdb_error,
            CompilationDatabaseError.ERROR_CANNOTLOADDATABASE)
Beispiel #27
0
    def test_create_fail(self):
        """Check we fail loading a database with an assertion"""
        path = os.path.dirname(__file__)

        # clang_CompilationDatabase_fromDirectory calls fprintf(stderr, ...)
        # Suppress its output.
        stderr = os.dup(2)
        with open(os.devnull, 'wb') as null:
            os.dup2(null.fileno(), 2)
        with self.assertRaises(CompilationDatabaseError) as cm:
            cdb = CompilationDatabase.fromDirectory(path)
        os.dup2(stderr, 2)
        os.close(stderr)

        e = cm.exception
        self.assertEqual(e.cdb_error,
            CompilationDatabaseError.ERROR_CANNOTLOADDATABASE)
Beispiel #28
0
 def test_2_compilecommand(self):
     """Check file with 2 compile commands"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project2.cpp')
     self.assertEqual(len(cmds), 2)
     expected = [
         { 'wd': '/home/john.doe/MyProjectA',
           'line': ['clang++', '-o', 'project2.o', '-c',
                    '/home/john.doe/MyProject/project2.cpp']},
         { 'wd': '/home/john.doe/MyProjectB',
           'line': ['clang++', '-DFEATURE=1', '-o', 'project2-feature.o', '-c',
                    '/home/john.doe/MyProject/project2.cpp']}
         ]
     for i in range(len(cmds)):
         self.assertEqual(cmds[i].directory, expected[i]['wd'])
         for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
             self.assertEqual(arg, exp)
Beispiel #29
0
def translation_units(compile_commands: cindex.CompilationDatabase, cache_path: Optional[str]) -> Iterator[typing.Union[cindex.TranslationUnit, SerializedTU]]:
    '''Returns an iterator over a translation unit for each file in the compilation database.'''
    compile_command: cindex.CompileCommand
    for compile_command in compile_commands.getAllCompileCommands():
        if cache_path:
            full_path = os.path.join(
                compile_command.directory,
                compile_command.filename,
            )
            serialized_tu = read_tu(
                cache_path,
                full_path,
            )
            modified_time = os.path.getmtime(full_path)
            if serialized_tu.serialization_time >= modified_time:
                log(LogLevel.INFO, f'Using cached analysis for {full_path}')
                yield serialized_tu
                continue

        log(
            LogLevel.INFO,
            f'parsing {compile_command.filename}'
        )
        try:
            if 'lua' in compile_command.filename:
                continue
            os.chdir(compile_command.directory)
            translation_unit = cindex.TranslationUnit.from_source(
                os.path.join(compile_command.directory,
                             compile_command.filename),
                args=[arg for arg in compile_command.arguments
                      if arg != compile_command.filename] + ['-I' + inc.decode() for inc in ccsyspath.system_include_paths('clang')],
            )
            for diag in translation_unit.diagnostics:
                log(
                    LogLevel.WARNING,
                    f'Parsing: {compile_command.filename}: {diag}'
                )
            yield translation_unit
        except cindex.TranslationUnitLoadError:
            log(
                LogLevel.WARNING,
                f'could not parse {os.path.join(compile_command.directory, compile_command.filename)}',
            )
Beispiel #30
0
    def init(self):

        clang_complete_flags = self.vim.eval('g:clang_complete_lib_flags')
        library_path = self.vim.eval('g:clang_library_path')
        clang_compilation_database = self.vim.eval(
            'g:clang_compilation_database')

        if library_path:
            if os.path.isdir(library_path):
                Config.set_library_path(library_path)
            else:
                Config.set_library_file(library_path)

        Config.set_compatibility_check(False)

        try:
            self.index = Index.create()
        except Exception as e:
            if library_path:
                suggestion = "Are you sure '%s' contains libclang?" % library_path
            else:
                suggestion = "Consider setting g:clang_library_path."

            logger.exception(
                "Loading libclang failed, completion won't be available. %s %s ",
                suggestion, exception_msg)
            return 0

        if not self.canFindBuiltinHeaders(self.index):
            self.builtinHeaderPath = self.getBuiltinHeaderPath(library_path)

            if not self.builtinHeaderPath:
                logger.warn("libclang find builtin header path failed: %s",
                            self.builtinHeaderPath)

        self.complete_flags = int(clang_complete_flags)
        if clang_compilation_database != '':
            self.compilation_database = CompilationDatabase.fromDirectory(
                clang_compilation_database)
        else:
            self.compilation_database = None
        return 1
Beispiel #31
0
def test_all_compilecommand():
    """Check we get all results from the db"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    cmds = cdb.getAllCompileCommands()
    assert len(cmds) == 3
    expected = [
        { 'wd': '/home/john.doe/MyProject',
          'line': ['clang++', '-o', 'project.o', '-c',
                   '/home/john.doe/MyProject/project.cpp']},
        { 'wd': '/home/john.doe/MyProjectA',
          'line': ['clang++', '-o', 'project2.o', '-c',
                   '/home/john.doe/MyProject/project2.cpp']},
        { 'wd': '/home/john.doe/MyProjectB',
          'line': ['clang++', '-DFEATURE=1', '-o', 'project2-feature.o', '-c',
                   '/home/john.doe/MyProject/project2.cpp']}
        ]
    for i in range(len(cmds)):
        assert cmds[i].directory == expected[i]['wd']
        for arg, exp in zip(cmds[i].arguments, expected[i]['line']):
            assert arg == exp
Beispiel #32
0
 def test_create(self):
     """Check we can load a compilation database"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
Beispiel #33
0
 def test_lookup_succeed(self):
     """Check we get some results if the file exists in the db"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
     self.assertNotEqual(len(cmds), 0)
Beispiel #34
0
 def test_lookup_succeed_pathlike(self):
     """Same as test_lookup_succeed, but with PathLikes"""
     cdb = CompilationDatabase.fromDirectory(str_to_path(kInputsDir))
     cmds = cdb.getCompileCommands(str_to_path('/home/john.doe/MyProject/project.cpp'))
     self.assertNotEqual(len(cmds), 0)
Beispiel #35
0
 def test_lookup_succeed(self):
     """Check we get some results if the file exists in the db"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     cmds = cdb.getCompileCommands('/home/john.doe/MyProject/project.cpp')
     self.assertNotEqual(len(cmds), 0)
Beispiel #36
0
 def test_create(self):
     """Check we can load a compilation database"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
Beispiel #37
0
 def test_lookup_succeed_pathlike(self):
     """Same as test_lookup_succeed, but with PathLikes"""
     cdb = CompilationDatabase.fromDirectory(str_to_path(kInputsDir))
     cmds = cdb.getCompileCommands(str_to_path('/home/john.doe/MyProject/project.cpp'))
     self.assertNotEqual(len(cmds), 0)
def main():
    from clang.cindex import Index, CompilationDatabase, CompilationDatabaseError, Cursor
    from pprint import pprint
    from optparse import OptionParser
    import re

    parser = OptionParser("usage: %prog [options] {filename} [clang-args*]")
    parser.add_option("",
                      "--anti-pattern",
                      dest="aPat",
                      help="Check if classes in file are FD anti-pattern",
                      action="store_true",
                      default=False)
    parser.add_option(
        "",
        "--compile-commands",
        dest="cComs",
        help=
        "Source file is compile_commands.json (full path) of the project to parse",
        action="store_true",
        default=False)
    parser.add_option(
        "",
        "--file-list",
        dest="fList",
        help="Source file is .txt file with the sources of the files to parse",
        action="store_true",
        default=False)

    parser.disable_interspersed_args()
    (opts, args) = parser.parse_args()
    if len(args) == 0:
        parser.error('invalid number arguments')
    input_file = args[0]
    input_args = args[1:]
    files_list = []
    if (opts.aPat):
        #from sklearn.svm import SVC, LinearSVC
        from sklearn.externals import joblib
        clf = joblib.load('classifier_params.pkl')
    if (opts.cComs):
        if input_file[len(input_file) - 21:] != 'compile_commands.json':
            pprint(('Invalid file: ' + input_file +
                    '. Should be compile_commands.json'))
            return
        ind = -1
        for i in xrange(len(input_file)):
            if input_file[i] == '/':
                ind = i

        path_dir = input_file[:ind + 1]
        # Step 1: load the compilation database
        compdb = CompilationDatabase.fromDirectory(path_dir)
        # Step 2: query compilation flags
        try:
            commands = compdb.getAllCompileCommands()
            for command in commands:
                comp_args = list(command.arguments)
                source_file = comp_args[len(comp_args) - 1]
                if source_file[len(source_file) - 4:] == '.cpp':
                    files_list.append({
                        'source_file':
                        source_file,
                        'file_args':
                        comp_args[:len(comp_args) - 1]
                    })
        except CompilationDatabaseError:
            pprint(('Could not load compilation flags for ' + source_file))

    elif (opts.fList):
        if input_file[len(input_file) - 4:] != '.txt':
            pprint(('Invalid file format: ' + input_file + '. Should be .txt'))
            return
        f = open(input_file, 'r')
        for line in f:
            if line.strip() == '':
                continue
            comp_args = re.split(' ', line.strip())
            source_file = comp_args[len(comp_args) - 1]
            if len(comp_args) == 1:
                source_args = input_args
            else:
                source_args = comp_args[:len(comp_args) - 1]
            if source_file[len(source_file) - 4:] == '.cpp':
                files_list.append({
                    'source_file': source_file,
                    'file_args': source_args
                })
    else:
        if input_file[len(input_file) - 4:] == '.cpp':
            files_list.append({
                'source_file': input_file,
                'file_args': input_args
            })
        else:
            pprint(('Invalid file format: ' + input_file + '. Should be .cpp'))
    import csv
    output_file_name = 'result.csv'
    with open(output_file_name, 'w') as csvfile:
        fieldnames = [
            'name', 'file_path', 'label', 'verb_in_name', 'template',
            'is_inherited', 'constructors', 'total_methods',
            'nonpublic_methods', 'static_methods', 'biggest_method_size',
            'total_fields', 'nonpublic_fields', 'fields_class_refs', 'getters',
            'setters', 'CG_edges', 'CG_no_entrance_vertexes',
            'CG_no_out_vertexes', 'CG_comp_num', 'CG_strong_comp_num'
        ]
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()

        index = Index.create()
        classes = {}
        handled_classes = set()
        not_defined_classes = set()
        for src_file in files_list:
            tu = index.parse(src_file['source_file'], src_file['file_args'])
            if not tu:
                parser.error("unable to load file")
            classes_defined_in_file = set()
            visit_classdecl(tu.cursor, classes, handled_classes,
                            not_defined_classes, classes_defined_in_file)
            for class_def in classes_defined_in_file:
                if classes[class_def].are_methods_defined:
                    not_defined_classes.discard(class_def)
                    if classes.get(class_def) is not None:
                        class_prec = classes.pop(class_def)
                        if (opts.aPat):
                            class_prec.mark_class(classify(class_prec, clf))
                        if (opts.cComs) | (opts.fList):
                            export_precedent(class_prec, writer)
                        else:
                            if class_prec.label == 1:
                                pprint(('Class ' + class_prec.name +
                                        ' in file ' + class_prec.file_name +
                                        ' is a FD anti-pattern.'))
                            elif class_prec.label == 0:
                                pprint(('Class ' + class_prec.name +
                                        ' in file ' + class_prec.file_name +
                                        ' is not a FD anti-pattern.'))
                            pprint((class_prec.name + ' : ',
                                    class_prec.get_print_info()))
                    handled_classes.add(class_def)
        for class_prec in classes:
            if (opts.aPat):
                classes[class_prec].mark_class(
                    classify(classes[class_prec], clf))
            if (opts.cComs) | (opts.fList):
                export_precedent(classes[class_prec], writer)
            else:
                if classes[class_prec].label == 1:
                    pprint(('Class ' + classes[class_prec].name + ' in file ' +
                            classes[class_prec].file_name +
                            ' is a FD anti-pattern.'))
                elif classes[class_prec].label == 0:
                    pprint(('Class ' + classes[class_prec].name + ' in file ' +
                            classes[class_prec].file_name +
                            ' is not a FD anti-pattern.'))
                pprint((classes[class_prec].name + ' : ',
                        classes[class_prec].get_print_info()))
Beispiel #39
0
def main():
    # This script should be run using filters and only those nodes who match the
    # filter types will be processed.
    #
    # Example:
    #   ./libclang.py CXX_METHOD FUNCTION_DECL
    filters = [getattr(CursorKind, arg) for arg in sys.argv]

    file_ext = vim.eval("expand('%:p:e')")
    filename = vim.eval("expand('%:p')")
    ext = file_ext if file_ext else vim.eval('&filetype')
    workdir = vim.eval("expand('%:p:h')")
    try:
        Config.set_library_file(vim.eval('g:doge_libclang_path'))
    except:
        pass

    try:
        args = vim.eval('g:doge_clang_args')
    except vim.error:
        args = []

    lines = vim.eval("getline(line(1), line('$'))")
    current_line = int(vim.eval("line('.')"))
    compilation_database_path = vim.eval("getcwd()")

    # Normalize the expression by transforming everything into 1 line.
    opener_pos = int(vim.eval("search('\m[{;]', 'n')"))
    normalized_expr = vim.eval(
        "join(map(getline(line('.'), {}), 'doge#helpers#trim(v:val)'), ' ')".
        format(opener_pos))
    del lines[current_line - 1:opener_pos]
    lines.insert(current_line - 1, normalized_expr)

    try:
        index = Index.create()
        compiledb = CompilationDatabase.fromDirectory(
            compilation_database_path)
        file_args = compiledb.getCompileCommands(filename)
        args = list(file_args[0].arguments)
        args = args[1:-1] + SYSTEM_INCLUDE
        tu = index.parse(filename,
                         args=args,
                         unsaved_files=[(filename, '\n'.join(lines))],
                         options=0x200)
        # tu = index.parse(filename, args=args, options=0x200)
        # print(list(tu.diagnostics))
        # print(filename, args)
        if tu:
            # print([(x.source, x.depth) for x in tu.get_includes()])
            with open('/tmp/libclang.log', 'w') as fp:
                node = find_node(tu.cursor, filename, current_line, fp, 0)
            if node and node.kind in filters:
                if node.kind == CursorKind.FIELD_DECL:
                    print_field_decl(node)
                elif node.kind == CursorKind.STRUCT_DECL:
                    print_struct_decl(node)
                elif node.kind in [
                        CursorKind.CONSTRUCTOR, CursorKind.CXX_METHOD,
                        CursorKind.FUNCTION_DECL, CursorKind.FUNCTION_TEMPLATE,
                        CursorKind.CLASS_TEMPLATE
                ]:
                    print_func_decl(node)
    except Exception as e:
        print(type(e), e)
        import traceback
        traceback.print_exc
    finally:
        pass
Beispiel #40
0
def test_lookup_succeed():
    """Check we get some results if the file exists in the db"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    cmds = cdb.getCompileCommands("/home/john.doe/MyProject/project.cpp")
    assert len(cmds) != 0
Beispiel #41
0
def test_lookup_fail():
    """Check file lookup failure"""
    cdb = CompilationDatabase.fromDirectory(kInputsDir)
    assert cdb.getCompileCommands('file_do_not_exist.cpp') == None
 def test_lookup_fail(self):
     """Check file lookup failure"""
     cdb = CompilationDatabase.fromDirectory(kInputsDir)
     self.assertIsNone(cdb.getCompileCommands('file_do_not_exist.cpp'))